@api-client/core 0.4.0 → 0.4.1

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 (106) hide show
  1. package/build/browser.d.ts +5 -2
  2. package/build/browser.js +7 -2
  3. package/build/browser.js.map +1 -1
  4. package/build/index.d.ts +6 -4
  5. package/build/index.js +4 -3
  6. package/build/index.js.map +1 -1
  7. package/build/src/models/HttpHistory.d.ts +29 -0
  8. package/build/src/models/HttpHistory.js.map +1 -1
  9. package/build/src/models/RequestLog.d.ts +8 -0
  10. package/build/src/models/RequestLog.js +14 -1
  11. package/build/src/models/RequestLog.js.map +1 -1
  12. package/build/src/runtime/node/InteropInterfaces.d.ts +115 -0
  13. package/build/src/runtime/node/InteropInterfaces.js +2 -0
  14. package/build/src/runtime/node/InteropInterfaces.js.map +1 -0
  15. package/build/src/runtime/node/ProjectParallelRunner.d.ts +2 -7
  16. package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -1
  17. package/build/src/runtime/node/ProjectRequestRunner.d.ts +17 -49
  18. package/build/src/runtime/node/ProjectRequestRunner.js +39 -10
  19. package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -1
  20. package/build/src/runtime/node/ProjectRunner.d.ts +1 -59
  21. package/build/src/runtime/node/ProjectRunner.js.map +1 -1
  22. package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -1
  23. package/build/src/runtime/store/AuthSdk.d.ts +28 -0
  24. package/build/src/runtime/store/AuthSdk.js +123 -0
  25. package/build/src/runtime/store/AuthSdk.js.map +1 -0
  26. package/build/src/runtime/store/BackendSdk.d.ts +8 -0
  27. package/build/src/runtime/store/BackendSdk.js +25 -0
  28. package/build/src/runtime/store/BackendSdk.js.map +1 -0
  29. package/build/src/runtime/store/HistorySdk.d.ts +48 -0
  30. package/build/src/runtime/store/HistorySdk.js +142 -0
  31. package/build/src/runtime/store/HistorySdk.js.map +1 -0
  32. package/build/src/runtime/store/Http.d.ts +14 -0
  33. package/build/src/runtime/store/Http.js +4 -0
  34. package/build/src/runtime/store/Http.js.map +1 -0
  35. package/build/src/runtime/store/HttpNode.d.ts +16 -0
  36. package/build/src/runtime/store/HttpNode.js +95 -0
  37. package/build/src/runtime/store/HttpNode.js.map +1 -0
  38. package/build/src/runtime/store/HttpWeb.d.ts +15 -0
  39. package/build/src/runtime/store/HttpWeb.js +83 -0
  40. package/build/src/runtime/store/HttpWeb.js.map +1 -0
  41. package/build/src/runtime/store/ProjectsSdk.d.ts +43 -0
  42. package/build/src/runtime/store/ProjectsSdk.js +144 -0
  43. package/build/src/runtime/store/ProjectsSdk.js.map +1 -0
  44. package/build/src/runtime/store/RouteBuilder.d.ts +42 -0
  45. package/build/src/runtime/store/RouteBuilder.js +75 -0
  46. package/build/src/runtime/store/RouteBuilder.js.map +1 -0
  47. package/build/src/runtime/store/Sdk.d.ts +79 -0
  48. package/build/src/runtime/store/Sdk.js +117 -0
  49. package/build/src/runtime/store/Sdk.js.map +1 -0
  50. package/build/src/runtime/store/SdkBase.d.ts +45 -0
  51. package/build/src/runtime/store/SdkBase.js +40 -0
  52. package/build/src/runtime/store/SdkBase.js.map +1 -0
  53. package/build/src/runtime/store/SpacesSdk.d.ts +50 -0
  54. package/build/src/runtime/store/SpacesSdk.js +185 -0
  55. package/build/src/runtime/store/SpacesSdk.js.map +1 -0
  56. package/build/src/runtime/store/StoreSdkNode.d.ts +10 -0
  57. package/build/src/runtime/store/StoreSdkNode.js +11 -0
  58. package/build/src/runtime/store/StoreSdkNode.js.map +1 -0
  59. package/build/src/runtime/store/StoreSdkWeb.d.ts +10 -0
  60. package/build/src/runtime/store/StoreSdkWeb.js +11 -0
  61. package/build/src/runtime/store/StoreSdkWeb.js.map +1 -0
  62. package/build/src/runtime/store/UsersSdk.d.ts +18 -0
  63. package/build/src/runtime/store/UsersSdk.js +88 -0
  64. package/build/src/runtime/store/UsersSdk.js.map +1 -0
  65. package/build/src/runtime/store/WsClient.d.ts +24 -0
  66. package/build/src/runtime/store/WsClient.js +4 -0
  67. package/build/src/runtime/store/WsClient.js.map +1 -0
  68. package/build/src/runtime/store/WsClientNode.d.ts +24 -0
  69. package/build/src/runtime/store/WsClientNode.js +66 -0
  70. package/build/src/runtime/store/WsClientNode.js.map +1 -0
  71. package/build/src/runtime/store/WsClientWeb.d.ts +23 -0
  72. package/build/src/runtime/store/WsClientWeb.js +73 -0
  73. package/build/src/runtime/store/WsClientWeb.js.map +1 -0
  74. package/package.json +4 -6
  75. package/src/models/HttpHistory.ts +30 -0
  76. package/src/models/RequestLog.ts +17 -1
  77. package/src/runtime/node/InteropInterfaces.ts +121 -0
  78. package/src/runtime/node/ProjectParallelRunner.ts +2 -9
  79. package/src/runtime/node/ProjectRequestRunner.ts +42 -61
  80. package/src/runtime/node/ProjectRunner.ts +1 -60
  81. package/src/runtime/node/ProjectRunnerWorker.ts +2 -1
  82. package/src/runtime/store/AuthSdk.ts +126 -0
  83. package/src/runtime/store/BackendSdk.ts +25 -0
  84. package/src/runtime/store/HistorySdk.ts +157 -0
  85. package/src/runtime/store/Http.ts +18 -0
  86. package/src/runtime/store/HttpNode.ts +100 -0
  87. package/src/runtime/store/HttpWeb.ts +89 -0
  88. package/src/runtime/store/ProjectsSdk.ts +147 -0
  89. package/src/runtime/store/RouteBuilder.ts +89 -0
  90. package/src/runtime/store/Sdk.ts +131 -0
  91. package/src/runtime/store/SdkBase.ts +72 -0
  92. package/src/runtime/store/SpacesSdk.ts +190 -0
  93. package/src/runtime/store/StoreSdkNode.ts +13 -0
  94. package/src/runtime/store/StoreSdkWeb.ts +13 -0
  95. package/src/runtime/store/UsersSdk.ts +89 -0
  96. package/src/runtime/store/WsClient.ts +28 -0
  97. package/src/runtime/store/WsClientNode.ts +68 -0
  98. package/src/runtime/store/WsClientWeb.ts +75 -0
  99. package/build/src/models/HistoryIndex.d.ts +0 -45
  100. package/build/src/models/HistoryIndex.js +0 -56
  101. package/build/src/models/HistoryIndex.js.map +0 -1
  102. package/build/src/runtime/store/StoreSdk.d.ts +0 -279
  103. package/build/src/runtime/store/StoreSdk.js +0 -842
  104. package/build/src/runtime/store/StoreSdk.js.map +0 -1
  105. package/src/models/HistoryIndex.ts +0 -81
  106. package/src/runtime/store/StoreSdk.ts +0 -895
