generated from dfpc-coe/etl-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.ts
60 lines (48 loc) · 1.61 KB
/
task.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
import { Static, Type, TSchema } from '@sinclair/typebox';
import type { InputFeatureCollection } from '@tak-ps/etl'
import ETL, { Event, SchemaType, handler as internal, local, env } from '@tak-ps/etl';
const Environment = Type.Object({
URL: Type.String(),
QueryParams: Type.Optional(Type.Array(Type.Object({
key: Type.String(),
value: Type.String()
}))),
Headers: Type.Optional(Type.Array(Type.Object({
key: Type.String(),
value: Type.String()
}))),
});
export default class Task extends ETL {
static name = 'etl-georss';
async schema(type: SchemaType = SchemaType.Input): Promise<TSchema> {
if (type === SchemaType.Input) {
return Environment
} else {
return Type.Object({})
}
}
async control(): Promise<void> {
const env = await this.env(Environment);
const url = new URL(env.URL);
for (const param of env.QueryParams || []) {
url.searchParams.append(param.key, param.value);
}
const headers: Record<string, string> = {};
for (const header of env.Headers || []) {
headers[header.key] = header.value;
}
await fetch(url, {
method: 'GET',
headers
});
const fc: Static<typeof InputFeatureCollection> = {
type: 'FeatureCollection',
features: []
};
await this.submit(fc);
}
}
await local(new Task(import.meta.url), import.meta.url);
export async function handler(event: Event = {}) {
return await internal(new Task(import.meta.url), event);
}