@api-client/core 0.3.3 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (176) hide show
  1. package/package.json +1 -1
  2. package/src/data/DataReader.ts +11 -0
  3. package/src/data/DataUtils.ts +108 -0
  4. package/src/data/JmesparthReader.ts +26 -0
  5. package/src/data/Json2Xml.ts +190 -0
  6. package/src/data/JsonReader.ts +41 -0
  7. package/src/data/PayloadPointer.ts +48 -0
  8. package/src/data/RequestDataExtractor.ts +133 -0
  9. package/src/data/UrlEncodedReader.ts +20 -0
  10. package/src/data/XmlReader.ts +103 -0
  11. package/src/events/BaseEvents.ts +259 -0
  12. package/src/events/CustomEvent.ts +27 -0
  13. package/src/events/EventTypes.ts +19 -0
  14. package/src/events/Events.ts +19 -0
  15. package/src/events/authorization/AuthorizationEventTypes.ts +22 -0
  16. package/src/events/authorization/AuthorizationEvents.ts +61 -0
  17. package/src/events/cookies/CookieEventTypes.ts +13 -0
  18. package/src/events/cookies/CookieEvents.ts +157 -0
  19. package/src/events/encryption/EncryptionEventTypes.ts +4 -0
  20. package/src/events/encryption/EncryptionEvents.ts +51 -0
  21. package/src/events/environment/EnvironmentEventTypes.ts +3 -0
  22. package/src/events/environment/EnvironmentEvents.ts +24 -0
  23. package/src/events/models/ClientCertificateEvents.ts +87 -0
  24. package/src/events/models/ModelEventTypes.ts +47 -0
  25. package/src/events/models/ModelEvents.ts +7 -0
  26. package/src/events/models/ProjectEvents.ts +331 -0
  27. package/src/events/process/ProcessEventTypes.ts +5 -0
  28. package/src/events/process/ProcessEvents.ts +76 -0
  29. package/src/events/readme.md +22 -0
  30. package/src/events/reporting/ReportingEventTypes.ts +3 -0
  31. package/src/events/reporting/ReportingEvents.ts +28 -0
  32. package/src/events/telemetry/TelemetryEventTypes.ts +10 -0
  33. package/src/events/telemetry/TelemetryEvents.ts +156 -0
  34. package/src/lib/cookies/Cookie.ts +312 -0
  35. package/src/lib/cookies/Cookies.ts +326 -0
  36. package/src/lib/cookies/Utils.ts +168 -0
  37. package/src/lib/headers/Headers.ts +219 -0
  38. package/src/lib/logging/DefaultLogger.ts +19 -0
  39. package/src/lib/logging/DummyLogger.ts +21 -0
  40. package/src/lib/logging/Logger.ts +16 -0
  41. package/src/lib/transformers/PayloadSerializer.ts +332 -0
  42. package/src/lib/transformers/Utils.ts +18 -0
  43. package/src/lib/uuid.ts +40 -0
  44. package/src/mocking/LegacyInterfaces.ts +52 -0
  45. package/src/mocking/LegacyMock.ts +37 -0
  46. package/src/mocking/legacy/Authorization.ts +39 -0
  47. package/src/mocking/legacy/Certificates.ts +145 -0
  48. package/src/mocking/legacy/Cookies.ts +51 -0
  49. package/src/mocking/legacy/HostRules.ts +43 -0
  50. package/src/mocking/legacy/Http.ts +236 -0
  51. package/src/mocking/legacy/HttpResponse.ts +106 -0
  52. package/src/mocking/legacy/RestApi.ts +68 -0
  53. package/src/mocking/legacy/Urls.ts +44 -0
  54. package/src/mocking/legacy/Variables.ts +53 -0
  55. package/src/models/ArcResponse.ts +166 -0
  56. package/src/models/Authorization.ts +481 -0
  57. package/src/models/AuthorizationData.ts +60 -0
  58. package/src/models/Backend.ts +107 -0
  59. package/src/models/ClientCertificate.ts +68 -0
  60. package/src/models/Environment.ts +279 -0
  61. package/src/models/ErrorResponse.ts +101 -0
  62. package/src/models/HistoryIndex.ts +76 -0
  63. package/src/models/HistoryRequest.ts +28 -0
  64. package/src/models/HostRule.ts +163 -0
  65. package/src/models/HttpCookie.ts +285 -0
  66. package/src/models/HttpProject.ts +1294 -0
  67. package/src/models/HttpProjectListItem.ts +23 -0
  68. package/src/models/HttpRequest.ts +124 -0
  69. package/src/models/HttpResponse.ts +143 -0
  70. package/src/models/License.ts +113 -0
  71. package/src/models/ProjectDefinitionProperty.ts +40 -0
  72. package/src/models/ProjectFolder.ts +439 -0
  73. package/src/models/ProjectItem.ts +135 -0
  74. package/src/models/ProjectParent.ts +113 -0
  75. package/src/models/ProjectRequest.ts +277 -0
  76. package/src/models/ProjectSchema.ts +202 -0
  77. package/src/models/Property.ts +423 -0
  78. package/src/models/Provider.ts +98 -0
  79. package/src/models/README.md +20 -0
  80. package/src/models/Request.ts +452 -0
  81. package/src/models/RequestActions.ts +163 -0
  82. package/src/models/RequestAuthorization.ts +115 -0
  83. package/src/models/RequestConfig.ts +317 -0
  84. package/src/models/RequestLog.ts +159 -0
  85. package/src/models/RequestTime.ts +108 -0
  86. package/src/models/RequestUiMeta.ts +258 -0
  87. package/src/models/RequestsSize.ts +65 -0
  88. package/src/models/ResponseAuthorization.ts +104 -0
  89. package/src/models/ResponseRedirect.ts +158 -0
  90. package/src/models/RevisionInfo.ts +37 -0
  91. package/src/models/SentRequest.ts +125 -0
  92. package/src/models/SerializablePayload.ts +68 -0
  93. package/src/models/Server.ts +153 -0
  94. package/src/models/Thing.ts +110 -0
  95. package/src/models/Url.ts +90 -0
  96. package/src/models/User.ts +120 -0
  97. package/src/models/WebApi.ts +234 -0
  98. package/src/models/WebApiIndex.ts +122 -0
  99. package/src/models/Workspace.ts +182 -0
  100. package/src/models/actions/Action.ts +213 -0
  101. package/src/models/actions/ActionView.ts +40 -0
  102. package/src/models/actions/Condition.ts +207 -0
  103. package/src/models/actions/ConditionView.ts +42 -0
  104. package/src/models/actions/Enums.ts +29 -0
  105. package/src/models/actions/RunnableAction.ts +144 -0
  106. package/src/models/actions/runnable/DeleteCookieAction.ts +113 -0
  107. package/src/models/actions/runnable/Runnable.ts +9 -0
  108. package/src/models/actions/runnable/SetCookieAction.ts +216 -0
  109. package/src/models/actions/runnable/SetVariableAction.ts +81 -0
  110. package/src/models/legacy/DataExport.ts +172 -0
  111. package/src/models/legacy/Normalizer.ts +110 -0
  112. package/src/models/legacy/actions/Actions.ts +269 -0
  113. package/src/models/legacy/authorization/Authorization.ts +572 -0
  114. package/src/models/legacy/models/ApiTypes.ts +202 -0
  115. package/src/models/legacy/models/ArcLegacyProject.ts +39 -0
  116. package/src/models/legacy/models/AuthData.ts +17 -0
  117. package/src/models/legacy/models/ClientCertificate.ts +95 -0
  118. package/src/models/legacy/models/Cookies.ts +52 -0
  119. package/src/models/legacy/models/HostRule.ts +35 -0
  120. package/src/models/legacy/models/RestApi.ts +49 -0
  121. package/src/models/legacy/models/UrlHistory.ts +37 -0
  122. package/src/models/legacy/models/Variable.ts +43 -0
  123. package/src/models/legacy/models/base.d.ts +95 -0
  124. package/src/models/legacy/request/ArcRequest.ts +405 -0
  125. package/src/models/legacy/request/ArcResponse.ts +177 -0
  126. package/src/models/legacy/request/HistoryData.ts +47 -0
  127. package/src/models/legacy/request/Legacy.ts +45 -0
  128. package/src/models/legacy/request/RequestBody.ts +87 -0
  129. package/src/models/transformers/ArcDexieTransformer.ts +323 -0
  130. package/src/models/transformers/ArcLegacyNormalizer.ts +85 -0
  131. package/src/models/transformers/ArcLegacyTransformer.ts +200 -0
  132. package/src/models/transformers/ArcPouchTransformer.ts +184 -0
  133. package/src/models/transformers/BaseTransformer.ts +116 -0
  134. package/src/models/transformers/ImportUtils.ts +141 -0
  135. package/src/models/transformers/LegacyDataExportToApiProject.ts +76 -0
  136. package/src/models/transformers/LegacyExportProcessor.ts +252 -0
  137. package/src/models/transformers/PostmanBackupTransformer.ts +306 -0
  138. package/src/models/transformers/PostmanDataTransformer.ts +50 -0
  139. package/src/models/transformers/PostmanTransformer.ts +106 -0
  140. package/src/models/transformers/PostmanV21Transformer.ts +311 -0
  141. package/src/models/transformers/PostmanV2Transformer.ts +308 -0
  142. package/src/models/transformers/har.ts +865 -0
  143. package/src/runtime/actions/ActionRunner.ts +83 -0
  144. package/src/runtime/actions/ConditionRunner.ts +194 -0
  145. package/src/runtime/actions/RunnableCondition.ts +57 -0
  146. package/src/runtime/actions/runnable/ActionRunnable.ts +19 -0
  147. package/src/runtime/actions/runnable/DeleteCookieRunnable.ts +39 -0
  148. package/src/runtime/actions/runnable/SetCookieRunnable.ts +92 -0
  149. package/src/runtime/actions/runnable/SetVariableRunnable.ts +53 -0
  150. package/src/runtime/http-engine/ArcEngine.ts +1064 -0
  151. package/src/runtime/http-engine/Errors.ts +13 -0
  152. package/src/runtime/http-engine/FormData.ts +85 -0
  153. package/src/runtime/http-engine/HttpEngine.ts +874 -0
  154. package/src/runtime/http-engine/HttpErrorCodes.ts +270 -0
  155. package/src/runtime/http-engine/NodeEngine.ts +787 -0
  156. package/src/runtime/http-engine/NodeEngineDirect.ts +476 -0
  157. package/src/runtime/http-engine/PayloadSupport.ts +84 -0
  158. package/src/runtime/http-engine/RequestUtils.ts +164 -0
  159. package/src/runtime/http-engine/ntlm/Des.ts +345 -0
  160. package/src/runtime/http-engine/ntlm/MD4.ts +135 -0
  161. package/src/runtime/http-engine/ntlm/NtlmAuth.ts +186 -0
  162. package/src/runtime/http-engine/ntlm/NtlmMessage.ts +57 -0
  163. package/src/runtime/modules/BasicAuthCache.ts +133 -0
  164. package/src/runtime/modules/ExecutionResponse.ts +4 -0
  165. package/src/runtime/modules/ModulesRegistry.ts +136 -0
  166. package/src/runtime/modules/RequestAuthorization.ts +110 -0
  167. package/src/runtime/modules/RequestCookies.ts +145 -0
  168. package/src/runtime/node/ProjectRunner.ts +275 -0
  169. package/src/runtime/node/RequestFactory.ts +422 -0
  170. package/src/runtime/node/VariablesStore.ts +25 -0
  171. package/src/runtime/store/StoreSdk.ts +838 -0
  172. package/src/runtime/variables/Cache.ts +53 -0
  173. package/src/runtime/variables/EvalFunctions.ts +132 -0
  174. package/src/runtime/variables/ProjectVariables.ts +6 -0
  175. package/src/runtime/variables/VariablesProcessor.ts +543 -0
  176. package/src/runtime/variables/VariablesTokenizer.ts +55 -0
