@api-client/core 0.3.3 → 0.3.6

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 (259) hide show
  1. package/build/browser.d.ts +3 -0
  2. package/build/browser.js +9 -0
  3. package/build/browser.js.map +1 -1
  4. package/build/index.d.ts +11 -2
  5. package/build/index.js +20 -2
  6. package/build/index.js.map +1 -1
  7. package/build/src/lib/fs/Fs.d.ts +52 -0
  8. package/build/src/lib/fs/Fs.js +245 -0
  9. package/build/src/lib/fs/Fs.js.map +1 -0
  10. package/build/src/lib/timers/Timers.d.ts +5 -0
  11. package/build/src/lib/timers/Timers.js +10 -0
  12. package/build/src/lib/timers/Timers.js.map +1 -0
  13. package/build/src/mocking/ProjectMock.d.ts +13 -0
  14. package/build/src/mocking/ProjectMock.js +16 -0
  15. package/build/src/mocking/ProjectMock.js.map +1 -0
  16. package/build/src/mocking/lib/Request.d.ts +32 -0
  17. package/build/src/mocking/lib/Request.js +63 -0
  18. package/build/src/mocking/lib/Request.js.map +1 -0
  19. package/build/src/mocking/lib/Response.d.ts +33 -0
  20. package/build/src/mocking/lib/Response.js +79 -0
  21. package/build/src/mocking/lib/Response.js.map +1 -0
  22. package/build/src/models/ErrorResponse.d.ts +5 -4
  23. package/build/src/models/ErrorResponse.js +18 -5
  24. package/build/src/models/ErrorResponse.js.map +1 -1
  25. package/build/src/models/SerializableError.d.ts +30 -0
  26. package/build/src/models/SerializableError.js +63 -0
  27. package/build/src/models/SerializableError.js.map +1 -0
  28. package/build/src/runtime/http-engine/ArcEngine.js +8 -4
  29. package/build/src/runtime/http-engine/ArcEngine.js.map +1 -1
  30. package/build/src/runtime/http-engine/HttpEngine.d.ts +3 -3
  31. package/build/src/runtime/http-engine/HttpEngine.js +3 -3
  32. package/build/src/runtime/http-engine/HttpEngine.js.map +1 -1
  33. package/build/src/runtime/http-engine/NodeEngine.js +9 -4
  34. package/build/src/runtime/http-engine/NodeEngine.js.map +1 -1
  35. package/build/src/runtime/http-engine/NodeEngineDirect.js +8 -2
  36. package/build/src/runtime/http-engine/NodeEngineDirect.js.map +1 -1
  37. package/build/src/runtime/node/BaseRunner.d.ts +21 -0
  38. package/build/src/runtime/node/BaseRunner.js +27 -0
  39. package/build/src/runtime/node/BaseRunner.js.map +1 -0
  40. package/build/src/runtime/node/ProjectParallelRunner.d.ts +81 -0
  41. package/build/src/runtime/node/ProjectParallelRunner.js +173 -0
  42. package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -0
  43. package/build/src/runtime/node/ProjectRequestRunner.d.ts +125 -0
  44. package/build/src/runtime/node/ProjectRequestRunner.js +185 -0
  45. package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -0
  46. package/build/src/runtime/node/ProjectRunner.d.ts +166 -64
  47. package/build/src/runtime/node/ProjectRunner.js +191 -139
  48. package/build/src/runtime/node/ProjectRunner.js.map +1 -1
  49. package/build/src/runtime/node/ProjectRunnerWorker.d.ts +1 -0
  50. package/build/src/runtime/node/ProjectRunnerWorker.js +58 -0
  51. package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -0
  52. package/build/src/runtime/node/ProjectSerialRunner.d.ts +11 -0
  53. package/build/src/runtime/node/ProjectSerialRunner.js +34 -0
  54. package/build/src/runtime/node/ProjectSerialRunner.js.map +1 -0
  55. package/build/src/runtime/reporters/ProjectRunCliReporter.d.ts +7 -0
  56. package/build/src/runtime/reporters/ProjectRunCliReporter.js +73 -0
  57. package/build/src/runtime/reporters/ProjectRunCliReporter.js.map +1 -0
  58. package/build/src/runtime/reporters/Reporter.d.ts +62 -0
  59. package/build/src/runtime/reporters/Reporter.js +98 -0
  60. package/build/src/runtime/reporters/Reporter.js.map +1 -0
  61. package/build/src/testing/TestCliHelper.d.ts +23 -0
  62. package/build/src/testing/TestCliHelper.js +71 -0
  63. package/build/src/testing/TestCliHelper.js.map +1 -0
  64. package/build/src/testing/getPort.d.ts +52 -0
  65. package/build/src/testing/getPort.js +169 -0
  66. package/build/src/testing/getPort.js.map +1 -0
  67. package/package.json +2 -1
  68. package/src/data/DataReader.ts +11 -0
  69. package/src/data/DataUtils.ts +108 -0
  70. package/src/data/JmesparthReader.ts +26 -0
  71. package/src/data/Json2Xml.ts +190 -0
  72. package/src/data/JsonReader.ts +41 -0
  73. package/src/data/PayloadPointer.ts +48 -0
  74. package/src/data/RequestDataExtractor.ts +133 -0
  75. package/src/data/UrlEncodedReader.ts +20 -0
  76. package/src/data/XmlReader.ts +103 -0
  77. package/src/events/BaseEvents.ts +259 -0
  78. package/src/events/CustomEvent.ts +27 -0
  79. package/src/events/EventTypes.ts +19 -0
  80. package/src/events/Events.ts +19 -0
  81. package/src/events/authorization/AuthorizationEventTypes.ts +22 -0
  82. package/src/events/authorization/AuthorizationEvents.ts +61 -0
  83. package/src/events/cookies/CookieEventTypes.ts +13 -0
  84. package/src/events/cookies/CookieEvents.ts +157 -0
  85. package/src/events/encryption/EncryptionEventTypes.ts +4 -0
  86. package/src/events/encryption/EncryptionEvents.ts +51 -0
  87. package/src/events/environment/EnvironmentEventTypes.ts +3 -0
  88. package/src/events/environment/EnvironmentEvents.ts +24 -0
  89. package/src/events/models/ClientCertificateEvents.ts +87 -0
  90. package/src/events/models/ModelEventTypes.ts +47 -0
  91. package/src/events/models/ModelEvents.ts +7 -0
  92. package/src/events/models/ProjectEvents.ts +331 -0
  93. package/src/events/process/ProcessEventTypes.ts +5 -0
  94. package/src/events/process/ProcessEvents.ts +76 -0
  95. package/src/events/readme.md +22 -0
  96. package/src/events/reporting/ReportingEventTypes.ts +3 -0
  97. package/src/events/reporting/ReportingEvents.ts +28 -0
  98. package/src/events/telemetry/TelemetryEventTypes.ts +10 -0
  99. package/src/events/telemetry/TelemetryEvents.ts +156 -0
  100. package/src/lib/cookies/Cookie.ts +312 -0
  101. package/src/lib/cookies/Cookies.ts +326 -0
  102. package/src/lib/cookies/Utils.ts +168 -0
  103. package/src/lib/fs/Fs.ts +258 -0
  104. package/src/lib/headers/Headers.ts +219 -0
  105. package/src/lib/logging/DefaultLogger.ts +19 -0
  106. package/src/lib/logging/DummyLogger.ts +21 -0
  107. package/src/lib/logging/Logger.ts +16 -0
  108. package/src/lib/timers/Timers.ts +9 -0
  109. package/src/lib/transformers/PayloadSerializer.ts +332 -0
  110. package/src/lib/transformers/Utils.ts +18 -0
  111. package/src/lib/uuid.ts +40 -0
  112. package/src/mocking/LegacyInterfaces.ts +52 -0
  113. package/src/mocking/LegacyMock.ts +37 -0
  114. package/src/mocking/ProjectMock.ts +20 -0
  115. package/src/mocking/legacy/Authorization.ts +39 -0
  116. package/src/mocking/legacy/Certificates.ts +145 -0
  117. package/src/mocking/legacy/Cookies.ts +51 -0
  118. package/src/mocking/legacy/HostRules.ts +43 -0
  119. package/src/mocking/legacy/Http.ts +236 -0
  120. package/src/mocking/legacy/HttpResponse.ts +106 -0
  121. package/src/mocking/legacy/RestApi.ts +68 -0
  122. package/src/mocking/legacy/Urls.ts +44 -0
  123. package/src/mocking/legacy/Variables.ts +53 -0
  124. package/src/mocking/lib/Request.ts +85 -0
  125. package/src/mocking/lib/Response.ts +101 -0
  126. package/src/models/ArcResponse.ts +166 -0
  127. package/src/models/Authorization.ts +481 -0
  128. package/src/models/AuthorizationData.ts +60 -0
  129. package/src/models/Backend.ts +107 -0
  130. package/src/models/ClientCertificate.ts +68 -0
  131. package/src/models/Environment.ts +279 -0
  132. package/src/models/ErrorResponse.ts +113 -0
  133. package/src/models/HistoryIndex.ts +76 -0
  134. package/src/models/HistoryRequest.ts +28 -0
  135. package/src/models/HostRule.ts +163 -0
  136. package/src/models/HttpCookie.ts +285 -0
  137. package/src/models/HttpProject.ts +1294 -0
  138. package/src/models/HttpProjectListItem.ts +23 -0
  139. package/src/models/HttpRequest.ts +124 -0
  140. package/src/models/HttpResponse.ts +143 -0
  141. package/src/models/License.ts +113 -0
  142. package/src/models/ProjectDefinitionProperty.ts +40 -0
  143. package/src/models/ProjectFolder.ts +439 -0
  144. package/src/models/ProjectItem.ts +135 -0
  145. package/src/models/ProjectParent.ts +113 -0
  146. package/src/models/ProjectRequest.ts +277 -0
  147. package/src/models/ProjectSchema.ts +202 -0
  148. package/src/models/Property.ts +423 -0
  149. package/src/models/Provider.ts +98 -0
  150. package/src/models/README.md +20 -0
  151. package/src/models/Request.ts +452 -0
  152. package/src/models/RequestActions.ts +163 -0
  153. package/src/models/RequestAuthorization.ts +115 -0
  154. package/src/models/RequestConfig.ts +317 -0
  155. package/src/models/RequestLog.ts +159 -0
  156. package/src/models/RequestTime.ts +108 -0
  157. package/src/models/RequestUiMeta.ts +258 -0
  158. package/src/models/RequestsSize.ts +65 -0
  159. package/src/models/ResponseAuthorization.ts +104 -0
  160. package/src/models/ResponseRedirect.ts +158 -0
  161. package/src/models/RevisionInfo.ts +37 -0
  162. package/src/models/SentRequest.ts +125 -0
  163. package/src/models/SerializableError.ts +80 -0
  164. package/src/models/SerializablePayload.ts +68 -0
  165. package/src/models/Server.ts +153 -0
  166. package/src/models/Thing.ts +110 -0
  167. package/src/models/Url.ts +90 -0
  168. package/src/models/User.ts +120 -0
  169. package/src/models/WebApi.ts +234 -0
  170. package/src/models/WebApiIndex.ts +122 -0
  171. package/src/models/Workspace.ts +182 -0
  172. package/src/models/actions/Action.ts +213 -0
  173. package/src/models/actions/ActionView.ts +40 -0
  174. package/src/models/actions/Condition.ts +207 -0
  175. package/src/models/actions/ConditionView.ts +42 -0
  176. package/src/models/actions/Enums.ts +29 -0
  177. package/src/models/actions/RunnableAction.ts +144 -0
  178. package/src/models/actions/runnable/DeleteCookieAction.ts +113 -0
  179. package/src/models/actions/runnable/Runnable.ts +9 -0
  180. package/src/models/actions/runnable/SetCookieAction.ts +216 -0
  181. package/src/models/actions/runnable/SetVariableAction.ts +81 -0
  182. package/src/models/legacy/DataExport.ts +172 -0
  183. package/src/models/legacy/Normalizer.ts +110 -0
  184. package/src/models/legacy/actions/Actions.ts +269 -0
  185. package/src/models/legacy/authorization/Authorization.ts +572 -0
  186. package/src/models/legacy/models/ApiTypes.ts +202 -0
  187. package/src/models/legacy/models/ArcLegacyProject.ts +39 -0
  188. package/src/models/legacy/models/AuthData.ts +17 -0
  189. package/src/models/legacy/models/ClientCertificate.ts +95 -0
  190. package/src/models/legacy/models/Cookies.ts +52 -0
  191. package/src/models/legacy/models/HostRule.ts +35 -0
  192. package/src/models/legacy/models/RestApi.ts +49 -0
  193. package/src/models/legacy/models/UrlHistory.ts +37 -0
  194. package/src/models/legacy/models/Variable.ts +43 -0
  195. package/src/models/legacy/models/base.d.ts +95 -0
  196. package/src/models/legacy/request/ArcRequest.ts +405 -0
  197. package/src/models/legacy/request/ArcResponse.ts +177 -0
  198. package/src/models/legacy/request/HistoryData.ts +47 -0
  199. package/src/models/legacy/request/Legacy.ts +45 -0
  200. package/src/models/legacy/request/RequestBody.ts +87 -0
  201. package/src/models/transformers/ArcDexieTransformer.ts +323 -0
  202. package/src/models/transformers/ArcLegacyNormalizer.ts +85 -0
  203. package/src/models/transformers/ArcLegacyTransformer.ts +200 -0
  204. package/src/models/transformers/ArcPouchTransformer.ts +184 -0
  205. package/src/models/transformers/BaseTransformer.ts +116 -0
  206. package/src/models/transformers/ImportUtils.ts +141 -0
  207. package/src/models/transformers/LegacyDataExportToApiProject.ts +76 -0
  208. package/src/models/transformers/LegacyExportProcessor.ts +252 -0
  209. package/src/models/transformers/PostmanBackupTransformer.ts +306 -0
  210. package/src/models/transformers/PostmanDataTransformer.ts +50 -0
  211. package/src/models/transformers/PostmanTransformer.ts +106 -0
  212. package/src/models/transformers/PostmanV21Transformer.ts +311 -0
  213. package/src/models/transformers/PostmanV2Transformer.ts +308 -0
  214. package/src/models/transformers/har.ts +865 -0
  215. package/src/runtime/actions/ActionRunner.ts +83 -0
  216. package/src/runtime/actions/ConditionRunner.ts +194 -0
  217. package/src/runtime/actions/RunnableCondition.ts +57 -0
  218. package/src/runtime/actions/runnable/ActionRunnable.ts +19 -0
  219. package/src/runtime/actions/runnable/DeleteCookieRunnable.ts +39 -0
  220. package/src/runtime/actions/runnable/SetCookieRunnable.ts +92 -0
  221. package/src/runtime/actions/runnable/SetVariableRunnable.ts +53 -0
  222. package/src/runtime/http-engine/ArcEngine.ts +1068 -0
  223. package/src/runtime/http-engine/FormData.ts +85 -0
  224. package/src/runtime/http-engine/HttpEngine.ts +874 -0
  225. package/src/runtime/http-engine/HttpErrorCodes.ts +270 -0
  226. package/src/runtime/http-engine/NodeEngine.ts +792 -0
  227. package/src/runtime/http-engine/NodeEngineDirect.ts +482 -0
  228. package/src/runtime/http-engine/PayloadSupport.ts +84 -0
  229. package/src/runtime/http-engine/RequestUtils.ts +164 -0
  230. package/src/runtime/http-engine/ntlm/Des.ts +345 -0
  231. package/src/runtime/http-engine/ntlm/MD4.ts +135 -0
  232. package/src/runtime/http-engine/ntlm/NtlmAuth.ts +186 -0
  233. package/src/runtime/http-engine/ntlm/NtlmMessage.ts +57 -0
  234. package/src/runtime/modules/BasicAuthCache.ts +133 -0
  235. package/src/runtime/modules/ExecutionResponse.ts +4 -0
  236. package/src/runtime/modules/ModulesRegistry.ts +136 -0
  237. package/src/runtime/modules/RequestAuthorization.ts +110 -0
  238. package/src/runtime/modules/RequestCookies.ts +145 -0
  239. package/src/runtime/node/BaseRunner.ts +29 -0
  240. package/src/runtime/node/ProjectParallelRunner.ts +234 -0
  241. package/src/runtime/node/ProjectRequestRunner.ts +281 -0
  242. package/src/runtime/node/ProjectRunner.ts +374 -0
  243. package/src/runtime/node/ProjectRunnerWorker.ts +62 -0
  244. package/src/runtime/node/ProjectSerialRunner.ts +36 -0
  245. package/src/runtime/node/RequestFactory.ts +422 -0
  246. package/src/runtime/node/VariablesStore.ts +25 -0
  247. package/src/runtime/reporters/ProjectRunCliReporter.ts +79 -0
  248. package/src/runtime/reporters/Reporter.ts +142 -0
  249. package/src/runtime/store/StoreSdk.ts +838 -0
  250. package/src/runtime/variables/Cache.ts +53 -0
  251. package/src/runtime/variables/EvalFunctions.ts +132 -0
  252. package/src/runtime/variables/ProjectVariables.ts +6 -0
  253. package/src/runtime/variables/VariablesProcessor.ts +543 -0
  254. package/src/runtime/variables/VariablesTokenizer.ts +55 -0
  255. package/src/testing/TestCliHelper.ts +76 -0
  256. package/src/testing/getPort.ts +212 -0
  257. package/build/src/runtime/http-engine/Errors.d.ts +0 -10
  258. package/build/src/runtime/http-engine/Errors.js +0 -14
  259. package/build/src/runtime/http-engine/Errors.js.map +0 -1
