@api-client/core 0.5.2 → 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 (88) hide show
  1. package/build/browser.d.ts +2 -1
  2. package/build/browser.js +2 -1
  3. package/build/browser.js.map +1 -1
  4. package/build/index.d.ts +2 -1
  5. package/build/index.js +2 -1
  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 +41 -0
  10. package/build/src/models/Project.js +94 -0
  11. package/build/src/models/Project.js.map +1 -0
  12. package/build/src/models/RevisionInfo.d.ts +5 -0
  13. package/build/src/models/RevisionInfo.js.map +1 -1
  14. package/build/src/models/SerializableError.d.ts +1 -0
  15. package/build/src/models/SerializableError.js.map +1 -1
  16. package/build/src/models/Thing.js +1 -1
  17. package/build/src/models/Thing.js.map +1 -1
  18. package/build/src/models/Workspace.d.ts +0 -17
  19. package/build/src/models/Workspace.js +3 -19
  20. package/build/src/models/Workspace.js.map +1 -1
  21. package/build/src/models/store/Deletion.d.ts +24 -0
  22. package/build/src/models/store/Deletion.js +2 -0
  23. package/build/src/models/store/Deletion.js.map +1 -0
  24. package/build/src/models/store/File.d.ts +75 -12
  25. package/build/src/models/store/File.js +120 -19
  26. package/build/src/models/store/File.js.map +1 -1
  27. package/build/src/models/store/Modification.d.ts +24 -0
  28. package/build/src/models/store/Modification.js +2 -0
  29. package/build/src/models/store/Modification.js.map +1 -0
  30. package/build/src/runtime/store/Errors.d.ts +50 -0
  31. package/build/src/runtime/store/Errors.js +63 -0
  32. package/build/src/runtime/store/Errors.js.map +1 -0
  33. package/build/src/runtime/store/FilesSdk.d.ts +94 -0
  34. package/build/src/runtime/store/FilesSdk.js +247 -0
  35. package/build/src/runtime/store/FilesSdk.js.map +1 -0
  36. package/build/src/runtime/store/HistorySdk.d.ts +14 -7
  37. package/build/src/runtime/store/HistorySdk.js +34 -12
  38. package/build/src/runtime/store/HistorySdk.js.map +1 -1
  39. package/build/src/runtime/store/RouteBuilder.d.ts +9 -17
  40. package/build/src/runtime/store/RouteBuilder.js +14 -26
  41. package/build/src/runtime/store/RouteBuilder.js.map +1 -1
  42. package/build/src/runtime/store/Sdk.d.ts +6 -7
  43. package/build/src/runtime/store/Sdk.js +6 -7
  44. package/build/src/runtime/store/Sdk.js.map +1 -1
  45. package/build/src/runtime/store/SdkBase.d.ts +16 -6
  46. package/build/src/runtime/store/SdkBase.js +54 -4
  47. package/build/src/runtime/store/SdkBase.js.map +1 -1
  48. package/build/src/runtime/store/SharedSdk.d.ts +8 -2
  49. package/build/src/runtime/store/SharedSdk.js +22 -8
  50. package/build/src/runtime/store/SharedSdk.js.map +1 -1
  51. package/build/src/runtime/store/StoreSdkNode.d.ts +1 -1
  52. package/build/src/runtime/store/StoreSdkWeb.d.ts +1 -1
  53. package/build/src/runtime/store/UsersSdk.d.ts +10 -4
  54. package/build/src/runtime/store/UsersSdk.js +12 -6
  55. package/build/src/runtime/store/UsersSdk.js.map +1 -1
  56. package/package.json +2 -2
  57. package/src/models/Backend.ts +7 -9
  58. package/src/models/HttpHistory.ts +2 -2
  59. package/src/models/Project.ts +110 -0
  60. package/src/models/RevisionInfo.ts +6 -0
  61. package/src/models/SerializableError.ts +1 -0
  62. package/src/models/Thing.ts +1 -1
  63. package/src/models/Workspace.ts +4 -29
  64. package/src/models/store/Deletion.ts +24 -0
  65. package/src/models/store/File.ts +146 -26
  66. package/src/models/store/Modification.ts +24 -0
  67. package/src/runtime/store/Errors.ts +100 -0
  68. package/src/runtime/store/FilesSdk.ts +302 -0
  69. package/src/runtime/store/HistorySdk.ts +42 -17
  70. package/src/runtime/store/RouteBuilder.ts +14 -28
  71. package/src/runtime/store/Sdk.ts +7 -7
  72. package/src/runtime/store/SdkBase.ts +63 -9
  73. package/src/runtime/store/SharedSdk.ts +27 -10
  74. package/src/runtime/store/StoreSdkNode.ts +1 -1
  75. package/src/runtime/store/StoreSdkWeb.ts +1 -1
  76. package/src/runtime/store/UsersSdk.ts +14 -8
  77. package/build/src/models/HttpProjectListItem.d.ts +0 -23
  78. package/build/src/models/HttpProjectListItem.js +0 -2
  79. package/build/src/models/HttpProjectListItem.js.map +0 -1
  80. package/build/src/runtime/store/ProjectsSdk.d.ts +0 -43
  81. package/build/src/runtime/store/ProjectsSdk.js +0 -144
  82. package/build/src/runtime/store/ProjectsSdk.js.map +0 -1
  83. package/build/src/runtime/store/SpacesSdk.d.ts +0 -62
  84. package/build/src/runtime/store/SpacesSdk.js +0 -194
  85. package/build/src/runtime/store/SpacesSdk.js.map +0 -1
  86. package/src/models/HttpProjectListItem.ts +0 -23
  87. package/src/runtime/store/ProjectsSdk.ts +0 -147
  88. package/src/runtime/store/SpacesSdk.ts +0 -209
