@api-client/core 0.5.3 → 0.5.6
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 +1 -0
- package/build/browser.js +1 -0
- package/build/browser.js.map +1 -1
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/index.js.map +1 -1
- package/build/src/models/Backend.d.ts +7 -9
- package/build/src/models/HttpHistory.d.ts +2 -2
- package/build/src/models/Project.d.ts +1 -1
- package/build/src/models/Project.js +2 -2
- package/build/src/models/Project.js.map +1 -1
- package/build/src/models/RevisionInfo.d.ts +2 -2
- package/build/src/models/SerializableError.d.ts +1 -0
- package/build/src/models/SerializableError.js.map +1 -1
- package/build/src/models/store/File.js +2 -2
- package/build/src/models/store/File.js.map +1 -1
- package/build/src/runtime/store/Errors.d.ts +50 -0
- package/build/src/runtime/store/Errors.js +63 -0
- package/build/src/runtime/store/Errors.js.map +1 -0
- package/build/src/runtime/store/FilesSdk.d.ts +94 -0
- package/build/src/runtime/store/FilesSdk.js +247 -0
- package/build/src/runtime/store/FilesSdk.js.map +1 -0
- package/build/src/runtime/store/HistorySdk.d.ts +14 -7
- package/build/src/runtime/store/HistorySdk.js +34 -12
- package/build/src/runtime/store/HistorySdk.js.map +1 -1
- package/build/src/runtime/store/RouteBuilder.d.ts +9 -17
- package/build/src/runtime/store/RouteBuilder.js +14 -26
- package/build/src/runtime/store/RouteBuilder.js.map +1 -1
- package/build/src/runtime/store/Sdk.d.ts +6 -7
- package/build/src/runtime/store/Sdk.js +6 -7
- package/build/src/runtime/store/Sdk.js.map +1 -1
- package/build/src/runtime/store/SdkBase.d.ts +16 -6
- package/build/src/runtime/store/SdkBase.js +54 -4
- package/build/src/runtime/store/SdkBase.js.map +1 -1
- package/build/src/runtime/store/SharedSdk.d.ts +8 -2
- package/build/src/runtime/store/SharedSdk.js +22 -8
- package/build/src/runtime/store/SharedSdk.js.map +1 -1
- package/build/src/runtime/store/StoreSdkNode.d.ts +1 -1
- package/build/src/runtime/store/StoreSdkWeb.d.ts +1 -1
- package/build/src/runtime/store/UsersSdk.d.ts +10 -4
- package/build/src/runtime/store/UsersSdk.js +12 -6
- package/build/src/runtime/store/UsersSdk.js.map +1 -1
- package/package.json +3 -4
- package/src/models/Backend.ts +7 -9
- package/src/models/HttpHistory.ts +2 -2
- package/src/models/Project.ts +2 -2
- package/src/models/RevisionInfo.ts +2 -2
- package/src/models/SerializableError.ts +1 -0
- package/src/models/store/File.ts +2 -2
- package/src/runtime/store/Errors.ts +100 -0
- package/src/runtime/store/FilesSdk.ts +302 -0
- package/src/runtime/store/HistorySdk.ts +42 -17
- package/src/runtime/store/RouteBuilder.ts +14 -28
- package/src/runtime/store/Sdk.ts +7 -7
- package/src/runtime/store/SdkBase.ts +63 -9
- package/src/runtime/store/SharedSdk.ts +27 -10
- package/src/runtime/store/StoreSdkNode.ts +1 -1
- package/src/runtime/store/StoreSdkWeb.ts +1 -1
- package/src/runtime/store/UsersSdk.ts +14 -8
- package/build/src/runtime/store/ProjectsSdk.d.ts +0 -43
- package/build/src/runtime/store/ProjectsSdk.js +0 -144
- package/build/src/runtime/store/ProjectsSdk.js.map +0 -1
- package/build/src/runtime/store/SpacesSdk.d.ts +0 -62
- package/build/src/runtime/store/SpacesSdk.js +0 -194
- package/build/src/runtime/store/SpacesSdk.js.map +0 -1
- package/json8-patch.d.ts +0 -270
- package/src/runtime/store/ProjectsSdk.ts +0 -147
- package/src/runtime/store/SpacesSdk.ts +0 -209
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import WebSocketNode from 'ws';
|
|
2
|
+
import { Patch } from '@api-client/json';
|
|
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: Patch.JsonPatch, media: false, request?: ISdkRequestOptions): Promise<Patch.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: Patch.JsonPatch, media: true, request?: ISdkRequestOptions): Promise<Patch.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 `@api-client/json` library
|
|
174
|
+
*/
|
|
175
|
+
async patch(key: string, value: Patch.JsonPatch, media?: boolean, request: ISdkRequestOptions = {}): Promise<Patch.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 Patch.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
|
+
}
|
|
@@ -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
|
|
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
|
|
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
|
|
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.
|
|
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
|
|
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
|
|
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);
|
|
@@ -3,45 +3,31 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export class RouteBuilder {
|
|
5
5
|
/**
|
|
6
|
-
* @returns The path to the /
|
|
6
|
+
* @returns The path to the /files route.
|
|
7
7
|
*/
|
|
8
|
-
static
|
|
9
|
-
return '/
|
|
8
|
+
static files(): string {
|
|
9
|
+
return '/files';
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* @returns The path to the /
|
|
13
|
+
* @returns The path to the /files/[id] route.
|
|
14
14
|
*/
|
|
15
|
-
static
|
|
16
|
-
return `/
|
|
15
|
+
static file(key: string): string {
|
|
16
|
+
return `/files/${key}`;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
|
-
* @returns The path to the /
|
|
20
|
+
* @returns The path to the /files/[id]/users route.
|
|
21
21
|
*/
|
|
22
|
-
static
|
|
23
|
-
return `/
|
|
22
|
+
static fileUsers(key: string): string {
|
|
23
|
+
return `/files/${key}/users`;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
* @returns The path to the /
|
|
27
|
+
* @returns The path to the /file/[id]/revisions route.
|
|
28
28
|
*/
|
|
29
|
-
static
|
|
30
|
-
return `/
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* @returns The path to the /spaces/[id]/projects/[id] route.
|
|
35
|
-
*/
|
|
36
|
-
static spaceProject(space: string, project: string): string {
|
|
37
|
-
return `/spaces/${space}/projects/${project}`;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* @returns The path to the /spaces/[id]/projects/[id]/revisions route.
|
|
42
|
-
*/
|
|
43
|
-
static projectRevisions(space: string, project: string): string {
|
|
44
|
-
return `/spaces/${space}/projects/${project}/revisions`;
|
|
29
|
+
static fileRevisions(file: string): string {
|
|
30
|
+
return `/file/${file}/revisions`;
|
|
45
31
|
}
|
|
46
32
|
|
|
47
33
|
/**
|
|
@@ -87,7 +73,7 @@ export class RouteBuilder {
|
|
|
87
73
|
return `/history/${key}`;
|
|
88
74
|
}
|
|
89
75
|
|
|
90
|
-
static
|
|
91
|
-
return '/shared
|
|
76
|
+
static shared(): string {
|
|
77
|
+
return '/shared';
|
|
92
78
|
}
|
|
93
79
|
}
|
package/src/runtime/store/Sdk.ts
CHANGED
|
@@ -3,8 +3,7 @@ import { Http } from './Http.js';
|
|
|
3
3
|
import { WsClient } from './WsClient.js';
|
|
4
4
|
import { AuthSdk } from './AuthSdk.js';
|
|
5
5
|
import { BackendSdk } from './BackendSdk.js';
|
|
6
|
-
import {
|
|
7
|
-
import { ProjectsSdk } from './ProjectsSdk.js';
|
|
6
|
+
import { FilesSdk } from './FilesSdk.js';
|
|
8
7
|
import { UsersSdk } from './UsersSdk.js';
|
|
9
8
|
import { HistorySdk } from './HistorySdk.js';
|
|
10
9
|
import { SharedSdk } from './SharedSdk.js';
|
|
@@ -30,11 +29,7 @@ export abstract class Sdk {
|
|
|
30
29
|
/**
|
|
31
30
|
* The user spaces features.
|
|
32
31
|
*/
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* The user projects features.
|
|
36
|
-
*/
|
|
37
|
-
project = new ProjectsSdk(this);
|
|
32
|
+
file = new FilesSdk(this);
|
|
38
33
|
/**
|
|
39
34
|
* Reads user information.
|
|
40
35
|
*/
|
|
@@ -56,6 +51,11 @@ export abstract class Sdk {
|
|
|
56
51
|
*/
|
|
57
52
|
shared = new SharedSdk(this);
|
|
58
53
|
|
|
54
|
+
/**
|
|
55
|
+
* When set it limits log output to minimum.
|
|
56
|
+
*/
|
|
57
|
+
silent = false;
|
|
58
|
+
|
|
59
59
|
|
|
60
60
|
[baseUriSymbol] = '';
|
|
61
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
|
|
5
|
-
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
|
6
|
-
headers?: Record<string, string>,
|
|
7
|
-
body?: string | Buffer;
|
|
5
|
+
export interface ISdkRequestOptions {
|
|
8
6
|
/**
|
|
9
|
-
*
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
}
|