@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,103 +1,103 @@
|
|
|
1
|
-
import type { Provider } from '@nestjs/common';
|
|
2
|
-
import type { IApiCallStrategy } from './context-call.interface.js';
|
|
3
|
-
import type { IEventStrategy } from './context-event.interface.js';
|
|
4
|
-
|
|
5
|
-
export type StrategySelectorToken<TStrategy = unknown> =
|
|
6
|
-
| string
|
|
7
|
-
| symbol
|
|
8
|
-
| (abstract new (...args: any[]) => TStrategy)
|
|
9
|
-
| (new (...args: any[]) => TStrategy);
|
|
10
|
-
|
|
11
|
-
export type StrategySelectorBehavior = 'throw' | 'fallback';
|
|
12
|
-
|
|
13
|
-
export interface StrategySelectorFactoryOptions {
|
|
14
|
-
inject?: any[];
|
|
15
|
-
useFactory?: (
|
|
16
|
-
...args: any[]
|
|
17
|
-
) => string | null | undefined | Promise<string | null | undefined>;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface StrategySelectorProviderOptions<TStrategy> {
|
|
21
|
-
provide: StrategySelectorToken<TStrategy>;
|
|
22
|
-
defaultStrategy: string;
|
|
23
|
-
strategies: Record<string, StrategySelectorToken<TStrategy>>;
|
|
24
|
-
selector?: StrategySelectorFactoryOptions;
|
|
25
|
-
unknownStrategyBehavior?: StrategySelectorBehavior;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function normalizeStrategyKey(strategy: string | null | undefined) {
|
|
29
|
-
const value = strategy?.trim();
|
|
30
|
-
return value ? value : undefined;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function StrategySelectorProvider<TStrategy>(
|
|
34
|
-
options: StrategySelectorProviderOptions<TStrategy>,
|
|
35
|
-
): Provider<TStrategy> {
|
|
36
|
-
const strategyEntries = Object.entries(options.strategies);
|
|
37
|
-
|
|
38
|
-
if (strategyEntries.length === 0) {
|
|
39
|
-
throw new Error('Strategy selector requires at least one strategy.');
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const defaultStrategyIndex = strategyEntries.findIndex(
|
|
43
|
-
([key]) => key === options.defaultStrategy,
|
|
44
|
-
);
|
|
45
|
-
|
|
46
|
-
if (defaultStrategyIndex < 0) {
|
|
47
|
-
throw new Error(
|
|
48
|
-
`Default strategy "${options.defaultStrategy}" is not registered.`,
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const behavior = options.unknownStrategyBehavior ?? 'throw';
|
|
53
|
-
const selectorInject = options.selector?.inject ?? [];
|
|
54
|
-
|
|
55
|
-
return {
|
|
56
|
-
provide: options.provide,
|
|
57
|
-
useFactory: async (...args: any[]) => {
|
|
58
|
-
const strategyInstances = args.slice(
|
|
59
|
-
0,
|
|
60
|
-
strategyEntries.length,
|
|
61
|
-
) as TStrategy[];
|
|
62
|
-
const selectorArgs = args.slice(strategyEntries.length);
|
|
63
|
-
const configuredStrategy = normalizeStrategyKey(
|
|
64
|
-
await options.selector?.useFactory?.(...selectorArgs),
|
|
65
|
-
);
|
|
66
|
-
const selectedStrategy = configuredStrategy ?? options.defaultStrategy;
|
|
67
|
-
const selectedStrategyIndex = strategyEntries.findIndex(
|
|
68
|
-
([key]) => key === selectedStrategy,
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
if (selectedStrategyIndex >= 0) {
|
|
72
|
-
return strategyInstances[selectedStrategyIndex];
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (behavior === 'fallback') {
|
|
76
|
-
return strategyInstances[defaultStrategyIndex];
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const availableStrategies = strategyEntries
|
|
80
|
-
.map(([key]) => key)
|
|
81
|
-
.join(', ');
|
|
82
|
-
throw new Error(
|
|
83
|
-
`Unknown strategy "${selectedStrategy}". Available strategies: ${availableStrategies}.`,
|
|
84
|
-
);
|
|
85
|
-
},
|
|
86
|
-
inject: [
|
|
87
|
-
...strategyEntries.map(([, strategyToken]) => strategyToken),
|
|
88
|
-
...selectorInject,
|
|
89
|
-
],
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export function ApiCallStrategySelectorProvider(
|
|
94
|
-
options: StrategySelectorProviderOptions<IApiCallStrategy>,
|
|
95
|
-
): Provider<IApiCallStrategy> {
|
|
96
|
-
return StrategySelectorProvider(options);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export function EventStrategySelectorProvider(
|
|
100
|
-
options: StrategySelectorProviderOptions<IEventStrategy>,
|
|
101
|
-
): Provider<IEventStrategy> {
|
|
102
|
-
return StrategySelectorProvider(options);
|
|
103
|
-
}
|
|
1
|
+
import type { Provider } from '@nestjs/common';
|
|
2
|
+
import type { IApiCallStrategy } from './context-call.interface.js';
|
|
3
|
+
import type { IEventStrategy } from './context-event.interface.js';
|
|
4
|
+
|
|
5
|
+
export type StrategySelectorToken<TStrategy = unknown> =
|
|
6
|
+
| string
|
|
7
|
+
| symbol
|
|
8
|
+
| (abstract new (...args: any[]) => TStrategy)
|
|
9
|
+
| (new (...args: any[]) => TStrategy);
|
|
10
|
+
|
|
11
|
+
export type StrategySelectorBehavior = 'throw' | 'fallback';
|
|
12
|
+
|
|
13
|
+
export interface StrategySelectorFactoryOptions {
|
|
14
|
+
inject?: any[];
|
|
15
|
+
useFactory?: (
|
|
16
|
+
...args: any[]
|
|
17
|
+
) => string | null | undefined | Promise<string | null | undefined>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface StrategySelectorProviderOptions<TStrategy> {
|
|
21
|
+
provide: StrategySelectorToken<TStrategy>;
|
|
22
|
+
defaultStrategy: string;
|
|
23
|
+
strategies: Record<string, StrategySelectorToken<TStrategy>>;
|
|
24
|
+
selector?: StrategySelectorFactoryOptions;
|
|
25
|
+
unknownStrategyBehavior?: StrategySelectorBehavior;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function normalizeStrategyKey(strategy: string | null | undefined) {
|
|
29
|
+
const value = strategy?.trim();
|
|
30
|
+
return value ? value : undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function StrategySelectorProvider<TStrategy>(
|
|
34
|
+
options: StrategySelectorProviderOptions<TStrategy>,
|
|
35
|
+
): Provider<TStrategy> {
|
|
36
|
+
const strategyEntries = Object.entries(options.strategies);
|
|
37
|
+
|
|
38
|
+
if (strategyEntries.length === 0) {
|
|
39
|
+
throw new Error('Strategy selector requires at least one strategy.');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const defaultStrategyIndex = strategyEntries.findIndex(
|
|
43
|
+
([key]) => key === options.defaultStrategy,
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
if (defaultStrategyIndex < 0) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
`Default strategy "${options.defaultStrategy}" is not registered.`,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const behavior = options.unknownStrategyBehavior ?? 'throw';
|
|
53
|
+
const selectorInject = options.selector?.inject ?? [];
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
provide: options.provide,
|
|
57
|
+
useFactory: async (...args: any[]) => {
|
|
58
|
+
const strategyInstances = args.slice(
|
|
59
|
+
0,
|
|
60
|
+
strategyEntries.length,
|
|
61
|
+
) as TStrategy[];
|
|
62
|
+
const selectorArgs = args.slice(strategyEntries.length);
|
|
63
|
+
const configuredStrategy = normalizeStrategyKey(
|
|
64
|
+
await options.selector?.useFactory?.(...selectorArgs),
|
|
65
|
+
);
|
|
66
|
+
const selectedStrategy = configuredStrategy ?? options.defaultStrategy;
|
|
67
|
+
const selectedStrategyIndex = strategyEntries.findIndex(
|
|
68
|
+
([key]) => key === selectedStrategy,
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
if (selectedStrategyIndex >= 0) {
|
|
72
|
+
return strategyInstances[selectedStrategyIndex];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (behavior === 'fallback') {
|
|
76
|
+
return strategyInstances[defaultStrategyIndex];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const availableStrategies = strategyEntries
|
|
80
|
+
.map(([key]) => key)
|
|
81
|
+
.join(', ');
|
|
82
|
+
throw new Error(
|
|
83
|
+
`Unknown strategy "${selectedStrategy}". Available strategies: ${availableStrategies}.`,
|
|
84
|
+
);
|
|
85
|
+
},
|
|
86
|
+
inject: [
|
|
87
|
+
...strategyEntries.map(([, strategyToken]) => strategyToken),
|
|
88
|
+
...selectorInject,
|
|
89
|
+
],
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function ApiCallStrategySelectorProvider(
|
|
94
|
+
options: StrategySelectorProviderOptions<IApiCallStrategy>,
|
|
95
|
+
): Provider<IApiCallStrategy> {
|
|
96
|
+
return StrategySelectorProvider(options);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function EventStrategySelectorProvider(
|
|
100
|
+
options: StrategySelectorProviderOptions<IEventStrategy>,
|
|
101
|
+
): Provider<IEventStrategy> {
|
|
102
|
+
return StrategySelectorProvider(options);
|
|
103
|
+
}
|