@nest-yalc-2/api-strategy 2.2.1 → 2.2.3

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.
Files changed (30) hide show
  1. package/package.json +10 -2
  2. package/src/__tests__/call-strategy-wrappers.spec.ts +217 -217
  3. package/src/__tests__/context-call.service.spec.ts +52 -52
  4. package/src/__tests__/context-event.service.spec.ts +52 -52
  5. package/src/__tests__/event-strategy-composition.spec.ts +185 -185
  6. package/src/__tests__/header-whitelist.helper.spec.ts +41 -30
  7. package/src/__tests__/http-abstract-call.strategy.spec.ts +17 -17
  8. package/src/__tests__/nest-http-call.strategy.spec.ts +164 -164
  9. package/src/__tests__/nest-local-call.strategy.spec.ts +263 -263
  10. package/src/__tests__/nest-local-event.strategy.spec.ts +55 -55
  11. package/src/__tests__/rabbitmq-event.strategy.spec.ts +220 -220
  12. package/src/__tests__/strategy-selector.provider.spec.ts +182 -182
  13. package/src/context-call.interface.ts +49 -49
  14. package/src/context-call.service.ts +31 -31
  15. package/src/context-event.interface.ts +4 -4
  16. package/src/context-event.service.ts +31 -31
  17. package/src/header-whitelist.helper.ts +12 -12
  18. package/src/index.ts +17 -17
  19. package/src/strategies/composite-event.strategy.ts +76 -76
  20. package/src/strategies/conditional-call.strategy.ts +70 -70
  21. package/src/strategies/conditional-event.strategy.ts +54 -54
  22. package/src/strategies/fallback-call.strategy.ts +67 -67
  23. package/src/strategies/http-abstract-call.strategy.ts +238 -238
  24. package/src/strategies/nest-http-call.strategy.ts +121 -121
  25. package/src/strategies/nest-local-call.strategy.ts +153 -153
  26. package/src/strategies/nest-local-event.strategy.d.ts +1 -1
  27. package/src/strategies/nest-local-event.strategy.ts +42 -42
  28. package/src/strategies/rabbitmq-event.strategy.ts +154 -154
  29. package/src/strategies/shadow-call.strategy.ts +87 -87
  30. package/src/strategy-selector.provider.ts +103 -103
