-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathhttpYacApi.ts
103 lines (94 loc) · 3.35 KB
/
httpYacApi.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { log } from './io';
import * as models from './models';
import './registerPlugins';
import { getEnvironmentConfig } from './store';
import * as utils from './utils';
import { HookCancel } from 'hookpoint';
/**
* process one httpRegion of HttpFile
* @param httpFile httpFile
*/
export async function send(context: models.SendContext): Promise<boolean> {
let result = false;
if (utils.isHttpRegionSendContext(context)) {
result = await sendHttpRegion(context);
} else if (utils.isHttpRegionsSendContext(context)) {
result = await sendHttpRegions(context);
} else {
result = await sendHttpFile(context);
}
return result;
}
async function sendHttpRegion(context: models.HttpRegionSendContext): Promise<boolean> {
const processorContext = await createEmptyProcessorContext(context);
if (await utils.executeGlobalScripts(processorContext)) {
return await context.httpRegion.execute(processorContext, true);
}
return false;
}
async function sendHttpRegions(context: models.HttpRegionsSendContext): Promise<boolean> {
const processorContext = await createEmptyProcessorContext(context);
if (await utils.executeGlobalScripts(processorContext)) {
if (context.progress) {
context.progress.divider = context.httpRegions.length;
}
for (const httpRegion of context.httpRegions) {
if (!(await httpRegion.execute(processorContext, true))) {
return false;
}
}
return true;
}
return false;
}
async function sendHttpFile(context: models.HttpFileSendContext): Promise<boolean> {
const httpRegions: Array<models.HttpRegion> = [];
for (const httpRegion of context.httpFile.httpRegions) {
if (context.httpRegionPredicate && !context.httpRegionPredicate(httpRegion)) {
log.debug(`${httpRegion.symbol.name} disabled by predicate`);
} else if (!httpRegion.isGlobal()) {
httpRegions.push(httpRegion);
}
}
return await sendHttpRegions({ ...context, httpRegions });
}
export async function createEmptyProcessorContext<T extends models.VariableProviderContext>(
context: T
): Promise<
T & {
variables: models.Variables;
options: Record<string, unknown>;
}
> {
return Object.assign(context, {
variables: await getVariables(context),
options: {},
});
}
export async function getVariables(context: models.VariableProviderContext): Promise<Record<string, unknown>> {
context.config = await getEnvironmentConfig(context.config, context.httpFile?.rootDir);
const vars = await context.httpFile.hooks.provideVariables.trigger(context.httpFile.activeEnvironment, context);
if (vars === HookCancel) {
return context.variables || {};
}
const variables = Object.assign({}, ...vars.map(variables => utils.cleanVariables(variables)), context.variables);
log.debug(variables);
return variables;
}
export async function getEnvironments(context: models.VariableProviderContext): Promise<Array<string>> {
context.config = await getEnvironmentConfig(context.config, context.httpFile?.rootDir);
const result = await context.httpFile.hooks.provideEnvironments.trigger(context);
if (result !== HookCancel && result.length > 0) {
return result
.reduce((prev, current) => {
for (const cur of current) {
if (prev.indexOf(cur) < 0) {
prev.push(cur);
}
}
return prev;
}, [] as Array<string>)
.sort();
}
return [];
}