@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,60 @@
1
+ export interface IAuthorizationData {
2
+ username?: string;
3
+ password?: string;
4
+ domain?: string;
5
+ }
6
+
7
+
8
+ /**
9
+ * Represents an auth data stored in ARC's data store to be retrieved
10
+ * when the response status is 401.
11
+ *
12
+ * Each entry represent a Basic or NTLM authorization.
13
+ */
14
+ export class AuthorizationData {
15
+ username?: string;
16
+ password?: string;
17
+ domain?: string;
18
+
19
+ /**
20
+ * @param input The provider definition used to restore the state.
21
+ */
22
+ constructor(input?: string|IAuthorizationData) {
23
+ let init: IAuthorizationData;
24
+ if (typeof input === 'string') {
25
+ init = JSON.parse(input);
26
+ } else if (typeof input === 'object') {
27
+ init = input;
28
+ } else {
29
+ init = {};
30
+ }
31
+ this.new(init);
32
+ }
33
+
34
+ /**
35
+ * Creates a new provider clearing anything that is so far defined.
36
+ *
37
+ * Note, this throws an error when the provider is not an ARC provider object.
38
+ */
39
+ new(init: IAuthorizationData): void {
40
+ const { username, password, domain } = init;
41
+ this.username = username;
42
+ this.password = password;
43
+ this.domain = domain;
44
+ }
45
+
46
+ toJSON(): IAuthorizationData {
47
+ const result:IAuthorizationData = {
48
+ };
49
+ if (this.username) {
50
+ result.username = this.username;
51
+ }
52
+ if (this.password) {
53
+ result.password = this.password;
54
+ }
55
+ if (this.domain) {
56
+ result.domain = this.domain;
57
+ }
58
+ return result;
59
+ }
60
+ }
@@ -0,0 +1,107 @@
1
+ export type BackendMode = 'single-user' | 'multi-user';
2
+
3
+ export interface IBackendInfo {
4
+ /**
5
+ * The model the store is on.
6
+ *
7
+ * The `single-user` mode is the default mode where external user authentication is not required
8
+ * (but clients must use the auth token issued by the session endpoint).
9
+ *
10
+ * In the `multi-user` model the authentication configuration is required and the user must
11
+ * authenticate through an external identity provider (by default Open ID Connect is supported).
12
+ * After that the client has to create an authenticated session in the store service and use
13
+ * the token with the API calls.
14
+ *
15
+ * @default single-user
16
+ */
17
+ mode: BackendMode;
18
+ /**
19
+ * Optional, the store base path when set on the configuration.
20
+ */
21
+ prefix?: string;
22
+ /**
23
+ * The path to the authentication endpoint.
24
+ */
25
+ authPath: string;
26
+ }
27
+
28
+ export interface IBackendCommand {
29
+ /**
30
+ * Optional path. When not set the path is the URL of the web socket channel.
31
+ */
32
+ path?: string;
33
+ /**
34
+ * The operation to perform
35
+ */
36
+ operation: 'delete' | 'patch' | 'create';
37
+ /**
38
+ * The command data.
39
+ */
40
+ value?: unknown;
41
+ }
42
+
43
+ export interface IBackendMessage {
44
+ /**
45
+ * The message type. Usually it is an `event`, meaning, a data change notification.
46
+ */
47
+ type: 'event';
48
+ /**
49
+ * In most cases an event has data associated with it like the created object or
50
+ * the patch that has been applied to the object.
51
+ * Not set for events related to deleting an object.
52
+ */
53
+ data?: unknown;
54
+ }
55
+
56
+ export interface IBackendEvent extends IBackendMessage {
57
+ /**
58
+ * The operation that has been performed on the resource.
59
+ *
60
+ * Note, `updated` is when the entire object must be revalidated in the opposite
61
+ * to `patch` where the patch should be applied to the object.
62
+ */
63
+ operation: 'created' | 'updated' | 'patch' | 'deleted' | 'access-granted' | 'access-removed';
64
+ /**
65
+ * The kind of data that has been changed.
66
+ */
67
+ kind: string;
68
+ /**
69
+ * For update events it is the key of the updated object.
70
+ */
71
+ id?: string;
72
+ }
73
+
74
+ export interface IListResponse {
75
+ /**
76
+ * The cursor to use with the next query.
77
+ * Not set when no more results.
78
+ */
79
+ cursor?: string;
80
+ /**
81
+ * The list of objects returned from the store.
82
+ */
83
+ data: unknown[];
84
+ }
85
+
86
+ export interface IListOptions {
87
+ /**
88
+ * Page cursor to use with the query.
89
+ */
90
+ cursor?: string;
91
+ /**
92
+ * Number of items in the result.
93
+ * Ignored when `cursor` is set.
94
+ *
95
+ * Note, when changing the number of items in the result
96
+ * you need to start listing over again.
97
+ */
98
+ limit?: number;
99
+ /**
100
+ * Supported by some endpoints. When set it performs a query on the data store.
101
+ */
102
+ query?: string;
103
+ /**
104
+ * Only with the `query` property. Tells the system in which fields to search for the query term.
105
+ */
106
+ queryField?: string[];
107
+ }
@@ -0,0 +1,68 @@
1
+ export type CertificateType = 'p12' | 'pem';
2
+
3
+ /**
4
+ * Represents a single certificate object (cert/key)
5
+ */
6
+ export interface ICertificate {
7
+ /**
8
+ * The certificate to use.
9
+ * The `p12` type certificate must be a Buffer.
10
+ */
11
+ data: string|ArrayBuffer|Buffer|Uint8Array;
12
+ /**
13
+ * A passphrase to use to unlock the certificate.
14
+ */
15
+ passphrase?: string;
16
+ /**
17
+ * The original data type of the certificate. This is used by the data store
18
+ * to move between buffers and string values stored in the store.
19
+ * By any means, outside the internal procedure of the data store this
20
+ * filed is always `undefined` and the `data` contains the original data format.
21
+ */
22
+ type?: string;
23
+ }
24
+
25
+ /**
26
+ * Represents a complete certificate configuration required to make
27
+ * a HTTP request.
28
+ */
29
+ export interface IRequestCertificate {
30
+ /**
31
+ * Certificate type. Either `p12` or `pem`.
32
+ */
33
+ type: CertificateType;
34
+ /**
35
+ * Certificate or list of certificates to use.
36
+ */
37
+ cert: ICertificate|ICertificate[];
38
+ /**
39
+ * Key for the `pem` type certificate.
40
+ */
41
+ key?: ICertificate|ICertificate[];
42
+ }
43
+
44
+ /**
45
+ * Client certificate index definition for listings.
46
+ */
47
+ export interface ICertificateIndex {
48
+ /**
49
+ * Certificate type. Either `p12` or `pem`.
50
+ */
51
+ type: CertificateType;
52
+ /**
53
+ * Custom name of the certificate.
54
+ */
55
+ name: string;
56
+ /**
57
+ * Timestamp when the certificate was inserted into the data store.
58
+ * Required when returning a result. Auto-generated when inserting, if missing.
59
+ */
60
+ created?: number;
61
+ }
62
+
63
+ /**
64
+ * A base client certificate object used in ARC to create / list objects.
65
+ * This is not a data entity but rather something used to outside of a context of a data store.
66
+ */
67
+ export interface IClientCertificate extends IRequestCertificate, ICertificateIndex {
68
+ }
@@ -0,0 +1,279 @@
1
+ import { Property, IProperty } from './Property.js';
2
+ import { Server, IServer } from './Server.js';
3
+ import { IThing, Thing, Kind as ThingKind } from './Thing.js';
4
+ import v4 from '../lib/uuid.js';
5
+ import { ARCVariable } from './legacy/models/Variable.js';
6
+
7
+ export interface IEnvironmentCloneOptions {
8
+ /**
9
+ * By default it revalidates (re-creates) keys in the environment.
10
+ * Set this to true to not make any changes to the keys.
11
+ */
12
+ withoutRevalidate?: boolean;
13
+ }
14
+
15
+ /**
16
+ * A project environment definition.
17
+ */
18
+ export interface IEnvironment {
19
+ kind: 'ARC#Environment';
20
+ /**
21
+ * The identifier of the environment.
22
+ */
23
+ key: string;
24
+ /**
25
+ * The environment's meta info.
26
+ */
27
+ info: IThing;
28
+ /**
29
+ * The server's definition for the environment.
30
+ * When a server is defined for the environment then all request that have no
31
+ * absolute URL will inherit this server configuration.
32
+ */
33
+ server?: IServer;
34
+ /**
35
+ * The variables added to this project.
36
+ * It overrides application defined variables.
37
+ */
38
+ variables: IProperty[];
39
+ /**
40
+ * When set this configuration does not allow to be extended by the parent object's environment configuration.
41
+ * By default an environment can be extended by the parent object's values.
42
+ */
43
+ encapsulated?: boolean;
44
+ /**
45
+ * The security to be applied to all requests that are going to this environment.
46
+ */
47
+ security?: unknown;
48
+ }
49
+
50
+ export const Kind = 'ARC#Environment';
51
+
52
+ /**
53
+ * An environment is applied to a project or a folder.
54
+ * It consists of a list of variables and a server configuration.
55
+ * This allows to apply the runtime configuration just by changing the environment.
56
+ */
57
+ export class Environment {
58
+ kind = Kind;
59
+ key = '';
60
+ /**
61
+ * The name of the environment.
62
+ */
63
+ info: Thing = new Thing({ kind: ThingKind });
64
+ /**
65
+ * The variables added to this project.
66
+ * It overrides application defined variables.
67
+ */
68
+ variables: Property[] = [];
69
+ /**
70
+ * The server's definition for the environment.
71
+ * When a server is defined for the environment then all request that have no
72
+ * absolute URL will inherit this server configuration.
73
+ */
74
+ server?: Server;
75
+ /**
76
+ * When set this configuration does not allow to be extended by the parent object's environment configuration.
77
+ * By default an environment can be extended by the parent object's values.
78
+ *
79
+ * When encapsulation is disabled you can, for example, skip server definition and only overwrite variables.
80
+ */
81
+ encapsulated = false;
82
+ /**
83
+ * The security to be applied to all requests that are going to this server.
84
+ */
85
+ security?: unknown;
86
+
87
+ /**
88
+ * Creates a new Environment object from a name.
89
+ *
90
+ * @param name The name to set.
91
+ */
92
+ static fromName(name: string): Environment {
93
+ const key = v4();
94
+ const info = new Thing({ kind: ThingKind, name });
95
+ const definition = new Environment({
96
+ key,
97
+ kind: Kind,
98
+ info: info.toJSON(),
99
+ variables: [],
100
+ });
101
+ return definition;
102
+ }
103
+
104
+ static fromLegacyVariables(name: string, variables: ARCVariable[]): Environment {
105
+ const key = v4();
106
+ const info = new Thing({ kind: ThingKind, name });
107
+ const definition = new Environment({
108
+ key,
109
+ kind: Kind,
110
+ info: info.toJSON(),
111
+ variables: [],
112
+ });
113
+ variables.forEach((i) => {
114
+ const name = i.variable || i.name;
115
+ const property = Property.fromApiType(i);
116
+ property.name = name;
117
+ definition.variables.push(property);
118
+ });
119
+ return definition;
120
+ }
121
+
122
+ /**
123
+ * @param input The environment definition used to restore the state.
124
+ */
125
+ constructor(input?: string|IEnvironment) {
126
+ let init: IEnvironment;
127
+ if (typeof input === 'string') {
128
+ init = JSON.parse(input);
129
+ } else if (typeof input === 'object') {
130
+ init = input;
131
+ } else {
132
+ init = {
133
+ kind: Kind,
134
+ key: v4(),
135
+ info: {
136
+ kind: ThingKind,
137
+ name: '',
138
+ },
139
+ variables: [],
140
+ };
141
+ }
142
+ this.new(init);
143
+ }
144
+
145
+ /**
146
+ * Creates a new environment clearing anything that is so far defined.
147
+ *
148
+ * Note, this throws an error when the environment is not an ARC environment.
149
+ */
150
+ new(init: IEnvironment): void {
151
+ if (!Environment.isEnvironment(init)) {
152
+ throw new Error(`Not an ARC environment.`);
153
+ }
154
+ const { key=v4(), variables, info, server, encapsulated=false, security } = init;
155
+ this.kind = Kind;
156
+ this.key = key;
157
+ this.encapsulated = encapsulated;
158
+ this.security = security;
159
+ if (Array.isArray(variables)) {
160
+ this.variables = variables.map(i => new Property(i))
161
+ } else {
162
+ this.variables = [];
163
+ }
164
+ if (server) {
165
+ this.server = new Server(server);
166
+ } else {
167
+ this.server = undefined;
168
+ }
169
+ if (info) {
170
+ this.info = new Thing(info);
171
+ } else {
172
+ this.info = new Thing({ kind: ThingKind, name: '' });
173
+ }
174
+ }
175
+
176
+ /**
177
+ * Checks whether the input is a definition of a environment.
178
+ */
179
+ static isEnvironment(input: unknown): boolean {
180
+ const typed = input as IEnvironment;
181
+ if (!input || typed.kind !== Kind) {
182
+ return false;
183
+ }
184
+ return true;
185
+ }
186
+
187
+ toJSON(): IEnvironment {
188
+ const { variables=[], encapsulated=false, server } = this;
189
+ const result: IEnvironment = {
190
+ kind: Kind,
191
+ key: this.key,
192
+ info: this.info.toJSON(),
193
+ variables: variables.map((v) => v.toJSON()),
194
+ encapsulated,
195
+ };
196
+ if (server) {
197
+ result.server = server.toJSON();
198
+ }
199
+ if (this.security) {
200
+ result.security = this.security;
201
+ }
202
+ return result;
203
+ }
204
+
205
+ addVariable(name: string, value: unknown): Property;
206
+ addVariable(variable: IProperty): Property;
207
+
208
+ /**
209
+ * Adds a new variable to the list of variables.
210
+ * It makes sure the variables property is initialized.
211
+ */
212
+ addVariable(variableOrName: IProperty|string, value?: unknown): Property {
213
+ if (!Array.isArray(this.variables)) {
214
+ this.variables = [];
215
+ }
216
+ let prop: Property;
217
+ if (typeof variableOrName === 'string') {
218
+ prop = Property.fromType(variableOrName, value);
219
+ } else {
220
+ prop = new Property(variableOrName);
221
+ }
222
+ this.variables.push(prop);
223
+ return prop;
224
+ }
225
+
226
+ /**
227
+ * Reads the server configuration.
228
+ * @param force When set then it creates a server instance when missing.
229
+ */
230
+ getServer(force?: boolean): Server|undefined {
231
+ if (!this.server && !force) {
232
+ return undefined;
233
+ }
234
+
235
+ if (!this.server) {
236
+ this.server = new Server();
237
+ }
238
+ return this.server;
239
+ }
240
+
241
+ /**
242
+ * Adds a new server definition.
243
+ * @param uri The base URI to create the server from
244
+ */
245
+ addServer(uri: string): Server;
246
+
247
+ /**
248
+ * Adds a new server definition.
249
+ * @param info The server definition.
250
+ */
251
+ addServer(info: IServer): Server;
252
+
253
+ /**
254
+ * Adds a new server definition.
255
+ * @param uriOrInfo Either URI to create the server from or the definition of the server.
256
+ */
257
+ addServer(uriOrInfo: string | IServer): Server {
258
+ let srv: Server;
259
+ if (typeof uriOrInfo === 'string') {
260
+ srv = Server.fromUri(uriOrInfo);
261
+ } else {
262
+ srv = new Server(uriOrInfo);
263
+ }
264
+ this.server = srv;
265
+ return srv;
266
+ }
267
+
268
+ /**
269
+ * Makes a copy of this environment
270
+ * @param opts Cloning options.
271
+ */
272
+ clone(opts: IEnvironmentCloneOptions = {}): Environment {
273
+ const copy = new Environment(this.toJSON());
274
+ if (!opts.withoutRevalidate) {
275
+ copy.key = v4();
276
+ }
277
+ return copy;
278
+ }
279
+ }
@@ -0,0 +1,101 @@
1
+ import { IHttpResponse, HttpResponse, Kind } from './HttpResponse.js';
2
+ import { ErrorResponse as LegacyErrorResponse } from './legacy/request/ArcResponse.js';
3
+ import { PayloadSerializer } from '../lib/transformers/PayloadSerializer.js';
4
+ import { Normalizer } from './legacy/Normalizer.js';
5
+
6
+ export interface IErrorResponse extends IHttpResponse {
7
+ /**
8
+ * An error associated with the response
9
+ */
10
+ error: string | Error;
11
+ }
12
+
13
+ export class ErrorResponse extends HttpResponse {
14
+ /**
15
+ * An error associated with the response
16
+ */
17
+ error = new Error('Unknown error');
18
+
19
+ /**
20
+ * @returns The same Error or new Error instance when passed string.
21
+ */
22
+ static ensureError(error: string | Error): Error {
23
+ return typeof error === 'string' ? new Error(error) : error;
24
+ }
25
+
26
+ /**
27
+ * @param error The error message or Error object to use.
28
+ */
29
+ static fromError(error: Error | string): ErrorResponse {
30
+ const err = ErrorResponse.ensureError(error);
31
+ return new ErrorResponse({
32
+ kind: Kind,
33
+ status: 0,
34
+ error: err,
35
+ });
36
+ }
37
+
38
+ static async fromLegacy(input: LegacyErrorResponse): Promise<ErrorResponse> {
39
+ const init: IErrorResponse = {
40
+ kind: Kind,
41
+ status: input.status || 0,
42
+ error: input.error ? ErrorResponse.ensureError(input.error) : new Error('Unknown error'),
43
+ };
44
+ if (input.headers) {
45
+ init.headers = input.headers;
46
+ }
47
+ if (input.statusText) {
48
+ init.statusText = input.statusText;
49
+ }
50
+ if (input.payload) {
51
+ const orig = Normalizer.restoreTransformedPayload(input.payload);
52
+ if (orig) {
53
+ init.payload = await PayloadSerializer.serialize(orig);
54
+ }
55
+ }
56
+ return new ErrorResponse(init);
57
+ }
58
+
59
+ /**
60
+ * @param input The request definition used to restore the state.
61
+ */
62
+ constructor(input?: string|IErrorResponse) {
63
+ super(input);
64
+ let init: IErrorResponse;
65
+ if (typeof input === 'string') {
66
+ init = JSON.parse(input);
67
+ } else if (typeof input === 'object') {
68
+ init = input;
69
+ } else {
70
+ init = {
71
+ kind: Kind,
72
+ status: 0,
73
+ error: new Error('Unknown error'),
74
+ };
75
+ }
76
+ this.new(init);
77
+ }
78
+
79
+ /**
80
+ * Creates a new request clearing anything that is so far defined.
81
+ *
82
+ * Note, this throws an error when the object is not an ARC request.
83
+ */
84
+ new(init: IErrorResponse): void {
85
+ super.new(init);
86
+ if (init.error) {
87
+ this.error = ErrorResponse.ensureError(init.error);
88
+ }
89
+ }
90
+
91
+ toJSON(): IErrorResponse {
92
+ const response = super.toJSON() as IErrorResponse;
93
+ response.error = this.error;
94
+ return response;
95
+ }
96
+
97
+ static isErrorResponse(input: unknown): boolean {
98
+ const typed = input as IErrorResponse;
99
+ return !!typed.error;
100
+ }
101
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * An object that is stored in ARCs internal store to build history navigation.
3
+ * It does not contain all the history data. Just the ones that are presented in the UI.
4
+ */
5
+ export interface IHistoryIndex {
6
+ /**
7
+ * Timestamp when the request was last updated.
8
+ */
9
+ updated: number;
10
+ /**
11
+ * A timestamp of the midnight when the request object was updated
12
+ */
13
+ midnight: number;
14
+ /**
15
+ * The request URL
16
+ */
17
+ url: string;
18
+ /**
19
+ * HTTP method name
20
+ */
21
+ method: string;
22
+ }
23
+
24
+ export class HistoryIndex {
25
+ /**
26
+ * Timestamp when the request was last updated.
27
+ */
28
+ updated = 0;
29
+ /**
30
+ * A timestamp of the midnight when the request object was updated
31
+ */
32
+ midnight = 0;
33
+ /**
34
+ * The request URL
35
+ */
36
+ url = '';
37
+ /**
38
+ * HTTP method name
39
+ */
40
+ method = 'GET';
41
+
42
+ constructor(input?: string|IHistoryIndex) {
43
+ let init: IHistoryIndex;
44
+ if (typeof input === 'string') {
45
+ init = JSON.parse(input);
46
+ } else if (typeof input === 'object') {
47
+ init = input;
48
+ } else {
49
+ init = {
50
+ updated: 0,
51
+ midnight: 0,
52
+ url: '',
53
+ method: 'GET',
54
+ };
55
+ }
56
+ this.new(init);
57
+ }
58
+
59
+ new(init: IHistoryIndex): void {
60
+ const { updated=0, midnight=0, url='', method='GET' } = init;
61
+ this.updated = updated;
62
+ this.midnight = midnight;
63
+ this.url = url;
64
+ this.method = method;
65
+ }
66
+
67
+ toJSON(): IHistoryIndex {
68
+ const result: IHistoryIndex = {
69
+ updated: this.updated,
70
+ midnight: this.midnight,
71
+ url: this.url,
72
+ method: this.method,
73
+ };
74
+ return result;
75
+ }
76
+ }