Skip to content

Commit 553a0cc

Browse files
Resul AvanResul Avan
authored andcommitted
handler-module implementation
1 parent ef74895 commit 553a0cc

File tree

101 files changed

+421
-155
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+421
-155
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ firebase-debug.log
102102
node_modules
103103

104104
# modules
105-
*/lib
105+
lib
106+
**/*.tar.gz
106107

107108
# firebase
108109
functions/nuxt.config.ts

functions/modules/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# modules
2+
3+
## build
4+
npm i
5+
npm run build
6+
7+
## install local modules
8+
npm i <local-path-of-the-module>
9+
10+
or
11+
npm i <tar.gz-path-of-the-module>
12+
13+
14+
sample
15+
16+
npm i ../modules/types-module
17+
npm i ../modules/types-module/types-module-1.0.0.tgz
18+
19+
## remove local modules
20+
npm remove <package-name-of-the-module>
21+
22+
sample
23+
24+
npm remove types-module
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# types module
2+
3+
## build
4+
npm i
5+
npm run build
6+
7+
## install
8+
npm i ../modules/types-module
9+
10+
## remove local modules
11+
npm remove types-module
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "handlers-module",
3+
"version": "1.0.0",
4+
"description": "Common types between backend and frontend",
5+
"main": "./lib/cjs/index.js",
6+
"typings": "./lib/cjs/index.d.ts",
7+
"scripts": {
8+
"prebuild": "npm run lint && rm -rf ./lib",
9+
"build": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json",
10+
"lint": "tslint --project tsconfig.json",
11+
"test": "echo \"Error: no test specified\" && exit 1"
12+
},
13+
"keywords": [],
14+
"author": "resul avan <[email protected]>",
15+
"license": "ISC",
16+
"devDependencies": {
17+
"@types/pluralize": "0.0.29",
18+
"prettier": "^2.0.5",
19+
"tslint": "^6.1.2",
20+
"tslint-config-prettier": "^1.18.0",
21+
"typescript": "^3.9.7"
22+
},
23+
"dependencies": {
24+
"@types/express": "^4.17.7",
25+
"@types/uuid": "^8.0.0",
26+
"express": "^4.17.1",
27+
"firebase": "^7.16.1",
28+
"firebase-admin": "^9.0.0",
29+
"http-status-codes": "^1.4.0",
30+
"sitemap": "^6.2.0",
31+
"types-module": "file:../types-module",
32+
"uuid": "^8.2.0"
33+
}
34+
}

functions/src/api/api-handler.ts renamed to functions/modules/handlers-module/src/api/api-handler.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { NextFunction, Request, RequestHandler, Response } from 'express'
2-
import { OK } from 'http-status-codes'
32
import admin from 'firebase-admin'
43
import {
54
extractHeadersFromRequest,
@@ -8,8 +7,8 @@ import {
87
handlerCalledLog
98
} from '../service/request-handler-service';
109
import { getDecodedIdToken } from '../service/firebase-admin-service'
10+
import { ApiErrorCode } from 'types-module'
1111
import DecodedIdToken = admin.auth.DecodedIdToken
12-
import { ApiErrorCode } from 'common-types'
1312

1413
export const extractHeaderHandler: RequestHandler = async (req: Request, res: Response, next: NextFunction) => {
1514
await extractHeadersFromRequest(req)
@@ -37,8 +36,3 @@ export const tokenHandler: RequestHandler = async (req: Request, res: Response,
3736
})
3837
.catch((error: Error) => handleApiErrors(req, res, error))
3938
}
40-
41-
export const healthyHandler: RequestHandler = (req, res) => {
42-
console.log(`${req.originalUrl} - healthyHandler called`)
43-
return res.status(OK).send('OK')
44-
}

functions/src/api/auth-handler.ts renamed to functions/modules/handlers-module/src/api/auth-handler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { RequestHandler } from 'express'
22
import { NO_CONTENT, OK } from 'http-status-codes'
33
import { setCustomClaims, toAuthUser, validateClaimsAndGet } from '../service/firebase-admin-service'
4-
import { ApiErrorCode, FirebaseClaimKey, FirebaseClaims } from 'common-types'
4+
import { ApiErrorCode, FirebaseClaimKey, FirebaseClaims } from 'types-module'
55
import {
66
getDecodedIdTokenFromRequest,
77
handleApiErrors,
@@ -27,6 +27,7 @@ export const verifyHandler: RequestHandler = async (req, res) => {
2727
}
2828

2929
export const claimsHandler: RequestHandler = async (req, res) => {
30+
3031
handlerCalledLog(req, 'claimsHandler')
3132
await getDecodedIdTokenFromRequest(req)
3233
.then(async (decodedIdToken: DecodedIdToken) => {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Request, RequestHandler, Response } from 'express'
2+
import { OK } from 'http-status-codes'
3+
import { ProviderType } from 'types-module'
4+
5+
export const healthyHandler: RequestHandler = (req: Request, res: Response) => {
6+
console.log(`${req.originalUrl} - healthyHandler called`, ProviderType.PASSWORD)
7+
return res.status(OK).send('OK')
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './healthy-handler'
2+
export * from './api-handler'
3+
export * from './auth-handler'
4+
export * from './notification-handler'

functions/src/api/notification-handler.ts renamed to functions/modules/handlers-module/src/api/notification-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
handlerCalledLog,
88
handlerLog
99
} from '../service/request-handler-service'
10-
import { ApiErrorCode, UserDevice } from 'common-types'
10+
import { ApiErrorCode, UserDevice } from 'types-module'
1111
import { deleteUserDevice, getPushNotification, getUserDevices } from '../service/firebase-admin-service'
1212
import DecodedIdToken = admin.auth.DecodedIdToken
1313

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import admin from 'firebase-admin'
2+
3+
export class HandlerConfig {
4+
5+
static initializer: () => void
6+
7+
static defaultInitializer () {
8+
admin.initializeApp({
9+
credential: admin.credential.applicationDefault()
10+
})
11+
}
12+
13+
static setInitializer (initializer: () => void) {
14+
this.initializer = initializer
15+
}
16+
17+
static initialize () {
18+
console.log('CUSTOM initializer: ', JSON.stringify(this.initializer))
19+
20+
if (admin.apps.length === 0) {
21+
this.initializer ? this.initializer() : this.defaultInitializer()
22+
}
23+
}
24+
25+
}

0 commit comments

Comments
 (0)