@api-client/core 0.3.1 → 0.3.4

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 (183) hide show
  1. package/build/browser.d.ts +1 -1
  2. package/build/index.d.ts +2 -2
  3. package/build/index.js.map +1 -1
  4. package/build/src/models/Backend.d.ts +22 -0
  5. package/build/src/runtime/store/StoreSdk.d.ts +192 -12
  6. package/build/src/runtime/store/StoreSdk.js +592 -53
  7. package/build/src/runtime/store/StoreSdk.js.map +1 -1
  8. package/package.json +3 -1
  9. package/src/data/DataReader.ts +11 -0
  10. package/src/data/DataUtils.ts +108 -0
  11. package/src/data/JmesparthReader.ts +26 -0
  12. package/src/data/Json2Xml.ts +190 -0
  13. package/src/data/JsonReader.ts +41 -0
  14. package/src/data/PayloadPointer.ts +48 -0
  15. package/src/data/RequestDataExtractor.ts +133 -0
  16. package/src/data/UrlEncodedReader.ts +20 -0
  17. package/src/data/XmlReader.ts +103 -0
  18. package/src/events/BaseEvents.ts +259 -0
  19. package/src/events/CustomEvent.ts +27 -0
  20. package/src/events/EventTypes.ts +19 -0
  21. package/src/events/Events.ts +19 -0
  22. package/src/events/authorization/AuthorizationEventTypes.ts +22 -0
  23. package/src/events/authorization/AuthorizationEvents.ts +61 -0
  24. package/src/events/cookies/CookieEventTypes.ts +13 -0
  25. package/src/events/cookies/CookieEvents.ts +157 -0
  26. package/src/events/encryption/EncryptionEventTypes.ts +4 -0
  27. package/src/events/encryption/EncryptionEvents.ts +51 -0
  28. package/src/events/environment/EnvironmentEventTypes.ts +3 -0
  29. package/src/events/environment/EnvironmentEvents.ts +24 -0
  30. package/src/events/models/ClientCertificateEvents.ts +87 -0
  31. package/src/events/models/ModelEventTypes.ts +47 -0
  32. package/src/events/models/ModelEvents.ts +7 -0
  33. package/src/events/models/ProjectEvents.ts +331 -0
  34. package/src/events/process/ProcessEventTypes.ts +5 -0
  35. package/src/events/process/ProcessEvents.ts +76 -0
  36. package/src/events/readme.md +22 -0
  37. package/src/events/reporting/ReportingEventTypes.ts +3 -0
  38. package/src/events/reporting/ReportingEvents.ts +28 -0
  39. package/src/events/telemetry/TelemetryEventTypes.ts +10 -0
  40. package/src/events/telemetry/TelemetryEvents.ts +156 -0
  41. package/src/lib/cookies/Cookie.ts +312 -0
  42. package/src/lib/cookies/Cookies.ts +326 -0
  43. package/src/lib/cookies/Utils.ts +168 -0
  44. package/src/lib/headers/Headers.ts +219 -0
  45. package/src/lib/logging/DefaultLogger.ts +19 -0
  46. package/src/lib/logging/DummyLogger.ts +21 -0
  47. package/src/lib/logging/Logger.ts +16 -0
  48. package/src/lib/transformers/PayloadSerializer.ts +332 -0
  49. package/src/lib/transformers/Utils.ts +18 -0
  50. package/src/lib/uuid.ts +40 -0
  51. package/src/mocking/LegacyInterfaces.ts +52 -0
  52. package/src/mocking/LegacyMock.ts +37 -0
  53. package/src/mocking/legacy/Authorization.ts +39 -0
  54. package/src/mocking/legacy/Certificates.ts +145 -0
  55. package/src/mocking/legacy/Cookies.ts +51 -0
  56. package/src/mocking/legacy/HostRules.ts +43 -0
  57. package/src/mocking/legacy/Http.ts +236 -0
  58. package/src/mocking/legacy/HttpResponse.ts +106 -0
  59. package/src/mocking/legacy/RestApi.ts +68 -0
  60. package/src/mocking/legacy/Urls.ts +44 -0
  61. package/src/mocking/legacy/Variables.ts +53 -0
  62. package/src/models/ArcResponse.ts +166 -0
  63. package/src/models/Authorization.ts +481 -0
  64. package/src/models/AuthorizationData.ts +60 -0
  65. package/src/models/Backend.ts +107 -0
  66. package/src/models/ClientCertificate.ts +68 -0
  67. package/src/models/Environment.ts +279 -0
  68. package/src/models/ErrorResponse.ts +101 -0
  69. package/src/models/HistoryIndex.ts +76 -0
  70. package/src/models/HistoryRequest.ts +28 -0
  71. package/src/models/HostRule.ts +163 -0
  72. package/src/models/HttpCookie.ts +285 -0
  73. package/src/models/HttpProject.ts +1294 -0
  74. package/src/models/HttpProjectListItem.ts +23 -0
  75. package/src/models/HttpRequest.ts +124 -0
  76. package/src/models/HttpResponse.ts +143 -0
  77. package/src/models/License.ts +113 -0
  78. package/src/models/ProjectDefinitionProperty.ts +40 -0
  79. package/src/models/ProjectFolder.ts +439 -0
  80. package/src/models/ProjectItem.ts +135 -0
  81. package/src/models/ProjectParent.ts +113 -0
  82. package/src/models/ProjectRequest.ts +277 -0
  83. package/src/models/ProjectSchema.ts +202 -0
  84. package/src/models/Property.ts +423 -0
  85. package/src/models/Provider.ts +98 -0
  86. package/src/models/README.md +20 -0
  87. package/src/models/Request.ts +452 -0
  88. package/src/models/RequestActions.ts +163 -0
  89. package/src/models/RequestAuthorization.ts +115 -0
  90. package/src/models/RequestConfig.ts +317 -0
  91. package/src/models/RequestLog.ts +159 -0
  92. package/src/models/RequestTime.ts +108 -0
  93. package/src/models/RequestUiMeta.ts +258 -0
  94. package/src/models/RequestsSize.ts +65 -0
  95. package/src/models/ResponseAuthorization.ts +104 -0
  96. package/src/models/ResponseRedirect.ts +158 -0
  97. package/src/models/RevisionInfo.ts +37 -0
  98. package/src/models/SentRequest.ts +125 -0
  99. package/src/models/SerializablePayload.ts +68 -0
  100. package/src/models/Server.ts +153 -0
  101. package/src/models/Thing.ts +110 -0
  102. package/src/models/Url.ts +90 -0
  103. package/src/models/User.ts +120 -0
  104. package/src/models/WebApi.ts +234 -0
  105. package/src/models/WebApiIndex.ts +122 -0
  106. package/src/models/Workspace.ts +182 -0
  107. package/src/models/actions/Action.ts +213 -0
  108. package/src/models/actions/ActionView.ts +40 -0
  109. package/src/models/actions/Condition.ts +207 -0
  110. package/src/models/actions/ConditionView.ts +42 -0
  111. package/src/models/actions/Enums.ts +29 -0
  112. package/src/models/actions/RunnableAction.ts +144 -0
  113. package/src/models/actions/runnable/DeleteCookieAction.ts +113 -0
  114. package/src/models/actions/runnable/Runnable.ts +9 -0
  115. package/src/models/actions/runnable/SetCookieAction.ts +216 -0
  116. package/src/models/actions/runnable/SetVariableAction.ts +81 -0
  117. package/src/models/legacy/DataExport.ts +172 -0
  118. package/src/models/legacy/Normalizer.ts +110 -0
  119. package/src/models/legacy/actions/Actions.ts +269 -0
  120. package/src/models/legacy/authorization/Authorization.ts +572 -0
  121. package/src/models/legacy/models/ApiTypes.ts +202 -0
  122. package/src/models/legacy/models/ArcLegacyProject.ts +39 -0
  123. package/src/models/legacy/models/AuthData.ts +17 -0
  124. package/src/models/legacy/models/ClientCertificate.ts +95 -0
  125. package/src/models/legacy/models/Cookies.ts +52 -0
  126. package/src/models/legacy/models/HostRule.ts +35 -0
  127. package/src/models/legacy/models/RestApi.ts +49 -0
  128. package/src/models/legacy/models/UrlHistory.ts +37 -0
  129. package/src/models/legacy/models/Variable.ts +43 -0
  130. package/src/models/legacy/models/base.d.ts +95 -0
  131. package/src/models/legacy/request/ArcRequest.ts +405 -0
  132. package/src/models/legacy/request/ArcResponse.ts +177 -0
  133. package/src/models/legacy/request/HistoryData.ts +47 -0
  134. package/src/models/legacy/request/Legacy.ts +45 -0
  135. package/src/models/legacy/request/RequestBody.ts +87 -0
  136. package/src/models/transformers/ArcDexieTransformer.ts +323 -0
  137. package/src/models/transformers/ArcLegacyNormalizer.ts +85 -0
  138. package/src/models/transformers/ArcLegacyTransformer.ts +200 -0
  139. package/src/models/transformers/ArcPouchTransformer.ts +184 -0
  140. package/src/models/transformers/BaseTransformer.ts +116 -0
  141. package/src/models/transformers/ImportUtils.ts +141 -0
  142. package/src/models/transformers/LegacyDataExportToApiProject.ts +76 -0
  143. package/src/models/transformers/LegacyExportProcessor.ts +252 -0
  144. package/src/models/transformers/PostmanBackupTransformer.ts +306 -0
  145. package/src/models/transformers/PostmanDataTransformer.ts +50 -0
  146. package/src/models/transformers/PostmanTransformer.ts +106 -0
  147. package/src/models/transformers/PostmanV21Transformer.ts +311 -0
  148. package/src/models/transformers/PostmanV2Transformer.ts +308 -0
  149. package/src/models/transformers/har.ts +865 -0
  150. package/src/runtime/actions/ActionRunner.ts +83 -0
  151. package/src/runtime/actions/ConditionRunner.ts +194 -0
  152. package/src/runtime/actions/RunnableCondition.ts +57 -0
  153. package/src/runtime/actions/runnable/ActionRunnable.ts +19 -0
  154. package/src/runtime/actions/runnable/DeleteCookieRunnable.ts +39 -0
  155. package/src/runtime/actions/runnable/SetCookieRunnable.ts +92 -0
  156. package/src/runtime/actions/runnable/SetVariableRunnable.ts +53 -0
  157. package/src/runtime/http-engine/ArcEngine.ts +1064 -0
  158. package/src/runtime/http-engine/Errors.ts +13 -0
  159. package/src/runtime/http-engine/FormData.ts +85 -0
  160. package/src/runtime/http-engine/HttpEngine.ts +874 -0
  161. package/src/runtime/http-engine/HttpErrorCodes.ts +270 -0
  162. package/src/runtime/http-engine/NodeEngine.ts +787 -0
  163. package/src/runtime/http-engine/NodeEngineDirect.ts +476 -0
  164. package/src/runtime/http-engine/PayloadSupport.ts +84 -0
  165. package/src/runtime/http-engine/RequestUtils.ts +164 -0
  166. package/src/runtime/http-engine/ntlm/Des.ts +345 -0
  167. package/src/runtime/http-engine/ntlm/MD4.ts +135 -0
  168. package/src/runtime/http-engine/ntlm/NtlmAuth.ts +186 -0
  169. package/src/runtime/http-engine/ntlm/NtlmMessage.ts +57 -0
  170. package/src/runtime/modules/BasicAuthCache.ts +133 -0
  171. package/src/runtime/modules/ExecutionResponse.ts +4 -0
  172. package/src/runtime/modules/ModulesRegistry.ts +136 -0
  173. package/src/runtime/modules/RequestAuthorization.ts +110 -0
  174. package/src/runtime/modules/RequestCookies.ts +145 -0
  175. package/src/runtime/node/ProjectRunner.ts +275 -0
  176. package/src/runtime/node/RequestFactory.ts +422 -0
  177. package/src/runtime/node/VariablesStore.ts +25 -0
  178. package/src/runtime/store/StoreSdk.ts +838 -0
  179. package/src/runtime/variables/Cache.ts +53 -0
  180. package/src/runtime/variables/EvalFunctions.ts +132 -0
  181. package/src/runtime/variables/ProjectVariables.ts +6 -0
  182. package/src/runtime/variables/VariablesProcessor.ts +543 -0
  183. package/src/runtime/variables/VariablesTokenizer.ts +55 -0
