From 18f13cfcfb81b6d4cb415cfeccd70792a38f25a8 Mon Sep 17 00:00:00 2001 From: Taimoor Farras Date: Sat, 11 Apr 2020 18:28:31 +0300 Subject: [PATCH] first commit --- .gitignore | 36 +++++++++++++++ CHANGELOG.md | 1 + CONTRIBUTING.md | 23 +++++++++ README.md | 62 ++++++++++++++++++++++++- nest-cli.json | 5 ++ package.json | 82 +++++++++++++++++++++++++++++++++ src/firebase-admin-sdk.type.ts | 3 ++ src/firebase-admin.constant.ts | 3 ++ src/firebase-admin.interface.ts | 13 ++++++ src/firebase-admin.module.ts | 53 +++++++++++++++++++++ src/index.ts | 4 ++ tsconfig.build.json | 4 ++ tsconfig.json | 17 +++++++ tslint.json | 18 ++++++++ 14 files changed, 322 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 CONTRIBUTING.md create mode 100644 nest-cli.json create mode 100644 package.json create mode 100644 src/firebase-admin-sdk.type.ts create mode 100644 src/firebase-admin.constant.ts create mode 100644 src/firebase-admin.interface.ts create mode 100644 src/firebase-admin.module.ts create mode 100644 src/index.ts create mode 100644 tsconfig.build.json create mode 100644 tsconfig.json create mode 100644 tslint.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd9fe10 --- /dev/null +++ b/.gitignore @@ -0,0 +1,36 @@ +# compiled output +/dist +/node_modules + +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# OS +.DS_Store + +# Tests +/coverage +/.nyc_output + +# IDEs and editors +/.idea +.project +.classpath +.c9/ +*.launch +.settings/ +*.sublime-workspace + +# IDE - VSCode +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json + +package-lock.json \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..6209b30 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1 @@ +RELEASE 1.0.0 \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..4530c64 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,23 @@ +# Contributing + +1. [Fork it](https://help.github.com/articles/fork-a-repo/) +2. Install dependencies (`npm install`) +3. Create your feature branch (`git checkout -b my-new-feature`) +4. Commit your changes (`git commit -am 'Added some feature'`) +5. Push to the branch (`git push origin my-new-feature`) +6. [Create new Pull Request](https://help.github.com/articles/creating-a-pull-request/) + +## Code Style + +We use [Prettier](https://prettier.io/) and tslint to maintain code style and best practices. +Please make sure your PR adheres to the guides by running: + +```bash +npm run format +``` + +and + +```bash +npm run lint +``` diff --git a/README.md b/README.md index 4462910..2909f62 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,60 @@ -# nestjs-firebase-admin -NestJS Module for Firebase Admin SDK +

+ +
+ + Nest Logo + +
+ +

NestJS Module for Firebase Admin SDK

