nestjs-firebase-admin 0.3.4 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './modules/admin/admin.module';
2
2
  export * from './modules/admin/admin.service';
3
+ export * from './modules/admin/database.service';
3
4
  export * from './modules/admin/interfaces';
4
5
  export * from './modules/admin/types';
package/dist/index.js CHANGED
@@ -16,5 +16,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./modules/admin/admin.module"), exports);
18
18
  __exportStar(require("./modules/admin/admin.service"), exports);
19
+ __exportStar(require("./modules/admin/database.service"), exports);
19
20
  __exportStar(require("./modules/admin/interfaces"), exports);
20
21
  __exportStar(require("./modules/admin/types"), exports);
@@ -1,16 +1,7 @@
1
- import { DynamicModule, Provider, Type } from '@nestjs/common';
2
- import type { AdminModuleAsyncOptions, AdminModuleOptionsFactory } from './interfaces';
1
+ import { DynamicModule } from '@nestjs/common';
2
+ import type { AdminModuleAsyncOptions } from './interfaces';
3
3
  import type { AdminModuleOptions } from './types';
4
4
  export declare class AdminModule {
5
5
  static register(options: AdminModuleOptions): DynamicModule;
6
- static registerAsync(options: AdminModuleAsyncOptions): {
7
- module: typeof AdminModule;
8
- imports: (Type<any> | DynamicModule | Promise<DynamicModule> | import("@nestjs/common").ForwardReference<any>)[] | undefined;
9
- providers: (Provider | {
10
- provide: Type<AdminModuleOptionsFactory> | undefined;
11
- useClass: Type<AdminModuleOptionsFactory> | undefined;
12
- })[];
13
- };
14
- private static createAsyncProviders;
15
- private static createAsyncOptionsProvider;
6
+ static registerAsync(options: AdminModuleAsyncOptions): DynamicModule;
16
7
  }
@@ -14,16 +14,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
14
14
  step((generator = generator.apply(thisArg, _arguments || [])).next());
15
15
  });
16
16
  };
17
+ var __importDefault = (this && this.__importDefault) || function (mod) {
18
+ return (mod && mod.__esModule) ? mod : { "default": mod };
19
+ };
17
20
  var AdminModule_1;
18
21
  Object.defineProperty(exports, "__esModule", { value: true });
19
22
  exports.AdminModule = void 0;
20
23
  const common_1 = require("@nestjs/common");
21
24
  const random_string_generator_util_1 = require("@nestjs/common/utils/random-string-generator.util");
25
+ const firebase_admin_1 = __importDefault(require("firebase-admin"));
22
26
  const admin_constants_1 = require("./admin.constants");
23
27
  const admin_service_1 = require("./admin.service");
24
28
  const database_service_1 = require("./database.service");
