@api-client/core 0.8.21 → 0.9.0

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 (215) hide show
  1. package/build/browser.d.ts +12 -5
  2. package/build/browser.js +2 -3
  3. package/build/browser.js.map +1 -1
  4. package/build/index.d.ts +12 -5
  5. package/build/index.js +2 -4
  6. package/build/index.js.map +1 -1
  7. package/build/src/events/BaseEvents.d.ts +125 -18
  8. package/build/src/events/BaseEvents.js.map +1 -1
  9. package/build/src/events/EventTypes.d.ts +0 -14
  10. package/build/src/events/EventTypes.js +0 -2
  11. package/build/src/events/EventTypes.js.map +1 -1
  12. package/build/src/events/Events.d.ts +1 -5
  13. package/build/src/events/Events.js +0 -2
  14. package/build/src/events/Events.js.map +1 -1
  15. package/build/src/events/transport/TransportEventTypes.d.ts +0 -1
  16. package/build/src/events/transport/TransportEventTypes.js +0 -1
  17. package/build/src/events/transport/TransportEventTypes.js.map +1 -1
  18. package/build/src/events/transport/TransportEvents.d.ts +0 -8
  19. package/build/src/events/transport/TransportEvents.js +0 -11
  20. package/build/src/events/transport/TransportEvents.js.map +1 -1
  21. package/build/src/mocking/ProjectMock.d.ts +0 -3
  22. package/build/src/mocking/ProjectMock.js +0 -3
  23. package/build/src/mocking/ProjectMock.js.map +1 -1
  24. package/build/src/mocking/lib/History.js +5 -0
  25. package/build/src/mocking/lib/History.js.map +1 -1
  26. package/build/src/models/{Workspace.d.ts → Folder.d.ts} +7 -7
  27. package/build/src/models/{Workspace.js → Folder.js} +5 -5
  28. package/build/src/models/Folder.js.map +1 -0
  29. package/build/src/models/HttpHistory.d.ts +38 -43
  30. package/build/src/models/HttpHistory.js +27 -34
  31. package/build/src/models/HttpHistory.js.map +1 -1
  32. package/build/src/models/HttpProject.d.ts +0 -2
  33. package/build/src/models/HttpProject.js +0 -27
  34. package/build/src/models/HttpProject.js.map +1 -1
  35. package/build/src/models/Project.d.ts +0 -8
  36. package/build/src/models/Project.js +0 -4
  37. package/build/src/models/Project.js.map +1 -1
  38. package/build/src/models/ProjectFolder.d.ts +0 -2
  39. package/build/src/models/ProjectFolder.js +0 -14
  40. package/build/src/models/ProjectFolder.js.map +1 -1
  41. package/build/src/models/ProjectItem.d.ts +0 -2
  42. package/build/src/models/ProjectItem.js +0 -20
  43. package/build/src/models/ProjectItem.js.map +1 -1
  44. package/build/src/models/ProjectRequest.d.ts +2 -3
  45. package/build/src/models/ProjectRequest.js.map +1 -1
  46. package/build/src/models/RequestConfig.d.ts +1 -1
  47. package/build/src/models/Space.d.ts +42 -0
  48. package/build/src/models/Space.js +81 -0
  49. package/build/src/models/Space.js.map +1 -0
  50. package/build/src/models/TrashEntry.d.ts +27 -0
  51. package/build/src/models/TrashEntry.js +2 -0
  52. package/build/src/models/TrashEntry.js.map +1 -0
  53. package/build/src/models/data/DataAssociation.d.ts +22 -5
  54. package/build/src/models/data/DataAssociation.js +29 -11
  55. package/build/src/models/data/DataAssociation.js.map +1 -1
  56. package/build/src/models/data/DataEntity.d.ts +12 -1
  57. package/build/src/models/data/DataEntity.js +49 -10
  58. package/build/src/models/data/DataEntity.js.map +1 -1
  59. package/build/src/models/data/DataNamespace.d.ts +34 -1
  60. package/build/src/models/data/DataNamespace.js +75 -1
  61. package/build/src/models/data/DataNamespace.js.map +1 -1
  62. package/build/src/models/store/Backend.d.ts +0 -315
  63. package/build/src/models/store/Backend.js +0 -1
  64. package/build/src/models/store/Backend.js.map +1 -1
  65. package/build/src/models/store/File.d.ts +30 -0
  66. package/build/src/models/store/File.js +5 -5
  67. package/build/src/models/store/File.js.map +1 -1
  68. package/build/src/proxy/ProxyService.d.ts +0 -10
  69. package/build/src/proxy/ProxyService.js +0 -14
  70. package/build/src/proxy/ProxyService.js.map +1 -1
  71. package/build/src/runtime/node/InteropInterfaces.d.ts +1 -2
  72. package/build/src/runtime/node/ProjectParallelRunner.d.ts +2 -3
  73. package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -1
  74. package/build/src/runtime/node/ProjectRequestRunner.d.ts +7 -8
  75. package/build/src/runtime/node/ProjectRequestRunner.js +2 -3
  76. package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -1
  77. package/build/src/runtime/node/ProjectRunner.d.ts +3 -4
  78. package/build/src/runtime/node/ProjectRunner.js.map +1 -1
  79. package/build/src/runtime/node/ProjectRunnerWorker.js +0 -4
  80. package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -1
  81. package/build/src/runtime/store/CertificatesSdk.d.ts +46 -0
  82. package/build/src/runtime/store/CertificatesSdk.js +227 -0
  83. package/build/src/runtime/store/CertificatesSdk.js.map +1 -0
  84. package/build/src/runtime/store/FilesSdk.d.ts +14 -9
  85. package/build/src/runtime/store/FilesSdk.js +60 -16
  86. package/build/src/runtime/store/FilesSdk.js.map +1 -1
  87. package/build/src/runtime/store/HistorySdk.d.ts +11 -8
  88. package/build/src/runtime/store/HistorySdk.js +140 -17
  89. package/build/src/runtime/store/HistorySdk.js.map +1 -1
  90. package/build/src/runtime/store/RevisionsSdk.d.ts +12 -0
  91. package/build/src/runtime/store/RevisionsSdk.js +43 -0
  92. package/build/src/runtime/store/RevisionsSdk.js.map +1 -0
  93. package/build/src/runtime/store/RouteBuilder.d.ts +16 -18
  94. package/build/src/runtime/store/RouteBuilder.js +33 -60
  95. package/build/src/runtime/store/RouteBuilder.js.map +1 -1
  96. package/build/src/runtime/store/Sdk.d.ts +10 -7
  97. package/build/src/runtime/store/Sdk.js +12 -5
  98. package/build/src/runtime/store/Sdk.js.map +1 -1
  99. package/build/src/runtime/store/SdkBase.js.map +1 -1
  100. package/build/src/runtime/store/SharedSdk.d.ts +2 -2
  101. package/build/src/runtime/store/SharedSdk.js.map +1 -1
  102. package/build/src/runtime/store/SpacesSdk.d.ts +29 -0
  103. package/build/src/runtime/store/SpacesSdk.js +135 -0
  104. package/build/src/runtime/store/SpacesSdk.js.map +1 -0
  105. package/build/src/runtime/store/TrashSdk.d.ts +35 -0
  106. package/build/src/runtime/store/TrashSdk.js +155 -0
  107. package/build/src/runtime/store/TrashSdk.js.map +1 -0
  108. package/build/src/runtime/store/UsersSdk.d.ts +2 -2
  109. package/build/src/runtime/store/UsersSdk.js.map +1 -1
  110. package/build/src/runtime/store/interfaces/ApiClientStore.d.ts +30 -0
  111. package/build/src/runtime/store/interfaces/ApiClientStore.js +2 -0
  112. package/build/src/runtime/store/interfaces/ApiClientStore.js.map +1 -0
  113. package/build/src/runtime/store/interfaces/Certificates.d.ts +49 -0
  114. package/build/src/runtime/store/interfaces/Certificates.js +2 -0
  115. package/build/src/runtime/store/interfaces/Certificates.js.map +1 -0
  116. package/build/src/runtime/store/interfaces/Files.d.ts +63 -0
  117. package/build/src/runtime/store/interfaces/Files.js +2 -0
  118. package/build/src/runtime/store/interfaces/Files.js.map +1 -0
  119. package/build/src/runtime/store/interfaces/History.d.ts +63 -0
  120. package/build/src/runtime/store/interfaces/History.js +2 -0
  121. package/build/src/runtime/store/interfaces/History.js.map +1 -0
  122. package/build/src/runtime/store/interfaces/Media.d.ts +42 -0
  123. package/build/src/runtime/store/interfaces/Media.js +2 -0
  124. package/build/src/runtime/store/interfaces/Media.js.map +1 -0
  125. package/build/src/runtime/store/interfaces/Revisions.d.ts +25 -0
  126. package/build/src/runtime/store/interfaces/Revisions.js +2 -0
  127. package/build/src/runtime/store/interfaces/Revisions.js.map +1 -0
  128. package/build/src/runtime/store/interfaces/Spaces.d.ts +42 -0
  129. package/build/src/runtime/store/interfaces/Spaces.js +2 -0
  130. package/build/src/runtime/store/interfaces/Spaces.js.map +1 -0
  131. package/build/src/runtime/store/interfaces/Trash.d.ts +38 -0
  132. package/build/src/runtime/store/interfaces/Trash.js +2 -0
  133. package/build/src/runtime/store/interfaces/Trash.js.map +1 -0
  134. package/package.json +12 -12
  135. package/src/events/BaseEvents.ts +136 -18
  136. package/src/events/EventTypes.ts +0 -2
  137. package/src/events/Events.ts +0 -2
  138. package/src/events/transport/TransportEventTypes.ts +0 -1
  139. package/src/events/transport/TransportEvents.ts +0 -13
  140. package/src/mocking/ProjectMock.ts +0 -4
  141. package/src/mocking/lib/History.ts +5 -0
  142. package/src/models/{Workspace.ts → Folder.ts} +12 -12
  143. package/src/models/HttpHistory.ts +53 -63
  144. package/src/models/HttpProject.ts +0 -29
  145. package/src/models/Project.ts +0 -8
  146. package/src/models/ProjectFolder.ts +0 -16
  147. package/src/models/ProjectItem.ts +0 -18
  148. package/src/models/ProjectRequest.ts +3 -4
  149. package/src/models/RequestConfig.ts +1 -1
  150. package/src/models/Space.ts +105 -0
  151. package/src/models/TrashEntry.ts +29 -0
  152. package/src/models/data/DataAssociation.ts +50 -15
  153. package/src/models/data/DataEntity.ts +51 -10
  154. package/src/models/data/DataNamespace.ts +78 -2
  155. package/src/models/store/Backend.ts +0 -342
  156. package/src/models/store/File.ts +38 -5
  157. package/src/proxy/ProxyService.ts +0 -15
  158. package/src/proxy/readme.md +0 -1
  159. package/src/runtime/node/InteropInterfaces.ts +1 -2
  160. package/src/runtime/node/ProjectParallelRunner.ts +2 -3
  161. package/src/runtime/node/ProjectRequestRunner.ts +13 -14
  162. package/src/runtime/node/ProjectRunner.ts +3 -4
  163. package/src/runtime/node/ProjectRunnerWorker.ts +1 -4
  164. package/src/runtime/store/CertificatesSdk.ts +231 -0
  165. package/src/runtime/store/FilesSdk.ts +71 -26
  166. package/src/runtime/store/HistorySdk.ts +145 -33
  167. package/src/runtime/store/RevisionsSdk.ts +44 -0
  168. package/src/runtime/store/RouteBuilder.ts +37 -69
  169. package/src/runtime/store/Sdk.ts +17 -7
  170. package/src/runtime/store/SdkBase.ts +1 -1
  171. package/src/runtime/store/SharedSdk.ts +3 -3
  172. package/src/runtime/store/SpacesSdk.ts +136 -0
  173. package/src/runtime/store/TrashSdk.ts +159 -0
  174. package/src/runtime/store/UsersSdk.ts +3 -3
  175. package/src/runtime/store/interfaces/ApiClientStore.ts +39 -0
  176. package/src/runtime/store/interfaces/Certificates.ts +56 -0
  177. package/src/runtime/store/interfaces/Files.ts +70 -0
  178. package/src/runtime/store/interfaces/History.ts +64 -0
  179. package/src/runtime/store/interfaces/Media.ts +46 -0
  180. package/src/runtime/store/interfaces/Revisions.ts +27 -0
  181. package/src/runtime/store/interfaces/Spaces.ts +48 -0
  182. package/src/runtime/store/interfaces/Trash.ts +42 -0
  183. package/build/src/events/models/ClientCertificateEvents.d.ts +0 -54
  184. package/build/src/events/models/ClientCertificateEvents.js +0 -78
  185. package/build/src/events/models/ClientCertificateEvents.js.map +0 -1
  186. package/build/src/events/models/ModelEventTypes.d.ts +0 -13
  187. package/build/src/events/models/ModelEventTypes.js +0 -14
  188. package/build/src/events/models/ModelEventTypes.js.map +0 -1
  189. package/build/src/events/models/ModelEvents.d.ts +0 -4
  190. package/build/src/events/models/ModelEvents.js +0 -5
  191. package/build/src/events/models/ModelEvents.js.map +0 -1
  192. package/build/src/mocking/lib/App.d.ts +0 -36
  193. package/build/src/mocking/lib/App.js +0 -70
  194. package/build/src/mocking/lib/App.js.map +0 -1
  195. package/build/src/models/AppProject.d.ts +0 -817
  196. package/build/src/models/AppProject.js +0 -1721
  197. package/build/src/models/AppProject.js.map +0 -1
  198. package/build/src/models/AppRequest.d.ts +0 -69
  199. package/build/src/models/AppRequest.js +0 -150
  200. package/build/src/models/AppRequest.js.map +0 -1
  201. package/build/src/models/Workspace.js.map +0 -1
  202. package/build/src/proxy/AppProjectProxy.d.ts +0 -28
  203. package/build/src/proxy/AppProjectProxy.js +0 -107
  204. package/build/src/proxy/AppProjectProxy.js.map +0 -1
  205. package/build/src/runtime/store/AppSdk.d.ts +0 -213
  206. package/build/src/runtime/store/AppSdk.js +0 -836
  207. package/build/src/runtime/store/AppSdk.js.map +0 -1
  208. package/src/events/models/ClientCertificateEvents.ts +0 -85
  209. package/src/events/models/ModelEventTypes.ts +0 -13
  210. package/src/events/models/ModelEvents.ts +0 -5
  211. package/src/mocking/lib/App.ts +0 -101
  212. package/src/models/AppProject.ts +0 -2188
  213. package/src/models/AppRequest.ts +0 -176
  214. package/src/proxy/AppProjectProxy.ts +0 -126
  215. package/src/runtime/store/AppSdk.ts +0 -842
