Skip to content

Commit da1d0be

Browse files
committed
Address comments
1 parent 2616ccc commit da1d0be

File tree

12 files changed

+51
-60
lines changed

12 files changed

+51
-60
lines changed

config/webpack.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = {
5050
options: { esModules: true }
5151
},
5252
enforce: 'post',
53-
exclude: [/\.test\.ts$/, /test(ing)?\//]
53+
exclude: [/\.test\.ts$/, /\btest(ing)?\//]
5454
},
5555
{
5656
test: /\.js$/,

packages/messaging/src/controllers/sw-controller.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2019 Google Inc.
3+
* Copyright 2017 Google Inc.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -23,12 +23,13 @@ import {
2323
MessagePayload,
2424
NotificationDetails
2525
} from '../interfaces/message-payload';
26-
import { FCM_MSG, FN_CAMPAIGN_ID, DEFAULT_VAPID_KEY } from '../util/constants';
26+
import { FCM_MSG, DEFAULT_VAPID_KEY } from '../util/constants';
2727
import { MessageType, InternalMessage } from '../interfaces/internal-message';
2828
import { dbGet } from '../helpers/idb-manager';
2929
import { Unsubscribe } from '@firebase/util';
3030
import { sleep } from '../helpers/sleep';
3131
import { FirebaseApp } from '@firebase/app-types';
32+
import { isConsoleMessage } from '../helpers/is-console-message';
3233

3334
// Let TS know that this is a service worker
3435
declare const self: ServiceWorkerGlobalScope;
@@ -355,7 +356,7 @@ function getLink(payload: MessagePayload): string | null {
355356
return link;
356357
}
357358

358-
if (payload.data?.[FN_CAMPAIGN_ID]) {
359+
if (isConsoleMessage(payload.data)) {
359360
// Notification created in the Firebase Console. Redirect to origin.
360361
return self.location.origin;
361362
} else {

packages/messaging/src/controllers/window-controller.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2019 Google Inc.
3+
* Copyright 2017 Google Inc.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -31,6 +31,8 @@ import {
3131
DEFAULT_VAPID_KEY
3232
} from '../util/constants';
3333
import { FirebaseApp } from '@firebase/app-types';
34+
import { ConsoleMessageData } from '../interfaces/message-payload';
35+
import { isConsoleMessage } from '../helpers/is-console-message';
3436

3537
export class WindowController implements FirebaseMessaging {
3638
private vapidKey: string | null = null;
@@ -187,12 +189,7 @@ export class WindowController implements FirebaseMessaging {
187189
}
188190

189191
const { data } = payload;
190-
if (
191-
data &&
192-
FN_CAMPAIGN_ID in data &&
193-
data[FN_CAMPAIGN_ANALYTICS_ENABLED] === '1'
194-
) {
195-
// This message has a campaign id, meaning it was sent using the FN Console.
192+
if (isConsoleMessage(data) && data[FN_CAMPAIGN_ANALYTICS_ENABLED] === '1') {
196193
// Analytics is enabled on this message, so we should log it.
197194
await this.logEvent(type, data);
198195
}
@@ -225,11 +222,3 @@ function getEventType(messageType: MessageType): string {
225222
throw new Error();
226223
}
227224
}
228-
229-
/** Additional data of a message sent from the FN Console. */
230-
interface ConsoleMessageData {
231-
[FN_CAMPAIGN_ID]: string;
232-
[FN_CAMPAIGN_TIME]: string;
233-
[FN_CAMPAIGN_NAME]?: string;
234-
[FN_CAMPAIGN_ANALYTICS_ENABLED]?: '1';
235-
}

packages/messaging/src/core/api.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2019 Google Inc.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -17,9 +17,8 @@
1717

1818
import { ErrorCode, ERROR_FACTORY } from '../util/errors';
1919
import { DEFAULT_VAPID_KEY, ENDPOINT } from '../util/constants';
20-
import { TokenDetails } from '../interfaces/token-details';
20+
import { TokenDetails, SubscriptionOptions } from '../interfaces/token-details';
2121
import { FirebaseInternalDependencies } from '../interfaces/internal-dependencies';
22-
import { SubscriptionOptions } from '../interfaces/subscription-options';
2322
import { AppConfig } from '../interfaces/app-config';
2423

2524
export interface ApiResponse {

packages/messaging/src/core/token-management.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ import { dbGet, dbSet } from '../helpers/idb-manager';
2828
import * as apiModule from './api';
2929
import { Stub } from '../testing/sinon-types';
3030
import { getFakeTokenDetails } from '../testing/fakes/token-details';
31-
import { TokenDetails } from '../interfaces/token-details';
31+
import { TokenDetails, SubscriptionOptions } from '../interfaces/token-details';
3232
import { arrayToBase64 } from '../helpers/array-to-base64';
33-
import { SubscriptionOptions } from '../interfaces/subscription-options';
3433

3534
describe('Token Management', () => {
3635
let tokenDetails: TokenDetails;

packages/messaging/src/core/token-management.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
import { dbGet, dbSet, dbRemove } from '../helpers/idb-manager';
1919
import { FirebaseInternalDependencies } from '../interfaces/internal-dependencies';
20-
import { SubscriptionOptions } from '../interfaces/subscription-options';
21-
import { TokenDetails } from '../interfaces/token-details';
20+
import { TokenDetails, SubscriptionOptions } from '../interfaces/token-details';
2221
import { requestUpdateToken, requestGetToken, requestDeleteToken } from './api';
2322
import { arrayToBase64 } from '../helpers/array-to-base64';
2423
import { ERROR_FACTORY, ErrorCode } from '../util/errors';

packages/messaging/src/helpers/array-to-base64.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2019 Google Inc.
3+
* Copyright 2017 Google Inc.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

packages/messaging/src/helpers/idb-manager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ const OBJECT_STORE_NAME = 'firebase-messaging-store';
2828
let dbPromise: Promise<DB> | null = null;
2929
function getDbPromise(): Promise<DB> {
3030
if (!dbPromise) {
31-
dbPromise = openDb(DATABASE_NAME, DATABASE_VERSION, upgradeDB => {
31+
dbPromise = openDb(DATABASE_NAME, DATABASE_VERSION, upgradeDb => {
3232
// We don't use 'break' in this switch statement, the fall-through
3333
// behavior is what we want, because if there are multiple versions between
3434
// the old version and the current version, we want ALL the migrations
3535
// that correspond to those versions to run, not only the last one.
3636
// eslint-disable-next-line default-case
37-
switch (upgradeDB.oldVersion) {
37+
switch (upgradeDb.oldVersion) {
3838
case 0:
39-
upgradeDB.createObjectStore(OBJECT_STORE_NAME);
39+
upgradeDb.createObjectStore(OBJECT_STORE_NAME);
4040
}
4141
});
4242
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { ConsoleMessageData } from '../interfaces/message-payload';
2+
import { FN_CAMPAIGN_ID } from '../util/constants';
3+
4+
export function isConsoleMessage(data: unknown): data is ConsoleMessageData {
5+
// This message has a campaign id, meaning it was sent using the FN Console.
6+
return typeof data === 'object' && !!data && FN_CAMPAIGN_ID in data;
7+
}

packages/messaging/src/interfaces/message-payload.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
* limitations under the License.
1616
*/
1717

18+
import {
19+
FN_CAMPAIGN_ID,
20+
FN_CAMPAIGN_TIME,
21+
FN_CAMPAIGN_NAME,
22+
FN_CAMPAIGN_ANALYTICS_ENABLED
23+
} from '../util/constants';
24+
1825
export interface NotificationDetails extends NotificationOptions {
1926
title: string;
2027
click_action?: string; // eslint-disable-line camelcase
@@ -27,5 +34,13 @@ export interface FcmOptions {
2734
export interface MessagePayload {
2835
fcmOptions?: FcmOptions;
2936
notification?: NotificationDetails;
30-
data?: any; // eslint-disable-line @typescript-eslint/no-explicit-any
37+
data?: unknown;
38+
}
39+
40+
/** Additional data of a message sent from the FN Console. */
41+
export interface ConsoleMessageData {
42+
[FN_CAMPAIGN_ID]: string;
43+
[FN_CAMPAIGN_TIME]: string;
44+
[FN_CAMPAIGN_NAME]?: string;
45+
[FN_CAMPAIGN_ANALYTICS_ENABLED]?: '1';
3146
}

packages/messaging/src/interfaces/subscription-options.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

packages/messaging/src/interfaces/token-details.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,20 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { SubscriptionOptions } from './subscription-options';
19-
2018
export interface TokenDetails {
2119
token: string;
2220
createTime: number;
2321
/** Does not exist in Safari since it's not using Push API. */
2422
subscriptionOptions?: SubscriptionOptions;
2523
}
24+
25+
/**
26+
* Additional options and values required by a Push API subscription.
27+
*/
28+
export interface SubscriptionOptions {
29+
vapidKey: string;
30+
swScope: string;
31+
endpoint: string;
32+
auth: string;
33+
p256dh: string;
34+
}

0 commit comments

Comments
 (0)