forked from tl-its-umich-edu/canvas-course-manager-next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.ts
184 lines (160 loc) · 6.48 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
import {
CanvasCourseBase, CanvasCourseSection, CanvasCourseSectionBase, CanvasEnrollment,
CanvasUserCondensed, CourseWithSections
} from './models/canvas.js'
import { ExternalUserSuccess } from './models/externalUser.js'
import { Globals, CsrfToken } from './models/models.js'
import handleErrors, { CanvasError } from './utils/handleErrors.js'
const jsonMimeType = 'application/json'
const addStateChangeCallHeaders = (csrfToken: string): RequestInit => {
const headers: Array<[string, string]> = [['Content-Type', jsonMimeType], ['Accept', jsonMimeType], ['x-csrf-token', csrfToken]]
const request: RequestInit = { headers }
return request
}
const getGet = (): RequestInit => {
const request: RequestInit = {}
request.method = 'GET'
return request
}
const getPost = (body: string, csrfToken: string): RequestInit => {
const request = addStateChangeCallHeaders(csrfToken)
request.method = 'POST'
request.body = body
return request
}
const getDelete = (body: string, csrfToken: string): RequestInit => {
const request = addStateChangeCallHeaders(csrfToken)
request.method = 'DELETE'
request.body = body
return request
}
// This currently assumes all put requests have a JSON payload and receive a JSON response.
const getPut = (body: string, csrfToken: string): RequestInit => {
const request = addStateChangeCallHeaders(csrfToken)
request.method = 'PUT'
request.body = body
return request
}
export const getCourse = async (courseId: number): Promise<CanvasCourseBase> => {
const request = getGet()
const resp = await fetch(`/api/course/${courseId}`, request)
await handleErrors(resp)
return await resp.json()
}
export const setCourseName = async (courseId: number, newName: string, csrfToken: string): Promise<CanvasCourseBase> => {
const request = getPut(JSON.stringify({ newName: newName }), csrfToken)
const resp = await fetch(`/api/course/${courseId}/name`, request)
await handleErrors(resp)
return await resp.json()
}
export const getGlobals = async (): Promise<Globals> => {
const request = getGet()
const resp = await fetch('/api/globals', request)
await handleErrors(resp)
return await resp.json()
}
export const getCourseSections = async (courseId: number): Promise<CanvasCourseSection[]> => {
const request = getGet()
const resp = await fetch('/api/course/' + courseId.toString() + '/sections', request)
await handleErrors(resp)
return await resp.json()
}
export const addCourseSections = async (courseId: number, sectionNames: string[], csrfToken: string): Promise<CanvasCourseSection[]> => {
const body = JSON.stringify({ sections: sectionNames })
const request = getPost(body, csrfToken)
const resp = await fetch('/api/course/' + courseId.toString() + '/sections', request)
await handleErrors(resp)
return await resp.json()
}
export interface AddSectionEnrollment {
loginId: string
role: string
}
interface AddEnrollmentWithSectionId extends AddSectionEnrollment {
sectionId: number
}
export const getStudentsEnrolledInSection = async (sectionId: number): Promise<string[]> => {
const request = getGet()
const resp = await fetch(`/api/sections/${sectionId}/students`, request)
await handleErrors(resp)
return await resp.json()
}
export const addSectionEnrollments = async (
sectionId: number, enrollments: AddSectionEnrollment[], csrfToken: string
): Promise<CanvasEnrollment[]> => {
const body = JSON.stringify({ users: enrollments })
const request = getPost(body, csrfToken)
const resp = await fetch(`/api/sections/${sectionId}/enroll`, request)
await handleErrors(resp)
return await resp.json()
}
export const addEnrollmentsToSections = async (enrollments: AddEnrollmentWithSectionId[], csrfToken: string): Promise<CanvasEnrollment[]> => {
const body = JSON.stringify({ enrollments })
const request = getPost(body, csrfToken)
const resp = await fetch('/api/sections/enroll', request)
await handleErrors(resp)
return await resp.json()
}
export const getCSRFTokenResponse = async (): Promise<CsrfToken> => {
const request = getGet()
const resp = await fetch('/auth/csrfToken', request)
await handleErrors(resp)
return await resp.json()
}
export const getTeacherSections = async (termId: number): Promise<CourseWithSections[]> => {
const request = getGet()
const resp = await fetch(`/api/instructor/sections?term_id=${termId}`, request)
await handleErrors(resp)
return await resp.json()
}
export const searchSections = async (termId: number, searchType: 'uniqname' | 'coursename', searchText: string): Promise<CourseWithSections[]> => {
const request = getGet()
const queryParam = searchType === 'uniqname' ? `instructor_name=${searchText}` : `course_name=${searchText}`
const resp = await fetch(`/api/admin/sections?term_id=${termId}&${queryParam}`, request)
await handleErrors(resp)
return await resp.json()
}
export const mergeSections = async (courseId: number, sectionsToMerge: CanvasCourseSection[], csrfToken: string): Promise<CanvasCourseSectionBase[]> => {
const body = JSON.stringify({ sectionIds: sectionsToMerge.map(section => { return section.id }) })
const request = getPost(body, csrfToken)
const resp = await fetch(`/api/course/${courseId}/sections/merge`, request)
await handleErrors(resp)
return await resp.json()
}
export const unmergeSections = async (sectionsToUnmerge: CanvasCourseSection[], csrfToken: string): Promise<CanvasCourseSectionBase[]> => {
const body = JSON.stringify({ sectionIds: sectionsToUnmerge.map(section => { return section.id }) })
const request = getDelete(body, csrfToken)
const resp = await fetch('/api/sections/unmerge', request)
await handleErrors(resp)
return await resp.json()
}
export const getUserInfo = async (loginId: string): Promise<CanvasUserCondensed | null> => {
const request = getGet()
const resp = await fetch(`/api/admin/user/${loginId}`, request)
try {
await handleErrors(resp)
} catch (error: unknown) {
if (
error instanceof CanvasError &&
error.errors.length > 0 &&
error.errors[0].canvasStatusCode === 404
) {
return null
} else {
throw error
}
}
return await resp.json()
}
interface ExternalUser {
email: string
surname: string
givenName: string
}
export const createExternalUsers = async (newUsers: ExternalUser[], csrfToken: string): Promise<ExternalUserSuccess[]> => {
const body = JSON.stringify({ users: newUsers })
const request = getPost(body, csrfToken)
const resp = await fetch('/api/admin/createExternalUsers', request)
await handleErrors(resp)
return await resp.json()
}