@@ -3,75 +3,16 @@ import { HttpProject } from '../../models/HttpProject.js';
3
3
  import { ProjectFolder } from '../../models/ProjectFolder.js';
4
4
  import { Environment, IEnvironment } from '../../models/Environment.js';
5
5
  import { DummyLogger } from '../../lib/logging/DummyLogger.js';
6
- import { Logger } from '../../lib/logging/Logger.js';
7
6
  import { IRequestLog } from '../../models/RequestLog.js';
8
7
  import { IHttpRequest } from '../../models/HttpRequest.js';
9
8
  import { ProjectRequestRunner } from './ProjectRequestRunner.js';
10
9
  import { IProjectExecutionIteration, IProjectExecutionLog } from '../reporters/Reporter.js';
11
10
  import { pathExists, readJson } from '../../lib/fs/Fs.js';
12
11
  import { BaseRunner } from './BaseRunner.js';
12
+ import { IProjectRunnerOptions } from './InteropInterfaces.js';
13
13
 
14
14
  type ProjectParent = HttpProject | ProjectFolder;
15
15
 
16
- export interface IProjectRunnerOptions {
17
- /**
18
- * The environment to use.
19
- * This can be a name or the key of the environment located under the parent or root.
20
- * It can also be a path to the environment definition. If the file exists it is used. Otherwise it tried to
21
- * find the environment in the project.
22
- */
23
- environment?: string;
24
- /**
25
- * The parent folder to execute.
26
- */
27
- parent?: string;
28
- /**
29
- * The names or the keys of requests to execute.
30
- * This can be used to limit the number of requests.
31
- */
32
- request?: string[];
33
- /**
34
- * The number of times the execution should be repeated.
35
- * Default to 1.
36
- */
37
- iterations?: number;
38
- /**
39
- * The number of milliseconds to wait between each iteration.
40
- * Default to the next frame (vary from 1 to tens of milliseconds).
41
- */
42
- iterationDelay?: number;
43
- /**
44
- * When set it performs parallel execution for each iteration.
45
- * The number of executions at the same time depends on the number of processor cores
46
- * available on the current machine. The maximum of the parallel execution
47
- * is the number of available cores. When the `iterations` number is higher
48
- * then the "rest" is added to the iterations per each core.
49
- */
50
- parallel?: boolean;
51
- /**
52
- * When set it includes requests in the current folder and sub-folder according to the order
53
- * defined in the folder.
54
- */
55
- recursive?: boolean;
56
- /**
57
- * The opposite of the `requests`. The list of names or keys of requests or folders to ignore.
58
- * Note, ignore is tested before the `requests`.
59
- */
60
- ignore?: string[];
61
- /**
62
- * The logger to use with the request factory.
63
- * When not set it uses the dummy logger (no output).
64
- */
65
- logger?: Logger;
66
- /**
67
- * When true it copies all system variables to the execution environment.
68
- * When an array of strings, only takes system variables that are listed in the array.
69
- * When a map, it uses this map as a list of variables.
70
- * When not set it does not read system variables.
71
- */
72
- variables?: boolean | string[] | Record<string, string>;
73
- }
74
-
75
16
  export interface ProjectRunner {
76
17
  /**
77
18
  * Event dispatched when an iteration is about to start.
@@ -3,7 +3,8 @@ import process from 'process';
3
3
  import cluster from 'cluster';
4
4
  import { HttpProject } from '../../models/HttpProject.js';
5
5
  import { IProjectExecutionLog } from '../reporters/Reporter.js';
6
- import { IWorkerMessage, IProjectParallelWorkerOptions } from './ProjectParallelRunner.js';
6
+ import { IWorkerMessage } from './ProjectParallelRunner.js';
7
+ import { IProjectParallelWorkerOptions } from './InteropInterfaces.js';
7
8
  import { sleep } from '../../lib/timers/Timers.js';
8
9
  import { ProjectRunner } from './ProjectRunner.js';
9
10
 
@@ -0,0 +1,126 @@
1
+ import { SdkBase, IStoreTokenInfo } from './SdkBase.js';
2
+ import { RouteBuilder } from './RouteBuilder.js';
3
+ import { Headers } from '../../lib/headers/Headers.js';
4
+ import WebSocketNode from 'ws';
5
+
6
+ export class AuthSdk extends SdkBase {
7
+ protected getExpires(headers: Headers): number | undefined {
8
+ const expires = headers.get('expires');
9
+ if (!expires) {
10
+ return undefined;
11
+ }
12
+ const d = new Date(expires);
13
+ const time = d.getTime();
14
+ if (Number.isNaN(time)) {
15
+ console.warn(`Invalid session response: the expires header cannot be parsed.`);
16
+ return undefined;
17
+ }
18
+ return time;
19
+ }
20
+
21
+ /**
22
+ * Creates unauthenticated session in the backend.
23
+ * @returns The JWT for unauthenticated user.
24
+ */
25
+ async createSession(): Promise<IStoreTokenInfo> {
26
+ const url = this.sdk.getUrl(RouteBuilder.sessions());
27
+ // console.log('Create session: ', url);
28
+ const result = await this.sdk.http.post(url.toString());
29
+ this.inspectCommonStatusCodes(result.status);
30
+ if (result.status !== 200) {
31
+ throw new Error(`Unable to create the session. Invalid response status: ${result.status}`);
32
+ }
33
+ if (!result.body) {
34
+ throw new Error(`Unable to create the session. Response has no token.`);
35
+ }
36
+ const info: IStoreTokenInfo = {
37
+ token: result.body,
38
+ };
39
+ const expires = this.getExpires(result.headers);
40
+ if (expires) {
41
+ info.expires = expires;
42
+ }
43
+ return info;
44
+ }
45
+
46
+ /**
47
+ * Initializes the authentication session.
48
+ * @param token The unauthenticated session JWT. Required when not set on the class.
49
+ * @returns The location of the authorization endpoint.
50
+ */
51
+ async createAuthSession(token?: string, loginPath = '/auth/login'): Promise<string> {
52
+ const url = this.sdk.getUrl(loginPath);
53
+ const result = await this.sdk.http.post(url.toString(), { token });
54
+ this.inspectCommonStatusCodes(result.status);
55
+ const loc = result.headers.get('location');
56
+ if (!loc) {
57
+ throw new Error(`The location header not returned by the server.`);
58
+ }
59
+ return loc;
60
+ }
61
+
62
+ /**
63
+ * Listens to the first message coming to the client from the auth endpoint.
64
+ * @param authPath The authorization path returned by the server info or 401 response.
65
+ * @param token Optional token to use.
66
+ */
67
+ async listenAuth(authPath: string, token?: string): Promise<void> {
68
+ const url = this.sdk.getUrl(authPath);
69
+ const client = await this.sdk.ws.createAndConnect(url.toString(), token);
70
+ return new Promise((resolve, reject) => {
71
+ const { sdk } = this;
72
+ async function finishData(data: any): Promise<void> {
73
+ let message: any;
74
+ try {
75
+ message = JSON.parse(data.toString());
76
+ await sdk.ws.disconnect(client);
77
+ } catch (cause) {
78
+ reject(cause);
79
+ return;
80
+ }
81
+ if (message.status === 'OK') {
82
+ resolve();
83
+ } else {
84
+ reject(new Error(message.message || 'Unknown error'));
85
+ }
86
+ }
87
+ const typedNode = client as WebSocketNode;
88
+ if (typeof typedNode.on === 'function') {
89
+ typedNode.on('message', (data: Buffer) => {
90
+ finishData(data);
91
+ });
92
+ } else {
93
+ const typedWeb = client as WebSocket;
94
+ typedWeb.addEventListener('message', (event) => {
95
+ finishData(event.data);
96
+ });
97
+ }
98
+ });
99
+ }
100
+
101
+ /**
102
+ * Renews authenticated token to a new one when the token expires.
103
+ * @param token Optional token to use.
104
+ * @returns
105
+ */
106
+ async renewToken(token = this.sdk.token): Promise<IStoreTokenInfo> {
107
+ const authPath = RouteBuilder.sessionRenew();
108
+ const url = this.sdk.getUrl(authPath);
109
+ const result = await this.sdk.http.post(url.toString(), { token });
110
+ this.inspectCommonStatusCodes(result.status);
111
+ if (result.status !== 200) {
112
+ throw new Error(`Unable to renew the token. Invalid response status: ${result.status}`);
113
+ }
114
+ if (!result.body) {
115
+ throw new Error(`Unable to create the session. Response has no token.`);
116
+ }
117
+ const info: IStoreTokenInfo = {
118
+ token: result.body,
119
+ };
120
+ const expires = this.getExpires(result.headers);
121
+ if (expires) {
122
+ info.expires = expires;
123
+ }
124
+ return info;
125
+ }
126
+ }
@@ -0,0 +1,25 @@
1
+ import { SdkBase } from './SdkBase.js';
2
+ import { RouteBuilder } from './RouteBuilder.js';
3
+ import { IBackendInfo } from '../../models/Backend.js';
4
+
5
+ export class BackendSdk extends SdkBase {
6
+ /**
7
+ * @returns Client information about the store configuration.
8
+ */
9
+ async getInfo(): Promise<IBackendInfo> {
10
+ const url = this.sdk.getUrl(RouteBuilder.backend());
11
+ const result = await this.sdk.http.get(url.toString());
12
+ this.inspectCommonStatusCodes(result.status);
13
+ if (result.status !== 200) {
14
+ throw new Error(`Invalid store response. Expected 200 status and ${result.status} received.`);
15
+ }
16
+ const body = result.body as string;
17
+ let data;
18
+ try {
19
+ data = JSON.parse(body);
20
+ } catch (e) {
21
+ throw new Error(`The server returned invalid response. Unable to read store status.`);
22
+ }
23
+ return data as IBackendInfo;
24
+ }
25
+ }
@@ -0,0 +1,157 @@
1
+ import { SdkBase, E_RESPONSE_STATUS, E_RESPONSE_NO_VALUE, E_INVALID_JSON, E_RESPONSE_UNKNOWN, E_RESPONSE_LOCATION } from './SdkBase.js';
2
+ import { RouteBuilder } from './RouteBuilder.js';
3
+ import { IListResponse, HistoryListOptions } from '../../models/Backend.js';
4
+ import { IHttpHistory, IHttpHistoryBulkAdd, Kind as HttpHistoryKind } from '../../models/HttpHistory.js';
5
+
6
+ export class HistorySdk extends SdkBase {
7
+ /**
8
+ * Creates a history object.
9
+ * A history object can be created per app (type = app) or a store's space/[project/[request]].
10
+ *
11
+ * The user can always read their own history. If the history is created for a space/project then
12
+ * history records are shared as any other object in the space.
13
+ *
14
+ * Note, history objects cannot be updated. They can only be created or deleted.
15
+ *
16
+ * @param history The history to create
17
+ * @returns The key of the created history.
18
+ */
19
+ async create(history: IHttpHistory): Promise<string> {
20
+ const { token } = this.sdk;
21
+ const url = this.sdk.getUrl(RouteBuilder.history());
22
+ const body = JSON.stringify(history);
23
+ const result = await this.sdk.http.post(url.toString(), { token, body });
24
+ this.inspectCommonStatusCodes(result.status);
25
+ const E_PREFIX = 'Unable to create a history. ';
26
+ if (result.status !== 200) {
27
+ this.logInvalidResponse(result);
28
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
29
+ }
30
+ const location = result.headers.get('location');
31
+ if (!location) {
32
+ throw new Error(`${E_PREFIX}${E_RESPONSE_LOCATION}`);
33
+ }
34
+ const id = location.split('/').pop();
35
+ return id as string;
36
+ }
37
+
38
+ /**
39
+ * Creates a multiple history objects in a batch operation.
40
+ *
41
+ * @param info The bulk create info object.
42
+ */
43
+ async createBulk(info: IHttpHistoryBulkAdd): Promise<string[]> {
44
+ const { token } = this.sdk;
45
+ const url = this.sdk.getUrl(RouteBuilder.historyBatchCreate());
46
+ const body = JSON.stringify(info);
47
+ const result = await this.sdk.http.post(url.toString(), { token, body });
48
+ this.inspectCommonStatusCodes(result.status);
49
+ const E_PREFIX = 'Unable to create a bulk history. ';
50
+ if (result.status !== 200) {
51
+ this.logInvalidResponse(result);
52
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
53
+ }
54
+ if (!result.body) {
55
+ throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
56
+ }
57
+ let data: string[];
58
+ try {
59
+ data = JSON.parse(result.body);
60
+ } catch (e) {
61
+ throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
62
+ }
63
+ return data;
64
+ }
65
+
66
+ /**
67
+ * Lists the history.
68
+ *
69
+ * @param options Optional query options.
70
+ */
71
+ async list(options: HistoryListOptions): Promise<IListResponse> {
72
+ const { token } = this.sdk;
73
+ const url = this.sdk.getUrl(RouteBuilder.history());
74
+ this.sdk.appendListOptions(url, options);
75
+ const result = await this.sdk.http.get(url.toString(), { token });
76
+ this.inspectCommonStatusCodes(result.status);
77
+ const E_PREFIX = 'Unable to list history. ';
78
+ if (result.status !== 200) {
79
+ this.logInvalidResponse(result);
80
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
81
+ }
82
+ if (!result.body) {
83
+ throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
84
+ }
85
+ let data: IListResponse;
86
+ try {
87
+ data = JSON.parse(result.body);
88
+ } catch (e) {
89
+ throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
90
+ }
91
+ if (!Array.isArray(data.data)) {
92
+ throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
93
+ }
94
+ return data;
95
+ }
96
+
97
+ /**
98
+ * Deletes a history object form the store.
99
+ *
100
+ * @param key The key returned by the store when created the history. Also available via the `key` property on the history object.
101
+ */
102
+ delete(key: string): Promise<void>;
103
+ /**
104
+ * Deletes a list of history objects in a batch operation.
105
+ *
106
+ * @param keys The keys returned by the store when created the history. Also available via the `key` property on the history object.
107
+ */
108
+ delete(keys: string[]): Promise<void>;
109
+ /**
110
+ * Deletes a history or a list of history objects from the store.
111
+ * @param key A key or a list of keys.
112
+ */
113
+ async delete(key: string | string[]): Promise<void> {
114
+ const { token } = this.sdk;
115
+ const isArray = Array.isArray(key);
116
+ const path = isArray ? RouteBuilder.historyBatchDelete() : RouteBuilder.historyItem(key);
117
+ const url = this.sdk.getUrl(path);
118
+
119
+ const result = isArray ? await this.sdk.http.post(url.toString(), { token, body: JSON.stringify(key) }) : await this.sdk.http.delete(url.toString(), { token });
120
+ this.inspectCommonStatusCodes(result.status);
121
+ const E_PREFIX = 'Unable to delete history. ';
122
+ if (result.status !== 204) {
123
+ this.logInvalidResponse(result);
124
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Reads a history definition from the store.
130
+ * @param key The history key
131
+ * @returns The history object
132
+ */
133
+ async read(key: string): Promise<IHttpHistory> {
134
+ const { token } = this.sdk;
135
+ const url = this.sdk.getUrl(RouteBuilder.historyItem(key));
136
+ const result = await this.sdk.http.get(url.toString(), { token });
137
+ this.inspectCommonStatusCodes(result.status);
138
+ const E_PREFIX = 'Unable to read a history. ';
139
+ if (result.status !== 200) {
140
+ this.logInvalidResponse(result);
141
+ throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
142
+ }
143
+ if (!result.body) {
144
+ throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
145
+ }
146
+ let data: IHttpHistory;
147
+ try {
148
+ data = JSON.parse(result.body);
149
+ } catch (e) {
150
+ throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
151
+ }
152
+ if (data.kind !== HttpHistoryKind) {
153
+ throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
154
+ }
155
+ return data;
156
+ }
157
+ }
@@ -0,0 +1,18 @@
1
+ import { SdkBase, IStoreRequestOptions, IStoreResponse } from './SdkBase.js';
2
+
3
+ export abstract class Http extends SdkBase {
4
+ /**
5
+ * Performs the GET request.
6
+ *
7
+ * @param url The request URL
8
+ * @param opts The request options
9
+ * @returns The response info.
10
+ */
11
+ abstract get(url: string, opts?: IStoreRequestOptions): Promise<IStoreResponse>;
12
+
13
+ abstract post(url: string, opts?: IStoreRequestOptions): Promise<IStoreResponse>;
14
+
15
+ abstract patch(url: string, opts?: IStoreRequestOptions): Promise<IStoreResponse>;
16
+
17
+ abstract delete(url: string, opts?: IStoreRequestOptions): Promise<IStoreResponse>;
18
+ }
@@ -0,0 +1,100 @@
1
+ import http from 'http';
2
+ import https from 'https';
3
+ import { Headers } from '../../lib/headers/Headers.js';
4
+ import { Http } from './Http.js';
5
+ import { IStoreRequestOptions, IStoreResponse } from './SdkBase.js';
6
+
7
+ export class HttpNode extends Http {
8
+ /**
9
+ * Performs the GET request.
10
+ *
11
+ * @param url The request URL
12
+ * @param opts The request options
13
+ * @returns The response info.
14
+ */
15
+ async get(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
16
+ const result = await this._get(url, opts);
17
+ if ([301, 302, 308, 307].includes(result.status)) {
18
+ const loc = result.headers.get('location');
19
+ if (!loc) {
20
+ throw new Error('Expected redirection but no "location" header.')
21
+ }
22
+ const newUrl = new URL(loc, url);
23
+ return this.get(newUrl.toString(), opts);
24
+ }
25
+ return result;
26
+ }
27
+
28
+ private _get(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
29
+ return new Promise((resolve, reject) => {
30
+ const { method='GET', headers={}, token = this.sdk.token } = opts;
31
+ if (token) {
32
+ headers.authorization = `Bearer ${token}`;
33
+ }
34
+ const isSsl = url.startsWith('https:');
35
+ const lib = isSsl ? https : http;
36
+ // console.log(`${method} ${url}`);
37
+ const request = lib.request(url, {
38
+ method,
39
+ headers,
40
+ rejectUnauthorized: false,
41
+ });
42
+ request.on('response', (response) => {
43
+ const ro: IStoreResponse = {
44
+ status: response.statusCode as number,
45
+ headers: new Headers(response.headers),
46
+ body: '',
47
+ };
48
+ response.on('data', (chunk) => {
49
+ ro.body += chunk;
50
+ });
51
+ response.on('end', () => resolve(ro));
52
+ });
53
+ request.on('error', (error) => reject(error));
54
+ request.end();
55
+ });
56
+ }
57
+
58
+ post(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
59
+ return new Promise((resolve, reject) => {
60
+ const { method='POST', headers={}, token = this.sdk.token } = opts;
61
+ if (token) {
62
+ headers.authorization = `Bearer ${token}`;
63
+ }
64
+ const isSsl = url.startsWith('https:');
65
+ const lib = isSsl ? https : http;
66
+ const request = lib.request(url, {
67
+ method,
68
+ headers,
69
+ });
70
+ request.on('response', (response) => {
71
+ const ro: IStoreResponse = {
72
+ status: response.statusCode as number,
73
+ headers: new Headers(response.headers),
74
+ body: '',
75
+ };
76
+ response.on('data', (chunk) => {
77
+ ro.body += chunk;
78
+ });
79
+ response.on('end', () => resolve(ro));
80
+ });
81
+ request.on('error', (error) => reject(error));
82
+ if (opts.body) {
83
+ request.write(opts.body);
84
+ }
85
+ request.end();
86
+ });
87
+ }
88
+
89
+ patch(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
90
+ const options = { ...opts };
91
+ options.method = 'PATCH';
92
+ return this.post(url, options);
93
+ }
94
+
95
+ delete(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
96
+ const options = { ...opts };
97
+ options.method = 'DELETE';
98
+ return this.post(url, options);
99
+ }
100
+ }
@@ -0,0 +1,89 @@
1
+ import { Http } from './Http.js';
2
+ import { IStoreRequestOptions, IStoreResponse } from './SdkBase.js';
3
+ import { Headers } from '../../lib/headers/Headers.js';
4
+
5
+ export class HttpWeb extends Http {
6
+ /**
7
+ * Performs the GET request.
8
+ *
9
+ * @param url The request URL
10
+ * @param opts The request options
11
+ * @returns The response info.
12
+ */
13
+ async get(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
14
+ const { method='GET', headers={}, token = this.sdk.token } = opts;
15
+ if (token) {
16
+ headers.authorization = `Bearer ${token}`;
17
+ }
18
+ // eslint-disable-next-line no-undef
19
+ const init: RequestInit = {
20
+ method,
21
+ headers,
22
+ redirect: 'follow',
23
+ };
24
+ const response = await fetch(url, init);
25
+ let body: string | undefined;
26
+ try {
27
+ body = await response.text();
28
+ } catch (e) {
29
+ // ...
30
+ }
31
+
32
+ const responseHeaders = new Headers();
33
+ response.headers.forEach((value: string, key: string) => {
34
+ responseHeaders.append(key, value);
35
+ });
36
+
37
+ const result: IStoreResponse = {
38
+ status: response.status,
39
+ headers: responseHeaders,
40
+ body,
41
+ };
42
+ return result;
43
+ }
44
+
45
+ async post(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
46
+ const { method='POST', headers={}, token = this.sdk.token } = opts;
47
+ if (token) {
48
+ headers.authorization = `Bearer ${token}`;
49
+ }
50
+ // eslint-disable-next-line no-undef
51
+ const init: RequestInit = {
52
+ method,
53
+ headers,
54
+ redirect: 'follow',
55
+ };
56
+ if (opts.body) {
57
+ init.body = opts.body.toString();
58
+ }
59
+ const response = await fetch(url, init);
60
+ let body: string | undefined;
61
+ try {
62
+ body = await response.text();
63
+ } catch (e) {
64
+ // ...
65
+ }
66
+
67
+ const responseHeaders = new Headers();
68
+ response.headers.forEach((value: string, key: string) => {
69
+ responseHeaders.append(key, value);
70
+ });
71
+
72
+ const result: IStoreResponse = {
73
+ status: response.status,
74
+ headers: responseHeaders,
75
+ body,
76
+ };
77
+ return result;
78
+ }
79
+
80
+ patch(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
81
+ const options: IStoreRequestOptions = { ...opts, method: 'PATCH' };
82
+ return this.post(url, options);
83
+ }
84
+
85
+ delete(url: string, opts: IStoreRequestOptions = {}): Promise<IStoreResponse> {
86
+ const options: IStoreRequestOptions = { ...opts, method: 'DELETE' };
87
+ return this.post(url, options);
88
+ }
89
+ }