Skip to content

Commit

Permalink
feat: provide real standalone providers (#3503)
Browse files Browse the repository at this point in the history
* feat: provide real standalone providers

usage before
`importProvidersFrom(provideFirebaseApp(() => initializeApp(...)))`

usage now
`provideFirebaseApp(() => initializeApp(...))`

* test: fixed failed tests

* fix: update the missed lite.module.ts
  • Loading branch information
robertIsaac authored May 14, 2024
1 parent 35ab9cc commit 4d322f9
Show file tree
Hide file tree
Showing 25 changed files with 249 additions and 103 deletions.
33 changes: 25 additions & 8 deletions src/analytics/analytics.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { APP_INITIALIZER, InjectionToken, Injector, ModuleWithProviders, NgModule, NgZone, Optional } from '@angular/core';
import {
APP_INITIALIZER,
EnvironmentProviders,
InjectionToken,
Injector,
NgModule,
NgZone,
Optional,
makeEnvironmentProviders,
} from '@angular/core';
import { VERSION, ɵAngularFireSchedulers, ɵgetDefaultInstanceOf } from '@angular/fire';
import { FirebaseApp, FirebaseApps } from '@angular/fire/app';
import { Analytics as FirebaseAnalytics } from 'firebase/analytics';
Expand Down Expand Up @@ -60,10 +69,18 @@ export class AnalyticsModule {
}
}

