@nest-yalc-2/api-strategy 2.2.2 → 2.2.4
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/package.json +5 -3
- package/src/__tests__/call-strategy-wrappers.spec.ts +217 -217
- package/src/__tests__/context-call.service.spec.ts +52 -52
- package/src/__tests__/context-event.service.spec.ts +52 -52
- package/src/__tests__/event-strategy-composition.spec.ts +185 -185
- package/src/__tests__/header-whitelist.helper.spec.ts +41 -30
- package/src/__tests__/http-abstract-call.strategy.spec.ts +17 -17
- package/src/__tests__/nest-http-call.strategy.spec.ts +164 -164
- package/src/__tests__/nest-local-call.strategy.spec.ts +263 -263
- package/src/__tests__/nest-local-event.strategy.spec.ts +55 -55
- package/src/__tests__/rabbitmq-event.strategy.spec.ts +220 -220
- package/src/__tests__/strategy-selector.provider.spec.ts +182 -182
- package/src/context-call.interface.ts +49 -49
- package/src/context-call.service.ts +31 -31
- package/src/context-event.interface.ts +4 -4
- package/src/context-event.service.ts +31 -31
- package/src/header-whitelist.helper.ts +12 -12
- package/src/index.ts +17 -17
- package/src/strategies/composite-event.strategy.ts +76 -76
- package/src/strategies/conditional-call.strategy.ts +70 -70
- package/src/strategies/conditional-event.strategy.ts +54 -54
- package/src/strategies/fallback-call.strategy.ts +67 -67
- package/src/strategies/http-abstract-call.strategy.ts +238 -238
- package/src/strategies/nest-http-call.strategy.ts +121 -121
- package/src/strategies/nest-local-call.strategy.ts +153 -153
- package/src/strategies/nest-local-event.strategy.d.ts +1 -1
- package/src/strategies/nest-local-event.strategy.ts +42 -42
- package/src/strategies/rabbitmq-event.strategy.ts +154 -154
- package/src/strategies/shadow-call.strategy.ts +87 -87
- package/src/strategy-selector.provider.ts +103 -103
|
@@ -1,121 +1,121 @@
|
|
|
1
|
-
import { ClassType } from '@node-yalc/types/globals.js';
|
|
2
|
-
import { HttpService } from '@nestjs/axios';
|
|
3
|
-
import {
|
|
4
|
-
HttpAbstractStrategy,
|
|
5
|
-
IHttpCallStrategyResponse,
|
|
6
|
-
HttpOptions,
|
|
7
|
-
IHttpCallStrategyOptions,
|
|
8
|
-
} from './http-abstract-call.strategy.js';
|
|
9
|
-
import { AxiosRequestConfig } from 'axios';
|
|
10
|
-
import { YalcGlobalClsService } from '@nest-yalc-2/app/cls.module.js';
|
|
11
|
-
import { filterHeaders } from '../header-whitelist.helper.js';
|
|
12
|
-
import type { OutgoingHttpHeaders } from 'node:http2';
|
|
13
|
-
|
|
14
|
-
export type NestHttpCallStrategyOptions = IHttpCallStrategyOptions & {
|
|
15
|
-
internalRequestHeader?: string;
|
|
16
|
-
internalRequestToken?: string;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export class NestHttpCallStrategy extends HttpAbstractStrategy {
|
|
20
|
-
private readonly internalHeader: string;
|
|
21
|
-
private readonly internalToken?: string;
|
|
22
|
-
|
|
23
|
-
constructor(
|
|
24
|
-
protected readonly httpService: HttpService,
|
|
25
|
-
protected readonly clsService: YalcGlobalClsService,
|
|
26
|
-
private baseUrl = '',
|
|
27
|
-
protected readonly options: NestHttpCallStrategyOptions = {},
|
|
28
|
-
) {
|
|
29
|
-
super();
|
|
30
|
-
this.internalHeader =
|
|
31
|
-
options.internalRequestHeader ?? 'x-internal-request-token';
|
|
32
|
-
this.internalToken = options.internalRequestToken;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async call<TOptData, TParams extends Record<string, any>, TResData>(
|
|
36
|
-
path: string,
|
|
37
|
-
options?: HttpOptions<TOptData, TParams>,
|
|
38
|
-
): Promise<IHttpCallStrategyResponse<TResData>> {
|
|
39
|
-
const clsHeaders = filterHeaders(
|
|
40
|
-
this.clsService.get('headers'),
|
|
41
|
-
this.options.headersWhitelist,
|
|
42
|
-
);
|
|
43
|
-
const headers: Record<string, any> = {
|
|
44
|
-
...clsHeaders,
|
|
45
|
-
...(options?.headers ?? {}),
|
|
46
|
-
};
|
|
47
|
-
if (
|
|
48
|
-
this.internalHeader &&
|
|
49
|
-
this.internalToken &&
|
|
50
|
-
!headers[this.internalHeader]
|
|
51
|
-
) {
|
|
52
|
-
headers[this.internalHeader] = this.internalToken;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* We need this to do a type check on the options and
|
|
56
|
-
* implement the mapping from HttpOptions to AxiosRequestConfig
|
|
57
|
-
*/
|
|
58
|
-
const _options:
|
|
59
|
-
| {
|
|
60
|
-
[k: string | number | symbol]: never;
|
|
61
|
-
}
|
|
62
|
-
| AxiosRequestConfig = {
|
|
63
|
-
headers,
|
|
64
|
-
method: options?.method,
|
|
65
|
-
signal: options?.signal,
|
|
66
|
-
data: options?.data,
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
if (options?.parameters) {
|
|
70
|
-
_options.params = new URLSearchParams(options.parameters);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const { data, ...res } = await this.httpService.axiosRef.request({
|
|
74
|
-
headers: _options?.headers as any,
|
|
75
|
-
method: _options?.method,
|
|
76
|
-
signal: _options?.signal,
|
|
77
|
-
data: _options?.data,
|
|
78
|
-
params: _options?.params,
|
|
79
|
-
url: `${this.baseUrl}${path}`,
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
return {
|
|
83
|
-
...res,
|
|
84
|
-
headers: res.headers as OutgoingHttpHeaders,
|
|
85
|
-
data,
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export interface NestHttpCallStrategyProviderOptions {
|
|
91
|
-
baseUrl?: string;
|
|
92
|
-
NestHttpStrategy?: ClassType<NestHttpCallStrategy>;
|
|
93
|
-
headersWhitelist?: string[];
|
|
94
|
-
internalRequestHeader?: string;
|
|
95
|
-
internalRequestToken?: string;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Just a convenient provider to inject the NestLocalCallStrategy
|
|
100
|
-
*/
|
|
101
|
-
export const NestHttpCallStrategyProvider = (
|
|
102
|
-
provide: string,
|
|
103
|
-
options: NestHttpCallStrategyProviderOptions = {},
|
|
104
|
-
) => ({
|
|
105
|
-
provide,
|
|
106
|
-
useFactory: (httpAdapter: HttpService, clsService: YalcGlobalClsService) => {
|
|
107
|
-
const _options = {
|
|
108
|
-
baseUrl: '',
|
|
109
|
-
NestHttpStrategy: NestHttpCallStrategy,
|
|
110
|
-
...options,
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
return new _options.NestHttpStrategy(
|
|
114
|
-
httpAdapter,
|
|
115
|
-
clsService,
|
|
116
|
-
_options.baseUrl,
|
|
117
|
-
_options,
|
|
118
|
-
);
|
|
119
|
-
},
|
|
120
|
-
inject: [HttpService, YalcGlobalClsService],
|
|
121
|
-
});
|
|
1
|
+
import { ClassType } from '@node-yalc/types/globals.js';
|
|
2
|
+
import { HttpService } from '@nestjs/axios';
|
|
3
|
+
import {
|
|
4
|
+
HttpAbstractStrategy,
|
|
5
|
+
IHttpCallStrategyResponse,
|
|
6
|
+
HttpOptions,
|
|
7
|
+
IHttpCallStrategyOptions,
|
|
8
|
+
} from './http-abstract-call.strategy.js';
|
|
9
|
+
import { AxiosRequestConfig } from 'axios';
|
|
10
|
+
import { YalcGlobalClsService } from '@nest-yalc-2/app/cls.module.js';
|
|
11
|
+
import { filterHeaders } from '../header-whitelist.helper.js';
|
|
12
|
+
import type { OutgoingHttpHeaders } from 'node:http2';
|
|
13
|
+
|
|
14
|
+
export type NestHttpCallStrategyOptions = IHttpCallStrategyOptions & {
|
|
15
|
+
internalRequestHeader?: string;
|
|
16
|
+
internalRequestToken?: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export class NestHttpCallStrategy extends HttpAbstractStrategy {
|
|
20
|
+
private readonly internalHeader: string;
|
|
21
|
+
private readonly internalToken?: string;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
protected readonly httpService: HttpService,
|
|
25
|
+
protected readonly clsService: YalcGlobalClsService,
|
|
26
|
+
private baseUrl = '',
|
|
27
|
+
protected readonly options: NestHttpCallStrategyOptions = {},
|
|
28
|
+
) {
|
|
29
|
+
super();
|
|
30
|
+
this.internalHeader =
|
|
31
|
+
options.internalRequestHeader ?? 'x-internal-request-token';
|
|
32
|
+
this.internalToken = options.internalRequestToken;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async call<TOptData, TParams extends Record<string, any>, TResData>(
|
|
36
|
+
path: string,
|
|
37
|
+
options?: HttpOptions<TOptData, TParams>,
|
|
38
|
+
): Promise<IHttpCallStrategyResponse<TResData>> {
|
|
39
|
+
const clsHeaders = filterHeaders(
|
|
40
|
+
this.clsService.get('headers'),
|
|
41
|
+
this.options.headersWhitelist,
|
|
42
|
+
);
|
|
43
|
+
const headers: Record<string, any> = {
|
|
44
|
+
...clsHeaders,
|
|
45
|
+
...(options?.headers ?? {}),
|
|
46
|
+
};
|
|
47
|
+
if (
|
|
48
|
+
this.internalHeader &&
|
|
49
|
+
this.internalToken &&
|
|
50
|
+
!headers[this.internalHeader]
|
|
51
|
+
) {
|
|
52
|
+
headers[this.internalHeader] = this.internalToken;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* We need this to do a type check on the options and
|
|
56
|
+
* implement the mapping from HttpOptions to AxiosRequestConfig
|
|
57
|
+
*/
|
|
58
|
+
const _options:
|
|
59
|
+
| {
|
|
60
|
+
[k: string | number | symbol]: never;
|
|
61
|
+
}
|
|
62
|
+
| AxiosRequestConfig = {
|
|
63
|
+
headers,
|
|
64
|
+
method: options?.method,
|
|
65
|
+
signal: options?.signal,
|
|
66
|
+
data: options?.data,
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
if (options?.parameters) {
|
|
70
|
+
_options.params = new URLSearchParams(options.parameters);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const { data, ...res } = await this.httpService.axiosRef.request({
|
|
74
|
+
headers: _options?.headers as any,
|
|
75
|
+
method: _options?.method,
|
|
76
|
+
signal: _options?.signal,
|
|
77
|
+
data: _options?.data,
|
|
78
|
+
params: _options?.params,
|
|
79
|
+
url: `${this.baseUrl}${path}`,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
...res,
|
|
84
|
+
headers: res.headers as OutgoingHttpHeaders,
|
|
85
|
+
data,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface NestHttpCallStrategyProviderOptions {
|
|
91
|
+
baseUrl?: string;
|
|
92
|
+
NestHttpStrategy?: ClassType<NestHttpCallStrategy>;
|
|
93
|
+
headersWhitelist?: string[];
|
|
94
|
+
internalRequestHeader?: string;
|
|
95
|
+
internalRequestToken?: string;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Just a convenient provider to inject the NestLocalCallStrategy
|
|
100
|
+
*/
|
|
101
|
+
export const NestHttpCallStrategyProvider = (
|
|
102
|
+
provide: string,
|
|
103
|
+
options: NestHttpCallStrategyProviderOptions = {},
|
|
104
|
+
) => ({
|
|
105
|
+
provide,
|
|
106
|
+
useFactory: (httpAdapter: HttpService, clsService: YalcGlobalClsService) => {
|
|
107
|
+
const _options = {
|
|
108
|
+
baseUrl: '',
|
|
109
|
+
NestHttpStrategy: NestHttpCallStrategy,
|
|
110
|
+
...options,
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
return new _options.NestHttpStrategy(
|
|
114
|
+
httpAdapter,
|
|
115
|
+
clsService,
|
|
116
|
+
_options.baseUrl,
|
|
117
|
+
_options,
|
|
118
|
+
);
|
|
119
|
+
},
|
|
120
|
+
inject: [HttpService, YalcGlobalClsService],
|
|
121
|
+
});
|
|
@@ -1,153 +1,153 @@
|
|
|
1
|
-
import { HttpAdapterHost } from '@nestjs/core';
|
|
2
|
-
import {
|
|
3
|
-
HttpAbstractStrategy,
|
|
4
|
-
HttpOptions,
|
|
5
|
-
IHttpCallStrategyOptions,
|
|
6
|
-
IHttpCallStrategyResponse,
|
|
7
|
-
} from './http-abstract-call.strategy.js';
|
|
8
|
-
import { FastifyAdapter } from '@nestjs/platform-fastify';
|
|
9
|
-
import { ClassType } from '@node-yalc/types/globals.js';
|
|
10
|
-
import { InjectOptions } from 'fastify';
|
|
11
|
-
import { YalcGlobalClsService } from '@nest-yalc-2/app/cls.module.js';
|
|
12
|
-
import { filterHeaders } from '../header-whitelist.helper.js';
|
|
13
|
-
import { AppConfigService } from '@nest-yalc-2/app/app-config.service.js';
|
|
14
|
-
import { MAIN_APP_CONFIG_SERVICE } from '@nest-yalc-2/app/def.const.js';
|
|
15
|
-
|
|
16
|
-
export type NestLocalCallStrategyOptions = IHttpCallStrategyOptions & {
|
|
17
|
-
internalRequestHeader?: string;
|
|
18
|
-
internalRequestToken?: string;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export class NestLocalCallStrategy extends HttpAbstractStrategy {
|
|
22
|
-
private readonly internalHeader: string;
|
|
23
|
-
private readonly internalToken?: string;
|
|
24
|
-
|
|
25
|
-
constructor(
|
|
26
|
-
protected readonly adapterHost: HttpAdapterHost,
|
|
27
|
-
protected readonly clsService: YalcGlobalClsService,
|
|
28
|
-
protected readonly configService: AppConfigService,
|
|
29
|
-
private baseUrl = '',
|
|
30
|
-
protected readonly options: NestLocalCallStrategyOptions = {},
|
|
31
|
-
) {
|
|
32
|
-
super();
|
|
33
|
-
this.internalHeader =
|
|
34
|
-
options.internalRequestHeader ?? 'x-internal-request-token';
|
|
35
|
-
this.internalToken =
|
|
36
|
-
options.internalRequestToken ??
|
|
37
|
-
(configService as any)?.values?.internalRequestToken;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async call<
|
|
41
|
-
TOptData extends string | object | Buffer | NodeJS.ReadableStream,
|
|
42
|
-
TParams extends Record<string, any>,
|
|
43
|
-
TResData,
|
|
44
|
-
>(
|
|
45
|
-
path: string,
|
|
46
|
-
options?: HttpOptions<TOptData, TParams>,
|
|
47
|
-
): Promise<IHttpCallStrategyResponse<TResData>> {
|
|
48
|
-
const instance: FastifyAdapter = this.adapterHost.httpAdapter.getInstance();
|
|
49
|
-
const clsHeaders = filterHeaders(
|
|
50
|
-
this.clsService.get('headers'),
|
|
51
|
-
this.options.headersWhitelist,
|
|
52
|
-
);
|
|
53
|
-
const headers: Record<string, any> = {
|
|
54
|
-
...clsHeaders,
|
|
55
|
-
...(options?.headers ?? {}),
|
|
56
|
-
};
|
|
57
|
-
if (
|
|
58
|
-
this.internalHeader &&
|
|
59
|
-
this.internalToken &&
|
|
60
|
-
!headers[this.internalHeader]
|
|
61
|
-
) {
|
|
62
|
-
headers[this.internalHeader] = this.internalToken;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* We need this to do a type check on the options and
|
|
66
|
-
* implement the mapping from HttpOptions to InjectOptions;
|
|
67
|
-
*/
|
|
68
|
-
const _options:
|
|
69
|
-
| {
|
|
70
|
-
[k: string | number | symbol]: never;
|
|
71
|
-
}
|
|
72
|
-
| InjectOptions = {
|
|
73
|
-
headers,
|
|
74
|
-
method: options?.method,
|
|
75
|
-
/**@todo investigate where to set thi */
|
|
76
|
-
// signal: options?.signal,
|
|
77
|
-
payload: options?.data, // map data to payload
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
const args: InjectOptions = {
|
|
81
|
-
..._options,
|
|
82
|
-
url: `${this.baseUrl}${path}`,
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
if (options?.parameters) {
|
|
86
|
-
args.query = Object.fromEntries(new URLSearchParams(options.parameters));
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const result = await instance.inject(args);
|
|
90
|
-
|
|
91
|
-
let data;
|
|
92
|
-
try {
|
|
93
|
-
if (
|
|
94
|
-
this.options.shouldSkipJsonParse &&
|
|
95
|
-
this.options.shouldSkipJsonParse(result.body)
|
|
96
|
-
) {
|
|
97
|
-
data = result.body;
|
|
98
|
-
} else {
|
|
99
|
-
data = result.json();
|
|
100
|
-
}
|
|
101
|
-
} catch (_e) {
|
|
102
|
-
//The content-type of the response is not application/json
|
|
103
|
-
// if so, we use the body instead
|
|
104
|
-
data = result.body;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return {
|
|
108
|
-
data,
|
|
109
|
-
headers: result.headers,
|
|
110
|
-
status: result.statusCode,
|
|
111
|
-
statusText: result.statusMessage,
|
|
112
|
-
request: result.payload, // TODO: double check if it's the correct value
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export interface NestLocalCallStrategyProviderOptions {
|
|
118
|
-
baseUrl?: string;
|
|
119
|
-
NestLocalStrategy?: ClassType<NestLocalCallStrategy>;
|
|
120
|
-
headersWhitelist?: string[];
|
|
121
|
-
internalRequestHeader?: string;
|
|
122
|
-
internalRequestToken?: string;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Just a convenient provider to inject the NestLocalCallStrategy
|
|
127
|
-
*/
|
|
128
|
-
export const NestLocalCallStrategyProvider = (
|
|
129
|
-
provide: string,
|
|
130
|
-
options: NestLocalCallStrategyProviderOptions = {},
|
|
131
|
-
) => ({
|
|
132
|
-
provide,
|
|
133
|
-
useFactory: (
|
|
134
|
-
httpAdapter: HttpAdapterHost,
|
|
135
|
-
clsService: YalcGlobalClsService,
|
|
136
|
-
configService: AppConfigService,
|
|
137
|
-
) => {
|
|
138
|
-
const _options = {
|
|
139
|
-
baseUrl: '',
|
|
140
|
-
NestLocalStrategy: NestLocalCallStrategy,
|
|
141
|
-
...options,
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
return new _options.NestLocalStrategy(
|
|
145
|
-
httpAdapter,
|
|
146
|
-
clsService,
|
|
147
|
-
configService,
|
|
148
|
-
_options.baseUrl,
|
|
149
|
-
_options,
|
|
150
|
-
);
|
|
151
|
-
},
|
|
152
|
-
inject: [HttpAdapterHost, YalcGlobalClsService, MAIN_APP_CONFIG_SERVICE],
|
|
153
|
-
});
|
|
1
|
+
import { HttpAdapterHost } from '@nestjs/core';
|
|
2
|
+
import {
|
|
3
|
+
HttpAbstractStrategy,
|
|
4
|
+
HttpOptions,
|
|
5
|
+
IHttpCallStrategyOptions,
|
|
6
|
+
IHttpCallStrategyResponse,
|
|
7
|
+
} from './http-abstract-call.strategy.js';
|
|
8
|
+
import { FastifyAdapter } from '@nestjs/platform-fastify';
|
|
9
|
+
import { ClassType } from '@node-yalc/types/globals.js';
|
|
10
|
+
import { InjectOptions } from 'fastify';
|
|
11
|
+
import { YalcGlobalClsService } from '@nest-yalc-2/app/cls.module.js';
|
|
12
|
+
import { filterHeaders } from '../header-whitelist.helper.js';
|
|
13
|
+
import { AppConfigService } from '@nest-yalc-2/app/app-config.service.js';
|
|
14
|
+
import { MAIN_APP_CONFIG_SERVICE } from '@nest-yalc-2/app/def.const.js';
|
|
15
|
+
|
|
16
|
+
export type NestLocalCallStrategyOptions = IHttpCallStrategyOptions & {
|
|
17
|
+
internalRequestHeader?: string;
|
|
18
|
+
internalRequestToken?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export class NestLocalCallStrategy extends HttpAbstractStrategy {
|
|
22
|
+
private readonly internalHeader: string;
|
|
23
|
+
private readonly internalToken?: string;
|
|
24
|
+
|
|
25
|
+
constructor(
|
|
26
|
+
protected readonly adapterHost: HttpAdapterHost,
|
|
27
|
+
protected readonly clsService: YalcGlobalClsService,
|
|
28
|
+
protected readonly configService: AppConfigService,
|
|
29
|
+
private baseUrl = '',
|
|
30
|
+
protected readonly options: NestLocalCallStrategyOptions = {},
|
|
31
|
+
) {
|
|
32
|
+
super();
|
|
33
|
+
this.internalHeader =
|
|
34
|
+
options.internalRequestHeader ?? 'x-internal-request-token';
|
|
35
|
+
this.internalToken =
|
|
36
|
+
options.internalRequestToken ??
|
|
37
|
+
(configService as any)?.values?.internalRequestToken;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async call<
|
|
41
|
+
TOptData extends string | object | Buffer | NodeJS.ReadableStream,
|
|
42
|
+
TParams extends Record<string, any>,
|
|
43
|
+
TResData,
|
|
44
|
+
>(
|
|
45
|
+
path: string,
|
|
46
|
+
options?: HttpOptions<TOptData, TParams>,
|
|
47
|
+
): Promise<IHttpCallStrategyResponse<TResData>> {
|
|
48
|
+
const instance: FastifyAdapter = this.adapterHost.httpAdapter.getInstance();
|
|
49
|
+
const clsHeaders = filterHeaders(
|
|
50
|
+
this.clsService.get('headers'),
|
|
51
|
+
this.options.headersWhitelist,
|
|
52
|
+
);
|
|
53
|
+
const headers: Record<string, any> = {
|
|
54
|
+
...clsHeaders,
|
|
55
|
+
...(options?.headers ?? {}),
|
|
56
|
+
};
|
|
57
|
+
if (
|
|
58
|
+
this.internalHeader &&
|
|
59
|
+
this.internalToken &&
|
|
60
|
+
!headers[this.internalHeader]
|
|
61
|
+
) {
|
|
62
|
+
headers[this.internalHeader] = this.internalToken;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* We need this to do a type check on the options and
|
|
66
|
+
* implement the mapping from HttpOptions to InjectOptions;
|
|
67
|
+
*/
|
|
68
|
+
const _options:
|
|
69
|
+
| {
|
|
70
|
+
[k: string | number | symbol]: never;
|
|
71
|
+
}
|
|
72
|
+
| InjectOptions = {
|
|
73
|
+
headers,
|
|
74
|
+
method: options?.method,
|
|
75
|
+
/**@todo investigate where to set thi */
|
|
76
|
+
// signal: options?.signal,
|
|
77
|
+
payload: options?.data, // map data to payload
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const args: InjectOptions = {
|
|
81
|
+
..._options,
|
|
82
|
+
url: `${this.baseUrl}${path}`,
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
if (options?.parameters) {
|
|
86
|
+
args.query = Object.fromEntries(new URLSearchParams(options.parameters));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const result = await instance.inject(args);
|
|
90
|
+
|
|
91
|
+
let data;
|
|
92
|
+
try {
|
|
93
|
+
if (
|
|
94
|
+
this.options.shouldSkipJsonParse &&
|
|
95
|
+
this.options.shouldSkipJsonParse(result.body)
|
|
96
|
+
) {
|
|
97
|
+
data = result.body;
|
|
98
|
+
} else {
|
|
99
|
+
data = result.json();
|
|
100
|
+
}
|
|
101
|
+
} catch (_e) {
|
|
102
|
+
//The content-type of the response is not application/json
|
|
103
|
+
// if so, we use the body instead
|
|
104
|
+
data = result.body;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
data,
|
|
109
|
+
headers: result.headers,
|
|
110
|
+
status: result.statusCode,
|
|
111
|
+
statusText: result.statusMessage,
|
|
112
|
+
request: result.payload, // TODO: double check if it's the correct value
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface NestLocalCallStrategyProviderOptions {
|
|
118
|
+
baseUrl?: string;
|
|
119
|
+
NestLocalStrategy?: ClassType<NestLocalCallStrategy>;
|
|
120
|
+
headersWhitelist?: string[];
|
|
121
|
+
internalRequestHeader?: string;
|
|
122
|
+
internalRequestToken?: string;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Just a convenient provider to inject the NestLocalCallStrategy
|
|
127
|
+
*/
|
|
128
|
+
export const NestLocalCallStrategyProvider = (
|
|
129
|
+
provide: string,
|
|
130
|
+
options: NestLocalCallStrategyProviderOptions = {},
|
|
131
|
+
) => ({
|
|
132
|
+
provide,
|
|
133
|
+
useFactory: (
|
|
134
|
+
httpAdapter: HttpAdapterHost,
|
|
135
|
+
clsService: YalcGlobalClsService,
|
|
136
|
+
configService: AppConfigService,
|
|
137
|
+
) => {
|
|
138
|
+
const _options = {
|
|
139
|
+
baseUrl: '',
|
|
140
|
+
NestLocalStrategy: NestLocalCallStrategy,
|
|
141
|
+
...options,
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
return new _options.NestLocalStrategy(
|
|
145
|
+
httpAdapter,
|
|
146
|
+
clsService,
|
|
147
|
+
configService,
|
|
148
|
+
_options.baseUrl,
|
|
149
|
+
_options,
|
|
150
|
+
);
|
|
151
|
+
},
|
|
152
|
+
inject: [HttpAdapterHost, YalcGlobalClsService, MAIN_APP_CONFIG_SERVICE],
|
|
153
|
+
});
|
|
@@ -13,5 +13,5 @@ export interface NestLocalEventStrategyProviderOptions {
|
|
|
13
13
|
export declare const NestLocalEventStrategyProvider: (provide: string, options?: NestLocalEventStrategyProviderOptions) => {
|
|
14
14
|
provide: string;
|
|
15
15
|
useFactory: (eventEmitter: EventEmitter2) => NestLocalEventStrategy<any, any>;
|
|
16
|
-
inject:
|
|
16
|
+
inject: typeof import("eventemitter2").EventEmitter2[];
|
|
17
17
|
};
|
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
import { IEventStrategy } from '../context-event.interface.js';
|
|
2
|
-
import { EventEmitter2 } from '@nestjs/event-emitter';
|
|
3
|
-
import { ClassType } from '@node-yalc/types/globals.js';
|
|
4
|
-
|
|
5
|
-
export class NestLocalEventStrategy<
|
|
6
|
-
P = any,
|
|
7
|
-
O = any,
|
|
8
|
-
> implements IEventStrategy {
|
|
9
|
-
constructor(private eventEmitter: EventEmitter2) {}
|
|
10
|
-
|
|
11
|
-
emit(path: string, payload: P, options?: O): boolean {
|
|
12
|
-
return this.eventEmitter.emit(path, payload, options);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
emitAsync(path: string, payload: P, options?: O): Promise<any> {
|
|
16
|
-
return this.eventEmitter.emitAsync(path, payload, options);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface NestLocalEventStrategyProviderOptions {
|
|
21
|
-
NestLocalStrategy?: ClassType<NestLocalEventStrategy>;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Just a convenient provider to inject the NestLocalCallStrategy
|
|
26
|
-
*/
|
|
27
|
-
export const NestLocalEventStrategyProvider = (
|
|
28
|
-
provide: string,
|
|
29
|
-
options: NestLocalEventStrategyProviderOptions = {},
|
|
30
|
-
) => ({
|
|
31
|
-
provide,
|
|
32
|
-
useFactory: (eventEmitter: EventEmitter2) => {
|
|
33
|
-
const _options = {
|
|
34
|
-
baseUrl: '',
|
|
35
|
-
NestLocalStrategy: NestLocalEventStrategy,
|
|
36
|
-
...options,
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
return new _options.NestLocalStrategy(eventEmitter);
|
|
40
|
-
},
|
|
41
|
-
inject: [EventEmitter2],
|
|
42
|
-
});
|
|
1
|
+
import { IEventStrategy } from '../context-event.interface.js';
|
|
2
|
+
import { EventEmitter2 } from '@nestjs/event-emitter';
|
|
3
|
+
import { ClassType } from '@node-yalc/types/globals.js';
|
|
4
|
+
|
|
5
|
+
export class NestLocalEventStrategy<
|
|
6
|
+
P = any,
|
|
7
|
+
O = any,
|
|
8
|
+
> implements IEventStrategy {
|
|
9
|
+
constructor(private eventEmitter: EventEmitter2) {}
|
|
10
|
+
|
|
11
|
+
emit(path: string, payload: P, options?: O): boolean {
|
|
12
|
+
return this.eventEmitter.emit(path, payload, options);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
emitAsync(path: string, payload: P, options?: O): Promise<any> {
|
|
16
|
+
return this.eventEmitter.emitAsync(path, payload, options);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface NestLocalEventStrategyProviderOptions {
|
|
21
|
+
NestLocalStrategy?: ClassType<NestLocalEventStrategy>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Just a convenient provider to inject the NestLocalCallStrategy
|
|
26
|
+
*/
|
|
27
|
+
export const NestLocalEventStrategyProvider = (
|
|
28
|
+
provide: string,
|
|
29
|
+
options: NestLocalEventStrategyProviderOptions = {},
|
|
30
|
+
) => ({
|
|
31
|
+
provide,
|
|
32
|
+
useFactory: (eventEmitter: EventEmitter2) => {
|
|
33
|
+
const _options = {
|
|
34
|
+
baseUrl: '',
|
|
35
|
+
NestLocalStrategy: NestLocalEventStrategy,
|
|
36
|
+
...options,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
return new _options.NestLocalStrategy(eventEmitter);
|
|
40
|
+
},
|
|
41
|
+
inject: [EventEmitter2],
|
|
42
|
+
});
|