@api-client/core 0.9.9 → 0.9.11

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 (45) hide show
  1. package/build/src/events/BaseEvents.d.ts +5 -1
  2. package/build/src/runtime/store/AuthSdk.js +3 -3
  3. package/build/src/runtime/store/AuthSdk.js.map +1 -1
  4. package/build/src/runtime/store/BackendSdk.js +1 -1
  5. package/build/src/runtime/store/BackendSdk.js.map +1 -1
  6. package/build/src/runtime/store/CertificatesSdk.d.ts +7 -0
  7. package/build/src/runtime/store/CertificatesSdk.js +18 -7
  8. package/build/src/runtime/store/CertificatesSdk.js.map +1 -1
  9. package/build/src/runtime/store/FilesSdk.d.ts +22 -1
  10. package/build/src/runtime/store/FilesSdk.js +60 -12
  11. package/build/src/runtime/store/FilesSdk.js.map +1 -1
  12. package/build/src/runtime/store/HistorySdk.d.ts +7 -0
  13. package/build/src/runtime/store/HistorySdk.js +20 -9
  14. package/build/src/runtime/store/HistorySdk.js.map +1 -1
  15. package/build/src/runtime/store/RevisionsSdk.js +1 -1
  16. package/build/src/runtime/store/RevisionsSdk.js.map +1 -1
  17. package/build/src/runtime/store/RouteBuilder.d.ts +1 -0
  18. package/build/src/runtime/store/RouteBuilder.js +3 -0
  19. package/build/src/runtime/store/RouteBuilder.js.map +1 -1
  20. package/build/src/runtime/store/SdkBase.d.ts +1 -1
  21. package/build/src/runtime/store/SdkBase.js +5 -4
  22. package/build/src/runtime/store/SdkBase.js.map +1 -1
  23. package/build/src/runtime/store/SharedSdk.js +1 -1
  24. package/build/src/runtime/store/SharedSdk.js.map +1 -1
  25. package/build/src/runtime/store/SpacesSdk.d.ts +26 -1
  26. package/build/src/runtime/store/SpacesSdk.js +71 -4
  27. package/build/src/runtime/store/SpacesSdk.js.map +1 -1
  28. package/build/src/runtime/store/TrashSdk.js +5 -5
  29. package/build/src/runtime/store/TrashSdk.js.map +1 -1
  30. package/build/src/runtime/store/UsersSdk.js +3 -3
  31. package/build/src/runtime/store/UsersSdk.js.map +1 -1
  32. package/package.json +1 -1
  33. package/src/events/BaseEvents.ts +5 -1
  34. package/src/runtime/store/AuthSdk.ts +3 -3
  35. package/src/runtime/store/BackendSdk.ts +1 -1
  36. package/src/runtime/store/CertificatesSdk.ts +20 -7
  37. package/src/runtime/store/FilesSdk.ts +74 -13
  38. package/src/runtime/store/HistorySdk.ts +22 -9
  39. package/src/runtime/store/RevisionsSdk.ts +1 -1
  40. package/src/runtime/store/RouteBuilder.ts +4 -0
  41. package/src/runtime/store/SdkBase.ts +5 -4
  42. package/src/runtime/store/SharedSdk.ts +1 -1
  43. package/src/runtime/store/SpacesSdk.ts +76 -5
  44. package/src/runtime/store/TrashSdk.ts +5 -5
  45. package/src/runtime/store/UsersSdk.ts +3 -3
@@ -1,3 +1,4 @@
1
+ import WebSocketNode from 'ws';
1
2
  import { SdkBase, E_RESPONSE_STATUS, E_RESPONSE_NO_VALUE, E_INVALID_JSON, E_RESPONSE_UNKNOWN, ISdkRequestOptions } from './SdkBase.js';
2
3
  import { RouteBuilder } from './RouteBuilder.js';
3
4
  import { ContextChangeRecord, ContextDeleteRecord, ContextListResult, ContextSpaceListOptions, IBulkOperationResult } from '../../events/BaseEvents.js';
