@cryptexlabs/codex-nodejs-common 0.15.5 → 0.16.1
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/lib/package.json +1 -1
- package/lib/src/decorator/api-meta-headers.d.ts +1 -1
- package/lib/src/decorator/api-meta-headers.js +31 -11
- package/lib/src/decorator/api-meta-headers.js.map +1 -1
- package/lib/src/response/strip-meta.response.interceptor.d.ts +5 -0
- package/lib/src/response/strip-meta.response.interceptor.js +34 -0
- package/lib/src/response/strip-meta.response.interceptor.js.map +1 -0
- package/package.json +1 -1
- package/src/decorator/api-meta-headers.ts +41 -11
- package/src/response/strip-meta.response.interceptor.ts +53 -0
package/lib/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function ApiMetaHeaders(): <TFunction extends Function, Y>(target: object | TFunction, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
1
|
+
export declare function ApiMetaHeaders(required?: boolean): <TFunction extends Function, Y>(target: object | TFunction, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
@@ -4,18 +4,22 @@ exports.ApiMetaHeaders = ApiMetaHeaders;
|
|
|
4
4
|
const swagger_1 = require("@nestjs/swagger");
|
|
5
5
|
const common_1 = require("@nestjs/common");
|
|
6
6
|
const uuid_1 = require("uuid");
|
|
7
|
-
function ApiMetaHeaders() {
|
|
7
|
+
function ApiMetaHeaders(required = undefined) {
|
|
8
8
|
return (0, common_1.applyDecorators)(...[
|
|
9
9
|
(0, swagger_1.ApiHeader)({
|
|
10
10
|
name: "X-Correlation-Id",
|
|
11
|
-
required:
|
|
11
|
+
required: required !== undefined
|
|
12
|
+
? required
|
|
13
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
12
14
|
schema: {
|
|
13
15
|
example: (0, uuid_1.v4)(),
|
|
14
16
|
},
|
|
15
17
|
}),
|
|
16
18
|
(0, swagger_1.ApiHeader)({
|
|
17
19
|
name: "Accept-Language",
|
|
18
|
-
required:
|
|
20
|
+
required: required !== undefined
|
|
21
|
+
? required
|
|
22
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
19
23
|
schema: {
|
|
20
24
|
example: "en-US",
|
|
21
25
|
},
|
|
@@ -23,7 +27,9 @@ function ApiMetaHeaders() {
|
|
|
23
27
|
(0, swagger_1.ApiHeader)({
|
|
24
28
|
name: "X-Started",
|
|
25
29
|
description: "The time that the very first user initiated or time initiated event that eventually lead to this api being called was created",
|
|
26
|
-
required:
|
|
30
|
+
required: required !== undefined
|
|
31
|
+
? required
|
|
32
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
27
33
|
schema: {
|
|
28
34
|
example: new Date().toISOString(),
|
|
29
35
|
},
|
|
@@ -31,7 +37,9 @@ function ApiMetaHeaders() {
|
|
|
31
37
|
(0, swagger_1.ApiHeader)({
|
|
32
38
|
name: "X-Context-Category",
|
|
33
39
|
description: "A category for the context of the request. For example 'test' or 'performance test'",
|
|
34
|
-
required:
|
|
40
|
+
required: required !== undefined
|
|
41
|
+
? required
|
|
42
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
35
43
|
schema: {
|
|
36
44
|
example: "default",
|
|
37
45
|
},
|
|
@@ -39,7 +47,9 @@ function ApiMetaHeaders() {
|
|
|
39
47
|
(0, swagger_1.ApiHeader)({
|
|
40
48
|
name: "X-Context-Id",
|
|
41
49
|
description: "A unique context identifier used for correlating logs and metrics usually for performance or experimental testing",
|
|
42
|
-
required:
|
|
50
|
+
required: required !== undefined
|
|
51
|
+
? required
|
|
52
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
43
53
|
schema: {
|
|
44
54
|
example: "none",
|
|
45
55
|
},
|
|
@@ -47,7 +57,9 @@ function ApiMetaHeaders() {
|
|
|
47
57
|
(0, swagger_1.ApiHeader)({
|
|
48
58
|
name: "X-Client-Id",
|
|
49
59
|
description: "A unique identifier for the client to help identify exactly which application or third party is making the requests",
|
|
50
|
-
required:
|
|
60
|
+
required: required !== undefined
|
|
61
|
+
? required
|
|
62
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
51
63
|
schema: {},
|
|
52
64
|
examples: {
|
|
53
65
|
UUIDV4: {
|
|
@@ -61,7 +73,9 @@ function ApiMetaHeaders() {
|
|
|
61
73
|
(0, swagger_1.ApiHeader)({
|
|
62
74
|
name: "X-Client-Name",
|
|
63
75
|
description: "The name of the client",
|
|
64
|
-
required:
|
|
76
|
+
required: required !== undefined
|
|
77
|
+
? required
|
|
78
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
65
79
|
schema: {
|
|
66
80
|
example: "swagger",
|
|
67
81
|
},
|
|
@@ -70,7 +84,9 @@ function ApiMetaHeaders() {
|
|
|
70
84
|
name: "X-Client-Version",
|
|
71
85
|
description: "The version of the client. If the client is making bad requests this helps " +
|
|
72
86
|
"identify which version of the client is making the bad requests",
|
|
73
|
-
required:
|
|
87
|
+
required: required !== undefined
|
|
88
|
+
? required
|
|
89
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
74
90
|
schema: {
|
|
75
91
|
example: "0.3.6",
|
|
76
92
|
},
|
|
@@ -78,7 +94,9 @@ function ApiMetaHeaders() {
|
|
|
78
94
|
(0, swagger_1.ApiHeader)({
|
|
79
95
|
name: "X-Client-Variant",
|
|
80
96
|
description: "Usually an environment or build type such as dev, test, or prod",
|
|
81
|
-
required:
|
|
97
|
+
required: required !== undefined
|
|
98
|
+
? required
|
|
99
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
82
100
|
schema: {
|
|
83
101
|
example: "dev",
|
|
84
102
|
},
|
|
@@ -86,7 +104,9 @@ function ApiMetaHeaders() {
|
|
|
86
104
|
(0, swagger_1.ApiHeader)({
|
|
87
105
|
name: "X-Created",
|
|
88
106
|
description: "The time the client initiated the http request",
|
|
89
|
-
required:
|
|
107
|
+
required: required !== undefined
|
|
108
|
+
? required
|
|
109
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
90
110
|
schema: {
|
|
91
111
|
example: new Date().toISOString(),
|
|
92
112
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api-meta-headers.js","sourceRoot":"","sources":["../../../src/decorator/api-meta-headers.ts"],"names":[],"mappings":";;AAIA,
|
|
1
|
+
{"version":3,"file":"api-meta-headers.js","sourceRoot":"","sources":["../../../src/decorator/api-meta-headers.ts"],"names":[],"mappings":";;AAIA,wCA+IC;AAnJD,6CAA4C;AAC5C,2CAAiD;AACjD,+BAAoC;AAEpC,SAAgB,cAAc,CAAC,WAAoB,SAAS;IAC1D,OAAO,IAAA,wBAAe,EACpB,GAAG;QACD,IAAA,mBAAS,EAAC;YACR,IAAI,EAAE,kBAAkB;YACxB,QAAQ,EACN,QAAQ,KAAK,SAAS;gBACpB,CAAC,CAAC,QAAQ;gBACV,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,uBAAuB,KAAK,OAAO;YACrD,MAAM,EAAE;gBACN,OAAO,EAAE,IAAA,SAAM,GAAE;aAClB;SACF,CAAC;QACF,IAAA,mBAAS,EAAC;YACR,IAAI,EAAE,iBAAiB;YACvB,QAAQ,EACN,QAAQ,KAAK,SAAS;gBACpB,CAAC,CAAC,QAAQ;gBACV,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,uBAAuB,KAAK,OAAO;YACrD,MAAM,EAAE;gBACN,OAAO,EAAE,OAAO;aACjB;SACF,CAAC;QACF,IAAA,mBAAS,EAAC;YACR,IAAI,EAAE,WAAW;YACjB,WAAW,EACT,+HAA+H;YACjI,QAAQ,EACN,QAAQ,KAAK,SAAS;gBACpB,CAAC,CAAC,QAAQ;gBACV,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,uBAAuB,KAAK,OAAO;YACrD,MAAM,EAAE;gBACN,OAAO,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;aAClC;SACF,CAAC;QACF,IAAA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N,QAAQ,KAAK,SAAS;gBACpB,CAAC,CAAC,QAAQ;gBACV,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,uBAAuB,KAAK,OAAO;YACrD,MAAM,EAAE;gBACN,OAAO,EAAE,OAAO;aACjB;SACF,CAAC;QACF,IAAA,mBAAS,EAAC;YACR,IAAI,EAAE,kBAAkB;YACxB,WAAW,EACT,iEAAiE;YACnE,QAAQ,EACN,QAAQ,KAAK,SAAS;gBACpB,CAAC,CAAC,QAAQ;gBACV,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,uBAAuB,KAAK,OAAO;YACrD,MAAM,EAAE;gBACN,OAAO,EAAE,KAAK;aACf;SACF,CAAC;QACF,IAAA,mBAAS,EAAC;YACR,IAAI,EAAE,WAAW;YACjB,WAAW,EAAE,gDAAgD;YAC7D,QAAQ,EACN,QAAQ,KAAK,SAAS;gBACpB,CAAC,CAAC,QAAQ;gBACV,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,uBAAuB,KAAK,OAAO;YACrD,MAAM,EAAE;gBACN,OAAO,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;aAClC;SACF,CAAC;QACF,IAAA,mBAAS,EAAC;YACR,IAAI,EAAE,OAAO;YACb,WAAW,EAAE,gDAAgD;YAC7D,QAAQ,EAAE,KAAK;YACf,MAAM,EAAE;gBACN,OAAO,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;aAClC;SACF,CAAC;QACF,IAAA,mBAAS,EAAC;YACR,IAAI,EAAE,YAAY;YAClB,WAAW,EACT,yEAAyE;YAC3E,QAAQ,EAAE,KAAK;YACf,MAAM,EAAE;gBACN,OAAO,EAAE,iBAAiB;aAC3B;SACF,CAAC;KACH,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { CallHandler, ExecutionContext, NestInterceptor } from "@nestjs/common";
|
|
2
|
+
import { Observable } from "rxjs";
|
|
3
|
+
export declare class StripMetaResponseInterceptor implements NestInterceptor {
|
|
4
|
+
intercept(context: ExecutionContext, next: CallHandler): Observable<any>;
|
|
5
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StripMetaResponseInterceptor = void 0;
|
|
4
|
+
const rxjs_1 = require("rxjs");
|
|
5
|
+
class StripMetaResponseInterceptor {
|
|
6
|
+
intercept(context, next) {
|
|
7
|
+
var _a;
|
|
8
|
+
if (context.getType() !== "http") {
|
|
9
|
+
return next.handle();
|
|
10
|
+
}
|
|
11
|
+
const request = context.switchToHttp().getRequest();
|
|
12
|
+
const headerValue = (_a = request === null || request === void 0 ? void 0 : request.headers) === null || _a === void 0 ? void 0 : _a["x-include-meta"];
|
|
13
|
+
const headerString = Array.isArray(headerValue)
|
|
14
|
+
? headerValue[0]
|
|
15
|
+
: headerValue;
|
|
16
|
+
const headerWantsMeta = (headerString !== null && headerString !== void 0 ? headerString : "").toString().toLowerCase() === "true";
|
|
17
|
+
const envSaysStripByDefault = process.env.X_INCLUDE_META_DEFAULT === "false";
|
|
18
|
+
const headerIsUnset = headerString === undefined || headerString === "false";
|
|
19
|
+
const shouldStrip = !headerWantsMeta && envSaysStripByDefault && headerIsUnset;
|
|
20
|
+
if (!shouldStrip) {
|
|
21
|
+
return next.handle();
|
|
22
|
+
}
|
|
23
|
+
return next.handle().pipe((0, rxjs_1.map)((value) => {
|
|
24
|
+
if (value &&
|
|
25
|
+
typeof value === "object" &&
|
|
26
|
+
"data" in value) {
|
|
27
|
+
return value.data;
|
|
28
|
+
}
|
|
29
|
+
return value;
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.StripMetaResponseInterceptor = StripMetaResponseInterceptor;
|
|
34
|
+
//# sourceMappingURL=strip-meta.response.interceptor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strip-meta.response.interceptor.js","s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,CAAC;YACD,OAAO,KAAK,CAAC;QACf,CAAC,CAAC,CACH,CAAC;IACJ,CAAC;CACF;AAjDD,oEAiDC"}
|
package/package.json
CHANGED
|
@@ -2,19 +2,25 @@ import { ApiHeader } from "@nestjs/swagger";
|
|
|
2
2
|
import { applyDecorators } from "@nestjs/common";
|
|
3
3
|
import { v4 as uuidV4 } from "uuid";
|
|
4
4
|
|
|
5
|
-
export function ApiMetaHeaders() {
|
|
5
|
+
export function ApiMetaHeaders(required: boolean = undefined) {
|
|
6
6
|
return applyDecorators(
|
|
7
7
|
...[
|
|
8
8
|
ApiHeader({
|
|
9
9
|
name: "X-Correlation-Id",
|
|
10
|
-
required:
|
|
10
|
+
required:
|
|
11
|
+
required !== undefined
|
|
12
|
+
? required
|
|
13
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
11
14
|
schema: {
|
|
12
15
|
example: uuidV4(),
|
|
13
16
|
},
|
|
14
17
|
}),
|
|
15
18
|
ApiHeader({
|
|
16
19
|
name: "Accept-Language",
|
|
17
|
-
required:
|
|
20
|
+
required:
|
|
21
|
+
required !== undefined
|
|
22
|
+
? required
|
|
23
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
18
24
|
schema: {
|
|
19
25
|
example: "en-US",
|
|
20
26
|
},
|
|
@@ -23,7 +29,10 @@ export function ApiMetaHeaders() {
|
|
|
23
29
|
name: "X-Started",
|
|
24
30
|
description:
|
|
25
31
|
"The time that the very first user initiated or time initiated event that eventually lead to this api being called was created",
|
|
26
|
-
required:
|
|
32
|
+
required:
|
|
33
|
+
required !== undefined
|
|
34
|
+
? required
|
|
35
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
27
36
|
schema: {
|
|
28
37
|
example: new Date().toISOString(),
|
|
29
38
|
},
|
|
@@ -32,7 +41,10 @@ export function ApiMetaHeaders() {
|
|
|
32
41
|
name: "X-Context-Category",
|
|
33
42
|
description:
|
|
34
43
|
"A category for the context of the request. For example 'test' or 'performance test'",
|
|
35
|
-
required:
|
|
44
|
+
required:
|
|
45
|
+
required !== undefined
|
|
46
|
+
? required
|
|
47
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
36
48
|
schema: {
|
|
37
49
|
example: "default",
|
|
38
50
|
},
|
|
@@ -41,7 +53,10 @@ export function ApiMetaHeaders() {
|
|
|
41
53
|
name: "X-Context-Id",
|
|
42
54
|
description:
|
|
43
55
|
"A unique context identifier used for correlating logs and metrics usually for performance or experimental testing",
|
|
44
|
-
required:
|
|
56
|
+
required:
|
|
57
|
+
required !== undefined
|
|
58
|
+
? required
|
|
59
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
45
60
|
schema: {
|
|
46
61
|
example: "none",
|
|
47
62
|
},
|
|
@@ -50,7 +65,10 @@ export function ApiMetaHeaders() {
|
|
|
50
65
|
name: "X-Client-Id",
|
|
51
66
|
description:
|
|
52
67
|
"A unique identifier for the client to help identify exactly which application or third party is making the requests",
|
|
53
|
-
required:
|
|
68
|
+
required:
|
|
69
|
+
required !== undefined
|
|
70
|
+
? required
|
|
71
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
54
72
|
schema: {},
|
|
55
73
|
examples: {
|
|
56
74
|
UUIDV4: {
|
|
@@ -64,7 +82,10 @@ export function ApiMetaHeaders() {
|
|
|
64
82
|
ApiHeader({
|
|
65
83
|
name: "X-Client-Name",
|
|
66
84
|
description: "The name of the client",
|
|
67
|
-
required:
|
|
85
|
+
required:
|
|
86
|
+
required !== undefined
|
|
87
|
+
? required
|
|
88
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
68
89
|
schema: {
|
|
69
90
|
example: "swagger",
|
|
70
91
|
},
|
|
@@ -74,7 +95,10 @@ export function ApiMetaHeaders() {
|
|
|
74
95
|
description:
|
|
75
96
|
"The version of the client. If the client is making bad requests this helps " +
|
|
76
97
|
"identify which version of the client is making the bad requests",
|
|
77
|
-
required:
|
|
98
|
+
required:
|
|
99
|
+
required !== undefined
|
|
100
|
+
? required
|
|
101
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
78
102
|
schema: {
|
|
79
103
|
example: "0.3.6",
|
|
80
104
|
},
|
|
@@ -83,7 +107,10 @@ export function ApiMetaHeaders() {
|
|
|
83
107
|
name: "X-Client-Variant",
|
|
84
108
|
description:
|
|
85
109
|
"Usually an environment or build type such as dev, test, or prod",
|
|
86
|
-
required:
|
|
110
|
+
required:
|
|
111
|
+
required !== undefined
|
|
112
|
+
? required
|
|
113
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
87
114
|
schema: {
|
|
88
115
|
example: "dev",
|
|
89
116
|
},
|
|
@@ -91,7 +118,10 @@ export function ApiMetaHeaders() {
|
|
|
91
118
|
ApiHeader({
|
|
92
119
|
name: "X-Created",
|
|
93
120
|
description: "The time the client initiated the http request",
|
|
94
|
-
required:
|
|
121
|
+
required:
|
|
122
|
+
required !== undefined
|
|
123
|
+
? required
|
|
124
|
+
: process.env.X_META_HEADERS_REQUIRED !== "false",
|
|
95
125
|
schema: {
|
|
96
126
|
example: new Date().toISOString(),
|
|
97
127
|
},
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { CallHandler, ExecutionContext, NestInterceptor } from "@nestjs/common";
|
|
2
|
+
import { map, Observable } from "rxjs";
|
|
3
|
+
|
|
4
|
+
export class StripMetaResponseInterceptor implements NestInterceptor {
|
|
5
|
+
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
|
|
6
|
+
// Only applicable to HTTP requests (headers)
|
|
7
|
+
if (context.getType() !== "http") {
|
|
8
|
+
return next.handle();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const request = context.switchToHttp().getRequest();
|
|
12
|
+
const headerValue = request?.headers?.["x-include-meta"] as
|
|
13
|
+
| string
|
|
14
|
+
| string[]
|
|
15
|
+
| undefined;
|
|
16
|
+
|
|
17
|
+
// Normalize header to a single string (or undefined)
|
|
18
|
+
const headerString = Array.isArray(headerValue)
|
|
19
|
+
? headerValue[0]
|
|
20
|
+
: headerValue;
|
|
21
|
+
|
|
22
|
+
// If header explicitly says "true", always include meta
|
|
23
|
+
const headerWantsMeta =
|
|
24
|
+
(headerString ?? "").toString().toLowerCase() === "true";
|
|
25
|
+
|
|
26
|
+
// Only strip when env is explicitly false AND header is not set at all
|
|
27
|
+
const envSaysStripByDefault =
|
|
28
|
+
process.env.X_INCLUDE_META_DEFAULT === "false";
|
|
29
|
+
const headerIsUnset =
|
|
30
|
+
headerString === undefined || headerString === "false";
|
|
31
|
+
|
|
32
|
+
const shouldStrip =
|
|
33
|
+
!headerWantsMeta && envSaysStripByDefault && headerIsUnset;
|
|
34
|
+
|
|
35
|
+
if (!shouldStrip) {
|
|
36
|
+
return next.handle();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Strip to value.data when present; otherwise leave as-is
|
|
40
|
+
return next.handle().pipe(
|
|
41
|
+
map((value: unknown) => {
|
|
42
|
+
if (
|
|
43
|
+
value &&
|
|
44
|
+
typeof value === "object" &&
|
|
45
|
+
"data" in (value as Record<string, unknown>)
|
|
46
|
+
) {
|
|
47
|
+
return (value as { data: unknown }).data;
|
|
48
|
+
}
|
|
49
|
+
return value;
|
|
50
|
+
})
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
}
|