@api-client/core 0.3.5 → 0.3.6

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.
Files changed (68) hide show
  1. package/build/browser.d.ts +2 -0
  2. package/build/browser.js +8 -0
  3. package/build/browser.js.map +1 -1
  4. package/build/index.d.ts +10 -1
  5. package/build/index.js +19 -1
  6. package/build/index.js.map +1 -1
  7. package/build/src/lib/fs/Fs.d.ts +52 -0
  8. package/build/src/lib/fs/Fs.js +245 -0
  9. package/build/src/lib/fs/Fs.js.map +1 -0
  10. package/build/src/lib/timers/Timers.d.ts +5 -0
  11. package/build/src/lib/timers/Timers.js +10 -0
  12. package/build/src/lib/timers/Timers.js.map +1 -0
  13. package/build/src/mocking/ProjectMock.d.ts +13 -0
  14. package/build/src/mocking/ProjectMock.js +16 -0
  15. package/build/src/mocking/ProjectMock.js.map +1 -0
  16. package/build/src/mocking/lib/Request.d.ts +32 -0
  17. package/build/src/mocking/lib/Request.js +63 -0
  18. package/build/src/mocking/lib/Request.js.map +1 -0
  19. package/build/src/mocking/lib/Response.d.ts +33 -0
  20. package/build/src/mocking/lib/Response.js +79 -0
  21. package/build/src/mocking/lib/Response.js.map +1 -0
  22. package/build/src/runtime/node/BaseRunner.d.ts +21 -0
  23. package/build/src/runtime/node/BaseRunner.js +27 -0
  24. package/build/src/runtime/node/BaseRunner.js.map +1 -0
  25. package/build/src/runtime/node/ProjectParallelRunner.d.ts +81 -0
  26. package/build/src/runtime/node/ProjectParallelRunner.js +173 -0
  27. package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -0
  28. package/build/src/runtime/node/ProjectRequestRunner.d.ts +125 -0
  29. package/build/src/runtime/node/ProjectRequestRunner.js +185 -0
  30. package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -0
  31. package/build/src/runtime/node/ProjectRunner.d.ts +164 -62
  32. package/build/src/runtime/node/ProjectRunner.js +191 -146
  33. package/build/src/runtime/node/ProjectRunner.js.map +1 -1
  34. package/build/src/runtime/node/ProjectRunnerWorker.d.ts +1 -0
  35. package/build/src/runtime/node/ProjectRunnerWorker.js +58 -0
  36. package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -0
  37. package/build/src/runtime/node/ProjectSerialRunner.d.ts +11 -0
  38. package/build/src/runtime/node/ProjectSerialRunner.js +34 -0
  39. package/build/src/runtime/node/ProjectSerialRunner.js.map +1 -0
  40. package/build/src/runtime/reporters/ProjectRunCliReporter.d.ts +7 -0
  41. package/build/src/runtime/reporters/ProjectRunCliReporter.js +73 -0
  42. package/build/src/runtime/reporters/ProjectRunCliReporter.js.map +1 -0
  43. package/build/src/runtime/reporters/Reporter.d.ts +62 -0
  44. package/build/src/runtime/reporters/Reporter.js +98 -0
  45. package/build/src/runtime/reporters/Reporter.js.map +1 -0
  46. package/build/src/testing/TestCliHelper.d.ts +23 -0
  47. package/build/src/testing/TestCliHelper.js +71 -0
  48. package/build/src/testing/TestCliHelper.js.map +1 -0
  49. package/build/src/testing/getPort.d.ts +52 -0
  50. package/build/src/testing/getPort.js +169 -0
  51. package/build/src/testing/getPort.js.map +1 -0
  52. package/package.json +2 -1
  53. package/src/lib/fs/Fs.ts +258 -0
  54. package/src/lib/timers/Timers.ts +9 -0
  55. package/src/mocking/LegacyInterfaces.ts +1 -1
  56. package/src/mocking/ProjectMock.ts +20 -0
  57. package/src/mocking/lib/Request.ts +85 -0
  58. package/src/mocking/lib/Response.ts +101 -0
  59. package/src/runtime/node/BaseRunner.ts +29 -0
  60. package/src/runtime/node/ProjectParallelRunner.ts +234 -0
  61. package/src/runtime/node/ProjectRequestRunner.ts +281 -0
  62. package/src/runtime/node/ProjectRunner.ts +279 -186
  63. package/src/runtime/node/ProjectRunnerWorker.ts +62 -0
  64. package/src/runtime/node/ProjectSerialRunner.ts +36 -0
  65. package/src/runtime/reporters/ProjectRunCliReporter.ts +79 -0
  66. package/src/runtime/reporters/Reporter.ts +142 -0
  67. package/src/testing/TestCliHelper.ts +76 -0
  68. package/src/testing/getPort.ts +212 -0
