-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathISO3166CountriesURL.ts
More file actions
56 lines (52 loc) · 1.98 KB
/
ISO3166CountriesURL.ts
File metadata and controls
56 lines (52 loc) · 1.98 KB
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
import { Output, array, length, object, string } from 'valibot';
import ExternalResource from '../../utils/data/models/ExternalResource';
import { isEnabledFilter } from '../../utils/filters';
import { testbedCountryCodes } from '../../utils/data/testbed-country-codes';
const CountriesInputDataSchema = array(
object({
cca2: string([length(2)]),
cca3: string([length(3)]),
name: object({
common: string(),
official: string(),
}),
})
);
type CountriesInputData = Output<typeof CountriesInputDataSchema>;
const CountrySchema = object({
id: string(),
displayName: string(),
englishName: string(),
nativeName: string(),
twoLetterISORegionName: string([length(2)]),
threeLetterISORegionName: string([length(3)]),
});
type Country = Output<typeof CountrySchema>;
const CountriesResponseSchema = array(CountrySchema);
export default new ExternalResource({
name: 'ISO3166CountriesURL',
uri: 'https://github.com/mledoze/countries/blob/master/countries.json?raw=true',
mime: 'application/json; charset=utf-8',
parsers: {
input: CountriesInputDataSchema,
async transform(countriesRaw: CountriesInputData, params?: Record<string, string>) {
const countries = countriesRaw.map((countryData) => {
return {
id: countryData.cca2,
displayName: countryData.name.common,
englishName: countryData.name.official,
nativeName: '',
twoLetterISORegionName: countryData.cca2,
threeLetterISORegionName: countryData.cca3,
};
});
if (isEnabledFilter(params, 'testbed')) {
return countries.filter((country: Country) =>
testbedCountryCodes.includes(country.threeLetterISORegionName)
);
}
return countries;
},
output: CountriesResponseSchema,
},
});