@nathapp/nestjs-idempotency 4.0.0 → 4.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/dist/interceptor/idempotency.interceptor.js +17 -3
- package/dist/interceptor/idempotency.interceptor.js.map +1 -1
- package/dist/provider/fingerprint.js +15 -6
- package/dist/provider/fingerprint.js.map +1 -1
- package/dist/provider/in-memory-idempotency-store.d.ts +8 -1
- package/dist/provider/in-memory-idempotency-store.js +27 -1
- package/dist/provider/in-memory-idempotency-store.js.map +1 -1
- package/package.json +6 -6
- package/.eslintrc.js +0 -8
- package/.nax/features/nestjs-idempotency/.nax-acceptance.test.ts +0 -766
- package/.nax/features/nestjs-idempotency/.nax-suggested.test.ts +0 -439
- package/.nax/test-debug.ts +0 -22
- package/.turbo/turbo-build.log +0 -4
- package/CHANGELOG.md +0 -42
- package/lib/constant/idempotency.constants.ts +0 -8
- package/lib/constant/index.ts +0 -1
- package/lib/decorator/idempotent.decorator.ts +0 -4
- package/lib/exception/idempotency-exception-code.enum.ts +0 -5
- package/lib/exception/idempotency.exceptions.ts +0 -15
- package/lib/index.ts +0 -13
- package/lib/interceptor/idempotency.interceptor.ts +0 -187
- package/lib/interface/idempotency-module-options.interface.ts +0 -21
- package/lib/interface/idempotency-record.interface.ts +0 -14
- package/lib/interface/idempotency-store.interface.ts +0 -8
- package/lib/module/idempotency.module.ts +0 -61
- package/lib/provider/default-principal-extractor.ts +0 -17
- package/lib/provider/fingerprint.ts +0 -17
- package/lib/provider/in-memory-idempotency-store.ts +0 -53
- package/lib/provider/resolve-idempotency-options.ts +0 -32
- package/test/barrel/index.spec.ts +0 -40
- package/test/constant/idempotency.constants.spec.ts +0 -10
- package/test/decorator/idempotent.decorator.spec.ts +0 -20
- package/test/e2e/express.e2e.spec.ts +0 -219
- package/test/e2e/fastify.e2e.spec.ts +0 -64
- package/test/exception/idempotency.exceptions.spec.ts +0 -24
- package/test/interceptor/idempotency.interceptor.spec.ts +0 -739
- package/test/jest-nax-setup-test.js +0 -21
- package/test/jest-nax-setup.js +0 -92
- package/test/jest-nax.json +0 -30
- package/test/jest-test.json +0 -28
- package/test/module/idempotency.module.spec.ts +0 -87
- package/test/provider/fingerprint.spec.ts +0 -27
- package/test/provider/in-memory-idempotency-store.spec.ts +0 -212
- package/test/provider/resolve-idempotency-options.spec.ts +0 -42
- package/tsconfig.json +0 -9
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
-
import { describe, expect, it, beforeAll, afterAll, jest } from '@jest/globals';
|
|
3
|
-
import {
|
|
4
|
-
Controller,
|
|
5
|
-
Post,
|
|
6
|
-
Body,
|
|
7
|
-
INestApplication,
|
|
8
|
-
HttpException,
|
|
9
|
-
HttpStatus,
|
|
10
|
-
} from '@nestjs/common';
|
|
11
|
-
import { APP_INTERCEPTOR } from '@nestjs/core';
|
|
12
|
-
import { Test } from '@nestjs/testing';
|
|
13
|
-
import { IdempotencyModule } from '../../lib/module/idempotency.module';
|
|
14
|
-
import { IdempotencyInterceptor } from '../../lib/interceptor/idempotency.interceptor';
|
|
15
|
-
import { Idempotent } from '../../lib/decorator/idempotent.decorator';
|
|
16
|
-
|
|
17
|
-
// ─── Shared state for the main @Idempotent() controller ──────────────────────
|
|
18
|
-
|
|
19
|
-
let _mainHandlerCount = 0;
|
|
20
|
-
let _mainCounter = 0;
|
|
21
|
-
let _mainBlocked = false;
|
|
22
|
-
let _mainUnblockFn: (() => void) | null = null;
|
|
23
|
-
|
|
24
|
-
@Controller()
|
|
25
|
-
class MainTestController {
|
|
26
|
-
@Post('test')
|
|
27
|
-
@Idempotent()
|
|
28
|
-
async handle(@Body() _body: unknown) {
|
|
29
|
-
_mainHandlerCount++;
|
|
30
|
-
_mainCounter++;
|
|
31
|
-
if (_mainBlocked && _mainUnblockFn === null) {
|
|
32
|
-
await new Promise<void>(r => {
|
|
33
|
-
_mainUnblockFn = r;
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
return { count: _mainCounter };
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
let _errorHandlerCount = 0;
|
|
41
|
-
|
|
42
|
-
@Controller()
|
|
43
|
-
class ErrorTestController {
|
|
44
|
-
@Post('test')
|
|
45
|
-
@Idempotent()
|
|
46
|
-
handle(@Body() _body: unknown): never {
|
|
47
|
-
_errorHandlerCount++;
|
|
48
|
-
throw new HttpException({ error: 'fail' }, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// ─── Suite 1: replay and pass-through (AC6, AC10) ────────────────────────────
|
|
53
|
-
|
|
54
|
-
describe('Express integration — replay and pass-through', () => {
|
|
55
|
-
let app: INestApplication;
|
|
56
|
-
|
|
57
|
-
beforeAll(async () => {
|
|
58
|
-
_mainHandlerCount = 0;
|
|
59
|
-
_mainCounter = 0;
|
|
60
|
-
_mainBlocked = false;
|
|
61
|
-
_mainUnblockFn = null;
|
|
62
|
-
const moduleRef = await Test.createTestingModule({
|
|
63
|
-
imports: [IdempotencyModule.register()],
|
|
64
|
-
controllers: [MainTestController],
|
|
65
|
-
providers: [{ provide: APP_INTERCEPTOR, useClass: IdempotencyInterceptor }],
|
|
66
|
-
}).compile();
|
|
67
|
-
app = moduleRef.createNestApplication();
|
|
68
|
-
await app.init();
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
afterAll(async () => {
|
|
72
|
-
await app?.close();
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it('AC6: two sequential requests with the same Idempotency-Key and body — handler runs once, both responses identical (success path)', async () => {
|
|
76
|
-
const supertest = require('supertest');
|
|
77
|
-
const countBefore = _mainHandlerCount;
|
|
78
|
-
|
|
79
|
-
const first = await supertest(app.getHttpServer())
|
|
80
|
-
.post('/test')
|
|
81
|
-
.set('Idempotency-Key', 'K-replay')
|
|
82
|
-
.send({ a: 1 });
|
|
83
|
-
const second = await supertest(app.getHttpServer())
|
|
84
|
-
.post('/test')
|
|
85
|
-
.set('Idempotency-Key', 'K-replay')
|
|
86
|
-
.send({ a: 1 });
|
|
87
|
-
|
|
88
|
-
expect(first.statusCode).toBe(200);
|
|
89
|
-
expect(second.statusCode).toBe(200);
|
|
90
|
-
expect(second.body).toEqual(first.body);
|
|
91
|
-
expect(_mainHandlerCount).toBe(countBefore + 1);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('AC10: a @Idempotent() route called without the Idempotency-Key header (default requireKey=false) executes normally (success path)', async () => {
|
|
95
|
-
const supertest = require('supertest');
|
|
96
|
-
const countBefore = _mainHandlerCount;
|
|
97
|
-
|
|
98
|
-
const res = await supertest(app.getHttpServer())
|
|
99
|
-
.post('/test')
|
|
100
|
-
.send({ a: 99 });
|
|
101
|
-
|
|
102
|
-
expect(res.statusCode).toBe(200);
|
|
103
|
-
expect(res.body).toBeDefined();
|
|
104
|
-
expect(res.body.count).toBeDefined();
|
|
105
|
-
expect(_mainHandlerCount).toBe(countBefore + 1);
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
// ─── Suite 2: 409 (in-flight) and 422 (body-mismatch) (AC7, AC8) ─────────────
|
|
110
|
-
|
|
111
|
-
describe('Express integration — 409 in-flight and 422 body-mismatch', () => {
|
|
112
|
-
let app: INestApplication;
|
|
113
|
-
|
|
114
|
-
beforeAll(async () => {
|
|
115
|
-
_mainHandlerCount = 0;
|
|
116
|
-
_mainCounter = 0;
|
|
117
|
-
_mainBlocked = false;
|
|
118
|
-
_mainUnblockFn = null;
|
|
119
|
-
const moduleRef = await Test.createTestingModule({
|
|
120
|
-
imports: [IdempotencyModule.register()],
|
|
121
|
-
controllers: [MainTestController],
|
|
122
|
-
providers: [{ provide: APP_INTERCEPTOR, useClass: IdempotencyInterceptor }],
|
|
123
|
-
}).compile();
|
|
124
|
-
app = moduleRef.createNestApplication();
|
|
125
|
-
await app.init();
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
afterAll(async () => {
|
|
129
|
-
_mainBlocked = false;
|
|
130
|
-
_mainUnblockFn?.();
|
|
131
|
-
await app?.close();
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
it('AC7: concurrent requests with the same key while the first is still pending — second receives 409 (boundary — duplicate in-flight)', async () => {
|
|
135
|
-
const supertest = require('supertest');
|
|
136
|
-
_mainBlocked = true;
|
|
137
|
-
_mainUnblockFn = null;
|
|
138
|
-
|
|
139
|
-
const firstPromise = supertest(app.getHttpServer())
|
|
140
|
-
.post('/test')
|
|
141
|
-
.set('Idempotency-Key', 'K-concurrent')
|
|
142
|
-
.send({ a: 1 });
|
|
143
|
-
// Fire the first request without awaiting its response; the .then() triggers
|
|
144
|
-
// supertest's lazy send so the second request sees a pending store record.
|
|
145
|
-
firstPromise.then(
|
|
146
|
-
() => undefined,
|
|
147
|
-
() => undefined,
|
|
148
|
-
);
|
|
149
|
-
|
|
150
|
-
// Let the first request register as pending in the store before the second arrives.
|
|
151
|
-
await new Promise(r => setTimeout(r, 80));
|
|
152
|
-
|
|
153
|
-
const second = await supertest(app.getHttpServer())
|
|
154
|
-
.post('/test')
|
|
155
|
-
.set('Idempotency-Key', 'K-concurrent')
|
|
156
|
-
.send({ a: 1 });
|
|
157
|
-
expect(second.statusCode).toBe(409);
|
|
158
|
-
|
|
159
|
-
// Release the first request.
|
|
160
|
-
_mainUnblockFn?.();
|
|
161
|
-
_mainBlocked = false;
|
|
162
|
-
const first = await firstPromise;
|
|
163
|
-
expect(first.statusCode).toBe(200);
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
it('AC8: same Idempotency-Key with a different body — second request receives 422 (boundary — fingerprint mismatch)', async () => {
|
|
167
|
-
const supertest = require('supertest');
|
|
168
|
-
_mainBlocked = false;
|
|
169
|
-
|
|
170
|
-
const first = await supertest(app.getHttpServer())
|
|
171
|
-
.post('/test')
|
|
172
|
-
.set('Idempotency-Key', 'K-mismatch')
|
|
173
|
-
.send({ a: 1 });
|
|
174
|
-
expect(first.statusCode).toBe(200);
|
|
175
|
-
|
|
176
|
-
const second = await supertest(app.getHttpServer())
|
|
177
|
-
.post('/test')
|
|
178
|
-
.set('Idempotency-Key', 'K-mismatch')
|
|
179
|
-
.send({ a: 2 });
|
|
180
|
-
expect(second.statusCode).toBe(422);
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
// ─── Suite 3: non-2xx responses are not cached (AC9) ────────────────────────
|
|
185
|
-
|
|
186
|
-
describe('Express integration — non-2xx response not cached', () => {
|
|
187
|
-
let app: INestApplication;
|
|
188
|
-
|
|
189
|
-
beforeAll(async () => {
|
|
190
|
-
_errorHandlerCount = 0;
|
|
191
|
-
const moduleRef = await Test.createTestingModule({
|
|
192
|
-
imports: [IdempotencyModule.register()],
|
|
193
|
-
controllers: [ErrorTestController],
|
|
194
|
-
providers: [{ provide: APP_INTERCEPTOR, useClass: IdempotencyInterceptor }],
|
|
195
|
-
}).compile();
|
|
196
|
-
app = moduleRef.createNestApplication();
|
|
197
|
-
await app.init();
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
afterAll(async () => {
|
|
201
|
-
await app?.close();
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
it('AC9: a handler returning 500 — second request with the same key re-invokes the handler (boundary — failed attempt not cached)', async () => {
|
|
205
|
-
const supertest = require('supertest');
|
|
206
|
-
const first = await supertest(app.getHttpServer())
|
|
207
|
-
.post('/test')
|
|
208
|
-
.set('Idempotency-Key', 'K-error')
|
|
209
|
-
.send({});
|
|
210
|
-
expect(first.statusCode).toBe(500);
|
|
211
|
-
|
|
212
|
-
const second = await supertest(app.getHttpServer())
|
|
213
|
-
.post('/test')
|
|
214
|
-
.set('Idempotency-Key', 'K-error')
|
|
215
|
-
.send({});
|
|
216
|
-
expect(second.statusCode).toBe(500);
|
|
217
|
-
expect(_errorHandlerCount).toBe(2);
|
|
218
|
-
});
|
|
219
|
-
});
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
-
import { describe, expect, it, beforeAll, afterAll } from '@jest/globals';
|
|
3
|
-
import { Controller, Post, Body } from '@nestjs/common';
|
|
4
|
-
import { APP_INTERCEPTOR } from '@nestjs/core';
|
|
5
|
-
import { Test } from '@nestjs/testing';
|
|
6
|
-
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
|
|
7
|
-
import { IdempotencyModule } from '../../lib/module/idempotency.module';
|
|
8
|
-
import { IdempotencyInterceptor } from '../../lib/interceptor/idempotency.interceptor';
|
|
9
|
-
import { Idempotent } from '../../lib/decorator/idempotent.decorator';
|
|
10
|
-
|
|
11
|
-
let _handlerCount = 0;
|
|
12
|
-
let _counter = 0;
|
|
13
|
-
|
|
14
|
-
@Controller()
|
|
15
|
-
class FastifyTestController {
|
|
16
|
-
@Post('test')
|
|
17
|
-
@Idempotent()
|
|
18
|
-
handle(@Body() _body: unknown) {
|
|
19
|
-
_handlerCount++;
|
|
20
|
-
_counter++;
|
|
21
|
-
return { count: _counter };
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
describe('Fastify integration — replay via HttpAdapterHost', () => {
|
|
26
|
-
let app: NestFastifyApplication;
|
|
27
|
-
|
|
28
|
-
beforeAll(async () => {
|
|
29
|
-
_handlerCount = 0;
|
|
30
|
-
_counter = 0;
|
|
31
|
-
const moduleRef = await Test.createTestingModule({
|
|
32
|
-
imports: [IdempotencyModule.register()],
|
|
33
|
-
controllers: [FastifyTestController],
|
|
34
|
-
providers: [{ provide: APP_INTERCEPTOR, useClass: IdempotencyInterceptor }],
|
|
35
|
-
}).compile();
|
|
36
|
-
app = moduleRef.createNestApplication<NestFastifyApplication>(new FastifyAdapter());
|
|
37
|
-
await app.init();
|
|
38
|
-
await app.getHttpAdapter().getInstance().ready();
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
afterAll(async () => {
|
|
42
|
-
await app?.close();
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('AC11: two sequential requests with the same Idempotency-Key and body invoke the handler once, and the second response body and status equal the first (success path)', async () => {
|
|
46
|
-
const first = await app.inject({
|
|
47
|
-
method: 'POST',
|
|
48
|
-
url: '/test',
|
|
49
|
-
headers: { 'idempotency-key': 'K-fastify', 'content-type': 'application/json' },
|
|
50
|
-
payload: { a: 1 },
|
|
51
|
-
});
|
|
52
|
-
const second = await app.inject({
|
|
53
|
-
method: 'POST',
|
|
54
|
-
url: '/test',
|
|
55
|
-
headers: { 'idempotency-key': 'K-fastify', 'content-type': 'application/json' },
|
|
56
|
-
payload: { a: 1 },
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
expect(first.statusCode).toBe(200);
|
|
60
|
-
expect(second.statusCode).toBe(200);
|
|
61
|
-
expect(JSON.parse(second.body)).toEqual(JSON.parse(first.body));
|
|
62
|
-
expect(_handlerCount).toBe(1);
|
|
63
|
-
});
|
|
64
|
-
});
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from '@jest/globals';
|
|
2
|
-
import { AppException } from '@nathapp/nestjs-common';
|
|
3
|
-
import { IdempotencyConflictException, IdempotencyKeyReuseException } from '../../lib/exception/idempotency.exceptions';
|
|
4
|
-
import { IdempotencyExceptionCode } from '../../lib/exception/idempotency-exception-code.enum';
|
|
5
|
-
|
|
6
|
-
describe('IdempotencyConflictException', () => {
|
|
7
|
-
it('AC9: is instanceof AppException with httpStatus 409 and code === CONFLICT_IN_FLIGHT (40900)', () => {
|
|
8
|
-
const ex = new IdempotencyConflictException();
|
|
9
|
-
expect(ex).toBeInstanceOf(AppException);
|
|
10
|
-
expect(ex.httpStatus).toBe(409);
|
|
11
|
-
expect(ex.code).toBe(IdempotencyExceptionCode.CONFLICT_IN_FLIGHT);
|
|
12
|
-
expect(IdempotencyExceptionCode.CONFLICT_IN_FLIGHT).toBe(40900);
|
|
13
|
-
});
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
describe('IdempotencyKeyReuseException', () => {
|
|
17
|
-
it('AC10: is instanceof AppException with httpStatus 422 and code === KEY_REUSE_MISMATCH (40901)', () => {
|
|
18
|
-
const ex = new IdempotencyKeyReuseException();
|
|
19
|
-
expect(ex).toBeInstanceOf(AppException);
|
|
20
|
-
expect(ex.httpStatus).toBe(422);
|
|
21
|
-
expect(ex.code).toBe(IdempotencyExceptionCode.KEY_REUSE_MISMATCH);
|
|
22
|
-
expect(IdempotencyExceptionCode.KEY_REUSE_MISMATCH).toBe(40901);
|
|
23
|
-
});
|
|
24
|
-
});
|