-
Notifications
You must be signed in to change notification settings - Fork 5
/
canvasAPI.ts
57 lines (52 loc) · 1.69 KB
/
canvasAPI.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
import { assertType } from 'typescript-is';
import { SetCanvasURLParameters, TestCanvasURLParameters } from '@mymicds/sdk';
import * as api from '../libs/api';
import * as canvas from '../libs/canvas';
import * as feeds from '../libs/feeds';
import * as jwt from '../libs/jwt';
import RoutesFunction from './routesFunction';
export default ((app, db, socketIO) => {
app.post('/canvas/test', async (req, res) => {
try {
assertType<TestCanvasURLParameters>(req.body);
const { isValid, url } = await canvas.verifyURL(req.body.url);
api.success(res, { valid: isValid, url });
} catch (err) {
api.error(res, err);
}
});
app.put('/canvas/url', jwt.requireLoggedIn, async (req, res) => {
try {
assertType<SetCanvasURLParameters>(req.body);
const { isValid, validURL } = await canvas.setURL(db, req.apiUser!, req.body.url);
socketIO.user(req.apiUser!, 'canvas', 'set-url', validURL);
api.success(res, { valid: isValid, url: validURL });
} catch (err) {
api.error(res, err);
}
});
app.get('/canvas/events', jwt.requireLoggedIn, async (req, res) => {
try {
const responseObj = await feeds.canvasCacheRetry(db, req.apiUser!);
api.success(res, responseObj);
} catch (err) {
api.error(res, err);
}
});
app.get('/canvas/classes', jwt.requireLoggedIn, async (req, res) => {
try {
const responseObj = await canvas.getClasses(db, req.apiUser!);
api.success(res, responseObj);
} catch (err) {
api.error(res, err);
}
});
app.get('/canvas/unique-events', jwt.requireScope('faculty'), async (req, res) => {
try {
const events = await canvas.getUniqueEvents(db);
api.success(res, { events });
} catch (err) {
api.error(res, err);
}
});
}) as RoutesFunction;