@api-client/core 0.3.5 → 0.3.8
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/build/browser.d.ts +5 -0
- package/build/browser.js +14 -0
- package/build/browser.js.map +1 -1
- package/build/index.d.ts +13 -1
- package/build/index.js +25 -1
- package/build/index.js.map +1 -1
- package/build/src/lib/calculators/DataCalculator.d.ts +27 -0
- package/build/src/lib/calculators/DataCalculator.js +88 -0
- package/build/src/lib/calculators/DataCalculator.js.map +1 -0
- package/build/src/lib/fs/Fs.d.ts +52 -0
- package/build/src/lib/fs/Fs.js +245 -0
- package/build/src/lib/fs/Fs.js.map +1 -0
- package/build/src/lib/parsers/UrlEncoder.d.ts +51 -0
- package/build/src/lib/parsers/UrlEncoder.js +74 -0
- package/build/src/lib/parsers/UrlEncoder.js.map +1 -0
- package/build/src/lib/parsers/UrlParser.d.ts +104 -0
- package/build/src/lib/parsers/UrlParser.js +189 -0
- package/build/src/lib/parsers/UrlParser.js.map +1 -0
- package/build/src/lib/parsers/UrlValueParser.d.ts +92 -0
- package/build/src/lib/parsers/UrlValueParser.js +172 -0
- package/build/src/lib/parsers/UrlValueParser.js.map +1 -0
- package/build/src/lib/timers/Timers.d.ts +5 -0
- package/build/src/lib/timers/Timers.js +10 -0
- package/build/src/lib/timers/Timers.js.map +1 -0
- package/build/src/mocking/ProjectMock.d.ts +13 -0
- package/build/src/mocking/ProjectMock.js +16 -0
- package/build/src/mocking/ProjectMock.js.map +1 -0
- package/build/src/mocking/lib/Request.d.ts +32 -0
- package/build/src/mocking/lib/Request.js +63 -0
- package/build/src/mocking/lib/Request.js.map +1 -0
- package/build/src/mocking/lib/Response.d.ts +33 -0
- package/build/src/mocking/lib/Response.js +79 -0
- package/build/src/mocking/lib/Response.js.map +1 -0
- package/build/src/runtime/node/BaseRunner.d.ts +21 -0
- package/build/src/runtime/node/BaseRunner.js +27 -0
- package/build/src/runtime/node/BaseRunner.js.map +1 -0
- package/build/src/runtime/node/ProjectParallelRunner.d.ts +81 -0
- package/build/src/runtime/node/ProjectParallelRunner.js +173 -0
- package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -0
- package/build/src/runtime/node/ProjectRequestRunner.d.ts +125 -0
- package/build/src/runtime/node/ProjectRequestRunner.js +185 -0
- package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -0
- package/build/src/runtime/node/ProjectRunner.d.ts +164 -62
- package/build/src/runtime/node/ProjectRunner.js +191 -146
- package/build/src/runtime/node/ProjectRunner.js.map +1 -1
- package/build/src/runtime/node/ProjectRunnerWorker.d.ts +1 -0
- package/build/src/runtime/node/ProjectRunnerWorker.js +58 -0
- package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -0
- package/build/src/runtime/node/ProjectSerialRunner.d.ts +11 -0
- package/build/src/runtime/node/ProjectSerialRunner.js +34 -0
- package/build/src/runtime/node/ProjectSerialRunner.js.map +1 -0
- package/build/src/runtime/reporters/ProjectRunCliReporter.d.ts +7 -0
- package/build/src/runtime/reporters/ProjectRunCliReporter.js +73 -0
- package/build/src/runtime/reporters/ProjectRunCliReporter.js.map +1 -0
- package/build/src/runtime/reporters/Reporter.d.ts +62 -0
- package/build/src/runtime/reporters/Reporter.js +98 -0
- package/build/src/runtime/reporters/Reporter.js.map +1 -0
- package/build/src/testing/TestCliHelper.d.ts +29 -0
- package/build/src/testing/TestCliHelper.js +80 -0
- package/build/src/testing/TestCliHelper.js.map +1 -0
- package/build/src/testing/getPort.d.ts +52 -0
- package/build/src/testing/getPort.js +169 -0
- package/build/src/testing/getPort.js.map +1 -0
- package/package.json +3 -2
- package/src/lib/calculators/DataCalculator.ts +91 -0
- package/src/lib/fs/Fs.ts +258 -0
- package/src/lib/parsers/UrlEncoder.ts +74 -0
- package/src/lib/parsers/UrlParser.ts +201 -0
- package/src/lib/parsers/UrlValueParser.ts +211 -0
- package/src/lib/timers/Timers.ts +9 -0
- package/src/mocking/LegacyInterfaces.ts +1 -1
- package/src/mocking/ProjectMock.ts +20 -0
- package/src/mocking/lib/Request.ts +85 -0
- package/src/mocking/lib/Response.ts +101 -0
- package/src/runtime/node/BaseRunner.ts +29 -0
- package/src/runtime/node/ProjectParallelRunner.ts +234 -0
- package/src/runtime/node/ProjectRequestRunner.ts +281 -0
- package/src/runtime/node/ProjectRunner.ts +279 -186
- package/src/runtime/node/ProjectRunnerWorker.ts +62 -0
- package/src/runtime/node/ProjectSerialRunner.ts +36 -0
- package/src/runtime/reporters/ProjectRunCliReporter.ts +79 -0
- package/src/runtime/reporters/Reporter.ts +142 -0
- package/src/testing/TestCliHelper.ts +87 -0
- package/src/testing/getPort.ts +212 -0
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { Environment } from '../../models/Environment.js';
|
|
3
|
+
import { Logger } from '../../lib/logging/Logger.js';
|
|
4
|
+
import { IRequestLog, RequestLog } from '../../models/RequestLog.js';
|
|
5
|
+
import { Property } from '../../models/Property.js';
|
|
6
|
+
import { ProjectFolder, Kind as ProjectFolderKind } from '../../models/ProjectFolder.js';
|
|
7
|
+
import { ProjectRequest } from '../../models/ProjectRequest.js';
|
|
8
|
+
import { IHttpRequest } from '../../models/HttpRequest.js';
|
|
9
|
+
import { HttpProject, IProjectRequestIterator } from '../../models/HttpProject.js';
|
|
10
|
+
import { SentRequest } from '../../models/SentRequest.js';
|
|
11
|
+
import { ErrorResponse } from '../../models/ErrorResponse.js';
|
|
12
|
+
import { VariablesStore } from './VariablesStore.js';
|
|
13
|
+
import { VariablesProcessor } from '../variables/VariablesProcessor.js';
|
|
14
|
+
import { RequestFactory } from './RequestFactory.js';
|
|
15
|
+
import { EventTypes } from '../../events/EventTypes.js';
|
|
16
|
+
|
|
17
|
+
export interface ProjectRunnerOptions {
|
|
18
|
+
/**
|
|
19
|
+
* When provided it overrides any project / folder defined environment.
|
|
20
|
+
*/
|
|
21
|
+
environment?: Environment;
|
|
22
|
+
/**
|
|
23
|
+
* Additional variables to pass to the selected environment.
|
|
24
|
+
* This can be use to pass system variables, when needed.
|
|
25
|
+
*
|
|
26
|
+
* To use system variables tou can use `init.variables = process.env`;
|
|
27
|
+
*/
|
|
28
|
+
variables?: Record<string, string>;
|
|
29
|
+
/**
|
|
30
|
+
* Overrides the default logger (console).
|
|
31
|
+
*/
|
|
32
|
+
logger?: Logger;
|
|
33
|
+
/**
|
|
34
|
+
* The event target to use.
|
|
35
|
+
* By default it creates its own target.
|
|
36
|
+
*/
|
|
37
|
+
eventTarget?: EventTarget;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface ProjectRunnerRunOptions extends IProjectRequestIterator {
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface RunResult {
|
|
44
|
+
/**
|
|
45
|
+
* The key of the request from the HttpProject that was executed.
|
|
46
|
+
*/
|
|
47
|
+
key: string;
|
|
48
|
+
/**
|
|
49
|
+
* The key of parent folder of the executed request.
|
|
50
|
+
*/
|
|
51
|
+
parent?: string;
|
|
52
|
+
/**
|
|
53
|
+
* Set when a fatal error occurred so the request couldn't be executed.
|
|
54
|
+
* This is not the same as error reported during a request. The log's response can still be IResponseError.
|
|
55
|
+
*/
|
|
56
|
+
error?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* The error message. Always set when the `error` is `true`.
|
|
59
|
+
*/
|
|
60
|
+
errorMessage?: string;
|
|
61
|
+
/**
|
|
62
|
+
* The request log.
|
|
63
|
+
* Always set when the `error` is `false`.
|
|
64
|
+
*/
|
|
65
|
+
log?: IRequestLog;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface ProjectRequestRunner {
|
|
69
|
+
/**
|
|
70
|
+
* The request object is prepared and about to be sent to the HTTP engine
|
|
71
|
+
*/
|
|
72
|
+
on(event: 'request', listener: (key: string, request: IHttpRequest) => void): this;
|
|
73
|
+
/**
|
|
74
|
+
* The response is ready.
|
|
75
|
+
*/
|
|
76
|
+
on(event: 'response', listener: (key: string, log: IRequestLog) => void): this;
|
|
77
|
+
/**
|
|
78
|
+
* There was a general error during the request
|
|
79
|
+
*/
|
|
80
|
+
on(event: 'error', listener: (key: string, log: IRequestLog, message: string) => void): this;
|
|
81
|
+
/**
|
|
82
|
+
* The request object is prepared and about to be sent to the HTTP engine
|
|
83
|
+
*/
|
|
84
|
+
once(event: 'request', listener: (key: string, request: IHttpRequest) => void): this;
|
|
85
|
+
/**
|
|
86
|
+
* The response is ready.
|
|
87
|
+
*/
|
|
88
|
+
once(event: 'response', listener: (key: string, log: IRequestLog) => void): this;
|
|
89
|
+
/**
|
|
90
|
+
* There was a general error during the request
|
|
91
|
+
*/
|
|
92
|
+
once(event: 'error', listener: (key: string, log: IRequestLog, message: string) => void): this;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Runs requests in a project.
|
|
97
|
+
* Developers can run the entire project with the `recursive` flag set. They can also
|
|
98
|
+
* set the starting point with the `parent` options.
|
|
99
|
+
*
|
|
100
|
+
* Requests are executed in order defined in the folder.
|
|
101
|
+
*/
|
|
102
|
+
export class ProjectRequestRunner extends EventEmitter {
|
|
103
|
+
eventTarget: EventTarget;
|
|
104
|
+
logger?: Logger;
|
|
105
|
+
project: HttpProject;
|
|
106
|
+
|
|
107
|
+
protected masterEnvironment?: Environment;
|
|
108
|
+
protected extraVariables?: Record<string, string>;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The variables processor instance.
|
|
112
|
+
*/
|
|
113
|
+
protected variablesProcessor = new VariablesProcessor();
|
|
114
|
+
|
|
115
|
+
constructor(project: HttpProject, opts: ProjectRunnerOptions = {}) {
|
|
116
|
+
super();
|
|
117
|
+
this.project = project;
|
|
118
|
+
this.logger = opts.logger;
|
|
119
|
+
this.eventTarget = opts.eventTarget || new EventTarget();
|
|
120
|
+
this.masterEnvironment = opts.environment;
|
|
121
|
+
this.extraVariables = opts.variables;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Runs the request from the project root or a specified folder.
|
|
126
|
+
* @param options Run options.
|
|
127
|
+
* @returns A promise with the run result.
|
|
128
|
+
*/
|
|
129
|
+
async run(options?: ProjectRunnerRunOptions): Promise<RunResult[]> {
|
|
130
|
+
const { project } = this;
|
|
131
|
+
const executed: RunResult[] = [];
|
|
132
|
+
for (const request of project.requestIterator(options)) {
|
|
133
|
+
const parent = request.getParent() || project;
|
|
134
|
+
let variables: Record<string, string>;
|
|
135
|
+
if (VariablesStore.has(parent)) {
|
|
136
|
+
variables = VariablesStore.get(parent);
|
|
137
|
+
} else {
|
|
138
|
+
variables = await this.getVariables(parent);
|
|
139
|
+
VariablesStore.set(parent, variables);
|
|
140
|
+
}
|
|
141
|
+
const info = await this.execute(request, variables);
|
|
142
|
+
executed.push(info);
|
|
143
|
+
}
|
|
144
|
+
return executed;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
protected async execute(request: ProjectRequest, variables: Record<string, string>): Promise<RunResult> {
|
|
148
|
+
const config = request.getConfig();
|
|
149
|
+
const factory = new RequestFactory(this.eventTarget);
|
|
150
|
+
|
|
151
|
+
factory.variables = variables;
|
|
152
|
+
if (request.authorization) {
|
|
153
|
+
factory.authorization = request.authorization.map(i => i.toJSON());
|
|
154
|
+
}
|
|
155
|
+
if (request.actions) {
|
|
156
|
+
factory.actions = request.actions.toJSON();
|
|
157
|
+
}
|
|
158
|
+
if (request.clientCertificate) {
|
|
159
|
+
factory.certificates = [request.clientCertificate];
|
|
160
|
+
}
|
|
161
|
+
if (config.enabled !== false) {
|
|
162
|
+
factory.config = config.toJSON();
|
|
163
|
+
}
|
|
164
|
+
if (this.logger) {
|
|
165
|
+
factory.logger = this.logger;
|
|
166
|
+
}
|
|
167
|
+
const info: RunResult = {
|
|
168
|
+
key: request.key,
|
|
169
|
+
};
|
|
170
|
+
const requestData = request.expects.toJSON();
|
|
171
|
+
requestData.url = this.prepareRequestUrl(requestData.url, variables);
|
|
172
|
+
|
|
173
|
+
function variableHandler(e: CustomEvent): void {
|
|
174
|
+
if (e.defaultPrevented) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
const { name, value } = e.detail;
|
|
178
|
+
variables[name] = value;
|
|
179
|
+
e.preventDefault();
|
|
180
|
+
e.detail.result = Promise.resolve();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
this.eventTarget.addEventListener(EventTypes.Environment.set, variableHandler as any);
|
|
184
|
+
|
|
185
|
+
try {
|
|
186
|
+
// Below replaces the single call to the `run()` function of the factory to
|
|
187
|
+
// report via the events a request object that has evaluated with the Jexl library.
|
|
188
|
+
const requestCopy = await factory.processRequestVariables(requestData);
|
|
189
|
+
this.emit('request', request.key, { ...requestCopy });
|
|
190
|
+
await factory.processRequestLogic(requestCopy);
|
|
191
|
+
const result = await factory.executeRequest(requestCopy);
|
|
192
|
+
await factory.processResponse(result);
|
|
193
|
+
request.setLog(result);
|
|
194
|
+
info.log = result;
|
|
195
|
+
this.emit('response', request.key, { ...result });
|
|
196
|
+
} catch (e) {
|
|
197
|
+
const cause = e as Error;
|
|
198
|
+
info.error = true;
|
|
199
|
+
info.errorMessage = cause.message;
|
|
200
|
+
const sent = new SentRequest({ ...requestData, startTime: 0, endTime: 0, });
|
|
201
|
+
const response = ErrorResponse.fromError(info.errorMessage);
|
|
202
|
+
const log = RequestLog.fromRequestResponse(sent.toJSON(), response.toJSON()).toJSON();
|
|
203
|
+
this.emit('error', request.key, log, info.errorMessage);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
this.eventTarget.removeEventListener(EventTypes.Environment.set, variableHandler as any);
|
|
207
|
+
return info;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
protected async getVariables(parent: HttpProject | ProjectFolder): Promise<Record<string, string>> {
|
|
211
|
+
if (this.masterEnvironment) {
|
|
212
|
+
return this.applyVariables([this.masterEnvironment]);
|
|
213
|
+
}
|
|
214
|
+
return this.createEnvironment(parent);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
protected async createEnvironment(parent: HttpProject | ProjectFolder): Promise<Record<string, string>> {
|
|
218
|
+
const envs = await this.readEnvironments(parent);
|
|
219
|
+
return this.applyVariables(envs);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Reads the list of the environments to apply to this runtime.
|
|
224
|
+
*/
|
|
225
|
+
protected async readEnvironments(parent: HttpProject | ProjectFolder): Promise<Environment[]> {
|
|
226
|
+
const folderKey = parent.kind === ProjectFolderKind ? (parent as ProjectFolder).key : undefined;
|
|
227
|
+
return this.project.readEnvironments({ folderKey });
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Reads the variables and the base URI from the passed environments.
|
|
232
|
+
*/
|
|
233
|
+
protected async applyVariables(environments: Environment[]): Promise<Record<string, string>> {
|
|
234
|
+
let baseUri = '';
|
|
235
|
+
const variables: Property[] = [];
|
|
236
|
+
environments.forEach((environment) => {
|
|
237
|
+
const { server, variables: envVariables } = environment;
|
|
238
|
+
if (server) {
|
|
239
|
+
baseUri = server.readUri();
|
|
240
|
+
}
|
|
241
|
+
if (envVariables.length) {
|
|
242
|
+
envVariables.forEach((item) => {
|
|
243
|
+
const defined = variables.findIndex(i => i.name === item.name);
|
|
244
|
+
if (defined >= 0) {
|
|
245
|
+
variables[defined] = item;
|
|
246
|
+
} else {
|
|
247
|
+
variables.push(item);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
const { extraVariables } = this;
|
|
253
|
+
const ctx = VariablesProcessor.createContextFromProperties(variables);
|
|
254
|
+
if (extraVariables) {
|
|
255
|
+
Object.keys(extraVariables).forEach((key) => {
|
|
256
|
+
ctx[key] = extraVariables[key];
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
// the `baseUri` is reserved and always set to the environment's `baseUri`.
|
|
260
|
+
ctx.baseUri = baseUri || '';
|
|
261
|
+
return this.variablesProcessor.buildContext(ctx);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* When defined it applies the serve's base URI to relative URLs.
|
|
266
|
+
* @param currentUrl The URL to process.
|
|
267
|
+
*/
|
|
268
|
+
protected prepareRequestUrl(currentUrl: string, variables: Record<string, string>): string {
|
|
269
|
+
const { baseUri } = variables;
|
|
270
|
+
if (!baseUri) {
|
|
271
|
+
return currentUrl;
|
|
272
|
+
}
|
|
273
|
+
if (currentUrl.startsWith('http:') || currentUrl.startsWith('https:')) {
|
|
274
|
+
return currentUrl;
|
|
275
|
+
}
|
|
276
|
+
if (currentUrl.startsWith('/')) {
|
|
277
|
+
return `${baseUri}${currentUrl}`;
|
|
278
|
+
}
|
|
279
|
+
return `${baseUri}/${currentUrl}`;
|
|
280
|
+
}
|
|
281
|
+
}
|