-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdepartures.ts
92 lines (82 loc) · 2.11 KB
/
departures.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
import type {
Context,
EagerCollection,
Resource,
SkipService,
} from "@skipruntime/core";
import { runService } from "@skipruntime/server";
import {
GenericExternalService,
Polled,
defaultParamEncoder,
} from "@skipruntime/helpers";
const platform: "wasm" | "native" =
process.env["SKIP_PLATFORM"] == "native" ? "native" : "wasm";
type Departure = {
year: string;
origin: string;
origin_name: string;
asylum: string;
asylum_name: string;
destination: string;
destination_name: string;
persons: string;
};
type Result = {
results: Departure[];
};
type ResourceInputs = { config: EagerCollection<string, (string | number)[]> };
class DeparturesResource implements Resource<ResourceInputs> {
instantiate(
cs: ResourceInputs,
context: Context,
): EagerCollection<number, Departure> {
const get = (name: string, def: string) => {
try {
return cs.config.getUnique(name).join(",");
} catch (_e) {
return def;
}
};
const params = {
page: 1,
year: get("year", "2016,2017"),
origin: get("origin", "MMR,SYR"),
asylum: get("asylum", "JOR,LBN"),
resettlement: get("resettlement", "NOR,USA"),
};
return context.useExternalResource({
service: "externalDeparturesAPI",
identifier: "departuresFromAPI",
params,
});
}
}
const service: SkipService<ResourceInputs, ResourceInputs> = {
initialData: { config: [] },
resources: {
departures: DeparturesResource,
},
externalServices: {
externalDeparturesAPI: new GenericExternalService({
departuresFromAPI: new Polled(
"https://api.unhcr.org/rsq/v1/departures",
10000,
(data: Result) => data.results.map((v, idx) => [idx, [v]]),
defaultParamEncoder,
{ timeout: 1500 },
),
}),
},
createGraph: (ic) => ic,
};
const server = await runService(service, {
control_port: 3591,
streaming_port: 3590,
platform,
});
async function shutdown() {
await server.close();
}
// eslint-disable-next-line @typescript-eslint/no-misused-promises
["SIGTERM", "SIGINT"].map((sig) => process.on(sig, shutdown));