@api-client/core 0.5.4 → 0.5.5

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 (46) hide show
  1. package/build/browser.d.ts +1 -0
  2. package/build/browser.js +1 -0
  3. package/build/browser.js.map +1 -1
  4. package/build/index.d.ts +1 -0
  5. package/build/index.js +1 -0
  6. package/build/index.js.map +1 -1
  7. package/build/src/models/Backend.d.ts +7 -9
  8. package/build/src/models/HttpHistory.d.ts +2 -2
  9. package/build/src/models/Project.d.ts +1 -1
  10. package/build/src/models/Project.js +2 -2
  11. package/build/src/models/Project.js.map +1 -1
  12. package/build/src/models/SerializableError.d.ts +1 -0
  13. package/build/src/models/SerializableError.js.map +1 -1
  14. package/build/src/runtime/store/Errors.d.ts +50 -0
  15. package/build/src/runtime/store/Errors.js +63 -0
  16. package/build/src/runtime/store/Errors.js.map +1 -0
  17. package/build/src/runtime/store/FilesSdk.d.ts +41 -26
  18. package/build/src/runtime/store/FilesSdk.js +100 -53
  19. package/build/src/runtime/store/FilesSdk.js.map +1 -1
  20. package/build/src/runtime/store/HistorySdk.d.ts +14 -7
  21. package/build/src/runtime/store/HistorySdk.js +34 -12
  22. package/build/src/runtime/store/HistorySdk.js.map +1 -1
  23. package/build/src/runtime/store/Sdk.d.ts +4 -0
  24. package/build/src/runtime/store/Sdk.js +4 -0
  25. package/build/src/runtime/store/Sdk.js.map +1 -1
  26. package/build/src/runtime/store/SdkBase.d.ts +16 -6
  27. package/build/src/runtime/store/SdkBase.js +54 -4
  28. package/build/src/runtime/store/SdkBase.js.map +1 -1
  29. package/build/src/runtime/store/SharedSdk.d.ts +8 -2
  30. package/build/src/runtime/store/SharedSdk.js +21 -7
  31. package/build/src/runtime/store/SharedSdk.js.map +1 -1
  32. package/build/src/runtime/store/UsersSdk.d.ts +10 -4
  33. package/build/src/runtime/store/UsersSdk.js +12 -6
  34. package/build/src/runtime/store/UsersSdk.js.map +1 -1
  35. package/package.json +2 -2
  36. package/src/models/Backend.ts +7 -9
  37. package/src/models/HttpHistory.ts +2 -2
  38. package/src/models/Project.ts +2 -2
  39. package/src/models/SerializableError.ts +1 -0
  40. package/src/runtime/store/Errors.ts +100 -0
  41. package/src/runtime/store/FilesSdk.ts +116 -62
  42. package/src/runtime/store/HistorySdk.ts +42 -17
  43. package/src/runtime/store/Sdk.ts +5 -0
  44. package/src/runtime/store/SdkBase.ts +63 -9
  45. package/src/runtime/store/SharedSdk.ts +26 -9
  46. package/src/runtime/store/UsersSdk.ts +14 -8
@@ -1,17 +1,20 @@
1
+ import WebSocketNode from 'ws';
1
2
  import { JsonPatch } from 'json8-patch';
2
- import { SdkBase, E_RESPONSE_STATUS, E_RESPONSE_NO_VALUE, E_INVALID_JSON, E_RESPONSE_UNKNOWN, E_RESPONSE_LOCATION } from './SdkBase.js';
3
+ import { SdkBase, E_RESPONSE_STATUS, E_RESPONSE_NO_VALUE, E_INVALID_JSON, E_RESPONSE_UNKNOWN, E_RESPONSE_LOCATION, ISdkRequestOptions } from './SdkBase.js';
3
4
  import { RouteBuilder } from './RouteBuilder.js';
4
5
  import { IListOptions, IListResponse } from '../../models/Backend.js';
5
- import { IWorkspace, Workspace, Kind as WorkspaceKind } from '../../models/Workspace.js';
6
6
  import { AccessOperation } from '../../models/store/Permission.js';