@@ -1,1721 +0,0 @@
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 { Environment, Kind as EnvironmentKind } from './Environment.js';
10
- import { Thing, Kind as ThingKind } from "./Thing.js";
11
- import { Certificate } from "./ClientCertificate.js";
12
- import { File, DefaultOwner } from "./store/File.js";
13
- export const AppProjectKind = 'Core#AppProject';
14
- export const AppProjectFolderKind = 'Core#AppProjectFolder';
15
- export const AppProjectRequestKind = 'Core#AppProjectRequest';
16
- export const AppProjectFileKind = 'Core#AppProjectFile';
17
- export class AppProjectRequest extends Request {
18
- kind = AppProjectRequestKind;
19
- /**
20
- * The identifier of the request.
21
- */
22
- key = '';
23
- /**
24
- * A reference to the top level project object.
25
- */
26
- project;
27
- /**
28
- * Creates a project request from an URL.
29
- * This does not manipulate the project.
30
- *
31
- * @param url The Request URL. This is required.
32
- * @param project The parent project.
33
- */
34
- static fromUrl(url, project) {
35
- if (!project) {
36
- throw new Error(`The project is required.`);
37
- }
38
- const now = Date.now();
39
- const request = new AppProjectRequest(project, {
40
- key: v4(),
41
- kind: AppProjectRequestKind,
42
- created: now,
43
- updated: now,
44
- expects: HttpRequest.fromBaseValues({ url, method: 'GET' }).toJSON(),
45
- info: Thing.fromName(url).toJSON(),
46
- });
47
- return request;
48
- }
49
- /**
50
- * Creates a project request from a name.
51
- * This does not manipulate the project.
52
- *
53
- * @param name The Request name.
54
- * @param project The parent project.This is required.
55
- */
56
- static fromName(name, project) {
57
- if (!project) {
58
- throw new Error(`The project is required.`);
59
- }
60
- const now = Date.now();
61
- const request = new AppProjectRequest(project, {
62
- key: v4(),
63
- kind: AppProjectRequestKind,
64
- created: now,
65
- updated: now,
66
- expects: new HttpRequest().toJSON(),
67
- info: Thing.fromName(name).toJSON(),
68
- });
69
- return request;
70
- }
71
- /**
72
- * Creates a project request from an HttpRequest definition.
73
- * This does not manipulate the project.
74
- *
75
- * @param project The parent project This is required.
76
- * @param info The request data.
77
- */
78
- static fromHttpRequest(info, project) {
79
- if (!project) {
80
- throw new Error(`The project is required.`);
81
- }
82
- const now = Date.now();
83
- const request = new AppProjectRequest(project, {
84
- key: v4(),
85
- kind: AppProjectRequestKind,
86
- created: now,
87
- updated: now,
88
- expects: HttpRequest.fromBaseValues({ method: info.method, url: info.url, headers: info.headers, payload: info.payload }).toJSON(),
89
- info: Thing.fromName(info.url).toJSON(),
90
- });
91
- return request;
92
- }
93
- /**
94
- * Creates a project request for a schema of a Request.
95
- */
96
- static fromRequest(request, project) {
97
- const key = v4();
98
- const init = { ...request, key, kind: AppProjectRequestKind };
99
- const result = new AppProjectRequest(project, init);
100
- return result;
101
- }
102
- constructor(project, input) {
103
- super(input);
104
- this.project = project;
105
- let init;
106
- if (typeof input === 'string') {
107
- init = JSON.parse(input);
108
- }
109
- else if (typeof input === 'object') {
110
- init = input;
111
- }
112
- if (init) {
113
- this.key = init.key || v4();
114
- }
115
- this.kind = AppProjectRequestKind;
116
- }
117
- new(init) {
118
- super.new(init);
119
- const { key = v4() } = init;
120
- this.key = key;
121
- this.kind = AppProjectRequestKind;
122
- }
123
- toJSON() {
124
- const request = super.toJSON();
125
- const result = { ...request, key: this.key, kind: AppProjectRequestKind };
126
- return result;
127
- }
128
- /**
129
- * @returns The instance of the ArcProject or a ArcProjectFolder that is a closes parent of this instance.
130
- */
131
- getParent() {
132
- const { project, key } = this;
133
- return project.findParent(key);
134
- }
135
- /**
136
- * @returns A reference to the parent folder or the top-level HTTP project.
137
- */
138
- getProject() {
139
- return this.project;
140
- }
141
- /**
142
- * Removes this request from the project.
143
- */
144
- remove() {
145
- this.project.removeRequest(this.key);
146
- }
147
- /**
148
- * Makes a copy of this request.
149
- * By default it attaches the copied request to the same parent.
150
- * Use the options dictionary to control this behavior.
151
- */
152
- clone(opts = {}) {
153
- const copy = new AppProjectRequest(this.project, this.toJSON());
154
- if (!opts.withoutRevalidate) {
155
- copy.key = v4();
156
- }
157
- if (!opts.withoutAttach) {
158
- // if the parent is the project then add the request to the project.
159
- const parent = this.getParent();
160
- if (parent) {
161
- parent.addRequest(copy);
162
- }
163
- }
164
- return copy;
165
- }
166
- /**
167
- * The static version of the `clone()` method.
168
- *
169
- * @param request The request schema to clone.
170
- * @param project The project to add the request to.
171
- * @param opts Optional options.
172
- * @returns The copied request.
173
- */
174
- static clone(request, project, opts = {}) {
175
- const obj = new AppProjectRequest(project, request);
176
- return obj.clone(opts);
177
- }
178
- }
179
- export class AppProjectItem {
180
- /**
181
- * The kind of the item.
182
- */
183
- kind = AppProjectRequestKind;
184
- /**
185
- * The identifier of the object in the `definitions` array of the project.
186
- */
187
- key = '';
188
- /**
189
- * A reference to the top level project object.
190
- */
191
- project;
192
- /**
193
- * Checks whether the input is a definition of a project item.
194
- */
195
- static isProjectItem(input) {
196
- const typed = input;
197
- if (!input || ![AppProjectFolderKind, AppProjectRequestKind, EnvironmentKind].includes(typed.kind)) {
198
- return false;
199
- }
200
- return true;
201
- }
202
- /**
203
- * @return An instance that represents a request item
204
- */
205
- static projectRequest(project, key) {
206
- const item = new AppProjectItem(project, {
207
- kind: AppProjectRequestKind,
208
- key,
209
- });
210
- return item;
211
- }
212
- /**
213
- * @return An instance that represents a folder item
214
- */
215
- static projectFolder(project, key) {
216
- const item = new AppProjectItem(project, {
217
- kind: AppProjectFolderKind,
218
- key,
219
- });
220
- return item;
221
- }
222
- /**
223
- * @return An instance that represents an environment item
224
- */
225
- static projectEnvironment(project, key) {
226
- const item = new AppProjectItem(project, {
227
- kind: EnvironmentKind,
228
- key,
229
- });
230
- return item;
231
- }
232
- /**
233
- * @param project The top-most project.
234
- * @param input The project item definition used to restore the state.
235
- */
236
- constructor(project, input) {
237
- this.project = project;
238
- let init;
239
- if (typeof input === 'string') {
240
- if (input === 'http-request') {
241
- init = {
242
- kind: AppProjectRequestKind,
243
- key: '',
244
- };
245
- }
246
- else if (input === 'folder') {
247
- init = {
248
- kind: AppProjectFolderKind,
249
- key: '',
250
- };
251
- }
252
- else if (input === 'environment') {
253
- init = {
254
- kind: EnvironmentKind,
255
- key: '',
256
- };
257
- }
258
- else {
259
- init = JSON.parse(input);
260
- }
261
- }
262
- else if (typeof input === 'object') {
263
- init = input;
264
- }
265
- else {
266
- throw new Error('Specify the type of the item.');
267
- }
268
- this.new(init);
269
- }
270
- /**
271
- * Creates a new project item clearing anything that is so far defined.
272
- *
273
- * Note, this throws an error when the project item is not a project item.
274
- */
275
- new(init) {
276
- if (!AppProjectItem.isProjectItem(init)) {
277
- throw new Error(`Not a project item.`);
278
- }
279
- const { kind, key } = init;
280
- this.kind = kind;
281
- this.key = key;
282
- }
283
- toJSON() {
284
- const result = {
285
- kind: this.kind,
286
- key: this.key,
287
- };
288
- return result;
289
- }
290
- /**
291
- * @returns The instance of the definition associated with this item.
292
- */
293
- getItem() {
294
- const { project, key, kind } = this;
295
- const { definitions } = project;
296
- if (kind === AppProjectRequestKind) {
297
- return definitions.requests.find(i => i.key === key);
298
- }
299
- if (kind === AppProjectFolderKind) {
300
- return definitions.folders.find(i => i.key === key);
301
- }
302
- if (kind === EnvironmentKind) {
303
- return definitions.environments.find(i => i.key === key);
304
- }
305
- return undefined;
306
- }
307
- /**
308
- * @returns The instance of the ArcProject or a ArcProjectFolder that is a closest parent of this item.
309
- */
310
- getParent() {
311
- const { project, key } = this;
312
- return project.findParent(key);
313
- }
314
- }
315
- export class AppProjectParent {
316
- /**
317
- * The auto-generated key for the folder object.
318
- * For the project root this is the same as the `_id`.
319
- */
320
- kind;
321
- /**
322
- * The key of the project / folder.
323
- */
324
- key = '';
325
- /**
326
- * The basic information about the project / folder.
327
- */
328
- info = Thing.fromName('');
329
- /**
330
- * The list of items in the folder.
331
- * It is an ordered list of requests and folders.
332
- * The actual definition is kept in the root's `definitions`.
333
- */
334
- items = [];
335
- /**
336
- * Timestamp when the project was last updated.
337
- */
338
- updated = Date.now();
339
- /**
340
- * Timestamp when the project was created.
341
- */
342
- created = Date.now();
343
- constructor(kind, input) {
344
- this.kind = kind;
345
- let init;
346
- if (typeof input === 'string') {
347
- init = JSON.parse(input);
348
- }
349
- else if (typeof input === 'object') {
350
- init = input;
351
- }
352
- else {
353
- const now = Date.now();
354
- init = {
355
- kind,
356
- created: now,
357
- updated: now,
358
- items: [],
359
- key: v4(),
360
- info: Thing.fromName('New folder').toJSON(),
361
- };
362
- }
363
- this.new(init);
364
- }
365
- /**
366
- * Creates a new project folder clearing anything that is so far defined.
367
- *
368
- * Note, this throws an error when the project folder is not a project folder.
369
- */
370
- new(init) {
371
- const { key = v4(), created = Date.now(), updated = Date.now(), items, info, kind } = init;
372
- this.kind = kind;
373
- this.key = key;
374
- this.created = created;
375
- this.updated = updated;
376
- this.setItems(items);
377
- if (info) {
378
- this.info = new Thing(info);
379
- }
380
- else {
381
- this.info = Thing.fromName('New folder');
382
- }
383
- }
384
- /**
385
- * Lists items (not the actual definitions!) that are folders.
386
- */
387
- listFolderItems() {
388
- const { items = [] } = this;
389
- return items.filter(i => i.kind === AppProjectFolderKind);
390
- }
391
- /**
392
- * Lists items (not the actual definitions!) that are requests.
393
- */
394
- listRequestItems() {
395
- const { items = [] } = this;
396
- return items.filter(i => i.kind === AppProjectRequestKind);
397
- }
398
- }
399
- export class AppProjectFolder extends AppProjectParent {
400
- project;
401
- /**
402
- * Creates a new ArcProjectFolder object from a name.
403
- * @param project The top-most project.
404
- * @param name The name to set.
405
- */
406
- static fromName(project, name = 'New folder') {
407
- const now = Date.now();
408
- const key = v4();
409
- const info = Thing.fromName(name);
410
- const definition = new AppProjectFolder(project, {
411
- key,
412
- created: now,
413
- updated: now,
414
- items: [],
415
- kind: AppProjectFolderKind,
416
- info: info.toJSON(),
417
- });
418
- return definition;
419
- }
420
- constructor(project, input) {
421
- super(AppProjectFolderKind, input);
422
- this.project = project;
423
- let init;
424
- if (typeof input === 'string') {
425
- init = JSON.parse(input);
426
- }
427
- else if (typeof input === 'object') {
428
- init = input;
429
- }
430
- if (init) {
431
- this.key = init.key || v4();
432
- }
433
- this.kind = AppProjectFolderKind;
434
- }
435
- toJSON() {
436
- const result = {
437
- kind: AppProjectFolderKind,
438
- info: this.info.toJSON(),
439
- key: this.key,
440
- created: this.created,
441
- updated: this.updated,
442
- items: [],
443
- };
444
- if (Array.isArray(this.items)) {
445
- result.items = this.items.map(i => i.toJSON());
446
- }
447
- return result;
448
- }
449
- setItems(items) {
450
- if (Array.isArray(items)) {
451
- this.items = items.map(i => new AppProjectItem(this.project, i));
452
- }
453
- else {
454
- this.items = [];
455
- }
456
- }
457
- /**
458
- * Appends a new folder to the folder. It updates the project to add the request definition.
459
- * @param name The name to set. Optional.
460
- * @returns The key of newly inserted folder.
461
- */
462
- addFolder(name) {
463
- return this.project.addFolder(name, { parent: this.key });
464
- }
465
- /**
466
- * Appends a new request to the folder. It updates the project to add the request definition.
467
- * @param request The request to append to the folder.
468
- * @returns The key of newly inserted request.
469
- */
470
- addRequest(request, opts = {}) {
471
- const addOptions = { parent: this.key, ...opts };
472
- if (typeof request === 'string') {
473
- return this.project.addRequest(request, addOptions);
474
- }
475
- return this.project.addRequest(request, addOptions);
476
- }
477
- /**
478
- * @returns The instance of the ArcProject or a ArcProjectFolder that is a closes parent of this instance.
479
- */
480
- getParent() {
481
- const { project, key } = this;
482
- return project.findParent(key);
483
- }
484
- /**
485
- * @returns A reference to the parent folder or the top-level HTTP project.
486
- */
487
- getProject() {
488
- return this.project;
489
- }
490
- /**
491
- * Removes this folder from the project.
492
- */
493
- remove() {
494
- this.project.removeFolder(this.key);
495
- }
496
- /**
497
- * Makes a copy of this folder.
498
- * By default it attaches the copied folder to the same parent.
499
- * It also, by default, copies requests declared in this folder.
500
- *
501
- * Use the options dictionary to control these behaviors.
502
- *
503
- * @param opts Cloning options
504
- */
505
- clone(opts = {}) {
506
- const { targetProject = this.project, targetFolder } = opts;
507
- const copy = new AppProjectFolder(targetProject, this.toJSON());
508
- copy.key = v4();
509
- const extProject = targetProject !== this.project;
510
- if (extProject) {
511
- if (targetFolder) {
512
- const parent = targetProject.findFolder(targetFolder, { keyOnly: true });
513
- if (!parent) {
514
- throw new Error(`The target project does not contain the folder ${targetFolder}`);
515
- }
516
- parent.addFolder(copy);
517
- }
518
- else {
519
- targetProject.addFolder(copy);
520
- }
521
- }
522
- else {
523
- const parent = targetFolder ? this.project.findFolder(targetFolder, { keyOnly: true }) : this.getParent();
524
- if (parent) {
525
- parent.addFolder(copy);
526
- }
527
- else {
528
- throw new Error(`Unable to locate a parent of the folder.`);
529
- }
530
- }
531
- // removes all items. Depending on the passed option we re-add them next.
532
- copy.items = [];
533
- if (!opts.withoutRequests) {
534
- this.cloneRequests(copy, this.project);
535
- }
536
- if (!opts.withoutFolders) {
537
- this.cloneSubFolders(copy, this.project, !opts.withoutRequests);
538
- }
539
- return copy;
540
- }
541
- /**
542
- * Clones the current requests to the target folder.
543
- *
544
- * @param folder The target folder into which to put the requests. The folder has to have the target project attached to it.
545
- * @param project The originating project where the definitions are stored
546
- */
547
- cloneRequests(folder, project) {
548
- const requests = this.items.filter(i => i.kind === AppProjectRequestKind);
549
- requests.forEach(r => {
550
- const request = project.findRequest(r.key, { keyOnly: true });
551
- if (!request) {
552
- // Should we throw an error here?
553
- // CONS:
554
- // - 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.
555
- // - Ignoring this would allow us to make a copy that is error free.
556
- // - The error may occur in a situation when the user does not expect it (giving the nature of the error)
557
- // Pros:
558
- // - There's an inconsistency in the project definition that should be reported back to the UI for the user to inspect
559
- return;
560
- }
561
- const copy = request.clone({ withoutAttach: true });
562
- copy.project = folder.getProject();
563
- folder.addRequest(copy);
564
- });
565
- }
566
- /**
567
- * Clones the sub-folders to the target folder.
568
- *
569
- * @param folder The target folder into which to put the sub-folders. The folder has to have the target project attached to it.
570
- * @param project The originating project where the definitions are stored
571
- * @param withRequests Whether to clone requests with the folder.
572
- */
573
- cloneSubFolders(folder, project, withRequests = true) {
574
- const folders = this.items.filter(i => i.kind === AppProjectFolderKind);
575
- folders.forEach(f => {
576
- const definition = project.findFolder(f.key, { keyOnly: true });
577
- if (!definition) {
578
- return;
579
- }
580
- const copy = new AppProjectFolder(folder.getProject(), definition.toJSON());
581
- copy.key = v4();
582
- copy.items = [];
583
- folder.addFolder(copy);
584
- if (withRequests) {
585
- definition.cloneRequests(copy, project);
586
- }
587
- definition.cloneSubFolders(copy, project, withRequests);
588
- });
589
- }
590
- /**
591
- * Lists folders in this folder.
592
- */
593
- listFolders() {
594
- return this.project.listFolders({ folder: this.key });
595
- }
596
- /**
597
- * Lists requests in this folder.
598
- */
599
- listRequests() {
600
- return this.project.listRequests(this.key);
601
- }
602
- /**
603
- * Adds a request to the project that has been created for a previous version of ARC.
604
- *
605
- * @param legacy The legacy request definition.
606
- * @returns The created project request.
607
- */
608
- async addLegacyRequest(legacy) {
609
- const request = await Request.fromLegacy(legacy);
610
- const projectRequest = AppProjectRequest.fromRequest(request.toJSON(), this.project);
611
- return this.addRequest(projectRequest);
612
- }
613
- /**
614
- * Adds an environment to the project.
615
- * @returns The same or created environment.
616
- */
617
- addEnvironment(env, opts = {}) {
618
- const newOptions = { ...opts, parent: this.key };
619
- return this.project.addEnvironment(env, newOptions);
620
- }
621
- /**
622
- * @param key The environment key to read.
623
- */
624
- getEnvironment(key) {
625
- return this.project.getEnvironment(key, { parent: this.key });
626
- }
627
- /**
628
- * Removes an environment from the folder or a sub-folder.
629
- *
630
- * @param key the key of the environment to remove
631
- * @returns The removed environment, if any.
632
- */
633
- removeEnvironment(key) {
634
- return this.project.removeEnvironment(key, { parent: this.key });
635
- }
636
- /**
637
- * This is a link to the `getEnvironments()`. The difference is that on the
638
- * project level it won't return environments defined with the class initialization.
639
- */
640
- listEnvironments() {
641
- return this.project.listEnvironments({ parent: this.key });
642
- }
643
- /**
644
- * @returns The list of environments defined in this folder
645
- */
646
- getEnvironments() {
647
- return this.project.getEnvironments({ parent: this.key });
648
- }
649
- }
650
- /**
651
- * An app project is similar to the HttpProject but stored for a single application
652
- * and has no sharing options. The AppProject can be upgraded to an HttpProject.
653
- */
654
- export class AppProject extends AppProjectParent {
655
- /**
656
- * Timestamp when the project was last updated.
657
- */
658
- updated = Date.now();
659
- /**
660
- * Timestamp when the project was created.
661
- */
662
- created = Date.now();
663
- definitions;
664
- /**
665
- * Creates a new ARC project from a name.
666
- * @param name The name to set.
667
- */
668
- static fromName(name) {
669
- const project = new AppProject();
670
- const info = Thing.fromName(name);
671
- project.info = info;
672
- return project;
673
- }
674
- /**
675
- * Creates an HTTP project instance from ARC's legacy project definition.
676
- *
677
- * Note, the `requests` should be processed and the payload restored to it's original value.
678
- */
679
- static async fromLegacyProject(project, requests) {
680
- const { name = 'Unnamed project', description, requests: ids } = project;
681
- const result = AppProject.fromName(name);
682
- if (project._id) {
683
- result.key = project._id;
684
- }
685
- if (description) {
686
- result.info.description = description;
687
- }
688
- if (Array.isArray(ids) && ids.length) {
689
- const promises = ids.map(async (id) => {
690
- const old = requests.find((item) => item._id === id);
691
- if (!old) {
692
- return;
693
- }
694
- const request = await Request.fromLegacy(old);
695
- const projectRequest = AppProjectRequest.fromRequest(request.toJSON(), result);
696
- if (old._id) {
697
- projectRequest.key = old._id;
698
- }
699
- result.addRequest(projectRequest);
700
- });
701
- await Promise.allSettled(promises);
702
- }
703
- return result;
704
- }
705
- constructor(input) {
706
- super(AppProjectKind);
707
- this.definitions = {
708
- folders: [],
709
- requests: [],
710
- environments: [],
711
- certificates: [],
712
- };
713
- let init;
714
- if (typeof input === 'string') {
715
- init = JSON.parse(input);
716
- }
717
- else if (typeof input === 'object') {
718
- init = input;
719
- if (!init.kind) {
720
- init.kind = AppProjectKind;
721
- }
722
- }
723
- else {
724
- const now = Date.now();
725
- init = {
726
- kind: AppProjectKind,
727
- key: v4(),
728
- definitions: {},
729
- items: [],
730
- info: Thing.fromName('').toJSON(),
731
- created: now,
732
- updated: now,
733
- };
734
- }
735
- this.new(init);
736
- }
737
- new(init) {
738
- if (!init || !init.items) {
739
- throw new Error(`Not a project.`);
740
- }
741
- const { key = v4(), definitions = {}, items, info, created = Date.now(), updated = Date.now() } = init;
742
- this.key = key;
743
- this.created = created;
744
- this.updated = updated;
745
- if (info) {
746
- this.info = new Thing(info);
747
- }
748
- else {
749
- this.info = Thing.fromName('');
750
- }
751
- this.setItems(items);
752
- this.definitions = {
753
- folders: [],
754
- requests: [],
755
- environments: [],
756
- certificates: [],
757
- };
758
- if (Array.isArray(definitions.requests)) {
759
- this.definitions.requests = definitions.requests.map(i => {
760
- const instance = new AppProjectRequest(this, i);
761
- return instance;
762
- });
763
- }
764
- if (Array.isArray(definitions.folders)) {
765
- this.definitions.folders = definitions.folders.map(i => {
766
- const instance = new AppProjectFolder(this, i);
767
- return instance;
768
- });
769
- }
770
- if (Array.isArray(definitions.environments)) {
771
- this.definitions.environments = definitions.environments.map(i => new Environment(i));
772
- }
773
- if (Array.isArray(definitions.certificates)) {
774
- this.definitions.certificates = definitions.certificates.map(i => new Certificate(i));
775
- }
776
- }
777
- toJSON() {
778
- const result = {
779
- kind: AppProjectKind,
780
- key: this.key,
781
- definitions: {},
782
- items: [],
783
- info: this.info.toJSON(),
784
- created: this.created,
785
- updated: this.updated,
786
- };
787
- if (Array.isArray(this.definitions.requests) && this.definitions.requests.length) {
788
- result.definitions.requests = this.definitions.requests.map(i => i.toJSON());
789
- }
790
- if (Array.isArray(this.definitions.folders) && this.definitions.folders.length) {
791
- result.definitions.folders = this.definitions.folders.map(i => i.toJSON());
792
- }
793
- if (Array.isArray(this.definitions.environments) && this.definitions.environments.length) {
794
- result.definitions.environments = this.definitions.environments.map(i => i.toJSON());
795
- }
796
- if (Array.isArray(this.definitions.certificates) && this.definitions.certificates.length) {
797
- result.definitions.certificates = this.definitions.certificates.map(i => i.toJSON());
798
- }
799
- if (Array.isArray(this.items) && this.items.length) {
800
- result.items = this.items.map(i => i.toJSON());
801
- }
802
- return result;
803
- }
804
- setItems(items) {
805
- if (Array.isArray(items)) {
806
- this.items = items.map(i => new AppProjectItem(this, i));
807
- }
808
- else {
809
- this.items = [];
810
- }
811
- }
812
- /**
813
- * Finds a parent of a definition.
814
- *
815
- * @param key The key of the definition.
816
- * @returns The parent or undefine when not found.
817
- */
818
- findParent(key) {
819
- const { definitions, items = [] } = this;
820
- const projectItemsIndex = items.findIndex(i => i.key === key);
821
- if (projectItemsIndex > -1) {
822
- return this;
823
- }
824
- const definition = definitions.folders.find(i => i.items.some(item => item.key === key));
825
- if (definition) {
826
- return definition;
827
- }
828
- return undefined;
829
- }
830
- /**
831
- * Appends a new folder to the project or a sub-folder.
832
- *
833
- * Passing the folder schema as the fist argument is primarily used to insert a folder on the client side
834
- * after a folder was created in the store.
835
- *
836
- * @param init The name or a folder schema. When not set a default name is assumed.
837
- * @param opts Folder create options.
838
- * @returns The newly inserted folder. If the folder already existed it returns its instance.
839
- */
840
- addFolder(init = 'New Folder', opts = {}) {
841
- if (!Array.isArray(this.items)) {
842
- this.items = [];
843
- }
844
- if (!Array.isArray(this.definitions.folders)) {
845
- this.definitions.folders = [];
846
- }
847
- const { skipExisting, parent } = opts;
848
- let root;
849
- if (parent) {
850
- const rootCandidate = this.findFolder(parent);
851
- if (!rootCandidate) {
852
- throw new Error(`Unable to find the parent folder ${parent}`);
853
- }
854
- root = rootCandidate;
855
- }
856
- else {
857
- root = this;
858
- }
859
- let definition;
860
- if (typeof init === 'string') {
861
- definition = AppProjectFolder.fromName(this, init);
862
- }
863
- else if (init instanceof AppProjectFolder) {
864
- definition = init;
865
- }
866
- else {
867
- definition = new AppProjectFolder(this, init);
868
- }
869
- if (skipExisting) {
870
- const folders = root.listFolderItems();
871
- for (const item of folders) {
872
- const existing = this.findFolder(item.key, { keyOnly: true });
873
- if (existing && existing.info.name === definition.info.name) {
874
- return existing;
875
- }
876
- }
877
- }
878
- this.definitions.folders.push(definition);
879
- const item = AppProjectItem.projectFolder(this, definition.key);
880
- if (!Array.isArray(root.items)) {
881
- root.items = [];
882
- }
883
- if (typeof opts.index === 'number') {
884
- root.items.splice(opts.index, 0, item);
885
- }
886
- else {
887
- root.items.push(item);
888
- }
889
- return definition;
890
- }
891
- /**
892
- * Searches for a folder in the structure.
893
- *
894
- * @param nameOrKey The name or the key of the folder.
895
- * @param opts Optional search options.
896
- * @returns Found project folder or undefined.
897
- */
898
- findFolder(nameOrKey, opts = {}) {
899
- const { definitions } = this;
900
- const item = definitions.folders.find((i) => {
901
- if (i.kind !== AppProjectFolderKind) {
902
- return false;
903
- }
904
- const folder = i;
905
- if (folder.key === nameOrKey) {
906
- return true;
907
- }
908
- if (opts.keyOnly) {
909
- return false;
910
- }
911
- return !!folder.info && folder.info.name === nameOrKey;
912
- });
913
- if (item) {
914
- return item;
915
- }
916
- return undefined;
917
- }
918
- /**
919
- * Removes a folder from the project.
920
- * @param key The folder key. It ignores the name when searching to the folder to avoid ambiguity.
921
- * @param opts Folder remove options.
922
- * @returns The removed folder definition or undefined when not removed.
923
- */
924
- removeFolder(key, opts = {}) {
925
- const { definitions } = this;
926
- const folder = this.findFolder(key, { keyOnly: true });
927
- if (!folder) {
928
- if (opts.safe) {
929
- return undefined;
930
- }
931
- throw new Error(`Unable to find the folder ${key}`);
932
- }
933
- const parent = this.findParent(key);
934
- if (!parent) {
935
- if (opts.safe) {
936
- return undefined;
937
- }
938
- throw new Error(`Unable to find a parent of the folder ${key}`);
939
- }
940
- const requests = folder.listRequests();
941
- requests.forEach(r => r.remove());
942
- const folders = folder.listFolders();
943
- folders.forEach(f => f.remove());
944
- const itemIndex = parent.items.findIndex(i => i.key === key);
945
- const definitionIndex = definitions.folders.findIndex(i => i.key === key);
946
- definitions.folders.splice(definitionIndex, 1);
947
- if (itemIndex >= 0) {
948
- parent.items.splice(itemIndex, 1);
949
- }
950
- return folder;
951
- }
952
- /**
953
- * Moves a folder between folders and the project or between items inside a folder or a project
954
- *
955
- * Note, when the `parent` option is not specified it moved the folder to the project's root.
956
- *
957
- * @param key The key of the request to move.
958
- * @param opts The moving options.
959
- */
960
- moveFolder(key, opts = {}) {
961
- const { index, parent } = opts;
962
- const movedFolder = this.findFolder(key);
963
- if (!movedFolder) {
964
- throw new Error(`Unable to locate the folder ${key}`);
965
- }
966
- const parentFolder = this.findParent(key);
967
- if (!parentFolder) {
968
- throw new Error(`Unable to locate a parent of the folder ${key}`);
969
- }
970
- if (parent) {
971
- // check if moving a folder into another folder that is a child of the moved folder.
972
- if (this.hasChild(parent, key)) {
973
- throw new RangeError(`Unable to move a folder to its child.`);
974
- }
975
- }
976
- const target = parent ? this.findFolder(parent) : this;
977
- if (!target) {
978
- throw new Error(`Unable to locate the new parent folder ${parent}`);
979
- }
980
- const hasIndex = typeof index === 'number';
981
- if (hasIndex) {
982
- // comparing to the `.length` and not `.length - 1` in case we are adding at the end.
983
- const maxIndex = Math.max(target.items.length, 0);
984
- if (index > maxIndex) {
985
- throw new RangeError(`Index out of bounds. Maximum index is ${maxIndex}.`);
986
- }
987
- }
988
- const itemIndex = parentFolder.items.findIndex(i => i.key === key);
989
- const item = parentFolder.items.splice(itemIndex, 1)[0];
990
- if (hasIndex && target.items.length > index) {
991
- target.items.splice(index, 0, item);
992
- }
993
- else {
994
- target.items.push(item);
995
- }
996
- }
997
- /**
998
- * Checks whether a folder has a child (anywhere down the structure).
999
- *
1000
- * @param child The **key** of the child.
1001
- * @param folder The **key** of the folder. When not set it searches from the project root.
1002
- * @returns True when the child is located somewhere down the structure.
1003
- */
1004
- hasChild(child, folder) {
1005
- const target = folder ? this.findFolder(folder) : this;
1006
- if (!target) {
1007
- throw new Error(`Unable to locate the folder ${folder}`);
1008
- }
1009
- const { items = [] } = target;
1010
- for (const item of items) {
1011
- if (item.key === child) {
1012
- return true;
1013
- }
1014
- if (item.kind === AppProjectFolderKind) {
1015
- const hasChild = this.hasChild(child, item.key);
1016
- if (hasChild) {
1017
- return true;
1018
- }
1019
- }
1020
- }
1021
- return false;
1022
- }
1023
- /**
1024
- * Adds a request to the project or a sub-folder.
1025
- * @param request The request to add.
1026
- * @param opts Thew request add options.
1027
- * @returns The inserted into the definitions request.
1028
- */
1029
- addRequest(request, opts = {}) {
1030
- if (!Array.isArray(this.definitions.requests)) {
1031
- this.definitions.requests = [];
1032
- }
1033
- // the request can be already added to the project as the same method is used to refresh a request after
1034
- // a store update. From the system perspective it is the same event.
1035
- if (typeof request === 'object' && request.key) {
1036
- const existing = this.definitions.requests.find(i => i.key === request.key);
1037
- if (existing) {
1038
- existing.new(request);
1039
- return existing;
1040
- }
1041
- }
1042
- // if we got here, it means that we are adding a new request object to the project.
1043
- let finalRequest;
1044
- if (typeof request === 'string') {
1045
- finalRequest = AppProjectRequest.fromUrl(request, this);
1046
- }
1047
- else if (request instanceof AppProjectRequest) {
1048
- finalRequest = request;
1049
- finalRequest.project = this;
1050
- }
1051
- else {
1052
- finalRequest = new AppProjectRequest(this, request);
1053
- }
1054
- if (!finalRequest.key) {
1055
- finalRequest.key = v4();
1056
- }
1057
- let root;
1058
- if (opts.parent) {
1059
- const rootCandidate = this.findFolder(opts.parent);
1060
- if (!rootCandidate) {
1061
- throw new Error(`Unable to find the parent folder ${opts.parent}.`);
1062
- }
1063
- root = rootCandidate;
1064
- }
1065
- else {
1066
- root = this;
1067
- }
1068
- if (!Array.isArray(root.items)) {
1069
- root.items = [];
1070
- }
1071
- if (typeof opts.index === 'number') {
1072
- const maxIndex = Math.max(root.items.length - 1, 0);
1073
- if (opts.index > maxIndex) {
1074
- throw new RangeError(`Index out of bounds. Maximum index is ${maxIndex}.`);
1075
- }
1076
- }
1077
- this.definitions.requests.push(finalRequest);
1078
- const item = AppProjectItem.projectRequest(this, finalRequest.key);
1079
- if (typeof opts.index === 'number') {
1080
- root.items.splice(opts.index, 0, item);
1081
- }
1082
- else {
1083
- root.items.push(item);
1084
- }
1085
- return finalRequest;
1086
- }
1087
- /**
1088
- * Adds a request to the project that has been created for a previous version of ARC.
1089
- *
1090
- * @param legacy The legacy request definition.
1091
- * @returns The created project request.
1092
- */
1093
- async addLegacyRequest(legacy) {
1094
- const request = await Request.fromLegacy(legacy);
1095
- const projectRequest = AppProjectRequest.fromRequest(request.toJSON(), this);
1096
- return this.addRequest(projectRequest);
1097
- }
1098
- /**
1099
- * Searches for a request in the project.
1100
- *
1101
- * @param nameOrKey The name or the key of the request.
1102
- * @param opts Optional search options.
1103
- * @returns Found project request or undefined.
1104
- */
1105
- findRequest(nameOrKey, opts = {}) {
1106
- const { definitions } = this;
1107
- const item = definitions.requests.find((request) => {
1108
- if (request.key === nameOrKey) {
1109
- return true;
1110
- }
1111
- if (opts.keyOnly) {
1112
- return false;
1113
- }
1114
- return !!request.info && request.info.name === nameOrKey;
1115
- });
1116
- if (item) {
1117
- return item;
1118
- }
1119
- return undefined;
1120
- }
1121
- /**
1122
- * Removes a request from the project.
1123
- *
1124
- * @param key The request key. It ignores the name when searching to the request to avoid ambiguity.
1125
- * @param opts Request remove options.
1126
- * @returns The removed request definition or undefined when not removed.
1127
- */
1128
- removeRequest(key, opts = {}) {
1129
- const { definitions } = this;
1130
- const request = this.findRequest(key, { keyOnly: true });
1131
- if (!request) {
1132
- if (opts.safe) {
1133
- return undefined;
1134
- }
1135
- throw new Error(`Unable to find the request ${key}`);
1136
- }
1137
- const parent = this.findParent(key);
1138
- if (!parent) {
1139
- if (opts.safe) {
1140
- return undefined;
1141
- }
1142
- throw new Error(`Unable to find a parent of the request ${key}`);
1143
- }
1144
- const itemIndex = parent.items.findIndex(i => i.key === key);
1145
- const definitionIndex = definitions.requests.findIndex(i => i.key === key);
1146
- definitions.requests.splice(definitionIndex, 1);
1147
- if (itemIndex >= 0) {
1148
- parent.items.splice(itemIndex, 1);
1149
- }
1150
- return request;
1151
- }
1152
- /**
1153
- * Moves a request between folders and the project or between items inside a folder or a project.
1154
- *
1155
- * Note, when the `parent` option is not specified it moved the request to the project's root.
1156
- *
1157
- * @param key The key of the request to move.
1158
- * @param opts The moving options.
1159
- */
1160
- moveRequest(key, opts = {}) {
1161
- const { index, parent } = opts;
1162
- const request = this.findRequest(key);
1163
- if (!request) {
1164
- throw new Error(`Unable to locate the request ${key}`);
1165
- }
1166
- const parentFolder = this.findParent(key);
1167
- if (!parentFolder) {
1168
- throw new Error(`Unable to locate a parent of the request ${key}`);
1169
- }
1170
- const target = parent ? this.findFolder(parent) : this;
1171
- if (!target) {
1172
- throw new Error(`Unable to locate the new parent folder ${parent}`);
1173
- }
1174
- const hasIndex = typeof index === 'number';
1175
- if (hasIndex) {
1176
- // comparing to the `.length` and not `.length - 1` in case we are adding at the end.
1177
- const maxIndex = Math.max(target.items.length, 0);
1178
- if (index > maxIndex) {
1179
- throw new RangeError(`Index out of bounds. Maximum index is ${maxIndex}.`);
1180
- }
1181
- }
1182
- const itemIndex = parentFolder.items.findIndex(i => i.key === key);
1183
- const item = parentFolder.items.splice(itemIndex, 1)[0];
1184
- if (hasIndex && target.items.length > index) {
1185
- target.items.splice(index, 0, item);
1186
- }
1187
- else {
1188
- target.items.push(item);
1189
- }
1190
- }
1191
- /**
1192
- * Lists items (not the actual definitions!) that are folders.
1193
- */
1194
- listFolderItems() {
1195
- const { items = [] } = this;
1196
- return items.filter(i => i.kind === AppProjectFolderKind);
1197
- }
1198
- /**
1199
- * Lists items (not the actual definitions!) that are requests.
1200
- */
1201
- listRequestItems() {
1202
- const { items = [] } = this;
1203
- return items.filter(i => i.kind === AppProjectRequestKind);
1204
- }
1205
- /**
1206
- * Lists folders from the project or a sub-folder.
1207
- * @param opts Folder listing options.
1208
- */
1209
- listFolders(opts = {}) {
1210
- let root;
1211
- if (opts.folder) {
1212
- const parent = this.findFolder(opts.folder);
1213
- if (!parent) {
1214
- throw new Error(`Unable to find the folder ${opts.folder}.`);
1215
- }
1216
- root = parent;
1217
- }
1218
- else {
1219
- root = this;
1220
- }
1221
- const items = root.listFolderItems();
1222
- const result = [];
1223
- const { definitions } = this;
1224
- items.forEach((i) => {
1225
- const definition = definitions.folders.find(d => i.key === d.key);
1226
- if (definition) {
1227
- result.push(definition);
1228
- }
1229
- });
1230
- return result;
1231
- }
1232
- /**
1233
- * Lists requests in this project or a sub-folder.
1234
- * @param folder The optional folder name or the key to list requests for.
1235
- */
1236
- listRequests(folder) {
1237
- let root;
1238
- if (folder) {
1239
- const parent = this.findFolder(folder);
1240
- if (!parent) {
1241
- throw new Error(`Unable to find the folder ${folder}.`);
1242
- }
1243
- root = parent;
1244
- }
1245
- else {
1246
- root = this;
1247
- }
1248
- const items = root.listRequestItems();
1249
- const result = [];
1250
- const { definitions } = this;
1251
- items.forEach((i) => {
1252
- const definition = definitions.requests.find(d => i.key === d.key);
1253
- if (definition) {
1254
- result.push(definition);
1255
- }
1256
- });
1257
- return result;
1258
- }
1259
- /**
1260
- * Lists definitions for the `items` of the project or a folder.
1261
- * @param folder Optionally the folder name to list the definitions for.
1262
- */
1263
- listDefinitions(folder) {
1264
- let root;
1265
- if (folder) {
1266
- const parent = this.findFolder(folder);
1267
- if (!parent) {
1268
- throw new Error(`Unable to find the folder ${folder}`);
1269
- }
1270
- root = parent;
1271
- }
1272
- else {
1273
- root = this;
1274
- }
1275
- const result = [];
1276
- const { items = [] } = root;
1277
- const { definitions } = this;
1278
- items.forEach((item) => {
1279
- let definition;
1280
- if (item.kind === AppProjectFolderKind) {
1281
- definition = definitions.folders.find(d => item.key === d.key);
1282
- }
1283
- else if (item.kind === AppProjectRequestKind) {
1284
- definition = definitions.requests.find(d => item.key === d.key);
1285
- }
1286
- else if (item.kind === EnvironmentKind) {
1287
- definition = definitions.environments.find(d => item.key === d.key);
1288
- }
1289
- if (definition) {
1290
- result.push(definition);
1291
- }
1292
- });
1293
- return result;
1294
- }
1295
- /**
1296
- * @returns On the project level this always returns undefined.
1297
- */
1298
- getParent() {
1299
- return undefined;
1300
- }
1301
- getProject() {
1302
- return this;
1303
- }
1304
- /**
1305
- * Makes a copy of this project.
1306
- */
1307
- clone(opts = {}) {
1308
- const copy = new AppProject(this.toJSON());
1309
- if (!opts.withoutRevalidate) {
1310
- copy.key = v4();
1311
- AppProject.regenerateKeys(copy);
1312
- }
1313
- return copy;
1314
- }
1315
- static clone(project, opts = {}) {
1316
- const obj = new AppProject(project);
1317
- return obj.clone(opts);
1318
- }
1319
- /**
1320
- * Re-generates keys in the project, taking care of the references.
1321
- *
1322
- * Note, this changes the project properties. Make a copy of the project before calling this.
1323
- *
1324
- * @param src The project instance to re-generate keys for.
1325
- */
1326
- static regenerateKeys(src) {
1327
- const { items = [], definitions } = src;
1328
- // create a flat list of all "items" in the project and all folders.
1329
- let flatItems = [...items];
1330
- (definitions.folders || []).forEach((folder) => {
1331
- if (Array.isArray(folder.items) && folder.items.length) {
1332
- flatItems = flatItems.concat(folder.items);
1333
- }
1334
- });
1335
- (definitions.folders || []).forEach((folder) => {
1336
- // if (Array.isArray(folder.environments) && folder.environments.length) {
1337
- // withEnvironments.push(folder);
1338
- // }
1339
- const oldKey = folder.key;
1340
- const indexObject = flatItems.find(i => i.key === oldKey);
1341
- if (!indexObject) {
1342
- return;
1343
- }
1344
- const newKey = v4();
1345
- indexObject.key = newKey;
1346
- folder.key = newKey;
1347
- });
1348
- (definitions.requests || []).forEach((request) => {
1349
- const oldKey = request.key;
1350
- const indexObject = flatItems.find(i => i.key === oldKey);
1351
- if (!indexObject) {
1352
- return;
1353
- }
1354
- const newKey = v4();
1355
- indexObject.key = newKey;
1356
- request.key = newKey;
1357
- });
1358
- (definitions.environments || []).forEach((environment) => {
1359
- const oldKey = environment.key;
1360
- const indexObject = flatItems.find(i => i.key === oldKey);
1361
- if (!indexObject) {
1362
- return;
1363
- }
1364
- const newKey = v4();
1365
- indexObject.key = newKey;
1366
- environment.key = newKey;
1367
- });
1368
- (definitions.certificates || []).forEach((cert) => {
1369
- cert.key = v4();
1370
- });
1371
- }
1372
- /**
1373
- * Adds an environment to the project.
1374
- * @returns The same or created environment.
1375
- */
1376
- addEnvironment(env, opts = {}) {
1377
- const environment = this._createEnv(env);
1378
- const root = this._getRoot(opts);
1379
- const project = this.getProject();
1380
- if (!project.definitions.environments) {
1381
- project.definitions.environments = [];
1382
- }
1383
- project.definitions.environments.push(environment);
1384
- const item = AppProjectItem.projectEnvironment(project, environment.key);
1385
- this._insertItem(item, root, opts);
1386
- return environment;
1387
- }
1388
- _createEnv(env) {
1389
- let finalEnv;
1390
- if (env instanceof Environment) {
1391
- finalEnv = env;
1392
- }
1393
- else if (typeof env === 'string') {
1394
- finalEnv = Environment.fromName(env);
1395
- }
1396
- else {
1397
- finalEnv = new Environment(env);
1398
- }
1399
- if (!finalEnv.key) {
1400
- finalEnv.key = v4();
1401
- }
1402
- return finalEnv;
1403
- }
1404
- /**
1405
- * @param key The environment key to read.
1406
- */
1407
- getEnvironment(key, opts = {}) {
1408
- const root = this._getRoot(opts);
1409
- const item = root.items.find(i => i.key === key);
1410
- if (!item) {
1411
- return undefined;
1412
- }
1413
- const project = this.getProject();
1414
- if (!Array.isArray(project.definitions.environments)) {
1415
- project.definitions.environments = [];
1416
- }
1417
- return project.definitions.environments.find(e => e.key === key);
1418
- }
1419
- /**
1420
- * Removes an environment from the folder or a sub-folder.
1421
- *
1422
- * @param key the key of the environment to remove
1423
- * @returns The removed environment, if any.
1424
- */
1425
- removeEnvironment(key, opts = {}) {
1426
- const root = this._getRoot(opts);
1427
- const itemIndex = root.items.findIndex(i => i.key === key);
1428
- if (itemIndex < 0) {
1429
- return undefined;
1430
- }
1431
- root.items.splice(itemIndex, 1);
1432
- const project = this.getProject();
1433
- if (!Array.isArray(project.definitions.environments)) {
1434
- project.definitions.environments = [];
1435
- }
1436
- const defIndex = project.definitions.environments.findIndex(i => i.key === key);
1437
- if (defIndex < 0) {
1438
- return undefined;
1439
- }
1440
- const env = project.definitions.environments[defIndex];
1441
- project.definitions.environments.splice(defIndex, 1);
1442
- return env;
1443
- }
1444
- /**
1445
- * This method is a link to `listEnvironments()` to be compatible with the HttpProject.
1446
- *
1447
- * @returns The list of environments defined in this folder
1448
- */
1449
- getEnvironments(opts) {
1450
- return this.listEnvironments(opts);
1451
- }
1452
- /**
1453
- * This is a link to the `getEnvironments()`. The difference is that on the
1454
- * project level it won't return environments defined with the class initialization.
1455
- */
1456
- listEnvironments(opts = {}) {
1457
- const root = this._getRoot(opts);
1458
- const items = root.items.filter(i => i.kind === EnvironmentKind).map(i => i.key);
1459
- const project = this.getProject();
1460
- if (!Array.isArray(project.definitions.environments)) {
1461
- project.definitions.environments = [];
1462
- }
1463
- return project.definitions.environments.filter(e => items.includes(e.key));
1464
- }
1465
- /**
1466
- * Reads the list of environments from then selected folder up to the project root.
1467
- * It stops going up in the project structure when selected environment has the `encapsulated`
1468
- * property set to true.
1469
- * The environments are ordered from the top-most level to the selected folder.
1470
- *
1471
- * @param opts The environment read options
1472
- */
1473
- readEnvironments(opts = {}) {
1474
- const result = [];
1475
- const { parent, nameOrKey } = opts;
1476
- const root = parent ? this.findFolder(parent, { keyOnly: true }) : this;
1477
- if (!root) {
1478
- return result;
1479
- }
1480
- let current = root;
1481
- while (current) {
1482
- const environments = current.listEnvironments();
1483
- if (environments.length) {
1484
- const selected = nameOrKey ? environments.find(i => i.key === nameOrKey || i.info.name === nameOrKey) : environments[0];
1485
- if (selected) {
1486
- result.push(selected);
1487
- if (selected.encapsulated) {
1488
- break;
1489
- }
1490
- }
1491
- }
1492
- current = current.getParent();
1493
- }
1494
- return result.reverse();
1495
- }
1496
- /**
1497
- * Finds a definition for an environment regardless of its parent.
1498
- *
1499
- * @param key The Key of the environment to find.
1500
- * @returns The environment definition or undefined if not found.
1501
- */
1502
- findEnvironment(key) {
1503
- return this.definitions.environments.find(i => i.key === key);
1504
- }
1505
- /**
1506
- * Depending on the options returns a project or a folder.
1507
- * It throws when parent folder cannot ber found.
1508
- */
1509
- _getRoot(opts) {
1510
- const project = this.getProject();
1511
- if (opts.parent) {
1512
- const parent = project.findFolder(opts.parent);
1513
- if (!parent) {
1514
- throw new Error(`Unable to find the parent folder ${opts.parent}.`);
1515
- }
1516
- return parent;
1517
- }
1518
- return project;
1519
- }
1520
- _insertItem(item, root, opts) {
1521
- if (!Array.isArray(root.items)) {
1522
- root.items = [];
1523
- }
1524
- if (typeof opts.index === 'number') {
1525
- root.items.splice(opts.index, 0, item);
1526
- }
1527
- else {
1528
- root.items.push(item);
1529
- }
1530
- }
1531
- /**
1532
- * Finds a definition for a certificate.
1533
- *
1534
- * @param key The key of the certificate to find.
1535
- * @returns The certificate definition or undefined if not found.
1536
- */
1537
- findCertificate(key) {
1538
- return this.definitions.certificates.find(i => i.key === key);
1539
- }
1540
- /**
1541
- * Adds a certificate to the project.
1542
- *
1543
- * @param init Either an instance of a certificate or its definition.
1544
- * @returns The inserted certificate.
1545
- */
1546
- addCertificate(init) {
1547
- let finalCert;
1548
- if (init instanceof Certificate) {
1549
- finalCert = init;
1550
- }
1551
- else {
1552
- finalCert = new Certificate(init);
1553
- }
1554
- if (!this.definitions.certificates) {
1555
- this.definitions.certificates = [];
1556
- }
1557
- this.definitions.certificates.push(finalCert);
1558
- return finalCert;
1559
- }
1560
- /**
1561
- * Removes a certificate from the project.
1562
- *
1563
- * @param key the key of the certificate to remove
1564
- * @returns The removed certificate, if any.
1565
- */
1566
- removeCertificate(key) {
1567
- if (!Array.isArray(this.definitions.certificates)) {
1568
- return undefined;
1569
- }
1570
- const defIndex = this.definitions.certificates.findIndex(i => i.key === key);
1571
- if (defIndex < 0) {
1572
- return undefined;
1573
- }
1574
- const cert = this.definitions.certificates[defIndex];
1575
- this.definitions.certificates.splice(defIndex, 1);
1576
- return cert;
1577
- }
1578
- /**
1579
- * Finds the requests that are using the certificate identified by the key.
1580
- *
1581
- * @param key The key of the certificate to find the usage for.
1582
- * @returns The list of requests that use this certificate.
1583
- */
1584
- findCertificateRequests(key) {
1585
- return this.definitions.requests.filter((request) => {
1586
- if (!Array.isArray(request.authorization)) {
1587
- return false;
1588
- }
1589
- const ccAuth = request.authorization.find(auth => auth.type === 'client certificate');
1590
- if (!ccAuth) {
1591
- return false;
1592
- }
1593
- const cnf = ccAuth.config;
1594
- return !!cnf && !!cnf.certificate && cnf.certificate.key === key;
1595
- });
1596
- }
1597
- /**
1598
- * Iterates over requests in the project.
1599
- */
1600
- *requestIterator(opts = {}) {
1601
- const { definitions } = this;
1602
- const { ignore = [], parent, recursive, requests = [] } = opts;
1603
- const root = parent ? this.findFolder(parent) : this;
1604
- if (!root) {
1605
- throw new Error(`The parent folder not found: ${parent}.`);
1606
- }
1607
- const items = root.items;
1608
- if (!items || !items.length) {
1609
- return;
1610
- }
1611
- for (const item of items) {
1612
- if (ignore.includes(item.key)) {
1613
- continue;
1614
- }
1615
- if (item.kind === AppProjectRequestKind) {
1616
- const request = definitions.requests.find(i => i.key === item.key);
1617
- if (!request) {
1618
- continue;
1619
- }
1620
- const name = request.info.name || '';
1621
- if (ignore.includes(name)) {
1622
- continue;
1623
- }
1624
- if (requests.length && !requests.includes(item.key) && !requests.includes(name)) {
1625
- continue;
1626
- }
1627
- yield request;
1628
- }
1629
- else if (recursive && item.kind === AppProjectFolderKind) {
1630
- const folder = definitions.folders.find(i => i.key === item.key);
1631
- if (!folder) {
1632
- continue;
1633
- }
1634
- const name = folder.info.name || '';
1635
- if (ignore.includes(name)) {
1636
- continue;
1637
- }
1638
- const it = this.requestIterator({
1639
- parent: item.key,
1640
- recursive,
1641
- ignore,
1642
- requests,
1643
- });
1644
- for (const request of it) {
1645
- yield request;
1646
- }
1647
- }
1648
- }
1649
- }
1650
- }
1651
- export class AppProjectFile extends File {
1652
- kind = AppProjectFileKind;
1653
- static fromProject(project) {
1654
- let final;
1655
- if (typeof project.toJSON === 'function') {
1656
- final = project.toJSON();
1657
- }
1658
- else {
1659
- final = project;
1660
- }
1661
- const init = {
1662
- kind: AppProjectFileKind,
1663
- key: final.key,
1664
- info: { ...final.info },
1665
- lastModified: { user: '', time: 0, byMe: false },
1666
- owner: DefaultOwner,
1667
- parents: [],
1668
- permissionIds: [],
1669
- permissions: [],
1670
- };
1671
- return new AppProjectFile(init);
1672
- }
1673
- constructor(input) {
1674
- super();
1675
- let init;
1676
- if (typeof input === 'string') {
1677
- init = JSON.parse(input);
1678
- }
1679
- else if (typeof input === 'object') {
1680
- init = input;
1681
- }
1682
- else {
1683
- init = {
1684
- kind: AppProjectFileKind,
1685
- key: v4(),
1686
- info: {
1687
- kind: ThingKind,
1688
- name: '',
1689
- },
1690
- owner: DefaultOwner,
1691
- parents: [],
1692
- permissionIds: [],
1693
- permissions: [],
1694
- lastModified: { user: '', time: 0, byMe: false },
1695
- };
1696
- }
1697
- this.new(init);
1698
- }
1699
- new(init) {
1700
- if (!AppProjectFile.isProject(init)) {
1701
- throw new Error(`Not a project file.`);
1702
- }
1703
- super.new(init);
1704
- this.kind = AppProjectFileKind;
1705
- }
1706
- static isProject(input) {
1707
- const typed = input;
1708
- if (!input || typed.kind !== AppProjectFileKind) {
1709
- return false;
1710
- }
1711
- return true;
1712
- }
1713
- toJSON() {
1714
- const result = {
1715
- ...super.toJSON(),
1716
- kind: AppProjectFileKind,
1717
- };
1718
- return result;
1719
- }
1720
- }
1721
- //# sourceMappingURL=AppProject.js.map