-
Notifications
You must be signed in to change notification settings - Fork 30
/
regionScopedVariablesInterceptor.ts
56 lines (51 loc) · 1.96 KB
/
regionScopedVariablesInterceptor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { HookInterceptor, HookTriggerContext } from 'hookpoint';
import * as models from '../../../models';
import { toEnvironmentKey } from '../../../utils';
interface RegionScopedVariableOptions {
variables?: models.Variables;
}
export class RegionScopedVariablesInterceptor implements HookInterceptor<[models.ProcessorContext], boolean | void> {
id = 'regionScopedVariables';
async beforeLoop(
hookContext: HookTriggerContext<[models.ProcessorContext], boolean | undefined>
): Promise<boolean | undefined> {
const context = hookContext.args[0];
const env = toEnvironmentKey(context.activeEnvironment);
if (context.config?.useRegionScopedVariables) {
const regionScopedVariables: RegionScopedVariableOptions = context.options;
regionScopedVariables.variables = context.variables;
context.variables = Object.assign(
{},
context.variables,
...context.httpFile.globalHttpRegions.map(obj => obj.variablesPerEnv[env])
);
} else {
context.variables = Object.assign(
context.variables,
...context.httpFile.httpRegions.map(obj => obj.variablesPerEnv[env])
);
}
return true;
}
async afterLoop(
hookContext: HookTriggerContext<[models.ProcessorContext], boolean | undefined>
): Promise<boolean | undefined> {
const context = hookContext.args[0];
if (context.config?.useRegionScopedVariables) {
const regionScopedVariables: RegionScopedVariableOptions = context.options;
if (regionScopedVariables.variables) {
const newVariables = context.variables;
context.variables = regionScopedVariables.variables;
this.autoShareNewVariables(newVariables, context);
}
}
return true;
}
private autoShareNewVariables(variables: models.Variables, context: models.ProcessorContext) {
for (const [key, value] of Object.entries(variables)) {
if (!context.variables[key]) {
context.variables[key] = value;
}
}
}
}