@api-client/core 0.6.20 → 0.6.22
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 +2 -0
- package/build/browser.js +2 -0
- package/build/browser.js.map +1 -1
- package/build/index.d.ts +2 -0
- package/build/index.js +2 -0
- package/build/index.js.map +1 -1
- package/build/src/mocking/lib/Url.js +1 -1
- package/build/src/models/AuthorizationData.d.ts +2 -0
- package/build/src/models/AuthorizationData.js +10 -3
- package/build/src/models/AuthorizationData.js.map +1 -1
- package/build/src/models/ClientCertificate.js +1 -1
- package/build/src/models/ClientCertificate.js.map +1 -1
- package/build/src/models/HostRule.d.ts +8 -0
- package/build/src/models/HostRule.js +10 -1
- package/build/src/models/HostRule.js.map +1 -1
- package/build/src/models/HttpProject.js +3 -0
- package/build/src/models/HttpProject.js.map +1 -1
- package/build/src/models/Url.d.ts +4 -4
- package/build/src/models/Url.js +6 -6
- package/build/src/models/Url.js.map +1 -1
- package/build/src/models/arc/ArcHttpRequest.d.ts +39 -0
- package/build/src/models/arc/ArcHttpRequest.js +106 -0
- package/build/src/models/arc/ArcHttpRequest.js.map +1 -0
- package/build/src/models/arc/ArcProject.d.ts +621 -0
- package/build/src/models/arc/ArcProject.js +1242 -0
- package/build/src/models/arc/ArcProject.js.map +1 -0
- package/package.json +1 -1
- package/src/mocking/lib/Url.ts +1 -1
- package/src/models/AuthorizationData.ts +10 -2
- package/src/models/ClientCertificate.ts +2 -2
- package/src/models/HostRule.ts +17 -1
- package/src/models/HttpProject.ts +3 -0
- package/src/models/Url.ts +10 -10
- package/src/models/arc/ArcHttpRequest.ts +121 -0
- package/src/models/arc/ArcProject.ts +1605 -0
- package/src/models/arc/readme.md +3 -0
|
@@ -0,0 +1,1242 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
2
|
+
/* eslint-disable class-methods-use-this */
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-this-alias */
|
|
4
|
+
/* eslint-disable max-classes-per-file */
|
|
5
|
+
// import { ARCHistoryRequest, ARCSavedRequest } from "@api-client/core/build/legacy.js";
|
|
6
|
+
import v4 from "../../lib/uuid.js";
|
|
7
|
+
import { HttpRequest } from "../HttpRequest.js";
|
|
8
|
+
import { Request } from "../Request.js";
|
|
9
|
+
import { Thing } from "../Thing.js";
|
|
10
|
+
export const ArcProjectKind = 'ARC#HttpProject';
|
|
11
|
+
export const ArcProjectFolderKind = 'ARC#HttpProjectFolder';
|
|
12
|
+
export const ArcProjectRequestKind = 'ARC#HttpProjectRequest';
|
|
13
|
+
export class ArcProjectRequest extends Request {
|
|
14
|
+
kind = ArcProjectRequestKind;
|
|
15
|
+
/**
|
|
16
|
+
* The identifier of the request.
|
|
17
|
+
*/
|
|
18
|
+
key = '';
|
|
19
|
+
/**
|
|
20
|
+
* A reference to the top level project object.
|
|
21
|
+
*/
|
|
22
|
+
project;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a project request from an URL.
|
|
25
|
+
* This does not manipulate the project.
|
|
26
|
+
*
|
|
27
|
+
* @param url The Request URL. This is required.
|
|
28
|
+
* @param project The parent project.
|
|
29
|
+
*/
|
|
30
|
+
static fromUrl(url, project) {
|
|
31
|
+
if (!project) {
|
|
32
|
+
throw new Error(`The project is required.`);
|
|
33
|
+
}
|
|
34
|
+
const now = Date.now();
|
|
35
|
+
const request = new ArcProjectRequest(project, {
|
|
36
|
+
key: v4(),
|
|
37
|
+
kind: ArcProjectRequestKind,
|
|
38
|
+
created: now,
|
|
39
|
+
updated: now,
|
|
40
|
+
expects: HttpRequest.fromBaseValues({ url, method: 'GET' }).toJSON(),
|
|
41
|
+
info: Thing.fromName(url).toJSON(),
|
|
42
|
+
});
|
|
43
|
+
return request;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Creates a project request from a name.
|
|
47
|
+
* This does not manipulate the project.
|
|
48
|
+
*
|
|
49
|
+
* @param name The Request name.
|
|
50
|
+
* @param project The parent project.This is required.
|
|
51
|
+
*/
|
|
52
|
+
static fromName(name, project) {
|
|
53
|
+
if (!project) {
|
|
54
|
+
throw new Error(`The project is required.`);
|
|
55
|
+
}
|
|
56
|
+
const now = Date.now();
|
|
57
|
+
const request = new ArcProjectRequest(project, {
|
|
58
|
+
key: v4(),
|
|
59
|
+
kind: ArcProjectRequestKind,
|
|
60
|
+
created: now,
|
|
61
|
+
updated: now,
|
|
62
|
+
expects: new HttpRequest().toJSON(),
|
|
63
|
+
info: Thing.fromName(name).toJSON(),
|
|
64
|
+
});
|
|
65
|
+
return request;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Creates a project request from an HttpRequest definition.
|
|
69
|
+
* This does not manipulate the project.
|
|
70
|
+
*
|
|
71
|
+
* @param project The parent project This is required.
|
|
72
|
+
* @param info The request data.
|
|
73
|
+
*/
|
|
74
|
+
static fromHttpRequest(info, project) {
|
|
75
|
+
if (!project) {
|
|
76
|
+
throw new Error(`The project is required.`);
|
|
77
|
+
}
|
|
78
|
+
const now = Date.now();
|
|
79
|
+
const request = new ArcProjectRequest(project, {
|
|
80
|
+
key: v4(),
|
|
81
|
+
kind: ArcProjectRequestKind,
|
|
82
|
+
created: now,
|
|
83
|
+
updated: now,
|
|
84
|
+
expects: HttpRequest.fromBaseValues({ method: info.method, url: info.url, headers: info.headers, payload: info.payload }).toJSON(),
|
|
85
|
+
info: Thing.fromName(info.url).toJSON(),
|
|
86
|
+
});
|
|
87
|
+
return request;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Creates a project request for a schema of a Request.
|
|
91
|
+
*/
|
|
92
|
+
static fromRequest(request, project) {
|
|
93
|
+
const key = v4();
|
|
94
|
+
const init = { ...request, key, kind: ArcProjectRequestKind };
|
|
95
|
+
const result = new ArcProjectRequest(project, init);
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
constructor(project, input) {
|
|
99
|
+
super(input);
|
|
100
|
+
this.project = project;
|
|
101
|
+
let init;
|
|
102
|
+
if (typeof input === 'string') {
|
|
103
|
+
init = JSON.parse(input);
|
|
104
|
+
}
|
|
105
|
+
else if (typeof input === 'object') {
|
|
106
|
+
init = input;
|
|
107
|
+
}
|
|
108
|
+
if (init) {
|
|
109
|
+
this.key = init.key || v4();
|
|
110
|
+
}
|
|
111
|
+
this.kind = ArcProjectRequestKind;
|
|
112
|
+
}
|
|
113
|
+
new(init) {
|
|
114
|
+
super.new(init);
|
|
115
|
+
const { key = v4() } = init;
|
|
116
|
+
this.key = key;
|
|
117
|
+
this.kind = ArcProjectRequestKind;
|
|
118
|
+
}
|
|
119
|
+
toJSON() {
|
|
120
|
+
const request = super.toJSON();
|
|
121
|
+
const result = { ...request, key: this.key, kind: ArcProjectRequestKind };
|
|
122
|
+
return result;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* @returns The instance of the ArcProject or a ArcProjectFolder that is a closes parent of this instance.
|
|
126
|
+
*/
|
|
127
|
+
getParent() {
|
|
128
|
+
const { project, key } = this;
|
|
129
|
+
return project.findParent(key);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* @returns A reference to the parent folder or the top-level HTTP project.
|
|
133
|
+
*/
|
|
134
|
+
getProject() {
|
|
135
|
+
return this.project;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Removes this request from the project.
|
|
139
|
+
*/
|
|
140
|
+
remove() {
|
|
141
|
+
this.project.removeRequest(this.key);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Makes a copy of this request.
|
|
145
|
+
* By default it attaches the copied request to the same parent.
|
|
146
|
+
* Use the options dictionary to control this behavior.
|
|
147
|
+
*/
|
|
148
|
+
clone(opts = {}) {
|
|
149
|
+
const copy = new ArcProjectRequest(this.project, this.toJSON());
|
|
150
|
+
if (!opts.withoutRevalidate) {
|
|
151
|
+
copy.key = v4();
|
|
152
|
+
}
|
|
153
|
+
if (!opts.withoutAttach) {
|
|
154
|
+
// if the parent is the project then add the request to the project.
|
|
155
|
+
const parent = this.getParent();
|
|
156
|
+
if (parent) {
|
|
157
|
+
parent.addRequest(copy);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return copy;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The static version of the `clone()` method.
|
|
164
|
+
*
|
|
165
|
+
* @param request The request schema to clone.
|
|
166
|
+
* @param project The project to add the request to.
|
|
167
|
+
* @param opts Optional options.
|
|
168
|
+
* @returns The copied request.
|
|
169
|
+
*/
|
|
170
|
+
static clone(request, project, opts = {}) {
|
|
171
|
+
const obj = new ArcProjectRequest(project, request);
|
|
172
|
+
return obj.clone(opts);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
export class ArcProjectItem {
|
|
176
|
+
/**
|
|
177
|
+
* The kind of the item.
|
|
178
|
+
*/
|
|
179
|
+
kind = ArcProjectRequestKind;
|
|
180
|
+
/**
|
|
181
|
+
* The identifier of the object in the `definitions` array of the project.
|
|
182
|
+
*/
|
|
183
|
+
key = '';
|
|
184
|
+
/**
|
|
185
|
+
* A reference to the top level project object.
|
|
186
|
+
*/
|
|
187
|
+
project;
|
|
188
|
+
/**
|
|
189
|
+
* Checks whether the input is a definition of a project item.
|
|
190
|
+
*/
|
|
191
|
+
static isProjectItem(input) {
|
|
192
|
+
const typed = input;
|
|
193
|
+
if (!input || ![ArcProjectFolderKind, ArcProjectRequestKind].includes(typed.kind)) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* @return An instance that represents a request item
|
|
200
|
+
*/
|
|
201
|
+
static projectRequest(project, key) {
|
|
202
|
+
const item = new ArcProjectItem(project, {
|
|
203
|
+
kind: ArcProjectRequestKind,
|
|
204
|
+
key,
|
|
205
|
+
});
|
|
206
|
+
return item;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* @return An instance that represents a folder item
|
|
210
|
+
*/
|
|
211
|
+
static projectFolder(project, key) {
|
|
212
|
+
const item = new ArcProjectItem(project, {
|
|
213
|
+
kind: ArcProjectFolderKind,
|
|
214
|
+
key,
|
|
215
|
+
});
|
|
216
|
+
return item;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* @param project The top-most project.
|
|
220
|
+
* @param input The project item definition used to restore the state.
|
|
221
|
+
*/
|
|
222
|
+
constructor(project, input) {
|
|
223
|
+
this.project = project;
|
|
224
|
+
let init;
|
|
225
|
+
if (typeof input === 'string') {
|
|
226
|
+
if (input === 'http-request') {
|
|
227
|
+
init = {
|
|
228
|
+
kind: ArcProjectRequestKind,
|
|
229
|
+
key: '',
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
else if (input === 'folder') {
|
|
233
|
+
init = {
|
|
234
|
+
kind: ArcProjectFolderKind,
|
|
235
|
+
key: '',
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
init = JSON.parse(input);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
else if (typeof input === 'object') {
|
|
243
|
+
init = input;
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
throw new Error('Specify the type of the item.');
|
|
247
|
+
}
|
|
248
|
+
this.new(init);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Creates a new project item clearing anything that is so far defined.
|
|
252
|
+
*
|
|
253
|
+
* Note, this throws an error when the project item is not a project item.
|
|
254
|
+
*/
|
|
255
|
+
new(init) {
|
|
256
|
+
if (!ArcProjectItem.isProjectItem(init)) {
|
|
257
|
+
throw new Error(`Not a project item.`);
|
|
258
|
+
}
|
|
259
|
+
const { kind, key } = init;
|
|
260
|
+
this.kind = kind;
|
|
261
|
+
this.key = key;
|
|
262
|
+
}
|
|
263
|
+
toJSON() {
|
|
264
|
+
const result = {
|
|
265
|
+
kind: this.kind,
|
|
266
|
+
key: this.key,
|
|
267
|
+
};
|
|
268
|
+
return result;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* @returns The instance of the definition associated with this item.
|
|
272
|
+
*/
|
|
273
|
+
getItem() {
|
|
274
|
+
const { project, key, kind } = this;
|
|
275
|
+
const { definitions } = project;
|
|
276
|
+
if (kind === ArcProjectRequestKind) {
|
|
277
|
+
return definitions.requests.find(i => i.key === key);
|
|
278
|
+
}
|
|
279
|
+
if (kind === ArcProjectFolderKind) {
|
|
280
|
+
return definitions.folders.find(i => i.key === key);
|
|
281
|
+
}
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* @returns The instance of the ArcProject or a ArcProjectFolder that is a closest parent of this item.
|
|
286
|
+
*/
|
|
287
|
+
getParent() {
|
|
288
|
+
const { project, key } = this;
|
|
289
|
+
return project.findParent(key);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
export class ArcProjectParent {
|
|
293
|
+
/**
|
|
294
|
+
* The auto-generated key for the folder object.
|
|
295
|
+
* For the project root this is the same as the `_id`.
|
|
296
|
+
*/
|
|
297
|
+
kind;
|
|
298
|
+
/**
|
|
299
|
+
* The key of the project / folder.
|
|
300
|
+
*/
|
|
301
|
+
key = '';
|
|
302
|
+
/**
|
|
303
|
+
* The basic information about the project / folder.
|
|
304
|
+
*/
|
|
305
|
+
info = Thing.fromName('');
|
|
306
|
+
/**
|
|
307
|
+
* The list of items in the folder.
|
|
308
|
+
* It is an ordered list of requests and folders.
|
|
309
|
+
* The actual definition is kept in the root's `definitions`.
|
|
310
|
+
*/
|
|
311
|
+
items = [];
|
|
312
|
+
/**
|
|
313
|
+
* Timestamp when the project was last updated.
|
|
314
|
+
*/
|
|
315
|
+
updated = Date.now();
|
|
316
|
+
/**
|
|
317
|
+
* Timestamp when the project was created.
|
|
318
|
+
*/
|
|
319
|
+
created = Date.now();
|
|
320
|
+
constructor(kind, input) {
|
|
321
|
+
this.kind = kind;
|
|
322
|
+
let init;
|
|
323
|
+
if (typeof input === 'string') {
|
|
324
|
+
init = JSON.parse(input);
|
|
325
|
+
}
|
|
326
|
+
else if (typeof input === 'object') {
|
|
327
|
+
init = input;
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
const now = Date.now();
|
|
331
|
+
init = {
|
|
332
|
+
kind,
|
|
333
|
+
created: now,
|
|
334
|
+
updated: now,
|
|
335
|
+
items: [],
|
|
336
|
+
key: v4(),
|
|
337
|
+
info: Thing.fromName('New folder').toJSON(),
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
this.new(init);
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Creates a new project folder clearing anything that is so far defined.
|
|
344
|
+
*
|
|
345
|
+
* Note, this throws an error when the project folder is not a project folder.
|
|
346
|
+
*/
|
|
347
|
+
new(init) {
|
|
348
|
+
const { key = v4(), created = Date.now(), updated = Date.now(), items, info, kind } = init;
|
|
349
|
+
this.kind = kind;
|
|
350
|
+
this.key = key;
|
|
351
|
+
this.created = created;
|
|
352
|
+
this.updated = updated;
|
|
353
|
+
this.setItems(items);
|
|
354
|
+
if (info) {
|
|
355
|
+
this.info = new Thing(info);
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
this.info = Thing.fromName('New folder');
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Lists items (not the actual definitions!) that are folders.
|
|
363
|
+
*/
|
|
364
|
+
listFolderItems() {
|
|
365
|
+
const { items = [] } = this;
|
|
366
|
+
return items.filter(i => i.kind === ArcProjectFolderKind);
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Lists items (not the actual definitions!) that are requests.
|
|
370
|
+
*/
|
|
371
|
+
listRequestItems() {
|
|
372
|
+
const { items = [] } = this;
|
|
373
|
+
return items.filter(i => i.kind === ArcProjectRequestKind);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
export class ArcProjectFolder extends ArcProjectParent {
|
|
377
|
+
project;
|
|
378
|
+
/**
|
|
379
|
+
* Creates a new ArcProjectFolder object from a name.
|
|
380
|
+
* @param project The top-most project.
|
|
381
|
+
* @param name The name to set.
|
|
382
|
+
*/
|
|
383
|
+
static fromName(project, name = 'New folder') {
|
|
384
|
+
const now = Date.now();
|
|
385
|
+
const key = v4();
|
|
386
|
+
const info = Thing.fromName(name);
|
|
387
|
+
const definition = new ArcProjectFolder(project, {
|
|
388
|
+
key,
|
|
389
|
+
created: now,
|
|
390
|
+
updated: now,
|
|
391
|
+
items: [],
|
|
392
|
+
kind: ArcProjectFolderKind,
|
|
393
|
+
info: info.toJSON(),
|
|
394
|
+
});
|
|
395
|
+
return definition;
|
|
396
|
+
}
|
|
397
|
+
constructor(project, input) {
|
|
398
|
+
super(ArcProjectFolderKind, input);
|
|
399
|
+
this.project = project;
|
|
400
|
+
let init;
|
|
401
|
+
if (typeof input === 'string') {
|
|
402
|
+
init = JSON.parse(input);
|
|
403
|
+
}
|
|
404
|
+
else if (typeof input === 'object') {
|
|
405
|
+
init = input;
|
|
406
|
+
}
|
|
407
|
+
if (init) {
|
|
408
|
+
this.key = init.key || v4();
|
|
409
|
+
}
|
|
410
|
+
this.kind = ArcProjectFolderKind;
|
|
411
|
+
}
|
|
412
|
+
toJSON() {
|
|
413
|
+
const result = {
|
|
414
|
+
kind: ArcProjectFolderKind,
|
|
415
|
+
info: this.info.toJSON(),
|
|
416
|
+
key: this.key,
|
|
417
|
+
created: this.created,
|
|
418
|
+
updated: this.updated,
|
|
419
|
+
items: [],
|
|
420
|
+
};
|
|
421
|
+
if (Array.isArray(this.items)) {
|
|
422
|
+
result.items = this.items.map(i => i.toJSON());
|
|
423
|
+
}
|
|
424
|
+
return result;
|
|
425
|
+
}
|
|
426
|
+
setItems(items) {
|
|
427
|
+
if (Array.isArray(items)) {
|
|
428
|
+
this.items = items.map(i => new ArcProjectItem(this.project, i));
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
this.items = [];
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Appends a new folder to the folder. It updates the project to add the request definition.
|
|
436
|
+
* @param name The name to set. Optional.
|
|
437
|
+
* @returns The key of newly inserted folder.
|
|
438
|
+
*/
|
|
439
|
+
addFolder(name) {
|
|
440
|
+
return this.project.addFolder(name, { parent: this.key });
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Appends a new request to the folder. It updates the project to add the request definition.
|
|
444
|
+
* @param request The request to append to the folder.
|
|
445
|
+
* @returns The key of newly inserted request.
|
|
446
|
+
*/
|
|
447
|
+
addRequest(request, opts = {}) {
|
|
448
|
+
const addOptions = { parent: this.key, ...opts };
|
|
449
|
+
if (typeof request === 'string') {
|
|
450
|
+
return this.project.addRequest(request, addOptions);
|
|
451
|
+
}
|
|
452
|
+
return this.project.addRequest(request, addOptions);
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* @returns The instance of the ArcProject or a ArcProjectFolder that is a closes parent of this instance.
|
|
456
|
+
*/
|
|
457
|
+
getParent() {
|
|
458
|
+
const { project, key } = this;
|
|
459
|
+
return project.findParent(key);
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* @returns A reference to the parent folder or the top-level HTTP project.
|
|
463
|
+
*/
|
|
464
|
+
getProject() {
|
|
465
|
+
return this.project;
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Removes this folder from the project.
|
|
469
|
+
*/
|
|
470
|
+
remove() {
|
|
471
|
+
this.project.removeFolder(this.key);
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Makes a copy of this folder.
|
|
475
|
+
* By default it attaches the copied folder to the same parent.
|
|
476
|
+
* It also, by default, copies requests declared in this folder.
|
|
477
|
+
*
|
|
478
|
+
* Use the options dictionary to control these behaviors.
|
|
479
|
+
*
|
|
480
|
+
* @param opts Cloning options
|
|
481
|
+
*/
|
|
482
|
+
clone(opts = {}) {
|
|
483
|
+
const { targetProject = this.project, targetFolder } = opts;
|
|
484
|
+
const copy = new ArcProjectFolder(targetProject, this.toJSON());
|
|
485
|
+
copy.key = v4();
|
|
486
|
+
const extProject = targetProject !== this.project;
|
|
487
|
+
if (extProject) {
|
|
488
|
+
if (targetFolder) {
|
|
489
|
+
const parent = targetProject.findFolder(targetFolder, { keyOnly: true });
|
|
490
|
+
if (!parent) {
|
|
491
|
+
throw new Error(`The target project does not contain the folder ${targetFolder}`);
|
|
492
|
+
}
|
|
493
|
+
parent.addFolder(copy);
|
|
494
|
+
}
|
|
495
|
+
else {
|
|
496
|
+
targetProject.addFolder(copy);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
else {
|
|
500
|
+
const parent = targetFolder ? this.project.findFolder(targetFolder, { keyOnly: true }) : this.getParent();
|
|
501
|
+
if (parent) {
|
|
502
|
+
parent.addFolder(copy);
|
|
503
|
+
}
|
|
504
|
+
else {
|
|
505
|
+
throw new Error(`Unable to locate a parent of the folder.`);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
// removes all items. Depending on the passed option we re-add them next.
|
|
509
|
+
copy.items = [];
|
|
510
|
+
if (!opts.withoutRequests) {
|
|
511
|
+
this.cloneRequests(copy, this.project);
|
|
512
|
+
}
|
|
513
|
+
if (!opts.withoutFolders) {
|
|
514
|
+
this.cloneSubFolders(copy, this.project, !opts.withoutRequests);
|
|
515
|
+
}
|
|
516
|
+
return copy;
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Clones the current requests to the target folder.
|
|
520
|
+
*
|
|
521
|
+
* @param folder The target folder into which to put the requests. The folder has to have the target project attached to it.
|
|
522
|
+
* @param project The originating project where the definitions are stored
|
|
523
|
+
*/
|
|
524
|
+
cloneRequests(folder, project) {
|
|
525
|
+
const requests = this.items.filter(i => i.kind === ArcProjectRequestKind);
|
|
526
|
+
requests.forEach(r => {
|
|
527
|
+
const request = project.findRequest(r.key, { keyOnly: true });
|
|
528
|
+
if (!request) {
|
|
529
|
+
// Should we throw an error here?
|
|
530
|
+
// CONS:
|
|
531
|
+
// - It's not really related to the operation. It means there is an inconsistency in the project. That's the role of the project class.
|
|
532
|
+
// - Ignoring this would allow us to make a copy that is error free.
|
|
533
|
+
// - The error may occur in a situation when the user does not expect it (giving the nature of the error)
|
|
534
|
+
// Pros:
|
|
535
|
+
// - There's an inconsistency in the project definition that should be reported back to the UI for the user to inspect
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
const copy = request.clone({ withoutAttach: true });
|
|
539
|
+
copy.project = folder.getProject();
|
|
540
|
+
folder.addRequest(copy);
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Clones the sub-folders to the target folder.
|
|
545
|
+
*
|
|
546
|
+
* @param folder The target folder into which to put the sub-folders. The folder has to have the target project attached to it.
|
|
547
|
+
* @param project The originating project where the definitions are stored
|
|
548
|
+
* @param withRequests Whether to clone requests with the folder.
|
|
549
|
+
*/
|
|
550
|
+
cloneSubFolders(folder, project, withRequests = true) {
|
|
551
|
+
const folders = this.items.filter(i => i.kind === ArcProjectFolderKind);
|
|
552
|
+
folders.forEach(f => {
|
|
553
|
+
const definition = project.findFolder(f.key, { keyOnly: true });
|
|
554
|
+
if (!definition) {
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
const copy = new ArcProjectFolder(folder.getProject(), definition.toJSON());
|
|
558
|
+
copy.key = v4();
|
|
559
|
+
copy.items = [];
|
|
560
|
+
folder.addFolder(copy);
|
|
561
|
+
if (withRequests) {
|
|
562
|
+
definition.cloneRequests(copy, project);
|
|
563
|
+
}
|
|
564
|
+
definition.cloneSubFolders(copy, project, withRequests);
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* Lists folders in this folder.
|
|
569
|
+
*/
|
|
570
|
+
listFolders() {
|
|
571
|
+
return this.project.listFolders({ folder: this.key });
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Lists requests in this folder.
|
|
575
|
+
*/
|
|
576
|
+
listRequests() {
|
|
577
|
+
return this.project.listRequests(this.key);
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* Adds a request to the project that has been created for a previous version of ARC.
|
|
581
|
+
*
|
|
582
|
+
* @param legacy The legacy request definition.
|
|
583
|
+
* @returns The created project request.
|
|
584
|
+
*/
|
|
585
|
+
async addLegacyRequest(legacy) {
|
|
586
|
+
const request = await Request.fromLegacy(legacy);
|
|
587
|
+
const projectRequest = ArcProjectRequest.fromRequest(request.toJSON(), this.project);
|
|
588
|
+
return this.addRequest(projectRequest);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
export class ArcProject extends ArcProjectParent {
|
|
592
|
+
/**
|
|
593
|
+
* Timestamp when the project was last updated.
|
|
594
|
+
*/
|
|
595
|
+
updated = Date.now();
|
|
596
|
+
/**
|
|
597
|
+
* Timestamp when the project was created.
|
|
598
|
+
*/
|
|
599
|
+
created = Date.now();
|
|
600
|
+
definitions;
|
|
601
|
+
/**
|
|
602
|
+
* Creates a new ARC project from a name.
|
|
603
|
+
* @param name The name to set.
|
|
604
|
+
*/
|
|
605
|
+
static fromName(name) {
|
|
606
|
+
const project = new ArcProject();
|
|
607
|
+
const info = Thing.fromName(name);
|
|
608
|
+
project.info = info;
|
|
609
|
+
return project;
|
|
610
|
+
}
|
|
611
|
+
constructor(input) {
|
|
612
|
+
super(ArcProjectKind);
|
|
613
|
+
this.definitions = {
|
|
614
|
+
folders: [],
|
|
615
|
+
requests: [],
|
|
616
|
+
};
|
|
617
|
+
let init;
|
|
618
|
+
if (typeof input === 'string') {
|
|
619
|
+
init = JSON.parse(input);
|
|
620
|
+
}
|
|
621
|
+
else if (typeof input === 'object') {
|
|
622
|
+
init = input;
|
|
623
|
+
if (!init.kind) {
|
|
624
|
+
init.kind = ArcProjectKind;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
else {
|
|
628
|
+
init = {
|
|
629
|
+
kind: ArcProjectKind,
|
|
630
|
+
key: v4(),
|
|
631
|
+
definitions: {},
|
|
632
|
+
items: [],
|
|
633
|
+
info: Thing.fromName('').toJSON(),
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
this.new(init);
|
|
637
|
+
}
|
|
638
|
+
new(init) {
|
|
639
|
+
if (!init || !init.items) {
|
|
640
|
+
throw new Error(`Not a project.`);
|
|
641
|
+
}
|
|
642
|
+
const { key = v4(), definitions = {}, items, info, created = Date.now(), updated = Date.now() } = init;
|
|
643
|
+
this.key = key;
|
|
644
|
+
this.created = created;
|
|
645
|
+
this.updated = updated;
|
|
646
|
+
if (info) {
|
|
647
|
+
this.info = new Thing(info);
|
|
648
|
+
}
|
|
649
|
+
else {
|
|
650
|
+
this.info = Thing.fromName('');
|
|
651
|
+
}
|
|
652
|
+
this.setItems(items);
|
|
653
|
+
this.definitions = {
|
|
654
|
+
folders: [],
|
|
655
|
+
requests: [],
|
|
656
|
+
};
|
|
657
|
+
if (Array.isArray(definitions.requests)) {
|
|
658
|
+
this.definitions.requests = definitions.requests.map(i => {
|
|
659
|
+
const instance = new ArcProjectRequest(this, i);
|
|
660
|
+
return instance;
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
if (Array.isArray(definitions.folders)) {
|
|
664
|
+
this.definitions.folders = definitions.folders.map(i => {
|
|
665
|
+
const instance = new ArcProjectFolder(this, i);
|
|
666
|
+
return instance;
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
toJSON() {
|
|
671
|
+
const result = {
|
|
672
|
+
kind: ArcProjectKind,
|
|
673
|
+
key: this.key,
|
|
674
|
+
definitions: {},
|
|
675
|
+
items: [],
|
|
676
|
+
info: this.info.toJSON(),
|
|
677
|
+
};
|
|
678
|
+
if (Array.isArray(this.definitions.requests) && this.definitions.requests.length) {
|
|
679
|
+
result.definitions.requests = this.definitions.requests.map(i => i.toJSON());
|
|
680
|
+
}
|
|
681
|
+
if (Array.isArray(this.definitions.folders) && this.definitions.folders.length) {
|
|
682
|
+
result.definitions.folders = this.definitions.folders.map(i => i.toJSON());
|
|
683
|
+
}
|
|
684
|
+
if (Array.isArray(this.items) && this.items.length) {
|
|
685
|
+
result.items = this.items.map(i => i.toJSON());
|
|
686
|
+
}
|
|
687
|
+
return result;
|
|
688
|
+
}
|
|
689
|
+
setItems(items) {
|
|
690
|
+
if (Array.isArray(items)) {
|
|
691
|
+
this.items = items.map(i => new ArcProjectItem(this, i));
|
|
692
|
+
}
|
|
693
|
+
else {
|
|
694
|
+
this.items = [];
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
* Finds a parent of a definition.
|
|
699
|
+
*
|
|
700
|
+
* @param key The key of the definition.
|
|
701
|
+
* @returns The parent or undefine when not found.
|
|
702
|
+
*/
|
|
703
|
+
findParent(key) {
|
|
704
|
+
const { definitions, items = [] } = this;
|
|
705
|
+
const projectItemsIndex = items.findIndex(i => i.key === key);
|
|
706
|
+
if (projectItemsIndex > -1) {
|
|
707
|
+
return this;
|
|
708
|
+
}
|
|
709
|
+
const definition = definitions.folders.find(i => i.items.some(item => item.key === key));
|
|
710
|
+
if (definition) {
|
|
711
|
+
return definition;
|
|
712
|
+
}
|
|
713
|
+
return undefined;
|
|
714
|
+
}
|
|
715
|
+
/**
|
|
716
|
+
* Appends a new folder to the project or a sub-folder.
|
|
717
|
+
*
|
|
718
|
+
* Passing the folder schema as the fist argument is primarily used to insert a folder on the client side
|
|
719
|
+
* after a folder was created in the store.
|
|
720
|
+
*
|
|
721
|
+
* @param init The name or a folder schema. When not set a default name is assumed.
|
|
722
|
+
* @param opts Folder create options.
|
|
723
|
+
* @returns The newly inserted folder. If the folder already existed it returns its instance.
|
|
724
|
+
*/
|
|
725
|
+
addFolder(init = 'New Folder', opts = {}) {
|
|
726
|
+
if (!Array.isArray(this.items)) {
|
|
727
|
+
this.items = [];
|
|
728
|
+
}
|
|
729
|
+
if (!Array.isArray(this.definitions.folders)) {
|
|
730
|
+
this.definitions.folders = [];
|
|
731
|
+
}
|
|
732
|
+
const { skipExisting, parent } = opts;
|
|
733
|
+
let root;
|
|
734
|
+
if (parent) {
|
|
735
|
+
const rootCandidate = this.findFolder(parent);
|
|
736
|
+
if (!rootCandidate) {
|
|
737
|
+
throw new Error(`Unable to find the parent folder ${parent}`);
|
|
738
|
+
}
|
|
739
|
+
root = rootCandidate;
|
|
740
|
+
}
|
|
741
|
+
else {
|
|
742
|
+
root = this;
|
|
743
|
+
}
|
|
744
|
+
let definition;
|
|
745
|
+
if (typeof init === 'string') {
|
|
746
|
+
definition = ArcProjectFolder.fromName(this, init);
|
|
747
|
+
}
|
|
748
|
+
else if (init instanceof ArcProjectFolder) {
|
|
749
|
+
definition = init;
|
|
750
|
+
}
|
|
751
|
+
else {
|
|
752
|
+
definition = new ArcProjectFolder(this, init);
|
|
753
|
+
}
|
|
754
|
+
if (skipExisting) {
|
|
755
|
+
const folders = root.listFolderItems();
|
|
756
|
+
for (const item of folders) {
|
|
757
|
+
const existing = this.findFolder(item.key, { keyOnly: true });
|
|
758
|
+
if (existing && existing.info.name === definition.info.name) {
|
|
759
|
+
return existing;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
this.definitions.folders.push(definition);
|
|
764
|
+
const item = ArcProjectItem.projectFolder(this, definition.key);
|
|
765
|
+
if (!Array.isArray(root.items)) {
|
|
766
|
+
root.items = [];
|
|
767
|
+
}
|
|
768
|
+
if (typeof opts.index === 'number') {
|
|
769
|
+
root.items.splice(opts.index, 0, item);
|
|
770
|
+
}
|
|
771
|
+
else {
|
|
772
|
+
root.items.push(item);
|
|
773
|
+
}
|
|
774
|
+
return definition;
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Searches for a folder in the structure.
|
|
778
|
+
*
|
|
779
|
+
* @param nameOrKey The name or the key of the folder.
|
|
780
|
+
* @param opts Optional search options.
|
|
781
|
+
* @returns Found project folder or undefined.
|
|
782
|
+
*/
|
|
783
|
+
findFolder(nameOrKey, opts = {}) {
|
|
784
|
+
const { definitions } = this;
|
|
785
|
+
const item = definitions.folders.find((i) => {
|
|
786
|
+
if (i.kind !== ArcProjectFolderKind) {
|
|
787
|
+
return false;
|
|
788
|
+
}
|
|
789
|
+
const folder = i;
|
|
790
|
+
if (folder.key === nameOrKey) {
|
|
791
|
+
return true;
|
|
792
|
+
}
|
|
793
|
+
if (opts.keyOnly) {
|
|
794
|
+
return false;
|
|
795
|
+
}
|
|
796
|
+
return !!folder.info && folder.info.name === nameOrKey;
|
|
797
|
+
});
|
|
798
|
+
if (item) {
|
|
799
|
+
return item;
|
|
800
|
+
}
|
|
801
|
+
return undefined;
|
|
802
|
+
}
|
|
803
|
+
/**
|
|
804
|
+
* Removes a folder from the project.
|
|
805
|
+
* @param key The folder key. It ignores the name when searching to the folder to avoid ambiguity.
|
|
806
|
+
* @param opts Folder remove options.
|
|
807
|
+
* @returns The removed folder definition or undefined when not removed.
|
|
808
|
+
*/
|
|
809
|
+
removeFolder(key, opts = {}) {
|
|
810
|
+
const { definitions } = this;
|
|
811
|
+
const folder = this.findFolder(key, { keyOnly: true });
|
|
812
|
+
if (!folder) {
|
|
813
|
+
if (opts.safe) {
|
|
814
|
+
return undefined;
|
|
815
|
+
}
|
|
816
|
+
throw new Error(`Unable to find the folder ${key}`);
|
|
817
|
+
}
|
|
818
|
+
const parent = this.findParent(key);
|
|
819
|
+
if (!parent) {
|
|
820
|
+
if (opts.safe) {
|
|
821
|
+
return undefined;
|
|
822
|
+
}
|
|
823
|
+
throw new Error(`Unable to find a parent of the folder ${key}`);
|
|
824
|
+
}
|
|
825
|
+
const requests = folder.listRequests();
|
|
826
|
+
requests.forEach(r => r.remove());
|
|
827
|
+
const folders = folder.listFolders();
|
|
828
|
+
folders.forEach(f => f.remove());
|
|
829
|
+
const itemIndex = parent.items.findIndex(i => i.key === key);
|
|
830
|
+
const definitionIndex = definitions.folders.findIndex(i => i.key === key);
|
|
831
|
+
definitions.folders.splice(definitionIndex, 1);
|
|
832
|
+
if (itemIndex >= 0) {
|
|
833
|
+
parent.items.splice(itemIndex, 1);
|
|
834
|
+
}
|
|
835
|
+
return folder;
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* Moves a folder between folders and the project or between items inside a folder or a project
|
|
839
|
+
*
|
|
840
|
+
* Note, when the `parent` option is not specified it moved the folder to the project's root.
|
|
841
|
+
*
|
|
842
|
+
* @param key The key of the request to move.
|
|
843
|
+
* @param opts The moving options.
|
|
844
|
+
*/
|
|
845
|
+
moveFolder(key, opts = {}) {
|
|
846
|
+
const { index, parent } = opts;
|
|
847
|
+
const movedFolder = this.findFolder(key);
|
|
848
|
+
if (!movedFolder) {
|
|
849
|
+
throw new Error(`Unable to locate the folder ${key}`);
|
|
850
|
+
}
|
|
851
|
+
const parentFolder = this.findParent(key);
|
|
852
|
+
if (!parentFolder) {
|
|
853
|
+
throw new Error(`Unable to locate a parent of the folder ${key}`);
|
|
854
|
+
}
|
|
855
|
+
if (parent) {
|
|
856
|
+
// check if moving a folder into another folder that is a child of the moved folder.
|
|
857
|
+
if (this.hasChild(parent, key)) {
|
|
858
|
+
throw new RangeError(`Unable to move a folder to its child.`);
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
const target = parent ? this.findFolder(parent) : this;
|
|
862
|
+
if (!target) {
|
|
863
|
+
throw new Error(`Unable to locate the new parent folder ${parent}`);
|
|
864
|
+
}
|
|
865
|
+
const hasIndex = typeof index === 'number';
|
|
866
|
+
if (hasIndex) {
|
|
867
|
+
// comparing to the `.length` and not `.length - 1` in case we are adding at the end.
|
|
868
|
+
const maxIndex = Math.max(target.items.length, 0);
|
|
869
|
+
if (index > maxIndex) {
|
|
870
|
+
throw new RangeError(`Index out of bounds. Maximum index is ${maxIndex}.`);
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
const itemIndex = parentFolder.items.findIndex(i => i.key === key);
|
|
874
|
+
const item = parentFolder.items.splice(itemIndex, 1)[0];
|
|
875
|
+
if (hasIndex && target.items.length > index) {
|
|
876
|
+
target.items.splice(index, 0, item);
|
|
877
|
+
}
|
|
878
|
+
else {
|
|
879
|
+
target.items.push(item);
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
/**
|
|
883
|
+
* Checks whether a folder has a child (anywhere down the structure).
|
|
884
|
+
*
|
|
885
|
+
* @param child The **key** of the child.
|
|
886
|
+
* @param folder The **key** of the folder. When not set it searches from the project root.
|
|
887
|
+
* @returns True when the child is located somewhere down the structure.
|
|
888
|
+
*/
|
|
889
|
+
hasChild(child, folder) {
|
|
890
|
+
const target = folder ? this.findFolder(folder) : this;
|
|
891
|
+
if (!target) {
|
|
892
|
+
throw new Error(`Unable to locate the folder ${folder}`);
|
|
893
|
+
}
|
|
894
|
+
const { items = [] } = target;
|
|
895
|
+
for (const item of items) {
|
|
896
|
+
if (item.key === child) {
|
|
897
|
+
return true;
|
|
898
|
+
}
|
|
899
|
+
if (item.kind === ArcProjectFolderKind) {
|
|
900
|
+
const hasChild = this.hasChild(child, item.key);
|
|
901
|
+
if (hasChild) {
|
|
902
|
+
return true;
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
return false;
|
|
907
|
+
}
|
|
908
|
+
/**
|
|
909
|
+
* Adds a request to the project or a sub-folder.
|
|
910
|
+
* @param request The request to add.
|
|
911
|
+
* @param opts Thew request add options.
|
|
912
|
+
* @returns The inserted into the definitions request.
|
|
913
|
+
*/
|
|
914
|
+
addRequest(request, opts = {}) {
|
|
915
|
+
if (!Array.isArray(this.definitions.requests)) {
|
|
916
|
+
this.definitions.requests = [];
|
|
917
|
+
}
|
|
918
|
+
// the request can be already added to the project as the same method is used to refresh a request after
|
|
919
|
+
// a store update. From the system perspective it is the same event.
|
|
920
|
+
if (typeof request === 'object' && request.key) {
|
|
921
|
+
const existing = this.definitions.requests.find(i => i.key === request.key);
|
|
922
|
+
if (existing) {
|
|
923
|
+
existing.new(request);
|
|
924
|
+
return existing;
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
// if we got here, it means that we are adding a new request object to the project.
|
|
928
|
+
let finalRequest;
|
|
929
|
+
if (typeof request === 'string') {
|
|
930
|
+
finalRequest = ArcProjectRequest.fromUrl(request, this);
|
|
931
|
+
}
|
|
932
|
+
else if (request instanceof ArcProjectRequest) {
|
|
933
|
+
finalRequest = request;
|
|
934
|
+
finalRequest.project = this;
|
|
935
|
+
}
|
|
936
|
+
else {
|
|
937
|
+
finalRequest = new ArcProjectRequest(this, request);
|
|
938
|
+
}
|
|
939
|
+
if (!finalRequest.key) {
|
|
940
|
+
finalRequest.key = v4();
|
|
941
|
+
}
|
|
942
|
+
let root;
|
|
943
|
+
if (opts.parent) {
|
|
944
|
+
const rootCandidate = this.findFolder(opts.parent);
|
|
945
|
+
if (!rootCandidate) {
|
|
946
|
+
throw new Error(`Unable to find the parent folder ${opts.parent}.`);
|
|
947
|
+
}
|
|
948
|
+
root = rootCandidate;
|
|
949
|
+
}
|
|
950
|
+
else {
|
|
951
|
+
root = this;
|
|
952
|
+
}
|
|
953
|
+
if (!Array.isArray(root.items)) {
|
|
954
|
+
root.items = [];
|
|
955
|
+
}
|
|
956
|
+
if (typeof opts.index === 'number') {
|
|
957
|
+
const maxIndex = Math.max(root.items.length - 1, 0);
|
|
958
|
+
if (opts.index > maxIndex) {
|
|
959
|
+
throw new RangeError(`Index out of bounds. Maximum index is ${maxIndex}.`);
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
this.definitions.requests.push(finalRequest);
|
|
963
|
+
const item = ArcProjectItem.projectRequest(this, finalRequest.key);
|
|
964
|
+
if (typeof opts.index === 'number') {
|
|
965
|
+
root.items.splice(opts.index, 0, item);
|
|
966
|
+
}
|
|
967
|
+
else {
|
|
968
|
+
root.items.push(item);
|
|
969
|
+
}
|
|
970
|
+
return finalRequest;
|
|
971
|
+
}
|
|
972
|
+
/**
|
|
973
|
+
* Adds a request to the project that has been created for a previous version of ARC.
|
|
974
|
+
*
|
|
975
|
+
* @param legacy The legacy request definition.
|
|
976
|
+
* @returns The created project request.
|
|
977
|
+
*/
|
|
978
|
+
async addLegacyRequest(legacy) {
|
|
979
|
+
const request = await Request.fromLegacy(legacy);
|
|
980
|
+
const projectRequest = ArcProjectRequest.fromRequest(request.toJSON(), this);
|
|
981
|
+
return this.addRequest(projectRequest);
|
|
982
|
+
}
|
|
983
|
+
/**
|
|
984
|
+
* Searches for a request in the project.
|
|
985
|
+
*
|
|
986
|
+
* @param nameOrKey The name or the key of the request.
|
|
987
|
+
* @param opts Optional search options.
|
|
988
|
+
* @returns Found project request or undefined.
|
|
989
|
+
*/
|
|
990
|
+
findRequest(nameOrKey, opts = {}) {
|
|
991
|
+
const { definitions } = this;
|
|
992
|
+
const item = definitions.requests.find((request) => {
|
|
993
|
+
if (request.key === nameOrKey) {
|
|
994
|
+
return true;
|
|
995
|
+
}
|
|
996
|
+
if (opts.keyOnly) {
|
|
997
|
+
return false;
|
|
998
|
+
}
|
|
999
|
+
return !!request.info && request.info.name === nameOrKey;
|
|
1000
|
+
});
|
|
1001
|
+
if (item) {
|
|
1002
|
+
return item;
|
|
1003
|
+
}
|
|
1004
|
+
return undefined;
|
|
1005
|
+
}
|
|
1006
|
+
/**
|
|
1007
|
+
* Removes a request from the project.
|
|
1008
|
+
*
|
|
1009
|
+
* @param key The request key. It ignores the name when searching to the request to avoid ambiguity.
|
|
1010
|
+
* @param opts Request remove options.
|
|
1011
|
+
* @returns The removed request definition or undefined when not removed.
|
|
1012
|
+
*/
|
|
1013
|
+
removeRequest(key, opts = {}) {
|
|
1014
|
+
const { definitions } = this;
|
|
1015
|
+
const request = this.findRequest(key, { keyOnly: true });
|
|
1016
|
+
if (!request) {
|
|
1017
|
+
if (opts.safe) {
|
|
1018
|
+
return undefined;
|
|
1019
|
+
}
|
|
1020
|
+
throw new Error(`Unable to find the request ${key}`);
|
|
1021
|
+
}
|
|
1022
|
+
const parent = this.findParent(key);
|
|
1023
|
+
if (!parent) {
|
|
1024
|
+
if (opts.safe) {
|
|
1025
|
+
return undefined;
|
|
1026
|
+
}
|
|
1027
|
+
throw new Error(`Unable to find a parent of the request ${key}`);
|
|
1028
|
+
}
|
|
1029
|
+
const itemIndex = parent.items.findIndex(i => i.key === key);
|
|
1030
|
+
const definitionIndex = definitions.requests.findIndex(i => i.key === key);
|
|
1031
|
+
definitions.requests.splice(definitionIndex, 1);
|
|
1032
|
+
if (itemIndex >= 0) {
|
|
1033
|
+
parent.items.splice(itemIndex, 1);
|
|
1034
|
+
}
|
|
1035
|
+
return request;
|
|
1036
|
+
}
|
|
1037
|
+
/**
|
|
1038
|
+
* Moves a request between folders and the project or between items inside a folder or a project.
|
|
1039
|
+
*
|
|
1040
|
+
* Note, when the `parent` option is not specified it moved the request to the project's root.
|
|
1041
|
+
*
|
|
1042
|
+
* @param key The key of the request to move.
|
|
1043
|
+
* @param opts The moving options.
|
|
1044
|
+
*/
|
|
1045
|
+
moveRequest(key, opts = {}) {
|
|
1046
|
+
const { index, parent } = opts;
|
|
1047
|
+
const request = this.findRequest(key);
|
|
1048
|
+
if (!request) {
|
|
1049
|
+
throw new Error(`Unable to locate the request ${key}`);
|
|
1050
|
+
}
|
|
1051
|
+
const parentFolder = this.findParent(key);
|
|
1052
|
+
if (!parentFolder) {
|
|
1053
|
+
throw new Error(`Unable to locate a parent of the request ${key}`);
|
|
1054
|
+
}
|
|
1055
|
+
const target = parent ? this.findFolder(parent) : this;
|
|
1056
|
+
if (!target) {
|
|
1057
|
+
throw new Error(`Unable to locate the new parent folder ${parent}`);
|
|
1058
|
+
}
|
|
1059
|
+
const hasIndex = typeof index === 'number';
|
|
1060
|
+
if (hasIndex) {
|
|
1061
|
+
// comparing to the `.length` and not `.length - 1` in case we are adding at the end.
|
|
1062
|
+
const maxIndex = Math.max(target.items.length, 0);
|
|
1063
|
+
if (index > maxIndex) {
|
|
1064
|
+
throw new RangeError(`Index out of bounds. Maximum index is ${maxIndex}.`);
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
const itemIndex = parentFolder.items.findIndex(i => i.key === key);
|
|
1068
|
+
const item = parentFolder.items.splice(itemIndex, 1)[0];
|
|
1069
|
+
if (hasIndex && target.items.length > index) {
|
|
1070
|
+
target.items.splice(index, 0, item);
|
|
1071
|
+
}
|
|
1072
|
+
else {
|
|
1073
|
+
target.items.push(item);
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
/**
|
|
1077
|
+
* Lists items (not the actual definitions!) that are folders.
|
|
1078
|
+
*/
|
|
1079
|
+
listFolderItems() {
|
|
1080
|
+
const { items = [] } = this;
|
|
1081
|
+
return items.filter(i => i.kind === ArcProjectFolderKind);
|
|
1082
|
+
}
|
|
1083
|
+
/**
|
|
1084
|
+
* Lists items (not the actual definitions!) that are requests.
|
|
1085
|
+
*/
|
|
1086
|
+
listRequestItems() {
|
|
1087
|
+
const { items = [] } = this;
|
|
1088
|
+
return items.filter(i => i.kind === ArcProjectRequestKind);
|
|
1089
|
+
}
|
|
1090
|
+
/**
|
|
1091
|
+
* Lists folders from the project or a sub-folder.
|
|
1092
|
+
* @param opts Folder listing options.
|
|
1093
|
+
*/
|
|
1094
|
+
listFolders(opts = {}) {
|
|
1095
|
+
let root;
|
|
1096
|
+
if (opts.folder) {
|
|
1097
|
+
const parent = this.findFolder(opts.folder);
|
|
1098
|
+
if (!parent) {
|
|
1099
|
+
throw new Error(`Unable to find the folder ${opts.folder}.`);
|
|
1100
|
+
}
|
|
1101
|
+
root = parent;
|
|
1102
|
+
}
|
|
1103
|
+
else {
|
|
1104
|
+
root = this;
|
|
1105
|
+
}
|
|
1106
|
+
const items = root.listFolderItems();
|
|
1107
|
+
const result = [];
|
|
1108
|
+
const { definitions } = this;
|
|
1109
|
+
items.forEach((i) => {
|
|
1110
|
+
const definition = definitions.folders.find(d => i.key === d.key);
|
|
1111
|
+
if (definition) {
|
|
1112
|
+
result.push(definition);
|
|
1113
|
+
}
|
|
1114
|
+
});
|
|
1115
|
+
return result;
|
|
1116
|
+
}
|
|
1117
|
+
/**
|
|
1118
|
+
* Lists requests in this project or a sub-folder.
|
|
1119
|
+
* @param folder The optional folder name or the key to list requests for.
|
|
1120
|
+
*/
|
|
1121
|
+
listRequests(folder) {
|
|
1122
|
+
let root;
|
|
1123
|
+
if (folder) {
|
|
1124
|
+
const parent = this.findFolder(folder);
|
|
1125
|
+
if (!parent) {
|
|
1126
|
+
throw new Error(`Unable to find the folder ${folder}.`);
|
|
1127
|
+
}
|
|
1128
|
+
root = parent;
|
|
1129
|
+
}
|
|
1130
|
+
else {
|
|
1131
|
+
root = this;
|
|
1132
|
+
}
|
|
1133
|
+
const items = root.listRequestItems();
|
|
1134
|
+
const result = [];
|
|
1135
|
+
const { definitions } = this;
|
|
1136
|
+
items.forEach((i) => {
|
|
1137
|
+
const definition = definitions.requests.find(d => i.key === d.key);
|
|
1138
|
+
if (definition) {
|
|
1139
|
+
result.push(definition);
|
|
1140
|
+
}
|
|
1141
|
+
});
|
|
1142
|
+
return result;
|
|
1143
|
+
}
|
|
1144
|
+
/**
|
|
1145
|
+
* Lists definitions for the `items` of the project or a folder.
|
|
1146
|
+
* @param folder Optionally the folder name to list the definitions for.
|
|
1147
|
+
*/
|
|
1148
|
+
listDefinitions(folder) {
|
|
1149
|
+
let root;
|
|
1150
|
+
if (folder) {
|
|
1151
|
+
const parent = this.findFolder(folder);
|
|
1152
|
+
if (!parent) {
|
|
1153
|
+
throw new Error(`Unable to find the folder ${folder}`);
|
|
1154
|
+
}
|
|
1155
|
+
root = parent;
|
|
1156
|
+
}
|
|
1157
|
+
else {
|
|
1158
|
+
root = this;
|
|
1159
|
+
}
|
|
1160
|
+
const result = [];
|
|
1161
|
+
const { items = [] } = root;
|
|
1162
|
+
const { definitions } = this;
|
|
1163
|
+
items.forEach((item) => {
|
|
1164
|
+
let definition;
|
|
1165
|
+
if (item.kind === ArcProjectFolderKind) {
|
|
1166
|
+
definition = definitions.folders.find(d => item.key === d.key);
|
|
1167
|
+
}
|
|
1168
|
+
else if (item.kind === ArcProjectRequestKind) {
|
|
1169
|
+
definition = definitions.requests.find(d => item.key === d.key);
|
|
1170
|
+
}
|
|
1171
|
+
if (definition) {
|
|
1172
|
+
result.push(definition);
|
|
1173
|
+
}
|
|
1174
|
+
});
|
|
1175
|
+
return result;
|
|
1176
|
+
}
|
|
1177
|
+
/**
|
|
1178
|
+
* @returns On the project level this always returns undefined.
|
|
1179
|
+
*/
|
|
1180
|
+
getParent() {
|
|
1181
|
+
return undefined;
|
|
1182
|
+
}
|
|
1183
|
+
getProject() {
|
|
1184
|
+
return this;
|
|
1185
|
+
}
|
|
1186
|
+
/**
|
|
1187
|
+
* Makes a copy of this project.
|
|
1188
|
+
*/
|
|
1189
|
+
clone(opts = {}) {
|
|
1190
|
+
const copy = new ArcProject(this.toJSON());
|
|
1191
|
+
if (!opts.withoutRevalidate) {
|
|
1192
|
+
copy.key = v4();
|
|
1193
|
+
ArcProject.regenerateKeys(copy);
|
|
1194
|
+
}
|
|
1195
|
+
return copy;
|
|
1196
|
+
}
|
|
1197
|
+
static clone(project, opts = {}) {
|
|
1198
|
+
const obj = new ArcProject(project);
|
|
1199
|
+
return obj.clone(opts);
|
|
1200
|
+
}
|
|
1201
|
+
/**
|
|
1202
|
+
* Re-generates keys in the project, taking care of the references.
|
|
1203
|
+
*
|
|
1204
|
+
* Note, this changes the project properties. Make a copy of the project before calling this.
|
|
1205
|
+
*
|
|
1206
|
+
* @param src The project instance to re-generate keys for.
|
|
1207
|
+
*/
|
|
1208
|
+
static regenerateKeys(src) {
|
|
1209
|
+
const { items = [], definitions } = src;
|
|
1210
|
+
// create a flat list of all "items" in the project and all folders.
|
|
1211
|
+
let flatItems = [...items];
|
|
1212
|
+
(definitions.folders || []).forEach((folder) => {
|
|
1213
|
+
if (Array.isArray(folder.items) && folder.items.length) {
|
|
1214
|
+
flatItems = flatItems.concat(folder.items);
|
|
1215
|
+
}
|
|
1216
|
+
});
|
|
1217
|
+
(definitions.folders || []).forEach((folder) => {
|
|
1218
|
+
// if (Array.isArray(folder.environments) && folder.environments.length) {
|
|
1219
|
+
// withEnvironments.push(folder);
|
|
1220
|
+
// }
|
|
1221
|
+
const oldKey = folder.key;
|
|
1222
|
+
const indexObject = flatItems.find(i => i.key === oldKey);
|
|
1223
|
+
if (!indexObject) {
|
|
1224
|
+
return;
|
|
1225
|
+
}
|
|
1226
|
+
const newKey = v4();
|
|
1227
|
+
indexObject.key = newKey;
|
|
1228
|
+
folder.key = newKey;
|
|
1229
|
+
});
|
|
1230
|
+
(definitions.requests || []).forEach((request) => {
|
|
1231
|
+
const oldKey = request.key;
|
|
1232
|
+
const indexObject = flatItems.find(i => i.key === oldKey);
|
|
1233
|
+
if (!indexObject) {
|
|
1234
|
+
return;
|
|
1235
|
+
}
|
|
1236
|
+
const newKey = v4();
|
|
1237
|
+
indexObject.key = newKey;
|
|
1238
|
+
request.key = newKey;
|
|
1239
|
+
});
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
//# sourceMappingURL=ArcProject.js.map
|