Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/borderless/fetch-router
Simple routing middleware for fetch
https://github.com/borderless/fetch-router
cloudflare-workers fetch path-to-regexp router routing
Last synced: about 2 months ago
JSON representation
Simple routing middleware for fetch
- Host: GitHub
- URL: https://github.com/borderless/fetch-router
- Owner: borderless
- License: mit
- Created: 2020-05-14T04:07:18.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-12-12T06:31:03.000Z (about 1 year ago)
- Last Synced: 2024-08-10T10:38:41.981Z (4 months ago)
- Topics: cloudflare-workers, fetch, path-to-regexp, router, routing
- Language: TypeScript
- Homepage:
- Size: 855 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
# Fetch Router
[![NPM version][npm-image]][npm-url]
[![NPM downloads][downloads-image]][downloads-url]
[![Build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]> Simple routing middleware for [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).
## Installation
```sh
npm install @borderless/fetch-router --save
```## Usage
```js
import { compose } from "throwback";
import { get, post, paramsKey } from "@borderless/fetch-router";const animals = ["rabbit", "dog", "cat"];
const app = compose([
get("/pets", function () {
return new Response(animals.join("\n"));
}),
get("/pets/:id", function (req) {
return new Response(animals[Number(req[paramsKey].id)]);
}),
]);
```### Composition
If you need more control, the package exposes the internally used functions: `method` and `use`.
- `use(path, fn, options?)` - Match an incoming request against [Path To RegExp](https://github.com/pillarjs/path-to-regexp).
- `method(verb, fn)` - Match an incoming request against a HTTP method.**Tip:** You can recursively mount routes using `use()` and `{ end: false }`:
```js
const nested = get("/pets", () => new Response("test"));
const app = use("/api", nested, { end: false }); // Allows partial match on `/api/pets`.
```## TypeScript
This project is written using [TypeScript](https://github.com/Microsoft/TypeScript) and publishes the definitions directly to NPM.
## License
MIT
[npm-image]: https://img.shields.io/npm/v/@borderless/fetch-router.svg?style=flat
[npm-url]: https://npmjs.org/package/@borderless/fetch-router
[downloads-image]: https://img.shields.io/npm/dm/@borderless/fetch-router.svg?style=flat
[downloads-url]: https://npmjs.org/package/@borderless/fetch-router
[travis-image]: https://img.shields.io/travis/BorderlessLabs/fetch-router.svg?style=flat
[travis-url]: https://travis-ci.org/BorderlessLabs/fetch-router
[coveralls-image]: https://img.shields.io/coveralls/BorderlessLabs/fetch-router.svg?style=flat
[coveralls-url]: https://coveralls.io/r/BorderlessLabs/fetch-router?branch=master