-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
74 lines (59 loc) · 1.89 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
import "./cron.ts";
import { getAkvaplanistFromAdCsvExport } from "./fetch.ts";
import { sectionList } from "./constants.ts";
import { getAkvaplanistEntry, listAkvaplanists } from "./kv.ts";
import {
error,
extractParams,
ptrn,
response,
responseFromKvList,
} from "./api_helpers.ts";
import type { RequestHandler } from "./types.ts";
const ptrnPersonId = ptrn("/person/:id");
const listAkvaplanistsHandler = (req: Request) =>
responseFromKvList(listAkvaplanists(), req);
const listPriorAkvaplanistsHandler = async (req: Request) =>
response(
(await Array.fromAsync(listAkvaplanists())).filter(({ value: { prior } }) =>
prior === true
),
req,
);
const listEmployedAkvaplanistsHandler = async (req: Request) =>
response(
(await Array.fromAsync(listAkvaplanists())).filter(({ value: { prior } }) =>
prior !== true
),
req,
);
const personHandler = async (req: Request) => {
const { id } = extractParams<{ id: string }>(ptrnPersonId, req);
const apn = await getAkvaplanistEntry(id);
if (!apn) {
return error(404);
}
return response(apn, req);
};
const listFreshAkvaplanistsFromAdExportHandler = async (req: Request) =>
response(await getAkvaplanistFromAdCsvExport(), req);
const getSections = async () => await sectionList;
const listSectionsHandler = async (req: Request) =>
response(await getSections(), req);
const handlers = new Map<URLPattern, RequestHandler>([
[ptrn("/"), listEmployedAkvaplanistsHandler],
[ptrn("/all"), listAkvaplanistsHandler],
[ptrn("/fresh"), listFreshAkvaplanistsFromAdExportHandler],
[ptrn("/prior"), listPriorAkvaplanistsHandler],
[ptrnPersonId, personHandler],
[ptrn("/sections"), listSectionsHandler],
]);
const handler = (req: Request) => {
for (const [pattern, handler] of handlers) {
if (pattern.test(req.url)) {
return handler(req);
}
}
return error(400);
};
Deno.serve(handler);