@api-client/core 0.4.0 → 0.4.3

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 (110) hide show
  1. package/build/browser.d.ts +6 -3
  2. package/build/browser.js +7 -2
  3. package/build/browser.js.map +1 -1
  4. package/build/index.d.ts +7 -5
  5. package/build/index.js +4 -3
  6. package/build/index.js.map +1 -1
  7. package/build/src/models/HttpHistory.d.ts +29 -0
  8. package/build/src/models/HttpHistory.js.map +1 -1
  9. package/build/src/models/HttpProject.d.ts +37 -1
  10. package/build/src/models/HttpProject.js +49 -1
  11. package/build/src/models/HttpProject.js.map +1 -1
  12. package/build/src/models/RequestLog.d.ts +8 -0
  13. package/build/src/models/RequestLog.js +14 -1
  14. package/build/src/models/RequestLog.js.map +1 -1
  15. package/build/src/runtime/node/InteropInterfaces.d.ts +115 -0
  16. package/build/src/runtime/node/InteropInterfaces.js +2 -0
  17. package/build/src/runtime/node/InteropInterfaces.js.map +1 -0
  18. package/build/src/runtime/node/ProjectParallelRunner.d.ts +2 -7
  19. package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -1
  20. package/build/src/runtime/node/ProjectRequestRunner.d.ts +17 -49
  21. package/build/src/runtime/node/ProjectRequestRunner.js +39 -10
  22. package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -1
  23. package/build/src/runtime/node/ProjectRunner.d.ts +1 -59
  24. package/build/src/runtime/node/ProjectRunner.js.map +1 -1
  25. package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -1
  26. package/build/src/runtime/store/AuthSdk.d.ts +28 -0
  27. package/build/src/runtime/store/AuthSdk.js +123 -0
  28. package/build/src/runtime/store/AuthSdk.js.map +1 -0
  29. package/build/src/runtime/store/BackendSdk.d.ts +8 -0
  30. package/build/src/runtime/store/BackendSdk.js +25 -0
  31. package/build/src/runtime/store/BackendSdk.js.map +1 -0
  32. package/build/src/runtime/store/HistorySdk.d.ts +48 -0
  33. package/build/src/runtime/store/HistorySdk.js +142 -0
  34. package/build/src/runtime/store/HistorySdk.js.map +1 -0
  35. package/build/src/runtime/store/Http.d.ts +14 -0
  36. package/build/src/runtime/store/Http.js +4 -0
  37. package/build/src/runtime/store/Http.js.map +1 -0
  38. package/build/src/runtime/store/HttpNode.d.ts +16 -0
  39. package/build/src/runtime/store/HttpNode.js +95 -0
  40. package/build/src/runtime/store/HttpNode.js.map +1 -0
  41. package/build/src/runtime/store/HttpWeb.d.ts +15 -0
  42. package/build/src/runtime/store/HttpWeb.js +83 -0
  43. package/build/src/runtime/store/HttpWeb.js.map +1 -0
  44. package/build/src/runtime/store/ProjectsSdk.d.ts +43 -0
  45. package/build/src/runtime/store/ProjectsSdk.js +144 -0
  46. package/build/src/runtime/store/ProjectsSdk.js.map +1 -0
  47. package/build/src/runtime/store/RouteBuilder.d.ts +42 -0
  48. package/build/src/runtime/store/RouteBuilder.js +75 -0
  49. package/build/src/runtime/store/RouteBuilder.js.map +1 -0
  50. package/build/src/runtime/store/Sdk.d.ts +79 -0
  51. package/build/src/runtime/store/Sdk.js +117 -0
  52. package/build/src/runtime/store/Sdk.js.map +1 -0
  53. package/build/src/runtime/store/SdkBase.d.ts +45 -0
  54. package/build/src/runtime/store/SdkBase.js +40 -0
  55. package/build/src/runtime/store/SdkBase.js.map +1 -0
  56. package/build/src/runtime/store/SpacesSdk.d.ts +50 -0
  57. package/build/src/runtime/store/SpacesSdk.js +185 -0
  58. package/build/src/runtime/store/SpacesSdk.js.map +1 -0
  59. package/build/src/runtime/store/StoreSdkNode.d.ts +10 -0
  60. package/build/src/runtime/store/StoreSdkNode.js +11 -0
  61. package/build/src/runtime/store/StoreSdkNode.js.map +1 -0
  62. package/build/src/runtime/store/StoreSdkWeb.d.ts +10 -0
  63. package/build/src/runtime/store/StoreSdkWeb.js +11 -0
  64. package/build/src/runtime/store/StoreSdkWeb.js.map +1 -0
  65. package/build/src/runtime/store/UsersSdk.d.ts +18 -0
  66. package/build/src/runtime/store/UsersSdk.js +88 -0
  67. package/build/src/runtime/store/UsersSdk.js.map +1 -0
  68. package/build/src/runtime/store/WsClient.d.ts +24 -0
  69. package/build/src/runtime/store/WsClient.js +4 -0
  70. package/build/src/runtime/store/WsClient.js.map +1 -0
  71. package/build/src/runtime/store/WsClientNode.d.ts +24 -0
  72. package/build/src/runtime/store/WsClientNode.js +66 -0
  73. package/build/src/runtime/store/WsClientNode.js.map +1 -0
  74. package/build/src/runtime/store/WsClientWeb.d.ts +23 -0
  75. package/build/src/runtime/store/WsClientWeb.js +73 -0
  76. package/build/src/runtime/store/WsClientWeb.js.map +1 -0
  77. package/package.json +4 -6
  78. package/src/models/HttpHistory.ts +30 -0
  79. package/src/models/HttpProject.ts +82 -1
  80. package/src/models/RequestLog.ts +17 -1
  81. package/src/runtime/node/InteropInterfaces.ts +121 -0
  82. package/src/runtime/node/ProjectParallelRunner.ts +2 -9
  83. package/src/runtime/node/ProjectRequestRunner.ts +42 -61
  84. package/src/runtime/node/ProjectRunner.ts +1 -60
  85. package/src/runtime/node/ProjectRunnerWorker.ts +2 -1
  86. package/src/runtime/store/AuthSdk.ts +126 -0
  87. package/src/runtime/store/BackendSdk.ts +25 -0
  88. package/src/runtime/store/HistorySdk.ts +157 -0
  89. package/src/runtime/store/Http.ts +18 -0
  90. package/src/runtime/store/HttpNode.ts +100 -0
  91. package/src/runtime/store/HttpWeb.ts +89 -0
  92. package/src/runtime/store/ProjectsSdk.ts +147 -0
  93. package/src/runtime/store/RouteBuilder.ts +89 -0
  94. package/src/runtime/store/Sdk.ts +131 -0
  95. package/src/runtime/store/SdkBase.ts +72 -0
  96. package/src/runtime/store/SpacesSdk.ts +190 -0
  97. package/src/runtime/store/StoreSdkNode.ts +13 -0
  98. package/src/runtime/store/StoreSdkWeb.ts +13 -0
  99. package/src/runtime/store/UsersSdk.ts +89 -0
  100. package/src/runtime/store/WsClient.ts +28 -0
  101. package/src/runtime/store/WsClientNode.ts +68 -0
  102. package/src/runtime/store/WsClientWeb.ts +75 -0
  103. package/build/src/models/HistoryIndex.d.ts +0 -45
  104. package/build/src/models/HistoryIndex.js +0 -56
  105. package/build/src/models/HistoryIndex.js.map +0 -1
  106. package/build/src/runtime/store/StoreSdk.d.ts +0 -279
  107. package/build/src/runtime/store/StoreSdk.js +0 -842
  108. package/build/src/runtime/store/StoreSdk.js.map +0 -1
  109. package/src/models/HistoryIndex.ts +0 -81
  110. package/src/runtime/store/StoreSdk.ts +0 -895
