Skip to content

Commit

Permalink
feat(fastify): add fastify route constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
Fcmam5 committed Oct 14, 2023
1 parent ecdd86f commit df96d74
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
26 changes: 20 additions & 6 deletions packages/platform-fastify/adapters/fastify-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ import {
FastifyStaticOptions,
FastifyViewOptions,
} from '../interfaces/external';
import { FASTIFY_ROUTE_CONFIG_METADATA } from '../constants';
import {
FASTIFY_ROUTE_CONFIG_METADATA,
FASTIFY_ROUTE_CONSTRAINTS_METADATA,
} from '../constants';

type FastifyHttp2SecureOptions<
Server extends http2.Http2SecureServer,
Expand Down Expand Up @@ -659,17 +662,27 @@ export class FastifyAdapter<
FASTIFY_ROUTE_CONFIG_METADATA,
handlerRef,
);

const routeConstraints = Reflect.getMetadata(
FASTIFY_ROUTE_CONSTRAINTS_METADATA,
handlerRef,
);

const hasConfig = !isUndefined(routeConfig);
const hasConstraints = !isUndefined(routeConstraints);

if (isVersioned || hasConfig) {
if (isVersioned || hasConstraints || hasConfig) {
const isPathAndRouteTuple = args.length === 2;
if (isPathAndRouteTuple) {
const options = {
const constraints = {
...(hasConstraints && routeConstraints),
...(isVersioned && {
constraints: {
version: handlerRef.version,
},
version: handlerRef.version,
}),
};

const options = {
constraints,
...(hasConfig && {
config: {
...routeConfig,
Expand All @@ -680,6 +693,7 @@ export class FastifyAdapter<
return this.instance[routerMethodKey](path, options, handlerRef);
}
}

return this.instance[routerMethodKey](
...(args as Parameters<
RouteShorthandMethod<TServer, TRawRequest, TRawResponse>
Expand Down
2 changes: 2 additions & 0 deletions packages/platform-fastify/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const FASTIFY_ROUTE_CONFIG_METADATA = '__fastify_route_config__';
export const FASTIFY_ROUTE_CONSTRAINTS_METADATA =
'__fastify_route_constraints__';
1 change: 1 addition & 0 deletions packages/platform-fastify/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './route-config.decorator';
export * from './route-constraints.decorator';
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { SetMetadata } from '@nestjs/common';
import { FASTIFY_ROUTE_CONSTRAINTS_METADATA } from '../constants';

type IRouteConstraints = { version?: any; host?: any; [name: string]: any };
/**
* @publicApi
*
* @param config See {@link https://fastify.dev/docs/latest/Reference/Routes/#constraints}
*/
export const RouteConstraints = (config: IRouteConstraints) =>
SetMetadata(FASTIFY_ROUTE_CONSTRAINTS_METADATA, config);
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect } from 'chai';
import { FASTIFY_ROUTE_CONSTRAINTS_METADATA } from '../../constants';
import { RouteConstraints } from '../../decorators/route-constraints.decorator';

describe('@RouteConstraints', () => {
describe('has version constraints', () => {
const routeConstraints = { version: '1.2.x' };
class TestVersionConstraints {
config;
@RouteConstraints(routeConstraints)
public static test() {}
}

it('should have a version constraint', () => {
const path = Reflect.getMetadata(
FASTIFY_ROUTE_CONSTRAINTS_METADATA,
TestVersionConstraints.test,
);
expect(path).to.be.eql(routeConstraints);
});
});

describe('has custom constraints', () => {
const customRouteConstraints = { something: 'foo' };
class TestConstraints {
config;
@RouteConstraints(customRouteConstraints)
public static test() {}
}

it('should set a custom constraint', () => {
const path = Reflect.getMetadata(
FASTIFY_ROUTE_CONSTRAINTS_METADATA,
TestConstraints.test,
);
expect(path).to.be.eql(customRouteConstraints);
});
});
});

0 comments on commit df96d74

Please sign in to comment.