@@ -17,7 +18,7 @@ export class CertificatesSdk extends SdkBase {
17
18
  url.searchParams.set('space', space);
18
19
  const body = JSON.stringify(value);
19
20
  const result = await this.sdk.http.post(url.toString(), { token, body });
20
- this.inspectCommonStatusCodes(result.status);
21
+ this.inspectCommonStatusCodes(result);
21
22
  const E_PREFIX = 'Unable to create a certificate. ';
22
23
  if (result.status !== 200) {
23
24
  this.logInvalidResponse(result);
@@ -55,7 +56,7 @@ export class CertificatesSdk extends SdkBase {
55
56
  url.searchParams.set('space', space);
56
57
  const body = JSON.stringify(values);
57
58
  const result = await this.sdk.http.post(url.toString(), { token, body });
58
- this.inspectCommonStatusCodes(result.status);
59
+ this.inspectCommonStatusCodes(result);
59
60
  const E_PREFIX = 'Unable to create a bulk certificates. ';
60
61
  if (result.status !== 200) {
61
62
  this.logInvalidResponse(result);
@@ -88,7 +89,7 @@ export class CertificatesSdk extends SdkBase {
88
89
  const token = request.token || this.sdk.token;
89
90
  const url = this.sdk.getUrl(RouteBuilder.certificate(key));
90
91
  const result = await this.sdk.http.get(url.toString(), { token });
91
- this.inspectCommonStatusCodes(result.status);
92
+ this.inspectCommonStatusCodes(result);
92
93
  const E_PREFIX = 'Unable to read a certificate. ';
93
94
  if (result.status !== 200) {
94
95
  this.logInvalidResponse(result);
@@ -125,7 +126,7 @@ export class CertificatesSdk extends SdkBase {
125
126
  const url = this.sdk.getUrl(RouteBuilder.certificates());
126
127
  this.sdk.appendListOptions(url, options);
127
128
  const result = await this.sdk.http.get(url.toString(), { token });
128
- this.inspectCommonStatusCodes(result.status);
129
+ this.inspectCommonStatusCodes(result);
129
130
  const E_PREFIX = 'Unable to list certificates. ';
130
131
  if (result.status !== 200) {
131
132
  this.logInvalidResponse(result);
@@ -160,7 +161,7 @@ export class CertificatesSdk extends SdkBase {
160
161
  const token = request.token || this.sdk.token;
161
162
  const url = this.sdk.getUrl(RouteBuilder.certificate(key));
162
163
  const result = await this.sdk.http.delete(url.toString(), { token });
163
- this.inspectCommonStatusCodes(result.status, result.body);
164
+ this.inspectCommonStatusCodes(result);
164
165
  const E_PREFIX = 'Unable to delete the certificate. ';
165
166
  if (result.status !== 204) {
166
167
  this.logInvalidResponse(result);
@@ -181,7 +182,7 @@ export class CertificatesSdk extends SdkBase {
181
182
  const token = request.token || this.sdk.token;
182
183
  const url = this.sdk.getUrl(RouteBuilder.certificateBatchDelete());
183
184
  const result = await this.sdk.http.post(url.toString(), { token, body: JSON.stringify(keys) });
184
- this.inspectCommonStatusCodes(result.status);
185
+ this.inspectCommonStatusCodes(result);
185
186
  const E_PREFIX = 'Unable to delete history. ';
186
187
  if (result.status !== 200) {
187
188
  this.logInvalidResponse(result);
@@ -216,7 +217,7 @@ export class CertificatesSdk extends SdkBase {
216
217
  const token = request.token || this.sdk.token;
217
218
  const url = this.sdk.getUrl(RouteBuilder.certificatesEmpty(space));
218
219
  const result = await this.sdk.http.delete(url.toString(), { token });
219
- this.inspectCommonStatusCodes(result.status, result.body);
220
+ this.inspectCommonStatusCodes(result);
220
221
  const E_PREFIX = 'Unable to delete the certificate. ';
221
222
  if (result.status !== 204) {
222
223
  this.logInvalidResponse(result);
@@ -228,4 +229,16 @@ export class CertificatesSdk extends SdkBase {
228
229
  throw e;
229
230
  }
230
231
  }
232
+
233
+ /**
234
+ * Creates a WS client that listens to the certificate events.
235
+ * @param space The parent space.
236
+ * @param request Optional request options.
237
+ */
238
+ async observe(space: string, request: ISdkRequestOptions = {}): Promise<WebSocketNode | WebSocket> {
239
+ const token = request.token || this.sdk.token;
240
+ const url = this.sdk.getUrl(RouteBuilder.certificates());
241
+ url.searchParams.set('space', space);
242
+ return this.sdk.ws.createAndConnect(url.toString(), token);
243
+ }
231
244
  }
@@ -7,7 +7,19 @@ import { Kind as ProjectKind } from '../../models/Project.js';
7
7
  import { Kind as FolderKind } from '../../models/Folder.js';
8
8
  import { Kind as DataNamespaceKind } from '../../models/data/DataNamespace.js';
9
9
  import { SdkError } from './Errors.js';
10
- import { ContextListResult, IPatchInfo, IPatchRevision, IAccessPatchInfo, ContextChangeRecord, IBulkOperationResult, ContextListOptions } from '../../events/BaseEvents.js';
10
+ import {
11
+ ContextListResult,
12
+ IPatchInfo,
13
+ IPatchRevision,
14
+ IAccessPatchInfo,
15
+ ContextChangeRecord,
16
+ IBulkOperationResult,
17
+ ContextListOptions,
18
+ } from '../../events/BaseEvents.js';
19
+ import {
20
+ IAccessAddOperation, IAccessRemoveOperation, PermissionRole
21
+ } from '../..//models/store/Permission.js';
22
+ import uuid from '../../lib/uuid.js';
11
23
 
12
24
  export interface IMetaCreateOptions {
13
25
  /**
@@ -55,7 +67,7 @@ export class FilesSdk extends SdkBase {
55
67
  }
56
68
  url.searchParams.set('space', space);
57
69
  const result = await this.sdk.http.get(url.toString(), { token });
58
- this.inspectCommonStatusCodes(result.status, result.body);
70
+ this.inspectCommonStatusCodes(result);
59
71
  const E_PREFIX = 'Unable to list files. ';
60
72
  if (result.status !== 200) {
61
73
  this.logInvalidResponse(result);
@@ -119,7 +131,7 @@ export class FilesSdk extends SdkBase {
119
131
  'content-type': 'application/json'
120
132
  },
121
133
  });
122
- this.inspectCommonStatusCodes(result.status, result.body);
134
+ this.inspectCommonStatusCodes(result);
123
135
  const E_PREFIX = 'Unable to create a file. ';
124
136
  if (result.status !== 200) {
125
137
  this.logInvalidResponse(result);
@@ -174,7 +186,7 @@ export class FilesSdk extends SdkBase {
174
186
  'content-type': mime
175
187
  },
176
188
  });
177
- this.inspectCommonStatusCodes(result.status, result.body);
189
+ this.inspectCommonStatusCodes(result);
178
190
  const E_PREFIX = 'Unable to create a file. ';
179
191
  if (result.status !== 204) {
180
192
  this.logInvalidResponse(result);
@@ -219,7 +231,7 @@ export class FilesSdk extends SdkBase {
219
231
  url.searchParams.set('alt', 'media');
220
232
  }
221
233
  const result = await this.sdk.http.get(url.toString(), { token });
222
- this.inspectCommonStatusCodes(result.status, result.body);
234
+ this.inspectCommonStatusCodes(result);
223
235
  const E_PREFIX = 'Unable to read a file. ';
224
236
  if (result.status !== 200) {
225
237
  this.logInvalidResponse(result);
@@ -260,7 +272,7 @@ export class FilesSdk extends SdkBase {
260
272
  url.searchParams.set('space', space);
261
273
  const body = JSON.stringify(keys);
262
274
  const result = await this.sdk.http.post(url.toString(), { token, body });
263
- this.inspectCommonStatusCodes(result.status, result.body);
275
+ this.inspectCommonStatusCodes(result);
264
276
  const E_PREFIX = 'Unable to read files in bulk. ';
265
277
  if (result.status !== 200) {
266
278
  this.logInvalidResponse(result);
@@ -320,7 +332,7 @@ export class FilesSdk extends SdkBase {
320
332
  }
321
333
  const body = JSON.stringify(value);
322
334
  const result = await this.sdk.http.patch(url.toString(), { token, body });
323
- this.inspectCommonStatusCodes(result.status, result.body);
335
+ this.inspectCommonStatusCodes(result);
324
336
  const E_PREFIX = 'Unable to patch a file. ';
325
337
  if (result.status !== 200) {
326
338
  this.logInvalidResponse(result);
@@ -358,7 +370,7 @@ export class FilesSdk extends SdkBase {
358
370
  const url = this.sdk.getUrl(RouteBuilder.file(key));
359
371
  url.searchParams.set('space', space);
360
372
  const result = await this.sdk.http.delete(url.toString(), { token });
361
- this.inspectCommonStatusCodes(result.status, result.body);
373
+ this.inspectCommonStatusCodes(result);
362
374
  const E_PREFIX = 'Unable to delete a file. ';
363
375
  if (result.status !== 204) {
364
376
  this.logInvalidResponse(result);
@@ -374,6 +386,7 @@ export class FilesSdk extends SdkBase {
374
386
  /**
375
387
  * Updates the sharing options of the file.
376
388
  *
389
+ * @param space The parent space key
377
390
  * @param key The file key
378
391
  * @param value The patch operation on the file's ACL
379
392
  * @param request Optional request options.
@@ -384,8 +397,8 @@ export class FilesSdk extends SdkBase {
384
397
  url.searchParams.set('space', space);
385
398
  const body = JSON.stringify(value);
386
399
  const result = await this.sdk.http.patch(url.toString(), { token, body });
387
- this.inspectCommonStatusCodes(result.status, result.body);
388
- const E_PREFIX = 'Unable to patch a file. ';
400
+ this.inspectCommonStatusCodes(result);
401
+ const E_PREFIX = 'Unable to patch the file. ';
389
402
  if (result.status !== 204) {
390
403
  this.logInvalidResponse(result);
391
404
  let e = this.createGenericSdkError(result.body)
@@ -397,6 +410,52 @@ export class FilesSdk extends SdkBase {
397
410
  }
398
411
  }
399
412
 
413
+ /**
414
+ * A shorthand method to add access to the file to a single user.
415
+ *
416
+ * @param space The parent space key
417
+ * @param key The file key
418
+ * @param otherUserKey The key of the user that gets access to the space
419
+ * @param role The user role to grant
420
+ * @param expires Optionally, when the permission expires.
421
+ */
422
+ async addUser(space: string, key: string, otherUserKey: string, role: PermissionRole, expires?: number, request?: ISdkRequestOptions): Promise<void> {
423
+ const op: IAccessAddOperation = {
424
+ op: 'add',
425
+ type: 'user',
426
+ value: role,
427
+ id: otherUserKey,
428
+ };
429
+ if (typeof expires === 'number') {
430
+ op.expirationTime = expires;
431
+ }
432
+ const patch: IAccessPatchInfo = {
433
+ patch: [op],
434
+ id: uuid(),
435
+ };
436
+ return this.patchUsers(space, key, patch, request);
437
+ }
438
+
439
+ /**
440
+ * A shorthand method to remove access to the space to a single user.
441
+ *
442
+ * @param space The space key to share
443
+ * @param key The file key
444
+ * @param otherUserKey The key of the user that gets access to the space
445
+ */
446
+ async removeUser(space: string, key: string, otherUserKey: string, request?: ISdkRequestOptions): Promise<void> {
447
+ const op: IAccessRemoveOperation = {
448
+ op: 'remove',
449
+ type: 'user',
450
+ id: otherUserKey,
451
+ };
452
+ const patch: IAccessPatchInfo = {
453
+ patch: [op],
454
+ id: uuid(),
455
+ };
456
+ return this.patchUsers(space, key, patch, request);
457
+ }
458
+
400
459
  /**
401
460
  * Lists uses having access to the file.
402
461
  *
@@ -408,7 +467,7 @@ export class FilesSdk extends SdkBase {
408
467
  const url = this.sdk.getUrl(RouteBuilder.fileUsers(key));
409
468
  url.searchParams.set('space', space);
410
469
  const result = await this.sdk.http.get(url.toString(), { token });
411
- this.inspectCommonStatusCodes(result.status, result.body);
470
+ this.inspectCommonStatusCodes(result);
412
471
  const E_PREFIX = 'Unable to list users in the file. ';
413
472
  if (result.status !== 200) {
414
473
  this.logInvalidResponse(result);
@@ -436,11 +495,13 @@ export class FilesSdk extends SdkBase {
436
495
 
437
496
  /**
438
497
  * Creates a WS client that listens to the files events.
498
+ * @param space The parent space.
439
499
  * @param request Optional request options.
440
500
  */
441
- async observeFiles(request: ISdkRequestOptions = {}): Promise<WebSocketNode | WebSocket> {
501
+ async observeFiles(space: string, request: ISdkRequestOptions = {}): Promise<WebSocketNode | WebSocket> {
442
502
  const token = request.token || this.sdk.token;
443
503
  const url = this.sdk.getUrl(RouteBuilder.files());
504
+ url.searchParams.set('space', space);
444
505
  return this.sdk.ws.createAndConnect(url.toString(), token);
445
506
  }
446
507
 
@@ -470,7 +531,7 @@ export class FilesSdk extends SdkBase {
470
531
  const url = this.sdk.getUrl(RouteBuilder.fileBreadcrumbs(key));
471
532
  url.searchParams.set('space', space);
472
533
  const result = await this.sdk.http.get(url.toString(), { token });
473
- this.inspectCommonStatusCodes(result.status, result.body);
534
+ this.inspectCommonStatusCodes(result);
474
535
  const E_PREFIX = 'Unable to read a file. ';
475
536
  if (result.status !== 200) {
476
537
  this.logInvalidResponse(result);
@@ -1,3 +1,4 @@
1
+ import WebSocketNode from 'ws';
1
2
  import { SdkBase, E_RESPONSE_STATUS, E_RESPONSE_NO_VALUE, E_INVALID_JSON, E_RESPONSE_UNKNOWN, ISdkRequestOptions } from './SdkBase.js';
2
3
  import { RouteBuilder } from './RouteBuilder.js';
3
4
  import { IHttpHistory, IHttpHistoryBulkAdd, Kind as HttpHistoryKind } from '../../models/HttpHistory.js';
@@ -23,7 +24,7 @@ export class HistorySdk extends SdkBase {
23
24
  const url = this.sdk.getUrl(RouteBuilder.history());
24
25
  const body = JSON.stringify(history);
25
26
  const result = await this.sdk.http.post(url.toString(), { token, body });
26
- this.inspectCommonStatusCodes(result.status);
27
+ this.inspectCommonStatusCodes(result);
27
28
  const E_PREFIX = 'Unable to create a history. ';
28
29
  if (result.status !== 200) {
29
30
  this.logInvalidResponse(result);
@@ -60,7 +61,7 @@ export class HistorySdk extends SdkBase {
60
61
  const url = this.sdk.getUrl(RouteBuilder.historyBatchCreate());
61
62
  const body = JSON.stringify(info);
62
63
  const result = await this.sdk.http.post(url.toString(), { token, body });
63
- this.inspectCommonStatusCodes(result.status);
64
+ this.inspectCommonStatusCodes(result);
64
65
  const E_PREFIX = 'Unable to create a bulk history. ';
65
66
  if (result.status !== 200) {
66
67
  this.logInvalidResponse(result);
@@ -97,7 +98,7 @@ export class HistorySdk extends SdkBase {
97
98
  const url = this.sdk.getUrl(RouteBuilder.history());
98
99
  this.appendHistoryListParameters(url, options);
99
100
  const result = await this.sdk.http.get(url.toString(), { token });
100
- this.inspectCommonStatusCodes(result.status);
101
+ this.inspectCommonStatusCodes(result);
101
102
  const E_PREFIX = 'Unable to list history. ';
102
103
  if (result.status !== 200) {
103
104
  this.logInvalidResponse(result);
@@ -154,7 +155,7 @@ export class HistorySdk extends SdkBase {
154
155
  const url = this.sdk.getUrl(path);
155
156
 
156
157
  const result = await this.sdk.http.delete(url.toString(), { token });
157
- this.inspectCommonStatusCodes(result.status);
158
+ this.inspectCommonStatusCodes(result);
158
159
  const E_PREFIX = 'Unable to delete history. ';
159
160
  if (result.status !== 204) {
160
161
  this.logInvalidResponse(result);
@@ -178,7 +179,7 @@ export class HistorySdk extends SdkBase {
178
179
  const path = RouteBuilder.historyBatchDelete();
179
180
  const url = this.sdk.getUrl(path);
180
181
  const result = await this.sdk.http.post(url.toString(), { token, body: JSON.stringify(keys) });
181
- this.inspectCommonStatusCodes(result.status);
182
+ this.inspectCommonStatusCodes(result);
182
183
  const E_PREFIX = 'Unable to delete history. ';
183
184
  if (result.status !== 200) {
184
185
  this.logInvalidResponse(result);
@@ -215,7 +216,7 @@ export class HistorySdk extends SdkBase {
215
216
  const token = request.token || this.sdk.token;
216
217
  const url = this.sdk.getUrl(RouteBuilder.historyItem(key));
217
218
  const result = await this.sdk.http.get(url.toString(), { token });
218
- this.inspectCommonStatusCodes(result.status);
219
+ this.inspectCommonStatusCodes(result);
219
220
  const E_PREFIX = 'Unable to read a history. ';
220
221
  if (result.status !== 200) {
221
222
  this.logInvalidResponse(result);
@@ -245,7 +246,7 @@ export class HistorySdk extends SdkBase {
245
246
  const token = request.token || this.sdk.token;
246
247
  const url = this.sdk.getUrl(RouteBuilder.historyClear('space', spaceKey));
247
248
  const result = await this.sdk.http.delete(url.toString(), { token });
248
- this.inspectCommonStatusCodes(result.status, result.body);
249
+ this.inspectCommonStatusCodes(result);
249
250
  const E_PREFIX = 'Unable to clear history for the space. ';
250
251
  if (result.status !== 204) {
251
252
  this.logInvalidResponse(result);
@@ -262,7 +263,7 @@ export class HistorySdk extends SdkBase {
262
263
  const token = request.token || this.sdk.token;
263
264
  const url = this.sdk.getUrl(RouteBuilder.historyClear('project', spaceKey, projectKey));
264
265
  const result = await this.sdk.http.delete(url.toString(), { token });
265
- this.inspectCommonStatusCodes(result.status, result.body);
266
+ this.inspectCommonStatusCodes(result);
266
267
  const E_PREFIX = 'Unable to clear history for the project. ';
267
268
  if (result.status !== 204) {
268
269
  this.logInvalidResponse(result);
@@ -279,7 +280,7 @@ export class HistorySdk extends SdkBase {
279
280
  const token = request.token || this.sdk.token;
280
281
  const url = this.sdk.getUrl(RouteBuilder.historyClear('request', spaceKey, projectKey, requestKey));
281
282
  const result = await this.sdk.http.delete(url.toString(), { token });
282
- this.inspectCommonStatusCodes(result.status, result.body);
283
+ this.inspectCommonStatusCodes(result);
283
284
  const E_PREFIX = 'Unable to clear history for the request. ';
284
285
  if (result.status !== 204) {
285
286
  this.logInvalidResponse(result);
@@ -291,4 +292,16 @@ export class HistorySdk extends SdkBase {
291
292
  throw e;
292
293
  }
293
294
  }
295
+
296
+ /**
297
+ * Creates a WS client that listens to the certificate events.
298
+ * @param space The parent space.
299
+ * @param request Optional request options.
300
+ */
301
+ async observe(space: string, request: ISdkRequestOptions = {}): Promise<WebSocketNode | WebSocket> {
302
+ const token = request.token || this.sdk.token;
303
+ const url = this.sdk.getUrl(RouteBuilder.history());
304
+ url.searchParams.set('space', space);
305
+ return this.sdk.ws.createAndConnect(url.toString(), token);
306
+ }
294
307
  }
@@ -16,7 +16,7 @@ export class RevisionsSdk extends SdkBase {
16
16
  const url = this.sdk.getUrl(RouteBuilder.fileRevisions(key));
17
17
  this.sdk.appendListOptions(url, options);
18
18
  const result = await this.sdk.http.get(url.toString(), { token });
19
- this.inspectCommonStatusCodes(result.status);
19
+ this.inspectCommonStatusCodes(result);
20
20
  const E_PREFIX = 'Unable to list revisions for a file. ';
21
21
  if (result.status !== 200) {
22
22
  this.logInvalidResponse(result);
@@ -10,6 +10,10 @@ export class RouteBuilder {
10
10
  return `/spaces/${key}`;
11
11
  }
12
12
 
13
+ static spaceUsers(key: string): string {
14
+ return `/spaces/${key}/users`;
15
+ }
16
+
13
17
  /**
14
18
  * @returns The path to the /files route.
15
19
  */
@@ -66,11 +66,12 @@ export class SdkBase {
66
66
  * Throws unified message for a common error status codes.
67
67
  * It handles 404, 403, and 401 status codes.
68
68
  */
69
- protected inspectCommonStatusCodes(status: number, body?: string): void {
69
+ protected inspectCommonStatusCodes(response: IStoreResponse): void {
70
+ const { status, body } = response;
70
71
  if (status === 404) {
71
72
  let e = this.createGenericSdkError(body)
72
73
  if (!e) {
73
- e = new SdkError(`Not found.`, 400);
74
+ e = new SdkError(`Not found.`, status);
74
75
  e.response = body;
75
76
  }
76
77
  throw e;
@@ -78,7 +79,7 @@ export class SdkBase {
78
79
  if (status === 403) {
79
80
  let e = this.createGenericSdkError(body)
80
81
  if (!e) {
81
- e = new SdkError(`You have no access to this resource.`, 403);
82
+ e = new SdkError(`You have no access to this resource.`, status);
82
83
  e.response = body;
83
84
  }
84
85
  throw e;
@@ -86,7 +87,7 @@ export class SdkBase {
86
87
  if (status === 401) {
87
88
  let e = this.createGenericSdkError(body)
88
89
  if (!e) {
89
- e = new SdkError(`Not authorized.`, 401);
90
+ e = new SdkError(`Not authorized.`, status);
90
91
  e.response = body;
91
92
  }
92
93
  throw e;
@@ -22,7 +22,7 @@ export class SharedSdk extends SdkBase {
22
22
  }
23
23
  url.searchParams.set('space', space);
24
24
  const result = await this.sdk.http.get(url.toString(), { token });
25
- this.inspectCommonStatusCodes(result.status, result.body);
25
+ this.inspectCommonStatusCodes(result);
26
26
  const E_PREFIX = 'Unable to list spaces. ';
27
27
  if (result.status !== 200) {
28
28
  this.logInvalidResponse(result);
@@ -1,8 +1,10 @@
1
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
- import { ContextChangeRecord, ContextListOptions, ContextListResult } from '../../events/BaseEvents.js';
3
+ import { ContextChangeRecord, ContextListOptions, ContextListResult, IAccessPatchInfo } from '../../events/BaseEvents.js';
4
4
  import { ISpace, Kind as SpaceKind } from '../../models/Space.js';
5
+ import { IAccessAddOperation, IAccessRemoveOperation, PermissionRole } from '../../models/store/Permission.js';
5
6
  import { SdkError } from './Errors.js';
7
+ import uuid from '../../lib/uuid.js';
6
8
 
7
9
  export class SpacesSdk extends SdkBase {
8
10
  /**
@@ -14,7 +16,7 @@ export class SpacesSdk extends SdkBase {
14
16
  const url = this.sdk.getUrl(RouteBuilder.spaces());
15
17
  this.sdk.appendListOptions(url, options);
16
18
  const result = await this.sdk.http.get(url.toString(), { token });
17
- this.inspectCommonStatusCodes(result.status);
19
+ this.inspectCommonStatusCodes(result);
18
20
  const E_PREFIX = 'Unable to list spaces. ';
19
21
  if (result.status !== 200) {
20
22
  this.logInvalidResponse(result);
@@ -50,7 +52,7 @@ export class SpacesSdk extends SdkBase {
50
52
  const url = this.sdk.getUrl(RouteBuilder.spaces());
51
53
  const body = JSON.stringify(space);
52
54
  const result = await this.sdk.http.post(url.toString(), { token, body });
53
- this.inspectCommonStatusCodes(result.status);
55
+ this.inspectCommonStatusCodes(result);
54
56
  const E_PREFIX = 'Unable to create a space. ';
55
57
  if (result.status !== 200) {
56
58
  this.logInvalidResponse(result);
@@ -84,7 +86,7 @@ export class SpacesSdk extends SdkBase {
84
86
  const token = request.token || this.sdk.token;
85
87
  const url = this.sdk.getUrl(RouteBuilder.space(key));
86
88
  const result = await this.sdk.http.get(url.toString(), { token });
87
- this.inspectCommonStatusCodes(result.status);
89
+ this.inspectCommonStatusCodes(result);
88
90
  const E_PREFIX = 'Unable to read a space. ';
89
91
  if (result.status !== 200) {
90
92
  this.logInvalidResponse(result);
@@ -121,7 +123,7 @@ export class SpacesSdk extends SdkBase {
121
123
  const token = request.token || this.sdk.token;
122
124
  const url = this.sdk.getUrl(RouteBuilder.space(key));
123
125
  const result = await this.sdk.http.delete(url.toString(), { token });
124
- this.inspectCommonStatusCodes(result.status, result.body);
126
+ this.inspectCommonStatusCodes(result);
125
127
  const E_PREFIX = 'Unable to delete the space. ';
126
128
  if (result.status !== 204) {
127
129
  this.logInvalidResponse(result);
@@ -133,4 +135,73 @@ export class SpacesSdk extends SdkBase {
133
135
  throw e;
134
136
  }
135
137
  }
138
+
139
+ /**
140
+ * Updates the sharing options of the space.
141
+ *
142
+ * @param key The space key
143
+ * @param value The patch operation on the space's ACL
144
+ * @param request Optional request options.
145
+ */
146
+ async patchUsers(space: string, value: IAccessPatchInfo, request: ISdkRequestOptions = {}): Promise<void> {
147
+ const token = request.token || this.sdk.token;
148
+ const url = this.sdk.getUrl(RouteBuilder.spaceUsers(space));
149
+ const body = JSON.stringify(value);
150
+ const result = await this.sdk.http.patch(url.toString(), { token, body });
151
+ this.inspectCommonStatusCodes(result);
152
+ const E_PREFIX = 'Unable to patch the space. ';
153
+ if (result.status !== 204) {
154
+ this.logInvalidResponse(result);
155
+ let e = this.createGenericSdkError(result.body)
156
+ if (!e) {
157
+ e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status);
158
+ e.response = result.body;
159
+ }
160
+ throw e;
161
+ }
162
+ }
163
+
164
+ /**
165
+ * A shorthand method to add access to the space to a single user.
166
+ *
167
+ * @param space The space key to share
168
+ * @param otherUserKey The key of the user that gets access to the space
169
+ * @param role The user role to grant
170
+ * @param expires Optionally, when the permission expires.
171
+ */
172
+ async addUser(space: string, otherUserKey: string, role: PermissionRole, expires?: number, request?: ISdkRequestOptions): Promise<void> {
173
+ const op: IAccessAddOperation = {
174
+ op: 'add',
175
+ type: 'user',
176
+ value: role,
177
+ id: otherUserKey,
178
+ };
179
+ if (typeof expires === 'number') {
180
+ op.expirationTime = expires;
181
+ }
182
+ const patch: IAccessPatchInfo = {
183
+ patch: [op],
184
+ id: uuid(),
185
+ };
186
+ return this.patchUsers(space, patch, request);
187
+ }
188
+
189
+ /**
190
+ * A shorthand method to remove access to the space to a single user.
191
+ *
192
+ * @param space The space key to share
193
+ * @param otherUserKey The key of the user that gets access to the space
194
+ */
195
+ async removeUser(space: string, otherUserKey: string, request?: ISdkRequestOptions): Promise<void> {
196
+ const op: IAccessRemoveOperation = {
197
+ op: 'remove',
198
+ type: 'user',
199
+ id: otherUserKey,
200
+ };
201
+ const patch: IAccessPatchInfo = {
202
+ patch: [op],
203
+ id: uuid(),
204
+ };
205
+ return this.patchUsers(space, patch, request);
206
+ }
136
207
  }
@@ -18,7 +18,7 @@ export class TrashSdk extends SdkBase {
18
18
  url.searchParams.set('space', space);
19
19
  const body = JSON.stringify(entry);
20
20
  const result = await this.sdk.http.post(url.toString(), { token, body });
21
- this.inspectCommonStatusCodes(result.status);
21
+ this.inspectCommonStatusCodes(result);
22
22
  const E_PREFIX = 'Unable to create a trash. ';
23
23
  if (result.status !== 200) {
24
24
  this.logInvalidResponse(result);
@@ -53,7 +53,7 @@ export class TrashSdk extends SdkBase {
53
53
  const url = this.sdk.getUrl(RouteBuilder.trash());
54
54
  this.sdk.appendListOptions(url, options);
55
55
  const result = await this.sdk.http.get(url.toString(), { token });
56
- this.inspectCommonStatusCodes(result.status);
56
+ this.inspectCommonStatusCodes(result);
57
57
  const E_PREFIX = 'Unable to list trash. ';
58
58
  if (result.status !== 200) {
59
59
  this.logInvalidResponse(result);
@@ -87,7 +87,7 @@ export class TrashSdk extends SdkBase {
87
87
  const token = request.token || this.sdk.token;
88
88
  const url = this.sdk.getUrl(RouteBuilder.trashItem(key));
89
89
  const result = await this.sdk.http.delete(url.toString(), { token });
90
- this.inspectCommonStatusCodes(result.status, result.body);
90
+ this.inspectCommonStatusCodes(result);
91
91
  const E_PREFIX = 'Unable to delete the trash entry. ';
92
92
  if (result.status !== 204) {
93
93
  this.logInvalidResponse(result);
@@ -110,7 +110,7 @@ export class TrashSdk extends SdkBase {
110
110
  const token = request.token || this.sdk.token;
111
111
  const url = this.sdk.getUrl(RouteBuilder.trashRestore(key));
112
112
  const result = await this.sdk.http.put(url.toString(), { token });
113
- this.inspectCommonStatusCodes(result.status, result.body);
113
+ this.inspectCommonStatusCodes(result);
114
114
  const E_PREFIX = 'Unable to restore from trash. ';
115
115
  if (result.status !== 204) {
116
116
  this.logInvalidResponse(result);
@@ -144,7 +144,7 @@ export class TrashSdk extends SdkBase {
144
144
  const token = request.token || this.sdk.token;
145
145
  const url = this.sdk.getUrl(RouteBuilder.trashEmpty(space));
146
146
  const result = await this.sdk.http.delete(url.toString(), { token });
147
- this.inspectCommonStatusCodes(result.status, result.body);
147
+ this.inspectCommonStatusCodes(result);
148
148
  const E_PREFIX = 'Unable to empty trash. ';
149
149
  if (result.status !== 204) {
150
150
  this.logInvalidResponse(result);
@@ -12,7 +12,7 @@ export class UsersSdk extends SdkBase {
12
12
  const token = request.token || this.sdk.token;
13
13
  const url = this.sdk.getUrl(RouteBuilder.usersMe());
14
14
  const result = await this.sdk.http.get(url.toString(), { token });
15
- this.inspectCommonStatusCodes(result.status);
15
+ this.inspectCommonStatusCodes(result);
16
16
  const E_PREFIX = 'Unable to read a user. ';
17
17
  if (result.status !== 200) {
18
18
  this.logInvalidResponse(result);
@@ -44,7 +44,7 @@ export class UsersSdk extends SdkBase {
44
44
  const url = this.sdk.getUrl(RouteBuilder.users());
45
45
  this.sdk.appendListOptions(url, options);
46
46
  const result = await this.sdk.http.get(url.toString(), { token });
47
- this.inspectCommonStatusCodes(result.status);
47
+ this.inspectCommonStatusCodes(result);
48
48
  const E_PREFIX = 'Unable to list projects. ';
49
49
  if (result.status !== 200) {
50
50
  this.logInvalidResponse(result);
@@ -75,7 +75,7 @@ export class UsersSdk extends SdkBase {
75
75
  const token = request.token || this.sdk.token;
76
76
  const url = this.sdk.getUrl(RouteBuilder.user(key));
77
77
  const result = await this.sdk.http.get(url.toString(), { token });
78
- this.inspectCommonStatusCodes(result.status);
78
+ this.inspectCommonStatusCodes(result);
79
79
  const E_PREFIX = 'Unable to read the user info. ';
80
80
  if (result.status !== 200) {
81
81
  this.logInvalidResponse(result);