@@ -48,6 +48,36 @@ export interface IHttpHistory {
48
48
  midnight?: number;
49
49
  }
50
50
 
51
+ /**
52
+ * An interface used when adding a history in bulk.
53
+ * The store creates a history object propagating meta values defined here
54
+ * onto the history objects.
55
+ * Because of that, bulk operation can only be performed when the requests are made in similar
56
+ * context (app, space, project).
57
+ */
58
+ export interface IHttpHistoryBulkAdd {
59
+ /**
60
+ * Optional user space id. Must be set when the originating request belongs to a user space.
61
+ */
62
+ space?: string;
63
+ /**
64
+ * Optional project id. Must be set when the originating request belongs to a user space.
65
+ */
66
+ project?: string;
67
+ /**
68
+ * Optional application id. Must be set when the application that created this record does not use the concept of a user space.
69
+ */
70
+ app?: string;
71
+ /**
72
+ * The optional request id in the project that generated this log.
73
+ */
74
+ request?: string;
75
+ /**
76
+ * The list of request logs.
77
+ */
78
+ log: IRequestLog[];
79
+ }
80
+
51
81
  /**
52
82
  * An HTTP history is an object containing an information of a request and response
53
83
  * made with the application.
@@ -165,6 +165,38 @@ export interface IProjectRequestIterator {
165
165
  ignore?: string[];
166
166
  }
167
167
 
168
+ export interface IProjectFolderIterator {
169
+ /**
170
+ * The parent folder key or name. Sets the starting point to iterate over the folder.
171
+ */
172
+ parent?: string
173
+ /**
174
+ * When set it includes folders in the current folder and sub-folder according to the order
175
+ * defined in the folder.
176
+ */
177
+ recursive?: boolean;
178
+ /**
179
+ * The list of names or keys to ignore.
180
+ */
181
+ ignore?: string[];
182
+ }
183
+
184
+ export interface IProjectFolderIteratorResult {
185
+ /**
186
+ * The folder.
187
+ */
188
+ folder: ProjectFolder;
189
+ /**
190
+ * Optional parent key.
191
+ */
192
+ parent?: string;
193
+ /**
194
+ * How deep in the structure the folder is located.
195
+ * The indent is relative to the `parent`.
196
+ */
197
+ indent: number;
198
+ }
199
+
168
200
  /**
169
201
  * The new definition of a project in API Client.
170
202
  * Note, this is not the same as future `ApiProject` which is reserved for building APIs
@@ -1233,7 +1265,7 @@ export class HttpProject extends ProjectParent {
1233
1265
  }
1234
1266
 
1235
1267
  /**
1236
- * Iterates over requests in the project,
1268
+ * Iterates over requests in the project.
1237
1269
  */
