-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathapi.ts
516 lines (454 loc) · 15.6 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
import { camelCaseObject, getConfig, snakeCaseObject } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
const getApiBaseUrl = () => getConfig().STUDIO_BASE_URL;
/**
* Get the URL for the content library API.
*/
export const getContentLibraryApiUrl = (libraryId: string) => `${getApiBaseUrl()}/api/libraries/v2/${libraryId}/`;
/**
* Get the URL for create content in library.
*/
export const getCreateLibraryBlockUrl = (libraryId: string) => `${getApiBaseUrl()}/api/libraries/v2/${libraryId}/blocks/`;
/**
* Get the URL for the content library team API.
*/
export const getLibraryTeamApiUrl = (libraryId: string) => `${getApiBaseUrl()}/api/libraries/v2/${libraryId}/team/`;
/**
* Get the URL for updating/deleting a content library team member.
*/
export const getLibraryTeamMemberApiUrl = (libraryId: string, username: string) => `${getApiBaseUrl()}/api/libraries/v2/${libraryId}/team/user/${username}/`;
/**
* Get the URL for library block metadata.
*/
export const getLibraryBlockMetadataUrl = (usageKey: string) => `${getApiBaseUrl()}/api/libraries/v2/blocks/${usageKey}/`;
/**
* Get the URL for library block metadata.
*/
export const getLibraryBlockCollectionsUrl = (usageKey: string) => `${getLibraryBlockMetadataUrl(usageKey)}collections/`;
/**
* Get the URL for content library list API.
*/
export const getContentLibraryV2ListApiUrl = () => `${getApiBaseUrl()}/api/libraries/v2/`;
/**
* Get the URL for commit/revert changes in library.
*/
export const getCommitLibraryChangesUrl = (libraryId: string) => `${getApiBaseUrl()}/api/libraries/v2/${libraryId}/commit/`;
/**
* Get the URL for paste clipboard content into library.
*/
export const getLibraryPasteClipboardUrl = (libraryId: string) => `${getApiBaseUrl()}/api/libraries/v2/${libraryId}/paste_clipboard/`;
/**
* Get the URL for the xblock fields/metadata API.
*/
export const getXBlockFieldsApiUrl = (usageKey: string) => `${getApiBaseUrl()}/api/xblock/v2/xblocks/${usageKey}/fields/`;
export const getXBlockFieldsVersionApiUrl = (usageKey: string, version: string) => `${getApiBaseUrl()}/api/xblock/v2/xblocks/${usageKey}@${version}/fields/`;
/**
* Get the URL for the xblock OLX API
*/
export const getXBlockOLXApiUrl = (usageKey: string) => `${getLibraryBlockMetadataUrl(usageKey)}olx/`;
/**
* Get the URL for the xblock Publish API
*/
export const getXBlockPublishApiUrl = (usageKey: string) => `${getApiBaseUrl()}/api/libraries/v2/blocks/${usageKey}/publish/`;
/**
* Get the URL for the xblock Assets List API
*/
export const getXBlockAssetsApiUrl = (usageKey: string) => `${getApiBaseUrl()}/api/libraries/v2/blocks/${usageKey}/assets/`;
/**
* Get the URL for the Library Collections API.
*/
export const getLibraryCollectionsApiUrl = (libraryId: string) => `${getApiBaseUrl()}/api/libraries/v2/${libraryId}/collections/`;
/**
* Get the URL for the collection detail API.
*/
export const getLibraryCollectionApiUrl = (libraryId: string, collectionId: string) => `${getLibraryCollectionsApiUrl(libraryId)}${collectionId}/`;
/**
* Get the URL for the collection components API.
*/
export const getLibraryCollectionComponentApiUrl = (libraryId: string, collectionId: string) => `${getLibraryCollectionApiUrl(libraryId, collectionId)}components/`;
/**
* Get the API URL for restoring deleted collection.
*/
export const getLibraryCollectionRestoreApiUrl = (libraryId: string, collectionId: string) => `${getLibraryCollectionApiUrl(libraryId, collectionId)}restore/`;
/**
* Get the URL for the xblock api.
*/
export const getXBlockBaseApiUrl = () => `${getApiBaseUrl()}/xblock/`;
export interface ContentLibrary {
id: string;
type: string;
org: string;
slug: string;
title: string;
description: string;
numBlocks: number;
version: number;
lastPublished: string | null;
lastDraftCreated: string | null;
publishedBy: string | null;
lastDraftCreatedBy: string | null;
allowLti: boolean;
allowPublicLearning: boolean;
allowPublicRead: boolean;
hasUnpublishedChanges: boolean;
hasUnpublishedDeletes: boolean;
canEditLibrary: boolean;
license: string;
created: string | null;
updated: string | null;
}
export type LibraryAccessLevel = 'read' | 'author' | 'admin';
export interface LibraryTeamMember {
username: string;
email: string;
accessLevel: LibraryAccessLevel,
}
export interface AddLibraryTeamMember {
libraryId: string,
email: string;
accessLevel: LibraryAccessLevel,
}
export interface DeleteLibraryTeamMember {
libraryId: string,
username: string;
}
export interface UpdateLibraryTeamMember extends DeleteLibraryTeamMember {
accessLevel: LibraryAccessLevel,
}
export interface Collection {
id: number;
key: string;
title: string;
description: string;
enabled: boolean;
createdBy: string | null;
created: string;
modified: string;
learningPackage: number;
}
export interface LibraryBlockType {
blockType: string;
displayName: string;
}
export interface LibrariesV2Response {
next: string | null,
previous: string | null,
count: number,
numPages: number,
currentPage: number,
start: number,
results: ContentLibrary[],
}
export interface XBlockFields {
displayName: string;
metadata: Record<string, unknown>;
data: string;
}
/* Additional custom parameters for the API request. */
export interface GetLibrariesV2CustomParams {
/* (optional) Library type, default `complex` */
type?: string,
/* (optional) Page number of results */
page?: number,
/* (optional) The number of results on each page, default `50` */
pageSize?: number,
/* (optional) Whether pagination is supported, default `true` */
pagination?: boolean,
/* (optional) Library field to order results by. Prefix with '-' for descending */
order?: string,
/* (optional) Search query to filter v2 Libraries by */
search?: string,
}
export interface CreateBlockDataRequest {
libraryId: string;
blockType: string;
definitionId: string;
}
export interface DeleteBlockDataRequest {
usageKey: string;
}
export interface CollectionMetadata {
key: string;
title: string;
}
export interface LibraryBlockMetadata {
id: string;
blockType: string;
defKey: string | null;
displayName: string;
lastPublished: string | null;
publishedBy: string | null;
lastDraftCreated: string | null;
lastDraftCreatedBy: string | null,
hasUnpublishedChanges: boolean;
created: string | null;
modified: string | null;
tagsCount: number;
collections: CollectionMetadata[];
}
export interface UpdateLibraryDataRequest {
id: string;
title?: string;
description?: string;
allow_public_learning?: boolean;
allow_public_read?: boolean;
type?: string;
license?: string;
}
export interface LibraryPasteClipboardRequest {
libraryId: string;
blockId: string;
}
export interface UpdateXBlockFieldsRequest {
data?: unknown;
metadata?: {
display_name?: string;
};
}
export interface CreateLibraryCollectionDataRequest {
title: string;
description: string | null;
}
export type UpdateCollectionComponentsRequest = Partial<CreateLibraryCollectionDataRequest>;
/**
* Fetch a content library by its ID.
*/
export async function getContentLibrary(libraryId: string): Promise<ContentLibrary> {
const { data } = await getAuthenticatedHttpClient().get(getContentLibraryApiUrl(libraryId));
return camelCaseObject(data);
}
export async function createLibraryBlock({
libraryId,
blockType,
definitionId,
}: CreateBlockDataRequest): Promise<LibraryBlockMetadata> {
const client = getAuthenticatedHttpClient();
const { data } = await client.post(
getCreateLibraryBlockUrl(libraryId),
{
block_type: blockType,
definition_id: definitionId,
},
);
return camelCaseObject(data);
}
export async function deleteLibraryBlock({ usageKey }: DeleteBlockDataRequest): Promise<void> {
const client = getAuthenticatedHttpClient();
await client.delete(getLibraryBlockMetadataUrl(usageKey));
}
/**
* Update library metadata.
*/
export async function updateLibraryMetadata(libraryData: UpdateLibraryDataRequest): Promise<ContentLibrary> {
const client = getAuthenticatedHttpClient();
const { id: libraryId, ...updateData } = libraryData;
const { data } = await client.patch(getContentLibraryApiUrl(libraryId), updateData);
return camelCaseObject(data);
}
/**
* Get a list of content libraries.
*/
export async function getContentLibraryV2List(customParams: GetLibrariesV2CustomParams): Promise<LibrariesV2Response> {
// Set default params if not passed in
const customParamsDefaults = {
type: customParams.type || 'complex',
page: customParams.page || 1,
pageSize: customParams.pageSize || 50,
pagination: customParams.pagination !== undefined ? customParams.pagination : true,
order: customParams.order || 'title',
textSearch: customParams.search,
};
const customParamsFormated = snakeCaseObject(customParamsDefaults);
const { data } = await getAuthenticatedHttpClient()
.get(getContentLibraryV2ListApiUrl(), { params: customParamsFormated });
return camelCaseObject(data);
}
/**
* Commit library changes.
*/
export async function commitLibraryChanges(libraryId: string) {
const client = getAuthenticatedHttpClient();
await client.post(getCommitLibraryChangesUrl(libraryId));
}
/**
* Revert library changes.
*/
export async function revertLibraryChanges(libraryId: string) {
const client = getAuthenticatedHttpClient();
await client.delete(getCommitLibraryChangesUrl(libraryId));
}
/**
* Fetch content library's team by library ID.
*/
export async function getLibraryTeam(libraryId: string): Promise<LibraryTeamMember[]> {
const client = getAuthenticatedHttpClient();
const { data } = await client.get(getLibraryTeamApiUrl(libraryId));
return camelCaseObject(data);
}
/**
* Add a new member to the library's team by email.
*/
export async function addLibraryTeamMember(memberData: AddLibraryTeamMember): Promise<LibraryTeamMember> {
const client = getAuthenticatedHttpClient();
const url = getLibraryTeamApiUrl(memberData.libraryId);
const { data } = await client.post(url, snakeCaseObject(memberData));
return camelCaseObject(data);
}
/**
* Delete an existing member from the library's team by username.
*/
export async function deleteLibraryTeamMember(memberData: DeleteLibraryTeamMember): Promise<LibraryTeamMember> {
const client = getAuthenticatedHttpClient();
const url = getLibraryTeamMemberApiUrl(memberData.libraryId, memberData.username);
const { data } = await client.delete(url);
return camelCaseObject(data);
}
/**
* Update an existing member's access to the library's team by username.
*/
export async function updateLibraryTeamMember(memberData: UpdateLibraryTeamMember): Promise<LibraryTeamMember> {
const client = getAuthenticatedHttpClient();
const url = getLibraryTeamMemberApiUrl(memberData.libraryId, memberData.username);
const { data } = await client.put(url, snakeCaseObject(memberData));
return camelCaseObject(data);
}
/**
* Paste clipboard content into library.
*/
export async function libraryPasteClipboard({
libraryId,
blockId,
}: LibraryPasteClipboardRequest): Promise<LibraryBlockMetadata> {
const client = getAuthenticatedHttpClient();
const { data } = await client.post(
getLibraryPasteClipboardUrl(libraryId),
{
block_id: blockId,
},
);
return data;
}
/**
* Fetch library block metadata.
*/
export async function getLibraryBlockMetadata(usageKey: string): Promise<LibraryBlockMetadata> {
const { data } = await getAuthenticatedHttpClient().get(getLibraryBlockMetadataUrl(usageKey));
return camelCaseObject(data);
}
/**
* Fetch xblock fields.
*/
export async function getXBlockFields(usageKey: string, version: string = 'draft'): Promise<XBlockFields> {
const { data } = await getAuthenticatedHttpClient().get(getXBlockFieldsVersionApiUrl(usageKey, version));
return camelCaseObject(data);
}
/**
* Update xblock fields.
*/
export async function updateXBlockFields(usageKey: string, xblockData: UpdateXBlockFieldsRequest) {
const client = getAuthenticatedHttpClient();
await client.post(getXBlockFieldsApiUrl(usageKey), xblockData);
}
/**
* Create a library collection
*/
export async function createCollection(libraryId: string, collectionData: CreateLibraryCollectionDataRequest) {
const client = getAuthenticatedHttpClient();
const { data } = await client.post(getLibraryCollectionsApiUrl(libraryId), collectionData);
return camelCaseObject(data);
}
/**
* Fetch the OLX for the given XBlock.
*/
// istanbul ignore next
export async function getXBlockOLX(usageKey: string): Promise<string> {
const { data } = await getAuthenticatedHttpClient().get(getXBlockOLXApiUrl(usageKey));
return data.olx;
}
/**
* Set the OLX for the given XBlock.
* Returns the OLX as it was actually saved.
*/
// istanbul ignore next
export async function setXBlockOLX(usageKey: string, newOLX: string): Promise<string> {
const { data } = await getAuthenticatedHttpClient().post(getXBlockOLXApiUrl(usageKey), { olx: newOLX });
return data.olx;
}
/**
* Publish the given XBlock.
*/
export async function publishXBlock(usageKey: string) {
const client = getAuthenticatedHttpClient();
await client.post(getXBlockPublishApiUrl(usageKey));
}
/**
* Fetch the asset (static file) list for the given XBlock.
*/
// istanbul ignore next
export async function getXBlockAssets(usageKey: string): Promise<{ path: string; url: string; size: number }[]> {
const { data } = await getAuthenticatedHttpClient().get(getXBlockAssetsApiUrl(usageKey));
return data.files;
}
/**
* Delete a single asset file
*/
// istanbul ignore next
export async function deleteXBlockAsset(usageKey: string, path: string): Promise<void> {
await getAuthenticatedHttpClient().delete(getXBlockAssetsApiUrl(usageKey) + encodeURIComponent(path));
}
/**
* Get the collection metadata.
*/
export async function getCollectionMetadata(libraryId: string, collectionId: string): Promise<Collection> {
const { data } = await getAuthenticatedHttpClient().get(getLibraryCollectionApiUrl(libraryId, collectionId));
return camelCaseObject(data);
}
/**
* Update collection metadata.
*/
export async function updateCollectionMetadata(
libraryId: string,
collectionId: string,
collectionData: UpdateCollectionComponentsRequest,
) {
const client = getAuthenticatedHttpClient();
await client.patch(getLibraryCollectionApiUrl(libraryId, collectionId), collectionData);
}
/**
* Add components to collection.
*/
export async function addComponentsToCollection(libraryId: string, collectionId: string, usageKeys: string[]) {
await getAuthenticatedHttpClient().patch(getLibraryCollectionComponentApiUrl(libraryId, collectionId), {
usage_keys: usageKeys,
});
}
/**
* Remove components from collection.
*/
export async function removeComponentsFromCollection(libraryId: string, collectionId: string, usageKeys: string[]) {
await getAuthenticatedHttpClient().delete(getLibraryCollectionComponentApiUrl(libraryId, collectionId), {
data: { usage_keys: usageKeys },
});
}
/**
* Soft-Delete collection.
*/
export async function deleteCollection(libraryId: string, collectionId: string) {
const client = getAuthenticatedHttpClient();
await client.delete(getLibraryCollectionApiUrl(libraryId, collectionId));
}
/**
* Restore soft-deleted collection
*/
export async function restoreCollection(libraryId: string, collectionId: string) {
const client = getAuthenticatedHttpClient();
await client.post(getLibraryCollectionRestoreApiUrl(libraryId, collectionId));
}
/**
* Update component collections.
*/
export async function updateComponentCollections(usageKey: string, collectionKeys: string[]) {
await getAuthenticatedHttpClient().patch(getLibraryBlockCollectionsUrl(usageKey), {
collection_keys: collectionKeys,
});
}