7
7
  import { IUser } from '../../models/store/User.js';
8
8
  import { IFile } from '../../models/store/File.js';
9
- import WebSocketNode from 'ws';
9
+ import { Kind as ProjectKind } from '../../models/Project.js';
10
+ import { Kind as WorkspaceKind } from '../../models/Workspace.js';
11
+ import { IHttpProject } from '../../models/HttpProject.js';
12
+ import { SdkError } from './Errors.js';
10
13
 
11
14
  export interface IFileCreateOptions {
12
15
  /**
13
- * Optional parent space id.
14
- * When set it creates a space under this parent.
16
+ * Optional parent file id.
17
+ * When set it creates a file under this parent.
15
18
  */
16
19
  parent?: string;
17
20
  }
@@ -19,18 +22,27 @@ export interface IFileCreateOptions {
19
22
  export class FilesSdk extends SdkBase {
20
23
  /**
21
24
  * Lists files (spaces, projects, etc) in the store.
25
+ *
26
+ * @param kinds the list of kinds to list. Spaces are always included.
22
27
  * @param options Optional query options.
28
+ * @param request Optional request options.
23
29
  */
24
- async list(options?: IListOptions): Promise<IListResponse<IFile>> {
25
- const { token } = this.sdk;
30
+ async list(kinds: (typeof ProjectKind | typeof WorkspaceKind)[], options?: IListOptions, request: ISdkRequestOptions = {}): Promise<IListResponse<IFile>> {
31
+ const token = request.token || this.sdk.token;
26
32
  const url = this.sdk.getUrl(RouteBuilder.files());
27
33
  this.sdk.appendListOptions(url, options);
34
+ kinds.forEach(k => url.searchParams.append('kind', k));
28
35
  const result = await this.sdk.http.get(url.toString(), { token });
29
- this.inspectCommonStatusCodes(result.status);
30
- const E_PREFIX = 'Unable to list spaces. ';
36
+ this.inspectCommonStatusCodes(result.status, result.body);
37
+ const E_PREFIX = 'Unable to list files. ';
31
38
  if (result.status !== 200) {
32
39
  this.logInvalidResponse(result);
33
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
40
+ let e = this.createGenericSdkError(result.body)
41
+ if (!e) {
42
+ e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
43
+ e.response = result.body;
44
+ }
45
+ throw e;
34
46
  }
35
47
  if (!result.body) {
36
48
  throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
@@ -48,21 +60,28 @@ export class FilesSdk extends SdkBase {
48
60
  }
49
61
 
50
62
  /**
51
- * Creates a workspace in the store.
52
- * @param space The workspace definition.
53
- * @returns The key of the creates space.
63
+ * Creates a file in the store.
64
+ *
65
+ * @param file The definition of a file that extends the IFile interface or one of the supported by the server schemas.
66
+ * @param request Optional request options.
67
+ * @returns The key of the creates file.
54
68
  */
55
- async create(space: IWorkspace | Workspace, opts: IFileCreateOptions = {}): Promise<string> {
56
- const { token } = this.sdk;
69
+ async create(file: IFile | IHttpProject, opts: IFileCreateOptions = {}, request: ISdkRequestOptions = {}): Promise<string> {
70
+ const token = request.token || this.sdk.token;
57
71
  const path = opts.parent ? RouteBuilder.file(opts.parent) : RouteBuilder.files();
58
72
  const url = this.sdk.getUrl(path);
59
- const body = JSON.stringify(space);
73
+ const body = JSON.stringify(file);
60
74
  const result = await this.sdk.http.post(url.toString(), { token, body });
61
- this.inspectCommonStatusCodes(result.status);
62
- const E_PREFIX = 'Unable to create a user space. ';
75
+ this.inspectCommonStatusCodes(result.status, result.body);
76
+ const E_PREFIX = 'Unable to create a file. ';
63
77
  if (result.status !== 204) {
64
78
  this.logInvalidResponse(result);
65
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
79
+ let e = this.createGenericSdkError(result.body)
80
+ if (!e) {
81
+ e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
82
+ e.response = result.body;
83
+ }
84
+ throw e;
66
85
  }
67
86
  const location = result.headers.get('location');
68
87
  if (!location) {
@@ -76,45 +95,53 @@ export class FilesSdk extends SdkBase {
76
95
  * Reads file metadata from the store.
77
96
  *
78
97
  * @param key The file key
98
+ * @param request Optional request options.
79
99
  * @returns THe file metadata
80
100
  */
81
- read(key: string, media: false): Promise<IFile>;
101
+ read(key: string, media: false, request?: ISdkRequestOptions): Promise<IFile>;
82
102
  /**
83
103
  * Reads file contents from the store.
84
104
  *
85
105
  * @param key The file key
106
+ * @param request Optional request options.
86
107
  * @returns THe file contents
87
108
  */
88
- read(key: string, media: true): Promise<unknown>;
109
+ read(key: string, media: true, request?: ISdkRequestOptions): Promise<unknown>;
89
110
 
90
111
  /**
91
112
  * Reads a user file definition from the store.
92
113
  * @param key The file key
93
114
  * @param media When true it reads file contents rather than metadata.
115
+ * @param request Optional request options.
94
116
  */
95
- async read(key: string, media?: boolean): Promise<IFile | unknown> {
96
- const { token } = this.sdk;
117
+ async read(key: string, media?: boolean, request: ISdkRequestOptions = {}): Promise<IFile | unknown> {
118
+ const token = request.token || this.sdk.token;
97
119
  const url = this.sdk.getUrl(RouteBuilder.file(key));
98
120
  if (media) {
99
121
  url.searchParams.set('alt', 'media');
100
122
  }
101
123
  const result = await this.sdk.http.get(url.toString(), { token });
102
- this.inspectCommonStatusCodes(result.status);
103
- const E_PREFIX = 'Unable to read a user space. ';
124
+ this.inspectCommonStatusCodes(result.status, result.body);
125
+ const E_PREFIX = 'Unable to read a file. ';
104
126
  if (result.status !== 200) {
105
127
  this.logInvalidResponse(result);
106
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
128
+ let e = this.createGenericSdkError(result.body)
129
+ if (!e) {
130
+ e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
131
+ e.response = result.body;
132
+ }
133
+ throw e;
107
134
  }
108
135
  if (!result.body) {
109
136
  throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
110
137
  }
111
- let data: IWorkspace;
138
+ let data: IFile;
112
139
  try {
113
140
  data = JSON.parse(result.body);
114
141
  } catch (e) {
115
142
  throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
116
143
  }
117
- if (data.kind !== WorkspaceKind) {
144
+ if (!data.kind) {
118
145
  throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
119
146
  }
120
147
  return data;
@@ -125,36 +152,44 @@ export class FilesSdk extends SdkBase {
125
152
  *
126
153
  * @param key The file key
127
154
  * @param value The patch to apply.
155
+ * @param request Optional request options.
128
156
  */
129
- patch(key: string, value: JsonPatch, media: false): Promise<JsonPatch>;
157
+ patch(key: string, value: JsonPatch, media: false, request?: ISdkRequestOptions): Promise<JsonPatch>;
130
158
 
131
159
  /**
132
160
  * Patches file's content in the store.
133
161
  *
134
162
  * @param key The file key
135
163
  * @param value The patch to apply.
164
+ * @param request Optional request options.
136
165
  */
137
- patch(key: string, value: JsonPatch, media: true): Promise<JsonPatch>;
166
+ patch(key: string, value: JsonPatch, media: true, request?: ISdkRequestOptions): Promise<JsonPatch>;
138
167
 
139
168
  /**
140
- * Patches a user space in the store.
141
- * @param key The key of the user space to patch
169
+ * Patches a file in the store.
170
+ * @param key The key of the file to patch
142
171
  * @param value The JSON patch to be processed.
172
+ * @param request Optional request options.
143
173
  * @returns The JSON patch to revert the change using the `json8-patch` library
144
174
  */
145
- async patch(key: string, value: JsonPatch, media?: boolean): Promise<JsonPatch> {
146
- const { token } = this.sdk;
175
+ async patch(key: string, value: JsonPatch, media?: boolean, request: ISdkRequestOptions = {}): Promise<JsonPatch> {
176
+ const token = request.token || this.sdk.token;
147
177
  const url = this.sdk.getUrl(RouteBuilder.file(key));
148
178
  if (media) {
149
179
  url.searchParams.set('alt', 'media');
150
180
  }
151
181
  const body = JSON.stringify(value);
152
182
  const result = await this.sdk.http.patch(url.toString(), { token, body });
153
- this.inspectCommonStatusCodes(result.status);
154
- const E_PREFIX = 'Unable to patch a user space. ';
183
+ this.inspectCommonStatusCodes(result.status, result.body);
184
+ const E_PREFIX = 'Unable to patch a file. ';
155
185
  if (result.status !== 200) {
156
186
  this.logInvalidResponse(result);
157
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
187
+ let e = this.createGenericSdkError(result.body)
188
+ if (!e) {
189
+ e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
190
+ e.response = result.body;
191
+ }
192
+ throw e;
158
193
  }
159
194
  if (!result.body) {
160
195
  throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
@@ -172,55 +207,73 @@ export class FilesSdk extends SdkBase {
172
207
  }
173
208
 
174
209
  /**
175
- * Deletes the space in the store.
210
+ * Deletes the file in the store.
176
211
  *
177
- * @param key The key of the space to delete.
212
+ * @param key The key of the file to delete.
213
+ * @param request Optional request options.
178
214
  */
179
- async delete(key: string): Promise<void> {
180
- const { token } = this.sdk;
215
+ async delete(key: string, request: ISdkRequestOptions = {}): Promise<void> {
216
+ const token = request.token || this.sdk.token;
181
217
  const url = this.sdk.getUrl(RouteBuilder.file(key));
182
218
  const result = await this.sdk.http.delete(url.toString(), { token });
183
- this.inspectCommonStatusCodes(result.status);
184
- const E_PREFIX = 'Unable to delete a user space. ';
219
+ this.inspectCommonStatusCodes(result.status, result.body);
220
+ const E_PREFIX = 'Unable to delete a file. ';
185
221
  if (result.status !== 204) {
186
222
  this.logInvalidResponse(result);
187
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
223
+ let e = this.createGenericSdkError(result.body)
224
+ if (!e) {
225
+ e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
226
+ e.response = result.body;
227
+ }
228
+ throw e;
188
229
  }
189
230
  }
190
231
 
191
232
  /**
192
- * Updates the sharing options of the space.
233
+ * Updates the sharing options of the file.
193
234
  *
194
- * @param key The user space key
195
- * @param value The patch operation on the space's ACL
235
+ * @param key The file key
236
+ * @param value The patch operation on the file's ACL
237
+ * @param request Optional request options.
196
238
  */
197
- async patchUsers(key: string, value: AccessOperation[]): Promise<void> {
198
- const { token } = this.sdk;
239
+ async patchUsers(key: string, value: AccessOperation[], request: ISdkRequestOptions = {}): Promise<void> {
240
+ const token = request.token || this.sdk.token;
199
241
  const url = this.sdk.getUrl(RouteBuilder.fileUsers(key));
200
242
  const body = JSON.stringify(value);
201
243
  const result = await this.sdk.http.patch(url.toString(), { token, body });
202
- this.inspectCommonStatusCodes(result.status);
203
- const E_PREFIX = 'Unable to patch a user space. ';
244
+ this.inspectCommonStatusCodes(result.status, result.body);
245
+ const E_PREFIX = 'Unable to patch a file. ';
204
246
  if (result.status !== 204) {
205
247
  this.logInvalidResponse(result);
206
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
248
+ let e = this.createGenericSdkError(result.body)
249
+ if (!e) {
250
+ e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
251
+ e.response = result.body;
252
+ }
253
+ throw e;
207
254
  }
208
255
  }
209
256
 
210
257
  /**
211
- * Lists uses having access to the user space.
258
+ * Lists uses having access to the file.
212
259
  *
213
- * @param key The user space key
260
+ * @param key The file key
261
+ * @param request Optional request options.
214
262
  */
215
- async listUsers(key: string): Promise<IListResponse<IUser>> {
216
- const { token } = this.sdk;
263
+ async listUsers(key: string, request: ISdkRequestOptions = {}): Promise<IListResponse<IUser>> {
264
+ const token = request.token || this.sdk.token;
217
265
  const url = this.sdk.getUrl(RouteBuilder.fileUsers(key));
218
266
  const result = await this.sdk.http.get(url.toString(), { token });
219
- this.inspectCommonStatusCodes(result.status);
220
- const E_PREFIX = 'Unable to list users in the space. ';
267
+ this.inspectCommonStatusCodes(result.status, result.body);
268
+ const E_PREFIX = 'Unable to list users in the file. ';
221
269
  if (result.status !== 200) {
222
270
  this.logInvalidResponse(result);
223
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
271
+ let e = this.createGenericSdkError(result.body)
272
+ if (!e) {
273
+ e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
274
+ e.response = result.body;
275
+ }
276
+ throw e;
224
277
  }
225
278
  if (!result.body) {
226
279
  throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
@@ -238,10 +291,11 @@ export class FilesSdk extends SdkBase {
238
291
  }
239
292
 
240
293
  /**
241
- * Creates a WS client that listens to the spaces events.
294
+ * Creates a WS client that listens to the files events.
295
+ * @param request Optional request options.
242
296
  */
243
- async observeFiles(): Promise<WebSocketNode | WebSocket> {
244
- const { token } = this.sdk;
297
+ async observeFiles(request: ISdkRequestOptions = {}): Promise<WebSocketNode | WebSocket> {
298
+ const token = request.token || this.sdk.token;
245
299
  const url = this.sdk.getUrl(RouteBuilder.files());
246
300
  return this.sdk.ws.createAndConnect(url.toString(), token);
247
301
  }
@@ -1,6 +1,6 @@
1
- import { SdkBase, E_RESPONSE_STATUS, E_RESPONSE_NO_VALUE, E_INVALID_JSON, E_RESPONSE_UNKNOWN, E_RESPONSE_LOCATION } from './SdkBase.js';
1
+ import { SdkBase, E_RESPONSE_STATUS, E_RESPONSE_NO_VALUE, E_INVALID_JSON, E_RESPONSE_UNKNOWN, E_RESPONSE_LOCATION, ISdkRequestOptions } from './SdkBase.js';
2
2
  import { RouteBuilder } from './RouteBuilder.js';
3
- import { IListResponse, HistoryListOptions } from '../../models/Backend.js';
3
+ import { IListResponse, HistoryListOptions, IHistoryRequestListOptions } from '../../models/Backend.js';
4
4
  import { IHttpHistory, IHttpHistoryBulkAdd, Kind as HttpHistoryKind } from '../../models/HttpHistory.js';
5
5
 
6
6
  export class HistorySdk extends SdkBase {
@@ -14,10 +14,11 @@ export class HistorySdk extends SdkBase {
14
14
  * Note, history objects cannot be updated. They can only be created or deleted.
15
15
  *
16
16
  * @param history The history to create
17
+ * @param request Optional request options.
17
18
  * @returns The key of the created history.
18
19
  */
19
- async create(history: IHttpHistory): Promise<string> {
20
- const { token } = this.sdk;
20
+ async create(history: IHttpHistory, request: ISdkRequestOptions = {}): Promise<string> {
21
+ const token = request.token || this.sdk.token;
21
22
  const url = this.sdk.getUrl(RouteBuilder.history());
22
23
  const body = JSON.stringify(history);
23
24
  const result = await this.sdk.http.post(url.toString(), { token, body });
@@ -39,9 +40,10 @@ export class HistorySdk extends SdkBase {
39
40
  * Creates a multiple history objects in a batch operation.
40
41
  *
41
42
  * @param info The bulk create info object.
43
+ * @param request Optional request options.
42
44
  */
43
- async createBulk(info: IHttpHistoryBulkAdd): Promise<string[]> {
44
- const { token } = this.sdk;
45
+ async createBulk(info: IHttpHistoryBulkAdd, request: ISdkRequestOptions = {}): Promise<string[]> {
46
+ const token = request.token || this.sdk.token;
45
47
  const url = this.sdk.getUrl(RouteBuilder.historyBatchCreate());
46
48
  const body = JSON.stringify(info);
47
49
  const result = await this.sdk.http.post(url.toString(), { token, body });
@@ -56,7 +58,7 @@ export class HistorySdk extends SdkBase {
56
58
  }
57
59
  let data: string[];
58
60
  try {
59
- data = JSON.parse(result.body);
61
+ data = JSON.parse(result.body).data;
60
62
  } catch (e) {
61
63
  throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
62
64
  }
@@ -67,11 +69,12 @@ export class HistorySdk extends SdkBase {
67
69
  * Lists the history.
68
70
  *
69
71
  * @param options Optional query options.
72
+ * @param request Optional request options.
70
73
  */
71
- async list(options: HistoryListOptions): Promise<IListResponse> {
72
- const { token } = this.sdk;
74
+ async list(options: HistoryListOptions, request: ISdkRequestOptions = {}): Promise<IListResponse<IHttpHistory>> {
75
+ const token = request.token || this.sdk.token;
73
76
  const url = this.sdk.getUrl(RouteBuilder.history());
74
- this.sdk.appendListOptions(url, options);
77
+ this.appendHistoryListParameters(url, options);
75
78
  const result = await this.sdk.http.get(url.toString(), { token });
76
79
  this.inspectCommonStatusCodes(result.status);
77
80
  const E_PREFIX = 'Unable to list history. ';
@@ -82,7 +85,7 @@ export class HistorySdk extends SdkBase {
82
85
  if (!result.body) {
83
86
  throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
84
87
  }
85
- let data: IListResponse;
88
+ let data: IListResponse<IHttpHistory>;
86
89
  try {
87
90
  data = JSON.parse(result.body);
88
91
  } catch (e) {
@@ -94,24 +97,45 @@ export class HistorySdk extends SdkBase {
94
97
  return data;
95
98
  }
96
99
 
100
+ appendHistoryListParameters(url: URL, options: HistoryListOptions): void {
101
+ this.sdk.appendListOptions(url, options);
102
+ const { searchParams } = url;
103
+ if (options.type) {
104
+ searchParams.set('type', options.type);
105
+ }
106
+ const projectOptions = options as IHistoryRequestListOptions;
107
+ if (projectOptions.id) {
108
+ searchParams.set('id', projectOptions.id);
109
+ }
110
+ if (projectOptions.user) {
111
+ searchParams.set('user', 'true');
112
+ }
113
+ if (projectOptions.project) {
114
+ searchParams.set('project', projectOptions.project);
115
+ }
116
+ }
117
+
97
118
  /**
98
119
  * Deletes a history object form the store.
99
120
  *
100
121
  * @param key The key returned by the store when created the history. Also available via the `key` property on the history object.
122
+ * @param request Optional request options.
101
123
  */
102
- delete(key: string): Promise<void>;
124
+ delete(key: string, request?: ISdkRequestOptions): Promise<void>;
103
125
  /**
104
126
  * Deletes a list of history objects in a batch operation.
105
127
  *
106
128
  * @param keys The keys returned by the store when created the history. Also available via the `key` property on the history object.
129
+ * @param request Optional request options.
107
130
  */
108
- delete(keys: string[]): Promise<void>;
131
+ delete(keys: string[], request?: ISdkRequestOptions): Promise<void>;
109
132
  /**
110
133
  * Deletes a history or a list of history objects from the store.
111
134
  * @param key A key or a list of keys.
135
+ * @param request Optional request options.
112
136
  */
113
- async delete(key: string | string[]): Promise<void> {
114
- const { token } = this.sdk;
137
+ async delete(key: string | string[], request: ISdkRequestOptions = {}): Promise<void> {
138
+ const token = request.token || this.sdk.token;
115
139
  const isArray = Array.isArray(key);
116
140
  const path = isArray ? RouteBuilder.historyBatchDelete() : RouteBuilder.historyItem(key);
117
141
  const url = this.sdk.getUrl(path);
@@ -128,10 +152,11 @@ export class HistorySdk extends SdkBase {
128
152
  /**
129
153
  * Reads a history definition from the store.
130
154
  * @param key The history key
155
+ * @param request Optional request options.
131
156
  * @returns The history object
132
157
  */
133
- async read(key: string): Promise<IHttpHistory> {
134
- const { token } = this.sdk;
158
+ async read(key: string, request: ISdkRequestOptions = {}): Promise<IHttpHistory> {
159
+ const token = request.token || this.sdk.token;
135
160
  const url = this.sdk.getUrl(RouteBuilder.historyItem(key));
136
161
  const result = await this.sdk.http.get(url.toString(), { token });
137
162
  this.inspectCommonStatusCodes(result.status);
@@ -51,6 +51,11 @@ export abstract class Sdk {
51
51
  */
52
52
  shared = new SharedSdk(this);
53
53
 
54
+ /**
55
+ * When set it limits log output to minimum.
56
+ */
57
+ silent = false;
58
+
54
59
 
55
60
  [baseUriSymbol] = '';
56
61
 
@@ -1,16 +1,20 @@
1
1
  import { Headers } from '../../lib/headers/Headers.js';
2
2
  import { Sdk } from './Sdk.js';
3
+ import { SdkError, IApiError } from './Errors.js';
3
4
 
4
- export interface IStoreRequestOptions {
5
- method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
6
- headers?: Record<string, string>,
7
- body?: string | Buffer;
5
+ export interface ISdkRequestOptions {
8
6
  /**
9
- * Adds the token to the headers.
7
+ * Uses the provided token for authentication.
10
8
  */
11
9
  token?: string;
12
10
  }
13
11
 
12
+ export interface IStoreRequestOptions extends ISdkRequestOptions {
13
+ method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
14
+ headers?: Record<string, string>,
15
+ body?: string | Buffer;
16
+ }
17
+
14
18
  export interface IStoreResponse {
15
19
  status: number;
16
20
  headers: Headers;
@@ -42,6 +46,9 @@ export class SdkBase {
42
46
  constructor(public sdk: Sdk) {}
43
47
 
44
48
  protected logInvalidResponse(response: IStoreResponse): void {
49
+ if (this.sdk.silent) {
50
+ return;
51
+ }
45
52
  if (response.body) {
46
53
  try {
47
54
  const data = JSON.parse(response.body);
@@ -58,15 +65,62 @@ export class SdkBase {
58
65
  * Throws unified message for a common error status codes.
59
66
  * It handles 404, 403, and 401 status codes.
60
67
  */
61
- protected inspectCommonStatusCodes(status: number): void {
68
+ protected inspectCommonStatusCodes(status: number, body?: string): void {
62
69
  if (status === 404) {
63
- throw new Error(`Not found.`);
70
+ let e = this.createGenericSdkError(body)
71
+ if (!e) {
72
+ e = new SdkError(`Not found.`, 400);
73
+ e.response = body;
74
+ }
75
+ throw e;
64
76
  }
65
77
  if (status === 403) {
66
- throw new Error(`You have no access to this resource.`);
78
+ let e = this.createGenericSdkError(body)
79
+ if (!e) {
80
+ e = new SdkError(`You have no access to this resource.`, 403);
81
+ e.response = body;
82
+ }
83
+ throw e;
67
84
  }
68
85
  if (status === 401) {
69
- throw new Error(`Not authorized.`);
86
+ let e = this.createGenericSdkError(body)
87
+ if (!e) {
88
+ e = new SdkError(`Not authorized.`, 401);
89
+ e.response = body;
90
+ }
91
+ throw e;
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Reads the response as ApiError
97
+ * @param body The message returned by the store.
98
+ * @returns The error schema or undefined when not an error;
99
+ */
100
+ protected readErrorResponse(body?: string): IApiError | undefined {
101
+ if (!body) {
102
+ return undefined;
103
+ }
104
+ let data: any;
105
+ try {
106
+ data = JSON.parse(body);
107
+ } catch (e) {
108
+ return undefined;
109
+ }
110
+ if (data.error && data.message) {
111
+ return data as IApiError;
112
+ }
113
+ return undefined;
114
+ }
115
+
116
+ protected createGenericSdkError(body?: string): SdkError | undefined {
117
+ const info = this.readErrorResponse(body);
118
+ if (!info) {
119
+ return undefined;
70
120
  }
121
+ const e = new SdkError(info.message, info.code);
122
+ e.detail = info.detail;
123
+ e.response = body;
124
+ return e;
71
125
  }
72
126
  }
@@ -1,34 +1,51 @@
1
- import { SdkBase, E_RESPONSE_STATUS, E_RESPONSE_NO_VALUE, E_INVALID_JSON, E_RESPONSE_UNKNOWN } from './SdkBase.js';
1
+ import { SdkBase, E_RESPONSE_STATUS, E_RESPONSE_NO_VALUE, E_INVALID_JSON, E_RESPONSE_UNKNOWN, ISdkRequestOptions } from './SdkBase.js';
2
2
  import { RouteBuilder } from './RouteBuilder.js';
3
3
  import { IListOptions, IListResponse } from '../../models/Backend.js';
4
+ import { Kind as ProjectKind } from '../../models/Project.js';
5
+ import { Kind as WorkspaceKind } from '../../models/Workspace.js';
6
+ import { IFile } from '../../models/store/File.js';
7
+ import { SdkError } from './Errors.js';
4
8
 
5
9
  export class SharedSdk extends SdkBase {
6
10
  /**
7
11
  * Lists shared with the user spaces.
12
+ *
13
+ * @param kinds the list of kinds to list. Spaces are always included.
8
14
  * @param options Optional query options.
15
+ * @param request Optional request options.
9
16
  */
10
- async list(options?: IListOptions): Promise<IListResponse> {
11
- const { token } = this.sdk;
17
+ async list(kinds: (typeof ProjectKind | typeof WorkspaceKind)[], options?: IListOptions, request: ISdkRequestOptions = {}): Promise<IListResponse<IFile>> {
18
+ const token = request.token || this.sdk.token;
12
19
  const url = this.sdk.getUrl(RouteBuilder.shared());
13
20
  this.sdk.appendListOptions(url, options);
21
+ kinds.forEach(k => url.searchParams.append('kind', k));
14
22
  const result = await this.sdk.http.get(url.toString(), { token });
15
- this.inspectCommonStatusCodes(result.status);
23
+ this.inspectCommonStatusCodes(result.status, result.body);
16
24
  const E_PREFIX = 'Unable to list spaces. ';
17
25
  if (result.status !== 200) {
18
26
  this.logInvalidResponse(result);
19
- throw new Error(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`);
27
+ let e = this.createGenericSdkError(result.body)
28
+ if (!e) {
29
+ e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
30
+ e.response = result.body;
31
+ }
32
+ throw e;
20
33
  }
21
34
  if (!result.body) {
22
- throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
35
+ throw new SdkError(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`, 0);
23
36
  }
24
- let data: IListResponse;
37
+ let data: IListResponse<IFile>;
25
38
  try {
26
39
  data = JSON.parse(result.body);
27
40
  } catch (e) {
28
- throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
41
+ const err = new SdkError(`${E_PREFIX}${E_INVALID_JSON}.`, 0);
42
+ err.response = result.body;
43
+ throw err;
29
44
  }
30
45
  if (!Array.isArray(data.data)) {
31
- throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
46
+ const err = new SdkError(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`, 0);
47
+ err.response = result.body;
48
+ throw err;
32
49
  }
33
50
  return data;
34
51
  }