123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.Messaging = void 0;
- const deep_copy_1 = require("../utils/deep-copy");
- const error_1 = require("../utils/error");
- const utils = require("../utils");
- const validator = require("../utils/validator");
- const messaging_internal_1 = require("./messaging-internal");
- const messaging_api_request_internal_1 = require("./messaging-api-request-internal");
- const FCM_SEND_HOST = 'fcm.googleapis.com';
- const FCM_SEND_PATH = '/fcm/send';
- const FCM_TOPIC_MANAGEMENT_HOST = 'iid.googleapis.com';
- const FCM_TOPIC_MANAGEMENT_ADD_PATH = '/iid/v1:batchAdd';
- const FCM_TOPIC_MANAGEMENT_REMOVE_PATH = '/iid/v1:batchRemove';
- const FCM_MAX_BATCH_SIZE = 500;
- const CAMELCASED_NOTIFICATION_PAYLOAD_KEYS_MAP = {
- bodyLocArgs: 'body_loc_args',
- bodyLocKey: 'body_loc_key',
- clickAction: 'click_action',
- titleLocArgs: 'title_loc_args',
- titleLocKey: 'title_loc_key',
- };
- const CAMELCASE_OPTIONS_KEYS_MAP = {
- dryRun: 'dry_run',
- timeToLive: 'time_to_live',
- collapseKey: 'collapse_key',
- mutableContent: 'mutable_content',
- contentAvailable: 'content_available',
- restrictedPackageName: 'restricted_package_name',
- };
- const MESSAGING_DEVICE_RESULT_KEYS_MAP = {
- message_id: 'messageId',
- registration_id: 'canonicalRegistrationToken',
- };
- const MESSAGING_DEVICES_RESPONSE_KEYS_MAP = {
- canonical_ids: 'canonicalRegistrationTokenCount',
- failure: 'failureCount',
- success: 'successCount',
- multicast_id: 'multicastId',
- };
- const MESSAGING_DEVICE_GROUP_RESPONSE_KEYS_MAP = {
- success: 'successCount',
- failure: 'failureCount',
- failed_registration_ids: 'failedRegistrationTokens',
- };
- const MESSAGING_TOPIC_RESPONSE_KEYS_MAP = {
- message_id: 'messageId',
- };
- const MESSAGING_CONDITION_RESPONSE_KEYS_MAP = {
- message_id: 'messageId',
- };
- function mapRawResponseToDevicesResponse(response) {
-
- utils.renameProperties(response, MESSAGING_DEVICES_RESPONSE_KEYS_MAP);
- if ('results' in response) {
- response.results.forEach((messagingDeviceResult) => {
- utils.renameProperties(messagingDeviceResult, MESSAGING_DEVICE_RESULT_KEYS_MAP);
-
- if ('error' in messagingDeviceResult) {
- const newError = error_1.FirebaseMessagingError.fromServerError(messagingDeviceResult.error, undefined, messagingDeviceResult.error);
- messagingDeviceResult.error = newError;
- }
- });
- }
- return response;
- }
- function mapRawResponseToDeviceGroupResponse(response) {
-
- utils.renameProperties(response, MESSAGING_DEVICE_GROUP_RESPONSE_KEYS_MAP);
-
-
- response.failedRegistrationTokens = response.failedRegistrationTokens || [];
- return response;
- }
- function mapRawResponseToTopicManagementResponse(response) {
-
- const result = {
- successCount: 0,
- failureCount: 0,
- errors: [],
- };
- if ('results' in response) {
- response.results.forEach((tokenManagementResult, index) => {
-
- if ('error' in tokenManagementResult) {
- result.failureCount += 1;
- const newError = error_1.FirebaseMessagingError.fromTopicManagementServerError(tokenManagementResult.error, undefined, tokenManagementResult.error);
- result.errors.push({
- index,
- error: newError,
- });
- }
- else {
- result.successCount += 1;
- }
- });
- }
- return result;
- }
- class Messaging {
-
- constructor(app) {
- if (!validator.isNonNullObject(app) || !('options' in app)) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_ARGUMENT, 'First argument passed to admin.messaging() must be a valid Firebase app instance.');
- }
- this.appInternal = app;
- this.messagingRequestHandler = new messaging_api_request_internal_1.FirebaseMessagingRequestHandler(app);
- }
-
- get app() {
- return this.appInternal;
- }
-
- send(message, dryRun) {
- const copy = (0, deep_copy_1.deepCopy)(message);
- (0, messaging_internal_1.validateMessage)(copy);
- if (typeof dryRun !== 'undefined' && !validator.isBoolean(dryRun)) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_ARGUMENT, 'dryRun must be a boolean');
- }
- return this.getUrlPath()
- .then((urlPath) => {
- const request = { message: copy };
- if (dryRun) {
- request.validate_only = true;
- }
- return this.messagingRequestHandler.invokeRequestHandler(FCM_SEND_HOST, urlPath, request);
- })
- .then((response) => {
- return response.name;
- });
- }
-
- sendEach(messages, dryRun) {
- if (validator.isArray(messages) && messages.constructor !== Array) {
-
-
-
- messages = Array.from(messages);
- }
- const copy = (0, deep_copy_1.deepCopy)(messages);
- if (!validator.isNonEmptyArray(copy)) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_ARGUMENT, 'messages must be a non-empty array');
- }
- if (copy.length > FCM_MAX_BATCH_SIZE) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_ARGUMENT, `messages list must not contain more than ${FCM_MAX_BATCH_SIZE} items`);
- }
- if (typeof dryRun !== 'undefined' && !validator.isBoolean(dryRun)) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_ARGUMENT, 'dryRun must be a boolean');
- }
- return this.getUrlPath()
- .then((urlPath) => {
- const requests = copy.map(async (message) => {
- (0, messaging_internal_1.validateMessage)(message);
- const request = { message };
- if (dryRun) {
- request.validate_only = true;
- }
- return this.messagingRequestHandler.invokeRequestHandlerForSendResponse(FCM_SEND_HOST, urlPath, request);
- });
- return Promise.allSettled(requests);
- }).then((results) => {
- const responses = [];
- results.forEach(result => {
- if (result.status === 'fulfilled') {
- responses.push(result.value);
- }
- else {
- responses.push({ success: false, error: result.reason });
- }
- });
- const successCount = responses.filter((resp) => resp.success).length;
- return {
- responses,
- successCount,
- failureCount: responses.length - successCount,
- };
- });
- }
-
- sendEachForMulticast(message, dryRun) {
- const copy = (0, deep_copy_1.deepCopy)(message);
- if (!validator.isNonNullObject(copy)) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_ARGUMENT, 'MulticastMessage must be a non-null object');
- }
- if (!validator.isNonEmptyArray(copy.tokens)) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_ARGUMENT, 'tokens must be a non-empty array');
- }
- if (copy.tokens.length > FCM_MAX_BATCH_SIZE) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_ARGUMENT, `tokens list must not contain more than ${FCM_MAX_BATCH_SIZE} items`);
- }
- const messages = copy.tokens.map((token) => {
- return {
- token,
- android: copy.android,
- apns: copy.apns,
- data: copy.data,
- notification: copy.notification,
- webpush: copy.webpush,
- fcmOptions: copy.fcmOptions,
- };
- });
- return this.sendEach(messages, dryRun);
- }
-
- sendAll(messages, dryRun) {
- if (validator.isArray(messages) && messages.constructor !== Array) {
-
-
-
- messages = Array.from(messages);
- }
- const copy = (0, deep_copy_1.deepCopy)(messages);
- if (!validator.isNonEmptyArray(copy)) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_ARGUMENT, 'messages must be a non-empty array');
- }
- if (copy.length > FCM_MAX_BATCH_SIZE) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_ARGUMENT, `messages list must not contain more than ${FCM_MAX_BATCH_SIZE} items`);
- }
- if (typeof dryRun !== 'undefined' && !validator.isBoolean(dryRun)) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_ARGUMENT, 'dryRun must be a boolean');
- }
- return this.getUrlPath()
- .then((urlPath) => {
- const requests = copy.map((message) => {
- (0, messaging_internal_1.validateMessage)(message);
- const request = { message };
- if (dryRun) {
- request.validate_only = true;
- }
- return {
- url: `https://${FCM_SEND_HOST}${urlPath}`,
- body: request,
- };
- });
- return this.messagingRequestHandler.sendBatchRequest(requests);
- });
- }
-
- sendMulticast(message, dryRun) {
- const copy = (0, deep_copy_1.deepCopy)(message);
- if (!validator.isNonNullObject(copy)) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_ARGUMENT, 'MulticastMessage must be a non-null object');
- }
- if (!validator.isNonEmptyArray(copy.tokens)) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_ARGUMENT, 'tokens must be a non-empty array');
- }
- if (copy.tokens.length > FCM_MAX_BATCH_SIZE) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_ARGUMENT, `tokens list must not contain more than ${FCM_MAX_BATCH_SIZE} items`);
- }
- const messages = copy.tokens.map((token) => {
- return {
- token,
- android: copy.android,
- apns: copy.apns,
- data: copy.data,
- notification: copy.notification,
- webpush: copy.webpush,
- fcmOptions: copy.fcmOptions,
- };
- });
- return this.sendAll(messages, dryRun);
- }
-
- sendToDevice(registrationTokenOrTokens, payload, options = {}) {
-
-
- this.validateRegistrationTokensType(registrationTokenOrTokens, 'sendToDevice', error_1.MessagingClientErrorCode.INVALID_RECIPIENT);
- this.validateMessagingPayloadAndOptionsTypes(payload, options);
- return Promise.resolve()
- .then(() => {
-
-
- this.validateRegistrationTokens(registrationTokenOrTokens, 'sendToDevice', error_1.MessagingClientErrorCode.INVALID_RECIPIENT);
- const payloadCopy = this.validateMessagingPayload(payload);
- const optionsCopy = this.validateMessagingOptions(options);
- const request = (0, deep_copy_1.deepCopy)(payloadCopy);
- (0, deep_copy_1.deepExtend)(request, optionsCopy);
- if (validator.isString(registrationTokenOrTokens)) {
- request.to = registrationTokenOrTokens;
- }
- else {
- request.registration_ids = registrationTokenOrTokens;
- }
- return this.messagingRequestHandler.invokeRequestHandler(FCM_SEND_HOST, FCM_SEND_PATH, request);
- })
- .then((response) => {
-
-
-
-
-
- if ('multicast_id' in response) {
- return mapRawResponseToDevicesResponse(response);
- }
- else {
- const groupResponse = mapRawResponseToDeviceGroupResponse(response);
- return {
- ...groupResponse,
- canonicalRegistrationTokenCount: -1,
- multicastId: -1,
- results: [],
- };
- }
- });
- }
-
- sendToDeviceGroup(notificationKey, payload, options = {}) {
- if (!validator.isNonEmptyString(notificationKey)) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_RECIPIENT, 'Notification key provided to sendToDeviceGroup() must be a non-empty string.');
- }
- else if (notificationKey.indexOf(':') !== -1) {
-
-
-
-
-
- return Promise.reject(new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_RECIPIENT, 'Notification key provided to sendToDeviceGroup() has the format of a registration token. ' +
- 'You should use sendToDevice() instead.'));
- }
-
-
- this.validateMessagingPayloadAndOptionsTypes(payload, options);
- return Promise.resolve()
- .then(() => {
-
-
- const payloadCopy = this.validateMessagingPayload(payload);
- const optionsCopy = this.validateMessagingOptions(options);
- const request = (0, deep_copy_1.deepCopy)(payloadCopy);
- (0, deep_copy_1.deepExtend)(request, optionsCopy);
- request.to = notificationKey;
- return this.messagingRequestHandler.invokeRequestHandler(FCM_SEND_HOST, FCM_SEND_PATH, request);
- })
- .then((response) => {
-
-
-
-
-
-
-
-
- if ('multicast_id' in response) {
- if (response.success === 0) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_RECIPIENT, 'Notification key provided to sendToDeviceGroup() is invalid.');
- }
- else {
- const devicesResponse = mapRawResponseToDevicesResponse(response);
- return {
- ...devicesResponse,
- failedRegistrationTokens: [],
- };
- }
- }
- return mapRawResponseToDeviceGroupResponse(response);
- });
- }
-
- sendToTopic(topic, payload, options = {}) {
-
-
- this.validateTopicType(topic, 'sendToTopic', error_1.MessagingClientErrorCode.INVALID_RECIPIENT);
- this.validateMessagingPayloadAndOptionsTypes(payload, options);
-
- topic = this.normalizeTopic(topic);
- return Promise.resolve()
- .then(() => {
-
-
- const payloadCopy = this.validateMessagingPayload(payload);
- const optionsCopy = this.validateMessagingOptions(options);
- this.validateTopic(topic, 'sendToTopic', error_1.MessagingClientErrorCode.INVALID_RECIPIENT);
- const request = (0, deep_copy_1.deepCopy)(payloadCopy);
- (0, deep_copy_1.deepExtend)(request, optionsCopy);
- request.to = topic;
- return this.messagingRequestHandler.invokeRequestHandler(FCM_SEND_HOST, FCM_SEND_PATH, request);
- })
- .then((response) => {
-
- utils.renameProperties(response, MESSAGING_TOPIC_RESPONSE_KEYS_MAP);
- return response;
- });
- }
-
- sendToCondition(condition, payload, options = {}) {
- if (!validator.isNonEmptyString(condition)) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_RECIPIENT, 'Condition provided to sendToCondition() must be a non-empty string.');
- }
-
-
- this.validateMessagingPayloadAndOptionsTypes(payload, options);
-
-
-
-
- condition = condition.replace(/"/g, '\'');
- return Promise.resolve()
- .then(() => {
-
-
- const payloadCopy = this.validateMessagingPayload(payload);
- const optionsCopy = this.validateMessagingOptions(options);
- const request = (0, deep_copy_1.deepCopy)(payloadCopy);
- (0, deep_copy_1.deepExtend)(request, optionsCopy);
- request.condition = condition;
- return this.messagingRequestHandler.invokeRequestHandler(FCM_SEND_HOST, FCM_SEND_PATH, request);
- })
- .then((response) => {
-
- utils.renameProperties(response, MESSAGING_CONDITION_RESPONSE_KEYS_MAP);
- return response;
- });
- }
-
- subscribeToTopic(registrationTokenOrTokens, topic) {
- return this.sendTopicManagementRequest(registrationTokenOrTokens, topic, 'subscribeToTopic', FCM_TOPIC_MANAGEMENT_ADD_PATH);
- }
-
- unsubscribeFromTopic(registrationTokenOrTokens, topic) {
- return this.sendTopicManagementRequest(registrationTokenOrTokens, topic, 'unsubscribeFromTopic', FCM_TOPIC_MANAGEMENT_REMOVE_PATH);
- }
- getUrlPath() {
- if (this.urlPath) {
- return Promise.resolve(this.urlPath);
- }
- return utils.findProjectId(this.app)
- .then((projectId) => {
- if (!validator.isNonEmptyString(projectId)) {
-
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_ARGUMENT, 'Failed to determine project ID for Messaging. Initialize the '
- + 'SDK with service account credentials or set project ID as an app option. '
- + 'Alternatively set the GOOGLE_CLOUD_PROJECT environment variable.');
- }
- this.urlPath = `/v1/projects/${projectId}/messages:send`;
- return this.urlPath;
- });
- }
-
- sendTopicManagementRequest(registrationTokenOrTokens, topic, methodName, path) {
- this.validateRegistrationTokensType(registrationTokenOrTokens, methodName);
- this.validateTopicType(topic, methodName);
-
- topic = this.normalizeTopic(topic);
- return Promise.resolve()
- .then(() => {
-
-
- this.validateRegistrationTokens(registrationTokenOrTokens, methodName);
- this.validateTopic(topic, methodName);
-
- let registrationTokensArray = registrationTokenOrTokens;
- if (validator.isString(registrationTokenOrTokens)) {
- registrationTokensArray = [registrationTokenOrTokens];
- }
- const request = {
- to: topic,
- registration_tokens: registrationTokensArray,
- };
- return this.messagingRequestHandler.invokeRequestHandler(FCM_TOPIC_MANAGEMENT_HOST, path, request);
- })
- .then((response) => {
- return mapRawResponseToTopicManagementResponse(response);
- });
- }
-
- validateMessagingPayloadAndOptionsTypes(payload, options) {
-
- if (!validator.isNonNullObject(payload)) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_PAYLOAD, 'Messaging payload must be an object with at least one of the "data" or "notification" properties.');
- }
-
- if (!validator.isNonNullObject(options)) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_OPTIONS, 'Messaging options must be an object.');
- }
- }
-
- validateMessagingPayload(payload) {
- const payloadCopy = (0, deep_copy_1.deepCopy)(payload);
- const payloadKeys = Object.keys(payloadCopy);
- const validPayloadKeys = ['data', 'notification'];
- let containsDataOrNotificationKey = false;
- payloadKeys.forEach((payloadKey) => {
-
- if (validPayloadKeys.indexOf(payloadKey) === -1) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_PAYLOAD, `Messaging payload contains an invalid "${payloadKey}" property. Valid properties are ` +
- '"data" and "notification".');
- }
- else {
- containsDataOrNotificationKey = true;
- }
- });
-
- if (!containsDataOrNotificationKey) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_PAYLOAD, 'Messaging payload must contain at least one of the "data" or "notification" properties.');
- }
- const validatePayload = (payloadKey, value) => {
-
- if (!validator.isNonNullObject(value)) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_PAYLOAD, `Messaging payload contains an invalid value for the "${payloadKey}" property. ` +
- 'Value must be an object.');
- }
- Object.keys(value).forEach((subKey) => {
- if (!validator.isString(value[subKey])) {
-
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_PAYLOAD, `Messaging payload contains an invalid value for the "${payloadKey}.${subKey}" ` +
- 'property. Values must be strings.');
- }
- else if (payloadKey === 'data' && /^google\./.test(subKey)) {
-
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_PAYLOAD, `Messaging payload contains the blacklisted "data.${subKey}" property.`);
- }
- });
- };
- if (payloadCopy.data !== undefined) {
- validatePayload('data', payloadCopy.data);
- }
- if (payloadCopy.notification !== undefined) {
- validatePayload('notification', payloadCopy.notification);
- }
-
- if ('data' in payloadCopy) {
- messaging_internal_1.BLACKLISTED_DATA_PAYLOAD_KEYS.forEach((blacklistedKey) => {
- if (blacklistedKey in payloadCopy.data) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_PAYLOAD, `Messaging payload contains the blacklisted "data.${blacklistedKey}" property.`);
- }
- });
- }
-
- if (payloadCopy.notification) {
- utils.renameProperties(payloadCopy.notification, CAMELCASED_NOTIFICATION_PAYLOAD_KEYS_MAP);
- }
- return payloadCopy;
- }
-
- validateMessagingOptions(options) {
- const optionsCopy = (0, deep_copy_1.deepCopy)(options);
-
- messaging_internal_1.BLACKLISTED_OPTIONS_KEYS.forEach((blacklistedKey) => {
- if (blacklistedKey in optionsCopy) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_OPTIONS, `Messaging options contains the blacklisted "${blacklistedKey}" property.`);
- }
- });
-
- utils.renameProperties(optionsCopy, CAMELCASE_OPTIONS_KEYS_MAP);
-
- if ('collapse_key' in optionsCopy && !validator.isNonEmptyString(optionsCopy.collapse_key)) {
- const keyName = ('collapseKey' in options) ? 'collapseKey' : 'collapse_key';
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_OPTIONS, `Messaging options contains an invalid value for the "${keyName}" property. Value must ` +
- 'be a non-empty string.');
- }
- else if ('dry_run' in optionsCopy && !validator.isBoolean(optionsCopy.dry_run)) {
- const keyName = ('dryRun' in options) ? 'dryRun' : 'dry_run';
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_OPTIONS, `Messaging options contains an invalid value for the "${keyName}" property. Value must ` +
- 'be a boolean.');
- }
- else if ('priority' in optionsCopy && !validator.isNonEmptyString(optionsCopy.priority)) {
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_OPTIONS, 'Messaging options contains an invalid value for the "priority" property. Value must ' +
- 'be a non-empty string.');
- }
- else if ('restricted_package_name' in optionsCopy &&
- !validator.isNonEmptyString(optionsCopy.restricted_package_name)) {
- const keyName = ('restrictedPackageName' in options) ? 'restrictedPackageName' : 'restricted_package_name';
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_OPTIONS, `Messaging options contains an invalid value for the "${keyName}" property. Value must ` +
- 'be a non-empty string.');
- }
- else if ('time_to_live' in optionsCopy && !validator.isNumber(optionsCopy.time_to_live)) {
- const keyName = ('timeToLive' in options) ? 'timeToLive' : 'time_to_live';
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_OPTIONS, `Messaging options contains an invalid value for the "${keyName}" property. Value must ` +
- 'be a number.');
- }
- else if ('content_available' in optionsCopy && !validator.isBoolean(optionsCopy.content_available)) {
- const keyName = ('contentAvailable' in options) ? 'contentAvailable' : 'content_available';
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_OPTIONS, `Messaging options contains an invalid value for the "${keyName}" property. Value must ` +
- 'be a boolean.');
- }
- else if ('mutable_content' in optionsCopy && !validator.isBoolean(optionsCopy.mutable_content)) {
- const keyName = ('mutableContent' in options) ? 'mutableContent' : 'mutable_content';
- throw new error_1.FirebaseMessagingError(error_1.MessagingClientErrorCode.INVALID_OPTIONS, `Messaging options contains an invalid value for the "${keyName}" property. Value must ` +
- 'be a boolean.');
- }
- return optionsCopy;
- }
-
- validateRegistrationTokensType(registrationTokenOrTokens, methodName, errorInfo = error_1.MessagingClientErrorCode.INVALID_ARGUMENT) {
- if (!validator.isNonEmptyArray(registrationTokenOrTokens) &&
- !validator.isNonEmptyString(registrationTokenOrTokens)) {
- throw new error_1.FirebaseMessagingError(errorInfo, `Registration token(s) provided to ${methodName}() must be a non-empty string or a ` +
- 'non-empty array.');
- }
- }
-
- validateRegistrationTokens(registrationTokenOrTokens, methodName, errorInfo = error_1.MessagingClientErrorCode.INVALID_ARGUMENT) {
- if (validator.isArray(registrationTokenOrTokens)) {
-
- if (registrationTokenOrTokens.length > 1000) {
- throw new error_1.FirebaseMessagingError(errorInfo, `Too many registration tokens provided in a single request to ${methodName}(). Batch ` +
- 'your requests to contain no more than 1,000 registration tokens per request.');
- }
-
- registrationTokenOrTokens.forEach((registrationToken, index) => {
- if (!validator.isNonEmptyString(registrationToken)) {
- throw new error_1.FirebaseMessagingError(errorInfo, `Registration token provided to ${methodName}() at index ${index} must be a ` +
- 'non-empty string.');
- }
- });
- }
- }
-
- validateTopicType(topic, methodName, errorInfo = error_1.MessagingClientErrorCode.INVALID_ARGUMENT) {
- if (!validator.isNonEmptyString(topic)) {
- throw new error_1.FirebaseMessagingError(errorInfo, `Topic provided to ${methodName}() must be a string which matches the format ` +
- '"/topics/[a-zA-Z0-9-_.~%]+".');
- }
- }
-
- validateTopic(topic, methodName, errorInfo = error_1.MessagingClientErrorCode.INVALID_ARGUMENT) {
- if (!validator.isTopic(topic)) {
- throw new error_1.FirebaseMessagingError(errorInfo, `Topic provided to ${methodName}() must be a string which matches the format ` +
- '"/topics/[a-zA-Z0-9-_.~%]+".');
- }
- }
-
- normalizeTopic(topic) {
- if (!/^\/topics\//.test(topic)) {
- topic = `/topics/${topic}`;
- }
- return topic;
- }
- }
- exports.Messaging = Messaging;
|