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: route constraints #5207

Merged
merged 3 commits into from
Dec 14, 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
19 changes: 6 additions & 13 deletions test/types/route.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,19 +444,12 @@ expectType<boolean>(fastify().hasRoute({
url: '/',
method: 'GET',
constraints: {
something: {
name: 'secret',
storage: function () {
return {
get: (type) => {
return null
},
set: (type, store) => {}
}
},
deriveConstraint: (req, ctx, done) => {},
mustMatchWhenDerived: true
}
// constraints value should accept any value
number: 12,
date: new Date(),
boolean: true,
function: () => {},
object: { foo: 'bar' }
}
}))

Expand Down
10 changes: 8 additions & 2 deletions types/route.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FastifyError } from '@fastify/error'
import { ConstraintStrategy } from 'find-my-way'
import { FastifyRequestContext } from './context'
import { onErrorMetaHookHandler, onRequestAbortMetaHookHandler, onRequestMetaHookHandler, onResponseMetaHookHandler, onSendMetaHookHandler, onTimeoutMetaHookHandler, preHandlerMetaHookHandler, preParsingMetaHookHandler, preSerializationMetaHookHandler, preValidationMetaHookHandler } from './hooks'
import { FastifyInstance } from './instance'
Expand All @@ -12,7 +13,6 @@ import {
ResolveFastifyReplyReturnType
} from './type-provider'
import { ContextConfigDefault, HTTPMethods, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault } from './utils'
import { ConstraintStrategy } from 'find-my-way'

export interface FastifyRouteConfig {
url: string;
Expand All @@ -25,6 +25,12 @@ export type RouteConstraintType = Omit<ConstraintStrategy<any>, 'deriveConstrain
deriveConstraint<Context>(req: RawRequestDefaultExpression<RawServerDefault>, ctx?: Context, done?: (err: Error, ...args: any) => any): any,
}

export interface RouteConstraint {
version?: string
host?: RegExp | string
[name: string]: unknown
}

/**
* Route shorthand options for the various shorthand methods
*/
Expand All @@ -48,7 +54,7 @@ export interface RouteShorthandOptions<
logLevel?: LogLevel;
config?: Omit<FastifyRequestContext<ContextConfig>['config'], 'url' | 'method'>;
version?: string;
constraints?: { version: string } | {host: RegExp | string} | {[name: string]: RouteConstraintType },
constraints?: RouteConstraint,
prefixTrailingSlash?: 'slash'|'no-slash'|'both';
errorHandler?: (
this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
Expand Down