@nathapp/nestjs-idempotency 3.3.0
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/.eslintrc.js +8 -0
- package/.nax/features/nestjs-idempotency/.nax-acceptance.test.ts +766 -0
- package/.nax/features/nestjs-idempotency/.nax-suggested.test.ts +439 -0
- package/.nax/test-debug.ts +22 -0
- package/.turbo/turbo-build.log +4 -0
- package/CHANGELOG.md +15 -0
- package/README.md +54 -0
- package/dist/constant/idempotency.constants.d.ts +8 -0
- package/dist/constant/idempotency.constants.js +12 -0
- package/dist/constant/idempotency.constants.js.map +1 -0
- package/dist/constant/index.d.ts +1 -0
- package/dist/constant/index.js +18 -0
- package/dist/constant/index.js.map +1 -0
- package/dist/decorator/idempotent.decorator.d.ts +1 -0
- package/dist/decorator/idempotent.decorator.js +8 -0
- package/dist/decorator/idempotent.decorator.js.map +1 -0
- package/dist/exception/idempotency-exception-code.enum.d.ts +5 -0
- package/dist/exception/idempotency-exception-code.enum.js +10 -0
- package/dist/exception/idempotency-exception-code.enum.js.map +1 -0
- package/dist/exception/idempotency.exceptions.d.ts +7 -0
- package/dist/exception/idempotency.exceptions.js +19 -0
- package/dist/exception/idempotency.exceptions.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/interceptor/idempotency.interceptor.d.ts +16 -0
- package/dist/interceptor/idempotency.interceptor.js +127 -0
- package/dist/interceptor/idempotency.interceptor.js.map +1 -0
- package/dist/interface/idempotency-module-options.interface.d.ts +10 -0
- package/dist/interface/idempotency-module-options.interface.js +3 -0
- package/dist/interface/idempotency-module-options.interface.js.map +1 -0
- package/dist/interface/idempotency-record.interface.d.ts +12 -0
- package/dist/interface/idempotency-record.interface.js +3 -0
- package/dist/interface/idempotency-record.interface.js.map +1 -0
- package/dist/interface/idempotency-store.interface.d.ts +7 -0
- package/dist/interface/idempotency-store.interface.js +3 -0
- package/dist/interface/idempotency-store.interface.js.map +1 -0
- package/dist/module/idempotency.module.d.ts +10 -0
- package/dist/module/idempotency.module.js +61 -0
- package/dist/module/idempotency.module.js.map +1 -0
- package/dist/provider/default-principal-extractor.d.ts +2 -0
- package/dist/provider/default-principal-extractor.js +11 -0
- package/dist/provider/default-principal-extractor.js.map +1 -0
- package/dist/provider/fingerprint.d.ts +1 -0
- package/dist/provider/fingerprint.js +19 -0
- package/dist/provider/fingerprint.js.map +1 -0
- package/dist/provider/in-memory-idempotency-store.d.ts +15 -0
- package/dist/provider/in-memory-idempotency-store.js +48 -0
- package/dist/provider/in-memory-idempotency-store.js.map +1 -0
- package/dist/provider/resolve-idempotency-options.d.ts +13 -0
- package/dist/provider/resolve-idempotency-options.js +18 -0
- package/dist/provider/resolve-idempotency-options.js.map +1 -0
- package/lib/constant/idempotency.constants.ts +8 -0
- package/lib/constant/index.ts +1 -0
- package/lib/decorator/idempotent.decorator.ts +4 -0
- package/lib/exception/idempotency-exception-code.enum.ts +5 -0
- package/lib/exception/idempotency.exceptions.ts +15 -0
- package/lib/index.ts +13 -0
- package/lib/interceptor/idempotency.interceptor.ts +187 -0
- package/lib/interface/idempotency-module-options.interface.ts +21 -0
- package/lib/interface/idempotency-record.interface.ts +14 -0
- package/lib/interface/idempotency-store.interface.ts +8 -0
- package/lib/module/idempotency.module.ts +61 -0
- package/lib/provider/default-principal-extractor.ts +17 -0
- package/lib/provider/fingerprint.ts +17 -0
- package/lib/provider/in-memory-idempotency-store.ts +53 -0
- package/lib/provider/resolve-idempotency-options.ts +32 -0
- package/package.json +54 -0
- package/test/barrel/index.spec.ts +40 -0
- package/test/constant/idempotency.constants.spec.ts +10 -0
- package/test/decorator/idempotent.decorator.spec.ts +20 -0
- package/test/e2e/express.e2e.spec.ts +219 -0
- package/test/e2e/fastify.e2e.spec.ts +64 -0
- package/test/exception/idempotency.exceptions.spec.ts +24 -0
- package/test/interceptor/idempotency.interceptor.spec.ts +739 -0
- package/test/jest-nax-setup-test.js +21 -0
- package/test/jest-nax-setup.js +92 -0
- package/test/jest-nax.json +30 -0
- package/test/jest-test.json +28 -0
- package/test/module/idempotency.module.spec.ts +87 -0
- package/test/provider/fingerprint.spec.ts +27 -0
- package/test/provider/in-memory-idempotency-store.spec.ts +212 -0
- package/test/provider/resolve-idempotency-options.spec.ts +42 -0
- package/tsconfig.json +9 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './constant/idempotency.constants';
|
|
2
|
+
export * from './decorator/idempotent.decorator';
|
|
3
|
+
export * from './exception/idempotency-exception-code.enum';
|
|
4
|
+
export * from './exception/idempotency.exceptions';
|
|
5
|
+
export * from './interceptor/idempotency.interceptor';
|
|
6
|
+
export * from './interface/idempotency-module-options.interface';
|
|
7
|
+
export * from './interface/idempotency-record.interface';
|
|
8
|
+
export * from './interface/idempotency-store.interface';
|
|
9
|
+
export * from './module/idempotency.module';
|
|
10
|
+
export * from './provider/default-principal-extractor';
|
|
11
|
+
export * from './provider/fingerprint';
|
|
12
|
+
export * from './provider/in-memory-idempotency-store';
|
|
13
|
+
export * from './provider/resolve-idempotency-options';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
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("./constant/idempotency.constants"), exports);
|
|
18
|
+
__exportStar(require("./decorator/idempotent.decorator"), exports);
|
|
19
|
+
__exportStar(require("./exception/idempotency-exception-code.enum"), exports);
|
|
20
|
+
__exportStar(require("./exception/idempotency.exceptions"), exports);
|
|
21
|
+
__exportStar(require("./interceptor/idempotency.interceptor"), exports);
|
|
22
|
+
__exportStar(require("./interface/idempotency-module-options.interface"), exports);
|
|
23
|
+
__exportStar(require("./interface/idempotency-record.interface"), exports);
|
|
24
|
+
__exportStar(require("./interface/idempotency-store.interface"), exports);
|
|
25
|
+
__exportStar(require("./module/idempotency.module"), exports);
|
|
26
|
+
__exportStar(require("./provider/default-principal-extractor"), exports);
|
|
27
|
+
__exportStar(require("./provider/fingerprint"), exports);
|
|
28
|
+
__exportStar(require("./provider/in-memory-idempotency-store"), exports);
|
|
29
|
+
__exportStar(require("./provider/resolve-idempotency-options"), exports);
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mEAAiD;AACjD,mEAAiD;AACjD,8EAA4D;AAC5D,qEAAmD;AACnD,wEAAsD;AACtD,mFAAiE;AACjE,2EAAyD;AACzD,0EAAwD;AACxD,8DAA4C;AAC5C,yEAAuD;AACvD,yDAAuC;AACvC,yEAAuD;AACvD,yEAAuD"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import { Reflector, HttpAdapterHost } from '@nestjs/core';
|
|
4
|
+
import { IIdempotencyStore } from '../interface/idempotency-store.interface';
|
|
5
|
+
import { InterceptorOptions } from '../provider/resolve-idempotency-options';
|
|
6
|
+
export declare class IdempotencyInterceptor implements NestInterceptor {
|
|
7
|
+
private readonly _reflector;
|
|
8
|
+
private readonly _store;
|
|
9
|
+
private readonly _httpAdapterHost;
|
|
10
|
+
private readonly _options;
|
|
11
|
+
private readonly _logger;
|
|
12
|
+
constructor(_reflector: Reflector, _store: IIdempotencyStore, _httpAdapterHost: HttpAdapterHost, _options: InterceptorOptions);
|
|
13
|
+
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown>;
|
|
14
|
+
private _executeAndStore;
|
|
15
|
+
private _replay;
|
|
16
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
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 __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var IdempotencyInterceptor_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.IdempotencyInterceptor = void 0;
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const rxjs_1 = require("rxjs");
|
|
19
|
+
const operators_1 = require("rxjs/operators");
|
|
20
|
+
const crypto_1 = require("crypto");
|
|
21
|
+
const core_1 = require("@nestjs/core");
|
|
22
|
+
const nestjs_common_1 = require("@nathapp/nestjs-common");
|
|
23
|
+
const idempotency_constants_1 = require("../constant/idempotency.constants");
|
|
24
|
+
const fingerprint_1 = require("../provider/fingerprint");
|
|
25
|
+
const idempotency_exceptions_1 = require("../exception/idempotency.exceptions");
|
|
26
|
+
const idempotency_exceptions_2 = require("../exception/idempotency.exceptions");
|
|
27
|
+
const idempotency_exception_code_enum_1 = require("../exception/idempotency-exception-code.enum");
|
|
28
|
+
let IdempotencyInterceptor = IdempotencyInterceptor_1 = class IdempotencyInterceptor {
|
|
29
|
+
constructor(_reflector, _store, _httpAdapterHost, _options) {
|
|
30
|
+
this._reflector = _reflector;
|
|
31
|
+
this._store = _store;
|
|
32
|
+
this._httpAdapterHost = _httpAdapterHost;
|
|
33
|
+
this._options = _options;
|
|
34
|
+
this._logger = new common_1.Logger(IdempotencyInterceptor_1.name);
|
|
35
|
+
}
|
|
36
|
+
intercept(context, next) {
|
|
37
|
+
var _a, _b;
|
|
38
|
+
const hasMetadata = this._reflector.get(idempotency_constants_1.IDEMPOTENT_METADATA_KEY, context.getHandler());
|
|
39
|
+
if (!hasMetadata) {
|
|
40
|
+
return next.handle();
|
|
41
|
+
}
|
|
42
|
+
if (context.getType() !== 'http') {
|
|
43
|
+
return next.handle();
|
|
44
|
+
}
|
|
45
|
+
const response = context.switchToHttp().getResponse();
|
|
46
|
+
if (response.statusCode === common_1.HttpStatus.CREATED) {
|
|
47
|
+
response.statusCode = common_1.HttpStatus.OK;
|
|
48
|
+
}
|
|
49
|
+
const request = context.switchToHttp().getRequest();
|
|
50
|
+
const headerName = this._options.headerName.toLowerCase();
|
|
51
|
+
const rawKey = request.headers[headerName];
|
|
52
|
+
const key = Array.isArray(rawKey) ? rawKey[0] : rawKey;
|
|
53
|
+
if (!key) {
|
|
54
|
+
if (this._options.requireKey) {
|
|
55
|
+
throw new nestjs_common_1.AppException(idempotency_exception_code_enum_1.IdempotencyExceptionCode.MISSING_REQUIRED_KEY, undefined, undefined, common_1.HttpStatus.BAD_REQUEST);
|
|
56
|
+
}
|
|
57
|
+
return next.handle();
|
|
58
|
+
}
|
|
59
|
+
const routePath = (_b = (_a = request.path) !== null && _a !== void 0 ? _a : request.url) !== null && _b !== void 0 ? _b : '';
|
|
60
|
+
const principal = this._options.principalExtractor(request);
|
|
61
|
+
if (!principal) {
|
|
62
|
+
this._logger.warn(`Idempotency key for ${request.method} ${routePath} is unscoped: principalExtractor ` +
|
|
63
|
+
`returned no principal. This is expected for public routes, but may indicate a ` +
|
|
64
|
+
`misconfigured principalExtractor or a missing auth guard.`);
|
|
65
|
+
}
|
|
66
|
+
const keyParts = principal
|
|
67
|
+
? `${request.method}\n${routePath}\n${principal}\n${key}`
|
|
68
|
+
: `${request.method}\n${routePath}\n${key}`;
|
|
69
|
+
const storageKey = (0, crypto_1.createHash)('sha256').update(keyParts).digest('hex');
|
|
70
|
+
const fingerprint = (0, fingerprint_1.computeFingerprint)(request.body);
|
|
71
|
+
return (0, rxjs_1.from)(this._store.putIfAbsent(storageKey, {
|
|
72
|
+
state: 'pending',
|
|
73
|
+
fingerprint,
|
|
74
|
+
createdAt: Date.now(),
|
|
75
|
+
})).pipe((0, operators_1.switchMap)(existing => {
|
|
76
|
+
if (existing === undefined) {
|
|
77
|
+
return this._executeAndStore(context, next, storageKey);
|
|
78
|
+
}
|
|
79
|
+
if (existing.fingerprint !== fingerprint) {
|
|
80
|
+
throw new idempotency_exceptions_2.IdempotencyKeyReuseException();
|
|
81
|
+
}
|
|
82
|
+
if (existing.state === 'pending') {
|
|
83
|
+
throw new idempotency_exceptions_1.IdempotencyConflictException();
|
|
84
|
+
}
|
|
85
|
+
if (!existing.response) {
|
|
86
|
+
throw new Error('Idempotency store returned a completed record with no cached response');
|
|
87
|
+
}
|
|
88
|
+
return this._replay(context, existing.response);
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
_executeAndStore(context, next, storageKey) {
|
|
92
|
+
const response = context.switchToHttp().getResponse();
|
|
93
|
+
return next.handle().pipe((0, operators_1.switchMap)(body => {
|
|
94
|
+
const status = response.statusCode;
|
|
95
|
+
if (status >= 200 && status < 300) {
|
|
96
|
+
const allHeaders = response.getHeaders();
|
|
97
|
+
const headers = this._options.replayHeaders.reduce((acc, h) => {
|
|
98
|
+
const v = allHeaders[h.toLowerCase()];
|
|
99
|
+
if (v !== undefined)
|
|
100
|
+
acc[h.toLowerCase()] = v;
|
|
101
|
+
return acc;
|
|
102
|
+
}, {});
|
|
103
|
+
return (0, rxjs_1.from)(this._store.complete(storageKey, { status, body, headers })).pipe((0, operators_1.map)(() => body));
|
|
104
|
+
}
|
|
105
|
+
return (0, rxjs_1.from)(this._store.remove(storageKey)).pipe((0, operators_1.map)(() => body));
|
|
106
|
+
}), (0, operators_1.catchError)(err => (0, rxjs_1.from)(this._store.remove(storageKey).catch(() => undefined)).pipe((0, operators_1.switchMap)(() => (0, rxjs_1.throwError)(() => err)))));
|
|
107
|
+
}
|
|
108
|
+
_replay(context, storedResponse) {
|
|
109
|
+
const rawResponse = context.switchToHttp().getResponse();
|
|
110
|
+
const httpAdapter = this._httpAdapterHost.httpAdapter;
|
|
111
|
+
for (const [header, value] of Object.entries(storedResponse.headers)) {
|
|
112
|
+
if (this._options.replayHeaders.some(h => h.toLowerCase() === header.toLowerCase())) {
|
|
113
|
+
httpAdapter.setHeader(rawResponse, header, value);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
httpAdapter.reply(rawResponse, storedResponse.body, storedResponse.status);
|
|
117
|
+
return (0, rxjs_1.of)(null);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
exports.IdempotencyInterceptor = IdempotencyInterceptor;
|
|
121
|
+
exports.IdempotencyInterceptor = IdempotencyInterceptor = IdempotencyInterceptor_1 = __decorate([
|
|
122
|
+
(0, common_1.Injectable)(),
|
|
123
|
+
__param(1, (0, common_1.Inject)(idempotency_constants_1.IDEMPOTENCY_STORE)),
|
|
124
|
+
__param(3, (0, common_1.Inject)(idempotency_constants_1.IDEMPOTENCY_INTERCEPTOR_OPTIONS)),
|
|
125
|
+
__metadata("design:paramtypes", [core_1.Reflector, Object, core_1.HttpAdapterHost, Object])
|
|
126
|
+
], IdempotencyInterceptor);
|
|
127
|
+
//# sourceMappingURL=idempotency.interceptor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"idempotency.interceptor.js","sourceRoot":"","sources":["../../lib/interceptor/idempotency.interceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAwH;AACxH,+BAAwD;AACxD,8CAA4D;AAC5D,mCAAoC;AACpC,uCAA0D;AAC1D,0DAAsD;AAGtD,6EAI2C;AAC3C,yDAA6D;AAC7D,gFAAmF;AACnF,gFAAmF;AACnF,kGAAwF;AAGjF,IAAM,sBAAsB,8BAA5B,MAAM,sBAAsB;IAGjC,YACmB,UAAqB,EACX,MAA0C,EACpD,gBAAiC,EACT,QAA6C;QAHrE,eAAU,GAAV,UAAU,CAAW;QACM,WAAM,GAAN,MAAM,CAAmB;QACpD,qBAAgB,GAAhB,gBAAgB,CAAiB;QACQ,aAAQ,GAAR,QAAQ,CAAoB;QANvE,YAAO,GAAG,IAAI,eAAM,CAAC,wBAAsB,CAAC,IAAI,CAAC,CAAC;IAOhE,CAAC;IAEJ,SAAS,CAAC,OAAyB,EAAE,IAAiB;;QACpD,MAAM,WAAW,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAU,+CAAuB,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC;QAChG,IAAI,CAAC,WAAW,EAAE,CAAC;YACjB,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;QACvB,CAAC;QAKD,IAAI,OAAO,CAAC,OAAO,EAAE,KAAK,MAAM,EAAE,CAAC;YACjC,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;QACvB,CAA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AC,SAAS,CAAC,CAAC,CAAC,IAAI,CAC9D,IAAA,qBAAS,EAAC,GAAG,EAAE,CAAC,IAAA,iBAAU,EAAC,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CACvC,CACF,CACF,CAAC;IACJ,CAAC;IAEO,OAAO,CACb,OAAyB,EACzB,cAA8G;QAE9G,MAAM,WAAW,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,WAAW,EAAE,CAAC;QACzD,MAAM,WAAW,GAAG,IAAI,CAAC,gBAAgB,CAAC,WAAW,CAAC;QAKtD,KAAK,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE,CAAC;YACrE,IAAI,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,KAAK,MAAM,CAAC,WAAW,EAAE,CAAC,EAAE,CAAC;gBACpF,WAAW,CAAC,SAAS,CAAC,WAAW,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;YACpD,CAAC;QACH,CAAC;QACD,WAAW,CAAC,KAAK,CAAC,WAAW,EAAE,cAAc,CAAC,IAAI,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC;QAE3E,OAAO,IAAA,SAAE,EAAC,IAAI,CAAC,CAAC;IAClB,CAAC;CACF,CAAA;AAvKY,wDAAsB;iCAAtB,sBAAsB;IADlC,IAAA,mBAAU,GAAE;IAMR,WAAA,IAAA,eAAM,EAAC,yCAAiB,CAAC,CAAA;IAEzB,WAAA,IAAA,eAAM,EAAC,uDAA+B,CAAC,CAAA;qCAHX,gBAAS,UAEH,sBAAe;GANzC,sBAAsB,CAuKlC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { IIdempotencyStore } from './idempotency-store.interface';
|
|
2
|
+
import { PrincipalExtractor } from '../provider/default-principal-extractor';
|
|
3
|
+
export interface IdempotencyModuleOptions {
|
|
4
|
+
store?: IIdempotencyStore;
|
|
5
|
+
headerName?: string;
|
|
6
|
+
ttlMs?: number;
|
|
7
|
+
requireKey?: boolean;
|
|
8
|
+
replayHeaders?: string[];
|
|
9
|
+
principalExtractor?: PrincipalExtractor;
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"idempotency-module-options.interface.js","sourceRoot":"","sources":["../../lib/interface/idempotency-module-options.interface.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type IdempotencyState = 'pending' | 'completed';
|
|
2
|
+
export interface StoredResponse {
|
|
3
|
+
status: number;
|
|
4
|
+
body: unknown;
|
|
5
|
+
headers: Record<string, string>;
|
|
6
|
+
}
|
|
7
|
+
export interface IdempotencyRecord {
|
|
8
|
+
state: IdempotencyState;
|
|
9
|
+
fingerprint: string;
|
|
10
|
+
createdAt: number;
|
|
11
|
+
response?: StoredResponse;
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"idempotency-record.interface.js","sourceRoot":"","sources":["../../lib/interface/idempotency-record.interface.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { IdempotencyRecord, StoredResponse } from './idempotency-record.interface';
|
|
2
|
+
export interface IIdempotencyStore {
|
|
3
|
+
putIfAbsent(key: string, record: IdempotencyRecord): Promise<IdempotencyRecord | undefined>;
|
|
4
|
+
complete(key: string, response: StoredResponse): Promise<void>;
|
|
5
|
+
remove(key: string): Promise<void>;
|
|
6
|
+
get(key: string): Promise<IdempotencyRecord | undefined>;
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"idempotency-store.interface.js","sourceRoot":"","sources":["../../lib/interface/idempotency-store.interface.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { ConfigurableModuleClass } from '@nathapp/nestjs-common';
|
|
3
|
+
import { IdempotencyModuleOptions } from '../interface/idempotency-module-options.interface';
|
|
4
|
+
type IdempotencyRegisterAsyncOptions = Parameters<ReturnType<typeof ConfigurableModuleClass<IdempotencyModuleOptions>>['registerAsync']>[0];
|
|
5
|
+
declare const IdempotencyModule_base: import("@nathapp/nestjs-common").ConfigurableModuleCls<IdempotencyModuleOptions, "register", "createOptions", {}>;
|
|
6
|
+
export declare class IdempotencyModule extends IdempotencyModule_base {
|
|
7
|
+
static register(options?: IdempotencyModuleOptions): DynamicModule;
|
|
8
|
+
static registerAsync(options: IdempotencyRegisterAsyncOptions): DynamicModule;
|
|
9
|
+
}
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
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 IdempotencyModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.IdempotencyModule = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const nestjs_common_1 = require("@nathapp/nestjs-common");
|
|
13
|
+
const idempotency_interceptor_1 = require("../interceptor/idempotency.interceptor");
|
|
14
|
+
const idempotency_constants_1 = require("../constant/idempotency.constants");
|
|
15
|
+
const resolve_idempotency_options_1 = require("../provider/resolve-idempotency-options");
|
|
16
|
+
const default_principal_extractor_1 = require("../provider/default-principal-extractor");
|
|
17
|
+
let IdempotencyModule = IdempotencyModule_1 = class IdempotencyModule extends (0, nestjs_common_1.ConfigurableModuleClass)(idempotency_constants_1.IDEMPOTENCY_MODULE_OPTIONS) {
|
|
18
|
+
static register(options) {
|
|
19
|
+
return {
|
|
20
|
+
...super.register(options !== null && options !== void 0 ? options : {}),
|
|
21
|
+
module: IdempotencyModule_1,
|
|
22
|
+
global: true,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
static registerAsync(options) {
|
|
26
|
+
return {
|
|
27
|
+
...super.registerAsync(options),
|
|
28
|
+
module: IdempotencyModule_1,
|
|
29
|
+
global: true,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
exports.IdempotencyModule = IdempotencyModule;
|
|
34
|
+
exports.IdempotencyModule = IdempotencyModule = IdempotencyModule_1 = __decorate([
|
|
35
|
+
(0, common_1.Module)({
|
|
36
|
+
providers: [
|
|
37
|
+
{
|
|
38
|
+
provide: idempotency_constants_1.IDEMPOTENCY_STORE,
|
|
39
|
+
useFactory: (options) => (0, resolve_idempotency_options_1.resolveIdempotencyOptions)(options).store,
|
|
40
|
+
inject: [idempotency_constants_1.IDEMPOTENCY_MODULE_OPTIONS],
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
provide: idempotency_constants_1.IDEMPOTENCY_INTERCEPTOR_OPTIONS,
|
|
44
|
+
useFactory: (options) => {
|
|
45
|
+
var _a, _b, _c, _d, _e;
|
|
46
|
+
return ({
|
|
47
|
+
headerName: (_a = options === null || options === void 0 ? void 0 : options.headerName) !== null && _a !== void 0 ? _a : idempotency_constants_1.DEFAULT_HEADER_NAME,
|
|
48
|
+
ttlMs: (_b = options === null || options === void 0 ? void 0 : options.ttlMs) !== null && _b !== void 0 ? _b : idempotency_constants_1.DEFAULT_TTL_MS,
|
|
49
|
+
requireKey: (_c = options === null || options === void 0 ? void 0 : options.requireKey) !== null && _c !== void 0 ? _c : idempotency_constants_1.DEFAULT_REQUIRE_KEY,
|
|
50
|
+
replayHeaders: (_d = options === null || options === void 0 ? void 0 : options.replayHeaders) !== null && _d !== void 0 ? _d : idempotency_constants_1.DEFAULT_REPLAY_HEADERS,
|
|
51
|
+
principalExtractor: (_e = options === null || options === void 0 ? void 0 : options.principalExtractor) !== null && _e !== void 0 ? _e : default_principal_extractor_1.defaultPrincipalExtractor,
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
inject: [idempotency_constants_1.IDEMPOTENCY_MODULE_OPTIONS],
|
|
55
|
+
},
|
|
56
|
+
idempotency_interceptor_1.IdempotencyInterceptor,
|
|
57
|
+
],
|
|
58
|
+
exports: [idempotency_interceptor_1.IdempotencyInterceptor, idempotency_constants_1.IDEMPOTENCY_STORE, idempotency_constants_1.IDEMPOTENCY_INTERCEPTOR_OPTIONS],
|
|
59
|
+
})
|
|
60
|
+
], IdempotencyModule);
|
|
61
|
+
//# sourceMappingURL=idempotency.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"idempotency.module.js","sourceRoot":"","sources":["../../lib/module/idempotency.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAuD;AACvD,0DAAiE;AAEjE,oFAAgF;AAChF,6EAQ2C;AAC3C,yFAAwG;AACxG,yFAAoF;AA4B7E,IAAM,iBAAiB,yBAAvB,MAAM,iBAAkB,SAAQ,IAAA,uCAAuB,EAC5D,kDAA0B,CAC3B;IACC,MAAM,CAAC,QAAQ,CAAC,OAAkC;QAChD,OAAO;YACL,GAAG,KAAK,CAAC,QAAQ,CAAC,OAAO,aAAP,OAAO,cAAP,OAAO,GAAK,EAA+B,CAAC;YAC9D,MAAM,EAAE,mBAAiB;YACzB,MAAM,EAAE,IAAI;SACb,CAAC;IACJ,CAAC;IAED,MAAM,CAAC,aAAa,CAAC,OAAwC;QAC3D,OAAO;YACL,GAAG,KAAK,CAAC,aAAa,CAAC,OAAO,CAAC;YAC/B,MAAM,EAAE,mBAAiB;YACzB,MAAM,EAAE,IAAI;SACb,CAAC;IACJ,CAAC;CACF,CAAA;AAlBY,8CAAiB;4BAAjB,iBAAiB;IAtB7B,IAAA,eAAM,EAAC;QACN,SAAS,EAAE;YACT;gBACE,OAAO,EAAE,yCAAiB;gBAC1B,UAAU,EAAE,CAAC,OAAkC,EAAE,EAAE,CAAC,IAAA,uDAAyB,EAAC,OAAO,CAAC,CAAC,KAAK;gBAC5F,MAAM,EAAE,CAAC,kDAA0B,CAAC;aACrC;YACD;gBACE,OAAO,EAAE,uDAA+B;gBACxC,UAAU,EAAE,CAAC,OAAkC,EAAsB,EAAE;;oBAAC,OAAA,CAAC;wBACvE,UAAU,EAAE,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,UAAU,mCAAI,2CAAmB;wBACtD,KAAK,EAAE,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,mCAAI,sCAAc;wBACvC,UAAU,EAAE,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,UAAU,mCAAI,2CAAmB;wBACtD,aAAa,EAAE,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,aAAa,mCAAI,8CAAsB;wBAC/D,kBAAkB,EAAE,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,kBAAkB,mCAAI,uDAAyB;qBAC7E,CAAC,CAAA;iBAAA;gBACF,MAAM,EAAE,CAAC,kDAA0B,CAAC;aACrC;YACD,gDAAsB;SACvB;QACD,OAAO,EAAE,CAAC,gDAAsB,EAAE,yCAAiB,EAAE,uDAA+B,CAAC;KACtF,CAAC;GACW,iBAAiB,CAkB7B"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultPrincipalExtractor = void 0;
|
|
4
|
+
const defaultPrincipalExtractor = (request) => {
|
|
5
|
+
var _a;
|
|
6
|
+
const user = request === null || request === void 0 ? void 0 : request.user;
|
|
7
|
+
const id = (_a = user === null || user === void 0 ? void 0 : user.id) !== null && _a !== void 0 ? _a : user === null || user === void 0 ? void 0 : user.sub;
|
|
8
|
+
return typeof id === 'string' && id.length > 0 ? id : undefined;
|
|
9
|
+
};
|
|
10
|
+
exports.defaultPrincipalExtractor = defaultPrincipalExtractor;
|
|
11
|
+
//# sourceMappingURL=default-principal-extractor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"default-principal-extractor.js","sourceRoot":"","sources":["../../lib/provider/default-principal-extractor.ts"],"names":[],"mappings":";;;AAYO,MAAM,yBAAyB,GAAuB,CAAC,OAAgB,EAAsB,EAAE;;IACpG,MAAM,IAAI,GAAI,OAAgE,aAAhE,OAAO,uBAAP,OAAO,CAA2D,IAAI,CAAC;IACrF,MAAM,EAAE,GAAG,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,EAAE,mCAAI,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,GAAG,CAAC;IACjC,OAAO,OAAO,EAAE,KAAK,QAAQ,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;AAClE,CAAC,CAAC;AAJW,QAAA,yBAAyB,6BAIpC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function computeFingerprint(body: unknown): string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.computeFingerprint = computeFingerprint;
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
5
|
+
function stableStringify(value) {
|
|
6
|
+
if (value === null || typeof value !== 'object') {
|
|
7
|
+
return JSON.stringify(value);
|
|
8
|
+
}
|
|
9
|
+
if (Array.isArray(value)) {
|
|
10
|
+
return '[' + value.map(stableStringify).join(',') + ']';
|
|
11
|
+
}
|
|
12
|
+
const sorted = Object.keys(value).sort();
|
|
13
|
+
return '{' + sorted.map(k => JSON.stringify(k) + ':' + stableStringify(value[k])).join(',') + '}';
|
|
14
|
+
}
|
|
15
|
+
function computeFingerprint(body) {
|
|
16
|
+
const serialized = stableStringify(body === undefined ? null : body);
|
|
17
|
+
return (0, crypto_1.createHash)('sha256').update(serialized).digest('hex');
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=fingerprint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fingerprint.js","sourceRoot":"","sources":["../../lib/provider/fingerprint.ts"],"names":[],"mappings":";;AAaA,gDAGC;AAhBD,mCAAoC;AAEpC,SAAS,eAAe,CAAC,KAAc;IACrC,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAChD,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;IAC1D,CAAC;IACD,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,KAAe,CAAC,CAAC,IAAI,EAAE,CAAC;IACnD,OAAO,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,eAAe,CAAE,KAAiC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;AACjI,CAAC;AAED,SAAgB,kBAAkB,CAAC,IAAa;IAC9C,MAAM,UAAU,GAAG,eAAe,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IACrE,OAAO,IAAA,mBAAU,EAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC/D,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { IIdempotencyStore } from '../interface/idempotency-store.interface';
|
|
2
|
+
import { IdempotencyRecord } from '../interface/idempotency-record.interface';
|
|
3
|
+
import { StoredResponse } from '../interface/idempotency-record.interface';
|
|
4
|
+
export declare class InMemoryIdempotencyStore implements IIdempotencyStore {
|
|
5
|
+
private readonly map;
|
|
6
|
+
private readonly ttlMs;
|
|
7
|
+
constructor(opts?: {
|
|
8
|
+
ttlMs?: number;
|
|
9
|
+
});
|
|
10
|
+
private isExpired;
|
|
11
|
+
putIfAbsent(key: string, record: IdempotencyRecord): Promise<IdempotencyRecord | undefined>;
|
|
12
|
+
complete(key: string, response: StoredResponse): Promise<void>;
|
|
13
|
+
remove(key: string): Promise<void>;
|
|
14
|
+
get(key: string): Promise<IdempotencyRecord | undefined>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InMemoryIdempotencyStore = void 0;
|
|
4
|
+
const idempotency_constants_1 = require("../constant/idempotency.constants");
|
|
5
|
+
class InMemoryIdempotencyStore {
|
|
6
|
+
constructor(opts) {
|
|
7
|
+
var _a;
|
|
8
|
+
this.map = new Map();
|
|
9
|
+
this.ttlMs = (_a = opts === null || opts === void 0 ? void 0 : opts.ttlMs) !== null && _a !== void 0 ? _a : idempotency_constants_1.DEFAULT_TTL_MS;
|
|
10
|
+
}
|
|
11
|
+
isExpired(record) {
|
|
12
|
+
return Date.now() - record.createdAt >= this.ttlMs;
|
|
13
|
+
}
|
|
14
|
+
async putIfAbsent(key, record) {
|
|
15
|
+
const existing = this.map.get(key);
|
|
16
|
+
if (existing !== undefined) {
|
|
17
|
+
if (!this.isExpired(existing)) {
|
|
18
|
+
return { ...existing };
|
|
19
|
+
}
|
|
20
|
+
this.map.delete(key);
|
|
21
|
+
}
|
|
22
|
+
this.map.set(key, record);
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
async complete(key, response) {
|
|
26
|
+
const existing = this.map.get(key);
|
|
27
|
+
if (existing === undefined || this.isExpired(existing)) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
this.map.set(key, { ...existing, state: 'completed', response });
|
|
31
|
+
}
|
|
32
|
+
async remove(key) {
|
|
33
|
+
this.map.delete(key);
|
|
34
|
+
}
|
|
35
|
+
async get(key) {
|
|
36
|
+
const record = this.map.get(key);
|
|
37
|
+
if (record === undefined) {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
if (this.isExpired(record)) {
|
|
41
|
+
this.map.delete(key);
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
return { ...record };
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.InMemoryIdempotencyStore = InMemoryIdempotencyStore;
|
|
48
|
+
//# sourceMappingURL=in-memory-idempotency-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"in-memory-idempotency-store.js","sourceRoot":"","sources":["../../lib/provider/in-memory-idempotency-store.ts"],"names":[],"mappings":";;;AAGA,6EAAmE;AAEnE,MAAa,wBAAwB;IAInC,YAAY,IAAyB;;QAHpB,QAAG,GAAG,IAAI,GAAG,EAA6B,CAAC;QAI1D,IAAI,CAAC,KAAK,GAAG,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,KAAK,mCAAI,sCAAc,CAAC;IAC7C,CAAC;IAEO,SAAS,CAAC,MAAyB;QACzC,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC,SAAS,IAAI,IAAI,CAAC,KAAK,CAAC;IACrD,CAAC;IAED,KAAK,CAAC,WAAW,CAAC,GAAW,EAAE,MAAyB;QACtD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SAAS,CAAC;QACnB,CAAC;QACD,OAAO,EAAE,GAAG,MAAM,EAAE,CAAC;IACvB,CAAC;CACF;AA/CD,4DA+CC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { IdempotencyModuleOptions } from '../interface/idempotency-module-options.interface';
|
|
2
|
+
import { IIdempotencyStore } from '../interface/idempotency-store.interface';
|
|
3
|
+
import { PrincipalExtractor } from './default-principal-extractor';
|
|
4
|
+
export interface ResolvedIdempotencyOptions {
|
|
5
|
+
store: IIdempotencyStore;
|
|
6
|
+
headerName: string;
|
|
7
|
+
ttlMs: number;
|
|
8
|
+
requireKey: boolean;
|
|
9
|
+
replayHeaders: string[];
|
|
10
|
+
principalExtractor: PrincipalExtractor;
|
|
11
|
+
}
|
|
12
|
+
export type InterceptorOptions = Omit<ResolvedIdempotencyOptions, 'store'>;
|
|
13
|
+
export declare function resolveIdempotencyOptions(options: IdempotencyModuleOptions | undefined): ResolvedIdempotencyOptions;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveIdempotencyOptions = resolveIdempotencyOptions;
|
|
4
|
+
const in_memory_idempotency_store_1 = require("./in-memory-idempotency-store");
|
|
5
|
+
const default_principal_extractor_1 = require("./default-principal-extractor");
|
|
6
|
+
const idempotency_constants_1 = require("../constant/idempotency.constants");
|
|
7
|
+
function resolveIdempotencyOptions(options) {
|
|
8
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
9
|
+
return {
|
|
10
|
+
store: (_a = options === null || options === void 0 ? void 0 : options.store) !== null && _a !== void 0 ? _a : new in_memory_idempotency_store_1.InMemoryIdempotencyStore({ ttlMs: (_b = options === null || options === void 0 ? void 0 : options.ttlMs) !== null && _b !== void 0 ? _b : idempotency_constants_1.DEFAULT_TTL_MS }),
|
|
11
|
+
headerName: (_c = options === null || options === void 0 ? void 0 : options.headerName) !== null && _c !== void 0 ? _c : idempotency_constants_1.DEFAULT_HEADER_NAME,
|
|
12
|
+
ttlMs: (_d = options === null || options === void 0 ? void 0 : options.ttlMs) !== null && _d !== void 0 ? _d : idempotency_constants_1.DEFAULT_TTL_MS,
|
|
13
|
+
requireKey: (_e = options === null || options === void 0 ? void 0 : options.requireKey) !== null && _e !== void 0 ? _e : idempotency_constants_1.DEFAULT_REQUIRE_KEY,
|
|
14
|
+
replayHeaders: (_f = options === null || options === void 0 ? void 0 : options.replayHeaders) !== null && _f !== void 0 ? _f : idempotency_constants_1.DEFAULT_REPLAY_HEADERS,
|
|
15
|
+
principalExtractor: (_g = options === null || options === void 0 ? void 0 : options.principalExtractor) !== null && _g !== void 0 ? _g : default_principal_extractor_1.defaultPrincipalExtractor,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=resolve-idempotency-options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-idempotency-options.js","sourceRoot":"","sources":["../../lib/provider/resolve-idempotency-options.ts"],"names":[],"mappings":";;AAsBA,8DASC;AA7BD,+EAAyE;AACzE,+EAA8F;AAC9F,6EAK2C;AAa3C,SAAgB,yBAAyB,CAAC,OAA6C;;IACrF,OAAO;QACL,KAAK,EAAE,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,mCAAI,IAAI,sDAAwB,CAAC,EAAE,KAAK,EAAE,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,mCAAI,sCAAc,EAAE,CAAC;QAClG,UAAU,EAAE,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,UAAU,mCAAI,2CAAmB;QACtD,KAAK,EAAE,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,mCAAI,sCAAc;QACvC,UAAU,EAAE,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,UAAU,mCAAI,2CAAmB;QACtD,aAAa,EAAE,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,aAAa,mCAAI,8CAAsB;QAC/D,kBAAkB,EAAE,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,kBAAkB,mCAAI,uDAAyB;KAC7E,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const IDEMPOTENCY_STORE = Symbol.for('IDEMPOTENCY_STORE');
|
|
2
|
+
export const IDEMPOTENCY_MODULE_OPTIONS = 'IDEMPOTENCY_MODULE_OPTIONS';
|
|
3
|
+
export const IDEMPOTENCY_INTERCEPTOR_OPTIONS = 'IDEMPOTENCY_INTERCEPTOR_OPTIONS';
|
|
4
|
+
export const IDEMPOTENT_METADATA_KEY = 'idempotent';
|
|
5
|
+
export const DEFAULT_HEADER_NAME = 'Idempotency-Key';
|
|
6
|
+
export const DEFAULT_TTL_MS = 86_400_000;
|
|
7
|
+
export const DEFAULT_REQUIRE_KEY = false;
|
|
8
|
+
export const DEFAULT_REPLAY_HEADERS = ['content-type'];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './idempotency.constants';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { HttpStatus } from '@nestjs/common';
|
|
2
|
+
import { AppException } from '@nathapp/nestjs-common';
|
|
3
|
+
import { IdempotencyExceptionCode } from './idempotency-exception-code.enum';
|
|
4
|
+
|
|
5
|
+
export class IdempotencyConflictException extends AppException {
|
|
6
|
+
constructor() {
|
|
7
|
+
super(IdempotencyExceptionCode.CONFLICT_IN_FLIGHT, undefined, undefined, HttpStatus.CONFLICT);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class IdempotencyKeyReuseException extends AppException {
|
|
12
|
+
constructor() {
|
|
13
|
+
super(IdempotencyExceptionCode.KEY_REUSE_MISMATCH, undefined, undefined, HttpStatus.UNPROCESSABLE_ENTITY);
|
|
14
|
+
}
|
|
15
|
+
}
|
package/lib/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './constant/idempotency.constants';
|
|
2
|
+
export * from './decorator/idempotent.decorator';
|
|
3
|
+
export * from './exception/idempotency-exception-code.enum';
|
|
4
|
+
export * from './exception/idempotency.exceptions';
|
|
5
|
+
export * from './interceptor/idempotency.interceptor';
|
|
6
|
+
export * from './interface/idempotency-module-options.interface';
|
|
7
|
+
export * from './interface/idempotency-record.interface';
|
|
8
|
+
export * from './interface/idempotency-store.interface';
|
|
9
|
+
export * from './module/idempotency.module';
|
|
10
|
+
export * from './provider/default-principal-extractor';
|
|
11
|
+
export * from './provider/fingerprint';
|
|
12
|
+
export * from './provider/in-memory-idempotency-store';
|
|
13
|
+
export * from './provider/resolve-idempotency-options';
|