-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (65 loc) · 1.62 KB
/
index.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
const request = require('request-promise-native');
const apiFetch = function (input) {
return request.post({
headers: {
'content-type': 'application/json'
},
url: 'https://worldofwarcraft.com/graphql',
body: {
operationName: 'GetInitialRealmStatusData',
variables: {
input
},
extensions: {
persistedQuery: {
version: 1,
sha256Hash: '9c7cc66367037fda3007b7f592201c2610edb2c9a9292975cd131a37bbe61930'
}
}
},
json: true
});
};
const makeRegionGameVersionSlug = function (region, version = 'retail') {
if (version === 'classic') {
return `classic-${region}`;
}
if (version === 'bc') {
return `bcc-${region}`;
}
return region;
};
const fetchRealms = async function (region, version = 'retail') {
const compoundRegionGameVersionSlug = makeRegionGameVersionSlug(region, version);
const {data} = await apiFetch({compoundRegionGameVersionSlug});
const realmsRaw = data.Realms;
return realmsRaw.map(realm => {
let population;
if (!realm.population) {
population = '-';
} else if (realm.population.slug === 'recomended') {
population = 'very-low';
} else {
population = realm.population.slug;
}
return {
name: realm.name,
slug: realm.slug,
locale: realm.locale,
timezone: realm.timezone,
online: realm.online,
type: realm.type.slug,
population
};
});
};
const fetchRealm = async function (region, realm, version = 'retail') {
const realms = await fetchRealms(region, version);
return realms.find(r => {
return [r.name.toLowerCase(), r.slug.toLowerCase()].includes(realm.toLowerCase());
});
};
module.exports = {
fetchRealms,
fetchRealm
};