25
29
  let AdminModule = AdminModule_1 = class AdminModule {
26
30
  static register(options) {
31
+ const firebaseApp = firebase_admin_1.default.initializeApp(Object.assign(Object.assign({}, options), { credential: firebase_admin_1.default.credential.cert(options.credential) }));
27
32
  return {
28
33
  module: AdminModule_1,
29
34
  providers: [
@@ -32,58 +37,82 @@ let AdminModule = AdminModule_1 = class AdminModule {
32
37
  useValue: options,
33
38
  },
34
39
  {
35
- provide: admin_constants_1.ADMIN_MODULE_ID,
36
- useValue: (0, random_string_generator_util_1.randomStringGenerator)(),
37
- },
38
- ],
39
- };
40
- }
41
- static registerAsync(options) {
42
- return {
43
- module: AdminModule_1,
44
- imports: options.imports,
45
- providers: [
46
- ...this.createAsyncProviders(options),
47
- {
48
- provide: admin_constants_1.FIREBASE_ADMIN_INSTANCE_TOKEN,
49
- useFactory: (options) => options,
50
- inject: [admin_constants_1.ADMIN_MODULE_OPTIONS],
40
+ provide: 'FIREBASE_ADMIN_APP',
41
+ useValue: firebaseApp,
51
42
  },
52
43
  {
53
44
  provide: admin_constants_1.ADMIN_MODULE_ID,
54
45
  useValue: (0, random_string_generator_util_1.randomStringGenerator)(),
55
46
  },
56
- ...(options.extraProviders || []),
57
47
  ],
58
48
  };
59
49
  }
60
- static createAsyncProviders(options) {
61
- if (options.useExisting || options.useFactory) {
62
- return [this.createAsyncOptionsProvider(options)];
50
+ static registerAsync(options) {
51
+ const providers = [];
52
+ if (options.useFactory) {
53
+ const factory = options.useFactory;
54
+ providers.push({
55
+ provide: admin_constants_1.FIREBASE_ADMIN_INSTANCE_TOKEN,
56
+ useFactory: factory,
57
+ inject: options.inject || [],
58
+ });
59
+ providers.push({
60
+ provide: 'FIREBASE_ADMIN_APP',
61
+ useFactory: (...args) => __awaiter(this, void 0, void 0, function* () {
62
+ const config = yield factory(...args);
63
+ return firebase_admin_1.default.initializeApp(Object.assign(Object.assign({}, config), { credential: firebase_admin_1.default.credential.cert(config.credential) }));
64
+ }),
65
+ inject: options.inject || [],
66
+ });
67
+ }
68
+ else if (options.useExisting) {
69
+ providers.push({
70
+ provide: admin_constants_1.FIREBASE_ADMIN_INSTANCE_TOKEN,
71
+ useFactory: (optionsFactory) => __awaiter(this, void 0, void 0, function* () {
72
+ const config = yield optionsFactory.createAdminOptions();
73
+ return config;
74
+ }),
75
+ inject: [options.useExisting],
76
+ });
77
+ providers.push({
78
+ provide: 'FIREBASE_ADMIN_APP',
79
+ useFactory: (optionsFactory) => __awaiter(this, void 0, void 0, function* () {
80
+ const config = yield optionsFactory.createAdminOptions();
81
+ return firebase_admin_1.default.initializeApp(Object.assign(Object.assign({}, config), { credential: firebase_admin_1.default.credential.cert(config.credential) }));
82
+ }),
83
+ inject: [options.useExisting],
84
+ });
63
85
  }
64
- return [
65
- this.createAsyncOptionsProvider(options),
66
- {
86
+ else if (options.useClass) {
87
+ providers.push({
67
88
  provide: options.useClass,
68
89
  useClass: options.useClass,
69
- },
70
- ];
71
- }
72
- static createAsyncOptionsProvider(options) {
73
- if (options.useFactory) {
74
- return {
75
- provide: admin_constants_1.ADMIN_MODULE_OPTIONS,
76
- useFactory: options.useFactory,
77
- inject: options.inject || [],
78
- };
90
+ });
91
+ providers.push({
92
+ provide: admin_constants_1.FIREBASE_ADMIN_INSTANCE_TOKEN,
93
+ useFactory: (optionsFactory) => __awaiter(this, void 0, void 0, function* () {
94
+ const config = yield optionsFactory.createAdminOptions();
95
+ return config;
96
+ }),
97
+ inject: [options.useClass],
98
+ });
99
+ providers.push({
100
+ provide: 'FIREBASE_ADMIN_APP',
101
+ useFactory: (optionsFactory) => __awaiter(this, void 0, void 0, function* () {
102
+ const config = yield optionsFactory.createAdminOptions();
103
+ return firebase_admin_1.default.initializeApp(Object.assign(Object.assign({}, config), { credential: firebase_admin_1.default.credential.cert(config.credential) }));
104
+ }),
105
+ inject: [options.useClass],
106
+ });
107
+ }
108
+ else {
109
+ throw new Error('One of useFactory, useExisting, or useClass must be provided in AdminModuleAsyncOptions');
79
110
  }
80
111
  return {
81
- provide: admin_constants_1.ADMIN_MODULE_OPTIONS,
82
- useFactory: (optionsFactory) => __awaiter(this, void 0, void 0, function* () { return optionsFactory.createAdminOptions(); }),
83
- inject: [
84
- (options.useExisting ||
85
- options.useClass),
86
- ],
112
+ module: AdminModule_1,
113
+ imports: options.imports,
114
+ providers,
115
+ exports: [admin_service_1.AdminService],
87
116
  };
88
117
  }
89
118
  };
@@ -5,7 +5,9 @@ import type { Agent } from 'node:http';
5
5
  import type { AdminModuleOptions } from './types';
6
6
  export declare class AdminService {
7
7
  protected readonly options: AdminModuleOptions;
8
- constructor(options: AdminModuleOptions);
8
+ private readonly firebaseApp;
9
+ private readonly app;
10
+ constructor(options: AdminModuleOptions, firebaseApp: App);
9
11
  applicationDefault(httpAgent?: Agent): import("firebase-admin/app").Credential;
10
12
  deleteApp(app: App): Promise<void>;
11
13
  get getApps(): App[];
@@ -22,9 +22,10 @@ const app_1 = require("firebase-admin/app");
22
22
  const firebase_admin_1 = __importDefault(require("firebase-admin"));
23
23
  const admin_constants_1 = require("./admin.constants");
24
24
  let AdminService = class AdminService {
25
- constructor(options) {
25
+ constructor(options, firebaseApp) {
26
26
  this.options = options;
27
- firebase_admin_1.default.initializeApp(Object.assign(Object.assign({}, this.options), { credential: firebase_admin_1.default.credential.cert(this.options.credential) }));
27
+ this.firebaseApp = firebaseApp;
28
+ this.app = firebaseApp;
28
29
  }
29
30
  applicationDefault(httpAgent) {
30
31
  return (0, app_1.applicationDefault)(httpAgent);
@@ -54,15 +55,16 @@ let AdminService = class AdminService {
54
55
  });
55
56
  }
56
57
  get appRef() {
57
- return this.initializeApp();
58
+ return this.app;
58
59
  }
59
60
  initializeApp() {
60
- return this.admin().initializeApp(Object.assign(Object.assign({}, this.options), { credential: firebase_admin_1.default.credential.cert(this.options.credential) }));
61
+ return this.app;
61
62
  }
62
63
  };
63
64
  exports.AdminService = AdminService;
64
65
  exports.AdminService = AdminService = __decorate([
65
66
  (0, common_1.Injectable)(),
66
67
  __param(0, (0, common_1.Inject)(admin_constants_1.FIREBASE_ADMIN_INSTANCE_TOKEN)),
67
- __metadata("design:paramtypes", [Object])
68
+ __param(1, (0, common_1.Inject)('FIREBASE_ADMIN_APP')),
69
+ __metadata("design:paramtypes", [Object, Object])
68
70
  ], AdminService);
@@ -46,6 +46,9 @@ describe('AdminService', () => {
46
46
  };
47
47
  beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
48
48
  jest.clearAllMocks();
49
+ const mockApp = { name: 'app-name', options: {} };
50
+ firebaseAdmin.initializeApp.mockReturnValue(mockApp);
51
+ firebaseAdmin.credential.cert.mockReturnValue('mocked-credential');
49
52
  const module = yield testing_1.Test.createTestingModule({
50
53
  providers: [
51
54
  admin_service_1.AdminService,
@@ -53,6 +56,10 @@ describe('AdminService', () => {
53
56
  provide: FIREBASE_ADMIN_INSTANCE_TOKEN,
54
57
  useValue: mockOptions,
55
58
  },
59
+ {
60
+ provide: 'FIREBASE_ADMIN_APP',
61
+ useValue: mockApp,
62
+ },
56
63
  ],
57
64
  }).compile();
58
65
  service = module.get(admin_service_1.AdminService);
@@ -61,11 +68,8 @@ describe('AdminService', () => {
61
68
  expect(service).toBeDefined();
62
69
  });
63
70
  describe('constructor', () => {
64
- it('must initialize the Firebase Admin SDK with the provided options', () => {
65
- expect(firebaseAdmin.initializeApp).toHaveBeenCalledWith(Object.assign(Object.assign({}, mockOptions), { credential: 'mocked-credential' }));
66
- });
67
- it('must pass the correct credentials to the cert method', () => {
68
- expect(firebaseAdmin.credential.cert).toHaveBeenCalledWith(mockOptions.credential);
71
+ it('must store the injected Firebase Admin app', () => {
72
+ expect(service.appRef).toEqual({ name: 'app-name', options: {} });
69
73
  });
70
74
  });
71
75
  describe('applicationDefault', () => {
@@ -142,7 +146,7 @@ describe('AdminService', () => {
142
146
  observable.subscribe({
143
147
  next: (app) => {
144
148
  expect(app).toBeDefined();
145
- expect(app).toEqual(service.appRef);
149
+ expect(app).toEqual({ name: 'app-name', options: {} });
146
150
  },
147
151
  complete: () => {
148
152
  done();
@@ -176,31 +180,15 @@ describe('AdminService', () => {
176
180
  });
177
181
  });
178
182
  describe('appRef', () => {
179
- it('should return the result of initializeApp', () => {
180
- const mockApp = { name: 'initialized-app' };
181
- firebaseAdmin.initializeApp.mockReturnValueOnce(mockApp);
183
+ it('should return the injected app instance', () => {
182
184
  const result = service.appRef;
183
- expect(firebaseAdmin.initializeApp).toHaveBeenCalled();
184
- expect(result).toEqual(mockApp);
185
+ expect(result).toEqual({ name: 'app-name', options: {} });
185
186
  });
186
187
  });
187
188
  describe('initializeApp', () => {
188
- it('should call initializeApp from Admin SDK with correct options', () => {
189
- firebaseAdmin.initializeApp.mockClear();
190
- service.initializeApp();
191
- expect(firebaseAdmin.initializeApp).toHaveBeenCalledWith(Object.assign(Object.assign({}, mockOptions), { credential: 'mocked-credential' }));
192
- });
193
- it('should return the initialized app instance', () => {
194
- const mockApp = { name: 'new-app', options: {} };
195
- firebaseAdmin.initializeApp.mockReturnValueOnce(mockApp);
189
+ it('should return the injected app instance', () => {
196
190
  const result = service.initializeApp();
197
- expect(result).toEqual(mockApp);
198
- });
199
- it('must propagate initializeApp errors', () => {
200
- firebaseAdmin.initializeApp.mockImplementationOnce(() => {
201
- throw new Error('Initialization error');
202
- });
203
- expect(() => service.initializeApp()).toThrow('Initialization error');
191
+ expect(result).toEqual({ name: 'app-name', options: {} });
204
192
  });
205
193
  });
206
194
  describe('complex use cases', () => {
@@ -213,14 +201,10 @@ describe('AdminService', () => {
213
201
  expect(firebaseAdminApp.deleteApp).toHaveBeenCalledWith(mockApp);
214
202
  }));
215
203
  it('should return the same app when appRef is called multiple times in a row', () => {
216
- const mockApp = { name: 'reused-app' };
217
- firebaseAdmin.initializeApp.mockClear();
218
- firebaseAdmin.initializeApp.mockReturnValue(mockApp);
219
- const app1 = service.appRef;
220
- const app2 = service.appRef;
221
- expect(app1).toBe(mockApp);
222
- expect(app2).toBe(mockApp);
223
- expect(firebaseAdmin.initializeApp).toHaveBeenCalledTimes(2);
204
+ const firstCall = service.appRef;
205
+ const secondCall = service.appRef;
206
+ expect(firstCall).toBe(secondCall);
207
+ expect(firstCall).toEqual({ name: 'app-name', options: {} });
224
208
  });
225
209
  it('should be defined', () => {
226
210
  expect(service).toBeDefined();
@@ -1,6 +1,11 @@
1
+ import type { AdminModuleOptions } from './types';
2
+ import { App } from 'firebase-admin/app';
1
3
  export declare class DatabaseService {
4
+ protected readonly options: AdminModuleOptions;
5
+ private readonly firebaseApp;
6
+ private app;
2
7
  private database;
3
- constructor();
8
+ constructor(options: AdminModuleOptions, firebaseApp: App);
4
9
  ref(path: string): import("@firebase/database-types").Reference;
5
10
  get<T>(path: string): Promise<T>;
6
11
  set<T>(path: string, data: T): Promise<void>;
@@ -8,6 +8,9 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
8
  var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
11
14
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
12
15
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
13
16
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -21,8 +24,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
21
24
  exports.DatabaseService = void 0;
22
25
  const common_1 = require("@nestjs/common");
23
26
  const database_1 = require("firebase-admin/database");
27
+ const admin_constants_1 = require("./admin.constants");
24
28
  let DatabaseService = class DatabaseService {
25
- constructor() {
29
+ constructor(options, firebaseApp) {
30
+ this.options = options;
31
+ this.firebaseApp = firebaseApp;
32
+ this.app = firebaseApp;
26
33
  this.database = (0, database_1.getDatabase)();
27
34
  }
28
35
  ref(path) {
@@ -59,5 +66,7 @@ let DatabaseService = class DatabaseService {
59
66
  exports.DatabaseService = DatabaseService;
60
67
  exports.DatabaseService = DatabaseService = __decorate([
61
68
  (0, common_1.Injectable)(),
62
- __metadata("design:paramtypes", [])
69
+ __param(0, (0, common_1.Inject)(admin_constants_1.FIREBASE_ADMIN_INSTANCE_TOKEN)),
70
+ __param(1, (0, common_1.Inject)('FIREBASE_ADMIN_APP')),
71
+ __metadata("design:paramtypes", [Object, Object])
63
72
  ], DatabaseService);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nestjs-firebase-admin",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
4
4
  "description": "Firebase Admin SDK for Nestjs",
5
5
  "author": "Hebert Cisco",
6
6
  "license": "MIT",
@@ -8,6 +8,7 @@
8
8
  "main": "./dist/index.js",
9
9
  "types": "./dist/index.d.ts",
10
10
  "scripts": {
11
+ "start:debug": "nest start --debug --watch",
11
12
  "build": "npx tsc",
12
13
  "prebuild": "rimraf dist",
13
14
  "format": "npx prettier --write \"{lib,test}/**/*.ts\"",