-
Notifications
You must be signed in to change notification settings - Fork 417
Expand file tree
/
Copy pathmessaging-api-request-internal.ts
More file actions
315 lines (294 loc) · 10.7 KB
/
Copy pathmessaging-api-request-internal.ts
File metadata and controls
315 lines (294 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*!
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { App } from '../app';
import { FirebaseApp } from '../app/firebase-app';
import {
HttpMethod, AuthorizedHttpClient, HttpRequestConfig, RequestResponseError, RequestResponse,
AuthorizedHttp2Client, Http2SessionHandler, Http2RequestConfig,
} from '../utils/api-request';
import { createFirebaseError, getErrorCode } from './messaging-errors-internal';
import { getSdkVersion } from '../utils/index';
import { SendResponse } from './messaging-api';
import * as validator from '../utils/validator';
import { FirebaseMessagingError } from './error';
export interface TopicSubscriptionResponse {
success: boolean;
error?: FirebaseMessagingError;
}
// FCM backend constants
const FIREBASE_MESSAGING_TIMEOUT = 15000;
const FIREBASE_MESSAGING_HTTP_METHOD: HttpMethod = 'POST';
const FIREBASE_MESSAGING_HEADERS = {
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
'access_token_auth': 'true',
};
/**
* Class that provides a mechanism to send requests to the Firebase Cloud Messaging backend.
*/
export class FirebaseMessagingRequestHandler {
private readonly httpClient: AuthorizedHttpClient;
private readonly http2Client: AuthorizedHttp2Client;
/**
* @param app - The app used to fetch access tokens to sign API requests.
* @constructor
*/
constructor(app: App) {
this.httpClient = new AuthorizedHttpClient(app as FirebaseApp);
this.http2Client = new AuthorizedHttp2Client(app as FirebaseApp);
}
/**
* Invokes the request handler with the provided request data.
*
* @param host - The host to which to send the request.
* @param path - The path to which to send the request.
* @param requestData - The request data.
* @returns A promise that resolves with the response.
*/
public invokeRequestHandler(host: string, path: string, requestData: object): Promise<object> {
const request: HttpRequestConfig = {
method: FIREBASE_MESSAGING_HTTP_METHOD,
url: `https://${host}${path}`,
data: requestData,
headers: FIREBASE_MESSAGING_HEADERS,
timeout: FIREBASE_MESSAGING_TIMEOUT,
};
return this.httpClient.send(request).then((response) => {
// Send non-JSON responses to the catch() below where they will be treated as errors.
if (!response.isJson()) {
throw new RequestResponseError(response);
}
// Check for backend errors in the response.
const errorCode = getErrorCode(response.data);
if (errorCode) {
throw new RequestResponseError(response);
}
// Return entire response.
return response.data;
})
.catch((err) => {
if (err instanceof RequestResponseError) {
throw createFirebaseError(err);
}
// Re-throw the error if it already has the proper format.
throw err;
});
}
/**
* Invokes the HTTP/1.1 request handler with the provided request data.
*
* @param host - The host to which to send the request.
* @param path - The path to which to send the request.
* @param requestData - The request data.
* @returns A promise that resolves with the {@link SendResponse}.
*/
public invokeHttpRequestHandlerForSendResponse(
host: string, path: string, requestData: object
): Promise<SendResponse> {
const request: HttpRequestConfig = {
method: FIREBASE_MESSAGING_HTTP_METHOD,
url: `https://${host}${path}`,
data: requestData,
headers: FIREBASE_MESSAGING_HEADERS,
timeout: FIREBASE_MESSAGING_TIMEOUT,
};
return this.httpClient.send(request).then((response) => {
return this.buildSendResponse(response);
})
.catch((err) => {
if (err instanceof RequestResponseError) {
return this.buildSendResponseFromError(err);
}
// Re-throw the error if it already has the proper format.
throw err;
});
}
/**
* Invokes the HTTP/2 request handler with the provided request data.
*
* @param host - The host to which to send the request.
* @param path - The path to which to send the request.
* @param requestData - The request data.
* @returns A promise that resolves with the {@link SendResponse}.
*/
public invokeHttp2RequestHandlerForSendResponse(
host: string, path: string, requestData: object, http2SessionHandler: Http2SessionHandler
): Promise<SendResponse> {
const request: Http2RequestConfig = {
method: FIREBASE_MESSAGING_HTTP_METHOD,
url: `https://${host}${path}`,
data: requestData,
headers: FIREBASE_MESSAGING_HEADERS,
timeout: FIREBASE_MESSAGING_TIMEOUT,
http2SessionHandler: http2SessionHandler
};
return this.http2Client.send(request).then((response) => {
return this.buildSendResponse(response);
})
.catch((err) => {
if (err instanceof RequestResponseError) {
return this.buildSendResponseFromError(err);
}
// Re-throw the error if it already has the proper format.
throw err;
});
}
private buildSendResponse(response: RequestResponse): SendResponse {
const result: SendResponse = {
success: response.status === 200,
};
if (result.success) {
result.messageId = response.data.name;
} else {
result.error = createFirebaseError(new RequestResponseError(response));
}
return result;
}
private buildSendResponseFromError(err: RequestResponseError): SendResponse {
return {
success: false,
error: createFirebaseError(err)
};
}
/**
* Invokes the HTTP/1.1 request handler for a topic management operation.
*
* @param host - The host to which to send the request.
* @param path - The path to which to send the request.
* @param methodName - The name of the calling method (subscribeToTopic or unsubscribeFromTopic).
* @param requestData - The request data (or undefined for DELETE).
* @returns A promise that resolves with the {@link TopicSubscriptionResponse}.
*/
public invokeHttpRequestHandlerForTopicSubscriptionResponse(
host: string, path: string, methodName: string, requestData?: object
): Promise<TopicSubscriptionResponse> {
const request: HttpRequestConfig = {
method: methodName === 'subscribeToTopic' ? 'POST' : 'DELETE',
url: `https://${host}${path}`,
data: requestData,
headers: FIREBASE_MESSAGING_HEADERS,
timeout: FIREBASE_MESSAGING_TIMEOUT,
};
return this.httpClient.send(request).then((response) => {
return this.buildTopicSubscriptionResponse(response, methodName);
})
.catch((err) => {
if (err instanceof RequestResponseError) {
return this.buildTopicSubscriptionResponseFromError(err, methodName);
}
// Re-throw the error if it already has the proper format.
throw err;
});
}
/**
* Invokes the HTTP/2 request handler for a topic management operation.
*
* @param host - The host to which to send the request.
* @param path - The path to which to send the request.
* @param methodName - The name of the calling method (subscribeToTopic or unsubscribeFromTopic).
* @param requestData - The request data (or undefined for DELETE).
* @param http2SessionHandler - The HTTP/2 session handler.
* @returns A promise that resolves with the {@link TopicSubscriptionResponse}.
*/
public invokeHttp2RequestHandlerForTopicSubscriptionResponse(
host: string, path: string, methodName: string, requestData: object | undefined, http2SessionHandler: Http2SessionHandler
): Promise<TopicSubscriptionResponse> {
const request: Http2RequestConfig = {
method: methodName === 'subscribeToTopic' ? 'POST' : 'DELETE',
url: `https://${host}${path}`,
data: requestData,
headers: FIREBASE_MESSAGING_HEADERS,
timeout: FIREBASE_MESSAGING_TIMEOUT,
http2SessionHandler: http2SessionHandler
};
return this.http2Client.send(request).then((response) => {
return this.buildTopicSubscriptionResponse(response, methodName);
})
.catch((err) => {
if (err instanceof RequestResponseError) {
return this.buildTopicSubscriptionResponseFromError(err, methodName);
}
// Re-throw the error if it already has the proper format.
throw err;
});
}
private buildTopicSubscriptionResponse(
response: RequestResponse, methodName: string
): TopicSubscriptionResponse {
if (response.status === 200) {
return { success: true };
}
return this.buildTopicSubscriptionResponseFromError(new RequestResponseError(response), methodName);
}
private buildTopicSubscriptionResponseFromError(
err: RequestResponseError, methodName: string
): TopicSubscriptionResponse {
if (err.response.isJson()) {
const json = err.response.data;
const errorCode = getErrorCode(json);
if (methodName === 'subscribeToTopic' && (errorCode === 'ALREADY_EXISTS' || err.response.status === 409)) {
return { success: true };
}
const errorMessage = (validator.isNonNullObject(json) && 'error' in json && validator.isNonEmptyString((json as any).error.message))
? (json as any).error.message
: undefined;
return {
success: false,
error: FirebaseMessagingError.fromTopicManagementServerError(
errorCode || 'UNKNOWN_ERROR',
errorMessage,
err,
),
};
}
// Non-JSON response
if (methodName === 'subscribeToTopic' && err.response.status === 409) {
return { success: true };
}
let serverErrorCode = 'UNKNOWN_ERROR';
switch (err.response.status) {
case 400:
serverErrorCode = 'INVALID_ARGUMENT';
break;
case 401:
case 403:
serverErrorCode = 'PERMISSION_DENIED';
break;
case 404:
serverErrorCode = 'NOT_FOUND';
break;
case 429:
serverErrorCode = 'RESOURCE_EXHAUSTED';
break;
case 500:
serverErrorCode = 'INTERNAL';
break;
case 503:
serverErrorCode = 'DEADLINE_EXCEEDED';
break;
default:
serverErrorCode = 'UNKNOWN_ERROR';
}
return {
success: false,
error: FirebaseMessagingError.fromTopicManagementServerError(
serverErrorCode,
`Server responded with status ${err.response.status}.`,
err,
),
};
}
}