nestjs-backend-common 0.0.0 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/README.md +4 -0
- package/.github/workflows/publish.yml +8 -4
- package/README.md +3 -0
- package/dist/jest-setup-after-env.d.ts +2 -0
- package/dist/jest-setup-after-env.d.ts.map +1 -0
- package/dist/jest.config.d.ts +9 -0
- package/dist/jest.config.d.ts.map +1 -0
- package/dist/src/correlation-id/correlation-id.constant.d.ts +3 -0
- package/dist/src/correlation-id/correlation-id.constant.d.ts.map +1 -0
- package/dist/src/correlation-id/correlation-id.interceptor.d.ts +10 -0
- package/dist/src/correlation-id/correlation-id.interceptor.d.ts.map +1 -0
- package/dist/src/correlation-id/correlation-id.module.d.ts +6 -0
- package/dist/src/correlation-id/correlation-id.module.d.ts.map +1 -0
- package/dist/src/correlation-id/correlation-id.service.d.ts +8 -0
- package/dist/src/correlation-id/correlation-id.service.d.ts.map +1 -0
- package/dist/src/correlation-id/correlation-id.type.d.ts +8 -0
- package/dist/src/correlation-id/correlation-id.type.d.ts.map +1 -0
- package/dist/src/correlation-id/index.d.ts +5 -0
- package/dist/src/correlation-id/index.d.ts.map +1 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/types/index.d.ts +3 -0
- package/dist/src/types/index.d.ts.map +1 -0
- package/jest-setup-after-env.ts +3 -0
- package/jest.config.ts +9 -0
- package/package.json +13 -6
- package/src/correlation-id/correlation-id.interceptor.spec.ts +45 -0
- package/src/correlation-id/correlation-id.interceptor.ts +17 -10
- package/src/correlation-id/correlation-id.service.spec.ts +27 -0
- package/src/global.d.ts +1 -0
- package/tsconfig.build.json +7 -2
- package/dist/src/correlation-id/correlation-id.constant.js +0 -6
- package/dist/src/correlation-id/correlation-id.constant.js.map +0 -1
- package/dist/src/correlation-id/correlation-id.interceptor.js +0 -58
- package/dist/src/correlation-id/correlation-id.interceptor.js.map +0 -1
- package/dist/src/correlation-id/correlation-id.module.js +0 -37
- package/dist/src/correlation-id/correlation-id.module.js.map +0 -1
- package/dist/src/correlation-id/correlation-id.service.js +0 -28
- package/dist/src/correlation-id/correlation-id.service.js.map +0 -1
- package/dist/src/correlation-id/correlation-id.type.js +0 -3
- package/dist/src/correlation-id/correlation-id.type.js.map +0 -1
- package/dist/src/correlation-id/index.js +0 -21
- package/dist/src/correlation-id/index.js.map +0 -1
- package/dist/src/index.js +0 -19
- package/dist/src/index.js.map +0 -1
- package/dist/src/types/index.js +0 -3
- package/dist/src/types/index.js.map +0 -1
package/.github/README.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
All the utility functions and common modules I usually use in my NestJS applications will be published and maintained here.
|
|
4
4
|
|
|
5
|
+
| Module | description |
|
|
6
|
+
| --------------------- | ---------------------------------------------------------------- |
|
|
7
|
+
| `CorrelationIdModule` | Adds a correlation ID to each request (`RPC`, `GraphQL`, `HTTP`) |
|
|
8
|
+
|
|
5
9
|
## [Increase Version](https://docs.npmjs.com/cli/v8/commands/npm-version)
|
|
6
10
|
|
|
7
11
|
```bash
|
|
@@ -20,9 +20,7 @@ jobs:
|
|
|
20
20
|
run: |
|
|
21
21
|
LAST_PUBLISHED_VERSION=$(npm view nestjs-backend-common version 2> /dev/null || echo "")
|
|
22
22
|
PACKAGE_VERSION=$(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[",]//g')
|
|
23
|
-
|
|
24
|
-
echo "Version is published already, if you need to publish a new version please increase the version in package.json"
|
|
25
|
-
fi
|
|
23
|
+
[[ $LAST_PUBLISHED_VERSION == $PACKAGE_VERSION ]] && exit 1 || exit 0
|
|
26
24
|
|
|
27
25
|
- uses: pnpm/action-setup@v4
|
|
28
26
|
name: Install pnpm
|
|
@@ -30,7 +28,7 @@ jobs:
|
|
|
30
28
|
version: 10
|
|
31
29
|
run_install: false
|
|
32
30
|
|
|
33
|
-
- name: Install
|
|
31
|
+
- name: Install NodeJS
|
|
34
32
|
uses: actions/setup-node@v4
|
|
35
33
|
with:
|
|
36
34
|
node-version: 22
|
|
@@ -40,6 +38,12 @@ jobs:
|
|
|
40
38
|
- name: Install Dependencies
|
|
41
39
|
run: pnpm install --frozen-lockfile
|
|
42
40
|
|
|
41
|
+
- name: Apply Eslint
|
|
42
|
+
run: pnpm lint
|
|
43
|
+
|
|
44
|
+
- name: Run Unit Tests
|
|
45
|
+
run: pnpm test:unit
|
|
46
|
+
|
|
43
47
|
- name: Publish Package on npm 📦
|
|
44
48
|
run: pnpm publish --provenance --access public --no-git-checks
|
|
45
49
|
env:
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jest-setup-after-env.d.ts","sourceRoot":"","sources":["../jest-setup-after-env.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jest.config.d.ts","sourceRoot":"","sources":["../jest.config.ts"],"names":[],"mappings":";;;;;;;AAEA,wBAMmB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"correlation-id.constant.d.ts","sourceRoot":"","sources":["../../../src/correlation-id/correlation-id.constant.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,0BAA0B,mBAAmB,CAAC;AAC3D,eAAO,MAAM,sBAAsB,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Observable } from 'rxjs';
|
|
2
|
+
import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
|
|
3
|
+
import { ClsService } from 'nestjs-cls';
|
|
4
|
+
export declare class CorrelationIdInterceptor implements NestInterceptor {
|
|
5
|
+
private readonly clsService;
|
|
6
|
+
private logger;
|
|
7
|
+
constructor(clsService: ClsService);
|
|
8
|
+
intercept(executionContext: ExecutionContext, next: CallHandler): Observable<unknown>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=correlation-id.interceptor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"correlation-id.interceptor.d.ts","sourceRoot":"","sources":["../../../src/correlation-id/correlation-id.interceptor.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAEvC,OAAO,EACL,WAAW,EACX,gBAAgB,EAGhB,eAAe,EAChB,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAQxC,qBACa,wBAAyB,YAAW,eAAe;IAGlD,OAAO,CAAC,QAAQ,CAAC,UAAU;IAFvC,OAAO,CAAC,MAAM,CAA6C;gBAE9B,UAAU,EAAE,UAAU;IAEnD,SAAS,CACP,gBAAgB,EAAE,gBAAgB,EAClC,IAAI,EAAE,WAAW,GAChB,UAAU,CAAC,OAAO,CAAC;CAgDvB"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { CorrelationIdModuleOptions } from './correlation-id.type';
|
|
3
|
+
export declare class CorrelationIdModule {
|
|
4
|
+
forRoot(options?: CorrelationIdModuleOptions): DynamicModule;
|
|
5
|
+
}
|
|
6
|
+
//# sourceMappingURL=correlation-id.module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"correlation-id.module.d.ts","sourceRoot":"","sources":["../../../src/correlation-id/correlation-id.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAU,MAAM,gBAAgB,CAAC;AAMvD,OAAO,EAAE,0BAA0B,EAAE,MAAM,uBAAuB,CAAC;AAEnE,qBACa,mBAAmB;IAC9B,OAAO,CAAC,OAAO,CAAC,EAAE,0BAA0B,GAAG,aAAa;CAe7D"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ClsService } from 'nestjs-cls';
|
|
2
|
+
export declare class CorrelationIdService {
|
|
3
|
+
private readonly clsService;
|
|
4
|
+
private readonly logger;
|
|
5
|
+
constructor(clsService: ClsService);
|
|
6
|
+
get correlationId(): string;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=correlation-id.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"correlation-id.service.d.ts","sourceRoot":"","sources":["../../../src/correlation-id/correlation-id.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAIxC,qBACa,oBAAoB;IAGnB,OAAO,CAAC,QAAQ,CAAC,UAAU;IAFvC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAyC;gBAEnC,UAAU,EAAE,UAAU;IAEnD,IAAI,aAAa,IAAI,MAAM,CAI1B;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"correlation-id.type.d.ts","sourceRoot":"","sources":["../../../src/correlation-id/correlation-id.type.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,0BAA0B;IACzC;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/correlation-id/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC;AACxC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C,MAAM,MAAM,sBAAsB,GAAG,SAAS,GAAG,WAAW,CAAC"}
|
package/jest.config.ts
ADDED
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nestjs-backend-common",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"main": "dist/index",
|
|
5
|
-
"types": "dist/index",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"main": "dist/src/index",
|
|
5
|
+
"types": "dist/src/index",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+ssh://git@github.com/kasir-barati/nestjs-backend-common.git"
|
|
@@ -26,23 +26,30 @@
|
|
|
26
26
|
"@nestjs/common": "^11.0.20",
|
|
27
27
|
"@nestjs/core": "^11.0.20",
|
|
28
28
|
"@nestjs/graphql": "^13.1.0",
|
|
29
|
-
"nestjs-cls": "^5.4.3"
|
|
29
|
+
"nestjs-cls": "^5.4.3",
|
|
30
|
+
"rxjs": "^7.8.2"
|
|
30
31
|
},
|
|
31
32
|
"devDependencies": {
|
|
32
33
|
"@eslint/js": "^9.25.0",
|
|
33
34
|
"@types/express": "^5.0.1",
|
|
35
|
+
"@types/jest": "^29.5.14",
|
|
34
36
|
"@types/node": "^22.14.1",
|
|
37
|
+
"eslint": "^9.25.0",
|
|
35
38
|
"eslint-config-prettier": "^10.1.2",
|
|
36
39
|
"eslint-plugin-perfectionist": "^4.11.0",
|
|
37
40
|
"eslint-plugin-prettier": "^5.2.6",
|
|
38
41
|
"globals": "^16.0.0",
|
|
42
|
+
"jest": "^29.7.0",
|
|
43
|
+
"jest-extended": "^4.0.2",
|
|
39
44
|
"prettier": "^3.5.3",
|
|
45
|
+
"ts-jest": "^29.3.2",
|
|
46
|
+
"ts-node": "^10.9.2",
|
|
40
47
|
"typescript": "^5.8.3",
|
|
41
48
|
"typescript-eslint": "^8.30.1"
|
|
42
49
|
},
|
|
43
50
|
"scripts": {
|
|
44
|
-
"test": "jest",
|
|
45
|
-
"build": "rm -rf dist && tsc",
|
|
51
|
+
"test:unit": "jest",
|
|
52
|
+
"build": "rm -rf dist && tsc --project tsconfig.build.json",
|
|
46
53
|
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\"",
|
|
47
54
|
"lint:fix": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
|
48
55
|
"prepublish": "npm run build"
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { CORRELATION_ID_CLS_KEY } from './correlation-id.constant';
|
|
2
|
+
import { CorrelationIdInterceptor } from './correlation-id.interceptor';
|
|
3
|
+
|
|
4
|
+
jest.mock('@nestjs/graphql', () => ({
|
|
5
|
+
GqlExecutionContext: {
|
|
6
|
+
create: jest.fn().mockReturnValue({
|
|
7
|
+
getContext: jest.fn().mockReturnValue({ req: { headers: {} } }),
|
|
8
|
+
}),
|
|
9
|
+
},
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
describe('CorrelationIdInterceptor', () => {
|
|
13
|
+
let service: CorrelationIdInterceptor;
|
|
14
|
+
let clsService: any;
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
clsService = {};
|
|
18
|
+
service = new CorrelationIdInterceptor(clsService);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it.each(['http', 'graphql', 'rpc'])(
|
|
22
|
+
'should store the correlation ID in nestjs-cls storage',
|
|
23
|
+
(type) => {
|
|
24
|
+
clsService.set = jest.fn();
|
|
25
|
+
const executionContext: any = {
|
|
26
|
+
getType: jest.fn(() => type),
|
|
27
|
+
switchToHttp: jest.fn().mockReturnValue({
|
|
28
|
+
getRequest: jest.fn().mockReturnValue({ headers: {} }),
|
|
29
|
+
}),
|
|
30
|
+
switchToRpc: jest.fn().mockReturnValue({
|
|
31
|
+
getContext: jest
|
|
32
|
+
.fn()
|
|
33
|
+
.mockReturnValue({ correlationId: 'correlation uuid' }),
|
|
34
|
+
}),
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
service.intercept(executionContext, { handle: jest.fn() });
|
|
38
|
+
|
|
39
|
+
expect(clsService.set).toHaveBeenCalledWith(
|
|
40
|
+
CORRELATION_ID_CLS_KEY,
|
|
41
|
+
expect.any(String),
|
|
42
|
+
);
|
|
43
|
+
},
|
|
44
|
+
);
|
|
45
|
+
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Request } from 'express';
|
|
2
|
+
import type { Observable } from 'rxjs';
|
|
2
3
|
|
|
3
4
|
import {
|
|
4
5
|
CallHandler,
|
|
@@ -23,7 +24,10 @@ export class CorrelationIdInterceptor implements NestInterceptor {
|
|
|
23
24
|
|
|
24
25
|
constructor(private readonly clsService: ClsService) {}
|
|
25
26
|
|
|
26
|
-
intercept(
|
|
27
|
+
intercept(
|
|
28
|
+
executionContext: ExecutionContext,
|
|
29
|
+
next: CallHandler,
|
|
30
|
+
): Observable<unknown> {
|
|
27
31
|
let correlationId: string;
|
|
28
32
|
|
|
29
33
|
switch (executionContext.getType<CommonExecutionContext>()) {
|
|
@@ -31,11 +35,11 @@ export class CorrelationIdInterceptor implements NestInterceptor {
|
|
|
31
35
|
const request: Request = executionContext
|
|
32
36
|
.switchToHttp()
|
|
33
37
|
.getRequest();
|
|
34
|
-
const
|
|
35
|
-
request.headers[CORRELATION_ID_HEADER_NAME]
|
|
36
|
-
)
|
|
37
|
-
?
|
|
38
|
-
:
|
|
38
|
+
const correlationIdHeader =
|
|
39
|
+
request.headers[CORRELATION_ID_HEADER_NAME];
|
|
40
|
+
const correlationIdValue = Array.isArray(correlationIdHeader)
|
|
41
|
+
? correlationIdHeader[0]
|
|
42
|
+
: correlationIdHeader;
|
|
39
43
|
|
|
40
44
|
correlationId = correlationIdValue ?? randomUUID();
|
|
41
45
|
|
|
@@ -43,11 +47,14 @@ export class CorrelationIdInterceptor implements NestInterceptor {
|
|
|
43
47
|
}
|
|
44
48
|
case 'graphql': {
|
|
45
49
|
const ctx = GqlExecutionContext.create(executionContext);
|
|
46
|
-
const
|
|
50
|
+
const request: Request = ctx.getContext().req;
|
|
51
|
+
const correlationIdHeader =
|
|
52
|
+
request.headers[CORRELATION_ID_HEADER_NAME];
|
|
53
|
+
const correlationIdValue = Array.isArray(correlationIdHeader)
|
|
54
|
+
? correlationIdHeader[0]
|
|
55
|
+
: correlationIdHeader;
|
|
47
56
|
|
|
48
|
-
correlationId =
|
|
49
|
-
CORRELATION_ID_HEADER_NAME
|
|
50
|
-
] as string;
|
|
57
|
+
correlationId = correlationIdValue ?? randomUUID();
|
|
51
58
|
|
|
52
59
|
break;
|
|
53
60
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { CorrelationIdService } from './correlation-id.service';
|
|
2
|
+
|
|
3
|
+
describe('CorrelationIdService', () => {
|
|
4
|
+
let service: CorrelationIdService;
|
|
5
|
+
let clsService: any;
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
clsService = {};
|
|
9
|
+
service = new CorrelationIdService(clsService);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('should return the correlation ID', () => {
|
|
13
|
+
clsService.get = jest.fn(() => 'correlation uuid');
|
|
14
|
+
|
|
15
|
+
const correlationId = service.correlationId;
|
|
16
|
+
|
|
17
|
+
expect(correlationId).toBe('correlation uuid');
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should generate a new UUID in case we did not have a correlation ID', () => {
|
|
21
|
+
clsService.get = jest.fn(() => undefined);
|
|
22
|
+
|
|
23
|
+
const correlationId = service.correlationId;
|
|
24
|
+
|
|
25
|
+
expect(correlationId).toBeString();
|
|
26
|
+
});
|
|
27
|
+
});
|
package/src/global.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import 'jest-extended';
|
package/tsconfig.build.json
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"declaration": true,
|
|
5
|
+
"declarationMap": true,
|
|
6
|
+
"emitDeclarationOnly": true
|
|
7
|
+
},
|
|
3
8
|
"exclude": [
|
|
4
9
|
"node_modules",
|
|
5
|
-
"test",
|
|
6
10
|
"dist",
|
|
7
|
-
"
|
|
11
|
+
"**/*.spec.ts",
|
|
12
|
+
"**/*.test.ts",
|
|
8
13
|
"eslint.config.mjs"
|
|
9
14
|
]
|
|
10
15
|
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CORRELATION_ID_CLS_KEY = exports.CORRELATION_ID_HEADER_NAME = void 0;
|
|
4
|
-
exports.CORRELATION_ID_HEADER_NAME = 'correlation-id';
|
|
5
|
-
exports.CORRELATION_ID_CLS_KEY = 'CORRELATION_ID_CLS_KEY';
|
|
6
|
-
//# sourceMappingURL=correlation-id.constant.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"correlation-id.constant.js","sourceRoot":"","sources":["../../../src/correlation-id/correlation-id.constant.ts"],"names":[],"mappings":";;;AAAa,QAAA,0BAA0B,GAAG,gBAAgB,CAAC;AAC9C,QAAA,sBAAsB,GAAG,wBAAwB,CAAC"}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var CorrelationIdInterceptor_1;
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.CorrelationIdInterceptor = void 0;
|
|
11
|
-
const common_1 = require("@nestjs/common");
|
|
12
|
-
const graphql_1 = require("@nestjs/graphql");
|
|
13
|
-
const crypto_1 = require("crypto");
|
|
14
|
-
const correlation_id_constant_1 = require("./correlation-id.constant");
|
|
15
|
-
let CorrelationIdInterceptor = CorrelationIdInterceptor_1 = class CorrelationIdInterceptor {
|
|
16
|
-
clsService;
|
|
17
|
-
logger = new common_1.Logger(CorrelationIdInterceptor_1.name);
|
|
18
|
-
constructor(clsService) {
|
|
19
|
-
this.clsService = clsService;
|
|
20
|
-
}
|
|
21
|
-
intercept(executionContext, next) {
|
|
22
|
-
let correlationId;
|
|
23
|
-
switch (executionContext.getType()) {
|
|
24
|
-
case 'http': {
|
|
25
|
-
const request = executionContext
|
|
26
|
-
.switchToHttp()
|
|
27
|
-
.getRequest();
|
|
28
|
-
const correlationIdValue = Array.isArray(request.headers[correlation_id_constant_1.CORRELATION_ID_HEADER_NAME])
|
|
29
|
-
? request.headers[correlation_id_constant_1.CORRELATION_ID_HEADER_NAME][0]
|
|
30
|
-
: request.headers[correlation_id_constant_1.CORRELATION_ID_HEADER_NAME];
|
|
31
|
-
correlationId = correlationIdValue ?? (0, crypto_1.randomUUID)();
|
|
32
|
-
break;
|
|
33
|
-
}
|
|
34
|
-
case 'graphql': {
|
|
35
|
-
const ctx = graphql_1.GqlExecutionContext.create(executionContext);
|
|
36
|
-
const req = ctx.getContext().req;
|
|
37
|
-
correlationId = req.headers[correlation_id_constant_1.CORRELATION_ID_HEADER_NAME];
|
|
38
|
-
break;
|
|
39
|
-
}
|
|
40
|
-
case 'rpc': {
|
|
41
|
-
const { correlationId: correlationIdValue } = executionContext
|
|
42
|
-
.switchToRpc()
|
|
43
|
-
.getContext();
|
|
44
|
-
correlationId = correlationIdValue ?? (0, crypto_1.randomUUID)();
|
|
45
|
-
break;
|
|
46
|
-
}
|
|
47
|
-
default:
|
|
48
|
-
throw new Error('Unimplemented request type');
|
|
49
|
-
}
|
|
50
|
-
this.clsService.set(correlation_id_constant_1.CORRELATION_ID_CLS_KEY, correlationId);
|
|
51
|
-
return next.handle();
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
exports.CorrelationIdInterceptor = CorrelationIdInterceptor;
|
|
55
|
-
exports.CorrelationIdInterceptor = CorrelationIdInterceptor = CorrelationIdInterceptor_1 = __decorate([
|
|
56
|
-
(0, common_1.Injectable)()
|
|
57
|
-
], CorrelationIdInterceptor);
|
|
58
|
-
//# sourceMappingURL=correlation-id.interceptor.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"correlation-id.interceptor.js","sourceRoot":"","sources":["../../../src/correlation-id/correlation-id.interceptor.ts"],"names":[],"mappings":";;;;;;;;;;AAEA,2CAMwB;AACxB,6CAAsD;AACtD,mCAAoC;AAIpC,uEAGmC;AAG5B,IAAM,wBAAwB,gCAA9B,MAAM,wBAAwB;IAGN;IAFrB,MAAM,GAAG,IAAI,eAAM,CAAC,0BAAwB,CAAC,IAAI,CAAC,CAAC;IAE3D,YAA6B,UAAsB;QAAtB,eAAU,GAAV,UAAU,CAAY;IAAG,CAAC;IAEvD,SAAS,CAAC,gBAAkC,EAAE,IAAiB;QAC7D,IAAI,aAAqB,CAAC;QAE1B,QAAQ,gBAAgB,CAAC,OAAO,EAA0B,EAAE,CAAC;YAC3D,KAAK,MAAM,CAAC,CAAC,CAAC;gBACZ,MAAM,OAAO,GAAY,gBAAgB;qBACtC,YAAY,EAAE;qBACd,UAAU,EAAE,CAAC;gBAChB,MAAM,kBAAkB,GAAG,KAAK,CAAC,OAAO,CACtC,OAAO,CAAC,OAAO,CAAC,oDAA0B,CAAC,CAC5C;oBACC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,oDAA0B,CAAC,CAAC,CAAC,CAAC;oBAChD,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,oDAA0B,CAAC,CAAC;gBAEhD,aAAa,GAAG,kBAAkB,IAAI,IAAA,mBAAU,GAAE,CAAC;gBAEnD,MAAM;YACR,CAAC;YACD,KAAK,SAAS,CAAC,CAAC,CAAC;gBACf,MAAM,GAAG,GAAG,6BAAmB,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC;gBACzD,MAAM,GAAG,GAAY,GAAG,CAAC,UAAU,EAAE,CAAC,GAAG,CAAC;gBAE1C,aAAa,GAAG,GAAG,CAAC,OAAO,CACzB,oDAA0B,CACjB,CAAC;gBAEZ,MAAM;YACR,CAAC;YACD,KAAK,KAAK,CAAC,CAAC,CAAC;gBACX,MAAM,EAAE,aAAa,EAAE,kBAAkB,EAAE,GAAG,gBAAgB;qBAC3D,WAAW,EAAE;qBACb,UAAU,EAA8B,CAAC;gBAE5C,aAAa,GAAG,kBAAkB,IAAI,IAAA,mBAAU,GAAE,CAAC;gBAEnD,MAAM;YACR,CAAC;YACD;gBACE,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;QAClD,CAAC;QAED,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,gDAAsB,EAAE,aAAa,CAAC,CAAC;QAE3D,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC;CACF,CAAA;AAlDY,4DAAwB;mCAAxB,wBAAwB;IADpC,IAAA,mBAAU,GAAE;GACA,wBAAwB,CAkDpC"}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var CorrelationIdModule_1;
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.CorrelationIdModule = void 0;
|
|
11
|
-
const common_1 = require("@nestjs/common");
|
|
12
|
-
const core_1 = require("@nestjs/core");
|
|
13
|
-
const nestjs_cls_1 = require("nestjs-cls");
|
|
14
|
-
const correlation_id_interceptor_1 = require("./correlation-id.interceptor");
|
|
15
|
-
const correlation_id_service_1 = require("./correlation-id.service");
|
|
16
|
-
let CorrelationIdModule = CorrelationIdModule_1 = class CorrelationIdModule {
|
|
17
|
-
forRoot(options) {
|
|
18
|
-
return {
|
|
19
|
-
global: options?.isGlobal ?? false,
|
|
20
|
-
module: CorrelationIdModule_1,
|
|
21
|
-
imports: [nestjs_cls_1.ClsModule],
|
|
22
|
-
providers: [
|
|
23
|
-
{
|
|
24
|
-
provide: core_1.APP_INTERCEPTOR,
|
|
25
|
-
useClass: correlation_id_interceptor_1.CorrelationIdInterceptor,
|
|
26
|
-
},
|
|
27
|
-
correlation_id_service_1.CorrelationIdService,
|
|
28
|
-
],
|
|
29
|
-
exports: [correlation_id_service_1.CorrelationIdService],
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
exports.CorrelationIdModule = CorrelationIdModule;
|
|
34
|
-
exports.CorrelationIdModule = CorrelationIdModule = CorrelationIdModule_1 = __decorate([
|
|
35
|
-
(0, common_1.Module)({})
|
|
36
|
-
], CorrelationIdModule);
|
|
37
|
-
//# sourceMappingURL=correlation-id.module.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"correlation-id.module.js","sourceRoot":"","sources":["../../../src/correlation-id/correlation-id.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAuD;AACvD,uCAA+C;AAC/C,2CAAuC;AAEvC,6EAAwE;AACxE,qEAAgE;AAIzD,IAAM,mBAAmB,2BAAzB,MAAM,mBAAmB;IAC9B,OAAO,CAAC,OAAoC;QAC1C,OAAO;YACL,MAAM,EAAE,OAAO,EAAE,QAAQ,IAAI,KAAK;YAClC,MAAM,EAAE,qBAAmB;YAC3B,OAAO,EAAE,CAAC,sBAAS,CAAC;YACpB,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,sBAAe;oBACxB,QAAQ,EAAE,qDAAwB;iBACnC;gBACD,6CAAoB;aACrB;YACD,OAAO,EAAE,CAAC,6CAAoB,CAAC;SAChC,CAAC;IACJ,CAAC;CACF,CAAA;AAhBY,kDAAmB;8BAAnB,mBAAmB;IAD/B,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,mBAAmB,CAgB/B"}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var CorrelationIdService_1;
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.CorrelationIdService = void 0;
|
|
11
|
-
const common_1 = require("@nestjs/common");
|
|
12
|
-
const crypto_1 = require("crypto");
|
|
13
|
-
const correlation_id_constant_1 = require("./correlation-id.constant");
|
|
14
|
-
let CorrelationIdService = CorrelationIdService_1 = class CorrelationIdService {
|
|
15
|
-
clsService;
|
|
16
|
-
logger = new common_1.Logger(CorrelationIdService_1.name);
|
|
17
|
-
constructor(clsService) {
|
|
18
|
-
this.clsService = clsService;
|
|
19
|
-
}
|
|
20
|
-
get correlationId() {
|
|
21
|
-
return (this.clsService.get(correlation_id_constant_1.CORRELATION_ID_CLS_KEY) ?? (0, crypto_1.randomUUID)());
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
exports.CorrelationIdService = CorrelationIdService;
|
|
25
|
-
exports.CorrelationIdService = CorrelationIdService = CorrelationIdService_1 = __decorate([
|
|
26
|
-
(0, common_1.Injectable)()
|
|
27
|
-
], CorrelationIdService);
|
|
28
|
-
//# sourceMappingURL=correlation-id.service.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"correlation-id.service.js","sourceRoot":"","sources":["../../../src/correlation-id/correlation-id.service.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAoD;AACpD,mCAAoC;AAGpC,uEAAmE;AAG5D,IAAM,oBAAoB,4BAA1B,MAAM,oBAAoB;IAGF;IAFZ,MAAM,GAAG,IAAI,eAAM,CAAC,sBAAoB,CAAC,IAAI,CAAC,CAAC;IAEhE,YAA6B,UAAsB;QAAtB,eAAU,GAAV,UAAU,CAAY;IAAG,CAAC;IAEvD,IAAI,aAAa;QACf,OAAO,CACL,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,gDAAsB,CAAC,IAAI,IAAA,mBAAU,GAAE,CAC5D,CAAC;IACJ,CAAC;CACF,CAAA;AAVY,oDAAoB;+BAApB,oBAAoB;IADhC,IAAA,mBAAU,GAAE;GACA,oBAAoB,CAUhC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"correlation-id.type.js","sourceRoot":"","sources":["../../../src/correlation-id/correlation-id.type.ts"],"names":[],"mappings":""}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./correlation-id.constant"), exports);
|
|
18
|
-
__exportStar(require("./correlation-id.module"), exports);
|
|
19
|
-
__exportStar(require("./correlation-id.service"), exports);
|
|
20
|
-
__exportStar(require("./correlation-id.type"), exports);
|
|
21
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/correlation-id/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4DAA0C;AAC1C,0DAAwC;AACxC,2DAAyC;AACzC,wDAAsC"}
|
package/dist/src/index.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./correlation-id"), exports);
|
|
18
|
-
__exportStar(require("./types"), exports);
|
|
19
|
-
//# sourceMappingURL=index.js.map
|
package/dist/src/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAiC;AACjC,0CAAwB"}
|
package/dist/src/types/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":""}
|