export function provideAnalytics(fn: (injector: Injector) => FirebaseAnalytics, ...deps: any[]): ModuleWithProviders<AnalyticsModule> {
return {
ngModule: AnalyticsModule,
providers: [{
export function provideAnalytics(fn: (injector: Injector) => FirebaseAnalytics, ...deps: any[]): EnvironmentProviders {
registerVersion('angularfire', VERSION.full, 'analytics');

return makeEnvironmentProviders([
DEFAULT_ANALYTICS_INSTANCE_PROVIDER,
ANALYTICS_INSTANCES_PROVIDER,
{
provide: APP_INITIALIZER,
useValue: isAnalyticsSupportedFactory.async,
multi: true,
},
{
provide: PROVIDED_ANALYTICS_INSTANCES,
useFactory: analyticsInstanceFactory(fn),
multi: true,
Expand All @@ -73,7 +90,7 @@ export function provideAnalytics(fn: (injector: Injector) => FirebaseAnalytics,
ɵAngularFireSchedulers,
FirebaseApps,
...deps,
]
}]
};
],
},
]);
}
2 changes: 1 addition & 1 deletion src/analytics/analytics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Analytics', () => {
beforeEach(() => {
appName = rando();
TestBed.configureTestingModule({
imports: [
providers: [
provideFirebaseApp(() => initializeApp(COMMON_CONFIG_TOO, appName)),
provideAnalytics(() => {
providedAnalytics = getAnalytics(getApp(appName));
Expand Down
26 changes: 19 additions & 7 deletions src/app-check/app-check.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { isPlatformServer } from '@angular/common';
import { InjectionToken, Injector, ModuleWithProviders, NgModule, NgZone, Optional, PLATFORM_ID, isDevMode } from '@angular/core';
import {
EnvironmentProviders,
InjectionToken,
Injector,
NgModule,
NgZone,
Optional,
PLATFORM_ID,
isDevMode,
makeEnvironmentProviders,
} from '@angular/core';
import { VERSION, ɵAPP_CHECK_PROVIDER_NAME, ɵAngularFireSchedulers, ɵAppCheckInstances, ɵgetDefaultInstanceOf } from '@angular/fire';
import { FirebaseApp, FirebaseApps } from '@angular/fire/app';
import { registerVersion } from 'firebase/app';
Expand Down Expand Up @@ -56,10 +66,12 @@ export class AppCheckModule {
}
}

export function provideAppCheck(fn: (injector: Injector) => FirebaseAppCheck, ...deps: any[]): ModuleWithProviders<AppCheckModule> {
return {
ngModule: AppCheckModule,
providers: [{
export function provideAppCheck(fn: (injector: Injector) => FirebaseAppCheck, ...deps: any[]): EnvironmentProviders {
registerVersion('angularfire', VERSION.full, 'app-check');
return makeEnvironmentProviders([
DEFAULT_APP_CHECK_INSTANCE_PROVIDER,
APP_CHECK_INSTANCES_PROVIDER,
{
provide: PROVIDED_APP_CHECK_INSTANCES,
useFactory: appCheckInstanceFactory(fn),
multi: true,
Expand All @@ -71,6 +83,6 @@ export function provideAppCheck(fn: (injector: Injector) => FirebaseAppCheck, ..
FirebaseApps,
...deps,
]
}]
};
}
]);
}
2 changes: 1 addition & 1 deletion src/app-check/app-check.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Auth', () => {
beforeEach(() => {
appName = rando();
TestBed.configureTestingModule({
imports: [
providers: [
provideFirebaseApp(() => initializeApp(COMMON_CONFIG, appName)),
provideAuth(() => {
providedAuth = getAuth(getApp(appName));
Expand Down
22 changes: 15 additions & 7 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {
EnvironmentProviders,
Inject,
InjectionToken,
Injector,
ModuleWithProviders,
VERSION as NG_VERSION,
NgModule,
NgZone,
Optional,
PLATFORM_ID,
makeEnvironmentProviders,
} from '@angular/core';
import { VERSION, ɵAngularFireSchedulers } from '@angular/fire';
import { FirebaseApp as IFirebaseApp, getApp, registerVersion } from 'firebase/app';
Expand Down Expand Up @@ -44,6 +45,12 @@ const FIREBASE_APPS_PROVIDER = {

export function firebaseAppFactory(fn: (injector: Injector) => IFirebaseApp) {
return (zone: NgZone, injector: Injector) => {
const platformId = injector.get(PLATFORM_ID);
registerVersion('angularfire', VERSION.full, 'core');
registerVersion('angularfire', VERSION.full, 'app');
// eslint-disable-next-line @typescript-eslint/no-base-to-string
registerVersion('angular', NG_VERSION.full, platformId.toString());

const app = zone.runOutsideAngular(() => fn(injector));
return new FirebaseApp(app);
};
Expand All @@ -68,10 +75,11 @@ export class FirebaseAppModule {
// Calling initializeApp({ ... }, 'name') multiple times will add more FirebaseApps into the FIREBASE_APPS
// injection scope. This allows developers to more easily work with multiple Firebase Applications. Downside
// is that DI for app name and options doesn't really make sense anymore.
export function provideFirebaseApp(fn: (injector: Injector) => IFirebaseApp, ...deps: any[]): ModuleWithProviders<FirebaseAppModule> {
return {
ngModule: FirebaseAppModule,
providers: [{
export function provideFirebaseApp(fn: (injector: Injector) => IFirebaseApp, ...deps: any[]): EnvironmentProviders {
return makeEnvironmentProviders([
DEFAULT_FIREBASE_APP_PROVIDER,
FIREBASE_APPS_PROVIDER,
{
provide: PROVIDED_FIREBASE_APPS,
useFactory: firebaseAppFactory(fn),
multi: true,
Expand All @@ -81,6 +89,6 @@ export function provideFirebaseApp(fn: (injector: Injector) => IFirebaseApp, ...
ɵAngularFireSchedulers,
...deps,
],
}],
};
}
])
}
2 changes: 1 addition & 1 deletion src/app/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('FirebaseApp', () => {
beforeEach(() => {
appName = rando();
TestBed.configureTestingModule({
imports: [
providers: [
provideFirebaseApp(() => {
providedApp = initializeApp(COMMON_CONFIG, appName);
return providedApp;
Expand Down
12 changes: 6 additions & 6 deletions src/auth-guard/auth-guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ describe('AuthGuard', () => {
appName = rando();
TestBed.configureTestingModule({
imports: [
provideFirebaseApp(() => initializeApp(COMMON_CONFIG, appName)),
provideAuth(() => {
const auth = getAuth(getApp(appName));
connectAuthEmulator(auth, 'http://localhost:9098');
return auth;
}),
AuthGuardModule,
RouterModule.forRoot([
{ path: 'a', component: TestComponent, canActivate: [AuthGuard] }
])
],
providers: [
provideFirebaseApp(() => initializeApp(COMMON_CONFIG, appName)),
provideAuth(() => {
const auth = getAuth(getApp(appName));
connectAuthEmulator(auth, 'http://localhost:9098');
return auth;
}),
{ provide: APP_BASE_HREF, useValue: 'http://localhost:4200/' }
]
});
Expand Down
24 changes: 17 additions & 7 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { InjectionToken, Injector, ModuleWithProviders, NgModule, NgZone, Optional } from '@angular/core';
import {
EnvironmentProviders,
InjectionToken,
Injector,
NgModule,
NgZone,
Optional,
makeEnvironmentProviders,
} from '@angular/core';
import { VERSION, ɵAngularFireSchedulers, ɵgetDefaultInstanceOf } from '@angular/fire';
import { ɵAppCheckInstances } from '@angular/fire';
import { FirebaseApp, FirebaseApps } from '@angular/fire/app';
Expand Down Expand Up @@ -48,10 +56,12 @@ export class AuthModule {
}
}

export function provideAuth(fn: (injector: Injector) => FirebaseAuth, ...deps: any[]): ModuleWithProviders<AuthModule> {
return {
ngModule: AuthModule,
providers: [{
export function provideAuth(fn: (injector: Injector) => FirebaseAuth, ...deps: any[]): EnvironmentProviders {
registerVersion('angularfire', VERSION.full, 'auth');
return makeEnvironmentProviders([
DEFAULT_AUTH_INSTANCE_PROVIDER,
AUTH_INSTANCES_PROVIDER,
{
provide: PROVIDED_AUTH_INSTANCES,
useFactory: authInstanceFactory(fn),
multi: true,
Expand All @@ -63,6 +73,6 @@ export function provideAuth(fn: (injector: Injector) => FirebaseAuth, ...deps: a
[new Optional(), ɵAppCheckInstances ],
...deps,
]
}]
};
}
]);
}
2 changes: 1 addition & 1 deletion src/auth/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Auth', () => {
beforeEach(() => {
appName = rando();
TestBed.configureTestingModule({
imports: [
providers: [
provideFirebaseApp(() => initializeApp(COMMON_CONFIG, appName)),
provideAuth(() => {
providedAuth = getAuth(getApp(appName));
Expand Down
24 changes: 17 additions & 7 deletions src/database/database.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { InjectionToken, Injector, ModuleWithProviders, NgModule, NgZone, Optional } from '@angular/core';
import {
EnvironmentProviders,
InjectionToken,
Injector,
NgModule,
NgZone,
Optional,
makeEnvironmentProviders,
} from '@angular/core';
import { VERSION, ɵAngularFireSchedulers, ɵgetDefaultInstanceOf } from '@angular/fire';
import { ɵAppCheckInstances } from '@angular/fire';
import { FirebaseApp, FirebaseApps } from '@angular/fire/app';
Expand Down Expand Up @@ -49,10 +57,12 @@ export class DatabaseModule {
}
}

export function provideDatabase(fn: (injector: Injector) => FirebaseDatabase, ...deps: any[]): ModuleWithProviders<DatabaseModule> {
return {
ngModule: DatabaseModule,
providers: [{
export function provideDatabase(fn: (injector: Injector) => FirebaseDatabase, ...deps: any[]): EnvironmentProviders {
registerVersion('angularfire', VERSION.full, 'rtdb');
return makeEnvironmentProviders([
DEFAULT_DATABASE_INSTANCE_PROVIDER,
DATABASE_INSTANCES_PROVIDER,
{
provide: PROVIDED_DATABASE_INSTANCES,
useFactory: databaseInstanceFactory(fn),
multi: true,
Expand All @@ -66,6 +76,6 @@ export function provideDatabase(fn: (injector: Injector) => FirebaseDatabase, ..
[new Optional(), ɵAppCheckInstances ],
...deps,
]
}]
};
}
]);
}
2 changes: 1 addition & 1 deletion src/database/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Database', () => {
beforeEach(() => {
appName = rando();
TestBed.configureTestingModule({
imports: [
providers: [
provideFirebaseApp(() => initializeApp(COMMON_CONFIG, appName)),
provideDatabase(() => {
providedDatabase = getDatabase(getApp(appName));
Expand Down
25 changes: 18 additions & 7 deletions src/firestore/firestore.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { InjectionToken, Injector, ModuleWithProviders, NgModule, NgZone, Optional } from '@angular/core';
import {
EnvironmentProviders,
InjectionToken,
Injector,
NgModule,
NgZone,
Optional,
makeEnvironmentProviders,
} from '@angular/core';
import { VERSION, ɵAngularFireSchedulers, ɵgetDefaultInstanceOf } from '@angular/fire';
import { ɵAppCheckInstances } from '@angular/fire';
import { FirebaseApp, FirebaseApps } from '@angular/fire/app';
Expand Down Expand Up @@ -49,10 +57,13 @@ export class FirestoreModule {
}
}

export function provideFirestore(fn: (injector: Injector) => FirebaseFirestore, ...deps: any[]): ModuleWithProviders<FirestoreModule> {
return {
ngModule: FirestoreModule,
providers: [{
export function provideFirestore(fn: (injector: Injector) => FirebaseFirestore, ...deps: any[]): EnvironmentProviders {
registerVersion('angularfire', VERSION.full, 'fst');

return makeEnvironmentProviders([
DEFAULT_FIRESTORE_INSTANCE_PROVIDER,
FIRESTORE_INSTANCES_PROVIDER,
{
provide: PROVIDED_FIRESTORE_INSTANCES,
useFactory: firestoreInstanceFactory(fn),
multi: true,
Expand All @@ -66,6 +77,6 @@ export function provideFirestore(fn: (injector: Injector) => FirebaseFirestore,
[new Optional(), ɵAppCheckInstances ],
...deps,
]
}]
};
}
]);
}
2 changes: 1 addition & 1 deletion src/firestore/firestore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Firestore', () => {
beforeEach(() => {
appName = rando();
TestBed.configureTestingModule({
imports: [
providers: [
provideFirebaseApp(() => initializeApp(COMMON_CONFIG, appName)),
provideFirestore(() => {
providedFirestore = getFirestore(getApp(appName));
Expand Down
25 changes: 18 additions & 7 deletions src/firestore/lite/lite.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { InjectionToken, Injector, ModuleWithProviders, NgModule, NgZone, Optional } from '@angular/core';
import {
EnvironmentProviders,
InjectionToken,
Injector,
NgModule,
NgZone,
Optional,
makeEnvironmentProviders,
} from '@angular/core';
import { VERSION, ɵAngularFireSchedulers, ɵAppCheckInstances, ɵgetDefaultInstanceOf } from '@angular/fire';
import { FirebaseApp, FirebaseApps } from '@angular/fire/app';
import { AuthInstances } from '@angular/fire/auth';
Expand Down Expand Up @@ -48,10 +56,13 @@ export class FirestoreModule {
}
}

export function provideFirestore(fn: (injector: Injector) => FirebaseFirestore, ...deps: any[]): ModuleWithProviders<FirestoreModule> {
return {
ngModule: FirestoreModule,
providers: [{
export function provideFirestore(fn: (injector: Injector) => FirebaseFirestore, ...deps: any[]): EnvironmentProviders {
registerVersion('angularfire', VERSION.full, 'lite');

return makeEnvironmentProviders([
DEFAULT_FIRESTORE_INSTANCE_PROVIDER,
FIRESTORE_INSTANCES_PROVIDER,
{
provide: PROVIDED_FIRESTORE_INSTANCES,
useFactory: firestoreInstanceFactory(fn),
multi: true,
Expand All @@ -65,6 +76,6 @@ export function provideFirestore(fn: (injector: Injector) => FirebaseFirestore,
[new Optional(), ɵAppCheckInstances ],
...deps,
]
}]
};
}
]);
}
2 changes: 1 addition & 1 deletion src/firestore/lite/lite.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Firestore-lite', () => {
beforeEach(() => {
appName = rando();
TestBed.configureTestingModule({
imports: [
providers: [
provideFirebaseApp(() => initializeApp(COMMON_CONFIG, appName)),
provideFirestore(() => {
providedFirestore = getFirestore(getApp(appName));
Expand Down
Loading

0 comments on commit 4d322f9

Please sign in to comment.