Skip to content

Commit

Permalink
refactor(platform-fastify): use the route method to inject routes
Browse files Browse the repository at this point in the history
  • Loading branch information
johaven committed Apr 24, 2024
1 parent 727032e commit 2e91c14
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions packages/platform-fastify/adapters/fastify-adapter.ts
Expand Up @@ -32,11 +32,12 @@ import {
RawServerBase,
RawServerDefault,
RequestGenericInterface,
RouteOptions,
RouteShorthandOptions,
fastify,
} from 'fastify';
import * as Reply from 'fastify/lib/reply';
import { kRouteContext } from 'fastify/lib/symbols';
import { RouteShorthandMethod } from 'fastify/types/route';
import * as http2 from 'http2';
import * as https from 'https';
import {
Expand Down Expand Up @@ -683,6 +684,13 @@ export class FastifyAdapter<
const hasConfig = !isUndefined(routeConfig);
const hasConstraints = !isUndefined(routeConstraints);

const routeToInject: RouteOptions<TServer, TRawRequest, TRawResponse> &
RouteShorthandOptions = {
method: routerMethodKey,
url: args[0],
handler: handlerRef,
};

if (isVersioned || hasConstraints || hasConfig) {
const isPathAndRouteTuple = args.length === 2;
if (isPathAndRouteTuple) {
Expand All @@ -701,15 +709,12 @@ export class FastifyAdapter<
},
}),
};
const path = args[0];
return this.instance[routerMethodKey](path, options, handlerRef);

const routeToInjectWithOptions = { ...routeToInject, ...options };

return this.instance.route(routeToInjectWithOptions);
}
}

return this.instance[routerMethodKey](
...(args as Parameters<
RouteShorthandMethod<TServer, TRawRequest, TRawResponse>
>),
);
return this.instance.route(routeToInject);
}
}

0 comments on commit 2e91c14

Please sign in to comment.