Skip to content

Update SDKs to use the new type system #2358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 14, 2019
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
2 changes: 2 additions & 0 deletions packages/analytics-interop-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export interface AnalyticsCallOptions {
global: boolean;
}

export type FirebaseAnalyticsInternalName = 'analytics-internal';

declare module '@firebase/component' {
interface NameServiceMapping {
'analytics-internal': FirebaseAnalyticsInternal;
Expand Down
41 changes: 21 additions & 20 deletions packages/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import { FirebaseAnalyticsInternal } from '@firebase/analytics-interop-types';
import { _FirebaseNamespace } from '@firebase/app-types/private';
import { factory, settings, resetGlobalVars } from './src/factory';
import { EventName } from './src/constants';
import { Component, ComponentType } from '@firebase/component';
import {
Component,
ComponentType,
ComponentContainer
} from '@firebase/component';
import { ERROR_FACTORY, AnalyticsError } from './src/errors';

declare global {
Expand Down Expand Up @@ -50,26 +54,23 @@ export function registerAnalytics(instance: _FirebaseNamespace): void {
);

instance.INTERNAL.registerComponent(
new Component(
'analytics-internal',
container => {
try {
const analytics = container
.getProvider(ANALYTICS_TYPE)
.getImmediate();
return {
logEvent: analytics.logEvent
};
} catch (e) {
throw ERROR_FACTORY.create(
AnalyticsError.INTEROP_COMPONENT_REG_FAILED,
{ reason: e }
);
}
},
ComponentType.PRIVATE
)
new Component('analytics-internal', internalFactory, ComponentType.PRIVATE)
);

function internalFactory(
container: ComponentContainer
): FirebaseAnalyticsInternal {
try {
const analytics = container.getProvider(ANALYTICS_TYPE).getImmediate();
return {
logEvent: analytics.logEvent
};
} catch (e) {
throw ERROR_FACTORY.create(AnalyticsError.INTEROP_COMPONENT_REG_FAILED, {
reason: e
});
}
}
}

export { factory, settings, resetGlobalVars };
Expand Down
8 changes: 7 additions & 1 deletion packages/database-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export interface Reference extends Query {
key: string | null;
onDisconnect(): OnDisconnect;
parent: Reference | null;
push(value?: any, onComplete?: (a: Error | null) => any): ThenableReference;
push(value?: any, onComplete?: (a: Error | null) => any): Reference;
remove(onComplete?: (a: Error | null) => any): Promise<any>;
root: Reference;
set(value: any, onComplete?: (a: Error | null) => any): Promise<any>;
Expand Down Expand Up @@ -132,3 +132,9 @@ export function enableLogging(
logger?: boolean | ((a: string) => any),
persistent?: boolean
): any;

declare module '@firebase/component' {
interface NameServiceMapping {
'database': FirebaseDatabase;
}
}
4 changes: 2 additions & 2 deletions packages/database/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
Provider,
ComponentContainer
} from '@firebase/component';
import { FirebaseAuthInternal } from '@firebase/auth-interop-types';
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';

setWebSocketImpl(Client);

Expand Down Expand Up @@ -64,7 +64,7 @@ export function initStandalone(app: FirebaseApp, url: string, version: string) {
* ComponentContainer('database-admin') is just a placeholder that doesn't perform
* any actual function.
*/
const authProvider = new Provider<FirebaseAuthInternal>(
const authProvider = new Provider<FirebaseAuthInternalName>(
'auth-internal',
new ComponentContainer('database-admin')
);
Expand Down
3 changes: 2 additions & 1 deletion packages/database/src/api/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import { validateUrl } from '../core/util/validation';
import { FirebaseApp } from '@firebase/app-types';
import { FirebaseService } from '@firebase/app-types/private';
import { RepoInfo } from '../core/RepoInfo';
import { FirebaseDatabase } from '@firebase/database-types';

/**
* Class representing a firebase database.
* @implements {FirebaseService}
*/
export class Database implements FirebaseService {
export class Database implements FirebaseService, FirebaseDatabase {
INTERNAL: DatabaseInternals;
private root_: Reference;

Expand Down
3 changes: 2 additions & 1 deletion packages/database/src/api/Reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ import { Deferred } from '@firebase/util';
import { SyncPoint } from '../core/SyncPoint';
import { Database } from './Database';
import { DataSnapshot } from './DataSnapshot';
import * as types from '@firebase/database-types';

export interface ReferenceConstructor {
new (repo: Repo, path: Path): Reference;
}

export class Reference extends Query {
export class Reference extends Query implements types.Reference {
public then: (a?: any) => Promise<any>;
public catch: (a?: Error) => Promise<any>;

Expand Down
7 changes: 5 additions & 2 deletions packages/database/src/core/AuthTokenProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
*/

import { FirebaseAuthTokenData } from '@firebase/app-types/private';
import { FirebaseAuthInternal } from '@firebase/auth-interop-types';
import {
FirebaseAuthInternal,
FirebaseAuthInternalName
} from '@firebase/auth-interop-types';
import { Provider } from '@firebase/component';
import { log, warn } from './util/util';
import { FirebaseApp } from '@firebase/app-types';
Expand All @@ -28,7 +31,7 @@ export class AuthTokenProvider {
private auth_: FirebaseAuthInternal | null = null;
constructor(
private app_: FirebaseApp,
private authProvider_: Provider<FirebaseAuthInternal>
private authProvider_: Provider<FirebaseAuthInternalName>
) {
this.auth_ = authProvider_.getImmediate({ optional: true });
if (!this.auth_) {
Expand Down
4 changes: 2 additions & 2 deletions packages/database/src/core/Repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { EventRegistration } from './view/EventRegistration';
import { StatsCollection } from './stats/StatsCollection';
import { Event } from './view/Event';
import { Node } from './snap/Node';
import { FirebaseAuthInternal } from '@firebase/auth-interop-types';
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import { Provider } from '@firebase/component';

const INTERRUPT_REASON = 'repo_interrupt';
Expand Down Expand Up @@ -82,7 +82,7 @@ export class Repo {
public repoInfo_: RepoInfo,
forceRestClient: boolean,
public app: FirebaseApp,
authProvider: Provider<FirebaseAuthInternal>
authProvider: Provider<FirebaseAuthInternalName>
) {
const authTokenProvider = new AuthTokenProvider(app, authProvider);

Expand Down
6 changes: 3 additions & 3 deletions packages/database/src/core/RepoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { validateUrl } from './util/validation';
import './Repo_transaction';
import { Database } from '../api/Database';
import { RepoInfo } from './RepoInfo';
import { FirebaseAuthInternal } from '@firebase/auth-interop-types';
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import { Provider } from '@firebase/component';

/** @const {string} */
Expand Down Expand Up @@ -93,7 +93,7 @@ export class RepoManager {
*/
databaseFromApp(
app: FirebaseApp,
authProvider: Provider<FirebaseAuthInternal>,
authProvider: Provider<FirebaseAuthInternalName>,
url?: string
): Database {
let dbUrl: string | undefined = url || app.options[DATABASE_URL_OPTION];
Expand Down Expand Up @@ -159,7 +159,7 @@ export class RepoManager {
createRepo(
repoInfo: RepoInfo,
app: FirebaseApp,
authProvider: Provider<FirebaseAuthInternal>
authProvider: Provider<FirebaseAuthInternalName>
): Repo {
let appRepos = safeGet(this.repos_, app.name);

Expand Down
3 changes: 2 additions & 1 deletion packages/database/test/helpers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ let numDatabases = 0;
() => ({
getToken: () => Promise.resolve(null),
addAuthTokenListener: () => {},
removeAuthTokenListener: () => {}
removeAuthTokenListener: () => {},
getUid: () => null
}),
ComponentType.PRIVATE
)
Expand Down
6 changes: 6 additions & 0 deletions packages/firestore-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1571,3 +1571,9 @@ export interface FirestoreError {
name: string;
stack?: string;
}

declare module '@firebase/component' {
interface NameServiceMapping {
'firestore': FirebaseFirestore;
}
}
7 changes: 5 additions & 2 deletions packages/firestore/src/api/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import { _FirebaseApp } from '@firebase/app-types/private';
import { User } from '../auth/user';
import { assert } from '../util/assert';
import { Code, FirestoreError } from '../util/error';
import { FirebaseAuthInternal } from '@firebase/auth-interop-types';
import {
FirebaseAuthInternal,
FirebaseAuthInternalName
} from '@firebase/auth-interop-types';
import { Provider } from '@firebase/component';

// TODO(mikelehen): This should be split into multiple files and probably
Expand Down Expand Up @@ -151,7 +154,7 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {

private auth: FirebaseAuthInternal | null;

constructor(authProvider: Provider<FirebaseAuthInternal>) {
constructor(authProvider: Provider<FirebaseAuthInternalName>) {
this.tokenListener = () => {
this.tokenCounter++;
this.currentUser = this.getUser();
Expand Down
4 changes: 2 additions & 2 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ import {
fieldPathFromArgument,
UserDataConverter
} from './user_data_converter';
import { FirebaseAuthInternal } from '@firebase/auth-interop-types';
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import { Provider } from '@firebase/component';

// settings() defaults:
Expand Down Expand Up @@ -312,7 +312,7 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {

constructor(
databaseIdOrApp: FirestoreDatabase | FirebaseApp,
authProvider: Provider<FirebaseAuthInternal>
authProvider: Provider<FirebaseAuthInternalName>
) {
if (typeof (databaseIdOrApp as FirebaseApp).options === 'object') {
// This is very likely a Firebase app object
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/test/util/api_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const FIRESTORE = new Firestore(
projectId: 'projectid',
database: 'database'
},
new Provider('auth-interop', new ComponentContainer('default'))
new Provider('auth-internal', new ComponentContainer('default'))
);

export function firestore(): Firestore {
Expand Down
6 changes: 6 additions & 0 deletions packages/functions-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,9 @@ export interface HttpsError extends Error {
*/
readonly details?: any;
}

declare module '@firebase/component' {
interface NameServiceMapping {
'functions': FirebaseFunctions;
}
}
8 changes: 4 additions & 4 deletions packages/functions/src/api/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import { _errorForResponse, HttpsErrorImpl } from './error';
import { ContextProvider } from '../context';
import { Serializer } from '../serializer';
import { Provider } from '@firebase/component';
import { FirebaseAuthInternal } from '@firebase/auth-interop-types';
import { FirebaseMessaging } from '@firebase/messaging-types';
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import { FirebaseMessagingName } from '@firebase/messaging-types';

/**
* The response to an http request.
Expand Down Expand Up @@ -83,8 +83,8 @@ export class Service implements FirebaseFunctions, FirebaseService {
*/
constructor(
private app_: FirebaseApp,
authProvider: Provider<FirebaseAuthInternal>,
messagingProvider: Provider<FirebaseMessaging>,
authProvider: Provider<FirebaseAuthInternalName>,
messagingProvider: Provider<FirebaseMessagingName>,
private region_: string = 'us-central1'
) {
this.contextProvider = new ContextProvider(authProvider, messagingProvider);
Expand Down
14 changes: 10 additions & 4 deletions packages/functions/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
* limitations under the License.
*/
import { _FirebaseApp } from '@firebase/app-types/private';
import { FirebaseMessaging } from '@firebase/messaging-types';
import { FirebaseAuthInternal } from '@firebase/auth-interop-types';
import {
FirebaseMessaging,
FirebaseMessagingName
} from '@firebase/messaging-types';
import {
FirebaseAuthInternal,
FirebaseAuthInternalName
} from '@firebase/auth-interop-types';
import { Provider } from '@firebase/component';

/**
Expand All @@ -34,8 +40,8 @@ export class ContextProvider {
private auth: FirebaseAuthInternal | null = null;
private messaging: FirebaseMessaging | null = null;
constructor(
authProvider: Provider<FirebaseAuthInternal>,
messagingProvider: Provider<FirebaseMessaging>
authProvider: Provider<FirebaseAuthInternalName>,
messagingProvider: Provider<FirebaseMessagingName>
) {
this.auth = authProvider.getImmediate({ optional: true });
this.messaging = messagingProvider.getImmediate({
Expand Down
7 changes: 5 additions & 2 deletions packages/functions/test/browser/callable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import * as sinon from 'sinon';
import { FirebaseApp } from '@firebase/app-types';
import { _FirebaseApp } from '@firebase/app-types/private';
import { makeFakeApp, createTestService } from '../utils';
import { FirebaseMessaging } from '@firebase/messaging-types';
import {
FirebaseMessaging,
FirebaseMessagingName
} from '@firebase/messaging-types';
import {
Provider,
ComponentContainer,
Expand All @@ -44,7 +47,7 @@ describe('Firebase Functions > Call', () => {
const messagingMock: FirebaseMessaging = ({
getToken: () => Promise.resolve('iid')
} as unknown) as FirebaseMessaging;
const messagingProvider = new Provider<FirebaseMessaging>(
const messagingProvider = new Provider<FirebaseMessagingName>(
'messaging',
new ComponentContainer('test')
);
Expand Down
7 changes: 5 additions & 2 deletions packages/functions/test/callable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import {
Component,
ComponentType
} from '@firebase/component';
import { FirebaseAuthInternal } from '@firebase/auth-interop-types';
import {
FirebaseAuthInternal,
FirebaseAuthInternalName
} from '@firebase/auth-interop-types';
import { makeFakeApp, createTestService } from './utils';

// eslint-disable-next-line @typescript-eslint/no-require-imports
Expand Down Expand Up @@ -100,7 +103,7 @@ describe('Firebase Functions > Call', () => {
const authMock: FirebaseAuthInternal = ({
getToken: () => Promise.resolve({ accessToken: 'token' })
} as unknown) as FirebaseAuthInternal;
const authProvider = new Provider<FirebaseAuthInternal>(
const authProvider = new Provider<FirebaseAuthInternalName>(
'auth-internal',
new ComponentContainer('test')
);
Expand Down
8 changes: 4 additions & 4 deletions packages/functions/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import { FirebaseOptions, FirebaseApp } from '@firebase/app-types';
import { Provider, ComponentContainer } from '@firebase/component';
import { FirebaseAuthInternal } from '@firebase/auth-interop-types';
import { FirebaseMessaging } from '@firebase/messaging-types';
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import { FirebaseMessagingName } from '@firebase/messaging-types';
import { Service } from '../src/api/service';

export function makeFakeApp(options: FirebaseOptions = {}): FirebaseApp {
Expand All @@ -43,11 +43,11 @@ export function makeFakeApp(options: FirebaseOptions = {}): FirebaseApp {
export function createTestService(
app: FirebaseApp,
region?: string,
authProvider = new Provider<FirebaseAuthInternal>(
authProvider = new Provider<FirebaseAuthInternalName>(
'auth-internal',
new ComponentContainer('test')
),
messagingProvider = new Provider<FirebaseMessaging>(
messagingProvider = new Provider<FirebaseMessagingName>(
'messaging',
new ComponentContainer('test')
)
Expand Down
Loading