-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
117 lines (92 loc) · 2.76 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const request = require('request');
const url = require('url');
const utils = require('./utils');
const armory = {};
// Makes the request.
armory._get = (path, options, callback) => {
if (!options.apiKey) {
throw new Error('apiKey must be provided');
}
if (!options.region) {
throw new Error('region must be provided');
}
if (options.locale) {
options._query.locale = options.locale;
}
options.jar = false;
options.json = true;
options._query.apikey = options.apiKey;
options.uri = url.format({
protocol: 'https:',
hostname: `${options.region}.api.battle.net`,
pathname: encodeURI(`/wow${path}`),
query: options._query
});
let cb;
if (callback) {
cb = (err, res, body) => {
if (res.statusCode > 399 && body) {
err = err || new Error(body.detail);
}
callback(err, body, res);
};
}
return request(options, cb);
};
// Retrieves object describing a battle pet.
armory.battlePetStats = (options, callback) => {
let path = `/pet/stats/${options.id}`;
options._query = utils.pick(options, ['breedId', 'level', 'qualityId']);
return armory._get(path, options, callback);
};
// Retrieves an array of challenge mode leaderboard information.
armory.challengeRegion = (options, callback) => {
options.id = 'region';
return armory.challenge(options, callback);
};
// Retrieves array of realm status information.
armory.realmStatus = (options, callback) => {
let path = '/realm/status';
let cb;
if (options.id) {
options._query.realm = options.id;
}
if (callback) {
cb = (err, body, res) => {
let data = utils.getKey(body, 'realms');
callback(err, data, res);
};
}
return armory._get(path, options, cb);
};
// Retrieves an object describing a character or guild.
['character', 'guild'].forEach((method) => {
armory[method] = (options, callback) => {
if (options.fields) {
options._query.fields = options.fields;
}
if (options.lastModified) {
options.headers['If-Modified-Since'] = new Date(options.lastModified)
.toUTCString();
}
let path = `/${method}/${options.realm}/${options.id}`;
return armory._get(path, options, callback);
};
});
// Definitions for generic functions.
require('./methods').forEach((definition) => {
definition.url = definition.url || definition.method;
armory[definition.method] = (options, callback) => {
let id = options.id ? `/${options.id}` : '';
let path = `/${definition.url}${id}`;
let cb = callback;
if (callback && definition.key) {
cb = (err, body, res) => {
let data = utils.getKey(body, definition.key);
callback(err, data, res);
};
}
return armory._get(path, options, cb);
};
});
module.exports = utils.wrap(armory, utils.initParams);