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

feat(fastify): add fastify route constraints #12567

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
import { RouteShorthandOptions } from 'fastify';

/**
* @publicApi
*
* @param config See {@link https://fastify.dev/docs/latest/Reference/Routes/#constraints}
*/
export const RouteConstraints = (config: RouteShorthandOptions['config']) =>
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);
});
});
});