Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/galvez/fastify-edge
Use Fastify idioms for writing Cloudflare Workers and Bun servers
https://github.com/galvez/fastify-edge
Last synced: 3 months ago
JSON representation
Use Fastify idioms for writing Cloudflare Workers and Bun servers
- Host: GitHub
- URL: https://github.com/galvez/fastify-edge
- Owner: galvez
- Created: 2022-04-24T01:57:10.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-16T20:55:31.000Z (8 months ago)
- Last Synced: 2024-06-20T10:28:07.707Z (5 months ago)
- Language: JavaScript
- Homepage:
- Size: 36.1 KB
- Stars: 94
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-bun - Fastify-Edge - Use Fastify idioms for writing Cloudflare Workers and Bun servers (Frameworks & Libraries)
README
# fastify-edge
An experimental **lightweight worker version** of [Fastify](https://fastify.io).
Currently [**Cloudflare Workers**](https://workers.cloudflare.com/) and [**Bun**](https://bun.sh/) are supported.
## Install
```js
npm i fastify-edge --save
````## Usage: Bun
```js
import FastifyEdge from 'fastify-edge/bun'const app = FastifyEdge();
app.get('/', (_, reply) => {
reply.send('Hello World')
})export default app;
```See [`examples/bun`](https://github.com/galvez/fastify-edge/tree/main/examples/bun).
## Usage: Cloudflare Workers
```js
import FastifyEdge from 'fastify-edge'const app = FastifyEdge()
app.get('/', (_, reply) => {
reply.send('Hello World')
})
```See [`examples/cloudflare`](https://github.com/galvez/fastify-edge/tree/main/examples/cloudflare) with [`miniflare`](https://github.com/cloudflare/miniflare).
## Advanced Example
```js
app.addHook('onSend', (req, reply, payload) => {
if (req.url === '/') {
return `${payload} World!`
}
})app.get('/redirect', (_, reply) => {
reply.redirect('/')
})app.get('/route-hook', {
onRequest (_, reply) {
reply.send('Content from onRequest hook')
},
handler (_, reply) {
reply.type('text/html')
}
})
```## Supported APIs
### Server
- `app.addHook(hook, function)`
- `app.route(settings)`
- `app.get(path, handlerOrSettings)`
- `app.post(path, handlerOrSettings)`
- `app.put(path, handlerOrSettings)`
- `app.delete(path, handlerOrSettings)`
- `app.options(path, handlerOrSettings)`### Request
`req.url`
Returns the request URL path (`URL.pathname` + `URL.search`).
`req.origin`
Returns the request URL origin (e.g., `http://localhost:3000`).
`req.hostname`
Returns the request URL hostname (e.g., `localhost`).
`req.protocol`
Returns the request URL protocol (e.g., `http` or `https`).
`req.query`
Maps to the `fetch` request URL's `searchParams` object through a `Proxy`.`req.body`
The consumed body following the parsing pattern from [this example](https://developers.cloudflare.com/workers/examples/read-post/).
`req.params`
The parsed route params from the internal Radix-tree router, **[radix3](https://github.com/unjs/radix3)**.
`req.headers`
Maps to the `fetch` request `headers` object through a `Proxy`.
`req.raw`
The raw `fetch` request object.
### Reply
`reply.code(code)`
Sets the `fetch` response `status` property.
`reply.header(key, value)`
Adds an individual header to the `fetch` response `headers` object.
`reply.headers(object)`
Adds multiple headers to the `fetch` response `headers` object.
`reply.getHeader(key)`
Retrieves an individual header from `fetch` response `headers` object.
`reply.getHeaders()`
Retrieves all headers from `fetch` response `headers` object.
`reply.removeHeader(key)`
Remove an individual header from `fetch` response `headers` object.
`reply.hasHeader(header)`
Asserts presence of an individual header in the `fetch` response `headers` object.
`reply.redirect(code, dest)`
`reply.redirect(dest)`Sets the `status` and redirect location for the `fetch` response object.
Defaults to the HTTP **302 Found** response code.`reply.type(contentType)`
Sets the `content-type` header for the `fetch` response object.
`reply.send(data)`
Sets the `body` for the `fetch` response object.
Can be a **string**, an **object**, a **buffer** or a **stream**.
Objects are automatically serialized as JSON.
## Supported hooks
The original Fastify
[`onRequest`](https://www.fastify.io/docs/latest/Reference/Hooks/#onrequest),
[`onSend`](https://www.fastify.io/docs/latest/Reference/Hooks/#onsend) and
[`onResponse`](https://www.fastify.io/docs/latest/Reference/Hooks/#onresponse) are supported.Diverging from Fastify, they're all treated as **async functions**.
They can be set at the **global** and **route** levels.
## Limitations
- No support for `preHandler`, `preParsing` and `preValdation` hooks.
- No support for Fastify's plugin system (yet).
- No support for Fastify's logging and validation facilities.
- Still heavily experimental, more equivalent APIs coming soon.