-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcourses.ts
70 lines (62 loc) · 1.76 KB
/
courses.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
/**
@module CoursesRoute
*/
import express, { Request } from 'express';
import { getCourseQuery } from '../helpers/gql';
const router = express.Router();
/**
* PPAPI proxy for course data
*/
router.get('/api', (req: Request<never, unknown, never, { courseID: string }, never>, res) => {
const r = fetch(process.env.PUBLIC_API_URL + 'courses/' + encodeURIComponent(req.query.courseID), {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
});
console.log(req.query.courseID);
r.then((response) => response.json()).then((data) => res.send(data.payload));
});
/**
* PPAPI proxy for course data
*/
router.post('/api/batch', (req: Request<never, unknown, { courses: string[] }, never>, res) => {
if (req.body.courses.length == 0) {
res.json({});
} else {
const r = fetch(process.env.PUBLIC_API_GRAPHQL_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: getCourseQuery(req.body.courses),
}),
});
r.then((response) => response.json()).then((data) =>
res.json(
Object.fromEntries(
Object.values(data.data)
.filter((x) => x !== null)
.map((x) => [(x as { id: string }).id, x]),
),
),
);
}
});
/**
* PPAPI proxy for grade distribution
*/
router.get('/api/grades', (req: Request<never, unknown, never, { department: string; number: string }>, res) => {
const r = fetch(
process.env.PUBLIC_API_URL +
'grades/raw?department=' +
encodeURIComponent(req.query.department) +
'&courseNumber=' +
req.query.number,
);
r.then((response) => response.json()).then((data) => {
res.send(data.payload);
});
});
export default router;