1 |
|
2 | "use strict";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | Object.defineProperty(exports, "__esModule", { value: true });
|
20 | exports.FirebaseMessagingRequestHandler = void 0;
|
21 | const api_request_1 = require("../utils/api-request");
|
22 | const messaging_errors_internal_1 = require("./messaging-errors-internal");
|
23 | const batch_request_internal_1 = require("./batch-request-internal");
|
24 | const index_1 = require("../utils/index");
|
25 |
|
26 | const FIREBASE_MESSAGING_TIMEOUT = 15000;
|
27 | const FIREBASE_MESSAGING_BATCH_URL = 'https://fcm.googleapis.com/batch';
|
28 | const FIREBASE_MESSAGING_HTTP_METHOD = 'POST';
|
29 | const FIREBASE_MESSAGING_HEADERS = {
|
30 | 'X-Firebase-Client': `fire-admin-node/${(0, index_1.getSdkVersion)()}`,
|
31 | };
|
32 | const LEGACY_FIREBASE_MESSAGING_HEADERS = {
|
33 | 'X-Firebase-Client': `fire-admin-node/${(0, index_1.getSdkVersion)()}`,
|
34 | 'access_token_auth': 'true',
|
35 | };
|
36 |
|
37 |
|
38 |
|
39 | class FirebaseMessagingRequestHandler {
|
40 | |
41 |
|
42 |
|
43 |
|
44 | constructor(app) {
|
45 | this.httpClient = new api_request_1.AuthorizedHttpClient(app);
|
46 | this.batchClient = new batch_request_internal_1.BatchRequestClient(this.httpClient, FIREBASE_MESSAGING_BATCH_URL, FIREBASE_MESSAGING_HEADERS);
|
47 | }
|
48 | |
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | invokeRequestHandler(host, path, requestData) {
|
57 | const request = {
|
58 | method: FIREBASE_MESSAGING_HTTP_METHOD,
|
59 | url: `https://${host}${path}`,
|
60 | data: requestData,
|
61 | headers: LEGACY_FIREBASE_MESSAGING_HEADERS,
|
62 | timeout: FIREBASE_MESSAGING_TIMEOUT,
|
63 | };
|
64 | return this.httpClient.send(request).then((response) => {
|
65 |
|
66 | if (!response.isJson()) {
|
67 | throw new api_request_1.HttpError(response);
|
68 | }
|
69 |
|
70 | const errorCode = (0, messaging_errors_internal_1.getErrorCode)(response.data);
|
71 | if (errorCode) {
|
72 | throw new api_request_1.HttpError(response);
|
73 | }
|
74 |
|
75 | return response.data;
|
76 | })
|
77 | .catch((err) => {
|
78 | if (err instanceof api_request_1.HttpError) {
|
79 | throw (0, messaging_errors_internal_1.createFirebaseError)(err);
|
80 | }
|
81 |
|
82 | throw err;
|
83 | });
|
84 | }
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | invokeRequestHandlerForSendResponse(host, path, requestData) {
|
94 | const request = {
|
95 | method: FIREBASE_MESSAGING_HTTP_METHOD,
|
96 | url: `https://${host}${path}`,
|
97 | data: requestData,
|
98 | headers: LEGACY_FIREBASE_MESSAGING_HEADERS,
|
99 | timeout: FIREBASE_MESSAGING_TIMEOUT,
|
100 | };
|
101 | return this.httpClient.send(request).then((response) => {
|
102 | return this.buildSendResponse(response);
|
103 | })
|
104 | .catch((err) => {
|
105 | if (err instanceof api_request_1.HttpError) {
|
106 | return this.buildSendResponseFromError(err);
|
107 | }
|
108 |
|
109 | throw err;
|
110 | });
|
111 | }
|
112 | |
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | sendBatchRequest(requests) {
|
120 | return this.batchClient.send(requests)
|
121 | .then((responses) => {
|
122 | return responses.map((part) => {
|
123 | return this.buildSendResponse(part);
|
124 | });
|
125 | }).then((responses) => {
|
126 | const successCount = responses.filter((resp) => resp.success).length;
|
127 | return {
|
128 | responses,
|
129 | successCount,
|
130 | failureCount: responses.length - successCount,
|
131 | };
|
132 | }).catch((err) => {
|
133 | if (err instanceof api_request_1.HttpError) {
|
134 | throw (0, messaging_errors_internal_1.createFirebaseError)(err);
|
135 | }
|
136 |
|
137 | throw err;
|
138 | });
|
139 | }
|
140 | buildSendResponse(response) {
|
141 | const result = {
|
142 | success: response.status === 200,
|
143 | };
|
144 | if (result.success) {
|
145 | result.messageId = response.data.name;
|
146 | }
|
147 | else {
|
148 | result.error = (0, messaging_errors_internal_1.createFirebaseError)(new api_request_1.HttpError(response));
|
149 | }
|
150 | return result;
|
151 | }
|
152 | buildSendResponseFromError(err) {
|
153 | return {
|
154 | success: false,
|
155 | error: (0, messaging_errors_internal_1.createFirebaseError)(err)
|
156 | };
|
157 | }
|
158 | }
|
159 | exports.FirebaseMessagingRequestHandler = FirebaseMessagingRequestHandler;
|