Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions packages/firebase/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ declare namespace firebase {

function initializeApp(options: Object, name?: string): firebase.app.App;

const messaging: firebase.messaging.MessagingFactory;
function messaging(app?: firebase.app.App): firebase.messaging.Messaging;

function storage(app?: firebase.app.App): firebase.storage.Storage;

Expand All @@ -121,7 +121,7 @@ declare namespace firebase.app {
auth(): firebase.auth.Auth;
database(): firebase.database.Database;
delete(): Promise<any>;
messaging: firebase.messaging.MessagingFactory;
messaging(): firebase.messaging.Messaging;
name: string;
options: Object;
storage(url?: string): firebase.storage.Storage;
Expand Down Expand Up @@ -549,11 +549,6 @@ declare namespace firebase.database.ServerValue {
}

declare namespace firebase.messaging {
interface MessagingFactory {
(app?: firebase.app.App): Messaging;
isSupported(): boolean;
}

interface Messaging {
deleteToken(token: string): Promise<boolean>;
getToken(): Promise<string | null>;
Expand All @@ -574,6 +569,8 @@ declare namespace firebase.messaging {
useServiceWorker(registration: ServiceWorkerRegistration): void;
usePublicVapidKey(b64PublicKey: string): void;
}

function isSupported(): boolean;
}

declare namespace firebase.storage {
Expand Down
21 changes: 7 additions & 14 deletions packages/messaging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@ import {
_FirebaseNamespace,
FirebaseServiceFactory
} from '@firebase/app-types/private';
import { FirebaseMessaging } from '@firebase/messaging-types';

import { SwController } from './src/controllers/sw-controller';
import { WindowController } from './src/controllers/window-controller';
import { ERROR_CODES, errorFactory } from './src/models/errors';

import * as types from '@firebase/messaging-types';

export interface MessagingServiceFactory extends FirebaseServiceFactory {
isSupported?(): boolean;
}

export function registerMessaging(instance: _FirebaseNamespace): void {
const messagingName = 'messaging';

const factoryMethod: MessagingServiceFactory = app => {
const factoryMethod: FirebaseServiceFactory = app => {
if (!isSupported()) {
throw errorFactory.create(ERROR_CODES.UNSUPPORTED_BROWSER);
}
Expand All @@ -46,11 +41,9 @@ export function registerMessaging(instance: _FirebaseNamespace): void {
return new WindowController(app);
}
};
factoryMethod.isSupported = isSupported;

const namespaceExports = {
// no-inline
Messaging: WindowController
isSupported
};

instance.INTERNAL.registerService(
Expand All @@ -67,13 +60,13 @@ registerMessaging(firebase as _FirebaseNamespace);
*/
declare module '@firebase/app-types' {
interface FirebaseNamespace {
messaging?: {
(app?: FirebaseApp): types.FirebaseMessaging;
Messaging: typeof types.FirebaseMessaging;
messaging: {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Soo... @firebase/messaging is optional; you can use @firebase/database etc. but not messaging just fine.
firebase the metapackage bundles everything together, cool.
But if you just depend on @firebase/messaging, it would monkey-patch @firebase/app-types to add itself optionally? weird

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are all optional (you can import them or not), but if you import them, then the namespace exists (and the type should not be "optional" anymore). I'm not sure why that ? ever existed, I'm hoping @jshcrowthe has an idea.

(app?: FirebaseApp): FirebaseMessaging;
isSupported(): boolean;
};
}
interface FirebaseApp {
messaging?(): types.FirebaseMessaging;
messaging(): FirebaseMessaging;
}
}

Expand Down
14 changes: 9 additions & 5 deletions packages/messaging/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ import { expect } from 'chai';
import { sandbox, SinonSandbox, SinonStub } from 'sinon';

import { FirebaseApp } from '@firebase/app-types';
import { _FirebaseNamespace } from '@firebase/app-types/private';
import {
_FirebaseNamespace,
FirebaseServiceFactory
} from '@firebase/app-types/private';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be really nice to have a test that works like the docs suggests a user to, i.e.

import firebase from '@firebase/app';
import '@firebase/messaging'
// Do stuff w/ `firebase` and `firebase.messaging`

rather than poking at the internals like this :/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree. The tests need an overhaul (not just here). I'll get to that soon™.


import { MessagingServiceFactory, registerMessaging } from '../index';
import { registerMessaging } from '../index';
import { ERROR_CODES } from '../src/models/errors';

import { SwController } from '../src/controllers/sw-controller';
Expand Down Expand Up @@ -52,7 +55,7 @@ describe('Firebase Messaging > registerMessaging', () => {
});

describe('factoryMethod', () => {
let factoryMethod: MessagingServiceFactory;
let factoryMethod: FirebaseServiceFactory;
let fakeApp: FirebaseApp;

beforeEach(() => {
Expand All @@ -65,8 +68,9 @@ describe('Firebase Messaging > registerMessaging', () => {
});

describe('isSupported', () => {
it('is a static method on factoryMethod', () => {
expect(factoryMethod.isSupported).to.be.a('function');
it('is a namespace export', () => {
const namespaceExports = registerService.getCall(0).args[2];
expect(namespaceExports.isSupported).to.be.a('function');
});
});

Expand Down