Skip to content

Commit

Permalink
fix: route constraints (#5207)
Browse files Browse the repository at this point in the history
* fix: route constraints

* test: update

* chore: linting
  • Loading branch information
climba03003 committed Dec 14, 2023
1 parent 635acc8 commit 256fb2c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
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

0 comments on commit 256fb2c

Please sign in to comment.