-
Notifications
You must be signed in to change notification settings - Fork 12
/
server.ts
67 lines (63 loc) · 1.76 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
import * as express from 'express';
import * as cors from 'cors';
import * as donutTypes from './data/types.json';
import * as donuts from './data/donuts.json';
const app = express();
const port = 3000;
interface Donut {
title: string;
url: string;
img: string;
imgAlt: string;
id: string;
types: string[];
bannerUrl: string;
nutritionalFacts: string;
description: string;
}
app.use(cors());
app.use(express.static('static'));
app.get('/', (req, res) => {
res.json({
hello: 'Welcome to the DonutAPI.',
apis: [
{
api: 'donuts',
description:
"Get's all Donuts from the api. Can optionally provide type as a a CSV query param",
},
{
api: 'donuts/:id',
description: "Get's a donut from the api",
},
{
api: 'types',
description: "Get's all donut types the api",
},
],
});
});
app.get('/donuts', (req, res) => {
const type: string = req.query.type as string;
let ds: Donut[] = (donuts as unknown) as Donut[];
if (type) {
const types = type.split(',');
ds = ds.filter((donut) =>
donut.types.reduce((acc, type) => {
return acc || types.includes(type);
}, false),
);
}
res.json(ds);
});
app.get('/donuts/:donutId', (req, res) => {
const donutId = req.params.donutId;
const donut = ((donuts as unknown) as Donut[]).find(
(donut: Donut) => donut.id === donutId,
);
res.json(donut || {});
});
app.get('/types', (req, res) => res.json(donutTypes));
app.listen(port, () =>
console.log(`Example app listening at http://localhost:${port}`),
);