Skip to content

Commit 589200a

Browse files
committed
feat(nestjs): add nestjs interceptor
1 parent 5a754d5 commit 589200a

9 files changed

Lines changed: 770 additions & 11 deletions

File tree

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ or
4141
yarn add stalier
4242
```
4343

44-
## USAGE
44+
## USAGE : Express
4545

4646
Stalier do not provide a cache on it's own, nor does it provide a logger. But you provide them both in the constructor.
4747

@@ -119,6 +119,28 @@ app.use(stalier({
119119
}));
120120
```
121121

122+
## USAGE : NestJS
123+
124+
Stalier uses the `NestJsInterceptor` to intercept the request and response, and uses the Cache Module to cache the response.
125+
126+
```typescript
127+
// declare a fake controller to test the interceptor
128+
@UseStalierInterceptor({ appName: 'myApp' })
129+
@Controller()
130+
class MyAppController {
131+
@Get('/default-kay')
132+
getDefaultKey() {
133+
return { hello: 'world' };
134+
}
135+
136+
@UseCacheKeyGen(() => 'test')
137+
@Get('/custom-key')
138+
getCustomKey() {
139+
return { hello: 'world' };
140+
}
141+
}
142+
```
143+
122144
## Header `x-stalier-cache-control` params
123145

124146
Stalier is using the `x-stalier-cache-control` header to control the cache behaviour.

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,29 @@
2828
"test": "jest --coverage"
2929
},
3030
"devDependencies": {
31+
"@nestjs/common": "9.0.7",
32+
"@nestjs/core": "9.0.7",
33+
"@nestjs/platform-express": "^9.0.7",
34+
"@nestjs/testing": "9.0.7",
35+
"@types/cache-manager": "^4.0.1",
3136
"@types/express": "4.17.13",
3237
"@types/jest": "28.1.6",
3338
"@types/node": "18.0.1",
3439
"@types/supertest": "2.0.12",
3540
"@typescript-eslint/eslint-plugin": "5.31.0",
3641
"@typescript-eslint/parser": "5.31.0",
42+
"cache-manager": "^4.1.0",
3743
"eslint": "8.20.0",
3844
"eslint-config-prettier": "8.5.0",
3945
"express": "4.18.1",
4046
"jest": "28.1.3",
4147
"prettier": "2.7.1",
48+
"reflect-metadata": "0.1.13",
49+
"rxjs": "7.5.6",
4250
"supertest": "6.2.4",
4351
"ts-jest": "28.0.7",
4452
"ts-node": "10.9.1",
4553
"typescript": "4.7.4"
46-
}
54+
},
55+
"dependencies": {}
4756
}

src/common/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export const STALIER_HEADER_KEY = 'X-Stalier-Cache-Control';
2+
export const STALIER_APP_NAME = 'STALIER_APP_NAME';
3+
export const STALIER_CACHE_KEY_GEN = 'STALIER_CACHE_KEY_GEN';
24

35
export const MATCH_HEADER = /s-maxage=([0-9]+)(\s*,\s*(stale-while-revalidate=([0-9]+)))?/;

src/common/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import { Request } from 'express';
44
* default cache key generator
55
*/
66
export const defaultKeyGenerator = (name: string) => (req: Request) => {
7-
return `${name}-${req.method}-${req.originalUrl}`;
7+
return `${name}-${req.method}-${req.originalUrl.replace(/[._~:/?#[\]@!$&'()*+,;=]/g, '')}`;
88
};

src/nestjs/decorators.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { applyDecorators, UseInterceptors, SetMetadata } from '@nestjs/common';
2+
import { Request } from 'express';
3+
import { STALIER_APP_NAME, STALIER_CACHE_KEY_GEN } from '../common/constants';
4+
import { StalierInterceptor } from './interceptor';
5+
6+
export const UseStalierInterceptor = (options: { appName: string; cacheKeyGen?: (req: Request) => string }) =>
7+
applyDecorators(
8+
SetMetadata(STALIER_APP_NAME, options.appName),
9+
SetMetadata(STALIER_CACHE_KEY_GEN, options.cacheKeyGen),
10+
UseInterceptors(StalierInterceptor),
11+
);
12+
13+
export const UseCacheKeyGen = (keyGen: (req: Request) => string) => SetMetadata(STALIER_CACHE_KEY_GEN, keyGen);

0 commit comments

Comments
 (0)