-
Notifications
You must be signed in to change notification settings - Fork 14
/
node_api_helpers.mjs
61 lines (54 loc) · 1.46 KB
/
node_api_helpers.mjs
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
import prettier from 'prettier';
import * as path from 'node:path';
import * as fs from 'node:fs';
import * as url from 'node:url';
export const API_LANGUAGES = ['de', 'en', 'es', 'fr', 'zh'];
const GW2_API_URL = 'https://api.guildwars2.com';
const AUTOGENERATE_HEADER = `
// This file is autogenerated by /generate_from_api.mjs
// Do not edit this file directly - it will be overwritten
`;
let prettier_options = null;
export async function initPrettier() {
prettier_options = await prettier.resolveConfig(
url.fileURLToPath(import.meta.url),
);
}
export async function writeSource(filepath, content) {
filepath = path.join('src', filepath);
let prettied = await prettier.format(AUTOGENERATE_HEADER + content, {
...prettier_options,
filepath,
});
console.log('Generating', filepath);
fs.writeFileSync(filepath, prettied, 'utf8');
}
const FETCH_OPTIONS = {
method: 'GET',
mode: 'cors',
credentials: 'omit',
redirect: 'error',
};
export async function fetch_api(path) {
let url = GW2_API_URL + path;
console.log('Fetching', url);
let retries = 3;
let res;
while (retries > 0) {
try {
res = await fetch(url, FETCH_OPTIONS);
break;
} catch (e) {
console.log(e);
if (retries <= 1) throw e;
}
retries--;
}
if (!res.ok) throw new Error(`HTTP Error: ${res.status}`);
let json = await res.json();
return json;
}
export function compare_strings(a, b) {
if (a === b) return 0;
return a < b ? -1 : 1;
}