-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.ts
63 lines (55 loc) · 1.74 KB
/
xml.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
import { z } from 'zod'
import { etlHelper, Format, Source } from '@caldwell619/etl-helper'
import { parseFloatStrict } from '@caldwell619/durable-parse-float'
import { xmlOutputSchema, xmlInputSchema } from './schema'
type ExchangeRateInput = z.infer<typeof xmlInputSchema>
type ExchangeRateOutput = z.infer<typeof xmlOutputSchema>
const validateInput = (input: ExchangeRateInput): boolean => {
const result = xmlInputSchema.safeParse(input)
if (!result.success) {
console.error(result.error)
// Do something with the error, reporting, DLQ, or if it meets your special criteria, return `true`
}
return result.success
}
const validateOutput = (output: ExchangeRateOutput): boolean => {
const result = xmlOutputSchema.safeParse(output)
if (!result.success) {
console.error(result.error)
// Do something with the error, reporting, DLQ, or if it meets your special criteria, return `true`
}
return result.success
}
const urlSource: Source<ExchangeRateInput> = {
url: 'http://api.nbp.pl/api/exchangerates/tables/A',
// XML needs to die in a hole somewhere
accessorKey: 'ArrayOfExchangeRatesTable.ExchangeRatesTable.0.Rates.0.Rate',
options: {
params: {
format: 'XML',
},
},
}
const urlProvidedNoTransformer = async () => {
await etlHelper<ExchangeRateInput, ExchangeRateOutput>({
source: urlSource,
format: Format.XML,
validateInput,
transformer(input) {
return {
currency: input.Currency[0],
code: input.Code[0],
mid: parseFloatStrict(input.Mid[0]),
ingestionDate: new Date(),
}
},
validateOutput,
persist: async outputs => {
console.log(outputs.length)
},
})
}
const xml = async () => {
await urlProvidedNoTransformer()
}
xml()