Skip to content

Commit

Permalink
fix: properly handle the object passed to the skip throttle decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcdo29 committed Oct 23, 2023
1 parent 4cd0f04 commit bc9e6b2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-papayas-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@nestjs/throttler': patch
---

Correctly assign metadata for multiple throttlers passed to `@SkipThrottle()`
9 changes: 3 additions & 6 deletions src/throttler.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,11 @@ export const SkipThrottle = (
propertyKey?: string | symbol,
descriptor?: TypedPropertyDescriptor<any>,
) => {
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;
};
};

Expand Down
13 changes: 9 additions & 4 deletions test/multi/multi-throttler.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe.each`
adapter | name
${ExpressAdapter} | ${'express'}
${FastifyAdapter} | ${'fastify'}
`('Mutli-Throttler Named Usage - $name', ({ adapter }: { adapter: Type<AbstractHttpAdapter> }) => {
`('Multi-Throttler Named Usage - $name', ({ adapter }: { adapter: Type<AbstractHttpAdapter> }) => {
let app: INestApplication;
beforeAll(async () => {
const modRef = await Test.createTestingModule({
Expand All @@ -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)
Expand Down Expand Up @@ -78,15 +78,20 @@ 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');
});
it('should skip the default and long trackers', async () => {
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();
});
});
});
});

0 comments on commit bc9e6b2

Please sign in to comment.