Skip to content

Commit 534df22

Browse files
committed
WIP
1 parent 8fd97db commit 534df22

25 files changed

+1649
-2993
lines changed

functions/package-lock.json

Lines changed: 1183 additions & 1466 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

functions/package.json

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,17 @@
1818
},
1919
"main": "lib/index.js",
2020
"dependencies": {
21+
"@fastify/auth": "^5.0.3",
22+
"@fastify/cors": "^11.1.0",
23+
"@fastify/swagger": "^9.5.2",
24+
"@fastify/swagger-ui": "^5.2.3",
25+
"@fastify/type-provider-typebox": "^6.0.0",
2126
"@google-cloud/storage": "^5.11.1",
27+
"@sinclair/typebox": "^0.34.41",
2228
"@types/jest": "^27.4.0",
2329
"@types/jest-expect-message": "^1.0.3",
30+
"fastify": "^5.6.1",
31+
"fastify-plugin": "^5.1.0",
2432
"firebase": "^8.8.1",
2533
"firebase-admin": "^12.1.0",
2634
"firebase-functions": "^5.0.1",
@@ -29,34 +37,16 @@
2937
"form-data": "^3.0.1",
3038
"lodash": "^4.17.21",
3139
"node-fetch": "^2.6.1",
32-
"fastify": "^4.24.3",
33-
"@fastify/swagger": "^8.12.0",
34-
"@fastify/swagger-ui": "^2.1.0",
35-
"@fastify/cors": "^9.0.1",
36-
"@fastify/helmet": "^11.1.1",
37-
"@fastify/rate-limit": "^9.1.0",
38-
"@sinclair/typebox": "^0.31.28",
39-
"jsonwebtoken": "^9.0.2",
40-
"bcryptjs": "^2.4.3",
41-
"ajv": "^8.12.0",
42-
"ajv-formats": "^2.1.1",
43-
"fastify-plugin": "^4.5.1",
44-
"@fastify/type-provider-typebox": "^3.5.0"
40+
"ts-custom-error": "^3.3.1"
4541
},
4642
"devDependencies": {
47-
"@types/node-fetch": "^2.5.12",
48-
"@types/jsonwebtoken": "^9.0.5",
49-
"@types/bcryptjs": "^2.4.6",
5043
"@typescript-eslint/eslint-plugin": "^5.12.0",
5144
"@typescript-eslint/parser": "^5.12.0",
5245
"eslint": "^8.9.0",
5346
"eslint-config-google": "^0.14.0",
5447
"eslint-plugin-import": "^2.25.4",
5548
"jest-expect-message": "^1.0.2",
56-
"typescript": "^4.9.5",
57-
"vitest": "^2.0.5",
58-
"nodemon": "^3.0.2",
59-
"pino-pretty": "^10.3.1"
49+
"typescript": "^4.9.5"
6050
},
6151
"private": true
6252
}

functions/src/api/README.md

Lines changed: 0 additions & 254 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { FastifyInstance } from 'fastify'
2+
3+
export const addContentTypeParserForServerless = (fastify: FastifyInstance) => {
4+
// For serverless compatibility
5+
fastify.addContentTypeParser('application/json', {}, (req, body, done) => {
6+
done(null, (body as any).body)
7+
})
8+
fastify.addContentTypeParser(
9+
'multipart/form-data',
10+
{},
11+
(req, body, done) => {
12+
done(null, req)
13+
}
14+
)
15+
}

functions/src/api/api.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
1-
import { buildServer } from './server'
2-
import { createConfig } from './config'
1+
import Fastify from 'fastify'
2+
import { addContentTypeParserForServerless } from './addContentTypeParserForServerless'
3+
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'
4+
import { fastifyAuth, FastifyAuthFunction } from '@fastify/auth'
5+
import { app as firebaseApp } from 'firebase-admin'
6+
import { apiKeyPlugin } from './plugins/apiKeyPlugin'
7+
import { firebasePlugin } from './plugins/firebasePlugin'
8+
import cors from '@fastify/cors'
9+
import { swaggerPlugin } from './plugins/swaggerPlugin'
10+
import { fastifyErrorHandler } from './others/fastifyErrorHandler'
11+
import { fastifyNotFoundHandler } from './others/fastifyNotFoundHandler'
312

4-
export async function createApiServer() {
5-
const config = createConfig()
6-
const server = await buildServer(config)
7-
8-
return server
13+
type Firebase = firebaseApp.App
14+
declare module 'fastify' {
15+
interface FastifyInstance {
16+
firebase: Firebase
17+
verifyApiKey: FastifyAuthFunction
18+
}
919
}
1020

11-
export { buildServer, createConfig }
12-
export * from './schemas'
13-
export * from './plugins/auth'
14-
export * from './plugins/error-handler'
21+
export async function createFastifyAPI() {
22+
const fastify = Fastify({
23+
logger: true, // always enable full log
24+
}).withTypeProvider<TypeBoxTypeProvider>()
25+
addContentTypeParserForServerless(fastify)
26+
27+
fastify.register(fastifyAuth)
28+
fastify.register(firebasePlugin)
29+
fastify.register(apiKeyPlugin)
30+
fastify.register(cors, {
31+
origin: '*',
32+
})
33+
fastify.register(swaggerPlugin)
34+
fastify.setErrorHandler(fastifyErrorHandler)
35+
fastify.setNotFoundHandler(fastifyNotFoundHandler)
36+
37+
fastify.addHook('onSend', (_, reply, _2, done: () => void) => {
38+
reply.header('Cache-Control', 'must-revalidate,no-cache,no-store')
39+
done()
40+
})
41+
return fastify
42+
}

0 commit comments

Comments
 (0)