Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/database/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ const RULES_URL_PATH = '.settings/rules.json';
class DatabaseRulesClient {

private readonly dbUrl: string;
private readonly httpClient: AuthorizedHttpClient;
private readonly httpClient: DatabaseRulesHttpClient;

constructor(app: App, dbUrl: string) {
let parsedUrl = new URL(dbUrl);
Expand All @@ -208,7 +208,7 @@ class DatabaseRulesClient {

parsedUrl.pathname = path.join(parsedUrl.pathname, RULES_URL_PATH);
this.dbUrl = parsedUrl.toString();
this.httpClient = new AuthorizedHttpClient(app as FirebaseApp);
this.httpClient = new DatabaseRulesHttpClient(app as FirebaseApp);
}

/**
Expand Down Expand Up @@ -316,6 +316,16 @@ class DatabaseRulesClient {
}
}

class DatabaseRulesHttpClient extends AuthorizedHttpClient {
protected getToken(): Promise<string> {
const emulatorHost = process.env.FIREBASE_DATABASE_EMULATOR_HOST;
if (emulatorHost) {
return Promise.resolve('owner');
}
return super.getToken();
}
}
Comment on lines +319 to +327

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve readability and maintainability, it's a good practice to avoid magic strings. Consider defining 'owner' as a private static constant within the class.

class DatabaseRulesHttpClient extends AuthorizedHttpClient {
  private static readonly EMULATOR_AUTH_TOKEN = 'owner';

  protected getToken(): Promise<string> {
    const emulatorHost = process.env.FIREBASE_DATABASE_EMULATOR_HOST;
    if (emulatorHost) {
      return Promise.resolve(DatabaseRulesHttpClient.EMULATOR_AUTH_TOKEN);
    }
    return super.getToken();
  }
}


function extractNamespace(parsedUrl: URL): string {
const ns = parsedUrl.searchParams.get('ns');
if (ns) {
Expand Down