-
Notifications
You must be signed in to change notification settings - Fork 0
/
country-iso-codes.ts
40 lines (37 loc) · 1.14 KB
/
country-iso-codes.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
import path from "path";
import { promises } from "fs";
import { writeLog } from "fast-node-logger";
interface GetCountryIsoCodesFnInput {
useCache: boolean;
}
export type CountryIsoCode = {
/** Text e.g. "Aland Islands" */
co: string;
/** two letters text e.g. "AX" */
c: string;
/** three letters e.g. "ALA" */
alpha3: string;
/** e.g. 248 */
countryCode: number;
};
/**@returns ISO 3166 defined internationally recognized codes of letters and/or numbers that we can use when we refer to countries and their subdivisions. [source](https://www.iso.org/iso-3166-country-codes.html) */
export async function getCountryIsoCodes({
useCache,
}: GetCountryIsoCodesFnInput): Promise<CountryIsoCode[]> {
writeLog(`getCountryIsoCodes()`, { level: "trace" });
const cacheLocation = path.join(
__dirname,
"..",
"..",
"lib",
"json",
"country-iso-codes.json",
);
if (useCache) {
const countryCodes = await promises.readFile(cacheLocation, "utf8");
return JSON.parse(countryCodes);
} else {
// TODO: getting countryCodes from internet
throw new Error("getting countryCodes from internet not implemented");
}
}