@@ -0,0 +1,452 @@
1
+ import { IRequestConfig, RequestConfig } from './RequestConfig.js';
2
+ import { Thing, IThing, Kind as ThingKind } from './Thing.js';
3
+ import { IRequestActions, RequestActions } from './RequestActions.js';
4
+ import { IRequestCertificate } from './ClientCertificate.js';
5
+ import { IRequestAuthorization, RequestAuthorization } from './RequestAuthorization.js';
6
+ import { IRequestLog, RequestLog, Kind as LogKind } from './RequestLog.js';
7
+ import { SentRequest } from './SentRequest.js';
8
+ import { ErrorResponse } from './ErrorResponse.js';
9
+ import { ArcResponse } from './ArcResponse.js';
10
+ import { RequestsSize } from './RequestsSize.js';
11
+ import { IHttpRequest, HttpRequest, Kind as HttpRequestKind } from './HttpRequest.js';
12
+ import { ARCSavedRequest, ARCHistoryRequest } from './legacy/request/ArcRequest.js';
13
+ import { ErrorResponse as LegacyErrorResponse, Response as LegacyResponse } from './legacy/request/ArcResponse.js';
14
+ import { PayloadSerializer } from '../lib/transformers/PayloadSerializer.js';
15
+ import { Normalizer } from './legacy/Normalizer.js';
16
+
17
+ export const Kind = 'ARC#Request';
18
+ export const createdSymbol = Symbol('created');
19
+ export const updatedSymbol = Symbol('updated');
20
+ export const midnightSymbol = Symbol('midnight');
21
+
22
+ /**
23
+ * The definition of a request object that functions inside ARC
24
+ * with the full configuration.
25
+ */
26
+ export interface IRequest {
27
+ kind: string;
28
+ /**
29
+ * The basic information about the project.
30
+ */
31
+ info: IThing;
32
+ /**
33
+ * The HTTP definition of the request.
34
+ */
35
+ expects: IHttpRequest;
36
+ /**
37
+ * The execution log of the last HTTP request with a response.
38
+ */
39
+ log?: IRequestLog;
40
+ /**
41
+ * Timestamp when the request was last updated.
42
+ */
43
+ updated?: number;
44
+ /**
45
+ * Timestamp when the request was created.
46
+ */
47
+ created?: number;
48
+ /**
49
+ * A timestamp of the midnight when the request object was updated
50
+ */
51
+ midnight?: number;
52
+ /**
53
+ * Request processing configuration.
54
+ */
55
+ config?: IRequestConfig;
56
+ /**
57
+ * Request authorization configuration
58
+ */
59
+ authorization?: IRequestAuthorization[];
60
+ /**
61
+ * Actions to be performed when the request is executed.
62
+ */
63
+ actions?: IRequestActions;
64
+ /**
65
+ * The list of certificates to use with the request.
66
+ */
67
+ clientCertificate?: IRequestCertificate;
68
+ }
69
+
70
+ export class Request {
71
+ [createdSymbol]: number;
72
+ [updatedSymbol]: number;
73
+ [midnightSymbol]: number;
74
+ kind = Kind;
75
+ /**
76
+ * The basic information about the project.
77
+ */
78
+ info: Thing = new Thing();
79
+ /**
80
+ * The HTTP definition of the request.
81
+ */
82
+ expects: HttpRequest = new HttpRequest();
83
+ /**
84
+ * The execution log of the last HTTP request with a response.
85
+ */
86
+ log?: RequestLog;
87
+ /**
88
+ * Request processing configuration.
89
+ */
90
+ config?: RequestConfig;
91
+ /**
92
+ * Request authorization configuration
93
+ */
94
+ authorization?: RequestAuthorization[];
95
+ /**
96
+ * Actions to be performed when the request is executed.
97
+ */
98
+ actions?: RequestActions;
99
+ /**
100
+ * The list of certificates to use with the request.
101
+ */
102
+ clientCertificate?: IRequestCertificate;
103
+
104
+ /**
105
+ * Creates a request from an URL.
106
+ *
107
+ * @param url The Request URL.
108
+ */
109
+ static fromUrl(url: string): Request {
110
+ const now:number = Date.now();
111
+ const request = new Request({
112
+ kind: Kind,
113
+ created: now,
114
+ updated: now,
115
+ expects: {
116
+ kind: HttpRequestKind,
117
+ method: 'GET',
118
+ url,
119
+ },
120
+ info: {
121
+ kind: ThingKind,
122
+ name: url,
123
+ },
124
+ });
125
+ return request;
126
+ }
127
+
128
+ /**
129
+ * Creates a request from a name.
130
+ *
131
+ * @param name The Request name.
132
+ */
133
+ static fromName(name: string): Request {
134
+ const now:number = Date.now();
135
+ const request = new Request({
136
+ kind: Kind,
137
+ created: now,
138
+ updated: now,
139
+ expects: {
140
+ kind: HttpRequestKind,
141
+ method: 'GET',
142
+ url: '',
143
+ },
144
+ info: {
145
+ kind: ThingKind,
146
+ name,
147
+ },
148
+ });
149
+ return request;
150
+ }
151
+
152
+ /**
153
+ * Creates a request from an HttpRequest definition.
154
+ *
155
+ * @param info The request data.
156
+ */
157
+ static fromHttpRequest(info: IHttpRequest): Request {
158
+ const now:number = Date.now();
159
+ const request = new Request({
160
+ kind: Kind,
161
+ created: now,
162
+ updated: now,
163
+ expects: {
164
+ kind: HttpRequestKind,
165
+ method: info.method,
166
+ url: info.url,
167
+ headers: info.headers,
168
+ payload: info.payload,
169
+ },
170
+ info: {
171
+ kind: ThingKind,
172
+ name: info.url,
173
+ },
174
+ });
175
+ return request;
176
+ }
177
+
178
+ static async fromLegacy(request: ARCSavedRequest|ARCHistoryRequest): Promise<Request> {
179
+ const normalized = Normalizer.normalizeRequest(request) as ARCSavedRequest;
180
+ if (!normalized) {
181
+ throw new Error(`Unknown object.`);
182
+ }
183
+ const init:IRequest = {
184
+ kind: Kind,
185
+ expects: {
186
+ kind: HttpRequestKind,
187
+ method: normalized.method || 'GET',
188
+ url: normalized.url || '',
189
+ headers: normalized.headers,
190
+ },
191
+ info: {
192
+ kind: ThingKind,
193
+ name: normalized.name || 'Unnamed request',
194
+ },
195
+ };
196
+ init.created = normalized.created;
197
+ init.updated = normalized.updated;
198
+ if (normalized.actions) {
199
+ init.actions = RequestActions.fromLegacy(normalized.actions).toJSON();
200
+ }
201
+ if (Array.isArray(normalized.authorization) && normalized.authorization.length) {
202
+ init.authorization = normalized.authorization.map((i) => RequestAuthorization.fromLegacy(i).toJSON());
203
+ }
204
+ if (normalized.config) {
205
+ init.config = RequestConfig.fromLegacy(normalized.config).toJSON();
206
+ }
207
+ if (normalized.payload) {
208
+ init.expects.payload = await PayloadSerializer.serialize(normalized.payload);
209
+ } else if (normalized.blob) {
210
+ init.expects.payload = {
211
+ type: 'blob',
212
+ data: normalized.blob,
213
+ };
214
+ } else if (normalized.multipart) {
215
+ init.expects.payload = {
216
+ type: 'formdata',
217
+ data: normalized.multipart,
218
+ };
219
+ }
220
+ const log = new RequestLog();
221
+ if (normalized.transportRequest) {
222
+ const sent = await SentRequest.fromLegacy(normalized.transportRequest);
223
+ log.request = sent;
224
+ }
225
+ if (normalized.response) {
226
+ const typedError = normalized.response as LegacyErrorResponse;
227
+ if (typedError.error) {
228
+ log.response = await ErrorResponse.fromLegacy(typedError);
229
+ } else {
230
+ const typedResponse = normalized.response as LegacyResponse;
231
+ log.response = await ArcResponse.fromLegacy(typedResponse);
232
+ if (Array.isArray(typedResponse.redirects) && typedResponse.redirects.length) {
233
+ const promises = typedResponse.redirects.map((redirect) => log.addLegacyRedirect(redirect));
234
+ await Promise.allSettled(promises);
235
+ }
236
+ if (typedResponse.size) {
237
+ log.size = new RequestsSize(typedResponse.size);
238
+ }
239
+ }
240
+ }
241
+ init.log = log.toJSON();
242
+ return new Request(init);
243
+ }
244
+
245
+ /**
246
+ * @param value The timestamp when the request was created.
247
+ */
248
+ set created(value: number | undefined) {
249
+ if (!value) {
250
+ this[createdSymbol] = Date.now();
251
+ } else {
252
+ this[createdSymbol] = value;
253
+ }
254
+ }
255
+
256
+ /**
257
+ * @returns The timestamp when the request was created.
258
+ */
259
+ get created(): number {
260
+ return this[createdSymbol] || Date.now();
261
+ }
262
+
263
+ /**
264
+ * @param value The timestamp when the request was last updated.
265
+ */
266
+ set updated(value: number | undefined) {
267
+ if (!value) {
268
+ this[updatedSymbol] = this.created;
269
+ } else {
270
+ this[updatedSymbol] = value;
271
+ }
272
+ const d = new Date(this[updatedSymbol]);
273
+ d.setHours(0, 0, 0, 0)
274
+ this[midnightSymbol] = d.getTime();
275
+ }
276
+
277
+ /**
278
+ * @returns The timestamp when the request was last updated.
279
+ */
280
+ get updated(): number {
281
+ return this[updatedSymbol] || this.created;
282
+ }
283
+
284
+ /**
285
+ * @param value The timestamp of the midnight when the request object was updated
286
+ */
287
+ set midnight(value: number | undefined) {
288
+ if (!value) {
289
+ this[midnightSymbol] = this.defaultMidnight();
290
+ } else {
291
+ this[midnightSymbol] = value;
292
+ }
293
+ }
294
+
295
+ /**
296
+ * @returns The timestamp of the midnight when the request object was updated
297
+ */
298
+ get midnight(): number {
299
+ if (this[midnightSymbol]) {
300
+ return this[midnightSymbol];
301
+ }
302
+ return this.defaultMidnight();
303
+ }
304
+
305
+ constructor(input?: string|IRequest) {
306
+ let init: IRequest;
307
+ if (typeof input === 'string') {
308
+ init = JSON.parse(input);
309
+ } else if (typeof input === 'object') {
310
+ init = input;
311
+ } else {
312
+ const now:number = Date.now();
313
+ init = {
314
+ kind: Kind,
315
+ created: now,
316
+ updated: now,
317
+ expects: {
318
+ kind: HttpRequestKind,
319
+ method: 'GET',
320
+ url: '',
321
+ },
322
+ info: {
323
+ kind: ThingKind,
324
+ name: '',
325
+ },
326
+ };
327
+ }
328
+ this.new(init);
329
+ }
330
+
331
+ new(init: IRequest): void {
332
+ const { expects, log, updated, created = Date.now(), midnight, config, authorization, actions, clientCertificate, info } = init;
333
+ if (expects) {
334
+ this.expects = new HttpRequest(expects);
335
+ } else {
336
+ this.expects = new HttpRequest();
337
+ }
338
+ if (info) {
339
+ if (!info.kind) {
340
+ info.kind = ThingKind;
341
+ }
342
+ this.info = new Thing(info);
343
+ } else {
344
+ this.info = new Thing({ kind: ThingKind, name: '' });
345
+ }
346
+ if (log) {
347
+ this.log = new RequestLog(log);
348
+ } else {
349
+ this.log = undefined;
350
+ }
351
+ if (config) {
352
+ this.config = new RequestConfig(config);
353
+ } else {
354
+ this.config = undefined;
355
+ }
356
+ if (Array.isArray(authorization)) {
357
+ this.authorization = authorization.map(i => new RequestAuthorization(i));
358
+ } else {
359
+ this.authorization = undefined;
360
+ }
361
+ this.created = created;
362
+ if (updated) {
363
+ this.updated = updated;
364
+ } else {
365
+ this.updated = this.created;
366
+ }
367
+ if (midnight) {
368
+ this.midnight = midnight;
369
+ }
370
+ if (actions) {
371
+ this.actions = new RequestActions(actions);
372
+ } else {
373
+ this.actions = undefined;
374
+ }
375
+ if (clientCertificate) {
376
+ this.clientCertificate = clientCertificate;
377
+ } else {
378
+ this.clientCertificate = undefined;
379
+ }
380
+ }
381
+
382
+ toJSON(): IRequest {
383
+ const result: IRequest = {
384
+ kind: Kind,
385
+ expects: this.expects.toJSON(),
386
+ info: this.info.toJSON(),
387
+ created: this.created,
388
+ updated: this.updated,
389
+ midnight: this.midnight,
390
+ };
391
+ if (this.log) {
392
+ result.log = this.log.toJSON();
393
+ }
394
+ if (this.config) {
395
+ result.config = this.config.toJSON();
396
+ }
397
+ if (Array.isArray(this.authorization)) {
398
+ result.authorization = this.authorization.map(i => i.toJSON());
399
+ }
400
+ if (this.actions) {
401
+ result.actions = this.actions.toJSON();
402
+ }
403
+ if (this.clientCertificate) {
404
+ result.clientCertificate = this.clientCertificate;
405
+ }
406
+ return result;
407
+ }
408
+
409
+ /**
410
+ * Sets the basic information about a project.
411
+ */
412
+ setInfo(info: IThing): void {
413
+ this.info = new Thing(info);
414
+ this.updated = Date.now();
415
+ }
416
+
417
+ /**
418
+ * If the info object does not exist it is being created.
419
+ * @return The instance of an HTTP request information.
420
+ */
421
+ getExpects(): HttpRequest {
422
+ if (!this.expects) {
423
+ this.expects = new HttpRequest();
424
+ }
425
+ return this.expects;
426
+ }
427
+
428
+ /**
429
+ * @returns The default value for the midnight when the request was last updated.
430
+ */
431
+ defaultMidnight(): number {
432
+ const d = new Date(this.updated);
433
+ d.setHours(0, 0, 0, 0);
434
+ return d.getTime();
435
+ }
436
+
437
+ /**
438
+ * Reads the config from the request. If the config object does not exists it creates one.
439
+ */
440
+ getConfig(): RequestConfig {
441
+ if (!this.config) {
442
+ this.config = new RequestConfig();
443
+ }
444
+ return this.config;
445
+ }
446
+
447
+ setLog(log: IRequestLog): void {
448
+ const info: IRequestLog = { ...log, kind: LogKind };
449
+ this.log = new RequestLog(info);
450
+ this.updated = Date.now();
451
+ }
452
+ }
@@ -0,0 +1,163 @@
1
+ import { IRunnableAction, RunnableAction, Kind as RunnableActionKind } from './actions/RunnableAction.js';
2
+ import { RequestActions as LegacyRequestActions } from './legacy/request/ArcRequest.js';
3
+
4
+ export interface IRequestActions {
5
+ /**
6
+ * Actions to be executed before the request is sent to the transport library.
7
+ */
8
+ request?: IRunnableAction[];
9
+ /**
10
+ * Actions to be executed after the response is fully received but before it is reported back to the application.
11
+ */
12
+ response?: IRunnableAction[];
13
+ }
14
+
15
+ export class RequestActions {
16
+ /**
17
+ * Actions to be executed before the request is sent to the transport library.
18
+ */
19
+ request?: RunnableAction[];
20
+ /**
21
+ * Actions to be executed after the response is fully received but before it is reported back to the application.
22
+ */
23
+ response?: RunnableAction[];
24
+
25
+ static fromLegacy(action: LegacyRequestActions): RequestActions {
26
+ const { request, response } = action;
27
+ const init: IRequestActions = {};
28
+ if (Array.isArray(request) && request.length) {
29
+ init.request = request.map(i => RunnableAction.fromLegacy(i).toJSON());
30
+ }
31
+ if (Array.isArray(response) && response.length) {
32
+ init.response = response.map(i => RunnableAction.fromLegacy(i).toJSON());
33
+ }
34
+ return new RequestActions(init);
35
+ }
36
+
37
+ static isLegacy(input: unknown): boolean {
38
+ const { request=[], response=[] } = input as RequestActions;
39
+ if (!request.length && !response.length) {
40
+ return false;
41
+ }
42
+ if (request[0]) {
43
+ const r = request[0];
44
+ if (r.kind === RunnableActionKind) {
45
+ return false;
46
+ }
47
+ }
48
+ if (response[0]) {
49
+ const r = response[0];
50
+ if (r.kind === RunnableActionKind) {
51
+ return false;
52
+ }
53
+ }
54
+ return true;
55
+ }
56
+
57
+ /**
58
+ * @param input The provider definition used to restore the state.
59
+ */
60
+ constructor(input?: string | IRequestActions) {
61
+ let init: IRequestActions;
62
+ if (typeof input === 'string') {
63
+ init = JSON.parse(input);
64
+ } else if (typeof input === 'object') {
65
+ init = input;
66
+ } else {
67
+ init = {};
68
+ }
69
+ this.new(init);
70
+ }
71
+
72
+ /**
73
+ * Creates a new provider clearing anything that is so far defined.
74
+ *
75
+ * Note, this throws an error when the provider is not an ARC provider object.
76
+ */
77
+ new(init: IRequestActions): void {
78
+ const { request, response } = init;
79
+ if (request) {
80
+ this.request = request.map(i => new RunnableAction(i));
81
+ } else {
82
+ this.request = undefined;
83
+ }
84
+ if (response) {
85
+ this.response = response.map(i => new RunnableAction(i));
86
+ } else {
87
+ this.response = undefined;
88
+ }
89
+ }
90
+
91
+ toJSON(): IRequestActions {
92
+ const result: IRequestActions = {};
93
+ if (this.request) {
94
+ result.request = this.request.map(i => i.toJSON());
95
+ }
96
+ if (this.response) {
97
+ result.response = this.response.map(i => i.toJSON());
98
+ }
99
+ return result;
100
+ }
101
+
102
+ /**
103
+ * Adds a request action to the list of actions.
104
+ * If the list does not exist it's created by this method.
105
+ *
106
+ * @param action The schema of the action.
107
+ * @returns Created instance of the action.
108
+ */
109
+ addRequestAction(action: IRunnableAction): RunnableAction;
110
+ /**
111
+ * Adds a request action to the list of actions.
112
+ * If the list does not exist it's created by this method.
113
+ *
114
+ * @param action The instance of the action.
115
+ * @returns The same instance of the action.
116
+ */
117
+ addRequestAction(action: RunnableAction): RunnableAction;
118
+
119
+ addRequestAction(action: RunnableAction | IRunnableAction): RunnableAction {
120
+ if (!Array.isArray(this.request)) {
121
+ this.request = [];
122
+ }
123
+ let finalAction: RunnableAction;
124
+ if (action instanceof RunnableAction) {
125
+ finalAction = action;
126
+ } else {
127
+ finalAction = new RunnableAction(action);
128
+ }
129
+ this.request.push(finalAction);
130
+ return finalAction;
131
+ }
132
+
133
+ /**
134
+ * Adds a request action to the list of actions.
135
+ * If the list does not exist it's created by this method.
136
+ *
137
+ * @param action The schema of the action.
138
+ * @returns Created instance of the action.
139
+ */
140
+ addResponseAction(action: IRunnableAction): RunnableAction;
141
+ /**
142
+ * Adds a response action to the list of actions.
143
+ * If the list does not exist it's created by this method.
144
+ *
145
+ * @param action The instance of the action.
146
+ * @returns The same instance of the action.
147
+ */
148
+ addResponseAction(action: RunnableAction): RunnableAction;
149
+
150
+ addResponseAction(action: RunnableAction | IRunnableAction): RunnableAction {
151
+ if (!Array.isArray(this.response)) {
152
+ this.response = [];
153
+ }
154
+ let finalAction: RunnableAction;
155
+ if (action instanceof RunnableAction) {
156
+ finalAction = action;
157
+ } else {
158
+ finalAction = new RunnableAction(action);
159
+ }
160
+ this.response.push(finalAction);
161
+ return finalAction;
162
+ }
163
+ }