@@ -1,8 +1,25 @@
1
1
  import { IPermission } from './Permission.js';
2
+ import { IModification } from './Modification.js';
3
+ import { IDeletion } from './Deletion.js';
4
+ import { IUser, Kind as UserKind } from './User.js';
5
+ import { IThing, Thing } from '../Thing.js';
6
+ import v4 from '../../lib/uuid.js';
2
7
 
3
8
  export const DefaultOwner = 'default';
4
9
 
5
10
  export interface IStoredFile {
11
+ /**
12
+ * The kind of the File
13
+ */
14
+ kind: string;
15
+ /**
16
+ * The identifier of the entity
17
+ */
18
+ key: string;
19
+ /**
20
+ * The projects's meta info.
21
+ */
22
+ info: IThing;
6
23
  /**
7
24
  * The list of parents of the object. It is an ordered list of parents
8
25
  * from the top (first element) to the lowest parent in the tree (last element).
@@ -23,17 +40,22 @@ export interface IStoredFile {
23
40
  */
24
41
  deleted?: boolean;
25
42
  /**
26
- * The timestamp of when the file was deleted.
27
- */
28
- deletedTime?: number;
29
- /**
30
- * The id of the user that has deleted the file.
43
+ * The information about the delete information.
44
+ * Always set when the `delete` is true.
31
45
  */
32
- deletingUser?: string;
46
+ deletedInfo?: IDeletion;
33
47
  /**
34
48
  * The owner of this object. The id of the User object.
35
49
  */
36
50
  owner: string;
51
+ /**
52
+ * The last modification made to this file.
53
+ */
54
+ lastModified: IModification;
55
+ /**
56
+ * An arbitrary list of labels applied to the file.
57
+ */
58
+ labels?: string[];
37
59
  }
38
60
 
