@api-client/core 0.4.0 → 0.4.3
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.
- package/build/browser.d.ts +6 -3
- package/build/browser.js +7 -2
- package/build/browser.js.map +1 -1
- package/build/index.d.ts +7 -5
- package/build/index.js +4 -3
- package/build/index.js.map +1 -1
- package/build/src/models/HttpHistory.d.ts +29 -0
- package/build/src/models/HttpHistory.js.map +1 -1
- package/build/src/models/HttpProject.d.ts +37 -1
- package/build/src/models/HttpProject.js +49 -1
- package/build/src/models/HttpProject.js.map +1 -1
- package/build/src/models/RequestLog.d.ts +8 -0
- package/build/src/models/RequestLog.js +14 -1
- package/build/src/models/RequestLog.js.map +1 -1
- package/build/src/runtime/node/InteropInterfaces.d.ts +115 -0
- package/build/src/runtime/node/InteropInterfaces.js +2 -0
- package/build/src/runtime/node/InteropInterfaces.js.map +1 -0
- package/build/src/runtime/node/ProjectParallelRunner.d.ts +2 -7
- package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -1
- package/build/src/runtime/node/ProjectRequestRunner.d.ts +17 -49
- package/build/src/runtime/node/ProjectRequestRunner.js +39 -10
- package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -1
- package/build/src/runtime/node/ProjectRunner.d.ts +1 -59
- package/build/src/runtime/node/ProjectRunner.js.map +1 -1
- package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -1
- package/build/src/runtime/store/AuthSdk.d.ts +28 -0
- package/build/src/runtime/store/AuthSdk.js +123 -0
- package/build/src/runtime/store/AuthSdk.js.map +1 -0
- package/build/src/runtime/store/BackendSdk.d.ts +8 -0
- package/build/src/runtime/store/BackendSdk.js +25 -0
- package/build/src/runtime/store/BackendSdk.js.map +1 -0
- package/build/src/runtime/store/HistorySdk.d.ts +48 -0
- package/build/src/runtime/store/HistorySdk.js +142 -0
- package/build/src/runtime/store/HistorySdk.js.map +1 -0
- package/build/src/runtime/store/Http.d.ts +14 -0
- package/build/src/runtime/store/Http.js +4 -0
- package/build/src/runtime/store/Http.js.map +1 -0
- package/build/src/runtime/store/HttpNode.d.ts +16 -0
- package/build/src/runtime/store/HttpNode.js +95 -0
- package/build/src/runtime/store/HttpNode.js.map +1 -0
- package/build/src/runtime/store/HttpWeb.d.ts +15 -0
- package/build/src/runtime/store/HttpWeb.js +83 -0
- package/build/src/runtime/store/HttpWeb.js.map +1 -0
- package/build/src/runtime/store/ProjectsSdk.d.ts +43 -0
- package/build/src/runtime/store/ProjectsSdk.js +144 -0
- package/build/src/runtime/store/ProjectsSdk.js.map +1 -0
- package/build/src/runtime/store/RouteBuilder.d.ts +42 -0
- package/build/src/runtime/store/RouteBuilder.js +75 -0
- package/build/src/runtime/store/RouteBuilder.js.map +1 -0
- package/build/src/runtime/store/Sdk.d.ts +79 -0
- package/build/src/runtime/store/Sdk.js +117 -0
- package/build/src/runtime/store/Sdk.js.map +1 -0
- package/build/src/runtime/store/SdkBase.d.ts +45 -0
- package/build/src/runtime/store/SdkBase.js +40 -0
- package/build/src/runtime/store/SdkBase.js.map +1 -0
- package/build/src/runtime/store/SpacesSdk.d.ts +50 -0
- package/build/src/runtime/store/SpacesSdk.js +185 -0
- package/build/src/runtime/store/SpacesSdk.js.map +1 -0
- package/build/src/runtime/store/StoreSdkNode.d.ts +10 -0
- package/build/src/runtime/store/StoreSdkNode.js +11 -0
- package/build/src/runtime/store/StoreSdkNode.js.map +1 -0
- package/build/src/runtime/store/StoreSdkWeb.d.ts +10 -0
- package/build/src/runtime/store/StoreSdkWeb.js +11 -0
- package/build/src/runtime/store/StoreSdkWeb.js.map +1 -0
- package/build/src/runtime/store/UsersSdk.d.ts +18 -0
- package/build/src/runtime/store/UsersSdk.js +88 -0
- package/build/src/runtime/store/UsersSdk.js.map +1 -0
- package/build/src/runtime/store/WsClient.d.ts +24 -0
- package/build/src/runtime/store/WsClient.js +4 -0
- package/build/src/runtime/store/WsClient.js.map +1 -0
- package/build/src/runtime/store/WsClientNode.d.ts +24 -0
- package/build/src/runtime/store/WsClientNode.js +66 -0
- package/build/src/runtime/store/WsClientNode.js.map +1 -0
- package/build/src/runtime/store/WsClientWeb.d.ts +23 -0
- package/build/src/runtime/store/WsClientWeb.js +73 -0
- package/build/src/runtime/store/WsClientWeb.js.map +1 -0
- package/package.json +4 -6
- package/src/models/HttpHistory.ts +30 -0
- package/src/models/HttpProject.ts +82 -1
- package/src/models/RequestLog.ts +17 -1
- package/src/runtime/node/InteropInterfaces.ts +121 -0
- package/src/runtime/node/ProjectParallelRunner.ts +2 -9
- package/src/runtime/node/ProjectRequestRunner.ts +42 -61
- package/src/runtime/node/ProjectRunner.ts +1 -60
- package/src/runtime/node/ProjectRunnerWorker.ts +2 -1
- package/src/runtime/store/AuthSdk.ts +126 -0
- package/src/runtime/store/BackendSdk.ts +25 -0
- package/src/runtime/store/HistorySdk.ts +157 -0
- package/src/runtime/store/Http.ts +18 -0
- package/src/runtime/store/HttpNode.ts +100 -0
- package/src/runtime/store/HttpWeb.ts +89 -0
- package/src/runtime/store/ProjectsSdk.ts +147 -0
- package/src/runtime/store/RouteBuilder.ts +89 -0
- package/src/runtime/store/Sdk.ts +131 -0
- package/src/runtime/store/SdkBase.ts +72 -0
- package/src/runtime/store/SpacesSdk.ts +190 -0
- package/src/runtime/store/StoreSdkNode.ts +13 -0
- package/src/runtime/store/StoreSdkWeb.ts +13 -0
- package/src/runtime/store/UsersSdk.ts +89 -0
- package/src/runtime/store/WsClient.ts +28 -0
- package/src/runtime/store/WsClientNode.ts +68 -0
- package/src/runtime/store/WsClientWeb.ts +75 -0
- package/build/src/models/HistoryIndex.d.ts +0 -45
- package/build/src/models/HistoryIndex.js +0 -56
- package/build/src/models/HistoryIndex.js.map +0 -1
- package/build/src/runtime/store/StoreSdk.d.ts +0 -279
- package/build/src/runtime/store/StoreSdk.js +0 -842
- package/build/src/runtime/store/StoreSdk.js.map +0 -1
- package/src/models/HistoryIndex.ts +0 -81
- package/src/runtime/store/StoreSdk.ts +0 -895
|
@@ -1,842 +0,0 @@
|
|
|
1
|
-
import http from 'http';
|
|
2
|
-
import https from 'https';
|
|
3
|
-
import WebSocket from 'ws';
|
|
4
|
-
import { URL } from 'url';
|
|
5
|
-
import { Kind as WorkspaceKind } from '../../models/Workspace.js';
|
|
6
|
-
import { Kind as HttpProjectKind } from '../../models/HttpProject.js';
|
|
7
|
-
const E_INVALID_JSON = 'The response is not a valid JSON.';
|
|
8
|
-
const E_RESPONSE_NO_VALUE = 'The response has no value.';
|
|
9
|
-
const E_RESPONSE_STATUS = 'Invalid response status: ';
|
|
10
|
-
const E_RESPONSE_UNKNOWN = 'The response has unknown format.';
|
|
11
|
-
const E_RESPONSE_LOCATION = 'The response has no "location" header.';
|
|
12
|
-
const baseUriSymbol = Symbol('baseUri');
|
|
13
|
-
/**
|
|
14
|
-
* NodeJS API for API Client's net-store module.
|
|
15
|
-
*/
|
|
16
|
-
export class StoreSdk {
|
|
17
|
-
basePath;
|
|
18
|
-
/**
|
|
19
|
-
* The token to use with requests. Can be overwritten with each API call.
|
|
20
|
-
*/
|
|
21
|
-
token;
|
|
22
|
-
/**
|
|
23
|
-
* The user authentication features.
|
|
24
|
-
*/
|
|
25
|
-
auth = new AuthSdk(this);
|
|
26
|
-
/**
|
|
27
|
-
* The store information features
|
|
28
|
-
*/
|
|
29
|
-
store = new BackendSdk(this);
|
|
30
|
-
/**
|
|
31
|
-
* The user spaces features.
|
|
32
|
-
*/
|
|
33
|
-
space = new SpacesSdk(this);
|
|
34
|
-
/**
|
|
35
|
-
* The user projects features.
|
|
36
|
-
*/
|
|
37
|
-
project = new ProjectsSdk(this);
|
|
38
|
-
/**
|
|
39
|
-
* Reads user information.
|
|
40
|
-
*/
|
|
41
|
-
user = new UsersSdk(this);
|
|
42
|
-
/**
|
|
43
|
-
* The HTTP(S) requests.
|
|
44
|
-
*/
|
|
45
|
-
http = new HttpClient(this);
|
|
46
|
-
/**
|
|
47
|
-
* The web socket requests.
|
|
48
|
-
*/
|
|
49
|
-
ws = new WsClient(this);
|
|
50
|
-
[baseUriSymbol] = '';
|
|
51
|
-
/**
|
|
52
|
-
* @returns The base URI to the store.
|
|
53
|
-
*/
|
|
54
|
-
get baseUri() {
|
|
55
|
-
return this[baseUriSymbol];
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* @param value The base URI to the store.
|
|
59
|
-
*/
|
|
60
|
-
set baseUri(value) {
|
|
61
|
-
if (!value) {
|
|
62
|
-
throw new Error(`Invalid value for the baseUri`);
|
|
63
|
-
}
|
|
64
|
-
let url;
|
|
65
|
-
try {
|
|
66
|
-
url = new URL(value);
|
|
67
|
-
}
|
|
68
|
-
catch (e) {
|
|
69
|
-
throw new Error(`Invalid baseUri: Not an URL`);
|
|
70
|
-
}
|
|
71
|
-
this[baseUriSymbol] = url.origin;
|
|
72
|
-
const { pathname } = url;
|
|
73
|
-
if (pathname && pathname !== '/') {
|
|
74
|
-
this.basePath = pathname;
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
this.basePath = undefined;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* @param baseUri The base URI to the store.
|
|
82
|
-
* @param basePath The base path (the router prefix) for the server.
|
|
83
|
-
*/
|
|
84
|
-
constructor(baseUri, basePath) {
|
|
85
|
-
this.basePath = basePath;
|
|
86
|
-
this.baseUri = baseUri;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Creates a full URL for a path.
|
|
90
|
-
* This adds the server's base path and the passed path to the configured base URL.
|
|
91
|
-
* @param path The path to the endpoint, without the base path.
|
|
92
|
-
*/
|
|
93
|
-
getUrl(path = '/') {
|
|
94
|
-
const { baseUri, basePath = '' } = this;
|
|
95
|
-
let userPath = path;
|
|
96
|
-
if (basePath && userPath.startsWith(basePath)) {
|
|
97
|
-
userPath = userPath.substring(basePath.length);
|
|
98
|
-
}
|
|
99
|
-
return new URL(`${basePath}${userPath}`, baseUri);
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Appends list options to the query parameters.
|
|
103
|
-
* @param url The URL object to the API endpoint
|
|
104
|
-
* @param options The list options, if any
|
|
105
|
-
*/
|
|
106
|
-
appendListOptions(url, options = {}) {
|
|
107
|
-
const { searchParams } = url;
|
|
108
|
-
if (options.cursor) {
|
|
109
|
-
searchParams.set('cursor', options.cursor);
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
if (typeof options.limit === 'number') {
|
|
113
|
-
searchParams.set('limit', String(options.limit));
|
|
114
|
-
}
|
|
115
|
-
if (options.query) {
|
|
116
|
-
searchParams.set('query', options.query);
|
|
117
|
-
}
|
|
118
|
-
if (Array.isArray(options.queryField)) {
|
|
119
|
-
options.queryField.forEach((field) => {
|
|
120
|
-
searchParams.append('queryField', field);
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
class SdkBase {
|
|
127
|
-
sdk;
|
|
128
|
-
constructor(sdk) {
|
|
129
|
-
this.sdk = sdk;
|
|
130
|
-
}
|
|
131
|
-
logInvalidResponse(response) {
|
|
132
|
-
if (response.body) {
|
|
133
|
-
try {
|
|
134
|
-
const data = JSON.parse(response.body);
|
|
135
|
-
if (data.message) {
|
|
136
|
-
console.warn(`[Store message]: ${data.message}`);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
catch (e) {
|
|
140
|
-
// .
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Throws unified message for a common error status codes.
|
|
146
|
-
* It handles 404, 403, and 401 status codes.
|
|
147
|
-
*/
|
|
148
|
-
inspectCommonStatusCodes(status) {
|
|
149
|
-
if (status === 404) {
|
|
150
|
-
throw new Error(`Not found.`);
|
|
151
|
-
}
|
|
152
|
-
if (status === 403) {
|
|
153
|
-
throw new Error(`You have no access to this resource.`);
|
|
154
|
-
}
|
|
155
|
-
if (status === 401) {
|
|
156
|
-
throw new Error(`Not authorized.`);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
class AuthSdk extends SdkBase {
|
|
161
|
-
getExpires(headers) {
|
|
162
|
-
if (!headers.expires) {
|
|
163
|
-
return undefined;
|
|
164
|
-
}
|
|
165
|
-
const d = new Date(headers.expires);
|
|
166
|
-
const time = d.getTime();
|
|
167
|
-
if (Number.isNaN(time)) {
|
|
168
|
-
console.warn(`Invalid session response: the expires header cannot be parsed.`);
|
|
169
|
-
return undefined;
|
|
170
|
-
}
|
|
171
|
-
return time;
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Creates unauthenticated session in the backend.
|
|
175
|
-
* @returns The JWT for unauthenticated user.
|
|
176
|
-
*/
|
|
177
|
-
async createSession() {
|
|
178
|
-
const url = this.sdk.getUrl('/sessions');
|
|
179
|
-
// console.log('Create session: ', url);
|
|
180
|
-
const result = await this.sdk.http.post(url.toString());
|
|
181
|
-
this.inspectCommonStatusCodes(result.status);
|
|
182
|
-
if (result.status !== 200) {
|
|
183
|
-
throw new Error(`Unable to create the session. Invalid response status: ${result.status}`);
|
|
184
|
-
}
|
|
185
|
-
if (!result.body) {
|
|
186
|
-
throw new Error(`Unable to create the session. Response has no token.`);
|
|
187
|
-
}
|
|
188
|
-
const info = {
|
|
189
|
-
token: result.body,
|
|
190
|
-
};
|
|
191
|
-
const expires = this.getExpires(result.headers);
|
|
192
|
-
if (expires) {
|
|
193
|
-
info.expires = expires;
|
|
194
|
-
}
|
|
195
|
-
return info;
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Initializes the authentication session.
|
|
199
|
-
* @param token The unauthenticated session JWT. Required when not set on the class.
|
|
200
|
-
* @returns The location of the authorization endpoint.
|
|
201
|
-
*/
|
|
202
|
-
async createAuthSession(token, loginPath = '/auth/login') {
|
|
203
|
-
const url = this.sdk.getUrl(loginPath);
|
|
204
|
-
const result = await this.sdk.http.post(url.toString(), { token });
|
|
205
|
-
this.inspectCommonStatusCodes(result.status);
|
|
206
|
-
const loc = result.headers.location;
|
|
207
|
-
if (!loc) {
|
|
208
|
-
throw new Error(`The location header not returned by the server.`);
|
|
209
|
-
}
|
|
210
|
-
return loc;
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Listens to the first message coming to the client from the auth endpoint.
|
|
214
|
-
* @param authPath The authorization path returned by the server info or 401 response.
|
|
215
|
-
* @param token Optional token to use.
|
|
216
|
-
*/
|
|
217
|
-
async listenAuth(authPath, token) {
|
|
218
|
-
const url = this.sdk.getUrl(authPath);
|
|
219
|
-
const client = await this.sdk.ws.createAndConnect(url.toString(), token);
|
|
220
|
-
return new Promise((resolve, reject) => {
|
|
221
|
-
client.on('message', (data) => {
|
|
222
|
-
const message = JSON.parse(data.toString());
|
|
223
|
-
this.sdk.ws.disconnect(client)
|
|
224
|
-
.then(() => {
|
|
225
|
-
if (message.status === 'OK') {
|
|
226
|
-
resolve();
|
|
227
|
-
}
|
|
228
|
-
else {
|
|
229
|
-
reject(new Error(message.message || 'Unknown error'));
|
|
230
|
-
}
|
|
231
|
-
}).catch(cause => reject(cause));
|
|
232
|
-
});
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Renews authenticated token to a new one when the token expires.
|
|
237
|
-
* @param token Optional token to use.
|
|
238
|
-
* @returns
|
|
239
|
-
*/
|
|
240
|
-
async renewToken(token = this.sdk.token) {
|
|
241
|
-
const authPath = '/sessions/renew';
|
|
242
|
-
const url = this.sdk.getUrl(authPath);
|
|
243
|
-
const result = await this.sdk.http.post(url.toString(), { token });
|
|
244
|
-
this.inspectCommonStatusCodes(result.status);
|
|
245
|
-
if (result.status !== 200) {
|
|
246
|
-
throw new Error(`Unable to renew the token. Invalid response status: ${result.status}`);
|
|
247
|
-
}
|
|
248
|
-
if (!result.body) {
|
|
249
|
-
throw new Error(`Unable to create the session. Response has no token.`);
|
|
250
|
-
}
|
|
251
|
-
const info = {
|
|
252
|
-
token: result.body,
|
|
253
|
-
};
|
|
254
|
-
const expires = this.getExpires(result.headers);
|
|
255
|
-
if (expires) {
|
|
256
|
-
info.expires = expires;
|
|
257
|
-
}
|
|
258
|
-
return info;
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
class BackendSdk extends SdkBase {
|
|
262
|
-
/**
|
|
263
|
-
* @returns Client information about the store configuration.
|
|
264
|
-
*/
|
|
265
|
-
async getInfo() {
|
|
266
|
-
const url = this.sdk.getUrl('/store');
|
|
267
|
-
const result = await this.sdk.http.get(url.toString());
|
|
268
|
-
this.inspectCommonStatusCodes(result.status);
|
|
269
|
-
if (result.status !== 200) {
|
|
270
|
-
throw new Error(`Invalid store response. Expected 200 status and ${result.status} received.`);
|
|
271
|
-
}
|
|
272
|
-
const body = result.body;
|
|
273
|
-
let data;
|
|
274
|
-
try {
|
|
275
|
-
data = JSON.parse(body);
|
|
276
|
-
}
|
|
277
|
-
catch (e) {
|
|
278
|
-
throw new Error(`The server returned invalid response. Unable to read store status.`);
|
|
279
|
-
}
|
|
280
|
-
return data;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
class SpacesSdk extends SdkBase {
|
|
284
|
-
/**
|
|
285
|
-
* Lists spaces in the store.
|
|
286
|
-
* @param options Optional query options.
|
|
287
|
-
*/
|
|
288
|
-
async list(options) {
|
|
289
|
-
const { token } = this.sdk;
|
|
290
|
-
const url = this.sdk.getUrl('/spaces');
|
|
291
|
-
this.sdk.appendListOptions(url, options);
|
|
292
|
-
const result = await this.sdk.http.get(url.toString(), { token });
|
|
293
|
-
this.inspectCommonStatusCodes(result.status);
|
|
294
|
-
const E_PREFIX = 'Unable to list spaces. ';
|
|
295
|
-
if (result.status !== 200) {
|
|
296
|
-
this.logInvalidResponse(result);
|
|
297
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
|
|
298
|
-
}
|
|
299
|
-
if (!result.body) {
|
|
300
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
|
|
301
|
-
}
|
|
302
|
-
let data;
|
|
303
|
-
try {
|
|
304
|
-
data = JSON.parse(result.body);
|
|
305
|
-
}
|
|
306
|
-
catch (e) {
|
|
307
|
-
throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
|
|
308
|
-
}
|
|
309
|
-
if (!Array.isArray(data.data)) {
|
|
310
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
|
|
311
|
-
}
|
|
312
|
-
return data;
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Creates a workspace in the store.
|
|
316
|
-
* @param space The workspace definition.
|
|
317
|
-
* @returns The key of the creates space.
|
|
318
|
-
*/
|
|
319
|
-
async create(space) {
|
|
320
|
-
const { token } = this.sdk;
|
|
321
|
-
const url = this.sdk.getUrl('/spaces');
|
|
322
|
-
const body = JSON.stringify(space);
|
|
323
|
-
const result = await this.sdk.http.post(url.toString(), { token, body });
|
|
324
|
-
this.inspectCommonStatusCodes(result.status);
|
|
325
|
-
const E_PREFIX = 'Unable to create a user space. ';
|
|
326
|
-
if (result.status !== 204) {
|
|
327
|
-
this.logInvalidResponse(result);
|
|
328
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
|
|
329
|
-
}
|
|
330
|
-
const { location } = result.headers;
|
|
331
|
-
if (!location) {
|
|
332
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_LOCATION}`);
|
|
333
|
-
}
|
|
334
|
-
const id = location.split('/').pop();
|
|
335
|
-
return id;
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Reads a user space definition from the store.
|
|
339
|
-
* @param key The user space key
|
|
340
|
-
* @returns The definition of the user space.
|
|
341
|
-
*/
|
|
342
|
-
async read(key) {
|
|
343
|
-
const { token } = this.sdk;
|
|
344
|
-
const url = this.sdk.getUrl(`/spaces/${key}`);
|
|
345
|
-
const result = await this.sdk.http.get(url.toString(), { token });
|
|
346
|
-
this.inspectCommonStatusCodes(result.status);
|
|
347
|
-
const E_PREFIX = 'Unable to read a user space. ';
|
|
348
|
-
if (result.status !== 200) {
|
|
349
|
-
this.logInvalidResponse(result);
|
|
350
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
|
|
351
|
-
}
|
|
352
|
-
if (!result.body) {
|
|
353
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
|
|
354
|
-
}
|
|
355
|
-
let data;
|
|
356
|
-
try {
|
|
357
|
-
data = JSON.parse(result.body);
|
|
358
|
-
}
|
|
359
|
-
catch (e) {
|
|
360
|
-
throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
|
|
361
|
-
}
|
|
362
|
-
if (data.kind !== WorkspaceKind) {
|
|
363
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
|
|
364
|
-
}
|
|
365
|
-
return data;
|
|
366
|
-
}
|
|
367
|
-
/**
|
|
368
|
-
* Patches a user space in the store.
|
|
369
|
-
* @param key The key of the user space to patch
|
|
370
|
-
* @param value The JSON patch to be processed.
|
|
371
|
-
* @returns The JSON patch to revert the change using the `json8-patch` library
|
|
372
|
-
*/
|
|
373
|
-
async patch(key, value) {
|
|
374
|
-
const { token } = this.sdk;
|
|
375
|
-
const url = this.sdk.getUrl(`/spaces/${key}`);
|
|
376
|
-
const body = JSON.stringify(value);
|
|
377
|
-
const result = await this.sdk.http.patch(url.toString(), { token, body });
|
|
378
|
-
this.inspectCommonStatusCodes(result.status);
|
|
379
|
-
const E_PREFIX = 'Unable to patch a user space. ';
|
|
380
|
-
if (result.status !== 200) {
|
|
381
|
-
this.logInvalidResponse(result);
|
|
382
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
|
|
383
|
-
}
|
|
384
|
-
if (!result.body) {
|
|
385
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
|
|
386
|
-
}
|
|
387
|
-
let data;
|
|
388
|
-
try {
|
|
389
|
-
data = JSON.parse(result.body);
|
|
390
|
-
}
|
|
391
|
-
catch (e) {
|
|
392
|
-
throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
|
|
393
|
-
}
|
|
394
|
-
if (!data.revert) {
|
|
395
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
|
|
396
|
-
}
|
|
397
|
-
return data.revert;
|
|
398
|
-
}
|
|
399
|
-
/**
|
|
400
|
-
* Deletes the space in the store.
|
|
401
|
-
*
|
|
402
|
-
* @param key The key of the space to delete.
|
|
403
|
-
*/
|
|
404
|
-
async delete(key) {
|
|
405
|
-
const { token } = this.sdk;
|
|
406
|
-
const url = this.sdk.getUrl(`/spaces/${key}`);
|
|
407
|
-
const result = await this.sdk.http.delete(url.toString(), { token });
|
|
408
|
-
this.inspectCommonStatusCodes(result.status);
|
|
409
|
-
const E_PREFIX = 'Unable to delete a user space. ';
|
|
410
|
-
if (result.status !== 204) {
|
|
411
|
-
this.logInvalidResponse(result);
|
|
412
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
* Updates the sharing options of the space.
|
|
417
|
-
*
|
|
418
|
-
* @param key The user space key
|
|
419
|
-
* @param value The patch operation on the space's ACL
|
|
420
|
-
*/
|
|
421
|
-
async patchUsers(key, value) {
|
|
422
|
-
const { token } = this.sdk;
|
|
423
|
-
const url = this.sdk.getUrl(`/spaces/${key}/users`);
|
|
424
|
-
const body = JSON.stringify(value);
|
|
425
|
-
const result = await this.sdk.http.patch(url.toString(), { token, body });
|
|
426
|
-
this.inspectCommonStatusCodes(result.status);
|
|
427
|
-
const E_PREFIX = 'Unable to patch a user space. ';
|
|
428
|
-
if (result.status !== 204) {
|
|
429
|
-
this.logInvalidResponse(result);
|
|
430
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
/**
|
|
434
|
-
* Lists uses having access to the user space.
|
|
435
|
-
*
|
|
436
|
-
* @param key The user space key
|
|
437
|
-
*/
|
|
438
|
-
async listUsers(key) {
|
|
439
|
-
const { token } = this.sdk;
|
|
440
|
-
const url = this.sdk.getUrl(`/spaces/${key}/users`);
|
|
441
|
-
const result = await this.sdk.http.get(url.toString(), { token });
|
|
442
|
-
this.inspectCommonStatusCodes(result.status);
|
|
443
|
-
const E_PREFIX = 'Unable to list users in the space. ';
|
|
444
|
-
if (result.status !== 200) {
|
|
445
|
-
this.logInvalidResponse(result);
|
|
446
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
|
|
447
|
-
}
|
|
448
|
-
if (!result.body) {
|
|
449
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
|
|
450
|
-
}
|
|
451
|
-
let data;
|
|
452
|
-
try {
|
|
453
|
-
data = JSON.parse(result.body);
|
|
454
|
-
}
|
|
455
|
-
catch (e) {
|
|
456
|
-
throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
|
|
457
|
-
}
|
|
458
|
-
if (!Array.isArray(data.data)) {
|
|
459
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
|
|
460
|
-
}
|
|
461
|
-
return data;
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
class HttpClient extends SdkBase {
|
|
465
|
-
/**
|
|
466
|
-
* Performs the GET request.
|
|
467
|
-
*
|
|
468
|
-
* @param url The request URL
|
|
469
|
-
* @param opts The request options
|
|
470
|
-
* @returns The response info.
|
|
471
|
-
*/
|
|
472
|
-
async get(url, opts = {}) {
|
|
473
|
-
const result = await this._get(url, opts);
|
|
474
|
-
if ([301, 302, 308, 307].includes(result.status)) {
|
|
475
|
-
const loc = result.headers.location;
|
|
476
|
-
if (!loc) {
|
|
477
|
-
throw new Error('Expected redirection but no "location" header.');
|
|
478
|
-
}
|
|
479
|
-
const newUrl = new URL(loc, url);
|
|
480
|
-
return this.get(newUrl.toString(), opts);
|
|
481
|
-
}
|
|
482
|
-
return result;
|
|
483
|
-
}
|
|
484
|
-
_get(url, opts = {}) {
|
|
485
|
-
return new Promise((resolve, reject) => {
|
|
486
|
-
const { method = 'GET', headers = {}, token = this.sdk.token } = opts;
|
|
487
|
-
if (token) {
|
|
488
|
-
headers.authorization = `Bearer ${token}`;
|
|
489
|
-
}
|
|
490
|
-
const isSsl = url.startsWith('https:');
|
|
491
|
-
const lib = isSsl ? https : http;
|
|
492
|
-
// console.log(`${method} ${url}`);
|
|
493
|
-
const request = lib.request(url, {
|
|
494
|
-
method,
|
|
495
|
-
headers,
|
|
496
|
-
rejectUnauthorized: false,
|
|
497
|
-
});
|
|
498
|
-
request.on('response', (response) => {
|
|
499
|
-
const ro = {
|
|
500
|
-
status: response.statusCode,
|
|
501
|
-
headers: response.headers,
|
|
502
|
-
body: '',
|
|
503
|
-
};
|
|
504
|
-
response.on('data', (chunk) => {
|
|
505
|
-
ro.body += chunk;
|
|
506
|
-
});
|
|
507
|
-
response.on('end', () => resolve(ro));
|
|
508
|
-
});
|
|
509
|
-
request.on('error', (error) => reject(error));
|
|
510
|
-
request.end();
|
|
511
|
-
});
|
|
512
|
-
}
|
|
513
|
-
post(url, opts = {}) {
|
|
514
|
-
return new Promise((resolve, reject) => {
|
|
515
|
-
const { method = 'POST', headers = {}, token = this.sdk.token } = opts;
|
|
516
|
-
if (token) {
|
|
517
|
-
headers.authorization = `Bearer ${token}`;
|
|
518
|
-
}
|
|
519
|
-
const isSsl = url.startsWith('https:');
|
|
520
|
-
const lib = isSsl ? https : http;
|
|
521
|
-
const request = lib.request(url, {
|
|
522
|
-
method,
|
|
523
|
-
headers,
|
|
524
|
-
});
|
|
525
|
-
request.on('response', (response) => {
|
|
526
|
-
const ro = {
|
|
527
|
-
status: response.statusCode,
|
|
528
|
-
headers: response.headers,
|
|
529
|
-
body: '',
|
|
530
|
-
};
|
|
531
|
-
response.on('data', (chunk) => {
|
|
532
|
-
ro.body += chunk;
|
|
533
|
-
});
|
|
534
|
-
response.on('end', () => resolve(ro));
|
|
535
|
-
});
|
|
536
|
-
request.on('error', (error) => reject(error));
|
|
537
|
-
if (opts.body) {
|
|
538
|
-
request.write(opts.body);
|
|
539
|
-
}
|
|
540
|
-
request.end();
|
|
541
|
-
});
|
|
542
|
-
}
|
|
543
|
-
patch(url, opts = {}) {
|
|
544
|
-
const options = { ...opts };
|
|
545
|
-
options.method = 'PATCH';
|
|
546
|
-
return this.post(url, options);
|
|
547
|
-
}
|
|
548
|
-
delete(url, opts = {}) {
|
|
549
|
-
const options = { ...opts };
|
|
550
|
-
options.method = 'DELETE';
|
|
551
|
-
return this.post(url, options);
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
class WsClient extends SdkBase {
|
|
555
|
-
/**
|
|
556
|
-
* Creates a WS client with optional token
|
|
557
|
-
* @param addr The ws:// address
|
|
558
|
-
* @param token Optional token to add.
|
|
559
|
-
*/
|
|
560
|
-
getClient(addr, token = this.sdk.token) {
|
|
561
|
-
let url = addr;
|
|
562
|
-
if (token) {
|
|
563
|
-
url += url.includes('?') ? '&' : '?';
|
|
564
|
-
url += 'token=';
|
|
565
|
-
url += token;
|
|
566
|
-
}
|
|
567
|
-
if (url.startsWith('http:')) {
|
|
568
|
-
url = `ws:${url.substring(5)}`;
|
|
569
|
-
}
|
|
570
|
-
else if (url.startsWith('https:')) {
|
|
571
|
-
url = `wss:${url.substring(6)}`;
|
|
572
|
-
}
|
|
573
|
-
return new WebSocket(url);
|
|
574
|
-
}
|
|
575
|
-
/**
|
|
576
|
-
* Connect to the WS server
|
|
577
|
-
*
|
|
578
|
-
* @param client The client to wait for connection.
|
|
579
|
-
*/
|
|
580
|
-
connect(client) {
|
|
581
|
-
return new Promise((resolve, reject) => {
|
|
582
|
-
client.once('open', () => {
|
|
583
|
-
client.removeAllListeners('error');
|
|
584
|
-
resolve();
|
|
585
|
-
});
|
|
586
|
-
client.once('error', (err) => {
|
|
587
|
-
client.removeAllListeners('open');
|
|
588
|
-
reject(err);
|
|
589
|
-
});
|
|
590
|
-
});
|
|
591
|
-
}
|
|
592
|
-
/**
|
|
593
|
-
* Disconnects from the WS server.
|
|
594
|
-
*/
|
|
595
|
-
disconnect(client) {
|
|
596
|
-
return new Promise((resolve, reject) => {
|
|
597
|
-
client.once('close', () => {
|
|
598
|
-
client.removeAllListeners('error');
|
|
599
|
-
resolve();
|
|
600
|
-
});
|
|
601
|
-
client.once('error', (err) => {
|
|
602
|
-
client.removeAllListeners('close');
|
|
603
|
-
reject(err);
|
|
604
|
-
});
|
|
605
|
-
client.close();
|
|
606
|
-
});
|
|
607
|
-
}
|
|
608
|
-
/**
|
|
609
|
-
* The combination of `getClient()` and `connect()`.
|
|
610
|
-
*/
|
|
611
|
-
async createAndConnect(addr, token) {
|
|
612
|
-
const client = this.getClient(addr, token);
|
|
613
|
-
await this.connect(client);
|
|
614
|
-
return client;
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
class ProjectsSdk extends SdkBase {
|
|
618
|
-
/**
|
|
619
|
-
* Creates a project in a user space.
|
|
620
|
-
*
|
|
621
|
-
* @param key The user space key
|
|
622
|
-
* @param project THe project to create
|
|
623
|
-
* @returns The key of the created project.
|
|
624
|
-
*/
|
|
625
|
-
async create(key, project) {
|
|
626
|
-
const { token } = this.sdk;
|
|
627
|
-
const url = this.sdk.getUrl(`/spaces/${key}/projects`);
|
|
628
|
-
const body = JSON.stringify(project);
|
|
629
|
-
const result = await this.sdk.http.post(url.toString(), { token, body });
|
|
630
|
-
this.inspectCommonStatusCodes(result.status);
|
|
631
|
-
const E_PREFIX = 'Unable to create a project. ';
|
|
632
|
-
if (result.status !== 204) {
|
|
633
|
-
this.logInvalidResponse(result);
|
|
634
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
|
|
635
|
-
}
|
|
636
|
-
const { location } = result.headers;
|
|
637
|
-
if (!location) {
|
|
638
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_LOCATION}`);
|
|
639
|
-
}
|
|
640
|
-
const id = location.split('/').pop();
|
|
641
|
-
return id;
|
|
642
|
-
}
|
|
643
|
-
/**
|
|
644
|
-
* Reads a project definition from the store.
|
|
645
|
-
* @param space The user space key
|
|
646
|
-
* @param project The project key
|
|
647
|
-
* @returns The definition of the project.
|
|
648
|
-
*/
|
|
649
|
-
async read(space, project) {
|
|
650
|
-
const { token } = this.sdk;
|
|
651
|
-
const url = this.sdk.getUrl(`/spaces/${space}/projects/${project}`);
|
|
652
|
-
const result = await this.sdk.http.get(url.toString(), { token });
|
|
653
|
-
this.inspectCommonStatusCodes(result.status);
|
|
654
|
-
const E_PREFIX = 'Unable to read a project. ';
|
|
655
|
-
if (result.status !== 200) {
|
|
656
|
-
this.logInvalidResponse(result);
|
|
657
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
|
|
658
|
-
}
|
|
659
|
-
if (!result.body) {
|
|
660
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
|
|
661
|
-
}
|
|
662
|
-
let data;
|
|
663
|
-
try {
|
|
664
|
-
data = JSON.parse(result.body);
|
|
665
|
-
}
|
|
666
|
-
catch (e) {
|
|
667
|
-
throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
|
|
668
|
-
}
|
|
669
|
-
if (data.kind !== HttpProjectKind) {
|
|
670
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
|
|
671
|
-
}
|
|
672
|
-
return data;
|
|
673
|
-
}
|
|
674
|
-
/**
|
|
675
|
-
* Lists projects in the space
|
|
676
|
-
*
|
|
677
|
-
* @param space The user space key
|
|
678
|
-
* @param options Optional query options.
|
|
679
|
-
*/
|
|
680
|
-
async list(space, options) {
|
|
681
|
-
const { token } = this.sdk;
|
|
682
|
-
const url = this.sdk.getUrl(`/spaces/${space}/projects`);
|
|
683
|
-
this.sdk.appendListOptions(url, options);
|
|
684
|
-
const result = await this.sdk.http.get(url.toString(), { token });
|
|
685
|
-
this.inspectCommonStatusCodes(result.status);
|
|
686
|
-
const E_PREFIX = 'Unable to list projects. ';
|
|
687
|
-
if (result.status !== 200) {
|
|
688
|
-
this.logInvalidResponse(result);
|
|
689
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
|
|
690
|
-
}
|
|
691
|
-
if (!result.body) {
|
|
692
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
|
|
693
|
-
}
|
|
694
|
-
let data;
|
|
695
|
-
try {
|
|
696
|
-
data = JSON.parse(result.body);
|
|
697
|
-
}
|
|
698
|
-
catch (e) {
|
|
699
|
-
throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
|
|
700
|
-
}
|
|
701
|
-
if (!Array.isArray(data.data)) {
|
|
702
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
|
|
703
|
-
}
|
|
704
|
-
return data;
|
|
705
|
-
}
|
|
706
|
-
/**
|
|
707
|
-
* Deletes a project form the store.
|
|
708
|
-
*
|
|
709
|
-
* @param space The key of the parent space.
|
|
710
|
-
* @param project The key of the project to delete.
|
|
711
|
-
*/
|
|
712
|
-
async delete(space, project) {
|
|
713
|
-
const { token } = this.sdk;
|
|
714
|
-
const url = this.sdk.getUrl(`/spaces/${space}/projects/${project}`);
|
|
715
|
-
const result = await this.sdk.http.delete(url.toString(), { token });
|
|
716
|
-
this.inspectCommonStatusCodes(result.status);
|
|
717
|
-
const E_PREFIX = 'Unable to delete a project. ';
|
|
718
|
-
if (result.status !== 204) {
|
|
719
|
-
this.logInvalidResponse(result);
|
|
720
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
/**
|
|
724
|
-
* Patches a project in the store.
|
|
725
|
-
* @param space The key of the parent user space
|
|
726
|
-
* @param project The key of project to patch.
|
|
727
|
-
* @param value The JSON patch to be processed.
|
|
728
|
-
* @returns The JSON patch to revert the change using the `json8-patch` library
|
|
729
|
-
*/
|
|
730
|
-
async patch(space, project, value) {
|
|
731
|
-
const { token } = this.sdk;
|
|
732
|
-
const url = this.sdk.getUrl(`/spaces/${space}/projects/${project}`);
|
|
733
|
-
const body = JSON.stringify(value);
|
|
734
|
-
const result = await this.sdk.http.patch(url.toString(), { token, body });
|
|
735
|
-
this.inspectCommonStatusCodes(result.status);
|
|
736
|
-
const E_PREFIX = 'Unable to patch a project. ';
|
|
737
|
-
if (result.status !== 200) {
|
|
738
|
-
this.logInvalidResponse(result);
|
|
739
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
|
|
740
|
-
}
|
|
741
|
-
if (!result.body) {
|
|
742
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
|
|
743
|
-
}
|
|
744
|
-
let data;
|
|
745
|
-
try {
|
|
746
|
-
data = JSON.parse(result.body);
|
|
747
|
-
}
|
|
748
|
-
catch (e) {
|
|
749
|
-
throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
|
|
750
|
-
}
|
|
751
|
-
if (!data.revert) {
|
|
752
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
|
|
753
|
-
}
|
|
754
|
-
return data.revert;
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
class UsersSdk extends SdkBase {
|
|
758
|
-
async me() {
|
|
759
|
-
const { token } = this.sdk;
|
|
760
|
-
const url = this.sdk.getUrl(`/users/me`);
|
|
761
|
-
const result = await this.sdk.http.get(url.toString(), { token });
|
|
762
|
-
this.inspectCommonStatusCodes(result.status);
|
|
763
|
-
const E_PREFIX = 'Unable to read a user. ';
|
|
764
|
-
if (result.status !== 200) {
|
|
765
|
-
this.logInvalidResponse(result);
|
|
766
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
|
|
767
|
-
}
|
|
768
|
-
if (!result.body) {
|
|
769
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
|
|
770
|
-
}
|
|
771
|
-
let data;
|
|
772
|
-
try {
|
|
773
|
-
data = JSON.parse(result.body);
|
|
774
|
-
}
|
|
775
|
-
catch (e) {
|
|
776
|
-
throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
|
|
777
|
-
}
|
|
778
|
-
if (!data.key) {
|
|
779
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
|
|
780
|
-
}
|
|
781
|
-
return data;
|
|
782
|
-
}
|
|
783
|
-
/**
|
|
784
|
-
* Lists users in the store
|
|
785
|
-
*
|
|
786
|
-
* @param options Optional query options.
|
|
787
|
-
*/
|
|
788
|
-
async list(options) {
|
|
789
|
-
const { token } = this.sdk;
|
|
790
|
-
const url = this.sdk.getUrl(`/users`);
|
|
791
|
-
this.sdk.appendListOptions(url, options);
|
|
792
|
-
const result = await this.sdk.http.get(url.toString(), { token });
|
|
793
|
-
this.inspectCommonStatusCodes(result.status);
|
|
794
|
-
const E_PREFIX = 'Unable to list projects. ';
|
|
795
|
-
if (result.status !== 200) {
|
|
796
|
-
this.logInvalidResponse(result);
|
|
797
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
|
|
798
|
-
}
|
|
799
|
-
if (!result.body) {
|
|
800
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
|
|
801
|
-
}
|
|
802
|
-
let data;
|
|
803
|
-
try {
|
|
804
|
-
data = JSON.parse(result.body);
|
|
805
|
-
}
|
|
806
|
-
catch (e) {
|
|
807
|
-
throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
|
|
808
|
-
}
|
|
809
|
-
if (!Array.isArray(data.data)) {
|
|
810
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
|
|
811
|
-
}
|
|
812
|
-
return data;
|
|
813
|
-
}
|
|
814
|
-
/**
|
|
815
|
-
* Reads a user information from the store.
|
|
816
|
-
* @param key The user key.
|
|
817
|
-
* @returns The user object
|
|
818
|
-
*/
|
|
819
|
-
async read(key) {
|
|
820
|
-
const { token } = this.sdk;
|
|
821
|
-
const url = this.sdk.getUrl(`/users/${key}`);
|
|
822
|
-
const result = await this.sdk.http.get(url.toString(), { token });
|
|
823
|
-
this.inspectCommonStatusCodes(result.status);
|
|
824
|
-
const E_PREFIX = 'Unable to read the user info. ';
|
|
825
|
-
if (result.status !== 200) {
|
|
826
|
-
this.logInvalidResponse(result);
|
|
827
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
|
|
828
|
-
}
|
|
829
|
-
if (!result.body) {
|
|
830
|
-
throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
|
|
831
|
-
}
|
|
832
|
-
let data;
|
|
833
|
-
try {
|
|
834
|
-
data = JSON.parse(result.body);
|
|
835
|
-
}
|
|
836
|
-
catch (e) {
|
|
837
|
-
throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
|
|
838
|
-
}
|
|
839
|
-
return data;
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
//# sourceMappingURL=StoreSdk.js.map
|