File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change
1
+ import { DiscoveredMethodWithMeta } from '@golevelup/nestjs-discovery' ;
2
+
3
+ export const uniqueDiscoveredMethods = < T > (
4
+ methods : Array < DiscoveredMethodWithMeta < T > > ,
5
+ ) => {
6
+ const seenClasses = new Map < object , Map < string , Set < unknown > > > ( ) ;
7
+ const uniqueMethods = [ ] as typeof methods ;
8
+ for ( const method of methods ) {
9
+ const clsInstance = method . discoveredMethod . parentClass . instance ;
10
+ const methodName = method . discoveredMethod . methodName ;
11
+ if ( ! seenClasses . has ( clsInstance ) ) {
12
+ seenClasses . set ( clsInstance , new Map ( ) ) ;
13
+ }
14
+ const seenMethods = seenClasses . get ( clsInstance ) ! ;
15
+ if ( ! seenMethods . has ( methodName ) ) {
16
+ seenMethods . set ( methodName , new Set ( ) ) ;
17
+ }
18
+ const seenMetadata = seenMethods . get ( methodName ) ! ;
19
+ if ( ! seenMetadata . has ( method . meta ) ) {
20
+ seenMetadata . add ( method . meta ) ;
21
+ uniqueMethods . push ( method ) ;
22
+ }
23
+ }
24
+ return uniqueMethods ;
25
+ } ;
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import {
15
15
import type { FastifyInstance , HTTPMethods , RouteOptions } from 'fastify' ;
16
16
import rawBody from 'fastify-raw-body' ;
17
17
import * as zlib from 'node:zlib' ;
18
+ import { uniqueDiscoveredMethods } from '~/common/discovery-unique-methods' ;
18
19
import { ConfigService } from '~/core/config/config.service' ;
19
20
import {
20
21
GlobalHttpHook ,
@@ -73,7 +74,7 @@ export class HttpAdapter extends PatchedFastifyAdapter {
73
74
. get ( DiscoveryService )
74
75
. providerMethodsWithMetaAtKey < keyof HttpHooks > ( GlobalHttpHook . KEY ) ;
75
76
const fastify = app . getHttpAdapter ( ) . getInstance ( ) ;
76
- for ( const globalHook of globalHooks ) {
77
+ for ( const globalHook of uniqueDiscoveredMethods ( globalHooks ) ) {
77
78
const handler = globalHook . discoveredMethod . handler . bind (
78
79
globalHook . discoveredMethod . parentClass . instance ,
79
80
) ;
You can’t perform that action at this time.
0 commit comments