+ +
+ + Built with NestJS + +
+ +# Installation + +```bash +npm install @tfarras/nestjs-firebase-admin +``` + +## Import module + +```typescript +import { Module } from '@nestjs/common'; +import { FirebaseAdminModule } from '@tfarras/nestjs-firebase-admin' +import * as admin from 'firebase-admin' + +@Module({ + imports: [ + FirebaseAdminModule.forRootAsync({ + useFactory: () => ({ + credential: admin.credential.applicationDefault() + }) + }), + ], +}) +export class AppModule {} +``` + +# Example + +## Inject FirebaseAdminSDK + +```typescript +import { Injectable, Inject } from '@nestjs/common'; +import { FIREBASE_ADMIN_INJECT, FirebaseAdminSDK } from '@tfarras/nestjs-firebase-admin'; + +@Injectable() +export class AppService { + constructor( + @Inject(FIREBASE_ADMIN_INJECT) private firebaseAdmin: FirebaseAdminSDK, + ) {} + + getUsers() { + return this.firebaseAdmin.auth().listUsers(); + } +} +``` diff --git a/nest-cli.json b/nest-cli.json new file mode 100644 index 0000000..0dde5f8 --- /dev/null +++ b/nest-cli.json @@ -0,0 +1,5 @@ +{ + "language": "ts", + "collection": "@nestjs/schematics", + "sourceRoot": "src" +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..19f270b --- /dev/null +++ b/package.json @@ -0,0 +1,82 @@ +{ + "name": "@tfarras/nestjs-firebase-admin", + "version": "1.0.0", + "description": "NestJS Module for Firebase Admin SDK", + "author": "Taimoor Farras ", + "readmeFilename": "README.md", + "main": "dist/index.js", + "license": "MIT", + "files": [ + "dist/**/*", + "*.md" + ], + "scripts": { + "start:dev": "tsc -w", + "build": "tsc", + "prepare": "npm run build", + "format": "prettier --write \"src/**/*.ts\"", + "lint": "tslint -p tsconfig.json -c tslint.json", + "test": "jest", + "test:watch": "jest --watch", + "test:cov": "jest --coverage", + "test:e2e": "jest --config ./test/jest-e2e.json" + }, + "keywords": [ + "nestjs", + "firebase", + "firebase-admin", + "sdk" + ], + "publishConfig": { + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/tfarras/nestjs-firebase-admin-sdk.git" + }, + "bugs": { + "url": "https://github.com/tfarras/nestjs-firebase-admin-sdk/issues" + }, + "homepage": "https://github.com/tfarras/nestjs-firebase-admin-sdk#readme", + "peerDependencies": { + "@nestjs/common": "^6.0.0", + "firebase-admin": "^8.10.0" + }, + "dependencies": { + "firebase-admin": "^8.10.0" + }, + "devDependencies": { + "@nestjs/common": "^6.0.0", + "@nestjs/core": "^6.0.0", + "@nestjs/platform-express": "^6.0.0", + "@nestjs/testing": "6.1.1", + "@types/express": "4.16.1", + "@types/jest": "24.0.11", + "@types/node": "11.13.4", + "@types/supertest": "2.0.7", + "rxjs": "^6.5.4", + "jest": "24.7.1", + "prettier": "1.17.0", + "supertest": "4.0.2", + "ts-jest": "24.0.2", + "ts-node": "8.1.0", + "tsc-watch": "2.2.1", + "tsconfig-paths": "3.8.0", + "tslint": "5.16.0", + "typescript": "3.7.2" + }, + "jest": { + "moduleFileExtensions": [ + "js", + "json", + "ts" + ], + "rootDir": "src", + "testRegex": ".spec.ts$", + "transform": { + "^.+\\.(t|j)s$": "ts-jest" + }, + "coverageDirectory": "../coverage", + "testEnvironment": "node" + } +} \ No newline at end of file diff --git a/src/firebase-admin-sdk.type.ts b/src/firebase-admin-sdk.type.ts new file mode 100644 index 0000000..2b3155f --- /dev/null +++ b/src/firebase-admin-sdk.type.ts @@ -0,0 +1,3 @@ +import * as admin from 'firebase-admin'; + +export type FirebaseAdminSDK = admin.app.App; diff --git a/src/firebase-admin.constant.ts b/src/firebase-admin.constant.ts new file mode 100644 index 0000000..395dc34 --- /dev/null +++ b/src/firebase-admin.constant.ts @@ -0,0 +1,3 @@ +export const FIREBASE_ADMIN_MODULE_OPTIONS = 'FIREBASE_ADMIN_MODULE_OPTIONS'; +export const FIREBASE_ADMIN_NAME = 'FIREBASE_ADMIN_NAME'; +export const FIREBASE_ADMIN_INJECT = 'FIREBASE_ADMIN_INJECT'; diff --git a/src/firebase-admin.interface.ts b/src/firebase-admin.interface.ts new file mode 100644 index 0000000..8eb7940 --- /dev/null +++ b/src/firebase-admin.interface.ts @@ -0,0 +1,13 @@ +import * as admin from 'firebase-admin'; +import { ModuleMetadata } from '@nestjs/common/interfaces'; + +export type FirebaseUser = admin.auth.DecodedIdToken; + +export interface FirebaseAdminModuleAsyncOptions + extends Pick { + name?: string; + useFactory?: ( + ...args: any[] + ) => Promise | admin.AppOptions; + inject?: any[]; +} diff --git a/src/firebase-admin.module.ts b/src/firebase-admin.module.ts new file mode 100644 index 0000000..58224fe --- /dev/null +++ b/src/firebase-admin.module.ts @@ -0,0 +1,53 @@ +import { Global, Module, DynamicModule } from '@nestjs/common'; +import { FirebaseAdminModuleAsyncOptions } from './firebase-admin.interface'; +import { FIREBASE_ADMIN_MODULE_OPTIONS, FIREBASE_ADMIN_INJECT } from './firebase-admin.constant'; +import * as admin from 'firebase-admin'; + +@Global() +@Module({}) +export class FirebaseAdminCoreModule { + static forRoot(options: admin.AppOptions): DynamicModule { + const firebaseAdminModuleOptions = { + provide: FIREBASE_ADMIN_MODULE_OPTIONS, + useValue: options, + }; + + const app = admin.apps.length === 0 ? admin.initializeApp(options) : admin.apps[0]; + + const firebaseAuthencationProvider = { + provide: FIREBASE_ADMIN_INJECT, + useValue: app, + }; + + return { + module: FirebaseAdminCoreModule, + providers: [firebaseAdminModuleOptions, firebaseAuthencationProvider], + exports: [firebaseAdminModuleOptions, firebaseAuthencationProvider], + }; + } + + static forRootAsync(options: FirebaseAdminModuleAsyncOptions): DynamicModule { + const firebaseAdminModuleOptions = { + provide: FIREBASE_ADMIN_MODULE_OPTIONS, + useFactory: options.useFactory, + inject: options.inject || [], + }; + + const firebaseAuthencationProvider = { + provide: FIREBASE_ADMIN_INJECT, + useFactory: (opt: admin.AppOptions) => { + const app = admin.apps.length === 0 ? admin.initializeApp(opt) : admin.apps[0]; + + return app; + }, + inject: [FIREBASE_ADMIN_MODULE_OPTIONS], + }; + + return { + module: FirebaseAdminCoreModule, + imports: options.imports, + providers: [firebaseAdminModuleOptions, firebaseAuthencationProvider], + exports: [firebaseAdminModuleOptions, firebaseAuthencationProvider], + }; + } +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..bfe20b5 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,4 @@ +export * from './firebase-admin-sdk.type'; +export * from './firebase-admin.constant'; +export * from './firebase-admin.interface'; +export * from './firebase-admin.module'; diff --git a/tsconfig.build.json b/tsconfig.build.json new file mode 100644 index 0000000..64f86c6 --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,4 @@ +{ + "extends": "./tsconfig.json", + "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..9e6f526 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "module": "commonjs", + "declaration": true, + "removeComments": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "target": "es6", + "sourceMap": false, + "outDir": "./dist", + "rootDir": "./src", + "baseUrl": "./", + "noLib": false + }, + "include": ["src/**/*.ts"], + "exclude": ["node_modules"] +} diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000..5651b2f --- /dev/null +++ b/tslint.json @@ -0,0 +1,18 @@ +{ + "defaultSeverity": "error", + "extends": ["tslint:recommended"], + "jsRules": { + "no-unused-expression": true + }, + "rules": { + "quotemark": [true, "single"], + "member-access": [false], + "ordered-imports": [false], + "max-line-length": [true, 150], + "member-ordering": [false], + "interface-name": [false], + "arrow-parens": false, + "object-literal-sort-keys": false + }, + "rulesDirectory": [] +}