@@ -0,0 +1,185 @@
1
+ import { EventEmitter } from 'events';
2
+ import { RequestLog } from '../../models/RequestLog.js';
3
+ import { Kind as ProjectFolderKind } from '../../models/ProjectFolder.js';
4
+ import { SentRequest } from '../../models/SentRequest.js';
5
+ import { ErrorResponse } from '../../models/ErrorResponse.js';
6
+ import { VariablesStore } from './VariablesStore.js';
7
+ import { VariablesProcessor } from '../variables/VariablesProcessor.js';
8
+ import { RequestFactory } from './RequestFactory.js';
9
+ import { EventTypes } from '../../events/EventTypes.js';
10
+ /**
11
+ * Runs requests in a project.
12
+ * Developers can run the entire project with the `recursive` flag set. They can also
13
+ * set the starting point with the `parent` options.
14
+ *
15
+ * Requests are executed in order defined in the folder.
16
+ */
17
+ export class ProjectRequestRunner extends EventEmitter {
18
+ eventTarget;
19
+ logger;
20
+ project;
21
+ masterEnvironment;
22
+ extraVariables;
23
+ /**
24
+ * The variables processor instance.
25
+ */
26
+ variablesProcessor = new VariablesProcessor();
27
+ constructor(project, opts = {}) {
28
+ super();
29
+ this.project = project;
30
+ this.logger = opts.logger;
31
+ this.eventTarget = opts.eventTarget || new EventTarget();
32
+ this.masterEnvironment = opts.environment;
33
+ this.extraVariables = opts.variables;
34
+ }
35
+ /**
36
+ * Runs the request from the project root or a specified folder.
37
+ * @param options Run options.
38
+ * @returns A promise with the run result.
39
+ */
40
+ async run(options) {
41
+ const { project } = this;
42
+ const executed = [];
43
+ for (const request of project.requestIterator(options)) {
44
+ const parent = request.getParent() || project;
45
+ let variables;
46
+ if (VariablesStore.has(parent)) {
47
+ variables = VariablesStore.get(parent);
48
+ }
49
+ else {
50
+ variables = await this.getVariables(parent);
51
+ VariablesStore.set(parent, variables);
52
+ }
53
+ const info = await this.execute(request, variables);
54
+ executed.push(info);
55
+ }
56
+ return executed;
57
+ }
58
+ async execute(request, variables) {
59
+ const config = request.getConfig();
60
+ const factory = new RequestFactory(this.eventTarget);
61
+ factory.variables = variables;
62
+ if (request.authorization) {
63
+ factory.authorization = request.authorization.map(i => i.toJSON());
64
+ }
65
+ if (request.actions) {
66
+ factory.actions = request.actions.toJSON();
67
+ }
68
+ if (request.clientCertificate) {
69
+ factory.certificates = [request.clientCertificate];
70
+ }
71
+ if (config.enabled !== false) {
72
+ factory.config = config.toJSON();
73
+ }
74
+ if (this.logger) {
75
+ factory.logger = this.logger;
76
+ }
77
+ const info = {
78
+ key: request.key,
79
+ };
80
+ const requestData = request.expects.toJSON();
81
+ requestData.url = this.prepareRequestUrl(requestData.url, variables);
82
+ function variableHandler(e) {
83
+ if (e.defaultPrevented) {
84
+ return;
85
+ }
86
+ const { name, value } = e.detail;
87
+ variables[name] = value;
88
+ e.preventDefault();
89
+ e.detail.result = Promise.resolve();
90
+ }
91
+ this.eventTarget.addEventListener(EventTypes.Environment.set, variableHandler);
92
+ try {
93
+ // Below replaces the single call to the `run()` function of the factory to
94
+ // report via the events a request object that has evaluated with the Jexl library.
95
+ const requestCopy = await factory.processRequestVariables(requestData);
96
+ this.emit('request', request.key, { ...requestCopy });
97
+ await factory.processRequestLogic(requestCopy);
98
+ const result = await factory.executeRequest(requestCopy);
99
+ await factory.processResponse(result);
100
+ request.setLog(result);
101
+ info.log = result;
102
+ this.emit('response', request.key, { ...result });
103
+ }
104
+ catch (e) {
105
+ const cause = e;
106
+ info.error = true;
107
+ info.errorMessage = cause.message;
108
+ const sent = new SentRequest({ ...requestData, startTime: 0, endTime: 0, });
109
+ const response = ErrorResponse.fromError(info.errorMessage);
110
+ const log = RequestLog.fromRequestResponse(sent.toJSON(), response.toJSON()).toJSON();
111
+ this.emit('error', request.key, log, info.errorMessage);
112
+ }
113
+ this.eventTarget.removeEventListener(EventTypes.Environment.set, variableHandler);
114
+ return info;
115
+ }
116
+ async getVariables(parent) {
117
+ if (this.masterEnvironment) {
118
+ return this.applyVariables([this.masterEnvironment]);
119
+ }
120
+ return this.createEnvironment(parent);
121
+ }
122
+ async createEnvironment(parent) {
123
+ const envs = await this.readEnvironments(parent);
124
+ return this.applyVariables(envs);
125
+ }
126
+ /**
127
+ * Reads the list of the environments to apply to this runtime.
128
+ */
129
+ async readEnvironments(parent) {
130
+ const folderKey = parent.kind === ProjectFolderKind ? parent.key : undefined;
131
+ return this.project.readEnvironments({ folderKey });
132
+ }
133
+ /**
134
+ * Reads the variables and the base URI from the passed environments.
135
+ */
136
+ async applyVariables(environments) {
137
+ let baseUri = '';
138
+ const variables = [];
139
+ environments.forEach((environment) => {
140
+ const { server, variables: envVariables } = environment;
141
+ if (server) {
142
+ baseUri = server.readUri();
143
+ }
144
+ if (envVariables.length) {
145
+ envVariables.forEach((item) => {
146
+ const defined = variables.findIndex(i => i.name === item.name);
147
+ if (defined >= 0) {
148
+ variables[defined] = item;
149
+ }
150
+ else {
151
+ variables.push(item);
152
+ }
153
+ });
154
+ }
155
+ });
156
+ const { extraVariables } = this;
157
+ const ctx = VariablesProcessor.createContextFromProperties(variables);
158
+ if (extraVariables) {
159
+ Object.keys(extraVariables).forEach((key) => {
160
+ ctx[key] = extraVariables[key];
161
+ });
162
+ }
163
+ // the `baseUri` is reserved and always set to the environment's `baseUri`.
164
+ ctx.baseUri = baseUri || '';
165
+ return this.variablesProcessor.buildContext(ctx);
166
+ }
167
+ /**
168
+ * When defined it applies the serve's base URI to relative URLs.
169
+ * @param currentUrl The URL to process.
170
+ */
171
+ prepareRequestUrl(currentUrl, variables) {
172
+ const { baseUri } = variables;
173
+ if (!baseUri) {
174
+ return currentUrl;
175
+ }
176
+ if (currentUrl.startsWith('http:') || currentUrl.startsWith('https:')) {
177
+ return currentUrl;
178
+ }
179
+ if (currentUrl.startsWith('/')) {
180
+ return `${baseUri}${currentUrl}`;
181
+ }
182
+ return `${baseUri}/${currentUrl}`;
183
+ }
184
+ }
185
+ //# sourceMappingURL=ProjectRequestRunner.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ProjectRequestRunner.js","sourceRoot":"","sources":["../../../../src/runtime/node/ProjectRequestRunner.ts"],"names":[],"mappings":"A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AS,CAAC,CAAC;QACtE,IAAI,cAAc,EAAE;YAClB,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;gBAC1C,GAAG,CAAC,GAAG,CAAC,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC;YACjC,CAAC,CAAC,CAAC;SACJ;QACD,2EAA2E;QAC3E,GAAG,CAAC,OAAO,GAAG,OAAO,IAAI,EAAE,CAAC;QAC5B,OAAO,IAAI,CAAC,kBAAkB,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;IACnD,CAAC;IAED;;;OAGG;IACO,iBAAiB,CAAC,UAAkB,EAAE,SAAiC;QAC/E,MAAM,EAAE,OAAO,EAAE,GAAG,SAAS,CAAC;QAC9B,IAAI,CAAC,OAAO,EAAE;YACZ,OAAO,UAAU,CAAC;SACnB;QACD,IAAI,UAAU,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,UAAU,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE;YACrE,OAAO,UAAU,CAAC;SACnB;QACD,IAAI,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;YAC9B,OAAO,GAAG,OAAO,GAAG,UAAU,EAAE,CAAC;SAClC;QACD,OAAO,GAAG,OAAO,IAAI,UAAU,EAAE,CAAC;IACpC,CAAC;CACF"}
@@ -1,62 +1,87 @@
1
- /// <reference types="node" />
2
- import { EventEmitter } from 'events';
1
+ import { HttpProject } from '../../models/HttpProject.js';
2
+ import { ProjectFolder } from '../../models/ProjectFolder.js';
3
3
  import { Environment } from '../../models/Environment.js';
