Skip to content

Commit

Permalink
Merge branch 'main' into (fastify#4439)-Access-handler-name-add-prope…
Browse files Browse the repository at this point in the history
…rties-to-req-route-options
  • Loading branch information
metcoder95 committed Aug 24, 2023
2 parents 232cc3e + 7270c00 commit 53ecf4a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/Guides/Getting-Started.md
Expand Up @@ -277,7 +277,7 @@ async function dbConnector (fastify, options) {

// Wrapping a plugin function with fastify-plugin exposes the decorators
// and hooks, declared inside the plugin to the parent scope.
module.exports = fastifyPlugin(dbConnector)
export default fastifyPlugin(dbConnector)

```

Expand Down
9 changes: 7 additions & 2 deletions test/types/instance.test-d.ts
Expand Up @@ -6,7 +6,8 @@ import fastify, {
FastifyInstance,
RawReplyDefaultExpression,
RawRequestDefaultExpression,
RawServerDefault
RawServerDefault,
RouteGenericInterface
} from '../../fastify'
import { HookHandlerDoneFunction } from '../../types/hooks'
import { FastifyReply } from '../../types/reply'
Expand Down Expand Up @@ -257,9 +258,13 @@ expectNotDeprecated(server.listen({ port: 3000, host: '::/0', ipv6Only: true },

expectAssignable<void>(server.routing({} as RawRequestDefaultExpression, {} as RawReplyDefaultExpression))

expectType<FastifyInstance>(fastify().get('/', {
expectType<FastifyInstance>(fastify().get<RouteGenericInterface, { contextKey: string }>('/', {
handler: () => {},
errorHandler: (error, request, reply) => {
expectAssignable<FastifyError>(error)
expectAssignable<FastifyRequest>(request)
expectAssignable<{ contextKey: string }>(request.routeConfig)
expectAssignable<FastifyReply>(reply)
expectAssignable<void>(server.errorHandler(error, request, reply))
}
}))
Expand Down
27 changes: 26 additions & 1 deletion test/types/type-provider.test-d.ts
Expand Up @@ -3,7 +3,8 @@ import fastify, {
HookHandlerDoneFunction,
FastifyRequest,
FastifyReply,
FastifyInstance
FastifyInstance,
FastifyError
} from '../../fastify'
import { expectAssignable, expectError, expectType } from 'tsd'
import { IncomingHttpHeaders } from 'http'
Expand Down Expand Up @@ -79,6 +80,14 @@ expectAssignable(server.withTypeProvider<TypeBoxProvider>().get(
y: Type.Number(),
z: Type.Number()
})
},
errorHandler: (error, request, reply) => {
expectType<FastifyError>(error)
expectAssignable<FastifyRequest>(request)
expectType<number>(request.body.x)
expectType<number>(request.body.y)
expectType<number>(request.body.z)
expectAssignable<FastifyReply>(reply)
}
},
(req) => {
Expand Down Expand Up @@ -108,6 +117,14 @@ expectAssignable(server.withTypeProvider<JsonSchemaToTsProvider>().get(
z: { type: 'boolean' }
}
} as const
},
errorHandler: (error, request, reply) => {
expectType<FastifyError>(error)
expectAssignable<FastifyRequest>(request)
expectType<number | undefined>(request.body.x)
expectType<string | undefined>(request.body.y)
expectType<boolean | undefined>(request.body.z)
expectAssignable<FastifyReply>(reply)
}
},
(req) => {
Expand Down Expand Up @@ -135,6 +152,14 @@ expectAssignable(server.withTypeProvider<TypeBoxProvider>().withTypeProvider<Jso
z: { type: 'boolean' }
}
} as const
},
errorHandler: (error, request, reply) => {
expectType<FastifyError>(error)
expectAssignable<FastifyRequest>(request)
expectType<number | undefined>(request.body.x)
expectType<string | undefined>(request.body.y)
expectType<boolean | undefined>(request.body.z)
expectAssignable<FastifyReply>(reply)
}
},
(req) => {
Expand Down
4 changes: 3 additions & 1 deletion types/hooks.d.ts
Expand Up @@ -630,7 +630,9 @@ export type ApplicationHookLookup<K extends ApplicationHook> = K extends 'onRegi
? onCloseHookHandler
: K extends 'preClose'
? preCloseHookHandler
: never
: K extends 'onRoute'
? onRouteHookHandler
: never

export type ApplicationHookAsyncLookup<K extends ApplicationHook> = K extends 'onRegister'
? onRegisterHookHandler
Expand Down
7 changes: 6 additions & 1 deletion types/route.d.ts
Expand Up @@ -45,7 +45,12 @@ export interface RouteShorthandOptions<
version?: string;
constraints?: { [name: string]: any },
prefixTrailingSlash?: 'slash'|'no-slash'|'both';
errorHandler?: (this: FastifyInstance, error: FastifyError, request: FastifyRequest, reply: FastifyReply) => void;
errorHandler?: (
this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
error: FastifyError,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
) => void;
childLoggerFactory?: FastifyChildLoggerFactory<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
schemaErrorFormatter?: SchemaErrorFormatter;

Expand Down

0 comments on commit 53ecf4a

Please sign in to comment.