-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
109 lines (95 loc) · 2.57 KB
/
server.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
import express, {Request, Response} from 'express'
import http from 'http'
import { Park } from 'index';
// eslint-disable-next-line
const Themeparks = require('themeparks');
const app = express()
const server = http.createServer(app)
const PORT = process.env.PORT || 3000
app.use(express.json({strict: true}))
const apiList = {
destinations: new Themeparks.DestinationsApi(),
entities: new Themeparks.EntitiesApi(),
};
app.get('/', (_req: Request, res: Response) => {
res.status(200).json({
status: 'ok',
})
})
app.get('/destinations', async (_req: Request, res: Response) => {
try {
const resp = await apiList.destinations.getDestinations();
res.status(200).json({
results: resp.destinations,
total: resp.destinations.length,
});
} catch (e) {
res.status(400).json({
status: 'error',
message: e.message,
})
}
})
app.get('/entity/:entityId', async (req: Request, res: Response) => {
try {
const resp = await apiList.entities.getEntity(req.params.entityId as string);
res.status(200).json(resp)
} catch (e) {
res.status(400).json({
status: 'error',
message: e.message,
})
}
})
app.get('/parks', async (_req: Request, res: Response) => {
const parks: Partial<Park>[] = []
try {
const destResp = await apiList.destinations.getDestinations();
for await (const dest of destResp.destinations) {
const childResp = await apiList.entities.getEntityChildren(dest.id);
for (const child of childResp.children) {
if (child.entityType === 'PARK') {
parks.push({
id: child.id,
name: child.name,
entityType: child.entityType,
})
}
}
}
res.status(200).json({
results: parks,
total: parks.length,
})
} catch (e) {
res.status(400).json({
status: 'error',
message: e.message,
})
}
})
app.get('/entity/:entityId/live', async (req: Request, res: Response) => {
try {
const resp = await apiList.entities.getEntityLiveData(req.params.entityId);
res.status(200).json(resp)
} catch (e) {
res.status(400).json({
status: 'error',
message: e.message,
})
}
})
app.get('/entity/:entityId/schedule', async (req: Request, res: Response) => {
try {
const resp = await apiList.entities.getEntityScheduleUpcoming(req.params.entityId);
res.status(200).json(resp)
} catch (e) {
res.status(400).json({
status: 'error',
message: e.message,
})
}
})
server.listen(PORT, () => {
console.log(`App started http://localhost:${PORT}`)
})