Skip to content

Commit

Permalink
Merge pull request #11992 from rich-w-lee/feature/fastify-route-config
Browse files Browse the repository at this point in the history
feat(fastify): supporting fastify route config
  • Loading branch information
kamilmysliwiec authored Aug 18, 2023
2 parents f8d9101 + ca4a48e commit d368d33
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 12 deletions.
14 changes: 14 additions & 0 deletions packages/core/router/router-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export class RouterExplorer {
},
};

this.copyMetadataToCallback(targetCallback, routeHandler);
routerMethodRef(path, routeHandler);

this.graphInspector.insertEntrypointDefinition<HttpEntrypointMetadata>(
Expand Down Expand Up @@ -422,4 +423,17 @@ export class RouterExplorer {
}
return contextId;
}

private copyMetadataToCallback(
originalCallback: RouterProxyCallback,
targetCallback: Function,
) {
for (const key of Reflect.getMetadataKeys(originalCallback)) {
Reflect.defineMetadata(
key,
Reflect.getMetadata(key, originalCallback),
targetCallback,
);
}
}
}
23 changes: 23 additions & 0 deletions packages/core/test/router/router-explorer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,27 @@ describe('RouterExplorer', () => {
);
});
});

describe('copyMetadataToCallback', () => {
it('should then copy the metadata from the original callback to the target callback', () => {
const originalCallback = () => {};
Reflect.defineMetadata(
'test_metadata_key',
'test_metadata_value',
originalCallback,
);

const targetCallback = () => {};

// We're using type assertion here because `copyMetadataToCallback` is private
(routerBuilder as any).copyMetadataToCallback(
originalCallback,
targetCallback,
);

expect(
Reflect.getMetadata('test_metadata_key', targetCallback),
).to.be.equal('test_metadata_value');
});
});
});
37 changes: 25 additions & 12 deletions packages/platform-fastify/adapters/fastify-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
FastifyStaticOptions,
FastifyViewOptions,
} from '../interfaces/external';
import { FASTIFY_ROUTE_CONFIG_METADATA } from '../constants';

type FastifyHttp2SecureOptions<
Server extends http2.Http2SecureServer,
Expand Down Expand Up @@ -261,31 +262,31 @@ export class FastifyAdapter<
}

public get(...args: any[]) {
return this.injectConstraintsIfVersioned('get', ...args);
return this.injectRouteOptions('get', ...args);
}

public post(...args: any[]) {
return this.injectConstraintsIfVersioned('post', ...args);
return this.injectRouteOptions('post', ...args);
}

public head(...args: any[]) {
return this.injectConstraintsIfVersioned('head', ...args);
return this.injectRouteOptions('head', ...args);
}

public delete(...args: any[]) {
return this.injectConstraintsIfVersioned('delete', ...args);
return this.injectRouteOptions('delete', ...args);
}

public put(...args: any[]) {
return this.injectConstraintsIfVersioned('put', ...args);
return this.injectRouteOptions('put', ...args);
}

public patch(...args: any[]) {
return this.injectConstraintsIfVersioned('patch', ...args);
return this.injectRouteOptions('patch', ...args);
}

public options(...args: any[]) {
return this.injectConstraintsIfVersioned('options', ...args);
return this.injectRouteOptions('options', ...args);
}

public applyVersionFilter(
Expand Down Expand Up @@ -638,7 +639,7 @@ export class FastifyAdapter<
return rawRequest.originalUrl || rawRequest.url;
}

private injectConstraintsIfVersioned(
private injectRouteOptions(
routerMethodKey:
| 'get'
| 'post'
Expand All @@ -653,14 +654,26 @@ export class FastifyAdapter<
const isVersioned =
!isUndefined(handlerRef.version) &&
handlerRef.version !== VERSION_NEUTRAL;
const routeConfig = Reflect.getMetadata(
FASTIFY_ROUTE_CONFIG_METADATA,
handlerRef,
);
const hasConfig = !isUndefined(routeConfig);

if (isVersioned) {
if (isVersioned || hasConfig) {
const isPathAndRouteTuple = args.length === 2;
if (isPathAndRouteTuple) {
const options = {
constraints: {
version: handlerRef.version,
},
...(isVersioned && {
constraints: {
version: handlerRef.version,
},
}),
...(hasConfig && {
config: {
...routeConfig,
},
}),
};
const path = args[0];
return this.instance[routerMethodKey](path, options, handlerRef);
Expand Down
1 change: 1 addition & 0 deletions packages/platform-fastify/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const FASTIFY_ROUTE_CONFIG_METADATA = '__fastify_route_config__';
1 change: 1 addition & 0 deletions packages/platform-fastify/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './route-config.decorator';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { SetMetadata } from '@nestjs/common';
import { FASTIFY_ROUTE_CONFIG_METADATA } from '../constants';

/**
* @param config See {@link https://fastify.dev/docs/latest/Reference/Routes/#config}
*/
export const RouteConfig = (config: any) =>
SetMetadata(FASTIFY_ROUTE_CONFIG_METADATA, config);
1 change: 1 addition & 0 deletions packages/platform-fastify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@

export * from './adapters';
export * from './interfaces';
export * from './decorators';
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect } from 'chai';
import { FASTIFY_ROUTE_CONFIG_METADATA } from '../../constants';
import { RouteConfig } from '../../decorators/route-config.decorator';

describe('@RouteConfig', () => {
const routeConfig = { testKey: 'testValue' };
class Test {
config;
@RouteConfig(routeConfig)
public static test() {}
}

it('should enhance method with expected fastify route config', () => {
const path = Reflect.getMetadata(FASTIFY_ROUTE_CONFIG_METADATA, Test.test);
expect(path).to.be.eql(routeConfig);
});
});

0 comments on commit d368d33

Please sign in to comment.