Skip to content

Commit 7e50e53

Browse files
lirantaljsumners
andauthored
feat: add a Firebase Functions step by step guide (fastify#5318)
* feat: add a Firebase Functions step by step guide This PR adds a new section to the Serverless Guide on the website that explains how to use Fastify as the wrapper around Firebase's own onRequest HTTP handler Signed-off-by: Liran Tal <[email protected]> * Update docs/Guides/Serverless.md Co-authored-by: James Sumners <[email protected]> Signed-off-by: Liran Tal <[email protected]> * Update docs/Guides/Serverless.md Co-authored-by: James Sumners <[email protected]> Signed-off-by: Liran Tal <[email protected]> * Update docs/Guides/Serverless.md Co-authored-by: James Sumners <[email protected]> Signed-off-by: Liran Tal <[email protected]> * Update docs/Guides/Serverless.md Co-authored-by: James Sumners <[email protected]> Signed-off-by: Liran Tal <[email protected]> --------- Signed-off-by: Liran Tal <[email protected]> Co-authored-by: James Sumners <[email protected]>
1 parent 54f8e3c commit 7e50e53

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

docs/Guides/Serverless.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ snippet of code.
2626

2727
- [AWS](#aws)
2828
- [Google Cloud Functions](#google-cloud-functions)
29+
- [Google Firebase Functions](#google-firebase-functions)
2930
- [Google Cloud Run](#google-cloud-run)
3031
- [Netlify Lambda](#netlify-lambda)
3132
- [Platformatic Cloud](#platformatic-cloud)
@@ -260,6 +261,115 @@ curl -X POST https://$GOOGLE_REGION-$GOOGLE_PROJECT.cloudfunctions.net/me \
260261
- [Google Cloud Functions - Node.js Quickstart
261262
](https://cloud.google.com/functions/docs/quickstart-nodejs)
262263

264+
## Google Firebase Functions
265+
266+
Follow this guide if you want to use Fastify as the HTTP framework for
267+
Firebase Functions instead of the vanilla JavaScript router provided with
268+
`onRequest(async (req, res) => {}`.
269+
270+
### The onRequest() handler
271+
272+
We use the `onRequest` function to wrap our Fastify application instance.
273+
274+
As such, we'll begin with importing it to the code:
275+
276+
```js
277+
const { onRequest } = require("firebase-functions/v2/https")
278+
```
279+
280+
### Creation of Fastify instance
281+
282+
Create the Fastify instance and encapsulate the returned application instance
283+
in a function which will register routes, await the server's processing of
284+
plugins, hooks and other settings. As follows:
285+
286+
```js
287+
const fastify = require("fastify")({
288+
logger: true,
289+
})
290+
291+
const fastifyApp = async (request, reply) => {
292+
await registerRoutes(fastify)
293+
await fastify.ready()
294+
fastify.server.emit("request", request, reply)
295+
}
296+
```
297+
298+
### Add Custom `contentTypeParser` to Fastify instance and define endpoints
299+
300+
Firebase Function's HTTP layer already parses the request
301+
and makes a JSON payload available. It also provides access
302+
to the raw body, unparsed, which is useful in order to calculate
303+
request signatures to validate HTTP webhooks.
304+
305+
Add as follows to the `registerRoutes()` function:
306+
307+
```js
308+
async function registerRoutes (fastify) {
309+
fastify.addContentTypeParser("application/json", {}, (req, payload, done) => {
310+
// useful to include the request's raw body on the `req` object that will
311+
// later be available in your other routes so you can calculate the HMAC
312+
// if needed
313+
req.rawBody = payload.rawBody
314+
315+
// payload.body is already the parsed JSON so we just fire the done callback
316+
// with it
317+
done(null, payload.body)
318+
})
319+
320+
// define your endpoints here...
321+
fastify.post("/some-route-here", async (request, reply) => {}
322+
323+
fastify.get('/', async (request, reply) => {
324+
reply.send({message: 'Hello World!'})
325+
})
326+
}
327+
```
328+
329+
### Export the function using Firebase onRequest
330+
331+
Final step is to export the Fastify app instance to Firebase's own
332+
`onRequest()` function so it can pass the request and reply objects to it:
333+
334+
```js
335+
exports.app = onRequest(fastifyApp)
336+
```
337+
338+
### Local test
339+
340+
Install the Firebase tools functions so you can use the CLI:
341+
342+
```bash
343+
npm i -g firebase-tools
344+
```
345+
346+
Then you can run your function locally with:
347+
348+
```bash
349+
firebase emulators:start --only functions
350+
```
351+
352+
### Deploy
353+
354+
Deploy your Firebase Functions with:
355+
356+
```bash
357+
firebase deploy --only functions
358+
```
359+
360+
#### Read logs
361+
362+
Use the Firebase tools CLI:
363+
364+
```bash
365+
firebase functions:log
366+
```
367+
368+
### References
369+
- [Fastify on Firebase Functions](https://github.com/lirantal/lemon-squeezy-firebase-webhook-fastify/blob/main/package.json)
370+
- [An article about HTTP webhooks on Firebase Functions and Fastify: A Practical Case Study with Lemon Squeezy](https://lirantal.com/blog/http-webhooks-firebase-functions-fastify-practical-case-study-lemon-squeezy)
371+
372+
263373
## Google Cloud Run
264374
265375
Unlike AWS Lambda or Google Cloud Functions, Google Cloud Run is a serverless

0 commit comments

Comments
 (0)