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

Fix/add missing types fastifycontextconfig #4850

Merged
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
82 changes: 79 additions & 3 deletions test/types/route.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,27 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
Headers: HeadersInterface;
}

fastify()[lowerCaseMethod]<RouteGeneric, RouteSpecificContextConfigType>('/', { config: { foo: 'bar', bar: 100, extra: true } }, (req, res) => {
fastify()[lowerCaseMethod]<RouteGeneric, RouteSpecificContextConfigType>('/', { config: { foo: 'bar', bar: 100, extra: true, url: '/', method: lowerCaseMethod } }, (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<boolean>(req.context.config.extra)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<boolean>(res.context.config.extra)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
})

fastify().route<RouteGeneric>({
url: '/',
method: method as HTTPMethods,
config: { foo: 'bar', bar: 100 },
config: { foo: 'bar', bar: 100, url: '/', method: method as HTTPMethods },
prefixTrailingSlash: 'slash',
onRequest: (req, res, done) => { // these handlers are tested in `hooks.test-d.ts`
expectType<BodyInterface>(req.body)
Expand All @@ -99,8 +103,12 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
},
preParsing: (req, res, payload, done) => {
expectType<BodyInterface>(req.body)
Expand All @@ -109,8 +117,12 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
expectType<RequestPayload>(payload)
expectAssignable<(err?: FastifyError | null, res?: RequestPayload) => void>(done)
expectAssignable<(err?: NodeJS.ErrnoException) => void>(done)
Expand All @@ -122,8 +134,12 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
},
preHandler: (req, res, done) => {
expectType<BodyInterface>(req.body)
Expand All @@ -132,8 +148,12 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
},
onResponse: (req, res, done) => {
expectType<BodyInterface>(req.body)
Expand All @@ -142,8 +162,12 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
expectType<number>(res.statusCode)
},
onError: (req, res, error, done) => {
Expand All @@ -153,8 +177,12 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
},
preSerialization: (req, res, payload, done) => {
expectType<BodyInterface>(req.body)
Expand All @@ -163,8 +191,12 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
},
onSend: (req, res, payload, done) => {
expectType<BodyInterface>(req.body)
Expand All @@ -173,8 +205,12 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
},
handler: (req, res) => {
expectType<BodyInterface>(req.body)
Expand All @@ -183,15 +219,19 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
}
})

fastify().route<RouteGeneric>({
url: '/',
method: method as HTTPMethods,
config: { foo: 'bar', bar: 100 },
config: { foo: 'bar', bar: 100, url: '/', method: method as HTTPMethods },
prefixTrailingSlash: 'slash',
onRequest: async (req, res, done) => { // these handlers are tested in `hooks.test-d.ts`
expectType<BodyInterface>(req.body)
Expand All @@ -200,8 +240,12 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
},
preParsing: async (req, res, payload, done) => {
expectType<BodyInterface>(req.body)
Expand All @@ -210,8 +254,12 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
expectType<RequestPayload>(payload)
expectAssignable<(err?: FastifyError | null, res?: RequestPayload) => void>(done)
expectAssignable<(err?: NodeJS.ErrnoException) => void>(done)
Expand All @@ -223,8 +271,12 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
},
preHandler: async (req, res, done) => {
expectType<BodyInterface>(req.body)
Expand All @@ -233,8 +285,12 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
},
onResponse: async (req, res, done) => {
expectType<BodyInterface>(req.body)
Expand All @@ -243,8 +299,12 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
expectType<number>(res.statusCode)
},
onError: async (req, res, error, done) => {
Expand All @@ -254,8 +314,12 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
},
preSerialization: async (req, res, payload, done) => {
expectType<BodyInterface>(req.body)
Expand All @@ -264,8 +328,12 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
},
onSend: async (req, res, payload, done) => {
expectType<BodyInterface>(req.body)
Expand All @@ -274,8 +342,12 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
},
handler: (req, res) => {
expectType<BodyInterface>(req.body)
Expand All @@ -284,8 +356,12 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
expectType<string>(req.context.config.foo)
expectType<number>(req.context.config.bar)
expectType<string>(req.context.config.url)
expectType<HTTPMethods | HTTPMethods[]>(req.context.config.method)
expectType<string>(res.context.config.foo)
expectType<number>(res.context.config.bar)
expectType<string>(req.routeConfig.url)
expectType<HTTPMethods | HTTPMethods[]>(req.routeConfig.method)
}
})
})
Expand Down
3 changes: 2 additions & 1 deletion types/context.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ContextConfigDefault } from './utils'
import { FastifyRouteConfig } from './route'

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface FastifyContextConfig {
export interface FastifyContextConfig extends FastifyRouteConfig {
}

/**
Expand Down
5 changes: 5 additions & 0 deletions types/route.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import {
} from './type-provider'
import { FastifyBaseLogger, LogLevel } from './logger'

export interface FastifyRouteConfig {
url: string;
method: HTTPMethods | HTTPMethods[];
}

export interface RouteGenericInterface extends RequestGenericInterface, ReplyGenericInterface {}

/**
Expand Down