-
-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathindex.ts
More file actions
40 lines (36 loc) · 1.18 KB
/
index.ts
File metadata and controls
40 lines (36 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { App, type Request, type Response } from '@tinyhttp/app'
import * as functions from 'firebase-functions'
const tinyhttp = new App()
/** Your application code */
tinyhttp.get('/api', (req, res) => {
res.send({ ok: true, q: req.query })
})
tinyhttp.post('/api', (req, res) => {
res.send({ ok: true, b: req.query })
})
tinyhttp.use('/', (_, res) => {
res.send('<h1>Hello World</h1>')
})
/** Make firebase function handler compatible with tinyhttp */
const onRequest = functions.https.onRequest as unknown as (
handler: (req: Request, res: Response) => void | Promise<void>
) => functions.HttpsFunction
// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions
export const app = onRequest((request, response) => {
makeReadonlySettable(request)
tinyhttp.handler(request, response)
})
/** Hack for firebase functions request object, it was read only */
function makeReadonlySettable(req: Request) {
return ['xhr', 'node:path'].forEach((key) => {
Object.defineProperty(req, key, {
get: function () {
return this[`_${key}`]
},
set: function (val) {
this[`_${key}`] = val
}
})
})
}