@api-client/core 0.5.15 → 0.5.18
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 +1 -0
- package/build/browser.js +1 -0
- package/build/browser.js.map +1 -1
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/index.js.map +1 -1
- package/build/src/lib/parsers/UriTemplate.d.ts +18 -3
- package/build/src/lib/parsers/UriTemplate.js +27 -6
- package/build/src/lib/parsers/UriTemplate.js.map +1 -1
- package/build/src/lib/parsers/UrlProcessor.d.ts +255 -0
- package/build/src/lib/parsers/UrlProcessor.js +687 -0
- package/build/src/lib/parsers/UrlProcessor.js.map +1 -0
- package/build/src/models/HttpProject.d.ts +85 -32
- package/build/src/models/HttpProject.js +181 -80
- package/build/src/models/HttpProject.js.map +1 -1
- package/build/src/models/ProjectFolder.d.ts +43 -6
- package/build/src/models/ProjectFolder.js +37 -12
- package/build/src/models/ProjectFolder.js.map +1 -1
- package/build/src/models/ProjectItem.d.ts +13 -6
- package/build/src/models/ProjectItem.js +24 -4
- package/build/src/models/ProjectItem.js.map +1 -1
- package/build/src/models/ProjectParent.d.ts +1 -42
- package/build/src/models/ProjectParent.js +1 -84
- package/build/src/models/ProjectParent.js.map +1 -1
- package/build/src/models/transformers/PostmanBackupTransformer.js +0 -1
- package/build/src/models/transformers/PostmanBackupTransformer.js.map +1 -1
- package/build/src/models/transformers/PostmanV21Transformer.js +0 -1
- package/build/src/models/transformers/PostmanV21Transformer.js.map +1 -1
- package/build/src/models/transformers/PostmanV2Transformer.js +0 -1
- package/build/src/models/transformers/PostmanV2Transformer.js.map +1 -1
- package/build/src/runtime/node/ProjectRequestRunner.js +1 -1
- package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/parsers/UriTemplate.ts +34 -11
- package/src/lib/parsers/UrlProcessor.ts +799 -0
- package/src/models/HttpProject.ts +236 -99
- package/src/models/ProjectFolder.ts +68 -17
- package/src/models/ProjectItem.ts +33 -11
- package/src/models/ProjectParent.ts +1 -110
- package/src/models/transformers/PostmanBackupTransformer.ts +0 -1
- package/src/models/transformers/PostmanV21Transformer.ts +0 -1
- package/src/models/transformers/PostmanV2Transformer.ts +0 -1
- package/src/runtime/node/ProjectRequestRunner.ts +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ProjectParent } from './ProjectParent.js';
|
|
2
2
|
import { IProjectDefinitionProperty } from './ProjectDefinitionProperty.js';
|
|
3
|
-
import { Environment, IEnvironment } from './Environment.js';
|
|
3
|
+
import { Environment, IEnvironment, Kind as EnvironmentKind } from './Environment.js';
|
|
4
4
|
import { License, ILicense } from './License.js';
|
|
5
5
|
import { Provider, IProvider } from './Provider.js';
|
|
6
6
|
import { IThing, Thing, Kind as ThingKind } from './Thing.js';
|
|
@@ -16,6 +16,24 @@ import { PostmanDataTransformer } from './transformers/PostmanDataTransformer.js
|
|
|
16
16
|
|
|
17
17
|
export const Kind = 'Core#HttpProject';
|
|
18
18
|
|
|
19
|
+
export interface IItemOptions {
|
|
20
|
+
/**
|
|
21
|
+
* The parent folder to add the item to.
|
|
22
|
+
*/
|
|
23
|
+
parent?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface IItemCreateOptions extends IItemOptions{
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The position at which to add the item.
|
|
30
|
+
*/
|
|
31
|
+
index?: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface IEnvironmentCreateOptions extends IItemCreateOptions {
|
|
35
|
+
}
|
|
36
|
+
|
|
19
37
|
/**
|
|
20
38
|
* A list of options to initialize a project in various situations.
|
|
21
39
|
*/
|
|
@@ -95,20 +113,16 @@ export interface IProjectMoveOptions {
|
|
|
95
113
|
parent?: string;
|
|
96
114
|
}
|
|
97
115
|
|
|
98
|
-
export interface IReadEnvironmentOptions {
|
|
116
|
+
export interface IReadEnvironmentOptions extends IItemOptions {
|
|
99
117
|
/**
|
|
100
118
|
* The name or the key of the environment to select.
|
|
101
119
|
*
|
|
102
120
|
* When the name is not specified it selects:
|
|
103
121
|
* - the first environment from the project, if any
|
|
104
|
-
* - any parent folder's first environment to the requested folder, if any (if
|
|
105
|
-
* - the requested folder's first environment, if any (if
|
|
122
|
+
* - any parent folder's first environment to the requested folder, if any (if parent is set)
|
|
123
|
+
* - the requested folder's first environment, if any (if parent is set)
|
|
106
124
|
*/
|
|
107
125
|
nameOrKey?: string;
|
|
108
|
-
/**
|
|
109
|
-
* The key of the folder to collect the environments for.
|
|
110
|
-
*/
|
|
111
|
-
folderKey?: string;
|
|
112
126
|
}
|
|
113
127
|
|
|
114
128
|
/**
|
|
@@ -226,11 +240,6 @@ export interface IHttpProject extends IProjectDefinitionProperty {
|
|
|
226
240
|
* This is where all the data are stored.
|
|
227
241
|
*/
|
|
228
242
|
definitions: IHttpProjectDefinitions;
|
|
229
|
-
/**
|
|
230
|
-
* The list of environment keys to apply to the project.
|
|
231
|
-
* Each key references an environment in the `definitions.environments` array.
|
|
232
|
-
*/
|
|
233
|
-
environments?: string[];
|
|
234
243
|
}
|
|
235
244
|
|
|
236
245
|
export interface IHttpProjectDefinitions {
|
|
@@ -377,12 +386,8 @@ export class HttpProject extends ProjectParent {
|
|
|
377
386
|
if (!init || !init.definitions || !init.items) {
|
|
378
387
|
throw new Error(`Not a project.`);
|
|
379
388
|
}
|
|
380
|
-
const { key = v4(), definitions = {}, items, info, license, provider
|
|
389
|
+
const { key = v4(), definitions = {}, items, info, license, provider } = init;
|
|
381
390
|
this.key = key;
|
|
382
|
-
this.environments = [];
|
|
383
|
-
if (Array.isArray(environments)) {
|
|
384
|
-
this.environments = [...environments];
|
|
385
|
-
}
|
|
386
391
|
if (license) {
|
|
387
392
|
this.license = new License(license);
|
|
388
393
|
} else {
|
|
@@ -430,7 +435,6 @@ export class HttpProject extends ProjectParent {
|
|
|
430
435
|
kind: Kind,
|
|
431
436
|
key: this.key,
|
|
432
437
|
definitions: {},
|
|
433
|
-
environments: [],
|
|
434
438
|
items: [],
|
|
435
439
|
info: this.info.toJSON(),
|
|
436
440
|
};
|
|
@@ -446,9 +450,6 @@ export class HttpProject extends ProjectParent {
|
|
|
446
450
|
if (Array.isArray(this.definitions.schemas) && this.definitions.schemas.length) {
|
|
447
451
|
result.definitions.schemas = this.definitions.schemas.map(i => i.toJSON());
|
|
448
452
|
}
|
|
449
|
-
if (Array.isArray(this.environments) && this.environments.length) {
|
|
450
|
-
result.environments = [...this.environments];
|
|
451
|
-
}
|
|
452
453
|
if (Array.isArray(this.items) && this.items.length) {
|
|
453
454
|
result.items = this.items.map(i => i.toJSON());
|
|
454
455
|
}
|
|
@@ -1010,24 +1011,18 @@ export class HttpProject extends ProjectParent {
|
|
|
1010
1011
|
const { items = [] } = root;
|
|
1011
1012
|
const { definitions } = this;
|
|
1012
1013
|
items.forEach((item) => {
|
|
1013
|
-
let definition: ProjectFolder | ProjectRequest | undefined;
|
|
1014
|
+
let definition: ProjectFolder | ProjectRequest | Environment | undefined;
|
|
1014
1015
|
if (item.kind === ProjectFolderKind) {
|
|
1015
1016
|
definition = definitions.folders.find(d => item.key === d.key);
|
|
1016
1017
|
} else if (item.kind === ProjectRequestKind) {
|
|
1017
1018
|
definition = definitions.requests.find(d => item.key === d.key);
|
|
1019
|
+
} else if (item.kind === EnvironmentKind) {
|
|
1020
|
+
definition = definitions.environments.find(d => item.key === d.key);
|
|
1018
1021
|
}
|
|
1019
1022
|
if (definition) {
|
|
1020
1023
|
result.push(definition);
|
|
1021
1024
|
}
|
|
1022
1025
|
});
|
|
1023
|
-
if (Array.isArray(root.environments)) {
|
|
1024
|
-
root.environments.map((id) => {
|
|
1025
|
-
const definition = definitions.environments.find(d => d.key === id);
|
|
1026
|
-
if (definition) {
|
|
1027
|
-
result.push(definition);
|
|
1028
|
-
}
|
|
1029
|
-
});
|
|
1030
|
-
}
|
|
1031
1026
|
return result;
|
|
1032
1027
|
}
|
|
1033
1028
|
|
|
@@ -1072,61 +1067,6 @@ export class HttpProject extends ProjectParent {
|
|
|
1072
1067
|
return this;
|
|
1073
1068
|
}
|
|
1074
1069
|
|
|
1075
|
-
/**
|
|
1076
|
-
* Reads the list of environments from then selected folder up to the project environments.
|
|
1077
|
-
* It stops going up in the project structure when selected environment has the `encapsulated`
|
|
1078
|
-
* property set to true.
|
|
1079
|
-
* The environments are ordered from the top-most level to the selected folder.
|
|
1080
|
-
*
|
|
1081
|
-
* @param opts The environment read options
|
|
1082
|
-
*/
|
|
1083
|
-
async readEnvironments(opts: IReadEnvironmentOptions = {}): Promise<Environment[]> {
|
|
1084
|
-
const result: Environment[] = [];
|
|
1085
|
-
const { folderKey, nameOrKey } = opts;
|
|
1086
|
-
|
|
1087
|
-
const root = folderKey ? this.findFolder(folderKey, { keyOnly: true }) : this;
|
|
1088
|
-
if (!root) {
|
|
1089
|
-
return result;
|
|
1090
|
-
}
|
|
1091
|
-
|
|
1092
|
-
let current: HttpProject | ProjectFolder | undefined = root;
|
|
1093
|
-
while (current) {
|
|
1094
|
-
const environments = current.getEnvironments();
|
|
1095
|
-
if (environments.length) {
|
|
1096
|
-
const selected = nameOrKey ? environments.find(i => i.key === nameOrKey || i.info.name === nameOrKey) : environments[0];
|
|
1097
|
-
if (selected) {
|
|
1098
|
-
result.push(selected);
|
|
1099
|
-
if (selected.encapsulated) {
|
|
1100
|
-
break;
|
|
1101
|
-
}
|
|
1102
|
-
}
|
|
1103
|
-
}
|
|
1104
|
-
current = current.getParent();
|
|
1105
|
-
}
|
|
1106
|
-
|
|
1107
|
-
return result.reverse();
|
|
1108
|
-
}
|
|
1109
|
-
|
|
1110
|
-
/**
|
|
1111
|
-
* @returns Returns the effective environments. If the project has been initialized with an environment then it is returned. Otherwise other environments.
|
|
1112
|
-
*/
|
|
1113
|
-
getEnvironments(): Environment[] {
|
|
1114
|
-
if (Array.isArray(this.initEnvironments)) {
|
|
1115
|
-
return this.initEnvironments;
|
|
1116
|
-
}
|
|
1117
|
-
return super.getEnvironments();
|
|
1118
|
-
}
|
|
1119
|
-
|
|
1120
|
-
/**
|
|
1121
|
-
* Finds a definition for an environment regardless of its parent.
|
|
1122
|
-
*
|
|
1123
|
-
* @param key The Key of the environment to find.
|
|
1124
|
-
* @returns The environment definition or undefined if not found.
|
|
1125
|
-
*/
|
|
1126
|
-
findEnvironment(key: string): Environment | undefined {
|
|
1127
|
-
return this.definitions.environments.find(i => i.key === key);
|
|
1128
|
-
}
|
|
1129
|
-
|
|
1130
1070
|
/**
|
|
1131
1071
|
* Makes a copy of this project.
|
|
1132
1072
|
*/
|
|
@@ -1160,14 +1100,14 @@ export class HttpProject extends ProjectParent {
|
|
|
1160
1100
|
flatItems = flatItems.concat(folder.items);
|
|
1161
1101
|
}
|
|
1162
1102
|
});
|
|
1163
|
-
const withEnvironments: (HttpProject | ProjectFolder)[] = [];
|
|
1164
|
-
if (Array.isArray(src.environments) && src.environments.length) {
|
|
1165
|
-
|
|
1166
|
-
}
|
|
1103
|
+
// const withEnvironments: (HttpProject | ProjectFolder)[] = [];
|
|
1104
|
+
// if (Array.isArray(src.environments) && src.environments.length) {
|
|
1105
|
+
// withEnvironments.push(src);
|
|
1106
|
+
// }
|
|
1167
1107
|
(definitions.folders || []).forEach((folder) => {
|
|
1168
|
-
if (Array.isArray(folder.environments) && folder.environments.length) {
|
|
1169
|
-
|
|
1170
|
-
}
|
|
1108
|
+
// if (Array.isArray(folder.environments) && folder.environments.length) {
|
|
1109
|
+
// withEnvironments.push(folder);
|
|
1110
|
+
// }
|
|
1171
1111
|
const oldKey = folder.key;
|
|
1172
1112
|
const indexObject = flatItems.find(i => i.key === oldKey);
|
|
1173
1113
|
if (!indexObject) {
|
|
@@ -1191,15 +1131,14 @@ export class HttpProject extends ProjectParent {
|
|
|
1191
1131
|
schema.key = v4();
|
|
1192
1132
|
});
|
|
1193
1133
|
(definitions.environments || []).forEach((environment) => {
|
|
1194
|
-
// project or folder that has the environment.
|
|
1195
|
-
const parent = withEnvironments.find(item => item.environments.includes(environment.key));
|
|
1196
1134
|
const oldKey = environment.key;
|
|
1135
|
+
const indexObject = flatItems.find(i => i.key === oldKey);
|
|
1136
|
+
if (!indexObject) {
|
|
1137
|
+
return;
|
|
1138
|
+
}
|
|
1197
1139
|
const newKey = v4();
|
|
1140
|
+
indexObject.key = newKey;
|
|
1198
1141
|
environment.key = newKey;
|
|
1199
|
-
if (parent) {
|
|
1200
|
-
const index = parent.environments.indexOf(oldKey);
|
|
1201
|
-
parent.environments[index] = newKey;
|
|
1202
|
-
}
|
|
1203
1142
|
});
|
|
1204
1143
|
}
|
|
1205
1144
|
|
|
@@ -1384,4 +1323,202 @@ export class HttpProject extends ProjectParent {
|
|
|
1384
1323
|
recursive: true,
|
|
1385
1324
|
});
|
|
1386
1325
|
}
|
|
1326
|
+
|
|
1327
|
+
/**
|
|
1328
|
+
* Depending on the options returns a project or a folder.
|
|
1329
|
+
* It throws when parent folder cannot ber found.
|
|
1330
|
+
*/
|
|
1331
|
+
protected _getRoot(opts: IItemOptions): ProjectFolder | HttpProject {
|
|
1332
|
+
const project = this.getProject();
|
|
1333
|
+
if (opts.parent) {
|
|
1334
|
+
const parent = project.findFolder(opts.parent);
|
|
1335
|
+
if (!parent) {
|
|
1336
|
+
throw new Error(`Unable to find the parent folder ${opts.parent}.`);
|
|
1337
|
+
}
|
|
1338
|
+
return parent;
|
|
1339
|
+
}
|
|
1340
|
+
return project;
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
protected _insertItem(item: ProjectItem, root: ProjectFolder | HttpProject, opts: IItemCreateOptions): void {
|
|
1344
|
+
if (!Array.isArray(root.items)) {
|
|
1345
|
+
root.items = [];
|
|
1346
|
+
}
|
|
1347
|
+
if (typeof opts.index === 'number') {
|
|
1348
|
+
root.items.splice(opts.index, 0, item);
|
|
1349
|
+
} else {
|
|
1350
|
+
root.items.push(item);
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
/**
|
|
1355
|
+
* Adds an environment to the project.
|
|
1356
|
+
*
|
|
1357
|
+
* @param env The definition of the environment to use to create the environment
|
|
1358
|
+
* @returns The same or created environment.
|
|
1359
|
+
*/
|
|
1360
|
+
addEnvironment(env: IEnvironment, opts?: IEnvironmentCreateOptions): Environment;
|
|
1361
|
+
|
|
1362
|
+
/**
|
|
1363
|
+
* Adds an environment to the project.
|
|
1364
|
+
*
|
|
1365
|
+
* @param env The instance of the environment to add
|
|
1366
|
+
* @returns The same or created environment.
|
|
1367
|
+
*/
|
|
1368
|
+
addEnvironment(env: Environment, opts?: IEnvironmentCreateOptions): Environment;
|
|
1369
|
+
|
|
1370
|
+
/**
|
|
1371
|
+
* Adds an environment to the project.
|
|
1372
|
+
*
|
|
1373
|
+
* @param env The name of the environment to create
|
|
1374
|
+
* @returns The same or created environment.
|
|
1375
|
+
*/
|
|
1376
|
+
addEnvironment(env: string, opts?: IEnvironmentCreateOptions): Environment;
|
|
1377
|
+
|
|
1378
|
+
/**
|
|
1379
|
+
* Adds an environment to the project.
|
|
1380
|
+
* @returns The same or created environment.
|
|
1381
|
+
*/
|
|
1382
|
+
addEnvironment(env: IEnvironment | Environment | string, opts: IEnvironmentCreateOptions = {}): Environment {
|
|
1383
|
+
const environment = this._createEnv(env);
|
|
1384
|
+
const root = this._getRoot(opts);
|
|
1385
|
+
const project = this.getProject();
|
|
1386
|
+
if (!project.definitions.environments) {
|
|
1387
|
+
project.definitions.environments = [];
|
|
1388
|
+
}
|
|
1389
|
+
project.definitions.environments.push(environment);
|
|
1390
|
+
const item = ProjectItem.projectEnvironment(project, environment.key);
|
|
1391
|
+
this._insertItem(item, root, opts)
|
|
1392
|
+
return environment;
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
protected _createEnv(env: IEnvironment | Environment | string): Environment {
|
|
1396
|
+
let finalEnv: Environment;
|
|
1397
|
+
if (env instanceof Environment) {
|
|
1398
|
+
finalEnv = env;
|
|
1399
|
+
} else if (typeof env === 'string') {
|
|
1400
|
+
finalEnv = Environment.fromName(env);
|
|
1401
|
+
} else {
|
|
1402
|
+
finalEnv = new Environment(env);
|
|
1403
|
+
}
|
|
1404
|
+
if (!finalEnv.key) {
|
|
1405
|
+
finalEnv.key = v4();
|
|
1406
|
+
}
|
|
1407
|
+
return finalEnv;
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
/**
|
|
1411
|
+
* This is different to `readEnvironments()`. While the `readEnvironments()`
|
|
1412
|
+
* function generate a list of all environments that apply to a folder, this method
|
|
1413
|
+
* just lists this folder's environments.
|
|
1414
|
+
*
|
|
1415
|
+
* @returns The list of environments defined in this folder
|
|
1416
|
+
*/
|
|
1417
|
+
getEnvironments(opts: IItemOptions = {}): Environment[] {
|
|
1418
|
+
if (Array.isArray(this.initEnvironments)) {
|
|
1419
|
+
return this.initEnvironments;
|
|
1420
|
+
}
|
|
1421
|
+
return this.listEnvironments(opts);
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
/**
|
|
1425
|
+
* @param key The environment key to read.
|
|
1426
|
+
*/
|
|
1427
|
+
getEnvironment(key: string, opts: IItemOptions = {}): Environment | undefined {
|
|
1428
|
+
const root = this._getRoot(opts);
|
|
1429
|
+
const item = root.items.find(i => i.key === key);
|
|
1430
|
+
if (!item) {
|
|
1431
|
+
return undefined;
|
|
1432
|
+
}
|
|
1433
|
+
const project = this.getProject();
|
|
1434
|
+
if (!Array.isArray(project.definitions.environments)) {
|
|
1435
|
+
project.definitions.environments = [];
|
|
1436
|
+
}
|
|
1437
|
+
return project.definitions.environments.find(e => e.key === key);
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
/**
|
|
1441
|
+
* Removes an environment from the folder or a sub-folder.
|
|
1442
|
+
*
|
|
1443
|
+
* @param key the key of the environment to remove
|
|
1444
|
+
* @returns The removed environment, if any.
|
|
1445
|
+
*/
|
|
1446
|
+
removeEnvironment(key: string, opts: IItemOptions = {}): Environment | undefined {
|
|
1447
|
+
const root = this._getRoot(opts);
|
|
1448
|
+
const itemIndex = root.items.findIndex(i => i.key === key);
|
|
1449
|
+
if (itemIndex < 0) {
|
|
1450
|
+
return undefined;
|
|
1451
|
+
}
|
|
1452
|
+
root.items.splice(itemIndex, 1);
|
|
1453
|
+
const project = this.getProject();
|
|
1454
|
+
if (!Array.isArray(project.definitions.environments)) {
|
|
1455
|
+
project.definitions.environments = [];
|
|
1456
|
+
}
|
|
1457
|
+
const defIndex = project.definitions.environments.findIndex(i => i.key === key);
|
|
1458
|
+
if (defIndex < 0) {
|
|
1459
|
+
return undefined;
|
|
1460
|
+
}
|
|
1461
|
+
const env = project.definitions.environments[defIndex];
|
|
1462
|
+
project.definitions.environments.splice(defIndex, 1);
|
|
1463
|
+
return env;
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
/**
|
|
1467
|
+
* This is a link to the `getEnvironments()`. The difference is that on the
|
|
1468
|
+
* project level it won't return environments defined with the class initialization.
|
|
1469
|
+
*/
|
|
1470
|
+
listEnvironments(opts: IItemOptions = {}): Environment[] {
|
|
1471
|
+
const root = this._getRoot(opts);
|
|
1472
|
+
const items = root.items.filter(i => i.kind === EnvironmentKind).map(i => i.key);
|
|
1473
|
+
const project = this.getProject();
|
|
1474
|
+
if (!Array.isArray(project.definitions.environments)) {
|
|
1475
|
+
project.definitions.environments = [];
|
|
1476
|
+
}
|
|
1477
|
+
return project.definitions.environments.filter(e => items.includes(e.key));
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
/**
|
|
1481
|
+
* Reads the list of environments from then selected folder up to the project root.
|
|
1482
|
+
* It stops going up in the project structure when selected environment has the `encapsulated`
|
|
1483
|
+
* property set to true.
|
|
1484
|
+
* The environments are ordered from the top-most level to the selected folder.
|
|
1485
|
+
*
|
|
1486
|
+
* @param opts The environment read options
|
|
1487
|
+
*/
|
|
1488
|
+
async readEnvironments(opts: IReadEnvironmentOptions = {}): Promise<Environment[]> {
|
|
1489
|
+
const result: Environment[] = [];
|
|
1490
|
+
const { parent, nameOrKey } = opts;
|
|
1491
|
+
|
|
1492
|
+
const root = parent ? this.findFolder(parent, { keyOnly: true }) : this;
|
|
1493
|
+
if (!root) {
|
|
1494
|
+
return result;
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
let current: HttpProject | ProjectFolder | undefined = root;
|
|
1498
|
+
while (current) {
|
|
1499
|
+
const environments = current.getEnvironments();
|
|
1500
|
+
if (environments.length) {
|
|
1501
|
+
const selected = nameOrKey ? environments.find(i => i.key === nameOrKey || i.info.name === nameOrKey) : environments[0];
|
|
1502
|
+
if (selected) {
|
|
1503
|
+
result.push(selected);
|
|
1504
|
+
if (selected.encapsulated) {
|
|
1505
|
+
break;
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
current = current.getParent();
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
return result.reverse();
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
/**
|
|
1516
|
+
* Finds a definition for an environment regardless of its parent.
|
|
1517
|
+
*
|
|
1518
|
+
* @param key The Key of the environment to find.
|
|
1519
|
+
* @returns The environment definition or undefined if not found.
|
|
1520
|
+
*/
|
|
1521
|
+
findEnvironment(key: string): Environment | undefined {
|
|
1522
|
+
return this.definitions.environments.find(i => i.key === key);
|
|
1523
|
+
}
|
|
1387
1524
|
}
|
|
@@ -2,8 +2,9 @@ import { ProjectParent } from './ProjectParent.js';
|
|
|
2
2
|
import { IProjectDefinitionProperty } from './ProjectDefinitionProperty.js';
|
|
3
3
|
import { ProjectItem, IProjectItem } from './ProjectItem.js';
|
|
4
4
|
import { ProjectRequest, Kind as ProjectRequestKind, IProjectRequest } from './ProjectRequest.js';
|
|
5
|
-
import { HttpProject } from './HttpProject.js';
|
|
5
|
+
import { HttpProject, IEnvironmentCreateOptions } from './HttpProject.js';
|
|
6
6
|
import { IThing, Thing, Kind as ThingKind } from './Thing.js';
|
|
7
|
+
import { Environment, IEnvironment } from './Environment.js';
|
|
7
8
|
import v4 from '../lib/uuid.js';
|
|
8
9
|
|
|
9
10
|
export const Kind = 'Core#ProjectFolder';
|
|
@@ -58,11 +59,6 @@ export interface IProjectFolder extends IProjectDefinitionProperty {
|
|
|
58
59
|
* The UI uses this to manipulate the view without changing the definitions.
|
|
59
60
|
*/
|
|
60
61
|
items: IProjectItem[];
|
|
61
|
-
/**
|
|
62
|
-
* The environments defined for this project.
|
|
63
|
-
* If not set it is inherited from the parent.
|
|
64
|
-
*/
|
|
65
|
-
environments?: string[];
|
|
66
62
|
/**
|
|
67
63
|
* Timestamp when the folder was last updated.
|
|
68
64
|
*/
|
|
@@ -132,7 +128,7 @@ export class ProjectFolder extends ProjectParent {
|
|
|
132
128
|
if (!ProjectFolder.isProjectFolder(init)) {
|
|
133
129
|
throw new Error(`Not a project folder.`);
|
|
134
130
|
}
|
|
135
|
-
const { key = v4(), created = Date.now(), updated = Date.now(), items,
|
|
131
|
+
const { key = v4(), created = Date.now(), updated = Date.now(), items, info } = init;
|
|
136
132
|
this.kind = Kind;
|
|
137
133
|
this.key = key;
|
|
138
134
|
this.created = created;
|
|
@@ -147,11 +143,6 @@ export class ProjectFolder extends ProjectParent {
|
|
|
147
143
|
} else {
|
|
148
144
|
this.info = new Thing({ kind: ThingKind, name: DefaultFolderName });
|
|
149
145
|
}
|
|
150
|
-
if (Array.isArray(environments)) {
|
|
151
|
-
this.environments = [...environments];
|
|
152
|
-
} else {
|
|
153
|
-
this.environments = [];
|
|
154
|
-
}
|
|
155
146
|
}
|
|
156
147
|
|
|
157
148
|
/**
|
|
@@ -173,14 +164,10 @@ export class ProjectFolder extends ProjectParent {
|
|
|
173
164
|
created: this.created,
|
|
174
165
|
updated: this.updated,
|
|
175
166
|
items: [],
|
|
176
|
-
environments: [],
|
|
177
167
|
};
|
|
178
168
|
if (Array.isArray(this.items)) {
|
|
179
169
|
result.items = this.items.map(i => i.toJSON());
|
|
180
170
|
}
|
|
181
|
-
if (Array.isArray(this.environments)) {
|
|
182
|
-
result.environments = [...this.environments];
|
|
183
|
-
}
|
|
184
171
|
return result;
|
|
185
172
|
}
|
|
186
173
|
|
|
@@ -198,7 +185,6 @@ export class ProjectFolder extends ProjectParent {
|
|
|
198
185
|
created: now,
|
|
199
186
|
updated: now,
|
|
200
187
|
items: [],
|
|
201
|
-
environments: [],
|
|
202
188
|
kind: Kind,
|
|
203
189
|
info: info.toJSON(),
|
|
204
190
|
});
|
|
@@ -271,6 +257,39 @@ export class ProjectFolder extends ProjectParent {
|
|
|
271
257
|
return this.project.addRequest(request, addOptions);
|
|
272
258
|
}
|
|
273
259
|
|
|
260
|
+
/**
|
|
261
|
+
* Adds an environment to the project.
|
|
262
|
+
*
|
|
263
|
+
* @param env The definition of the environment to use to create the environment
|
|
264
|
+
* @returns The same or created environment.
|
|
265
|
+
*/
|
|
266
|
+
addEnvironment(env: IEnvironment, opts?: IEnvironmentCreateOptions): Environment;
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Adds an environment to the project.
|
|
270
|
+
*
|
|
271
|
+
* @param env The instance of the environment to add
|
|
272
|
+
* @returns The same or created environment.
|
|
273
|
+
*/
|
|
274
|
+
addEnvironment(env: Environment, opts?: IEnvironmentCreateOptions): Environment;
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Adds an environment to the project.
|
|
278
|
+
*
|
|
279
|
+
* @param env The name of the environment to create
|
|
280
|
+
* @returns The same or created environment.
|
|
281
|
+
*/
|
|
282
|
+
addEnvironment(env: string, opts?: IEnvironmentCreateOptions): Environment;
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Adds an environment to the project.
|
|
286
|
+
* @returns The same or created environment.
|
|
287
|
+
*/
|
|
288
|
+
addEnvironment(env: IEnvironment | Environment | string, opts: IEnvironmentCreateOptions = {}): Environment {
|
|
289
|
+
const newOptions: IEnvironmentCreateOptions = { ...opts, parent: this.key };
|
|
290
|
+
return this.project.addEnvironment(env as Environment, newOptions);
|
|
291
|
+
}
|
|
292
|
+
|
|
274
293
|
/**
|
|
275
294
|
* Lists items (not the actual definitions!) that are folders.
|
|
276
295
|
*/
|
|
@@ -341,6 +360,38 @@ export class ProjectFolder extends ProjectParent {
|
|
|
341
360
|
this.project.removeFolder(this.key);
|
|
342
361
|
}
|
|
343
362
|
|
|
363
|
+
/**
|
|
364
|
+
* @returns The list of environments defined in this folder
|
|
365
|
+
*/
|
|
366
|
+
getEnvironments(): Environment[] {
|
|
367
|
+
return this.project.getEnvironments({ parent: this.key });
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* @param key The environment key to read.
|
|
372
|
+
*/
|
|
373
|
+
getEnvironment(key: string): Environment | undefined {
|
|
374
|
+
return this.project.getEnvironment(key, { parent: this.key });
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Removes an environment from the folder or a sub-folder.
|
|
379
|
+
*
|
|
380
|
+
* @param key the key of the environment to remove
|
|
381
|
+
* @returns The removed environment, if any.
|
|
382
|
+
*/
|
|
383
|
+
removeEnvironment(key: string): Environment | undefined {
|
|
384
|
+
return this.project.removeEnvironment(key, { parent: this.key });
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* This is a link to the `getEnvironments()`. The difference is that on the
|
|
389
|
+
* project level it won't return environments defined with the class initialization.
|
|
390
|
+
*/
|
|
391
|
+
listEnvironments(): Environment[] {
|
|
392
|
+
return this.project.listEnvironments({ parent: this.key });
|
|
393
|
+
}
|
|
394
|
+
|
|
344
395
|
/**
|
|
345
396
|
* Makes a copy of this folder.
|
|
346
397
|
* By default it attaches the copied folder to the same parent.
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { Kind as ProjectFolderKind, ProjectFolder } from './ProjectFolder.js';
|
|
2
2
|
import { Kind as ProjectRequestKind, ProjectRequest } from './ProjectRequest.js';
|
|
3
|
+
import { Kind as EnvironmentKind, Environment } from './Environment.js';
|
|
3
4
|
import { HttpProject } from './HttpProject.js';
|
|
4
5
|
|
|
6
|
+
type Kind = typeof ProjectFolderKind | typeof ProjectRequestKind | typeof EnvironmentKind;
|
|
7
|
+
|
|
5
8
|
export interface IProjectItem {
|
|
6
9
|
/**
|
|
7
10
|
* The kind of the item.
|
|
8
11
|
*/
|
|
9
|
-
kind:
|
|
12
|
+
kind: Kind;
|
|
10
13
|
/**
|
|
11
14
|
* The identifier in the `definitions` array of the project.
|
|
12
15
|
*/
|
|
@@ -17,7 +20,7 @@ export class ProjectItem {
|
|
|
17
20
|
/**
|
|
18
21
|
* The kind of the item.
|
|
19
22
|
*/
|
|
20
|
-
kind:
|
|
23
|
+
kind: Kind = ProjectRequestKind;
|
|
21
24
|
/**
|
|
22
25
|
* The identifier of the object in the `definitions` array of the project.
|
|
23
26
|
*/
|
|
@@ -32,16 +35,16 @@ export class ProjectItem {
|
|
|
32
35
|
*/
|
|
33
36
|
static isProjectItem(input: unknown): boolean {
|
|
34
37
|
const typed = input as IProjectItem;
|
|
35
|
-
if (!input || ![ProjectFolderKind, ProjectRequestKind].includes(typed.kind)) {
|
|
38
|
+
if (!input || ![ProjectFolderKind, ProjectRequestKind, EnvironmentKind].includes(typed.kind)) {
|
|
36
39
|
return false;
|
|
37
40
|
}
|
|
38
41
|
return true;
|
|
39
42
|
}
|
|
40
43
|
|
|
41
44
|
/**
|
|
42
|
-
* @return An instance that represents a request
|
|
45
|
+
* @return An instance that represents a request item
|
|
43
46
|
*/
|
|
44
|
-
static projectRequest(project: HttpProject, key: string)
|
|
47
|
+
static projectRequest(project: HttpProject, key: string): ProjectItem {
|
|
45
48
|
const item = new ProjectItem(project, {
|
|
46
49
|
kind: ProjectRequestKind,
|
|
47
50
|
key,
|
|
@@ -50,9 +53,9 @@ export class ProjectItem {
|
|
|
50
53
|
}
|
|
51
54
|
|
|
52
55
|
/**
|
|
53
|
-
* @return An instance that represents a folder
|
|
56
|
+
* @return An instance that represents a folder item
|
|
54
57
|
*/
|
|
55
|
-
static projectFolder(project: HttpProject, key: string)
|
|
58
|
+
static projectFolder(project: HttpProject, key: string): ProjectItem {
|
|
56
59
|
const item = new ProjectItem(project, {
|
|
57
60
|
kind: ProjectFolderKind,
|
|
58
61
|
key,
|
|
@@ -60,11 +63,22 @@ export class ProjectItem {
|
|
|
60
63
|
return item;
|
|
61
64
|
}
|
|
62
65
|
|
|
66
|
+
/**
|
|
67
|
+
* @return An instance that represents an environment item
|
|
68
|
+
*/
|
|
69
|
+
static projectEnvironment(project: HttpProject, key: string): ProjectItem {
|
|
70
|
+
const item = new ProjectItem(project, {
|
|
71
|
+
kind: EnvironmentKind,
|
|
72
|
+
key,
|
|
73
|
+
});
|
|
74
|
+
return item;
|
|
75
|
+
}
|
|
76
|
+
|
|
63
77
|
/**
|
|
64
78
|
* @param project The top-most project.
|
|
65
79
|
* @param input The project item definition used to restore the state.
|
|
66
80
|
*/
|
|
67
|
-
constructor(project: HttpProject, input: string|IProjectItem) {
|
|
81
|
+
constructor(project: HttpProject, input: string | IProjectItem) {
|
|
68
82
|
this.project = project;
|
|
69
83
|
let init: IProjectItem;
|
|
70
84
|
if (typeof input === 'string') {
|
|
@@ -78,6 +92,11 @@ export class ProjectItem {
|
|
|
78
92
|
kind: ProjectFolderKind,
|
|
79
93
|
key: '',
|
|
80
94
|
};
|
|
95
|
+
} else if (input === 'environment') {
|
|
96
|
+
init = {
|
|
97
|
+
kind: EnvironmentKind,
|
|
98
|
+
key: '',
|
|
99
|
+
};
|
|
81
100
|
} else {
|
|
82
101
|
init = JSON.parse(input);
|
|
83
102
|
}
|
|
@@ -114,7 +133,7 @@ export class ProjectItem {
|
|
|
114
133
|
/**
|
|
115
134
|
* @returns The instance of the definition associated with this item.
|
|
116
135
|
*/
|
|
117
|
-
getItem(): ProjectFolder | ProjectRequest| undefined {
|
|
136
|
+
getItem(): ProjectFolder | ProjectRequest | Environment | undefined {
|
|
118
137
|
const { project, key, kind } = this;
|
|
119
138
|
const { definitions } = project;
|
|
120
139
|
if (kind === ProjectRequestKind) {
|
|
@@ -123,12 +142,15 @@ export class ProjectItem {
|
|
|
123
142
|
if (kind === ProjectFolderKind) {
|
|
124
143
|
return definitions.folders.find(i => i.key === key);
|
|
125
144
|
}
|
|
145
|
+
if (kind === EnvironmentKind) {
|
|
146
|
+
return definitions.environments.find(i => i.key === key);
|
|
147
|
+
}
|
|
126
148
|
}
|
|
127
149
|
|
|
128
150
|
/**
|
|
129
|
-
* @returns The instance of the HttpProject or a ProjectFolder that is a
|
|
151
|
+
* @returns The instance of the HttpProject or a ProjectFolder that is a closest parent of this item.
|
|
130
152
|
*/
|
|
131
|
-
getParent(): ProjectFolder|HttpProject|undefined {
|
|
153
|
+
getParent(): ProjectFolder | HttpProject | undefined {
|
|
132
154
|
const { project, key } = this;
|
|
133
155
|
return project.findParent(key);
|
|
134
156
|
}
|