-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.ts
153 lines (132 loc) · 3.55 KB
/
request.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import fetch, {Response} from 'node-fetch';
import * as querystring from 'querystring';
import * as util from 'util';
type Region =
| '.com'
| '.asia'
| '.eu'
| '.ru'
type Language =
// "en" — English
| 'en'
// "ru" — Русский
| 'ru'
// "pl" — Polski
| 'pl'
// "de" — Deutsch
| 'de'
// "fr" — Français
| 'fr'
// "es" — Español
| 'es'
// "zh-cn" — 简体中文
| 'zh-cn'
// "zh-tw" — 繁體中文
| 'zh-tw'
// "tr" — Türkçe
| 'tr'
// "cs" — Čeština
| 'cs'
// "th" — ไทย (by default)
| 'th'
// "vi" — Tiếng Việt
| 'vi'
// "ko" — 한국어
| 'ko'
type HttpMethod = 'GET' | 'POST';
type RequestOptions = {
hostname: string;
language?: Language;
method?: HttpMethod;
path: string;
region?: Region;
}
class Request {
private appId: string | undefined;
region: Region;
language: Language;
userAgent = 'wotblitz-v1.3 (+/~https://github.com/CodeMan99/wotblitz.js)';
constructor(application_id?: string, region: Region = '.com', language: Language = 'en') {
this.appId = application_id;
this.region = region;
this.language = language;
}
get application_id(): string {
if (!this.appId) this.appId = process.env.APPLICATION_ID;
if (!this.appId) throw new Error('wotblitz/request: no APPLICATION_ID set in the environment');
return this.appId;
}
set application_id(appId: string) {
this.appId = appId;
}
/**
* WarGaming.net API request tool.
*
* @param {Object} options
* @param {string} options.hostname base host not including the TLD (example: "api.wotblitz")
* @param {string} [options.language=en] the response language, according to WarGaming.
* @param {string} [options.method=POST] the request method, normally "GET" or "POST"
* @param {string} options.path the request path part (example: "/wgn/servers/info/")
* @param {string} [options.region=.com] the region's top level domain part
* @param {Object} body use to specify the parameters of the route
* @returns {Promise<Object>} resolves to the "data" property of the request
* @see {@link https://developers.wargaming.net/documentation/guide/getting-started/|WarGaming.net Developer Room}
*/
async execute(options: RequestOptions, body: Record<string, any>): Promise<any> {
// assign defaults
options = Object.assign({
language: this.language,
method: 'POST',
region: this.region
}, options);
// Will throw error if `this.appId` is not set.
body.application_id = this.application_id;
body.language = options.language;
const url = 'https://' + options.hostname + options.region + options.path;
let request: Promise<Response>;
if (options.method !== 'GET') {
request = fetch(url, {
body: querystring.stringify(body),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': this.userAgent
},
method: options.method
});
} else {
request = fetch(url + '?' + querystring.stringify(body), {
headers: {
'User-Agent': this.userAgent
},
method: 'GET'
});
}
type ResponseError = {
code: number;
message: string;
field: string;
value: any;
}
type ResponseBody = {
status: 'ok';
data: any;
} | {
status: 'error';
error: ResponseError;
}
const response = await request;
const result: ResponseBody = await response.json();
switch (result.status) {
case 'ok':
return result.data;
case 'error': {
const e = result.error;
const message = util.format('%d %s: %s=%j', e.code, e.message, e.field, e.value);
throw new Error(message);
}
default:
return null;
}
}
}
export = Request;