Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow access to the body and query #128

Merged
merged 7 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ async function subsystem (fastify, opts) {

If you want to change the Fastify hook that the middleware will be attached to, pass a `hook` option like so:

*Note you can access `req.body` from the `preValidation` lifecycle step onwards. Take a look at the [Lifecycle](https://www.fastify.io/docs/latest/Lifecycle/) documentation page to see the order of the steps.*

```js
const fastify = require('fastify')()

Expand Down
12 changes: 11 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import {NextHandleFunction, SimpleHandleFunction} from 'connect'
import * as connect from 'connect'
import {FastifyPluginCallback} from 'fastify'
import * as http from "http";

export interface IncomingMessageExtended {
body?: any;
query?: any;
}

type NextFunction = (err?: any) => void;
type SimpleHandleFunction = (req: http.IncomingMessage & IncomingMessageExtended, res: http.ServerResponse) => void;
type NextHandleFunction = (req: connect.IncomingMessage & IncomingMessageExtended, res: http.ServerResponse, next: NextFunction) => void;

type Handler = SimpleHandleFunction | NextHandleFunction

Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ function middiePlugin (fastify, options, next) {
req.raw.ip = req.ip
req.raw.ips = req.ips
req.raw.log = req.log
req.raw.body = req.body
req.raw.query = req.query
reply.raw.log = req.log
this[kMiddie].run(req.raw, reply.raw, next)
} else {
Expand Down
30 changes: 20 additions & 10 deletions test/enhance-request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@ const cors = require('cors')
const middiePlugin = require('../index')

test('Should enhance the Node.js core request/response objects', t => {
t.plan(9)
t.plan(13)
const fastify = Fastify()
t.teardown(fastify.close)

fastify.register(middiePlugin)
fastify.register(middiePlugin, { hook: 'preHandler' })
.after(() => { fastify.use(cors()) })

fastify.get('/', async (req, reply) => {
fastify.post('/', async (req, reply) => {
t.equal(req.raw.originalUrl, req.raw.url)
t.equal(req.raw.id, req.id)
t.equal(req.raw.hostname, req.hostname)
t.equal(req.raw.ip, req.ip)
t.same(req.raw.ips, req.ips)
t.same(req.raw.body, req.body)
t.same(req.raw.query, req.query)
t.ok(req.raw.body.bar)
t.ok(req.raw.query.foo)
t.ok(req.raw.log)
t.ok(reply.raw.log)
return { hello: 'world' }
Expand All @@ -29,27 +33,31 @@ test('Should enhance the Node.js core request/response objects', t => {
fastify.listen(0, (err, address) => {
t.error(err)
sget({
method: 'GET',
url: address
method: 'POST',
url: `${address}?foo=bar`,
body: { bar: 'foo' },
json: true
}, (err, res, data) => {
t.error(err)
})
})
})

test('Should not enhance the Node.js core request/response objects when there are no middlewares', t => {
t.plan(9)
t.plan(11)
const fastify = Fastify()
t.teardown(fastify.close)

fastify.register(middiePlugin)
fastify.register(middiePlugin, { hook: 'preHandler' })

fastify.get('/', async (req, reply) => {
fastify.post('/', async (req, reply) => {
t.equal(req.raw.originalUrl, undefined)
t.equal(req.raw.id, undefined)
t.equal(req.raw.hostname, undefined)
t.equal(req.raw.ip, undefined)
t.equal(req.raw.ips, undefined)
t.same(req.raw.body, undefined)
t.same(req.raw.query, undefined)
mcollina marked this conversation as resolved.
Show resolved Hide resolved
t.notOk(req.raw.log)
t.notOk(reply.raw.log)
return { hello: 'world' }
Expand All @@ -58,8 +66,10 @@ test('Should not enhance the Node.js core request/response objects when there ar
fastify.listen(0, (err, address) => {
t.error(err)
sget({
method: 'GET',
url: address
method: 'POST',
url: `${address}?foo=bar`,
body: { bar: 'foo' },
json: true
}, (err, res, data) => {
t.error(err)
})
Expand Down
10 changes: 7 additions & 3 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import fastify from "fastify";
import middiePlugin, {MiddiePluginOptions} from "..";
import { expectAssignable } from "tsd";
import middiePlugin, {MiddiePluginOptions, IncomingMessageExtended} from "..";
import { expectAssignable, expectType } from "tsd";

const app = fastify();
app.register(middiePlugin);

expectAssignable<MiddiePluginOptions>({})

expectAssignable<MiddiePluginOptions>({ hook: 'preHandler' })
expectAssignable<IncomingMessageExtended>({ body: { foo: 'bar' }, query: { bar: 'foo' }})
expectAssignable<IncomingMessageExtended>({})

app.use('/', (_req, _res, next) => {
expectType<any | undefined>(_req.body)
expectType<any | undefined>(_req.query)

next()
})