4
4
  import { Logger } from '../../lib/logging/Logger.js';
5
5
  import { IRequestLog } from '../../models/RequestLog.js';
6
- import { ProjectFolder } from '../../models/ProjectFolder.js';
7
- import { ProjectRequest } from '../../models/ProjectRequest.js';
8
6
  import { IHttpRequest } from '../../models/HttpRequest.js';
9
- import { HttpProject, IProjectRequestIterator } from '../../models/HttpProject.js';
10
- import { VariablesProcessor } from '../variables/VariablesProcessor.js';
11
- export interface ProjectRunnerOptions {
7
+ import { IProjectExecutionIteration, IProjectExecutionLog } from '../reporters/Reporter.js';
8
+ import { BaseRunner } from './BaseRunner.js';
9
+ declare type ProjectParent = HttpProject | ProjectFolder;
10
+ export interface IProjectRunnerOptions {
12
11
  /**
13
- * When provided it overrides any project / folder defined environment.
12
+ * The environment to use.
13
+ * This can be a name or the key of the environment located under the parent or root.
14
+ * It can also be a path to the environment definition. If the file exists it is used. Otherwise it tried to
15
+ * find the environment in the project.
14
16
  */
15
- environment?: Environment;
17
+ environment?: string;
16
18
  /**
17
- * Additional variables to pass to the selected environment.
18
- * This can be use to pass system variables, when needed.
19
- *
20
- * To use system variables tou can use `init.variables = process.env`;
19
+ * The parent folder to execute.
21
20
  */
22
- variables?: Record<string, string>;
21
+ parent?: string;
23
22
  /**
24
- * Overrides the default logger (console).
23
+ * The names or the keys of requests to execute.
24
+ * This can be used to limit the number of requests.
25
25
  */
26
- logger?: Logger;
26
+ request?: string[];
27
27
  /**
28
- * The event target to use.
29
- * By default it creates its own target.
28
+ * The number of times the execution should be repeated.
29
+ * Default to 1.
30
30
  */
31
- eventTarget?: EventTarget;
32
- }
33
- export interface ProjectRunnerRunOptions extends IProjectRequestIterator {
34
- }
35
- export interface RunResult {
31
+ iterations?: number;
36
32
  /**
37
- * The key of the request from the HttpProject that was executed.
33
+ * The number of milliseconds to wait between each iteration.
34
+ * Default to the next frame (vary from 1 to tens of milliseconds).
38
35
  */
39
- key: string;
36
+ iterationDelay?: number;
40
37
  /**
41
- * The key of parent folder of the executed request.
38
+ * When set it performs parallel execution for each iteration.
39
+ * The number of executions at the same time depends on the number of processor cores
40
+ * available on the current machine. The maximum of the parallel execution
41
+ * is the number of available cores. When the `iterations` number is higher
42
+ * then the "rest" is added to the iterations per each core.
42
43
  */
43
- parent?: string;
44
+ parallel?: boolean;
45
+ /**
46
+ * When set it includes requests in the current folder and sub-folder according to the order
47
+ * defined in the folder.
48
+ */
49
+ recursive?: boolean;
44
50
  /**
45
- * Set when a fatal error occurred so the request couldn't be executed.
46
- * This is not the same as error reported during a request. The log's response can still be IResponseError.
51
+ * The opposite of the `requests`. The list of names or keys of requests or folders to ignore.
52
+ * Note, ignore is tested before the `requests`.
47
53
  */
48
- error?: boolean;
54
+ ignore?: string[];
49
55
  /**
50
- * The error message. Always set when the `error` is `true`.
56
+ * The logger to use with the request factory.
57
+ * When not set it uses the dummy logger (no output).
51
58
  */
52
- errorMessage?: string;
59
+ logger?: Logger;
53
60
  /**
54
- * The request log.
55
- * Always set when the `error` is `false`.
61
+ * When true it copies all system variables to the execution environment.
62
+ * When an array of strings, only takes system variables that are listed in the array.
63
+ * When a map, it uses this map as a list of variables.
64
+ * When not set it does not read system variables.
56
65
  */
57
- log?: IRequestLog;
66
+ variables?: boolean | string[] | Record<string, string>;
58
67
  }
59
68
  export interface ProjectRunner {
69
+ /**
70
+ * Event dispatched when an iteration is about to start.
71
+ */
72
+ on(event: 'before-iteration', listener: (index: number, iterated: boolean) => void): this;
73
+ /**
74
+ * Event dispatched when an iteration finished.
75
+ */
76
+ on(event: 'after-iteration', listener: (index: number, iterated: boolean) => void): this;
77
+ /**
78
+ * Event dispatched before the iteration is going to sleep for the set period of time.
79
+ */
80
+ on(event: 'before-sleep', listener: () => void): this;
81
+ /**
82
+ * Event dispatched after the iteration is woke up.
83
+ */
84
+ on(event: 'after-sleep', listener: () => void): this;
60
85
  /**
61
86
  * The request object is prepared and about to be sent to the HTTP engine
62
87
  */
@@ -69,6 +94,22 @@ export interface ProjectRunner {
69
94
  * There was a general error during the request
70
95
  */
71
96
  on(event: 'error', listener: (key: string, log: IRequestLog, message: string) => void): this;
97
+ /**
98
+ * Event dispatched when an iteration is about to start.
99
+ */
100
+ once(event: 'before-iteration', listener: (index: number, iterated: boolean) => void): this;
101
+ /**
102
+ * Event dispatched when an iteration finished.
103
+ */
104
+ once(event: 'after-iteration', listener: (index: number, iterated: boolean) => void): this;
105
+ /**
106
+ * Event dispatched before the iteration is going to sleep for the set period of time.
107
+ */
108
+ once(event: 'before-sleep', listener: () => void): this;
109
+ /**
110
+ * Event dispatched after the iteration is woke up.
111
+ */
112
+ once(event: 'after-sleep', listener: () => void): this;
72
113
  /**
73
114
  * The request object is prepared and about to be sent to the HTTP engine
74
115
  */
@@ -83,43 +124,104 @@ export interface ProjectRunner {
83
124
  once(event: 'error', listener: (key: string, log: IRequestLog, message: string) => void): this;
84
125
  }
85
126
  /**
86
- * Runs requests in a project.
87
- * Developers can run the entire project with the `recursive` flag set. They can also
88
- * set the starting point with the `parent` options.
127
+ * A class to be extended to run an entire API Project for the given configuration options.
89
128
  *
90
- * Requests are executed in order defined in the folder.
129
+ * The main purpose of this class (and its children) is to iterate over requests
130
+ * in the project and execute them one-by-one.
131
+ *
132
+ * Implementations allow to execute the requests in a serial model or in parallel mode, where
133
+ * a separate workers are created to run the same HTTP requests in each worker.
134
+ *
135
+ * This class generates a run report which other programs can use to build all kinds of UIs
136
+ * around the data collected during the run.
91
137
  */
92
- export declare class ProjectRunner extends EventEmitter {
93
- eventTarget: EventTarget;
94
- logger?: Logger;
95
- project: HttpProject;
96
- protected masterEnvironment?: Environment;
97
- protected extraVariables?: Record<string, string>;
138
+ export declare abstract class ProjectRunner extends BaseRunner {
139
+ /**
140
+ * The HTTP project to run requests from.
141
+ */
142
+ project?: HttpProject;
143
+ /**
144
+ * The execution options for the project.
145
+ */
146
+ options?: IProjectRunnerOptions;
147
+ /**
148
+ * The root object (project or a folder) where the program starts iterating over the requests.
149
+ */
150
+ root?: ProjectParent;
151
+ /**
152
+ * The selected environment to apply to the requests.
153
+ */
154
+ environment?: Environment;
155
+ /**
156
+ * The events target instance for events dispatched by the request factory.
157
+ */
158
+ target: EventTarget;
159
+ /**
160
+ * This is used with `--iterations`. The index of the current iteration.
161
+ */
162
+ protected index: number;
163
+ /**
164
+ * The currently executed iteration loop.
165
+ */
166
+ protected currentIteration?: IProjectExecutionIteration;
167
+ /**
168
+ * The number of remaining iterations to run.
169
+ */
170
+ protected remaining: number;
171
+ /**
172
+ * Whether the configuration allows iterations (the parallel mode).
173
+ */
174
+ protected hasIterations: boolean;
175
+ /**
176
+ * When set it won't amit any event.
177
+ * This can be used in a background worker when events are never handled.
178
+ */
179
+ noEmit: boolean;
180
+ constructor();
181
+ /**
182
+ * A required step before running the project.
183
+ * It configures the execution context. It may throw an error when configuration is not valid.
184
+ */
185
+ configure(project: HttpProject, opts?: IProjectRunnerOptions): Promise<void>;
186
+ /**
187
+ * Executes the requests in the project.
188
+ * @returns The execution log created by calling the `createReport()` function.
189
+ */
190
+ abstract execute(): Promise<IProjectExecutionLog>;
191
+ /**
192
+ * Creates the report of the execution.
193
+ */
194
+ protected createReport(): Promise<IProjectExecutionLog>;
195
+ /**
196
+ * Reads the environment data to use with the execution.
197
+ * If the configured environment is a location of a file
198
+ * it is read as API Client's environment and used in the execution.
199
+ * Otherwise it searches for the environment in the list of the defined
200
+ * environments by the name of the key.
201
+ *
202
+ * It throws when the environment cannot be found or when the file contents is invalid.
203
+ */
204
+ protected getEnvironment(): Promise<Environment | undefined>;
98
205
  /**
99
- * The variables processor instance.
206
+ * Runs the requests from the project as configured.
100
207
  */
101
- protected variablesProcessor: VariablesProcessor;
102
- constructor(project: HttpProject, opts?: ProjectRunnerOptions);
208
+ protected executeIteration(): Promise<void>;
103
209
  /**
104
- * Runs the request from the project root or a specified folder.
105
- * @param options Run options.
106
- * @returns A promise with the run result.
210
+ * Retargets the "request" event from the factory.
107
211
  */
108
- run(options?: ProjectRunnerRunOptions): Promise<RunResult[]>;
109
- protected execute(request: ProjectRequest, variables: Record<string, string>): Promise<RunResult>;
110
- protected getVariables(parent: HttpProject | ProjectFolder): Promise<Record<string, string>>;
111
- protected createEnvironment(parent: HttpProject | ProjectFolder): Promise<Record<string, string>>;
212
+ protected _requestHandler(key: string, request: IHttpRequest): void;
112
213
  /**
113
- * Reads the list of the environments to apply to this runtime.
214
+ * Retargets the "response" event from the factory.
114
215
  */
115
- protected readEnvironments(parent: HttpProject | ProjectFolder): Promise<Environment[]>;
216
+ protected _responseHandler(key: string, log: IRequestLog): void;
116
217
  /**
117
- * Reads the variables and the base URI from the passed environments.
218
+ * Retargets the "error" event from the factory.
118
219
  */
119
- protected applyVariables(environments: Environment[]): Promise<Record<string, string>>;
220
+ protected _errorHandler(key: string, log: IRequestLog, message: string): void;
120
221
  /**
121
- * When defined it applies the serve's base URI to relative URLs.
122
- * @param currentUrl The URL to process.
222
+ * @returns Reads the system variables based on the library configuration.
123
223
  */
124
- protected prepareRequestUrl(currentUrl: string, variables: Record<string, string>): string;
224
+ protected getSystemVariables(): Record<string, string>;
225
+ private _readSystemVariables;
125
226
  }
227
+ export {};