Skip to content

Fastify Integration ​

Use Wooks composables with Fastify. This adapter lets you register Wooks-style route handlers on top of an existing Fastify app — unmatched requests automatically fall through to Fastify's not-found handler.

INFO

Source code and issues: github.com/wooksjs/fastify-adapter

Install ​

bash
npm install @wooksjs/fastify-adapter @wooksjs/event-http wooks fastify

Quick Start ​

ts
import Fastify from 'fastify'
import { WooksFastify } from '@wooksjs/fastify-adapter'
import { useRouteParams, useRequest, HttpError } from '@wooksjs/event-http'

const app = Fastify()
const wooks = new WooksFastify(app)

// Return values become the response body
wooks.get('/hello/:name', () => {
    const { get } = useRouteParams()
    return { hello: get('name') }
})

// Async handlers work out of the box
wooks.post('/upload', async () => {
    const { rawBody } = useRequest()
    const body = await rawBody()
    return { received: body.length }
})

// Throw HttpError for error responses
wooks.get('/protected', () => {
    throw new HttpError(403, 'Forbidden')
})

app.listen({ port: 3000 }, () => console.log('listening on 3000'))

How It Works ​

WooksFastify extends WooksHttp and registers a catch-all route in Fastify. When a request comes in:

  1. Wooks checks if a matching route is registered
  2. If matched — the Wooks handler runs with full composable support
  3. If not matched — the request falls through to Fastify's not-found handler

This means you can use Wooks composables for your route handlers while still leveraging Fastify's plugin ecosystem:

ts
import Fastify from 'fastify'
import { WooksFastify } from '@wooksjs/fastify-adapter'

const app = Fastify()

// Wooks handles these routes
const wooks = new WooksFastify(app)
wooks.get('/api/users', () => {
    return [{ id: 1, name: 'Alice' }]
})

app.listen({ port: 3000 })

Content-type parsers are replaced

WooksFastify replaces all Fastify content-type parsers with a raw-buffer parser so Wooks can read bodies on demand. Native Fastify routes on the same instance receive request.body as a Buffer instead of parsed JSON — re-add parsers or parse manually in native routes if you mix them.

API ​

new WooksFastify(fastifyApp, options?) ​

Creates a new adapter instance and registers a catch-all route on the Fastify app.

OptionTypeDefaultDescription
raise404booleanfalseReturn 404 from Wooks for unmatched routes instead of using Fastify's not-found
onNotFound() => unknown—Custom handler for unmatched routes
loggerTConsoleBase—Custom logger instance
routerobject—Router options (ignoreTrailingSlash, ignoreCase, cacheLimit)
requestLimitsobject—Default request body size limits
defaultHeadersRecord<string, string>—Headers added to every response
responseClasstypeof WooksHttpResponse—Custom response class, see Error Responses

Route Methods ​

ts
wooks.get(path, handler)
wooks.post(path, handler)
wooks.put(path, handler)
wooks.patch(path, handler)
wooks.delete(path, handler)
wooks.head(path, handler)
wooks.options(path, handler)
wooks.all(path, handler)

Handlers take no arguments — use composables to access request data:

ts
wooks.get('/users/:id', () => {
    const { get } = useRouteParams()
    const { method, url, rawBody, getIp } = useRequest()
    const headers = useHeaders()
    const response = useResponse()

    response.setHeader('x-custom', 'value')
    return { id: get('id') }
})

wooks.listen(...args) ​

Starts the Fastify server and returns a promise that resolves when listening.

ts
await wooks.listen({ port: 3000 })

wooks.close() ​

Stops the server.

ts
await wooks.close()

Available Composables ​

All @wooksjs/event-http composables work inside Wooks handlers — see the Composables reference.

Released under the MIT License.