-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add dynamic error messages
- Loading branch information
Showing
5 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { APP_GUARD } from '@nestjs/core'; | ||
import { seconds, ThrottlerGuard, ThrottlerModule } from '../../src'; | ||
import { CustomErrorMessageController } from './custom-error-message.controller'; | ||
|
||
@Module({ | ||
imports: [ | ||
ThrottlerModule.forRoot({ | ||
errorMessage: (context, throttlerLimitDetail) => | ||
`${context.getClass().name}-${ | ||
context.getHandler().name | ||
} ${throttlerLimitDetail.tracker} ${throttlerLimitDetail.totalHits}`, | ||
throttlers: [ | ||
{ | ||
name: 'default', | ||
ttl: seconds(3), | ||
limit: 2, | ||
}, | ||
{ | ||
name: 'other', | ||
ttl: seconds(3), | ||
limit: 2, | ||
}, | ||
], | ||
}), | ||
], | ||
controllers: [CustomErrorMessageController], | ||
providers: [ | ||
{ | ||
provide: APP_GUARD, | ||
useClass: ThrottlerGuard, | ||
}, | ||
], | ||
}) | ||
export class CustomErrorMessageThrottlerModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { SkipThrottle } from '../../src'; | ||
|
||
@Controller() | ||
export class CustomErrorMessageController { | ||
@SkipThrottle({ other: true }) | ||
@Get('default') | ||
defaultRoute() { | ||
return { success: true }; | ||
} | ||
|
||
@SkipThrottle({ default: true }) | ||
@Get('other') | ||
otherRoute() { | ||
return { success: true }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { INestApplication, Type } from '@nestjs/common'; | ||
import { AbstractHttpAdapter } from '@nestjs/core'; | ||
import { ExpressAdapter } from '@nestjs/platform-express'; | ||
import { FastifyAdapter } from '@nestjs/platform-fastify'; | ||
import { Test } from '@nestjs/testing'; | ||
import { request, spec } from 'pactum'; | ||
import { CustomErrorMessageThrottlerModule } from './app.module'; | ||
|
||
jest.setTimeout(10000); | ||
|
||
describe.each` | ||
adapter | name | ||
${ExpressAdapter} | ${'express'} | ||
${FastifyAdapter} | ${'fastify'} | ||
`( | ||
'Function-Overrides-Throttler Named Usage - $name', | ||
({ adapter }: { adapter: Type<AbstractHttpAdapter> }) => { | ||
let app: INestApplication; | ||
beforeAll(async () => { | ||
const modRef = await Test.createTestingModule({ | ||
imports: [CustomErrorMessageThrottlerModule], | ||
}).compile(); | ||
app = modRef.createNestApplication(new adapter()); | ||
await app.listen(0); | ||
request.setBaseUrl(await app.getUrl()); | ||
}); | ||
afterAll(async () => { | ||
await app.close(); | ||
}); | ||
|
||
describe.each` | ||
route | errorMessage | ||
${'default'} | ${'CustomErrorMessageController-defaultRoute ::1 3'} | ||
${'other'} | ${'CustomErrorMessageController-otherRoute ::1 3'} | ||
`( | ||
'Custom-error-message Route - $route', | ||
({ route, errorMessage }: { route: string; errorMessage: string }) => { | ||
it('should receive a custom exception', async () => { | ||
const limit = 2; | ||
for (let i = 0; i < limit; i++) { | ||
await spec().get(`/${route}`).expectStatus(200); | ||
} | ||
|
||
await spec().get(`/${route}`).expectStatus(429).expectBodyContains(errorMessage); | ||
}); | ||
}, | ||
); | ||
}, | ||
); |