39
61
  /**
@@ -54,6 +76,18 @@ export interface IFile extends IStoredFile {
54
76
  }
55
77
 
56
78
  export class StoredFile {
79
+ /**
80
+ * The kind of the File
81
+ */
82
+ kind = '';
83
+ /**
84
+ * The identifier of the entity
85
+ */
86
+ key = '';
87
+ /**
88
+ * The name of the environment.
89
+ */
90
+ info: Thing = Thing.fromName('');
57
91
  /**
58
92
  * The list of parents of the object. It is an ordered list of parents
59
93
  * from the top (first element) to the lowest parent in the tree (last element).
@@ -74,54 +108,140 @@ export class StoredFile {
74
108
  */
75
109
  deleted?: boolean;
76
110
  /**
77
- * The timestamp of when the file was deleted.
111
+ * The information about the delete information.
112
+ * Always set when the `delete` is true.
78
113
  */
79
- deletedTime?: number;
80
- /**
81
- * The id of the user that has deleted the file.
82
- */
83
- deletingUser?: string;
114
+ deletedInfo?: IDeletion;
84
115
  /**
85
116
  * The owner of this space. The id of the User object.
86
117
  * Set to `default` when there are no users in the system (no authentication).
87
118
  */
88
- owner = '';
119
+ owner = DefaultOwner;
120
+ /**
121
+ * The last modification made to this file.
122
+ */
123
+ lastModified: IModification = { user: '', time: 0, byMe: false };
124
+ /**
125
+ * An arbitrary list of labels applied to the file.
126
+ */
127
+ labels?: string[];
89
128
 
90
129
  new(init: IStoredFile): void {
91
- const { parents=[], permissionIds=[], deleted, deletedTime, deletingUser, owner = DefaultOwner } = init;
130
+ const { key = v4(), info, kind, parents=[], permissionIds=[], deleted, deletedInfo, owner = DefaultOwner, lastModified, labels } = init;
131
+ this.key = key;
132
+ this.kind = kind;
133
+ if (info) {
134
+ this.info = new Thing(info);
135
+ } else {
136
+ this.info = Thing.fromName('');
137
+ }
92
138
  this.parents = parents;
93
139
  this.permissionIds = permissionIds;
94
140
  this.owner = owner;
141
+ this.lastModified = lastModified ? { ...lastModified } : { user: '', time: 0, byMe: false };
95
142
  if (typeof deleted === 'boolean') {
96
143
  this.deleted = deleted;
97
- this.deletedTime = deletedTime;
98
- this.deletingUser = deletingUser;
144
+ this.deletedInfo = deletedInfo ? { ...deletedInfo } : undefined;
99
145
  } else {
100
146
  this.deleted = undefined;
101
- this.deletedTime = undefined;
102
- this.deletingUser = undefined;
147
+ this.deletedInfo = undefined;
148
+ }
149
+ if (Array.isArray(labels)) {
150
+ this.labels = labels;
151
+ } else {
152
+ this.labels = undefined;
103
153
  }
104
154
  }
105
155
 
