Skip to content

Commit 1063d4a

Browse files
committed
Merge branch 'master' into hkj-jwt-sign
2 parents 7e38847 + d44ce49 commit 1063d4a

File tree

10 files changed

+251
-377
lines changed

10 files changed

+251
-377
lines changed

src/auth/auth-api-request.ts

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {deepCopy} from '../utils/deep-copy';
2020
import {FirebaseApp} from '../firebase-app';
2121
import {AuthClientErrorCode, FirebaseAuthError, FirebaseError} from '../utils/error';
2222
import {
23-
HttpMethod, SignedApiRequestHandler, ApiSettings,
23+
ApiSettings, AuthorizedHttpClient, HttpRequestConfig, HttpError,
2424
} from '../utils/api-request';
2525
import {CreateRequest, UpdateRequest} from './user-record';
2626
import {
@@ -429,12 +429,8 @@ export const FIREBASE_AUTH_SIGN_UP_NEW_USER = new ApiSettings('signupNewUser', '
429429
* Class that provides mechanism to send requests to the Firebase Auth backend endpoints.
430430
*/
431431
export class FirebaseAuthRequestHandler {
432-
private host: string = FIREBASE_AUTH_HOST;
433-
private port: number = FIREBASE_AUTH_PORT;
434-
private path: string = FIREBASE_AUTH_PATH;
435-
private headers: object = FIREBASE_AUTH_HEADER;
436-
private timeout: number = FIREBASE_AUTH_TIMEOUT;
437-
private signedApiRequestHandler: SignedApiRequestHandler;
432+
private baseUrl: string = `https://${FIREBASE_AUTH_HOST}${FIREBASE_AUTH_PATH}`;
433+
private httpClient: AuthorizedHttpClient;
438434

439435
/**
440436
* @param {any} response The response to check for errors.
@@ -449,7 +445,7 @@ export class FirebaseAuthRequestHandler {
449445
* @constructor
450446
*/
451447
constructor(app: FirebaseApp) {
452-
this.signedApiRequestHandler = new SignedApiRequestHandler(app);
448+
this.httpClient = new AuthorizedHttpClient(app);
453449
}
454450

455451
/**
@@ -805,38 +801,35 @@ export class FirebaseAuthRequestHandler {
805801
* @return {Promise<object>} A promise that resolves with the response.
806802
*/
807803
private invokeRequestHandler(apiSettings: ApiSettings, requestData: object): Promise<object> {
808-
const path: string = this.path + apiSettings.getEndpoint();
809-
const httpMethod: HttpMethod = apiSettings.getHttpMethod();
810804
return Promise.resolve()
811805
.then(() => {
812806
// Validate request.
813807
const requestValidator = apiSettings.getRequestValidator();
814808
requestValidator(requestData);
815809
// Process request.
816-
return this.signedApiRequestHandler.sendRequest(
817-
this.host, this.port, path, httpMethod, requestData, this.headers, this.timeout);
810+
const req: HttpRequestConfig = {
811+
method: apiSettings.getHttpMethod(),
812+
url: `${this.baseUrl}${apiSettings.getEndpoint()}`,
813+
headers: FIREBASE_AUTH_HEADER,
814+
data: requestData,
815+
timeout: FIREBASE_AUTH_TIMEOUT,
816+
};
817+
return this.httpClient.send(req);
818818
})
819819
.then((response) => {
820-
// Check for backend errors in the response.
821-
const errorCode = FirebaseAuthRequestHandler.getErrorCode(response);
822-
if (errorCode) {
823-
throw FirebaseAuthError.fromServerError(errorCode, /* message */ undefined, response);
824-
}
825820
// Validate response.
826821
const responseValidator = apiSettings.getResponseValidator();
827-
responseValidator(response);
822+
responseValidator(response.data);
828823
// Return entire response.
829-
return response;
824+
return response.data;
830825
})
831-
.catch((response) => {
832-
const error = (typeof response === 'object' && 'statusCode' in response) ?
833-
response.error : response;
834-
if (error instanceof FirebaseError) {
835-
throw error;
826+
.catch((err) => {
827+
if (err instanceof HttpError) {
828+
const error = err.response.data;
829+
const errorCode = FirebaseAuthRequestHandler.getErrorCode(error);
830+
throw FirebaseAuthError.fromServerError(errorCode, /* message */ undefined, error);
836831
}
837-
838-
const errorCode = FirebaseAuthRequestHandler.getErrorCode(error);
839-
throw FirebaseAuthError.fromServerError(errorCode, /* message */ undefined, error);
832+
throw err;
840833
});
841834
}
842835
}

src/index.d.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,18 +439,17 @@ declare namespace admin.messaging {
439439
title?: string;
440440
body?: string;
441441
};
442-
442+
443443
type WebpushConfig = {
444444
headers?: {[key: string]: string};
445445
data?: {[key: string]: string};
446446
notification?: WebpushNotification;
447447
};
448-
449-
type WebpushNotification = {
448+
449+
interface WebpushNotification extends NotificationOptions {
450450
title?: string;
451-
body?: string;
452-
icon?: string;
453-
};
451+
[key: string]: any;
452+
}
454453

455454
type DataMessagePayload = {
456455
[key: string]: string;

src/messaging/messaging.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,9 @@ export interface WebpushConfig {
129129
notification?: WebpushNotification;
130130
}
131131

132-
export interface WebpushNotification {
132+
export interface WebpushNotification extends NotificationOptions {
133133
title?: string;
134-
body?: string;
135-
icon?: string;
134+
[key: string]: any;
136135
}
137136

138137
export interface ApnsConfig {

src/utils/api-request.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,11 @@ class DefaultHttpResponse implements HttpResponse {
111111
}
112112

113113
export class HttpError extends Error {
114-
115-
public readonly response: HttpResponse;
116-
117-
constructor(resp: LowLevelResponse) {
118-
super(`Server responded with status ${resp.status}.`);
119-
this.response = new DefaultHttpResponse(resp);
114+
constructor(public readonly response: HttpResponse) {
115+
super(`Server responded with status ${response.status}.`);
116+
// Set the prototype so that instanceof checks will work correctly.
117+
// See: https://github.com/Microsoft/TypeScript/issues/13965
118+
Object.setPrototypeOf(this, HttpError.prototype);
120119
}
121120
}
122121

@@ -153,7 +152,7 @@ export class HttpClient {
153152
return this.sendWithRetry(config, attempts + 1);
154153
}
155154
if (err.response) {
156-
throw new HttpError(err.response);
155+
throw new HttpError(new DefaultHttpResponse(err.response));
157156
}
158157
if (err.code === 'ETIMEDOUT') {
159158
throw new FirebaseAppError(

0 commit comments

Comments
 (0)