Skip to content

Commit

Permalink
fix: correct type definition for genReqId argument (#4784)
Browse files Browse the repository at this point in the history
* Fixes #4748: correct type defiinition for genReqId(), it receives raw request as argument, not FastifyRequest

* Clarify this in docs

* Update docs/Reference/Server.md

Co-authored-by: Frazer Smith <frazer.dev@outlook.com>

* Improved tests

* Removed irrelevant tests

---------

Co-authored-by: Frazer Smith <frazer.dev@outlook.com>
Co-authored-by: Uzlopak <aras.abbasi@googlemail.com>
  • Loading branch information
3 people committed Jun 16, 2023
1 parent d1ad6c1 commit 109873c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/Reference/Server.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,8 @@ Defines the label used for the request identifier when logging the request.
### `genReqId`
<a id="factory-gen-request-id"></a>

Function for generating the request-id. It will receive the incoming request as
a parameter. This function is expected to be error-free.
Function for generating the request-id. It will receive the _raw_ incoming
request as a parameter. This function is expected to be error-free.

+ Default: `value of 'request-id' header if provided or monotonically increasing
integers`
Expand Down
2 changes: 1 addition & 1 deletion fastify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ declare namespace fastify {
requestIdHeader?: string | false,
requestIdLogLabel?: string;
jsonShorthand?: boolean;
genReqId?: <RequestGeneric extends RequestGenericInterface = RequestGenericInterface, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault>(req: FastifyRequest<RequestGeneric, RawServer, RawRequestDefaultExpression<RawServer>, FastifySchema, TypeProvider>) => string,
genReqId?: (req: RawRequestDefaultExpression<RawServer>) => string,
trustProxy?: boolean | string | string[] | number | TrustProxyFunction,
querystringParser?: (str: string) => { [key: string]: unknown },
/**
Expand Down
42 changes: 42 additions & 0 deletions test/genReqId.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { Readable } = require('stream')
const { test } = require('tap')
const Fastify = require('..')

Expand Down Expand Up @@ -30,3 +31,44 @@ test('Should accept a custom genReqId function', t => {
})
})
})

test('Custom genReqId function gets raw request as argument', t => {
t.plan(9)

const REQUEST_ID = 'REQ-1234'

const fastify = Fastify({
genReqId: function (req) {
t.notOk('id' in req)
t.notOk('raw' in req)
t.ok(req instanceof Readable)
// http.IncomingMessage does have `rawHeaders` property, but FastifyRequest does not
const index = req.rawHeaders.indexOf('x-request-id')
const xReqId = req.rawHeaders[index + 1]
t.equal(xReqId, REQUEST_ID)
t.equal(req.headers['x-request-id'], REQUEST_ID)
return xReqId
}
})

fastify.get('/', (req, reply) => {
t.equal(req.id, REQUEST_ID)
reply.send({ id: req.id })
})

fastify.listen({ port: 0 }, err => {
t.error(err)
fastify.inject({
method: 'GET',
headers: {
'x-request-id': REQUEST_ID
},
url: 'http://localhost:' + fastify.server.address().port
}, (err, res) => {
t.error(err)
const payload = JSON.parse(res.payload)
t.equal(payload.id, REQUEST_ID)
fastify.close()
})
})
})
8 changes: 7 additions & 1 deletion test/types/fastify.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fastify, {
LightMyRequestResponse,
LightMyRequestCallback,
InjectOptions, FastifyBaseLogger,
RawRequestDefaultExpression,
RouteGenericInterface,
ValidationResult,
FastifyErrorCodes,
Expand Down Expand Up @@ -127,7 +128,12 @@ expectAssignable<FastifyInstance>(fastify({ serverFactory: () => http.createServ
expectAssignable<FastifyInstance>(fastify({ caseSensitive: true }))
expectAssignable<FastifyInstance>(fastify({ requestIdHeader: 'request-id' }))
expectAssignable<FastifyInstance>(fastify({ requestIdHeader: false }))
expectAssignable<FastifyInstance>(fastify({ genReqId: () => 'request-id' }))
expectAssignable<FastifyInstance>(fastify({
genReqId: (req) => {
expectType<RawRequestDefaultExpression>(req)
return 'foo'
}
}))
expectAssignable<FastifyInstance>(fastify({ trustProxy: true }))
expectAssignable<FastifyInstance>(fastify({ querystringParser: () => ({ foo: 'bar' }) }))
expectAssignable<FastifyInstance>(fastify({ querystringParser: () => ({ foo: { bar: 'fuzz' } }) }))
Expand Down

0 comments on commit 109873c

Please sign in to comment.