@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,164 +1,164 @@
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 } from '@golevelup/ts-jest';
12
- import { HttpService } from '@nestjs/axios';
13
- import { AxiosInstance } from 'axios';
14
- import {
15
- NestHttpCallStrategy,
16
- NestHttpCallStrategyProvider,
17
- } from '../strategies/nest-http-call.strategy.js';
18
- import { YalcGlobalClsService } from '../../../app/src/cls.module.js';
19
-
20
- describe('NestHttpCallStrategy', () => {
21
- let httpService: HttpService;
22
- let clsService: DeepMocked<YalcGlobalClsService>;
23
-
24
- beforeEach(() => {
25
- const axiosRef = createMock<AxiosInstance>();
26
- httpService = createMock<HttpService>({
27
- axiosRef,
28
- });
29
-
30
- clsService = createMock<YalcGlobalClsService>();
31
- });
32
-
33
- it('should be defined', () => {
34
- expect(NestHttpCallStrategy).toBeDefined();
35
- });
36
-
37
- it('should be instantiable', () => {
38
- const instance = new NestHttpCallStrategy(httpService, clsService);
39
- expect(instance).toBeDefined();
40
- });
41
-
42
- it('should be able to execute the call method', async () => {
43
- const instance = new NestHttpCallStrategy(httpService, clsService);
44
- const result = await instance.call('http://localhost:3000', {
45
- method: 'GET',
46
- });
47
- expect(result).toBeDefined();
48
- });
49
-
50
- it('should be able to execute the get method', async () => {
51
- const instance = new NestHttpCallStrategy(httpService, clsService);
52
- const result = await instance.get('http://localhost:3000');
53
- expect(result).toBeDefined();
54
- });
55
-
56
- it('should be able to execute the post method', async () => {
57
- const instance = new NestHttpCallStrategy(httpService, clsService);
58
- const result = await instance.post('http://localhost:3000', {});
59
- expect(result).toBeDefined();
60
- });
61
-
62
- it('should create a provider', () => {
63
- const provider = NestHttpCallStrategyProvider('test');
64
- expect(provider).toBeDefined();
65
- });
66
-
67
- it('should create a provider and execute the useFactory method', () => {
68
- const provider = NestHttpCallStrategyProvider('test');
69
- expect(provider.useFactory(httpService, clsService)).toBeDefined();
70
- });
71
-
72
- it('should be able to execute the call method and return a json', async () => {
73
- const axiosRef = createMock<AxiosInstance>({
74
- request: (config: any) => ({ data: '{}' }),
75
- });
76
- httpService = createMock<HttpService>({
77
- axiosRef,
78
- });
79
-
80
- const instance = new NestHttpCallStrategy(httpService, clsService);
81
- const result = await instance.call('http://localhost:3000', {
82
- method: 'GET',
83
- });
84
- expect(result).toBeDefined();
85
- });
86
-
87
- it('should be able to execute the call method with parameters', async () => {
88
- const instance = new NestHttpCallStrategy(httpService, clsService);
89
- const result = await instance.call('http://localhost:3000', {
90
- method: 'GET',
91
- parameters: {
92
- test1: { test: 'test' },
93
- },
94
- });
95
- expect(result).toBeDefined();
96
- });
97
-
98
- it('should add internal request token header when provided', async () => {
99
- const requestSpy = jest.fn().mockResolvedValue({ data: '{}' });
100
- const axiosRef = createMock<AxiosInstance>({
101
- request: requestSpy as any,
102
- });
103
- httpService = createMock<HttpService>({
104
- axiosRef,
105
- });
106
-
107
- const instance = new NestHttpCallStrategy(
108
- httpService,
109
- clsService,
110
- 'http://localhost:3000',
111
- {
112
- internalRequestHeader: 'x-internal-token',
113
- internalRequestToken: 'test-token-123',
114
- },
115
- );
116
-
117
- await instance.call('/test', {
118
- method: 'GET',
119
- });
120
-
121
- expect(requestSpy).toHaveBeenCalledWith(
122
- expect.objectContaining({
123
- headers: expect.objectContaining({
124
- 'x-internal-token': 'test-token-123',
125
- }),
126
- }),
127
- );
128
- });
129
-
130
- it('should not override existing internal request token header', async () => {
131
- const requestSpy = jest.fn().mockResolvedValue({ data: '{}' });
132
- const axiosRef = createMock<AxiosInstance>({
133
- request: requestSpy as any,
134
- });
135
- httpService = createMock<HttpService>({
136
- axiosRef,
137
- });
138
-
139
- const instance = new NestHttpCallStrategy(
140
- httpService,
141
- clsService,
142
- 'http://localhost:3000',
143
- {
144
- internalRequestHeader: 'x-internal-token',
145
- internalRequestToken: 'test-token-123',
146
- },
147
- );
148
-
149
- await instance.call('/test', {
150
- method: 'GET',
151
- headers: {
152
- 'x-internal-token': 'user-provided-token',
153
- },
154
- });
155
-
156
- expect(requestSpy).toHaveBeenCalledWith(
157
- expect.objectContaining({
158
- headers: expect.objectContaining({
159
- 'x-internal-token': 'user-provided-token',
160
- }),
161
- }),
162
- );
163
- });
164
- });
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 } from '@golevelup/ts-jest';
12
+ import { HttpService } from '@nestjs/axios';
13
+ import { AxiosInstance } from 'axios';
14
+ import {
15
+ NestHttpCallStrategy,
16
+ NestHttpCallStrategyProvider,
17
+ } from '../strategies/nest-http-call.strategy.js';
18
+ import { YalcGlobalClsService } from '../../../app/src/cls.module.js';
19
+
20
+ describe('NestHttpCallStrategy', () => {
21
+ let httpService: HttpService;
22
+ let clsService: DeepMocked<YalcGlobalClsService>;
23
+
24
+ beforeEach(() => {
25
+ const axiosRef = createMock<AxiosInstance>();
26
+ httpService = createMock<HttpService>({
27
+ axiosRef,
28
+ });
29
+
30
+ clsService = createMock<YalcGlobalClsService>();
31
+ });
32
+
33
+ it('should be defined', () => {
34
+ expect(NestHttpCallStrategy).toBeDefined();
35
+ });
36
+
37
+ it('should be instantiable', () => {
38
+ const instance = new NestHttpCallStrategy(httpService, clsService);
39
+ expect(instance).toBeDefined();
40
+ });
41
+
42
+ it('should be able to execute the call method', async () => {
43
+ const instance = new NestHttpCallStrategy(httpService, clsService);
44
+ const result = await instance.call('http://localhost:3000', {
45
+ method: 'GET',
46
+ });
47
+ expect(result).toBeDefined();
48
+ });
49
+
50
+ it('should be able to execute the get method', async () => {
51
+ const instance = new NestHttpCallStrategy(httpService, clsService);
52
+ const result = await instance.get('http://localhost:3000');
53
+ expect(result).toBeDefined();
54
+ });
55
+
56
+ it('should be able to execute the post method', async () => {
57
+ const instance = new NestHttpCallStrategy(httpService, clsService);
58
+ const result = await instance.post('http://localhost:3000', {});
59
+ expect(result).toBeDefined();
60
+ });
61
+
62
+ it('should create a provider', () => {
63
+ const provider = NestHttpCallStrategyProvider('test');
64
+ expect(provider).toBeDefined();
65
+ });
66
+
67
+ it('should create a provider and execute the useFactory method', () => {
68
+ const provider = NestHttpCallStrategyProvider('test');
69
+ expect(provider.useFactory(httpService, clsService)).toBeDefined();
70
+ });
71
+
72
+ it('should be able to execute the call method and return a json', async () => {
73
+ const axiosRef = createMock<AxiosInstance>({
74
+ request: (config: any) => ({ data: '{}' }),
75
+ });
76
+ httpService = createMock<HttpService>({
77
+ axiosRef,
78
+ });
79
+
80
+ const instance = new NestHttpCallStrategy(httpService, clsService);
81
+ const result = await instance.call('http://localhost:3000', {
82
+ method: 'GET',
83
+ });
84
+ expect(result).toBeDefined();
85
+ });
86
+
87
+ it('should be able to execute the call method with parameters', async () => {
88
+ const instance = new NestHttpCallStrategy(httpService, clsService);
89
+ const result = await instance.call('http://localhost:3000', {
90
+ method: 'GET',
91
+ parameters: {
92
+ test1: { test: 'test' },
93
+ },
94
+ });
95
+ expect(result).toBeDefined();
96
+ });
97
+
98
+ it('should add internal request token header when provided', async () => {
99
+ const requestSpy = jest.fn().mockResolvedValue({ data: '{}' });
100
+ const axiosRef = createMock<AxiosInstance>({
101
+ request: requestSpy as any,
102
+ });
103
+ httpService = createMock<HttpService>({
104
+ axiosRef,
105
+ });
106
+
107
+ const instance = new NestHttpCallStrategy(
108
+ httpService,
109
+ clsService,
110
+ 'http://localhost:3000',
111
+ {
112
+ internalRequestHeader: 'x-internal-token',
113
+ internalRequestToken: 'test-token-123',
114
+ },
115
+ );
116
+
117
+ await instance.call('/test', {
118
+ method: 'GET',
119
+ });
120
+
121
+ expect(requestSpy).toHaveBeenCalledWith(
122
+ expect.objectContaining({
123
+ headers: expect.objectContaining({
124
+ 'x-internal-token': 'test-token-123',
125
+ }),
126
+ }),
127
+ );
128
+ });
129
+
130
+ it('should not override existing internal request token header', async () => {
131
+ const requestSpy = jest.fn().mockResolvedValue({ data: '{}' });
132
+ const axiosRef = createMock<AxiosInstance>({
133
+ request: requestSpy as any,
134
+ });
135
+ httpService = createMock<HttpService>({
136
+ axiosRef,
137
+ });
138
+
139
+ const instance = new NestHttpCallStrategy(
140
+ httpService,
141
+ clsService,
142
+ 'http://localhost:3000',
143
+ {
144
+ internalRequestHeader: 'x-internal-token',
145
+ internalRequestToken: 'test-token-123',
146
+ },
147
+ );
148
+
149
+ await instance.call('/test', {
150
+ method: 'GET',
151
+ headers: {
152
+ 'x-internal-token': 'user-provided-token',
153
+ },
154
+ });
155
+
156
+ expect(requestSpy).toHaveBeenCalledWith(
157
+ expect.objectContaining({
158
+ headers: expect.objectContaining({
159
+ 'x-internal-token': 'user-provided-token',
160
+ }),
161
+ }),
162
+ );
163
+ });
164
+ });