Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dfischer/firebase-functions-router
Router controller pattern to use with firebase functions.
https://github.com/dfischer/firebase-functions-router
firebase firebase-functions typescript
Last synced: 28 days ago
JSON representation
Router controller pattern to use with firebase functions.
- Host: GitHub
- URL: https://github.com/dfischer/firebase-functions-router
- Owner: dfischer
- License: mit
- Created: 2018-05-08T01:33:27.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-05-08T04:45:36.000Z (over 6 years ago)
- Last Synced: 2024-12-15T21:48:10.075Z (about 1 month ago)
- Topics: firebase, firebase-functions, typescript
- Language: TypeScript
- Size: 10.7 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/dfischer/firebase-functions-router.svg?branch=master)](https://travis-ci.org/dfischer/firebase-functions-router)
# firebase-functions-router
Router controller pattern to use with firebase functions or anything else that needs a generic interface to express-like `req, res` expectations.
## Usage
Firebase https onRequest handler
```typescript
export const HelloRouter = functions.https.onRequest(async (req, res) => {
try {
return await MyRouter(req, res);
} catch (e) {
console.error(e);
return res.sendStatus(500);
}
});
```and then you'd have a `HelloRouter` somewhere in your application like so:
```typescript
const HelloRouter = async (req, res) => {
return await Router({
req,
res,
get: async (_, res) => {
return await res.sendStatus(200);
},
});
};
```Add as many method handlers as you want per http spec.
## Middleware
If you want to do something fancy between the request cycle and when the router instance responds, then you may want to use middleware.
The Router takes middleware options like so:
```typescript
// imagine async stuff
const AuthMiddleware = async router => {
return await {
req: {
...router.req,
auth: await getUser('dan')
},
res: router.res,
};
};const FooMiddleware = async router => {
return await {
req: {
...router.req,
foo: await getFoo(); // async func that returns 'bar'
},
res: router.res,
};
};const router = async (req, res) => {
return await Router({
req,
res,
middleware: [AuthMiddleware, FooMiddleware],
get: async (req, res) => {
if (req.auth === 'dan' && req.foo === 'bar')
return await res.sendStatus(200);
},
});
};
```More examples are in the spec files.
# Development
Written in TypeScript.
# Contributing
Just open a PR (with tests if it makes sense.)