1238
1270
  * requestIterator(opts: IProjectRequestIterator = {}): Generator<ProjectRequest> {
1239
1271
  const { definitions } = this;
@@ -1285,6 +1317,55 @@ export class HttpProject extends ProjectParent {
1285
1317
  }
1286
1318
  }
1287
1319
 
1320
+ /**
1321
+ * Iterates over requests in the project.
1322
+ * @param opts Iterator configuration options
1323
+ * @param indent Used internally to add `indent` to the result
1324
+ */
1325
+ * folderIterator(opts: IProjectFolderIterator={}, indent=0): Generator<IProjectFolderIteratorResult> {
1326
+ const { definitions } = this;
1327
+ const { ignore=[], parent, recursive } = opts;
1328
+ const root = parent ? this.findFolder(parent) : this;
1329
+ if (!root) {
1330
+ throw new Error(`The parent folder not found: ${parent}.`);
1331
+ }
1332
+ const items = root.items;
1333
+ if (!items || !items.length) {
1334
+ return;
1335
+ }
1336
+ for (const item of items) {
1337
+ if (item.kind !== ProjectFolderKind) {
1338
+ continue;
1339
+ }
1340
+ const folder = definitions.folders.find(i => i.key === item.key);
1341
+ if (!folder) {
1342
+ // missing definition.
1343
+ continue;
1344
+ }
1345
+ if (ignore.includes(folder.key) || (folder.info.name && ignore.includes(folder.info.name))) {
1346
+ continue;
1347
+ }
1348
+ const result: IProjectFolderIteratorResult = {
1349
+ folder,
1350
+ indent,
1351
+ }
1352
+ if (parent) {
1353
+ result.parent = parent;
1354
+ }
1355
+ yield result;
1356
+ if (recursive) {
1357
+ const it = this.folderIterator({
1358
+ parent: folder.key,
1359
+ recursive,
1360
+ ignore,
1361
+ }, indent + 1);
1362
+ for (const f of it) {
1363
+ yield f;
1364
+ }
1365
+ }
1366
+ }
1367
+ }
1368
+
1288
1369
  [Symbol.iterator](): Generator<ProjectRequest> {
1289
1370
  return this.requestIterator({
1290
1371
  recursive: true,
@@ -28,6 +28,10 @@ export interface IRequestLog {
28
28
  * Request and response size. Some HTTP clients may not give this information.
29
29
  */
30
30
  size?: IRequestsSize;
31
+ /**
32
+ * Optional request ID defined on an HTTP project that triggered this log.
33
+ */
34
+ requestId?: string;
31
35
  }
32
36
 
33
37
  /**
@@ -51,6 +55,10 @@ export class RequestLog {
51
55
  * Request and response size. Some HTTP clients may not give this information.
52
56
  */
53
57
  size?: RequestsSize;
58
+ /**
59
+ * Optional request ID defined on an HTTP project that triggered this log.
60
+ */
61
+ requestId?: string;
54
62
 
55
63
  static fromRequest(request: ISentRequest): RequestLog {
56
64
  return new RequestLog({
@@ -88,7 +96,7 @@ export class RequestLog {
88
96
  * Creates a new response clearing anything that is so far defined.
89
97
  */
90
98
  new(init: IRequestLog): void {
91
- const { request, response, redirects, size } = init;
99
+ const { request, response, redirects, size, requestId } = init;
92
100
  this.kind = Kind;
93
101
  if (request) {
94
102
  this.request = new SentRequest(request);
@@ -112,6 +120,11 @@ export class RequestLog {
112
120
  } else {
113
121
  this.size = undefined;
114
122
  }
123
+ if (requestId) {
124
+ this.requestId = requestId;
125
+ } else {
126
+ this.requestId = undefined;
127
+ }
115
128
  }
116
129
 
117
130
  toJSON(): IRequestLog {
@@ -130,6 +143,9 @@ export class RequestLog {
130
143
  if (this.response) {
131
144
  result.response = this.response.toJSON();
132
145
  }
146
+ if (this.requestId) {
147
+ result.requestId = this.requestId;
148
+ }
133
149
  return result;
134
150
  }
135
151
 
@@ -0,0 +1,121 @@
1
+ import { IProjectRequestIterator, IHttpProject } from '../../models/HttpProject.js';
2
+ import { Environment } from '../../models/Environment.js';
3
+ import { Logger } from '../../lib/logging/Logger.js';
4
+ import { IRequestLog } from '../../models/RequestLog.js';
5
+
6
+ export interface ProjectRunnerOptions {
7
+ /**
8
+ * When provided it overrides any project / folder defined environment.
9
+ */
10
+ environment?: Environment;
11
+ /**
12
+ * Additional variables to pass to the selected environment.
13
+ * This can be use to pass system variables, when needed.
14
+ *
15
+ * To use system variables tou can use `init.variables = process.env`;
16
+ */
17
+ variables?: Record<string, string>;
18
+ /**
19
+ * Overrides the default logger (console).
20
+ */
21
+ logger?: Logger;
22
+ /**
23
+ * The event target to use.
24
+ * By default it creates its own target.
25
+ */
26
+ eventTarget?: EventTarget;
27
+ }
28
+
29
+ export interface ProjectRunnerRunOptions extends IProjectRequestIterator {
30
+ }
31
+
32
+ export interface RunResult {
33
+ /**
34
+ * The key of the request from the HttpProject that was executed.
35
+ */
36
+ key: string;
37
+ /**
38
+ * The key of parent folder of the executed request.
39
+ */
40
+ parent?: string;
41
+ /**
42
+ * Set when a fatal error occurred so the request couldn't be executed.
43
+ * This is not the same as error reported during a request. The log's response can still be IResponseError.
44
+ */
45
+ error?: boolean;
46
+ /**
47
+ * The error message. Always set when the `error` is `true`.
48
+ */
49
+ errorMessage?: string;
50
+ /**
51
+ * The request log.
52
+ * Always set when the `error` is `false`.
53
+ */
54
+ log?: IRequestLog;
55
+ }
56
+
57
+ export interface IProjectParallelRunnerOptions extends IProjectRunnerOptions {
58
+ }
59
+
60
+ export interface IProjectParallelWorkerOptions extends IProjectRunnerOptions {
61
+ project: IHttpProject;
62
+ }
63
+
64
+ export interface IProjectRunnerOptions {
65
+ /**
66
+ * The environment to use.
67
+ * This can be a name or the key of the environment located under the parent or root.
68
+ * It can also be a path to the environment definition. If the file exists it is used. Otherwise it tried to
69
+ * find the environment in the project.
70
+ */
71
+ environment?: string;
72
+ /**
73
+ * The parent folder to execute.
74
+ */
75
+ parent?: string;
76
+ /**
77
+ * The names or the keys of requests to execute.
78
+ * This can be used to limit the number of requests.
79
+ */
80
+ request?: string[];
81
+ /**
82
+ * The number of times the execution should be repeated.
83
+ * Default to 1.
84
+ */
85
+ iterations?: number;
86
+ /**
87
+ * The number of milliseconds to wait between each iteration.
88
+ * Default to the next frame (vary from 1 to tens of milliseconds).
89
+ */
90
+ iterationDelay?: number;
91
+ /**
92
+ * When set it performs parallel execution for each iteration.
93
+ * The number of executions at the same time depends on the number of processor cores
94
+ * available on the current machine. The maximum of the parallel execution
95
+ * is the number of available cores. When the `iterations` number is higher
96
+ * then the "rest" is added to the iterations per each core.
97
+ */
98
+ parallel?: boolean;
99
+ /**
100
+ * When set it includes requests in the current folder and sub-folder according to the order
101
+ * defined in the folder.
102
+ */
103
+ recursive?: boolean;
104
+ /**
105
+ * The opposite of the `requests`. The list of names or keys of requests or folders to ignore.
106
+ * Note, ignore is tested before the `requests`.
107
+ */
108
+ ignore?: string[];
109
+ /**
110
+ * The logger to use with the request factory.
111
+ * When not set it uses the dummy logger (no output).
112
+ */
113
+ logger?: Logger;
114
+ /**
115
+ * When true it copies all system variables to the execution environment.
116
+ * When an array of strings, only takes system variables that are listed in the array.
117
+ * When a map, it uses this map as a list of variables.
118
+ * When not set it does not read system variables.
119
+ */
120
+ variables?: boolean | string[] | Record<string, string>;
121
+ }
@@ -2,10 +2,10 @@ import cluster, { Worker } from 'cluster';
2
2
  import { cpus } from 'os';
3
3
  import { dirname, join } from 'path';
4
4
  import { fileURLToPath } from 'url';
5
- import { HttpProject, IHttpProject } from '../../models/HttpProject.js';
6
- import { IProjectRunnerOptions } from './ProjectRunner.js';
5
+ import { HttpProject } from '../../models/HttpProject.js';
7
6
  import { IProjectExecutionLog, IProjectExecutionIteration } from '../reporters/Reporter.js';
8
7
  import { BaseRunner } from './BaseRunner.js';
8
+ import { IProjectParallelRunnerOptions, IProjectParallelWorkerOptions } from './InteropInterfaces.js'
9
9
 
10
10
  const numCPUs = cpus().length;
11
11
  const __dirname = dirname(fileURLToPath(import.meta.url));
@@ -40,13 +40,6 @@ export interface IWorkerMessage {
40
40
  data?: unknown;
41
41
  }
42
42
 
43
- export interface IProjectParallelRunnerOptions extends IProjectRunnerOptions {
44
- }
45
-
46
- export interface IProjectParallelWorkerOptions extends IProjectRunnerOptions {
47
- project: IHttpProject;
48
- }
49
-
50
43
  export interface ProjectParallelRunner {
51
44
  /**
52
45
  * Dispatched when a status of a worker change.
@@ -6,64 +6,14 @@ import { Property } from '../../models/Property.js';
6
6
  import { ProjectFolder, Kind as ProjectFolderKind } from '../../models/ProjectFolder.js';
7
7
  import { ProjectRequest } from '../../models/ProjectRequest.js';
8
8
  import { IHttpRequest } from '../../models/HttpRequest.js';
9
- import { HttpProject, IProjectRequestIterator } from '../../models/HttpProject.js';
9
+ import { HttpProject } from '../../models/HttpProject.js';
10
10
  import { SentRequest } from '../../models/SentRequest.js';
11
11
  import { ErrorResponse } from '../../models/ErrorResponse.js';
12
12
  import { VariablesStore } from './VariablesStore.js';
13
13
  import { VariablesProcessor } from '../variables/VariablesProcessor.js';
14
14
  import { RequestFactory } from './RequestFactory.js';
15
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
- }
16
+ import { ProjectRunnerOptions, ProjectRunnerRunOptions, RunResult } from './InteropInterfaces.js';
67
17
 
68
18
  export interface ProjectRequestRunner {
69
19
  /**
@@ -130,20 +80,50 @@ export class ProjectRequestRunner extends EventEmitter {
130
80
  const { project } = this;
131
81
  const executed: RunResult[] = [];
132
82
  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);
83
+ const info = await this._runItem(request);
142
84
  executed.push(info);
143
85
  }
144
86
  return executed;
145
87
  }
146
88
 
89
+ /**
90
+ * Allows to iterate over project requests recursively and execute each request
91
+ * in order. The generator yields the `RunResult` for the request.
92
+ *
93
+ * Example:
94
+ *
95
+ * ```javascript
96
+ * const runner = new ProjectRequestRunner(...);
97
+ * for await (let runResult of runner) {
98
+ * console.log(runResult);
99
+ * }
100
+ * ```
101
+ */
102
+ async* [Symbol.asyncIterator](): AsyncGenerator<RunResult> {
103
+ const { project } = this;
104
+ for (const request of project.requestIterator({ recursive: true })) {
105
+ const info = await this._runItem(request);
106
+ yield info;
107
+ }
108
+ }
109
+
110
+ private async _runItem(request: ProjectRequest): Promise<RunResult> {
111
+ const folder = request.getParent();
112
+ const parent = folder || this.project;
113
+ let variables: Record<string, string>;
114
+ if (VariablesStore.has(parent)) {
115
+ variables = VariablesStore.get(parent);
116
+ } else {
117
+ variables = await this.getVariables(parent);
118
+ VariablesStore.set(parent, variables);
119
+ }
120
+ const info = await this.execute(request, variables);
121
+ if (folder && folder !== this.project) {
122
+ info.parent = folder.key;
123
+ }
124
+ return info;
125
+ }
126
+
147
127
  protected async execute(request: ProjectRequest, variables: Record<string, string>): Promise<RunResult> {
148
128
  const config = request.getConfig();
149
129
  const factory = new RequestFactory(this.eventTarget);
@@ -189,6 +169,7 @@ export class ProjectRequestRunner extends EventEmitter {
189
169
  this.emit('request', request.key, { ...requestCopy });
190
170
  await factory.processRequestLogic(requestCopy);
191
171
  const result = await factory.executeRequest(requestCopy);
172
+ result.requestId = request.key;
192
173
  await factory.processResponse(result);
193
174
  request.setLog(result);
194
175
  info.log = result;
@@ -3,75 +3,16 @@ import { HttpProject } from '../../models/HttpProject.js';
3
3
  import { ProjectFolder } from '../../models/ProjectFolder.js';
4
4
  import { Environment, IEnvironment } from '../../models/Environment.js';
5
5
  import { DummyLogger } from '../../lib/logging/DummyLogger.js';
6
- import { Logger } from '../../lib/logging/Logger.js';
7
6
  import { IRequestLog } from '../../models/RequestLog.js';
8
7
  import { IHttpRequest } from '../../models/HttpRequest.js';
9
8
  import { ProjectRequestRunner } from './ProjectRequestRunner.js';
10
9
  import { IProjectExecutionIteration, IProjectExecutionLog } from '../reporters/Reporter.js';
11
10
  import { pathExists, readJson } from '../../lib/fs/Fs.js';
12
11
  import { BaseRunner } from './BaseRunner.js';
12
+ import { IProjectRunnerOptions } from './InteropInterfaces.js';
13
13
 
14
14
  type ProjectParent = HttpProject | ProjectFolder;
15
15
 
16
- export interface IProjectRunnerOptions {
17
- /**
18
- * The environment to use.
19
- * This can be a name or the key of the environment located under the parent or root.
20
- * It can also be a path to the environment definition. If the file exists it is used. Otherwise it tried to
21
- * find the environment in the project.
22
- */
23
- environment?: string;
24
- /**
25
- * The parent folder to execute.
26
- */
27
- parent?: string;
28
- /**
29
- * The names or the keys of requests to execute.
30
- * This can be used to limit the number of requests.
31
- */
32
- request?: string[];
33
- /**
34
- * The number of times the execution should be repeated.
35
- * Default to 1.
36
- */
37
- iterations?: number;
38
- /**
39
- * The number of milliseconds to wait between each iteration.
40
- * Default to the next frame (vary from 1 to tens of milliseconds).
41
- */
42
- iterationDelay?: number;
43
- /**
44
- * When set it performs parallel execution for each iteration.
45
- * The number of executions at the same time depends on the number of processor cores
46
- * available on the current machine. The maximum of the parallel execution
47
- * is the number of available cores. When the `iterations` number is higher
48
- * then the "rest" is added to the iterations per each core.
49
- */
50
- parallel?: boolean;
51
- /**
52
- * When set it includes requests in the current folder and sub-folder according to the order
53
- * defined in the folder.
54
- */
55
- recursive?: boolean;
56
- /**
57
- * The opposite of the `requests`. The list of names or keys of requests or folders to ignore.
58
- * Note, ignore is tested before the `requests`.
59
- */
60
- ignore?: string[];
61
- /**
62
- * The logger to use with the request factory.
63
- * When not set it uses the dummy logger (no output).
64
- */
65
- logger?: Logger;
66
- /**
67
- * When true it copies all system variables to the execution environment.
68
- * When an array of strings, only takes system variables that are listed in the array.
69
- * When a map, it uses this map as a list of variables.
70
- * When not set it does not read system variables.
71
- */
72
- variables?: boolean | string[] | Record<string, string>;
73
- }
74
-
75
16
  export interface ProjectRunner {
76
17
  /**
77
18
  * Event dispatched when an iteration is about to start.
@@ -3,7 +3,8 @@ import process from 'process';
3
3
  import cluster from 'cluster';
4
4
  import { HttpProject } from '../../models/HttpProject.js';
5
5
  import { IProjectExecutionLog } from '../reporters/Reporter.js';
6
- import { IWorkerMessage, IProjectParallelWorkerOptions } from './ProjectParallelRunner.js';
6
+ import { IWorkerMessage } from './ProjectParallelRunner.js';
7
+ import { IProjectParallelWorkerOptions } from './InteropInterfaces.js';
7
8
  import { sleep } from '../../lib/timers/Timers.js';
8
9
  import { ProjectRunner } from './ProjectRunner.js';
9
10
 
@@ -0,0 +1,126 @@
1
+ import { SdkBase, IStoreTokenInfo } from './SdkBase.js';
2
+ import { RouteBuilder } from './RouteBuilder.js';
3
+ import { Headers } from '../../lib/headers/Headers.js';
4
+ import WebSocketNode from 'ws';
5
+
6
+ export class AuthSdk extends SdkBase {
7
+ protected getExpires(headers: Headers): number | undefined {
8
+ const expires = headers.get('expires');
9
+ if (!expires) {
10
+ return undefined;
11
+ }
12
+ const d = new Date(expires);
13
+ const time = d.getTime();
14
+ if (Number.isNaN(time)) {
15
+ console.warn(`Invalid session response: the expires header cannot be parsed.`);
16
+ return undefined;
17
+ }
18
+ return time;
19
+ }
20
+
21
+ /**
22
+ * Creates unauthenticated session in the backend.
23
+ * @returns The JWT for unauthenticated user.
24
+ */
25
+ async createSession(): Promise<IStoreTokenInfo> {
26
+ const url = this.sdk.getUrl(RouteBuilder.sessions());
27
+ // console.log('Create session: ', url);
28
+ const result = await this.sdk.http.post(url.toString());
29
+ this.inspectCommonStatusCodes(result.status);
30
+ if (result.status !== 200) {
31
+ throw new Error(`Unable to create the session. Invalid response status: ${result.status}`);
32
+ }
33
+ if (!result.body) {
34
+ throw new Error(`Unable to create the session. Response has no token.`);
35
+ }
36
+ const info: IStoreTokenInfo = {
37
+ token: result.body,
38
+ };
39
+ const expires = this.getExpires(result.headers);
40
+ if (expires) {
41
+ info.expires = expires;
42
+ }
43
+ return info;
44
+ }
45
+
46
+ /**
47
+ * Initializes the authentication session.
48
+ * @param token The unauthenticated session JWT. Required when not set on the class.
49
+ * @returns The location of the authorization endpoint.
50
+ */
51
+ async createAuthSession(token?: string, loginPath = '/auth/login'): Promise<string> {
52
+ const url = this.sdk.getUrl(loginPath);
53
+ const result = await this.sdk.http.post(url.toString(), { token });
54
+ this.inspectCommonStatusCodes(result.status);
55
+ const loc = result.headers.get('location');
56
+ if (!loc) {
57
+ throw new Error(`The location header not returned by the server.`);
58
+ }
59
+ return loc;
60
+ }
61
+
62
+ /**
63
+ * Listens to the first message coming to the client from the auth endpoint.
64
+ * @param authPath The authorization path returned by the server info or 401 response.
65
+ * @param token Optional token to use.
66
+ */
67
+ async listenAuth(authPath: string, token?: string): Promise<void> {
68
+ const url = this.sdk.getUrl(authPath);
69
+ const client = await this.sdk.ws.createAndConnect(url.toString(), token);
70
+ return new Promise((resolve, reject) => {
71
+ const { sdk } = this;
72
+ async function finishData(data: any): Promise<void> {
73
+ let message: any;
74
+ try {
75
+ message = JSON.parse(data.toString());
76
+ await sdk.ws.disconnect(client);
77
+ } catch (cause) {
78
+ reject(cause);
79
+ return;
80
+ }
81
+ if (message.status === 'OK') {
82
+ resolve();
83
+ } else {
84
+ reject(new Error(message.message || 'Unknown error'));
85
+ }
86
+ }
87
+ const typedNode = client as WebSocketNode;
88
+ if (typeof typedNode.on === 'function') {
89
+ typedNode.on('message', (data: Buffer) => {
90
+ finishData(data);
91
+ });
92
+ } else {
93
+ const typedWeb = client as WebSocket;
94
+ typedWeb.addEventListener('message', (event) => {
95
+ finishData(event.data);
96
+ });
97
+ }
98
+ });
99
+ }
100
+
101
+ /**
102
+ * Renews authenticated token to a new one when the token expires.
103
+ * @param token Optional token to use.
104
+ * @returns
105
+ */
106
+ async renewToken(token = this.sdk.token): Promise<IStoreTokenInfo> {
107
+ const authPath = RouteBuilder.sessionRenew();
108
+ const url = this.sdk.getUrl(authPath);
109
+ const result = await this.sdk.http.post(url.toString(), { token });
110
+ this.inspectCommonStatusCodes(result.status);
111
+ if (result.status !== 200) {
112
+ throw new Error(`Unable to renew the token. Invalid response status: ${result.status}`);
113
+ }
114
+ if (!result.body) {
115
+ throw new Error(`Unable to create the session. Response has no token.`);
116
+ }
117
+ const info: IStoreTokenInfo = {
118
+ token: result.body,
119
+ };
120
+ const expires = this.getExpires(result.headers);
121
+ if (expires) {
122
+ info.expires = expires;
123
+ }
124
+ return info;
125
+ }
126
+ }