-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathapi.ts
More file actions
264 lines (235 loc) · 7.88 KB
/
api.ts
File metadata and controls
264 lines (235 loc) · 7.88 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
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
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 getCourseSettingsApiUrl = (courseId: string) =>
`${getStudioBaseUrl()}/api/contentstore/v1/course_settings/${courseId}`;
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,
useReactMarkdownEditor: true,
useVideoGalleryFlow: false,
enableAuthzCourseAuthoring: 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);
}
export const getUserAgreementRecordApi = (agreementType: string) =>
`${getConfig().LMS_BASE_URL}/api/agreements/v1/agreement_record/${agreementType}`;
export async function getUserAgreementRecord(agreementType: string) {
const client = getAuthenticatedHttpClient();
const { data } = await client.get(getUserAgreementRecordApi(agreementType));
return camelCaseObject(data);
}
export async function updateUserAgreementRecord(agreementType: string) {
const client = getAuthenticatedHttpClient();
const { data } = await client.post(getUserAgreementRecordApi(agreementType));
return camelCaseObject(data);
}
export const getUserAgreementApi = (agreementType: string) =>
`${getConfig().LMS_BASE_URL}/api/agreements/v1/agreement/${agreementType}/`;
export async function getUserAgreement(agreementType: string) {
const client = getAuthenticatedHttpClient();
const { data } = await client.get(getUserAgreementApi(agreementType));
return camelCaseObject(data);
}
export interface CourseSettingsData {
aboutPageEditable: boolean;
canShowCertificateAvailableDateField: boolean;
courseDisplayName: string;
courseDisplayNameWithDefault: string;
creditEligibilityEnabled: boolean;
enableExtendedCourseDetails: boolean;
enrollmentEndEditable: boolean;
isCreditCourse: boolean;
isEntranceExamsEnabled: boolean;
isPrerequisiteCoursesEnabled: boolean;
languageOptions: [string, string][];
lmsLinkForAboutPage: string;
licensingEnabled: boolean;
marketingEnabled: boolean;
mfeProctoredExamSettingsUrl: string;
platformName: string;
possiblePreRequisiteCourses: {
courseKey: string;
displayName: string;
lmsLink: string;
number: string;
org: string;
rerunLink: string;
run: string;
url: string;
};
shortDescriptionEditable: boolean;
showMinGradeWarning: boolean;
sidebarHtmlEnabled: boolean;
upgradeDeadline: string | null;
}
/**
* Get course settings.
*/
export async function getCourseSettings(courseId: string): Promise<CourseSettingsData> {
const { data } = await getAuthenticatedHttpClient().get(getCourseSettingsApiUrl(courseId));
return camelCaseObject(data);
}