@@ -1,263 +1,263 @@
1
- import {
2
- expect,
3
- jest,
4
- describe,
5
- it,
6
- beforeEach,
7
- beforeAll,
8
- afterAll,
9
- afterEach,
10
- } from '@jest/globals';
11
- import { createMock, DeepMocked, PartialFuncReturn } from '@golevelup/ts-jest';
12
- import { HttpAdapterHost } from '@nestjs/core';
13
- import { FastifyAdapter } from '@nestjs/platform-fastify';
14
- import { InjectOptions } from 'fastify';
15
- import {
16
- NestLocalCallStrategy,
17
- NestLocalCallStrategyProvider,
18
- } from '../strategies/nest-local-call.strategy.js';
19
- import { YalcGlobalClsService } from '../../../app/src/cls.module.js';
20
-
21
- describe('NestLocalCallStrategy', () => {
22
- let adapterHost: HttpAdapterHost;
23
- let clsService: DeepMocked<YalcGlobalClsService>;
24
-
25
- beforeEach(() => {
26
- adapterHost = createMock<HttpAdapterHost>({
27
- httpAdapter: {
28
- getInstance: () =>
29
- createMock<FastifyAdapter>({
30
- inject: (
31
- _opts: string | InjectOptions,
32
- ): PartialFuncReturn<Promise<any>> =>
33
- ({
34
- body: '{}',
35
- json: () => ({}),
36
- } as any),
37
- }),
38
- },
39
- });
40
-
41
- clsService = createMock<YalcGlobalClsService>();
42
- });
43
-
44
- it('should be defined', () => {
45
- expect(NestLocalCallStrategy).toBeDefined();
46
- });
47
-
48
- it('should be instantiable', () => {
49
- const instance = new NestLocalCallStrategy(adapterHost, clsService);
50
- expect(instance).toBeDefined();
51
- });
52
-
53
- it('should be able to execute the call method', async () => {
54
- const instance = new NestLocalCallStrategy(adapterHost, clsService);
55
- const result = await instance.call('http://localhost:3000', {
56
- method: 'GET',
57
- });
58
- expect(result).toBeDefined();
59
- });
60
-
61
- it('should be able to execute the get method', async () => {
62
- const instance = new NestLocalCallStrategy(adapterHost, clsService);
63
- const result = await instance.get('http://localhost:3000');
64
- expect(result).toBeDefined();
65
- });
66
-
67
- it('should be able to execute the post method', async () => {
68
- const instance = new NestLocalCallStrategy(adapterHost, clsService);
69
- const result = await instance.post('http://localhost:3000', {});
70
- expect(result).toBeDefined();
71
- });
72
-
73
- it('should create a provider', () => {
74
- const provider = NestLocalCallStrategyProvider('test');
75
- expect(provider).toBeDefined();
76
- });
77
-
78
- it('should create a provider and execute the useFactory method', () => {
79
- const provider = NestLocalCallStrategyProvider('test');
80
- expect(provider.useFactory(adapterHost, clsService)).toBeDefined();
81
- });
82
-
83
- it('should be able to execute the call method with parameters', async () => {
84
- const instance = new NestLocalCallStrategy(adapterHost, clsService);
85
- const result = await instance.call('http://localhost:3000', {
86
- method: 'GET',
87
- parameters: {
88
- test1: { test: 'test' },
89
- },
90
- });
91
- expect(result).toBeDefined();
92
- });
93
-
94
- it('should skip parsing json if json parse throws an error', async () => {
95
- adapterHost = createMock<HttpAdapterHost>({
96
- httpAdapter: {
97
- getInstance: () =>
98
- createMock<FastifyAdapter>({
99
- inject: (
100
- _opts: string | InjectOptions,
101
- ): PartialFuncReturn<Promise<any>> =>
102
- ({
103
- body: '{',
104
- json: () => {
105
- throw new Error('test');
106
- },
107
- } as any),
108
- }),
109
- },
110
- });
111
-
112
- const instance = new NestLocalCallStrategy(adapterHost, clsService);
113
- expect(instance).toBeDefined();
114
-
115
- const result = await instance.call('http://localhost:3000', {
116
- method: 'GET',
117
- });
118
-
119
- expect(result.data).toEqual('{');
120
- });
121
-
122
- it('should skip parsing json if shouldSkipJsonParse is true', async () => {
123
- const instance = new NestLocalCallStrategy(
124
- adapterHost,
125
- clsService,
126
- // @ts-expect-error isnt needed for test
127
- {},
128
- '',
129
- {
130
- shouldSkipJsonParse: (body) => true,
131
- },
132
- );
133
- expect(instance).toBeDefined();
134
-
135
- const result = await instance.call('http://localhost:3000', {
136
- method: 'GET',
137
- });
138
-
139
- expect(result.data).toEqual('{}');
140
- });
141
-
142
- it('should parse json if shouldSkipJsonParse is false', async () => {
143
- const instance = new NestLocalCallStrategy(
144
- adapterHost,
145
- clsService,
146
- // @ts-expect-error isnt needed for test
147
- {},
148
- '',
149
- {
150
- shouldSkipJsonParse: (body) => false,
151
- },
152
- );
153
- expect(instance).toBeDefined();
154
-
155
- const result = await instance.call('http://localhost:3000', {
156
- method: 'GET',
157
- });
158
-
159
- expect(result.data).toMatchObject({});
160
- });
161
-
162
- it('should parse json if shouldSkipJsonParse is undefined', async () => {
163
- const instance = new NestLocalCallStrategy(adapterHost, clsService);
164
- expect(instance).toBeDefined();
165
-
166
- const result = await instance.call('http://localhost:3000', {
167
- method: 'GET',
168
- });
169
-
170
- expect(result.data).toMatchObject({});
171
- });
172
-
173
- it('should add internal request token header when provided', async () => {
174
- const injectSpy = jest.fn().mockResolvedValue({
175
- body: '{}',
176
- json: () => ({}),
177
- });
178
- adapterHost = createMock<HttpAdapterHost>({
179
- httpAdapter: {
180
- getInstance: () =>
181
- createMock<FastifyAdapter>({
182
- inject: injectSpy as any,
183
- }),
184
- },
185
- });
186
-
187
- const configService = {
188
- values: {
189
- internalRequestToken: 'test-token-123',
190
- },
191
- };
192
-
193
- const instance = new NestLocalCallStrategy(
194
- adapterHost,
195
- clsService,
196
- // @ts-expect-error minimal config for test
197
- configService,
198
- '',
199
- {
200
- internalRequestHeader: 'x-internal-token',
201
- },
202
- );
203
-
204
- await instance.call('/test', {
205
- method: 'GET',
206
- });
207
-
208
- expect(injectSpy).toHaveBeenCalledWith(
209
- expect.objectContaining({
210
- headers: expect.objectContaining({
211
- 'x-internal-token': 'test-token-123',
212
- }),
213
- }),
214
- );
215
- });
216
-
217
- it('should not override existing internal request token header', async () => {
218
- const injectSpy = jest.fn().mockResolvedValue({
219
- body: '{}',
220
- json: () => ({}),
221
- });
222
- adapterHost = createMock<HttpAdapterHost>({
223
- httpAdapter: {
224
- getInstance: () =>
225
- createMock<FastifyAdapter>({
226
- inject: injectSpy as any,
227
- }),
228
- },
229
- });
230
-
231
- const configService = {
232
- values: {
233
- internalRequestToken: 'test-token-123',
234
- },
235
- };
236
-
237
- const instance = new NestLocalCallStrategy(
238
- adapterHost,
239
- clsService,
240
- // @ts-expect-error minimal config for test
241
- configService,
242
- '',
243
- {
244
- internalRequestHeader: 'x-internal-token',
245
- },
246
- );
247
-
248
- await instance.call('/test', {
249
- method: 'GET',
250
- headers: {
251
- 'x-internal-token': 'user-provided-token',
252
- },
253
- });
254
-
255
- expect(injectSpy).toHaveBeenCalledWith(
256
- expect.objectContaining({
257
- headers: expect.objectContaining({
258
- 'x-internal-token': 'user-provided-token',
259
- }),
260
- }),
261
- );
262
- });
263
- });
1
+ import {
2
+ expect,
3
+ jest,
4
+ describe,
5
+ it,
6
+ beforeEach,
7
+ beforeAll,
8
+ afterAll,
9
+ afterEach,
10
+ } from '@jest/globals';
11
+ import { createMock, DeepMocked, PartialFuncReturn } from '@golevelup/ts-jest';
12
+ import { HttpAdapterHost } from '@nestjs/core';
13
+ import { FastifyAdapter } from '@nestjs/platform-fastify';
14
+ import { InjectOptions } from 'fastify';
15
+ import {
16
+ NestLocalCallStrategy,
17
+ NestLocalCallStrategyProvider,
18
+ } from '../strategies/nest-local-call.strategy.js';
19
+ import { YalcGlobalClsService } from '../../../app/src/cls.module.js';
20
+
21
+ describe('NestLocalCallStrategy', () => {
22
+ let adapterHost: HttpAdapterHost;
23
+ let clsService: DeepMocked<YalcGlobalClsService>;
24
+
25
+ beforeEach(() => {
26
+ adapterHost = createMock<HttpAdapterHost>({
27
+ httpAdapter: {
28
+ getInstance: () =>
29
+ createMock<FastifyAdapter>({
30
+ inject: (
31
+ _opts: string | InjectOptions,
32
+ ): PartialFuncReturn<Promise<any>> =>
33
+ ({
34
+ body: '{}',
35
+ json: () => ({}),
36
+ } as any),
37
+ }),
38
+ },
39
+ });
40
+
41
+ clsService = createMock<YalcGlobalClsService>();
42
+ });
43
+
44
+ it('should be defined', () => {
45
+ expect(NestLocalCallStrategy).toBeDefined();
46
+ });
47
+
48
+ it('should be instantiable', () => {
49
+ const instance = new NestLocalCallStrategy(adapterHost, clsService);
50
+ expect(instance).toBeDefined();
51
+ });
52
+
53
+ it('should be able to execute the call method', async () => {
54
+ const instance = new NestLocalCallStrategy(adapterHost, clsService);
55
+ const result = await instance.call('http://localhost:3000', {
56
+ method: 'GET',
57
+ });
58
+ expect(result).toBeDefined();
59
+ });
60
+
61
+ it('should be able to execute the get method', async () => {
62
+ const instance = new NestLocalCallStrategy(adapterHost, clsService);
63
+ const result = await instance.get('http://localhost:3000');
64
+ expect(result).toBeDefined();
65
+ });
66
+
67
+ it('should be able to execute the post method', async () => {
68
+ const instance = new NestLocalCallStrategy(adapterHost, clsService);
69
+ const result = await instance.post('http://localhost:3000', {});
70
+ expect(result).toBeDefined();
71
+ });
72
+
73
+ it('should create a provider', () => {
74
+ const provider = NestLocalCallStrategyProvider('test');
75
+ expect(provider).toBeDefined();
76
+ });
77
+
78
+ it('should create a provider and execute the useFactory method', () => {
79
+ const provider = NestLocalCallStrategyProvider('test');
80
+ expect(provider.useFactory(adapterHost, clsService)).toBeDefined();
81
+ });
82
+
83
+ it('should be able to execute the call method with parameters', async () => {
84
+ const instance = new NestLocalCallStrategy(adapterHost, clsService);
85
+ const result = await instance.call('http://localhost:3000', {
86
+ method: 'GET',
87
+ parameters: {
88
+ test1: { test: 'test' },
89
+ },
90
+ });
91
+ expect(result).toBeDefined();
92
+ });
93
+
94
+ it('should skip parsing json if json parse throws an error', async () => {
95
+ adapterHost = createMock<HttpAdapterHost>({
96
+ httpAdapter: {
97
+ getInstance: () =>
98
+ createMock<FastifyAdapter>({
99
+ inject: (
100
+ _opts: string | InjectOptions,
101
+ ): PartialFuncReturn<Promise<any>> =>
102
+ ({
103
+ body: '{',
104
+ json: () => {
105
+ throw new Error('test');
106
+ },
107
+ } as any),
108
+ }),
109
+ },
110
+ });
111
+
112
+ const instance = new NestLocalCallStrategy(adapterHost, clsService);
113
+ expect(instance).toBeDefined();
114
+
115
+ const result = await instance.call('http://localhost:3000', {
116
+ method: 'GET',
117
+ });
118
+
119
+ expect(result.data).toEqual('{');
120
+ });
121
+
122
+ it('should skip parsing json if shouldSkipJsonParse is true', async () => {
123
+ const instance = new NestLocalCallStrategy(
124
+ adapterHost,
125
+ clsService,
126
+ // @ts-expect-error isnt needed for test
127
+ {},
128
+ '',
129
+ {
130
+ shouldSkipJsonParse: (body) => true,
131
+ },
132
+ );
133
+ expect(instance).toBeDefined();
134
+
135
+ const result = await instance.call('http://localhost:3000', {
136
+ method: 'GET',
137
+ });
138
+
139
+ expect(result.data).toEqual('{}');
140
+ });
141
+
142
+ it('should parse json if shouldSkipJsonParse is false', async () => {
143
+ const instance = new NestLocalCallStrategy(
144
+ adapterHost,
145
+ clsService,
146
+ // @ts-expect-error isnt needed for test
147
+ {},
148
+ '',
149
+ {
150
+ shouldSkipJsonParse: (body) => false,
151
+ },
152
+ );
153
+ expect(instance).toBeDefined();
154
+
155
+ const result = await instance.call('http://localhost:3000', {
156
+ method: 'GET',
157
+ });
158
+
159
+ expect(result.data).toMatchObject({});
160
+ });
161
+
162
+ it('should parse json if shouldSkipJsonParse is undefined', async () => {
163
+ const instance = new NestLocalCallStrategy(adapterHost, clsService);
164
+ expect(instance).toBeDefined();
165
+
166
+ const result = await instance.call('http://localhost:3000', {
167
+ method: 'GET',
168
+ });
169
+
170
+ expect(result.data).toMatchObject({});
171
+ });
172
+
173
+ it('should add internal request token header when provided', async () => {
174
+ const injectSpy = jest.fn().mockResolvedValue({
175
+ body: '{}',
176
+ json: () => ({}),
177
+ });
178
+ adapterHost = createMock<HttpAdapterHost>({
179
+ httpAdapter: {
180
+ getInstance: () =>
181
+ createMock<FastifyAdapter>({
182
+ inject: injectSpy as any,
183
+ }),
184
+ },
185
+ });
186
+
187
+ const configService = {
188
+ values: {
189
+ internalRequestToken: 'test-token-123',
190
+ },
191
+ };
192
+
193
+ const instance = new NestLocalCallStrategy(
194
+ adapterHost,
195
+ clsService,
196
+ // @ts-expect-error minimal config for test
197
+ configService,
198
+ '',
199
+ {
200
+ internalRequestHeader: 'x-internal-token',
201
+ },
202
+ );
203
+
204
+ await instance.call('/test', {
205
+ method: 'GET',
206
+ });
207
+
208
+ expect(injectSpy).toHaveBeenCalledWith(
209
+ expect.objectContaining({
210
+ headers: expect.objectContaining({
211
+ 'x-internal-token': 'test-token-123',
212
+ }),
213
+ }),
214
+ );
215
+ });
216
+
217
+ it('should not override existing internal request token header', async () => {
218
+ const injectSpy = jest.fn().mockResolvedValue({
219
+ body: '{}',
220
+ json: () => ({}),
221
+ });
222
+ adapterHost = createMock<HttpAdapterHost>({
223
+ httpAdapter: {
224
+ getInstance: () =>
225
+ createMock<FastifyAdapter>({
226
+ inject: injectSpy as any,
227
+ }),
228
+ },
229
+ });
230
+
231
+ const configService = {
232
+ values: {
233
+ internalRequestToken: 'test-token-123',
234
+ },
235
+ };
236
+
237
+ const instance = new NestLocalCallStrategy(
238
+ adapterHost,
239
+ clsService,
240
+ // @ts-expect-error minimal config for test
241
+ configService,
242
+ '',
243
+ {
244
+ internalRequestHeader: 'x-internal-token',
245
+ },
246
+ );
247
+
248
+ await instance.call('/test', {
249
+ method: 'GET',
250
+ headers: {
251
+ 'x-internal-token': 'user-provided-token',
252
+ },
253
+ });
254
+
255
+ expect(injectSpy).toHaveBeenCalledWith(
256
+ expect.objectContaining({
257
+ headers: expect.objectContaining({
258
+ 'x-internal-token': 'user-provided-token',
259
+ }),
260
+ }),
261
+ );
262
+ });
263
+ });
@@ -1,55 +1,55 @@
1
- import {
2
- expect,
3
- jest,
4
- describe,
5
- it,
6
- beforeEach,
7
- beforeAll,
8
- afterAll,
9
- afterEach,
10
- } from '@jest/globals';
11
- import { createMock } from '@golevelup/ts-jest';
12
- import { EventEmitter2 } from '@nestjs/event-emitter';
13
- import {
14
- NestLocalEventStrategy,
15
- NestLocalEventStrategyProvider,
16
- } from '../strategies/nest-local-event.strategy.js';
17
-
18
- describe('NestLocalEventStrategy', () => {
19
- let eventEmitter: EventEmitter2;
20
-
21
- beforeEach(() => {
22
- eventEmitter = createMock<EventEmitter2>();
23
- });
24
-
25
- it('should be defined', () => {
26
- expect(NestLocalEventStrategy).toBeDefined();
27
- });
28
-
29
- it('should be instantiable', () => {
30
- const instance = new NestLocalEventStrategy(eventEmitter);
31
- expect(instance).toBeDefined();
32
- });
33
-
34
- it('should call method emit', () => {
35
- const instance = new NestLocalEventStrategy(eventEmitter);
36
- instance.emit('test', {});
37
- expect(eventEmitter.emit).toHaveBeenCalled();
38
- });
39
-
40
- it('should call method emit Async', () => {
41
- const instance = new NestLocalEventStrategy(eventEmitter);
42
- instance.emitAsync('test', {});
43
- expect(eventEmitter.emitAsync).toHaveBeenCalled();
44
- });
45
-
46
- it('should create a provider', () => {
47
- const provider = NestLocalEventStrategyProvider('test');
48
- expect(provider).toBeDefined();
49
- });
50
-
51
- it('should create a provider and execute the useFactory method', () => {
52
- const provider = NestLocalEventStrategyProvider('test');
53
- expect(provider.useFactory(eventEmitter)).toBeDefined();
54
- });
55
- });
1
+ import {
2
+ expect,
3
+ jest,
4
+ describe,
5
+ it,
6
+ beforeEach,
7
+ beforeAll,
8
+ afterAll,
9
+ afterEach,
10
+ } from '@jest/globals';
11
+ import { createMock } from '@golevelup/ts-jest';
12
+ import { EventEmitter2 } from '@nestjs/event-emitter';
13
+ import {
14
+ NestLocalEventStrategy,
15
+ NestLocalEventStrategyProvider,
16
+ } from '../strategies/nest-local-event.strategy.js';
17
+
18
+ describe('NestLocalEventStrategy', () => {
19
+ let eventEmitter: EventEmitter2;
20
+
21
+ beforeEach(() => {
22
+ eventEmitter = createMock<EventEmitter2>();
23
+ });
24
+
25
+ it('should be defined', () => {
26
+ expect(NestLocalEventStrategy).toBeDefined();
27
+ });
28
+
29
+ it('should be instantiable', () => {
30
+ const instance = new NestLocalEventStrategy(eventEmitter);
31
+ expect(instance).toBeDefined();
32
+ });
33
+
34
+ it('should call method emit', () => {
35
+ const instance = new NestLocalEventStrategy(eventEmitter);
36
+ instance.emit('test', {});
37
+ expect(eventEmitter.emit).toHaveBeenCalled();
38
+ });
39
+
40
+ it('should call method emit Async', () => {
41
+ const instance = new NestLocalEventStrategy(eventEmitter);
42
+ instance.emitAsync('test', {});
43
+ expect(eventEmitter.emitAsync).toHaveBeenCalled();
44
+ });
45
+
46
+ it('should create a provider', () => {
47
+ const provider = NestLocalEventStrategyProvider('test');
48
+ expect(provider).toBeDefined();
49
+ });
50
+
51
+ it('should create a provider and execute the useFactory method', () => {
52
+ const provider = NestLocalEventStrategyProvider('test');
53
+ expect(provider.useFactory(eventEmitter)).toBeDefined();
54
+ });
55
+ });