Skip to content

Commit 8f8a793

Browse files
committed
Improve code readability for applying tokens.
1 parent 0cab393 commit 8f8a793

File tree

3 files changed

+20
-33
lines changed

3 files changed

+20
-33
lines changed

packages/firestore/src/api/credentials.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ export interface Token {
6868
*/
6969
user?: User;
7070

71-
/** Invokes the given callback with the token's key and value. */
72-
applyHeaders(callback: (tokenKey: string, tokenValue: string) => void): void;
71+
/** Header values to set for this token */
72+
headers: Map<string, string>;
7373
}
7474

7575
export class OAuthToken implements Token {
7676
type = 'OAuth' as TokenType;
77-
constructor(private value: string, public user: User) {}
77+
headers = new Map();
7878

79-
applyHeaders(callback: (tokenKey: string, tokenValue: string) => void): void {
80-
callback('Authorization', `Bearer ${this.value}`);
79+
constructor(value: string, public user: User) {
80+
this.headers.set('Authorization', `Bearer ${value}`);
8181
}
8282
}
8383

@@ -395,21 +395,16 @@ interface Gapi {
395395
export class FirstPartyToken implements Token {
396396
type = 'FirstParty' as TokenType;
397397
user = User.FIRST_PARTY;
398+
headers = new Map();
398399

399-
constructor(
400-
private gapi: Gapi,
401-
private sessionIndex: string,
402-
private iamToken: string | null
403-
) {}
404-
405-
applyHeaders(callback: (tokenKey: string, tokenValue: string) => void): void {
406-
callback('X-Goog-AuthUser', this.sessionIndex);
407-
const authHeader = this.gapi['auth']['getAuthHeaderValueForFirstParty']([]);
400+
constructor(gapi: Gapi, sessionIndex: string, iamToken: string | null) {
401+
this.headers.set('X-Goog-AuthUser', sessionIndex);
402+
const authHeader = gapi['auth']['getAuthHeaderValueForFirstParty']([]);
408403
if (authHeader) {
409-
callback('Authorization', authHeader);
404+
this.headers.set('Authorization', authHeader);
410405
}
411-
if (this.iamToken) {
412-
callback('X-Goog-Iam-Authorization-Token', this.iamToken);
406+
if (iamToken) {
407+
this.headers.set('X-Goog-Iam-Authorization-Token', iamToken);
413408
}
414409
}
415410
}
@@ -449,11 +444,11 @@ export class FirstPartyAuthCredentialsProvider
449444

450445
export class AppCheckToken implements Token {
451446
type = 'AppCheck' as TokenType;
452-
constructor(private value: string) {}
447+
headers = new Map();
453448

454-
applyHeaders(callback: (tokenKey: string, tokenValue: string) => void): void {
455-
if (this.value && this.value.length > 0) {
456-
callback('x-firebase-appcheck', this.value);
449+
constructor(private value: string) {
450+
if (value && value.length > 0) {
451+
this.headers.set('x-firebase-appcheck', this.value);
457452
}
458453
}
459454
}

packages/firestore/src/platform/node/grpc_connection.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,10 @@ function createMetadata(
5959
);
6060
const metadata = new Metadata();
6161
if (authToken) {
62-
authToken.applyHeaders((tokenKey, tokenValue) =>
63-
metadata.set(tokenKey, tokenValue)
64-
);
62+
authToken.headers.forEach((value, key) => metadata.set(key, value));
6563
}
6664
if (appCheckToken) {
67-
appCheckToken.applyHeaders((tokenKey, tokenValue) =>
68-
metadata.set(tokenKey, tokenValue)
69-
);
65+
appCheckToken.headers.forEach((value, key) => metadata.set(key, value));
7066
}
7167
if (appId) {
7268
metadata.set('X-Firebase-GMPID', appId);

packages/firestore/src/remote/rest_connection.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,10 @@ export abstract class RestConnection implements Connection {
145145
}
146146

147147
if (authToken) {
148-
authToken.applyHeaders(
149-
(tokenKey, tokenValue) => (headers[tokenKey] = tokenValue)
150-
);
148+
authToken.headers.forEach((value, key) => (headers[key] = value));
151149
}
152150
if (appCheckToken) {
153-
appCheckToken.applyHeaders(
154-
(tokenKey, tokenValue) => (headers[tokenKey] = tokenValue)
155-
);
151+
appCheckToken.headers.forEach((value, key) => (headers[key] = value));
156152
}
157153
}
158154

0 commit comments

Comments
 (0)