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 Dec 13, 2023
1 parent 1e489c7 commit 4468928
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 748 deletions.
758 changes: 18 additions & 740 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
"eslint-plugin-import": "2.29.0",
"eventsource": "2.0.2",
"fancy-log": "2.0.0",
"fastify": "4.24.3",
"fastify": "4.25.0",
"graphql": "16.8.1",
"graphql-tools": "9.0.0",
"gulp": "4.0.2",
Expand Down
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);
2 changes: 1 addition & 1 deletion packages/platform-fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@fastify/cors": "8.4.2",
"@fastify/formbody": "7.4.0",
"@fastify/middie": "8.3.0",
"fastify": "4.24.3",
"fastify": "4.25.0",
"light-my-request": "5.11.0",
"path-to-regexp": "3.2.0",
"tslib": "2.6.2"
Expand Down
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 4468928

Please sign in to comment.