Skip to content

Commit 7d7f065

Browse files
authored
feat(types): router option types (#6282)
* feat(types): router option types * fix * fix * pr feedback
1 parent 812ef58 commit 7d7f065

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

fastify.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ declare namespace fastify {
8989

9090
type TrustProxyFunction = (address: string, hop: number) => boolean
9191

92+
export type FastifyRouterOptions<RawServer extends RawServerBase> = {
93+
allowUnsafeRegex?: boolean,
94+
buildPrettyMeta?: (route: { [k: string]: unknown, store: { [k: string]: unknown } }) => object,
95+
caseSensitive?: boolean,
96+
constraints?: {
97+
[name: string]: ConstraintStrategy<FindMyWayVersion<RawServer>, unknown>,
98+
},
99+
defaultRoute?: (req: FastifyRequest, res: FastifyReply) => void,
100+
ignoreDuplicateSlashes?: boolean,
101+
ignoreTrailingSlash?: boolean,
102+
maxParamLength?: number,
103+
onBadUrl?: (path: string, req: FastifyRequest, res: FastifyReply) => void,
104+
querystringParser?: (str: string) => { [key: string]: unknown },
105+
useSemicolonDelimiter?: boolean,
106+
}
107+
92108
/**
93109
* Options for a fastify server instance. Utilizes conditional logic on the generic server parameter to enforce certain https and http2
94110
*/
@@ -159,6 +175,7 @@ declare namespace fastify {
159175
clientErrorHandler?: (error: ConnectionError, socket: Socket) => void,
160176
childLoggerFactory?: FastifyChildLoggerFactory,
161177
allowErrorHandlerOverride?: boolean
178+
routerOptions?: FastifyRouterOptions<RawServer>,
162179
}
163180

164181
/**

test/types/instance.test-d.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { FastifyRequest } from '../../types/request'
1515
import { FastifySchemaControllerOptions, FastifySchemaCompiler, FastifySerializerCompiler } from '../../types/schema'
1616
import { AddressInfo } from 'node:net'
1717
import { Bindings, ChildLoggerOptions } from '../../types/logger'
18+
import { ConstraintStrategy } from 'find-my-way'
19+
import { FindMyWayVersion } from '../../types/instance'
1820

1921
const server = fastify()
2022

@@ -309,7 +311,22 @@ type InitialConfig = Readonly<{
309311
requestIdHeader?: string | false,
310312
requestIdLogLabel?: string,
311313
http2SessionTimeout?: number,
312-
useSemicolonDelimiter?: boolean
314+
useSemicolonDelimiter?: boolean,
315+
routerOptions?: {
316+
allowUnsafeRegex?: boolean,
317+
buildPrettyMeta?: (route: { [k: string]: unknown, store: { [k: string]: unknown } }) => object,
318+
caseSensitive?: boolean,
319+
constraints?: {
320+
[name: string]: ConstraintStrategy<FindMyWayVersion<RawServerDefault>, unknown>
321+
}
322+
defaultRoute?: (req: FastifyRequest, res: FastifyReply) => void,
323+
ignoreDuplicateSlashes?: boolean,
324+
ignoreTrailingSlash?: boolean,
325+
maxParamLength?: number,
326+
onBadUrl?: (path: string, req: FastifyRequest, res: FastifyReply) => void,
327+
querystringParser?: (str: string) => { [key: string]: unknown },
328+
useSemicolonDelimiter?: boolean,
329+
}
313330
}>
314331

315332
expectType<InitialConfig>(fastify().initialConfig)

types/instance.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
SafePromiseLike
2424
} from './type-provider'
2525
import { ContextConfigDefault, HTTPMethods, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault } from './utils'
26+
import { FastifyRouterOptions } from '../fastify'
2627

2728
export interface PrintRoutesOptions {
2829
method?: HTTPMethods;
@@ -603,5 +604,6 @@ export interface FastifyInstance<
603604
requestIdLogLabel?: string,
604605
http2SessionTimeout?: number,
605606
useSemicolonDelimiter?: boolean,
607+
routerOptions?: FastifyRouterOptions<RawServer>
606608
}>
607609
}

0 commit comments

Comments
 (0)