forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
209 lines (188 loc) · 6.06 KB
/
api.ts
File metadata and controls
209 lines (188 loc) · 6.06 KB
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
import { camelCaseObject, getConfig, snakeCaseObject } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
const getStudioBaseUrl = () => getConfig().STUDIO_BASE_URL as string;
export const getCourseDetailsUrl = (courseId: string, username: string) => (
`${getConfig().LMS_BASE_URL}/api/courses/v1/courses/${courseId}?username=${username}`
);
export type CourseDetailsData = {
blocksUrl: string;
courseId: string;
effort?: string;
end?: string;
enrollmentEnd?: string;
enrollmentStart?: string;
hidden: boolean;
id: string;
invitationOnly: boolean;
isEnrolled: boolean;
media: Record<
'image' | 'course_image' | 'banner_image' | 'course_video',
Record<string, string | null>
>;
mobileAvailable: boolean;
name: string;
number: string;
org: string;
overview: string;
pacing: string;
shortDescription?: string;
start?: string;
startDisplay?: string;
startType?: string;
};
/**
* Get the URL to check the migration task status
*/
export const getModulestoreMigrationStatusUrl = (migrationId: string) => `${getStudioBaseUrl()}/api/modulestore_migrator/v1/migrations/${migrationId}/`;
/**
* Get the URL for bulk migrate content to libraries
*/
export const bulkModulestoreMigrateUrl = () => `${getStudioBaseUrl()}/api/modulestore_migrator/v1/bulk_migration/`;
/**
* Get the url for the API endpoint to get preview migration
*/
export const getPreviewModulestoreMigrationUrl = () => `${getStudioBaseUrl()}/api/modulestore_migrator/v1/migration_preview/`;
export const getApiWaffleFlagsUrl = (courseId?: string): string => {
const baseUrl = getStudioBaseUrl();
const apiPath = '/api/contentstore/v1/course_waffle_flags';
return courseId ? `${baseUrl}${apiPath}/${courseId}` : `${baseUrl}${apiPath}`;
};
export async function getCourseDetails(courseId: string, username: string): Promise<CourseDetailsData> {
const { data } = await getAuthenticatedHttpClient()
.get(getCourseDetailsUrl(courseId, username));
return {
id: data.course_id,
...camelCaseObject(data),
};
}
/**
* The default values of waffle flags, used while we're loading the "real"
* values from Studio's REST API, and/or if we fail to load them.
* May drift from edx-platform's actual defaults!
* TODO: clarify our strategy here: https://github.com/openedx/frontend-app-authoring/issues/2094
*/
export const waffleFlagDefaults = {
enableCourseOptimizer: false,
enableNotifications: false,
enableCourseOptimizerCheckPrevRunLinks: false,
useNewHomePage: true,
useNewCustomPages: true,
useNewScheduleDetailsPage: true,
useNewAdvancedSettingsPage: true,
useNewGradingPage: true,
useNewUpdatesPage: true,
useNewImportPage: false,
useNewExportPage: true,
useNewFilesUploadsPage: true,
useNewVideoUploadsPage: true,
useNewCourseOutlinePage: true,
useNewUnitPage: false,
useNewCourseTeamPage: true,
useNewCertificatesPage: true,
useNewTextbooksPage: true,
useNewGroupConfigurationsPage: true,
useReactMarkdownEditor: true,
useVideoGalleryFlow: false,
} as const;
export type WaffleFlagName = keyof typeof waffleFlagDefaults;
export type WaffleFlagsStatus = { id: string | undefined } & Record<WaffleFlagName, boolean>;
/**
* Get Waffle Flags from Studio's REST API.
* Don't use this directly; use the `useWaffleFlags()` hook.
*
* A `mockWaffleFlags()` method is available if you need to override this in
* tests.
*
* @param courseId Get the flags for a specific course, which may be different
* than the system-wide flags.
*/
export async function getWaffleFlags(courseId?: string): Promise<WaffleFlagsStatus> {
const { data } = await getAuthenticatedHttpClient()
.get(getApiWaffleFlagsUrl(courseId));
return {
id: data.course_id,
...camelCaseObject(data),
};
}
export interface MigrateParameters {
id: number;
source: string;
target: string;
compositionLevel: string;
repeatHandlingStrategy: 'update' | 'skip' | 'fork';
preserveUrlSlugs: boolean;
targetCollectionSlug: string;
forwardSourceToTarget: boolean;
isFailed: boolean;
targetCollection: {
key: string;
title: string;
} | null;
}
export interface MigrateTaskStatusData {
state: string;
stateText: string;
completedSteps: number;
totalSteps: number;
attempts: number;
created: string;
modified: string;
artifacts: string[];
uuid: string;
parameters: MigrateParameters[];
}
export interface BulkMigrateRequestData {
sources: string[];
target: string;
targetCollectionSlugList?: string[];
createCollections?: boolean;
compositionLevel?: string;
repeatHandlingStrategy?: string;
preserveUrlSlugs?: boolean;
forwardSourceToTarget?: boolean;
}
/**
* Get migration task status
*/
export async function getModulestoreMigrationStatus(
migrationId: string,
): Promise<MigrateTaskStatusData> {
const client = getAuthenticatedHttpClient();
const { data } = await client.get(getModulestoreMigrationStatusUrl(migrationId));
return camelCaseObject(data);
}
/**
* Bulk migrate content to libraries
*/
export async function bulkModulestoreMigrate(
requestData: BulkMigrateRequestData,
): Promise<MigrateTaskStatusData> {
const client = getAuthenticatedHttpClient();
const { data } = await client.post(bulkModulestoreMigrateUrl(), snakeCaseObject(requestData));
return camelCaseObject(data);
}
export interface PreviewMigrationInfo {
state: 'partial' | 'success' | 'block_limit_reached';
unsupportedBlocks: number;
unsupportedPercentage: number;
blocksLimit: number;
totalBlocks: number;
totalComponents: number;
sections: number;
subsections: number;
units: number;
}
/**
* Get the preview for a modulestore migration given a source key and a library key
*/
export async function getPreviewModulestoreMigration(
libraryKey: string,
sourceKey: string,
): Promise<PreviewMigrationInfo> {
const client = getAuthenticatedHttpClient();
const params = new URLSearchParams();
params.append('target_key', libraryKey);
params.append('source_key', sourceKey);
const { data } = await client.get(getPreviewModulestoreMigrationUrl(), { params });
return camelCaseObject(data);
}