# firebase-functions-router

> Router controller pattern to use with firebase functions.

Latest version **0.0.1** (published 2018-05-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install firebase-functions-router
pnpm add firebase-functions-router
yarn add firebase-functions-router
bun add firebase-functions-router
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2018-05-08 |
| First published | 2018-05-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 64.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Daniel Fischer |
| Maintainers | danielfischer |
| Keywords | firebase, controller, router, typescript |

## Links

- npm: https://www.npmjs.com/package/firebase-functions-router
- Repository: https://github.com/dfischer/firebase-functions-router
- Issues: https://github.com/dfischer/firebase-functions-router/issues
- npm.io page: https://npm.io/package/firebase-functions-router

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 0.0.1 (latest) — 2018-05-08

## README

# 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): Promise<
  functions.Response
> => {
  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
const AuthMiddleware = async router => {
  return {
    req: {
      ...router.req,
      auth: 'dan',
    },
    res: router.res,
  };
};

const FooMiddleware = async router => {
  return {
    req: {
      ...router.req,
      foo: '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.)

---
_Source: https://npm.io/package/firebase-functions-router · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