106
156
  toJSON(): IStoredFile {
107
157
  const { owner = DefaultOwner } = this;
108
158
  const result: IStoredFile = {
159
+ key: this.key,
160
+ kind: this.kind,
161
+ info: this.info.toJSON(),
109
162
  parents: this.parents,
110
163
  permissionIds: this.permissionIds,
164
+ lastModified: this.lastModified,
111
165
  owner,
112
166
  };
113
- if (typeof this.deleted === 'boolean') {
167
+ if (this.deleted) {
114
168
  result.deleted = this.deleted;
115
-
116
- if (this.deletedTime) {
117
- result.deletedTime = this.deletedTime;
118
- }
119
- if (this.deletingUser) {
120
- result.deletingUser = this.deletingUser;
121
- }
169
+ result.deletedInfo = this.deletedInfo;
170
+ }
171
+ if (Array.isArray(this.labels)) {
172
+ result.labels = this.labels;
122
173
  }
123
174
  return result;
124
175
  }
176
+
177
+ /**
178
+ * Updates the "lastModified" value.
179
+ * A helper method for a common task.
180
+ *
181
+ * @param user The user that modifies the entity.
182
+ */
183
+ setLastModified(user: IUser): void {
184
+ if (!user) {
185
+ throw new Error(`The user is required.`);
186
+ }
187
+ if (user.kind !== UserKind) {
188
+ throw new Error(`Invalid value for the user when setting "lastModified".`);
189
+ }
190
+ this.lastModified = {
191
+ byMe: false,
192
+ time: Date.now(),
193
+ user: user.key,
194
+ name: user.name,
195
+ };
196
+ }
197
+
198
+ /**
199
+ * Adds a label to the list of labels.
200
+ *
201
+ * It makes sure the value is a valid, non-empty string and the `labels` array is set.
202
+ *
203
+ * It does nothing when the label already exists.
204
+ *
205
+ * @param label The label to set.
206
+ */
207
+ addLabel(label: string): void {
208
+ if (typeof label !== 'string') {
209
+ throw new Error(`The label must be a string.`);
210
+ }
211
+ if (!label.trim()) {
212
+ throw new Error(`The label is required.`);
213
+ }
214
+ if (!Array.isArray(this.labels)) {
215
+ this.labels = [];
216
+ }
217
+ if (this.labels.includes(label)) {
218
+ return;
219
+ }
220
+ this.labels.push(label);
221
+ }
222
+
223
+ /**
224
+ * Marks the entity as deleted.
225
+ *
226
+ * A helper method for a common task.
227
+ *
228
+ * @param user The user that deletes the entity.
229
+ */
230
+ setDeleted(user: IUser): void {
231
+ if (!user) {
232
+ throw new Error(`The user is required.`);
233
+ }
234
+ if (user.kind !== UserKind) {
235
+ throw new Error(`Invalid value for the user when setting "lastModified".`);
236
+ }
237
+ this.deleted = true;
238
+ this.deletedInfo = {
239
+ byMe: false,
240
+ time: Date.now(),
241
+ user: user.key,
242
+ name: user.name,
243
+ };
244
+ }
125
245
  }
126
246
 
