diff --git a/.changeset/twenty-papayas-bathe.md b/.changeset/twenty-papayas-bathe.md new file mode 100644 index 00000000..40350926 --- /dev/null +++ b/.changeset/twenty-papayas-bathe.md @@ -0,0 +1,5 @@ +--- +'@nestjs/throttler': patch +--- + +Correctly assign metadata for multiple throttlers passed to `@SkipThrottle()` diff --git a/src/throttler.decorator.ts b/src/throttler.decorator.ts index b7d35eb7..9036d725 100644 --- a/src/throttler.decorator.ts +++ b/src/throttler.decorator.ts @@ -57,14 +57,11 @@ export const SkipThrottle = ( propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor, ) => { + const reflectionTarget = descriptor?.value ?? target; for (const key in skip) { - if (descriptor) { - Reflect.defineMetadata(THROTTLER_SKIP + key, skip[key], descriptor.value); - return descriptor; - } - Reflect.defineMetadata(THROTTLER_SKIP + key, skip[key], target); - return target; + Reflect.defineMetadata(THROTTLER_SKIP + key, skip[key], reflectionTarget); } + return descriptor ?? target; }; }; diff --git a/test/multi/multi-throttler.e2e-spec.ts b/test/multi/multi-throttler.e2e-spec.ts index 440d11fd..1b4666f4 100644 --- a/test/multi/multi-throttler.e2e-spec.ts +++ b/test/multi/multi-throttler.e2e-spec.ts @@ -22,7 +22,7 @@ describe.each` adapter | name ${ExpressAdapter} | ${'express'} ${FastifyAdapter} | ${'fastify'} -`('Mutli-Throttler Named Usage - $name', ({ adapter }: { adapter: Type }) => { +`('Multi-Throttler Named Usage - $name', ({ adapter }: { adapter: Type }) => { let app: INestApplication; beforeAll(async () => { const modRef = await Test.createTestingModule({ @@ -37,7 +37,7 @@ describe.each` }); describe('Default Route: 1/s, 2/5s, 5/min', () => { - it('should receive an exception when firing 2 request swithin a second', async () => { + it('should receive an exception when firing 2 requests within a second', async () => { await spec() .get('/') .expectStatus(200) @@ -78,7 +78,7 @@ describe.each` }); }); describe('skips', () => { - it('should skip theshort throttler', async () => { + it('should skip the short throttler', async () => { await spec().get('/skip-short').expectStatus(200).expectHeader(remainingHeader(), '1'); await spec().get('/skip-short').expectStatus(200).expectHeader(remainingHeader(), '0'); }); @@ -86,7 +86,12 @@ describe.each` await spec() .get('/skip-default-and-long') .expectStatus(200) - .expectHeader(remainingHeader(short), '0'); + .expectHeader(remainingHeader(short), '0') + .expect((ctx) => { + const { headers } = ctx.res; + expect(headers[remainingHeader('default')]).toBeUndefined(); + expect(headers[remainingHeader('long')]).toBeUndefined(); + }); }); }); });