@@ -0,0 +1,184 @@
1
+ import v4 from '../../lib/uuid.js';
2
+ import { BaseTransformer, dataValue } from './BaseTransformer.js';
3
+ import {
4
+ ArcExportObject,
5
+ ExportArcProjects,
6
+ ExportArcSavedRequest,
7
+ ExportArcHistoryRequest,
8
+ ExportArcClientCertificateData,
9
+ ExportArcVariable,
10
+ } from '../legacy/DataExport.js';
11
+
12
+ /**
13
+ * Updates `updated` property and ensures `created` property
14
+ * @param item An item to updated
15
+ * @returns Shallow copy of the passed item.
16
+ */
17
+ export function updateItemTimings<T>(item: T): T {
18
+ const data = { ...item } as any;
19
+ if (!data.updated || Number.isNaN(data.updated)) {
20
+ data.updated = Date.now();
21
+ }
22
+ if (!data.created) {
23
+ data.created = data.updated;
24
+ }
25
+ return data;
26
+ }
27
+
28
+ /**
29
+ * Transforms data exported by the PouchDB system (legacy)
30
+ */
31
+ export class ArcPouchTransformer extends BaseTransformer {
32
+ /**
33
+ * Transforms PouchDB ARC export object based into current export data model.
34
+ *
35
+ * @returns New data model object.
36
+ */
37
+ async transform(): Promise<ArcExportObject> {
38
+ const data = /** @type ArcExportObject */ ({ ...this[dataValue] });
39
+ if (data.projects && data.projects.length) {
40
+ data.projects = this.transformProjects(data.projects);
41
+ }
42
+ if (data.requests && data.requests.length) {
43
+ data.requests = this.transformRequests(data.requests, data.projects);
44
+ }
45
+ if (data.projects && data.projects.length) {
46
+ (data.projects as any[]).forEach((item) => {
47
+ // @ts-ignore
48
+ delete item._referenceId;
49
+ });
50
+ }
51
+ if (data.history && data.history.length) {
52
+ data.history = this.transformHistory(data.history);
53
+ }
54
+ const socketUrls = /** @type ExportArcWebsocketUrl[] */ (data['websocket-url-history'] || data.websocketurlhistory);
55
+ if (socketUrls && socketUrls.length) {
56
+ data.websocketurlhistory = socketUrls;
57
+ }
58
+ const urls = /** @type ExportArcUrlHistory[] */ (data['url-history'] || data.urlhistory);
59
+ if (urls && urls.length) {
60
+ data.urlhistory = urls;
61
+ }
62
+ const authData = /** @type ExportArcAuthData[] */ (data['auth-data'] || data.authdata);
63
+ if (authData && authData.length) {
64
+ data.authdata = authData;
65
+ }
66
+ const hostRules = /** @type ExportArcHostRule[] */ (data['host-rules'] || data.hostrules);
67
+ if (hostRules && hostRules.length) {
68
+ data.hostrules = hostRules;
69
+ }
70
+ if (Array.isArray(data.variables) && data.variables.length) {
71
+ data.variables = this.transformVariables(data.variables);
72
+ }
73
+ if (!data.loadToWorkspace) {
74
+ data.kind = 'ARC#Import';
75
+ }
76
+ const ccs = /** @type ExportArcClientCertificateData[] */ (data['client-certificates'] || data.clientcertificates);
77
+ if (ccs && ccs.length) {
78
+ data.clientcertificates = this.transformClientCertificates(ccs);
79
+ }
80
+ return data;
81
+ }
82
+
83
+ /**
84
+ * Transforms the projects array.
85
+ *
86
+ * @param projects Projects list to upgrade
87
+ * @return Processed list
88
+ */
89
+ transformProjects(projects: ExportArcProjects[]): ExportArcProjects[] {
90
+ return projects.map((item) => {
91
+ const project = { ...item };
92
+ if (!project.key) {
93
+ project.key = v4();
94
+ }
95
+ return updateItemTimings(project);
96
+ });
97
+ }
98
+
99
+ /**
100
+ * @param requests The list of requests to process
101
+ * @param projects List of projects
102
+ * @returns Processed requests.
103
+ */
104
+ transformRequests(requests: ExportArcSavedRequest[], projects: ExportArcProjects[]=[]): ExportArcSavedRequest[] {
105
+ return requests.map((object) => {
106
+ const request = { ...object };
107
+ if (!request.key) {
108
+ // @ts-ignore
109
+ const refId = request._referenceLegacyProject || request.legacyProject;
110
+ request.key = this.generateRequestId(request, refId);
111
+ }
112
+ // @ts-ignore
113
+ const refId = request._referenceLegacyProject || request.legacyProject;
114
+ if (refId) {
115
+ // @ts-ignore
116
+ delete request._referenceLegacyProject;
117
+ // @ts-ignore
118
+ delete request.legacyProject;
119
+ // @ts-ignore
120
+ const project = projects.find((item) => item.key === refId || item._referenceId === refId);
121
+ if (project) {
122
+ this.addProjectReference(request, project.key);
123
+ this.addRequestReference(project, request.key);
124
+ }
125
+ }
126
+ request.name = request.name || 'unnamed';
127
+ request.url = request.url || 'http://';
128
+ request.method = request.method || 'GET';
129
+ request.headers = request.headers || '';
130
+ request.payload = request.payload || '';
131
+ request.kind = 'ARC#HttpRequest';
132
+ return updateItemTimings(request);
133
+ });
134
+ }
135
+
136
+ transformHistory(history: ExportArcHistoryRequest[]): ExportArcHistoryRequest[] {
137
+ return history.map((object) => {
138
+ const item = { ...object };
139
+ const result = updateItemTimings(item);
140
+ result.url = item.url || 'http://';
141
+ result.method = item.method || 'GET';
142
+ result.headers = item.headers || '';
143
+ result.payload = item.payload || '';
144
+ if (!result.key) {
145
+ result.key = this.generateHistoryId(item.created, item);
146
+ }
147
+ return result;
148
+ });
149
+ }
150
+
151
+ /**
152
+ * Transforms ARC's client certificate export object into intermediate structure
153
+ * used by the import panel.
154
+ *
155
+ * @param items [description]
156
+ * @return A list of certificates to import. In each element
157
+ * first item is the index data and the second is the certificates data.
158
+ */
159
+ transformClientCertificates(items: ExportArcClientCertificateData[]): ExportArcClientCertificateData[] {
160
+ const result: ExportArcClientCertificateData[] = [];
161
+ items.forEach((item) => {
162
+ if (item.kind !== 'ARC#ClientCertificate') {
163
+ return;
164
+ }
165
+ const data = updateItemTimings(item);
166
+ result[result.length] = data;
167
+ });
168
+ return result;
169
+ }
170
+
171
+ /**
172
+ * Maps the old `variable` property to the new `name` property.
173
+ */
174
+ transformVariables(variables: ExportArcVariable[]): ExportArcVariable[] {
175
+ return variables.map((item) => {
176
+ const variable = { ...item };
177
+ if (variable.variable && !variable.name) {
178
+ variable.name = variable.variable;
179
+ delete variable.variable;
180
+ }
181
+ return variable;
182
+ });
183
+ }
184
+ }
@@ -0,0 +1,116 @@
1
+ import {
2
+ ExportArcSavedRequest,
3
+ ExportArcHistoryRequest,
4
+ ExportArcProjects,
5
+ } from '../legacy/DataExport.js';
6
+
7
+ export const dataValue = Symbol('dataValue');
8
+
9
+ /**
10
+ * Base class for all transformers.
11
+ * Has common functions for the transformers.
12
+ */
13
+ export class BaseTransformer {
14
+ [dataValue]: any;
15
+ /**
16
+ * @param data Data to be transformed.
17
+ */
18
+ constructor(data: any) {
19
+ this[dataValue] = data;
20
+ }
21
+
22
+ /**
23
+ * Generates request's datastore ID value.
24
+ *
25
+ * @param item A request object property.
26
+ * @param projectId If set it adds project information to the ID.
27
+ * @return Request ID value.
28
+ */
29
+ generateRequestId(item: ExportArcSavedRequest | ExportArcHistoryRequest, projectId?: string): string {
30
+ const assumed = item as ExportArcSavedRequest;
31
+ const name = (assumed.name || 'unknown name').toLowerCase();
32
+ const url = (item.url || 'https://').toLowerCase();
33
+ const method = (item.method || 'GET').toLowerCase();
34
+
35
+ const eName = encodeURIComponent(name);
36
+ const eUrl = encodeURIComponent(url);
37
+
38
+ let id = `${eName}/${eUrl}/${method}`;
39
+ if (projectId) {
40
+ id += `/${projectId}`;
41
+ }
42
+ return id;
43
+ }
44
+
45
+ /**
46
+ * Computes history item ID
47
+ *
48
+ * @param timestamp The timestamp to use
49
+ * @param item History item
50
+ * @return Datastore ID
51
+ */
52
+ generateHistoryId(timestamp=Date.now(), item: ExportArcHistoryRequest): string {
53
+ const url = item.url.toLowerCase();
54
+ const method = item.method.toLowerCase();
55
+ let today;
56
+ try {
57
+ today = this.getDayToday(timestamp);
58
+ } catch (e) {
59
+ today = this.getDayToday(Date.now());
60
+ }
61
+ return `${today}/${encodeURIComponent(url)}/${method}`;
62
+ }
63
+
64
+ /**
65
+ * Sets hours, minutes, seconds and ms to 0 and returns timestamp.
66
+ *
67
+ * @param timestamp Day's timestamp.
68
+ * @returns Timestamp to the day.
69
+ */
70
+ getDayToday(timestamp: number): number {
71
+ const d = new Date(timestamp);
72
+ const tCheck = d.getTime();
73
+ if (Number.isNaN(tCheck)) {
74
+ throw new Error(`Invalid timestamp: ${ timestamp}`);
75
+ }
76
+ d.setMilliseconds(0);
77
+ d.setSeconds(0);
78
+ d.setMinutes(0);
79
+ d.setHours(0);
80
+ return d.getTime();
81
+ }
82
+
83
+ /**
84
+ * Adds project reference to a request object.
85
+ * @param request Request object to alter
86
+ * @param id Project id
87
+ */
88
+ addProjectReference(request: ExportArcSavedRequest, id?: string): void {
89
+ if (!id) {
90
+ return;
91
+ }
92
+ if (!request.projects) {
93
+ request.projects = [];
94
+ }
95
+ if (request.projects.indexOf(id) === -1) {
96
+ request.projects.push(id);
97
+ }
98
+ }
99
+
100
+ /**
101
+ * Adds request reference to a project object.
102
+ * @param project Project object to alter
103
+ * @param id Request id
104
+ */
105
+ addRequestReference(project: ExportArcProjects, id: string): void {
106
+ if (!id) {
107
+ return;
108
+ }
109
+ if (!project.requests) {
110
+ project.requests = [];
111
+ }
112
+ if (project.requests.indexOf(id) === -1) {
113
+ project.requests.push(id);
114
+ }
115
+ }
116
+ }
@@ -0,0 +1,141 @@
1
+ /* eslint-disable no-param-reassign */
2
+ /* eslint-disable no-plusplus */
3
+ /**
4
+ * User can export single request in ARC. In this case ARC opens new tab
5
+ * rather actually imports the data. This function tests if this is the case.
6
+ * @param data Normalized import data
7
+ */
8
+ export function isSingleRequest(data: any): boolean {
9
+ if (!data.requests || !data.requests.length) {
10
+ return false;
11
+ }
12
+ if (data.requests.length !== 1) {
13
+ return false;
14
+ }
15
+ if (data.projects && data.projects.length === 0) {
16
+ delete data.projects;
17
+ }
18
+ if (data.history && data.history.length === 0) {
19
+ delete data.history;
20
+ }
21
+ if (Object.keys(data).length === 4) {
22
+ return true;
23
+ }
24
+ return false;
25
+ }
26
+
27
+ /**
28
+ * First export / import system had single request data only. This function checks if given
29
+ * file is from this ancient system.
30
+ *
31
+ * @param object Decoded JSON data.
32
+ */
33
+ export function isOldImport(object: any): boolean {
34
+ if (!(object.projects || object.requests || object.history)) {
35
+ if ('headers' in object && 'url' in object && 'method' in object) {
36
+ return true;
37
+ }
38
+ }
39
+ return false;
40
+ }
41
+
42
+ /**
43
+ * Checks if the passed argument is an Object.
44
+ *
45
+ * @param object A value to test.
46
+ */
47
+ export function isObject(object: any): boolean {
48
+ return (
49
+ object !== null &&
50
+ typeof object === 'object' &&
51
+ Object.prototype.toString.call(object) === '[object Object]'
52
+ );
53
+ }
54
+
55
+ /**
56
+ * Tests if data is a Postman file data
57
+ * @param data Parsed file.
58
+ */
59
+ export function isPostman(data: any): boolean {
60
+ if (data.version && data.collections) {
61
+ return true;
62
+ }
63
+ if (data.info && data.info.schema) {
64
+ return true;
65
+ }
66
+ if (data.folders && data.requests) {
67
+ return true;
68
+ }
69
+ if (data._postman_variable_scope) {
70
+ return true;
71
+ }
72
+ return false;
73
+ }
74
+
75
+ /**
76
+ * Checks if passed `object` is the ARC export data.
77
+ *
78
+ * @param object A parsed JSON data.
79
+ * @return true if the passed object is an ARC file.
80
+ */
81
+ export function isArcFile(object: any): boolean {
82
+ if (!object || !isObject(object)) {
83
+ return false;
84
+ }
85
+ if (object.kind) {
86
+ if (object.kind.indexOf('ARC#') === 0) {
87
+ return true;
88
+ }
89
+ }
90
+ // Old export system does not have kind property.
91
+ // Have to check if it has required properties.
92
+ const arcEntries = [
93
+ 'projects',
94
+ 'requests',
95
+ 'history',
96
+ 'url-history',
97
+ 'websocket-url-history',
98
+ 'variables',
99
+ 'headers-sets',
100
+ 'auth-data',
101
+ 'cookies',
102
+ ];
103
+ for (let i = 0, len = arcEntries.length; i < len; i++) {
104
+ if (arcEntries[i] in object) {
105
+ return true;
106
+ }
107
+ }
108
+ if (isOldImport(object)) {
109
+ return true;
110
+ }
111
+ return false;
112
+ }
113
+
114
+ /**
115
+ * Parses file data with JSON parser and throws an error if not a JSON.
116
+ * If the passed `data` is JS object it does nothing.
117
+ *
118
+ * @param {string|object} data File content
119
+ * @return {object} Parsed data.
120
+ */
121
+ export function prepareImportObject(data: any): any {
122
+ if (typeof data === 'string') {
123
+ try {
124
+ data = JSON.parse(data);
125
+ } catch (e) {
126
+ const err = e as Error;
127
+ throw new Error(`Unable to read the file. Not a JSON: ${err.message}`);
128
+ }
129
+ }
130
+ return data;
131
+ }
132
+
133
+ /**
134
+ * Returns a promise resolved after a timeout.
135
+ * @param timeout A timeout to wait.
136
+ */
137
+ export async function aTimeout(timeout=0): Promise<void> {
138
+ return new Promise((resolve) => {
139
+ setTimeout(resolve, timeout);
140
+ });
141
+ }
@@ -0,0 +1,76 @@
1
+ import { HttpProject } from '../HttpProject.js';
2
+ import { ArcExportObject, ExportArcSavedRequest, ExportArcProjects, ExportArcVariable } from '../legacy/DataExport.js';
3
+ import { ARCProject } from '../legacy/models/ArcLegacyProject.js';
4
+ import { ARCSavedRequest } from '../legacy/request/ArcRequest.js';
5
+ import { Environment, IEnvironment } from '../Environment.js';
6
+
7
+ /**
8
+ * A class that transforms legacy export objects into API projects.
9
+ * Note, this only cares about the HttpProject data. All other data are discarded.
10
+ */
11
+ export class LegacyDataExportToApiProject {
12
+ async readProjects(data: ArcExportObject): Promise<HttpProject[]> {
13
+ const result: HttpProject[] = [];
14
+ const { variables } = data;
15
+ const projects = this.mapProjects(data.projects);
16
+ const requests = this.mapRequests(data.requests);
17
+ if (!projects.length || !requests.length) {
18
+ return result;
19
+ }
20
+ const environments = this.createEnvironments(variables);
21
+ const ps = projects.map(async (oldProject) => {
22
+ const project = await HttpProject.fromLegacy(oldProject, [...requests]);
23
+ environments.forEach(e => project.addEnvironment(e));
24
+ result.push(project);
25
+ });
26
+ await Promise.all(ps);
27
+ return result;
28
+ }
29
+
30
+ mapRequests(items?: ExportArcSavedRequest[]): ARCSavedRequest[] {
31
+ if (!Array.isArray(items)) {
32
+ return [];
33
+ }
34
+ return items.map((item) => {
35
+ const cp = { ...item } as ARCSavedRequest;
36
+ cp._id = item.key;
37
+ // @ts-ignore
38
+ delete cp.key;
39
+ return cp;
40
+ });
41
+ }
42
+
43
+ mapProjects(items?: ExportArcProjects[]): ARCProject[] {
44
+ if (!Array.isArray(items)) {
45
+ return [];
46
+ }
47
+ return items.map((item) => {
48
+ const cp = { ...item } as ARCProject;
49
+ cp._id = item.key;
50
+ // @ts-ignore
51
+ delete cp.key;
52
+ return cp;
53
+ });
54
+ }
55
+
56
+ createEnvironments(vars?: ExportArcVariable[]): IEnvironment[] {
57
+ if (!Array.isArray(vars) || !vars.length) {
58
+ return [];
59
+ }
60
+ const tmp: Record<string, ExportArcVariable[]> = {};
61
+ vars.forEach((variable) => {
62
+ const { environment='default' } = variable;
63
+ if (!tmp[environment]) {
64
+ tmp[environment] = [];
65
+ }
66
+ tmp[environment].push(variable);
67
+ });
68
+ const result:IEnvironment[] = [];
69
+ Object.keys(tmp).forEach((name) => {
70
+ const envVars = tmp[name];
71
+ const environment = Environment.fromLegacyVariables(name, envVars);
72
+ result.push(environment.toJSON());
73
+ });
74
+ return result;
75
+ }
76
+ }