Quick start
Register the plugin before declaring any QUERY route. The plugin adds the QUERY method to Fastify via addHttpMethod and exposes the query route shorthand.
Minimal example
js
import Fastify from 'fastify'
import fastifyHttpQuery from '@thecodepace/fastify-http-query'
const app = Fastify()
await app.register(fastifyHttpQuery)
// QUERY route — the body carries the query, just like GET carries the URL.
app.query('/search', {
schema: {
body: {
type: 'object',
properties: { q: { type: 'string' } },
required: ['q']
}
}
}, async (request) => {
return runSearch(request.body.q)
})
await app.listen({ port: 3000 })Calling it
sh
curl -X QUERY http://localhost:3000/search \
-H 'content-type: application/json' \
--data '{"q":"fastify"}'A missing Content-Type or an empty body is rejected with 400; see Error codes for the exact codes.
Plugin options
The plugin has no options. The signature is purely a FastifyPluginAsync:
ts
import type { FastifyPluginAsync } from 'fastify'
const fastifyHttpQuery: FastifyPluginAsyncWhat registering actually does
- Calls
fastify.addHttpMethod('QUERY', { hasBody: true })ifQUERYis not already infastify.supportedMethods(guards against double registration). - Adds an
onRequesthook that, forQUERYrequests only:- Rejects requests without a
Content-Typeheader withFST_ERR_QUERY_MISSING_CONTENT_TYPE. - Rejects requests without a body (no
Content-Length > 0and noTransfer-Encoding) withFST_ERR_QUERY_EMPTY_BODY.
- Rejects requests without a
Both errors carry statusCode: 400 and can be matched with instanceof against the exported error constructors — see Error codes.
Next steps
- Add caching and conditional requests on top of
@fastify/etagand@fastify/caching. - Set a Content-Location header on successful responses.