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

Adds async hooks signature on route level #4655

Merged
merged 3 commits into from
Apr 10, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/Reference/Hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,12 @@ fastify.route({
// This hook will always be executed after the shared `onRequest` hooks
done()
},
// // Example with an async hook. All hooks support this syntax
//
// onRequest: async function (request, reply) {
// // This hook will always be executed after the shared `onRequest` hooks
// await ...
// }
onResponse: function (request, reply, done) {
// this hook will always be executed after the shared `onResponse` hooks
done()
Expand Down
29 changes: 29 additions & 0 deletions test/route-hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,35 @@ function testExecutionHook (hook) {
})
})

test(`${hook} option could accept an array of async functions`, t => {
t.plan(3)
const fastify = Fastify()
const checker = Object.defineProperty({ calledTimes: 0 }, 'check', {
get: function () { return ++this.calledTimes }
})

fastify.post('/', {
[hook]: [
async (req, reply) => {
t.equal(checker.check, 1)
},
async (req, reply) => {
t.equal(checker.check, 2)
}
]
}, (req, reply) => {
reply.send({})
})

fastify.inject({
method: 'POST',
url: '/',
payload: { hello: 'world' }
}, (err, res) => {
t.error(err)
})
})

test(`${hook} option does not interfere with ${hook} hook`, t => {
t.plan(7)
const fastify = Fastify()
Expand Down
66 changes: 55 additions & 11 deletions test/types/hooks.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ server.addHook('onRequestAbort', function (request, done) {

server.addHook('onRoute', function (opts) {
expectType<FastifyInstance>(this)
expectType<RouteOptions & { routePath: string; path: string; prefix: string}>(opts)
expectType<RouteOptions & { routePath: string; path: string; prefix: string }>(opts)
})

server.addHook('onRegister', (instance, opts, done) => {
Expand Down Expand Up @@ -256,40 +256,84 @@ type CustomContextConfig = FastifyContextConfig & {
server.route<RouteGenericInterface, CustomContextConfig>({
method: 'GET',
url: '/',
handler: () => {},
onRequest: (request, reply) => {
handler: () => { },
onRequest: (request, reply, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
preParsing: (request, reply) => {
preParsing: (request, reply, payload, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
preValidation: (request, reply) => {
preValidation: (request, reply, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
preHandler: (request, reply) => {
preHandler: (request, reply, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
preSerialization: (request, reply) => {
preSerialization: (request, reply, payload, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
onSend: (request, reply) => {
onSend: (request, reply, payload, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
onResponse: (request, reply) => {
onResponse: (request, reply, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
onTimeout: (request, reply) => {
onTimeout: (request, reply, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
onError: (request, reply) => {
onError: (request, reply, error, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
}
})

type CustomContextRequest = FastifyRequest<any, any, any, any, any, CustomContextConfig, any>
type CustomContextReply = FastifyReply<any, any, any, any, CustomContextConfig, any, any>
server.route<RouteGenericInterface, CustomContextConfig>({
method: 'GET',
url: '/',
handler: () => { },
onRequest: async (request: CustomContextRequest, reply: CustomContextReply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
preParsing: async (request: CustomContextRequest, reply: CustomContextReply, payload: RequestPayload) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
preValidation: async (request: CustomContextRequest, reply: CustomContextReply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
preHandler: async (request: CustomContextRequest, reply: CustomContextReply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
preSerialization: async (request: CustomContextRequest, reply: CustomContextReply, payload: any) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
onSend: async (request: CustomContextRequest, reply: CustomContextReply, payload: any) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
onResponse: async (request: CustomContextRequest, reply: CustomContextReply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
onTimeout: async (request: CustomContextRequest, reply: CustomContextReply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
onError: async (request: CustomContextRequest, reply: CustomContextReply, error: FastifyError) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
}
Expand Down
111 changes: 106 additions & 5 deletions test/types/route.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ const routeHandlerWithReturnValue: RouteHandlerMethod = function (request, reply
return reply.send()
}

type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'options'
type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'options';

;['GET', 'POST', 'PUT', 'PATCH', 'HEAD', 'DELETE', 'OPTIONS'].forEach(method => {
['GET', 'POST', 'PUT', 'PATCH', 'HEAD', 'DELETE', 'OPTIONS'].forEach(method => {
// route method
expectType<FastifyInstance>(fastify().route({
method: method as HTTPMethods,
Expand Down Expand Up @@ -146,7 +146,7 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<number>(res.context.config.bar)
expectType<number>(res.statusCode)
},
onError: (req, res, done) => {
onError: (req, res, error, done) => {
expectType<BodyInterface>(req.body)
expectType<QuerystringInterface>(req.query)
expectType<ParamsInterface>(req.params)
Expand All @@ -156,7 +156,7 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
},
preSerialization: (req, res, done) => {
preSerialization: (req, res, payload, done) => {
expectType<BodyInterface>(req.body)
expectType<QuerystringInterface>(req.query)
expectType<ParamsInterface>(req.params)
Expand All @@ -166,7 +166,108 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
},
onSend: (req, res, done) => {
onSend: (req, res, payload, done) => {
expectType<BodyInterface>(req.body)
expectType<QuerystringInterface>(req.query)
expectType<ParamsInterface>(req.params)
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
},
handler: (req, res) => {
expectType<BodyInterface>(req.body)
expectType<QuerystringInterface>(req.query)
expectType<ParamsInterface>(req.params)
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
}
})

fastify().route<RouteGeneric>({
url: '/',
method: method as HTTPMethods,
config: { foo: 'bar', bar: 100 },
prefixTrailingSlash: 'slash',
onRequest: async (req, res, done) => { // these handlers are tested in `hooks.test-d.ts`
expectType<BodyInterface>(req.body)
expectType<QuerystringInterface>(req.query)
expectType<ParamsInterface>(req.params)
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
},
preParsing: async (req, res, payload, done) => {
expectType<BodyInterface>(req.body)
expectType<QuerystringInterface>(req.query)
expectType<ParamsInterface>(req.params)
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<RequestPayload>(payload)
expectAssignable<(err?: FastifyError | null, res?: RequestPayload) => void>(done)
expectAssignable<(err?: NodeJS.ErrnoException) => void>(done)
},
preValidation: async (req, res, done) => {
expectType<BodyInterface>(req.body)
expectType<QuerystringInterface>(req.query)
expectType<ParamsInterface>(req.params)
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
},
preHandler: async (req, res, done) => {
expectType<BodyInterface>(req.body)
expectType<QuerystringInterface>(req.query)
expectType<ParamsInterface>(req.params)
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
},
onResponse: async (req, res, done) => {
expectType<BodyInterface>(req.body)
expectType<QuerystringInterface>(req.query)
expectType<ParamsInterface>(req.params)
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<number>(res.statusCode)
},
onError: async (req, res, error, done) => {
expectType<BodyInterface>(req.body)
expectType<QuerystringInterface>(req.query)
expectType<ParamsInterface>(req.params)
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
},
preSerialization: async (req, res, payload, done) => {
expectType<BodyInterface>(req.body)
expectType<QuerystringInterface>(req.query)
expectType<ParamsInterface>(req.params)
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
},
onSend: async (req, res, payload, done) => {
expectType<BodyInterface>(req.body)
expectType<QuerystringInterface>(req.query)
expectType<ParamsInterface>(req.params)
Expand Down