39501daa-c150-4435-9003-cfc8305326d0.js
6.58 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"use strict";
cc._RF.push(module, '395012qwVBENZADz8gwUybQ', 'HttpClient');
// script/network/HttpClient.ts
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpClient = exports.HttpError = void 0;
class HttpError extends Error {
constructor(status, message) {
super(message);
this.status = status;
}
}
exports.HttpError = HttpError;
class HttpClient {
constructor(baseUrl, maxConcurrency = 0) {
this.serverBaseUrl = "";
this.authorization = null;
this.maxConcurrency = 0;
this.currentRequestNum = 0;
this.pendingRequests = [];
this.serverBaseUrl = baseUrl;
this.maxConcurrency = maxConcurrency;
}
setAuthorization(authorization) {
this.authorization = authorization;
}
setResponsePreprocessor(func) {
this.responsePreprocessor = func;
}
get(path = "", timeout = 5000, headers) {
return this._handleRequest({
path,
method: "GET",
timeout,
headers
});
}
post(path = "", timeout = 5000, body, contentType, headers) {
return this._handleRequest({
path,
method: "POST",
timeout,
contentType,
headers
}, body);
}
put(path = "", timeout = 5000, body, contentType, headers) {
return this._handleRequest({
path,
method: "POST",
timeout,
contentType,
headers
}, body);
}
delete(path = "", timeout = 5000, headers) {
return this._handleRequest({
path,
method: "GET",
timeout,
headers
});
}
_handleRequest(request, body) {
let promise = new Promise((resolve, reject) => {
request.onSuccess = resolve;
request.onError = reject;
if (this.maxConcurrency > 0 && this.currentRequestNum >= this.maxConcurrency) {
this.pendingRequests.push(request);
}
else {
this._doRequest(request, body);
}
});
request.promise = promise;
if (request.abort) {
promise.abort = request.abort;
}
else {
promise.abort = () => {
let idx = this.pendingRequests.indexOf(request);
if (idx >= 0) {
this.pendingRequests.splice(idx, 1);
}
};
}
return promise;
}
_doRequest(request, body) {
this.currentRequestNum++;
const xhr = new XMLHttpRequest();
xhr.timeout = request.timeout;
xhr.open(request.method, this.serverBaseUrl + request.path, true);
if (request.contentType) {
xhr.setRequestHeader("Content-Type", request.contentType);
}
if (request.headers) {
for (let key in request.headers) {
xhr.setRequestHeader(key, request.headers[key]);
}
}
let abort = () => {
xhr.abort();
};
;
if (request.promise) {
request.promise.abort = abort;
}
else {
request.abort = abort;
}
xhr.onreadystatechange = () => {
if (xhr.readyState == 4) {
if (!request.onError)
return;
if (xhr.status == 200) {
let response = xhr.responseText ? xhr.responseText : xhr.response;
if (response && this.responsePreprocessor) {
response = this.responsePreprocessor(response);
}
request.onSuccess(response);
}
else {
request.onError(new HttpError(xhr.status, getHttpStatusError(xhr.status)));
}
if (this.pendingRequests.length) {
this._doRequest(this.pendingRequests.shift());
}
else {
this.currentRequestNum--;
}
}
};
xhr.ontimeout = () => {
if (request.onError) {
request.onError(new HttpError(408, getHttpStatusError(408)));
request.onError = null;
request.onSuccess = null;
if (this.pendingRequests.length) {
this._doRequest(this.pendingRequests.shift());
}
else {
this.currentRequestNum--;
}
}
};
xhr.onabort = () => {
if (request.onError) {
request.onError(new HttpError(-1, "Request abort."));
request.onError = null;
request.onSuccess = null;
if (this.pendingRequests.length) {
this._doRequest(this.pendingRequests.shift());
}
else {
this.currentRequestNum--;
}
}
};
if (this.authorization) {
xhr.setRequestHeader("auth-token", this.authorization);
}
xhr.send(body);
}
}
exports.HttpClient = HttpClient;
function getHttpStatusError(status) {
switch (status) {
case 400: return "Bad request.";
case 401: return "Unauthorized.";
case 402: return "Payment required.";
case 403: return "Forbidden.";
case 404: return "Not found.";
case 405: return "Method not allowed.";
case 406: return "Not acceptable.";
case 407: return "Proxy authentication required.";
case 408: return "Request timeout.";
case 409: return "Conflict.";
case 410: return "Gone.";
case 411: return "Length required.";
case 412: return "Precondition failed.";
case 413: return "Payload too large.";
case 414: return "URI too long.";
case 416: return "Requested range not satisfiable.";
case 417: return "Expectation failed.";
case 427: return "Precondition required.";
case 429: return "Too many requests.";
case 449: return "Retry after doing the appropriate action.";
case 500: return "Internal server error.";
case 501: return "Server does not support the functionality required to fulfill the request.";
case 502: return "Error response received from gateway.";
case 503: return "Temporarily overloaded.";
case 504: return "Timed out waiting for gateway.";
case 505: return "HTTP version not supported.";
}
return "Unknown error code: " + status;
}
cc._RF.pop();