@@ -0,0 +1,439 @@
1
+ import { ProjectParent } from './ProjectParent.js';
2
+ import { IProjectDefinitionProperty } from './ProjectDefinitionProperty.js';
3
+ import { ProjectItem, IProjectItem } from './ProjectItem.js';
4
+ import { ProjectRequest, Kind as ProjectRequestKind, IProjectRequest } from './ProjectRequest.js';
5
+ import { HttpProject } from './HttpProject.js';
6
+ import { IThing, Thing, Kind as ThingKind } from './Thing.js';
7
+ import v4 from '../lib/uuid.js';
8
+
9
+ export const Kind = 'ARC#ProjectFolder';
10
+ export const DefaultFolderName = 'New folder';
11
+
12
+ export interface IFolderAddOptions {
13
+ /**
14
+ * Optionally the position at which to add the request into the list of items.
15
+ */
16
+ index?: number;
17
+ }
18
+
19
+ export interface IFolderCloneOptions {
20
+ /**
21
+ * By default it clones the folder with all requests in it.
22
+ * Set this to `true` to skip copying the requests along with the folder.
23
+ */
24
+ withoutRequests?: boolean;
25
+ /**
26
+ * By default it clones the folder with all folders in it.
27
+ * Set this to `true` to skip copying the folders along with the folder.
28
+ */
29
+ withoutFolders?: boolean;
30
+ /**
31
+ * The target project where to put the copied folder.
32
+ * When the target project is not the same as the source project then the folder
33
+ * is put into the project root rather than the parent folder (as it would when cloning
34
+ * a folder inside the same project).
35
+ */
36
+ targetProject?: HttpProject;
37
+ /**
38
+ * The **key** of the target folder.
39
+ *
40
+ * By default it clones the folder to its parent unless the clone is attached to another project.
41
+ * When the target folder is set then it places the clone under the passed target folder.
42
+ */
43
+ targetFolder?: string;
44
+ }
45
+
46
+ export interface IProjectFolder extends IProjectDefinitionProperty {
47
+ kind: typeof Kind;
48
+ /**
49
+ * The identifier of the folder.
50
+ */
51
+ key: string;
52
+ /**
53
+ * Folder meta
54
+ */
55
+ info: IThing;
56
+ /**
57
+ * The ordered list of HTTP requests / folders in the projects.
58
+ * The UI uses this to manipulate the view without changing the definitions.
59
+ */
60
+ 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
+ /**
67
+ * Timestamp when the folder was last updated.
68
+ */
69
+ updated: number;
70
+ /**
71
+ * Timestamp when the folder was created.
72
+ */
73
+ created: number;
74
+ }
75
+
76
+ /**
77
+ * Represents a folder, a group of requests or other folders, in a folder.
78
+ */
79
+ export class ProjectFolder extends ProjectParent {
80
+ /**
81
+ * The default name of the folder.
82
+ */
83
+ static get defaultName(): string {
84
+ return DefaultFolderName;
85
+ }
86
+
87
+ kind = Kind;
88
+ /**
89
+ * A reference to the top level project object.
90
+ */
91
+ project: HttpProject;
92
+ /**
93
+ * Timestamp when the folder was last updated.
94
+ */
95
+ updated = 0;
96
+ /**
97
+ * Timestamp when the folder was created.
98
+ */
99
+ created = 0;
100
+
101
+ constructor(project: HttpProject, input?: string | IProjectFolder) {
102
+ super();
103
+ this.project = project;
104
+ let init: IProjectFolder;
105
+ if (typeof input === 'string') {
106
+ init = JSON.parse(input);
107
+ } else if (typeof input === 'object') {
108
+ init = input;
109
+ } else {
110
+ const now: number = Date.now();
111
+ init = {
112
+ kind: Kind,
113
+ info: {
114
+ kind: ThingKind,
115
+ name: DefaultFolderName,
116
+ },
117
+ created: now,
118
+ updated: now,
119
+ items: [],
120
+ key: v4(),
121
+ };
122
+ }
123
+ this.new(init);
124
+ }
125
+
126
+ /**
127
+ * Creates a new project folder clearing anything that is so far defined.
128
+ *
129
+ * Note, this throws an error when the project folder is not an ARC project folder.
130
+ */
131
+ new(init: IProjectFolder): void {
132
+ if (!ProjectFolder.isProjectFolder(init)) {
133
+ throw new Error(`Not an ARC project folder.`);
134
+ }
135
+ const { key = v4(), created = Date.now(), updated = Date.now(), items, environments, info } = init;
136
+ this.kind = Kind;
137
+ this.key = key;
138
+ this.created = created;
139
+ this.updated = updated;
140
+ if (Array.isArray(items)) {
141
+ this.items = items.map(i => new ProjectItem(this.project, i));
142
+ } else {
143
+ this.items = [];
144
+ }
145
+ if (info) {
146
+ this.info = new Thing(info);
147
+ } else {
148
+ this.info = new Thing({ kind: ThingKind, name: DefaultFolderName });
149
+ }
150
+ if (Array.isArray(environments)) {
151
+ this.environments = environments;
152
+ } else {
153
+ this.environments = [];
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Checks whether the input is a definition of a project folder.
159
+ */
160
+ static isProjectFolder(input: unknown): boolean {
161
+ const typed = input as IProjectFolder;
162
+ if (!input || typed.kind !== Kind) {
163
+ return false;
164
+ }
165
+ return true;
166
+ }
167
+
168
+ toJSON(): IProjectFolder {
169
+ const result: IProjectFolder = {
170
+ kind: Kind,
171
+ info: this.info.toJSON(),
172
+ key: this.key,
173
+ created: this.created,
174
+ updated: this.updated,
175
+ items: [],
176
+ environments: [],
177
+ };
178
+ if (Array.isArray(this.items)) {
179
+ result.items = this.items.map(i => i.toJSON());
180
+ }
181
+ if (Array.isArray(this.environments)) {
182
+ result.environments = this.environments;
183
+ }
184
+ return result;
185
+ }
186
+
187
+ /**
188
+ * Creates a new ProjectFolder object from a name.
189
+ * @param project The top-most project.
190
+ * @param name The name to set.
191
+ */
192
+ static fromName(project: HttpProject, name = DefaultFolderName): ProjectFolder {
193
+ const now = Date.now();
194
+ const key = v4();
195
+ const info = new Thing({ kind: ThingKind, name });
196
+ const definition = new ProjectFolder(project, {
197
+ key,
198
+ created: now,
199
+ updated: now,
200
+ items: [],
201
+ environments: [],
202
+ kind: Kind,
203
+ info: info.toJSON(),
204
+ });
205
+ return definition;
206
+ }
207
+
208
+ /**
209
+ * Appends an instance of a folder to a project.
210
+ *
211
+ * @param folder The folder to add to this project.
212
+ * @returns The added folder.
213
+ */
214
+ addFolder(folder: ProjectFolder): ProjectFolder;
215
+
216
+ /**
217
+ * Appends new folder to a project from a full folder schema.
218
+ * This is primarily used to insert a folder on the client side
219
+ * after a folder was created in the store.
220
+ *
221
+ * @param folder The folder schema to add to this project.
222
+ * @returns The added folder.
223
+ */
224
+ addFolder(folder: IProjectFolder): ProjectFolder;
225
+
226
+ /**
227
+ * Appends a new folder to the project or a sub-folder.
228
+ *
229
+ * @param name The name to set. Optional.
230
+ * @returns The newly inserted folder. If the folder already existed it returns its instance.
231
+ */
232
+ addFolder(name?: string): ProjectFolder;
233
+
234
+ /**
235
+ * Appends a new folder to the folder. It updates the project to add the request definition.
236
+ * @param name The name to set. Optional.
237
+ * @returns The key of newly inserted folder.
238
+ */
239
+ addFolder(name: string | IProjectFolder | ProjectFolder | undefined): ProjectFolder {
240
+ return this.project.addFolder(name as ProjectFolder, { parent: this.key });
241
+ }
242
+
243
+ /**
244
+ * Adds a request to the project or a sub-folder.
245
+ *
246
+ * @param url The URL of the request.
247
+ * @param opts The request add options.
248
+ * @returns The inserted into the definitions request.
249
+ */
250
+ addRequest(url: string, opts?: IFolderAddOptions): ProjectRequest;
251
+
252
+ /**
253
+ * Adds a request to the project or a sub-folder.
254
+ *
255
+ * @param request The request to add.
256
+ * @param opts The request add options.
257
+ * @returns The inserted into the definitions request.
258
+ */
259
+ addRequest(request: IProjectRequest | ProjectRequest, opts?: IFolderAddOptions): ProjectRequest;
260
+
261
+ /**
262
+ * Appends a new request to the folder. It updates the project to add the request definition.
263
+ * @param request The request to append to the folder.
264
+ * @returns The key of newly inserted request.
265
+ */
266
+ addRequest(request: IProjectRequest | ProjectRequest | string, opts: IFolderAddOptions = {}): ProjectRequest {
267
+ const addOptions = { parent: this.key, ...opts };
268
+ if (typeof request === 'string') {
269
+ return this.project.addRequest(request, addOptions);
270
+ }
271
+ return this.project.addRequest(request, addOptions);
272
+ }
273
+
274
+ /**
275
+ * Lists items (not the actual definitions!) that are folders.
276
+ */
277
+ listFolderItems(): ProjectItem[] {
278
+ const { items = [] } = this;
279
+ return items.filter(i => i.kind === Kind);
280
+ }
281
+
282
+ /**
283
+ * Lists items (not the actual definitions!) that are requests.
284
+ */
285
+ listRequestItems(): ProjectItem[] {
286
+ const { items = [] } = this;
287
+ return items.filter(i => i.kind === ProjectRequestKind);
288
+ }
289
+
290
+ /**
291
+ * Lists folders in this folder.
292
+ */
293
+ listFolders(): ProjectFolder[] {
294
+ return this.project.listFolders({ folder: this.key });
295
+ }
296
+
297
+ /**
298
+ * Lists requests in this folder.
299
+ */
300
+ listRequests(): ProjectRequest[] {
301
+ return this.project.listRequests(this.key);
302
+ }
303
+
304
+ /**
305
+ * The callback called when the object is attached to a parent.
306
+ * This is called when the object is created and inserted to a project or a folder
307
+ * and when the object is moved between folders.
308
+ */
309
+ attachedCallback(): void {
310
+ // ...
311
+ }
312
+
313
+ /**
314
+ * The callback called when the object is detached from its parent.
315
+ * This callback is called when the item is deleted from a folder or a project,
316
+ * or when the item is about to be moved to another folder.
317
+ */
318
+ detachedCallback(): void {
319
+ // ...
320
+ }
321
+
322
+ /**
323
+ * @returns The instance of the HttpProject or a ProjectFolder that is a closes parent of this instance.
324
+ */
325
+ getParent(): ProjectFolder | HttpProject | undefined {
326
+ const { project, key } = this;
327
+ return project.findParent(key);
328
+ }
329
+
330
+ /**
331
+ * @returns A reference to the parent folder or the top-level HTTP project.
332
+ */
333
+ getProject(): HttpProject {
334
+ return this.project;
335
+ }
336
+
337
+ /**
338
+ * Removes this folder from the project.
339
+ */
340
+ remove(): void {
341
+ this.project.removeFolder(this.key);
342
+ }
343
+
344
+ /**
345
+ * Makes a copy of this folder.
346
+ * By default it attaches the copied folder to the same parent.
347
+ * It also, by default, copies requests declared in this folder.
348
+ *
349
+ * Use the options dictionary to control these behaviors.
350
+ *
351
+ * @param opts Cloning options
352
+ */
353
+ clone(opts: IFolderCloneOptions = {}): ProjectFolder {
354
+ const { targetProject=this.project, targetFolder } = opts;
355
+ const copy = new ProjectFolder(targetProject, this.toJSON());
356
+ copy.key = v4();
357
+
358
+ const extProject = targetProject !== this.project;
359
+ if (extProject) {
360
+ if (targetFolder) {
361
+ const parent = targetProject.findFolder(targetFolder, { keyOnly: true });
362
+ if (!parent) {
363
+ throw new Error(`The target project does not contain the folder ${targetFolder}`);
364
+ }
365
+ parent.addFolder(copy);
366
+ } else {
367
+ targetProject.addFolder(copy);
368
+ }
369
+ } else {
370
+ const parent = targetFolder ? this.project.findFolder(targetFolder, { keyOnly: true }) : this.getParent();
371
+ if (parent) {
372
+ parent.addFolder(copy);
373
+ } else {
374
+ throw new Error(`Unable to locate a parent of the folder.`);
375
+ }
376
+ }
377
+ // removes all items. Depending on the passed option we re-add them next.
378
+ copy.items = [];
379
+
380
+ if (!opts.withoutRequests) {
381
+ this.cloneRequests(copy, this.project);
382
+ }
383
+ if (!opts.withoutFolders) {
384
+ this.cloneSubFolders(copy, this.project, !opts.withoutRequests);
385
+ }
386
+ return copy;
387
+ }
388
+
389
+ /**
390
+ * Clones the current requests to the target folder.
391
+ *
392
+ * @param folder The target folder into which to put the requests. The folder has to have the target project attached to it.
393
+ * @param project The originating project where the definitions are stored
394
+ */
395
+ protected cloneRequests(folder: ProjectFolder, project: HttpProject): void {
396
+ const requests = this.items.filter(i => i.kind === ProjectRequestKind);
397
+ requests.forEach(r => {
398
+ const request = project.findRequest(r.key, { keyOnly: true });
399
+ if (!request) {
400
+ // Should we throw an error here?
401
+ // CONS:
402
+ // - 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.
403
+ // - Ignoring this would allow us to make a copy that is error free.
404
+ // - The error may occur in a situation when the user does not expect it (giving the nature of the error)
405
+ // Pros:
406
+ // - There's an inconsistency in the project definition that should be reported back to the UI for the user to inspect
407
+ return;
408
+ }
409
+ const copy = request.clone({ withoutAttach: true });
410
+ copy.project = folder.getProject();
411
+ folder.addRequest(copy);
412
+ });
413
+ }
414
+
415
+ /**
416
+ * Clones the sub-folders to the target folder.
417
+ *
418
+ * @param folder The target folder into which to put the sub-folders. The folder has to have the target project attached to it.
419
+ * @param project The originating project where the definitions are stored
420
+ * @param withRequests Whether to clone requests with the folder.
421
+ */
422
+ protected cloneSubFolders(folder: ProjectFolder, project: HttpProject, withRequests = true): void {
423
+ const folders = this.items.filter(i => i.kind === Kind);
424
+ folders.forEach(f => {
425
+ const definition = project.findFolder(f.key, { keyOnly: true });
426
+ if (!definition) {
427
+ return;
428
+ }
429
+ const copy = new ProjectFolder(folder.getProject(), definition.toJSON());
430
+ copy.key = v4();
431
+ copy.items = [];
432
+ folder.addFolder(copy);
433
+ if (withRequests) {
434
+ definition.cloneRequests(copy, project);
435
+ }
436
+ definition.cloneSubFolders(copy, project, withRequests);
437
+ });
438
+ }
439
+ }
@@ -0,0 +1,135 @@
1
+ import { Kind as ProjectFolderKind, ProjectFolder } from './ProjectFolder.js';
2
+ import { Kind as ProjectRequestKind, ProjectRequest } from './ProjectRequest.js';
3
+ import { HttpProject } from './HttpProject.js';
4
+
5
+ export interface IProjectItem {
6
+ /**
7
+ * The kind of the item.
8
+ */
9
+ kind: typeof ProjectFolderKind | typeof ProjectRequestKind;
10
+ /**
11
+ * The identifier in the `definitions` array of the project.
12
+ */
13
+ key: string;
14
+ }
15
+
16
+ export class ProjectItem {
17
+ /**
18
+ * The kind of the item.
19
+ */
20
+ kind: typeof ProjectFolderKind | typeof ProjectRequestKind = ProjectRequestKind;
21
+ /**
22
+ * The identifier of the object in the `definitions` array of the project.
23
+ */
24
+ key = '';
25
+ /**
26
+ * A reference to the top level project object.
27
+ */
28
+ private project: HttpProject;
29
+
30
+ /**
31
+ * Checks whether the input is a definition of a project item.
32
+ */
33
+ static isProjectItem(input: unknown): boolean {
34
+ const typed = input as IProjectItem;
35
+ if (!input || ![ProjectFolderKind, ProjectRequestKind].includes(typed.kind)) {
36
+ return false;
37
+ }
38
+ return true;
39
+ }
40
+
41
+ /**
42
+ * @return An instance that represents a request object
43
+ */
44
+ static projectRequest(project: HttpProject, key: string) : ProjectItem {
45
+ const item = new ProjectItem(project, {
46
+ kind: ProjectRequestKind,
47
+ key,
48
+ });
49
+ return item;
50
+ }
51
+
52
+ /**
53
+ * @return An instance that represents a folder object
54
+ */
55
+ static projectFolder(project: HttpProject, key: string) : ProjectItem {
56
+ const item = new ProjectItem(project, {
57
+ kind: ProjectFolderKind,
58
+ key,
59
+ });
60
+ return item;
61
+ }
62
+
63
+ /**
64
+ * @param project The top-most project.
65
+ * @param input The project item definition used to restore the state.
66
+ */
67
+ constructor(project: HttpProject, input: string|IProjectItem) {
68
+ this.project = project;
69
+ let init: IProjectItem;
70
+ if (typeof input === 'string') {
71
+ if (input === 'http-request') {
72
+ init = {
73
+ kind: ProjectRequestKind,
74
+ key: '',
75
+ };
76
+ } else if (input === 'folder') {
77
+ init = {
78
+ kind: ProjectFolderKind,
79
+ key: '',
80
+ };
81
+ } else {
82
+ init = JSON.parse(input);
83
+ }
84
+ } else if (typeof input === 'object') {
85
+ init = input;
86
+ } else {
87
+ throw new Error('Specify the type of the item.');
88
+ }
89
+ this.new(init);
90
+ }
91
+
92
+ /**
93
+ * Creates a new project item clearing anything that is so far defined.
94
+ *
95
+ * Note, this throws an error when the project item is not an ARC project item.
96
+ */
97
+ new(init: IProjectItem): void {
98
+ if (!ProjectItem.isProjectItem(init)) {
99
+ throw new Error(`Not an ARC project item.`);
100
+ }
101
+ const { kind, key } = init;
102
+ this.kind = kind;
103
+ this.key = key;
104
+ }
105
+
106
+ toJSON(): IProjectItem {
107
+ const result: IProjectItem = {
108
+ kind: this.kind,
109
+ key: this.key,
110
+ };
111
+ return result;
112
+ }
113
+
114
+ /**
115
+ * @returns The instance of the definition associated with this item.
116
+ */
117
+ getItem(): ProjectFolder | ProjectRequest| undefined {
118
+ const { project, key, kind } = this;
119
+ const { definitions } = project;
120
+ if (kind === ProjectRequestKind) {
121
+ return definitions.requests.find(i => i.key === key);
122
+ }
123
+ if (kind === ProjectFolderKind) {
124
+ return definitions.folders.find(i => i.key === key);
125
+ }
126
+ }
127
+
128
+ /**
129
+ * @returns The instance of the HttpProject or a ProjectFolder that is a closes parent of this item.
130
+ */
131
+ getParent(): ProjectFolder|HttpProject|undefined {
132
+ const { project, key } = this;
133
+ return project.findParent(key);
134
+ }
135
+ }
@@ -0,0 +1,113 @@
1
+ import { HttpProject } from "./HttpProject.js";
2
+ import { ProjectFolder } from "./ProjectFolder.js";
3
+ import { ProjectItem } from "./ProjectItem.js";
4
+ import { Thing, Kind as ThingKind } from "./Thing.js";
5
+ import { ProjectDefinitionProperty } from "./ProjectDefinitionProperty.js";
6
+ import { Environment, IEnvironment } from "./Environment.js";
7
+ import v4 from '../lib/uuid.js';
8
+
9
+ /**
10
+ * A class that contains a common properties and methods of an `HttpProject` and
11
+ * a `ProjectFolder`.
12
+ */
13
+ export abstract class ProjectParent implements ProjectDefinitionProperty {
14
+ kind: unknown;
15
+ key: string = '';
16
+ /**
17
+ * The environments defined for this project.
18
+ * If not set it is inherited from the parent.
19
+ */
20
+ environments: string[] = [];
21
+ /**
22
+ * The ordered list of HTTP requests / folders in the projects.
23
+ * The UI uses this to manipulate the view without changing the definitions.
24
+ */
25
+ items: ProjectItem[] = [];
26
+ /**
27
+ * The basic information about the project / folder.
28
+ */
29
+ info: Thing = new Thing({ kind: ThingKind, name: '' });
30
+
31
+ abstract attachedCallback(): void;
32
+
33
+ abstract detachedCallback(): void;
34
+
35
+ abstract getParent(): HttpProject | ProjectFolder | undefined;
36
+
37
+ abstract getProject(): HttpProject;
38
+
39
+ /**
40
+ * Adds an environment to the project.
41
+ *
42
+ * @param env The definition of the environment to use to create the environment
43
+ * @returns The same or created environment.
44
+ */
45
+ addEnvironment(env: IEnvironment): Environment;
46
+
47
+ /**
48
+ * Adds an environment to the project.
49
+ *
50
+ * @param env The instance of the environment to add
51
+ * @returns The same or created environment.
52
+ */
53
+ addEnvironment(env: Environment): Environment;
54
+
55
+ /**
56
+ * Adds an environment to the project.
57
+ *
58
+ * @param env The name of the environment to create
59
+ * @returns The same or created environment.
60
+ */
61
+ addEnvironment(env: string): Environment;
62
+
63
+ /**
64
+ * Adds an environment to the project.
65
+ * @returns The same or created environment.
66
+ */
67
+ addEnvironment(env: IEnvironment | Environment | string): Environment {
68
+ if (!Array.isArray(this.environments)) {
69
+ this.environments = [];
70
+ }
71
+ let finalEnv;
72
+ if (env instanceof Environment) {
73
+ finalEnv = env;
74
+ } else if (typeof env === 'string') {
75
+ finalEnv = Environment.fromName(env);
76
+ } else {
77
+ finalEnv = new Environment(env);
78
+ }
79
+ if (!finalEnv.key) {
80
+ finalEnv.key = v4();
81
+ }
82
+ const project = this.getProject();
83
+ if (!project.definitions.environments) {
84
+ project.definitions.environments = [];
85
+ }
86
+ project.definitions.environments.push(finalEnv);
87
+ this.environments.push(finalEnv.key);
88
+ return finalEnv;
89
+ }
90
+
91
+ /**
92
+ *
93
+ * @returns The list of environments defined in this folder.
94
+ */
95
+ getEnvironments(): Environment[] {
96
+ const { environments } = this;
97
+ if (!environments.length) {
98
+ return [];
99
+ }
100
+ const project = this.getProject();
101
+ if (!project.definitions.environments) {
102
+ return [];
103
+ }
104
+ const result: Environment[] = [];
105
+ environments.forEach((key) => {
106
+ const env = project.definitions.environments.find(i => i.key === key);
107
+ if (env) {
108
+ result.push(env);
109
+ }
110
+ });
111
+ return result;
112
+ }
113
+ }