-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdepartures.ts
84 lines (74 loc) · 2.06 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
import type {
Context,
EagerCollection,
Json,
Resource,
SkipService,
} from "@skipruntime/core";
import { runService } from "@skipruntime/server";
import { PolledExternalService } 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 readConfig = (key: string, ifNone: string[]) =>
cs.config.getUnique(key, { ifNone }).join(",");
const params = {
page: 1,
year: readConfig("year", ["2016", "2017"]),
origin: readConfig("origin", ["MMR", "SYR"]),
asylum: readConfig("asylum", ["JOR", "LBN"]),
resettlement: readConfig("resettlement", ["NOR", "USA"]),
};
return context.useExternalResource({
service: "polled",
identifier: "unhcrDeparturesAPI",
params,
});
}
}
const service: SkipService<ResourceInputs, ResourceInputs> = {
initialData: { config: [] },
resources: {
departures: DeparturesResource,
},
externalServices: {
polled: new PolledExternalService({
unhcrDeparturesAPI: {
url: "https://api.unhcr.org/rsq/v1/departures",
interval: 10000,
conv: (data: Json) =>
(data as Result).results.map((v, idx) => [idx, [v]]),
options: { 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));