127
247
  export class File extends StoredFile {
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Information about a modification of a File.
3
+ */
4
+ export interface IModification {
5
+ /**
6
+ * The key of the user modifying the record.
7
+ */
8
+ user: string;
9
+ /**
10
+ * User name modifying the record. May not be set when there's no actual user.
11
+ */
12
+ name?: string;
13
+ /**
14
+ * Whether the modification was performed by the requesting the data user.
15
+ *
16
+ * Note for store implementers, this field should not be stored and populated every time the
17
+ * record is requested.
18
+ */
19
+ byMe: boolean;
20
+ /**
21
+ * A timestamp when the object was modified.
22
+ */
23
+ time: number;
24
+ }
@@ -0,0 +1,100 @@
1
+ import { SerializableError, ISerializedError } from '../../models/SerializableError.js';
2
+
3
+ export interface IApiError {
4
+ /**
5
+ * Indicates the response is an error.
6
+ */
7
+ error: boolean;
8
+ /**
9
+ * The same as the status code but returned with the body.
10
+ */
11
+ code: number;
12
+ /**
13
+ * The human-readable error message
14
+ */
15
+ message: string;
16
+ /**
17
+ * The detailed message about the error, if any.
18
+ */
19
+ detail?: string;
20
+ }
21
+
22
+ export class ApiError {
23
+ get name(): string {
24
+ return 'ApiError';
25
+ }
26
+
27
+ code: number;
28
+ message: string;
29
+ detail?: string;
30
+
31
+ constructor(message: string, code: number);
32
+ constructor(init: IApiError);
33
+
34
+ constructor(messageOrInit: string | IApiError, code?: number) {
35
+ if (typeof messageOrInit === 'string') {
36
+ if (typeof code === 'number') {
37
+ this.code = code;
38
+ } else {
39
+ this.code = 0;
40
+ }
41
+ this.message = messageOrInit;
42
+ } else {
43
+ this.message = messageOrInit.message;
44
+ this.code = messageOrInit.code;
45
+ if (messageOrInit.detail) {
46
+ this.detail = messageOrInit.detail;
47
+ }
48
+ }
49
+ }
50
+
51
+ toJSON(): IApiError {
52
+ const { message, code, detail } = this;
53
+ const result: IApiError = {
54
+ error: true,
55
+ message,
56
+ code,
57
+ };
58
+ if (detail) {
59
+ result.detail = detail;
60
+ }
61
+ return result;
62
+ }
63
+
64
+ toString(): string {
65
+ return this.message;
66
+ }
67
+ }
68
+
69
+ export interface ISdkError extends ISerializedError {
70
+ /**
71
+ * The raw response from the server.
72
+ */
73
+ response?: string;
74
+ /**
75
+ * Optional detailed message returned by the server.
76
+ */
77
+ detail?: string;
78
+ }
79
+
80
+ export class SdkError extends SerializableError {
81
+ /**
82
+ * The raw response from the server.
83
+ */
84
+ response?: string;
85
+ /**
86
+ * Optional detailed message returned by the server.
87
+ */
88
+ detail?: string;
89
+
90
+ toJSON(): ISdkError {
91
+ const result = super.toJSON();
92
+ if (this.response) {
93
+ result.response = this.response;
94
+ }
95
+ if (this.detail) {
96
+ result.detail = this.detail;
97
+ }
98
+ return result;
99
+ }
100
+ }
@@ -0,0 +1,302 @@
1
+ import WebSocketNode from 'ws';
2
+ import { JsonPatch } from 'json8-patch';
3
+ import { SdkBase, E_RESPONSE_STATUS, E_RESPONSE_NO_VALUE, E_INVALID_JSON, E_RESPONSE_UNKNOWN, E_RESPONSE_LOCATION, ISdkRequestOptions } from './SdkBase.js';
4
+ import { RouteBuilder } from './RouteBuilder.js';
5
+ import { IListOptions, IListResponse } from '../../models/Backend.js';
6
+ import { AccessOperation } from '../../models/store/Permission.js';
7
+ import { IUser } from '../../models/store/User.js';
8
+ import { IFile } from '../../models/store/File.js';
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';
13
+
14
+ export interface IFileCreateOptions {
15
+ /**
16
+ * Optional parent file id.
17
+ * When set it creates a file under this parent.
18
+ */
19
+ parent?: string;
20
+ }
21
+
22
+ export class FilesSdk extends SdkBase {
23
+ /**
24
+ * Lists files (spaces, projects, etc) in the store.
25
+ *
26
+ * @param kinds the list of kinds to list. Spaces are always included.
27
+ * @param options Optional query options.
28
+ * @param request Optional request options.
29
+ */
30
+ async list(kinds: (typeof ProjectKind | typeof WorkspaceKind)[], options?: IListOptions, request: ISdkRequestOptions = {}): Promise<IListResponse<IFile>> {
31
+ const token = request.token || this.sdk.token;
32
+ const url = this.sdk.getUrl(RouteBuilder.files());
33
+ this.sdk.appendListOptions(url, options);
34
+ kinds.forEach(k => url.searchParams.append('kind', k));
35
+ const result = await this.sdk.http.get(url.toString(), { token });
36
+ this.inspectCommonStatusCodes(result.status, result.body);
37
+ const E_PREFIX = 'Unable to list files. ';
38
+ if (result.status !== 200) {
39
+ this.logInvalidResponse(result);
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;
46
+ }
47
+ if (!result.body) {
48
+ throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
49
+ }
50
+ let data: IListResponse<IFile>;
51
+ try {
52
+ data = JSON.parse(result.body);
53
+ } catch (e) {
54
+ throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
55
+ }
56
+ if (!Array.isArray(data.data)) {
57
+ throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
58
+ }
59
+ return data;
60
+ }
61
+
62
+ /**
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.
68
+ */
69
+ async create(file: IFile | IHttpProject, opts: IFileCreateOptions = {}, request: ISdkRequestOptions = {}): Promise<string> {
70
+ const token = request.token || this.sdk.token;
71
+ const path = opts.parent ? RouteBuilder.file(opts.parent) : RouteBuilder.files();
72
+ const url = this.sdk.getUrl(path);
73
+ const body = JSON.stringify(file);
74
+ const result = await this.sdk.http.post(url.toString(), { token, body });
75
+ this.inspectCommonStatusCodes(result.status, result.body);
76
+ const E_PREFIX = 'Unable to create a file. ';
77
+ if (result.status !== 204) {
78
+ this.logInvalidResponse(result);
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;
85
+ }
86
+ const location = result.headers.get('location');
87
+ if (!location) {
88
+ throw new Error(`${E_PREFIX}${E_RESPONSE_LOCATION}`);
89
+ }
90
+ const id = location.split('/').pop();
91
+ return id as string;
92
+ }
93
+
94
+ /**
95
+ * Reads file metadata from the store.
96
+ *
97
+ * @param key The file key
98
+ * @param request Optional request options.
99
+ * @returns THe file metadata
100
+ */
101
+ read(key: string, media: false, request?: ISdkRequestOptions): Promise<IFile>;
102
+ /**
103
+ * Reads file contents from the store.
104
+ *
105
+ * @param key The file key
106
+ * @param request Optional request options.
107
+ * @returns THe file contents
108
+ */
109
+ read(key: string, media: true, request?: ISdkRequestOptions): Promise<unknown>;
110
+
111
+ /**
112
+ * Reads a user file definition from the store.
113
+ * @param key The file key
114
+ * @param media When true it reads file contents rather than metadata.
115
+ * @param request Optional request options.
116
+ */
117
+ async read(key: string, media?: boolean, request: ISdkRequestOptions = {}): Promise<IFile | unknown> {
118
+ const token = request.token || this.sdk.token;
119
+ const url = this.sdk.getUrl(RouteBuilder.file(key));
120
+ if (media) {
121
+ url.searchParams.set('alt', 'media');
122
+ }
123
+ const result = await this.sdk.http.get(url.toString(), { token });
124
+ this.inspectCommonStatusCodes(result.status, result.body);
125
+ const E_PREFIX = 'Unable to read a file. ';
126
+ if (result.status !== 200) {
127
+ this.logInvalidResponse(result);
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;
134
+ }
135
+ if (!result.body) {
136
+ throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
137
+ }
138
+ let data: IFile;
139
+ try {
140
+ data = JSON.parse(result.body);
141
+ } catch (e) {
142
+ throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
143
+ }
144
+ if (!data.kind) {
145
+ throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
146
+ }
147
+ return data;
148
+ }
149
+
150
+ /**
151
+ * Patches file's meta in the store.
152
+ *
153
+ * @param key The file key
154
+ * @param value The patch to apply.
155
+ * @param request Optional request options.
156
+ */
157
+ patch(key: string, value: JsonPatch, media: false, request?: ISdkRequestOptions): Promise<JsonPatch>;
158
+
159
+ /**
160
+ * Patches file's content in the store.
161
+ *
162
+ * @param key The file key
163
+ * @param value The patch to apply.
164
+ * @param request Optional request options.
165
+ */
166
+ patch(key: string, value: JsonPatch, media: true, request?: ISdkRequestOptions): Promise<JsonPatch>;
167
+
168
+ /**
169
+ * Patches a file in the store.
170
+ * @param key The key of the file to patch
171
+ * @param value The JSON patch to be processed.
172
+ * @param request Optional request options.
173
+ * @returns The JSON patch to revert the change using the `json8-patch` library
174
+ */
175
+ async patch(key: string, value: JsonPatch, media?: boolean, request: ISdkRequestOptions = {}): Promise<JsonPatch> {
176
+ const token = request.token || this.sdk.token;
177
+ const url = this.sdk.getUrl(RouteBuilder.file(key));
178
+ if (media) {
179
+ url.searchParams.set('alt', 'media');
180
+ }
181
+ const body = JSON.stringify(value);
182
+ const result = await this.sdk.http.patch(url.toString(), { token, body });
183
+ this.inspectCommonStatusCodes(result.status, result.body);
184
+ const E_PREFIX = 'Unable to patch a file. ';
185
+ if (result.status !== 200) {
186
+ this.logInvalidResponse(result);
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;
193
+ }
194
+ if (!result.body) {
195
+ throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
196
+ }
197
+ let data: any;
198
+ try {
199
+ data = JSON.parse(result.body);
200
+ } catch (e) {
201
+ throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
202
+ }
203
+ if (!data.revert) {
204
+ throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
205
+ }
206
+ return data.revert as JsonPatch;
207
+ }
208
+
209
+ /**
210
+ * Deletes the file in the store.
211
+ *
212
+ * @param key The key of the file to delete.
213
+ * @param request Optional request options.
214
+ */
215
+ async delete(key: string, request: ISdkRequestOptions = {}): Promise<void> {
216
+ const token = request.token || this.sdk.token;
217
+ const url = this.sdk.getUrl(RouteBuilder.file(key));
218
+ const result = await this.sdk.http.delete(url.toString(), { token });
219
+ this.inspectCommonStatusCodes(result.status, result.body);
220
+ const E_PREFIX = 'Unable to delete a file. ';
221
+ if (result.status !== 204) {
222
+ this.logInvalidResponse(result);
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;
229
+ }
230
+ }
231
+
232
+ /**
233
+ * Updates the sharing options of the file.
234
+ *
235
+ * @param key The file key
236
+ * @param value The patch operation on the file's ACL
237
+ * @param request Optional request options.
238
+ */
239
+ async patchUsers(key: string, value: AccessOperation[], request: ISdkRequestOptions = {}): Promise<void> {
240
+ const token = request.token || this.sdk.token;
241
+ const url = this.sdk.getUrl(RouteBuilder.fileUsers(key));
242
+ const body = JSON.stringify(value);
243
+ const result = await this.sdk.http.patch(url.toString(), { token, body });
244
+ this.inspectCommonStatusCodes(result.status, result.body);
245
+ const E_PREFIX = 'Unable to patch a file. ';
246
+ if (result.status !== 204) {
247
+ this.logInvalidResponse(result);
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;
254
+ }
255
+ }
256
+
257
+ /**
258
+ * Lists uses having access to the file.
259
+ *
260
+ * @param key The file key
261
+ * @param request Optional request options.
262
+ */
263
+ async listUsers(key: string, request: ISdkRequestOptions = {}): Promise<IListResponse<IUser>> {
264
+ const token = request.token || this.sdk.token;
265
+ const url = this.sdk.getUrl(RouteBuilder.fileUsers(key));
266
+ const result = await this.sdk.http.get(url.toString(), { token });
267
+ this.inspectCommonStatusCodes(result.status, result.body);
268
+ const E_PREFIX = 'Unable to list users in the file. ';
269
+ if (result.status !== 200) {
270
+ this.logInvalidResponse(result);
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;
277
+ }
278
+ if (!result.body) {
279
+ throw new Error(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`);
280
+ }
281
+ let data: IListResponse<IUser>;
282
+ try {
283
+ data = JSON.parse(result.body);
284
+ } catch (e) {
285
+ throw new Error(`${E_PREFIX}${E_INVALID_JSON}.`);
286
+ }
287
+ if (!Array.isArray(data.data)) {
288
+ throw new Error(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`);
289
+ }
290
+ return data;
291
+ }
292
+
293
+ /**
294
+ * Creates a WS client that listens to the files events.
295
+ * @param request Optional request options.
296
+ */
297
+ async observeFiles(request: ISdkRequestOptions = {}): Promise<WebSocketNode | WebSocket> {
298
+ const token = request.token || this.sdk.token;
299
+ const url = this.sdk.getUrl(RouteBuilder.files());
300
+ return this.sdk.ws.createAndConnect(url.toString(), token);
301
+ }
302
+ }