clashofclans.js 2.9.1 → 2.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/rest/RequestHandler.js +62 -28
- package/package.json +1 -1
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
26
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
27
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
@@ -17,7 +40,7 @@ var _RequestHandler_keyIndex;
|
|
|
17
40
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
41
|
exports.RequestHandler = void 0;
|
|
19
42
|
const https_1 = __importDefault(require("https"));
|
|
20
|
-
const node_fetch_1 =
|
|
43
|
+
const node_fetch_1 = __importStar(require("node-fetch"));
|
|
21
44
|
const HTTPError_1 = require("./HTTPError");
|
|
22
45
|
const Constants_1 = require("../util/Constants");
|
|
23
46
|
const Store_1 = require("../util/Store");
|
|
@@ -64,35 +87,46 @@ class RequestHandler {
|
|
|
64
87
|
return this.exec(path, options);
|
|
65
88
|
}
|
|
66
89
|
async exec(path, options = {}, retries = 0) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
90
|
+
try {
|
|
91
|
+
const res = await (0, node_fetch_1.default)(`${this.baseURL}${path}`, {
|
|
92
|
+
agent,
|
|
93
|
+
body: options.body,
|
|
94
|
+
method: options.method,
|
|
95
|
+
timeout: options.restRequestTimeout ?? this.restRequestTimeout,
|
|
96
|
+
headers: { 'Authorization': `Bearer ${this._key}`, 'Content-Type': 'application/json' }
|
|
97
|
+
});
|
|
98
|
+
if (res.status === 504 && retries < (options.retryLimit ?? this.retryLimit)) {
|
|
99
|
+
return await this.exec(path, options, ++retries);
|
|
100
|
+
}
|
|
101
|
+
const data = await res.json();
|
|
102
|
+
if (this.creds &&
|
|
103
|
+
res.status === 403 &&
|
|
104
|
+
data.reason === 'accessDenied.invalidIp' &&
|
|
105
|
+
retries < (options.retryLimit ?? this.retryLimit)) {
|
|
106
|
+
const keys = await this.reValidateKeys().then(() => () => this.login());
|
|
107
|
+
if (keys.length)
|
|
108
|
+
return await this.exec(path, options, ++retries);
|
|
109
|
+
}
|
|
110
|
+
const maxAge = Number(res.headers.get('cache-control')?.split('=')?.[1] ?? 0) * 1000;
|
|
111
|
+
if (res.status === 403 && !data?.message && this.rejectIfNotValid) {
|
|
112
|
+
throw new HTTPError_1.HTTPError(HTTPError_1.PrivateWarLogError, res.status, path, maxAge);
|
|
113
|
+
}
|
|
114
|
+
if (!res.ok && this.rejectIfNotValid) {
|
|
115
|
+
throw new HTTPError_1.HTTPError(data, res.status, path, maxAge, options.method);
|
|
116
|
+
}
|
|
117
|
+
if (this.cached && maxAge > 0 && options.cache !== false && res.ok) {
|
|
118
|
+
await this.cached.set(path, { data, ttl: Date.now() + maxAge, status: res.status }, maxAge);
|
|
119
|
+
}
|
|
120
|
+
return { data, maxAge, status: res.status, path, ok: res.status === 200 };
|
|
91
121
|
}
|
|
92
|
-
|
|
93
|
-
|
|
122
|
+
catch (error) {
|
|
123
|
+
if (error instanceof node_fetch_1.FetchError && error.type === 'request-timeout' && retries < (options.retryLimit ?? this.retryLimit)) {
|
|
124
|
+
return this.exec(path, options, ++retries);
|
|
125
|
+
}
|
|
126
|
+
if (this.rejectIfNotValid)
|
|
127
|
+
throw error;
|
|
128
|
+
return { data: { message: error.message }, maxAge: 0, status: 500, path, ok: false };
|
|
94
129
|
}
|
|
95
|
-
return { data, maxAge, status: res?.status ?? 504, path, ok: res?.status === 200 };
|
|
96
130
|
}
|
|
97
131
|
async init(options) {
|
|
98
132
|
if (!(options.email && options.password))
|