@univerjs/network 0.1.0-beta.2 → 0.1.0-beta.4
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/{LICENSE.txt → LICENSE} +0 -2
- package/lib/cjs/index.js +2 -422
- package/lib/es/index.js +197 -354
- package/lib/umd/index.js +2 -422
- package/package.json +25 -22
package/{LICENSE.txt → LICENSE}
RENAMED
package/lib/cjs/index.js
CHANGED
|
@@ -1,422 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var
|
|
3
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
|
-
var __publicField = (obj, key, value) => {
|
|
5
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
6
|
-
return value;
|
|
7
|
-
};
|
|
8
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
9
|
-
const core = require("@univerjs/core");
|
|
10
|
-
const rxjs = require("rxjs");
|
|
11
|
-
const operators = require("rxjs/operators");
|
|
12
|
-
const redi = require("@wendellhu/redi");
|
|
13
|
-
const ApplicationJSONType = "application/json";
|
|
14
|
-
class HTTPHeaders {
|
|
15
|
-
constructor(headers) {
|
|
16
|
-
__publicField(this, "_headers", /* @__PURE__ */ new Map());
|
|
17
|
-
if (typeof headers === "string") {
|
|
18
|
-
headers.split("\n").forEach((header) => {
|
|
19
|
-
const [name, value] = header.split(":");
|
|
20
|
-
if (name && value) {
|
|
21
|
-
this._setHeader(name, value);
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
} else {
|
|
25
|
-
if (headers) {
|
|
26
|
-
Object.keys(headers).forEach(([name, value]) => {
|
|
27
|
-
this._setHeader(name, value);
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
forEach(callback) {
|
|
33
|
-
this._headers.forEach((v, key) => callback(key, v));
|
|
34
|
-
}
|
|
35
|
-
has(key) {
|
|
36
|
-
return !!this._headers.has(key.toLowerCase());
|
|
37
|
-
}
|
|
38
|
-
get(key) {
|
|
39
|
-
const k = key.toLowerCase();
|
|
40
|
-
return this._headers.has(k) ? this._headers.get(k) : null;
|
|
41
|
-
}
|
|
42
|
-
_setHeader(name, value) {
|
|
43
|
-
const lowerCase = name.toLowerCase();
|
|
44
|
-
if (this._headers.has(lowerCase)) {
|
|
45
|
-
this._headers.get(lowerCase).push(value.toString());
|
|
46
|
-
} else {
|
|
47
|
-
this._headers.set(lowerCase, [value.toString()]);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
const IHTTPImplementation = redi.createIdentifier("univer-pro.network.http-implementation");
|
|
52
|
-
class HTTPParams {
|
|
53
|
-
constructor(params) {
|
|
54
|
-
this.params = params;
|
|
55
|
-
}
|
|
56
|
-
toString() {
|
|
57
|
-
if (!this.params) {
|
|
58
|
-
return "";
|
|
59
|
-
}
|
|
60
|
-
return Object.keys(this.params).map((key) => `${key}=${this.params[key]}`).join("&");
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
class HTTPRequest {
|
|
64
|
-
constructor(method, url, requestParams) {
|
|
65
|
-
this.method = method;
|
|
66
|
-
this.url = url;
|
|
67
|
-
this.requestParams = requestParams;
|
|
68
|
-
}
|
|
69
|
-
get headers() {
|
|
70
|
-
return this.requestParams.headers;
|
|
71
|
-
}
|
|
72
|
-
get withCredentials() {
|
|
73
|
-
return this.requestParams.withCredentials;
|
|
74
|
-
}
|
|
75
|
-
get responseType() {
|
|
76
|
-
return this.requestParams.responseType;
|
|
77
|
-
}
|
|
78
|
-
getUrlWithParams() {
|
|
79
|
-
var _a, _b;
|
|
80
|
-
const params = (_b = (_a = this.requestParams) == null ? void 0 : _a.params) == null ? void 0 : _b.toString();
|
|
81
|
-
if (!params) {
|
|
82
|
-
return this.url;
|
|
83
|
-
}
|
|
84
|
-
return `${this.url}${this.url.includes("?") ? "&" : "?"}${params}`;
|
|
85
|
-
}
|
|
86
|
-
getBody() {
|
|
87
|
-
var _a;
|
|
88
|
-
const contentType = this.headers.get("Content-Type") ?? ApplicationJSONType;
|
|
89
|
-
const body = (_a = this.requestParams) == null ? void 0 : _a.body;
|
|
90
|
-
if (contentType === ApplicationJSONType && body && typeof body === "object") {
|
|
91
|
-
return JSON.stringify(body);
|
|
92
|
-
}
|
|
93
|
-
return body ? `${body}` : null;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
class HTTPResponse {
|
|
97
|
-
constructor({
|
|
98
|
-
body,
|
|
99
|
-
headers,
|
|
100
|
-
status,
|
|
101
|
-
statusText
|
|
102
|
-
}) {
|
|
103
|
-
__publicField(this, "body");
|
|
104
|
-
__publicField(this, "headers");
|
|
105
|
-
__publicField(this, "status");
|
|
106
|
-
__publicField(this, "statusText");
|
|
107
|
-
this.body = body;
|
|
108
|
-
this.headers = headers;
|
|
109
|
-
this.status = status;
|
|
110
|
-
this.statusText = statusText;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
class HTTPResponseError {
|
|
114
|
-
constructor({
|
|
115
|
-
headers,
|
|
116
|
-
status,
|
|
117
|
-
statusText,
|
|
118
|
-
error
|
|
119
|
-
}) {
|
|
120
|
-
__publicField(this, "headers");
|
|
121
|
-
__publicField(this, "status");
|
|
122
|
-
__publicField(this, "statusText");
|
|
123
|
-
__publicField(this, "error");
|
|
124
|
-
this.headers = headers;
|
|
125
|
-
this.status = status;
|
|
126
|
-
this.statusText = statusText;
|
|
127
|
-
this.error = error;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
class ResponseHeader {
|
|
131
|
-
constructor(headers, status, statusText) {
|
|
132
|
-
this.headers = headers;
|
|
133
|
-
this.status = status;
|
|
134
|
-
this.statusText = statusText;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
var __defProp2 = Object.defineProperty;
|
|
138
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
139
|
-
var __decorateClass = (decorators, target, key, kind) => {
|
|
140
|
-
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
141
|
-
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
142
|
-
if (decorator = decorators[i])
|
|
143
|
-
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
144
|
-
if (kind && result)
|
|
145
|
-
__defProp2(target, key, result);
|
|
146
|
-
return result;
|
|
147
|
-
};
|
|
148
|
-
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
149
|
-
exports.HTTPService = class HTTPService extends core.Disposable {
|
|
150
|
-
constructor(_http) {
|
|
151
|
-
super();
|
|
152
|
-
__publicField(this, "_interceptors", []);
|
|
153
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
154
|
-
__publicField(this, "_pipe");
|
|
155
|
-
this._http = _http;
|
|
156
|
-
}
|
|
157
|
-
registerHTTPInterceptor(interceptor) {
|
|
158
|
-
if (this._interceptors.indexOf(interceptor) !== -1) {
|
|
159
|
-
throw new Error("[HTTPService]: The interceptor has already been registered!");
|
|
160
|
-
}
|
|
161
|
-
this._interceptors.push(interceptor);
|
|
162
|
-
this._interceptors = this._interceptors.sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0));
|
|
163
|
-
this._pipe = null;
|
|
164
|
-
return core.toDisposable(() => core.remove(this._interceptors, interceptor));
|
|
165
|
-
}
|
|
166
|
-
get(url, options) {
|
|
167
|
-
return this._request("GET", url, options);
|
|
168
|
-
}
|
|
169
|
-
post(url, options) {
|
|
170
|
-
return this._request("POST", url, options);
|
|
171
|
-
}
|
|
172
|
-
put(url, options) {
|
|
173
|
-
return this._request("PUT", url, options);
|
|
174
|
-
}
|
|
175
|
-
delete(url, options) {
|
|
176
|
-
return this._request("DELETE", url, options);
|
|
177
|
-
}
|
|
178
|
-
/** The HTTP request implementations */
|
|
179
|
-
async _request(method, url, options) {
|
|
180
|
-
const headers = new HTTPHeaders(options == null ? void 0 : options.headers);
|
|
181
|
-
const params = new HTTPParams(options == null ? void 0 : options.params);
|
|
182
|
-
const request = new HTTPRequest(method, url, {
|
|
183
|
-
headers,
|
|
184
|
-
params,
|
|
185
|
-
withCredentials: (options == null ? void 0 : options.withCredentials) ?? false,
|
|
186
|
-
// default value for withCredentials is false by MDN
|
|
187
|
-
responseType: (options == null ? void 0 : options.responseType) ?? "json",
|
|
188
|
-
body: options == null ? void 0 : options.body
|
|
189
|
-
});
|
|
190
|
-
const events$ = rxjs.of(request).pipe(
|
|
191
|
-
operators.concatMap((request2) => this._runInterceptorsAndImplementation(request2))
|
|
192
|
-
);
|
|
193
|
-
const result = await rxjs.firstValueFrom(events$);
|
|
194
|
-
if (result instanceof HTTPResponse) {
|
|
195
|
-
return result;
|
|
196
|
-
}
|
|
197
|
-
throw new Error(`${result.error}`);
|
|
198
|
-
}
|
|
199
|
-
_runInterceptorsAndImplementation(request) {
|
|
200
|
-
if (!this._pipe) {
|
|
201
|
-
this._pipe = this._interceptors.map((handler) => handler.interceptor).reduceRight(
|
|
202
|
-
(nextHandlerFunction, interceptorFunction) => chainInterceptorFn(nextHandlerFunction, interceptorFunction),
|
|
203
|
-
(requestFromPrevInterceptor, finalHandler) => finalHandler(requestFromPrevInterceptor)
|
|
204
|
-
);
|
|
205
|
-
}
|
|
206
|
-
return this._pipe(
|
|
207
|
-
request,
|
|
208
|
-
(requestToNext) => this._http.send(requestToNext)
|
|
209
|
-
/* final handler */
|
|
210
|
-
);
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
exports.HTTPService = __decorateClass([
|
|
214
|
-
__decorateParam(0, IHTTPImplementation)
|
|
215
|
-
], exports.HTTPService);
|
|
216
|
-
function chainInterceptorFn(afterInterceptorChain, currentInterceptorFn) {
|
|
217
|
-
return (prevRequest, nextHandlerFn) => currentInterceptorFn(prevRequest, (nextRequest) => afterInterceptorChain(nextRequest, nextHandlerFn));
|
|
218
|
-
}
|
|
219
|
-
const SuccessStatusCodeLowerBound = 200;
|
|
220
|
-
const ErrorStatusCodeLowerBound = 300;
|
|
221
|
-
var HTTPStatusCode = /* @__PURE__ */ ((HTTPStatusCode2) => {
|
|
222
|
-
HTTPStatusCode2[HTTPStatusCode2["Continue"] = 100] = "Continue";
|
|
223
|
-
HTTPStatusCode2[HTTPStatusCode2["SwitchingProtocols"] = 101] = "SwitchingProtocols";
|
|
224
|
-
HTTPStatusCode2[HTTPStatusCode2["Processing"] = 102] = "Processing";
|
|
225
|
-
HTTPStatusCode2[HTTPStatusCode2["EarlyHints"] = 103] = "EarlyHints";
|
|
226
|
-
HTTPStatusCode2[HTTPStatusCode2["Ok"] = 200] = "Ok";
|
|
227
|
-
HTTPStatusCode2[HTTPStatusCode2["Created"] = 201] = "Created";
|
|
228
|
-
HTTPStatusCode2[HTTPStatusCode2["Accepted"] = 202] = "Accepted";
|
|
229
|
-
HTTPStatusCode2[HTTPStatusCode2["NonAuthoritativeInformation"] = 203] = "NonAuthoritativeInformation";
|
|
230
|
-
HTTPStatusCode2[HTTPStatusCode2["NoContent"] = 204] = "NoContent";
|
|
231
|
-
HTTPStatusCode2[HTTPStatusCode2["ResetContent"] = 205] = "ResetContent";
|
|
232
|
-
HTTPStatusCode2[HTTPStatusCode2["PartialContent"] = 206] = "PartialContent";
|
|
233
|
-
HTTPStatusCode2[HTTPStatusCode2["MultiStatus"] = 207] = "MultiStatus";
|
|
234
|
-
HTTPStatusCode2[HTTPStatusCode2["AlreadyReported"] = 208] = "AlreadyReported";
|
|
235
|
-
HTTPStatusCode2[HTTPStatusCode2["ImUsed"] = 226] = "ImUsed";
|
|
236
|
-
HTTPStatusCode2[HTTPStatusCode2["MultipleChoices"] = 300] = "MultipleChoices";
|
|
237
|
-
HTTPStatusCode2[HTTPStatusCode2["MovedPermanently"] = 301] = "MovedPermanently";
|
|
238
|
-
HTTPStatusCode2[HTTPStatusCode2["Found"] = 302] = "Found";
|
|
239
|
-
HTTPStatusCode2[HTTPStatusCode2["SeeOther"] = 303] = "SeeOther";
|
|
240
|
-
HTTPStatusCode2[HTTPStatusCode2["NotModified"] = 304] = "NotModified";
|
|
241
|
-
HTTPStatusCode2[HTTPStatusCode2["UseProxy"] = 305] = "UseProxy";
|
|
242
|
-
HTTPStatusCode2[HTTPStatusCode2["Unused"] = 306] = "Unused";
|
|
243
|
-
HTTPStatusCode2[HTTPStatusCode2["TemporaryRedirect"] = 307] = "TemporaryRedirect";
|
|
244
|
-
HTTPStatusCode2[HTTPStatusCode2["PermanentRedirect"] = 308] = "PermanentRedirect";
|
|
245
|
-
HTTPStatusCode2[HTTPStatusCode2["BadRequest"] = 400] = "BadRequest";
|
|
246
|
-
HTTPStatusCode2[HTTPStatusCode2["Unauthorized"] = 401] = "Unauthorized";
|
|
247
|
-
HTTPStatusCode2[HTTPStatusCode2["PaymentRequired"] = 402] = "PaymentRequired";
|
|
248
|
-
HTTPStatusCode2[HTTPStatusCode2["Forbidden"] = 403] = "Forbidden";
|
|
249
|
-
HTTPStatusCode2[HTTPStatusCode2["NotFound"] = 404] = "NotFound";
|
|
250
|
-
HTTPStatusCode2[HTTPStatusCode2["MethodNotAllowed"] = 405] = "MethodNotAllowed";
|
|
251
|
-
HTTPStatusCode2[HTTPStatusCode2["NotAcceptable"] = 406] = "NotAcceptable";
|
|
252
|
-
HTTPStatusCode2[HTTPStatusCode2["ProxyAuthenticationRequired"] = 407] = "ProxyAuthenticationRequired";
|
|
253
|
-
HTTPStatusCode2[HTTPStatusCode2["RequestTimeout"] = 408] = "RequestTimeout";
|
|
254
|
-
HTTPStatusCode2[HTTPStatusCode2["Conflict"] = 409] = "Conflict";
|
|
255
|
-
HTTPStatusCode2[HTTPStatusCode2["Gone"] = 410] = "Gone";
|
|
256
|
-
HTTPStatusCode2[HTTPStatusCode2["LengthRequired"] = 411] = "LengthRequired";
|
|
257
|
-
HTTPStatusCode2[HTTPStatusCode2["PreconditionFailed"] = 412] = "PreconditionFailed";
|
|
258
|
-
HTTPStatusCode2[HTTPStatusCode2["PayloadTooLarge"] = 413] = "PayloadTooLarge";
|
|
259
|
-
HTTPStatusCode2[HTTPStatusCode2["UriTooLong"] = 414] = "UriTooLong";
|
|
260
|
-
HTTPStatusCode2[HTTPStatusCode2["UnsupportedMediaType"] = 415] = "UnsupportedMediaType";
|
|
261
|
-
HTTPStatusCode2[HTTPStatusCode2["RangeNotSatisfiable"] = 416] = "RangeNotSatisfiable";
|
|
262
|
-
HTTPStatusCode2[HTTPStatusCode2["ExpectationFailed"] = 417] = "ExpectationFailed";
|
|
263
|
-
HTTPStatusCode2[HTTPStatusCode2["ImATeapot"] = 418] = "ImATeapot";
|
|
264
|
-
HTTPStatusCode2[HTTPStatusCode2["MisdirectedRequest"] = 421] = "MisdirectedRequest";
|
|
265
|
-
HTTPStatusCode2[HTTPStatusCode2["UnprocessableEntity"] = 422] = "UnprocessableEntity";
|
|
266
|
-
HTTPStatusCode2[HTTPStatusCode2["Locked"] = 423] = "Locked";
|
|
267
|
-
HTTPStatusCode2[HTTPStatusCode2["FailedDependency"] = 424] = "FailedDependency";
|
|
268
|
-
HTTPStatusCode2[HTTPStatusCode2["TooEarly"] = 425] = "TooEarly";
|
|
269
|
-
HTTPStatusCode2[HTTPStatusCode2["UpgradeRequired"] = 426] = "UpgradeRequired";
|
|
270
|
-
HTTPStatusCode2[HTTPStatusCode2["PreconditionRequired"] = 428] = "PreconditionRequired";
|
|
271
|
-
HTTPStatusCode2[HTTPStatusCode2["TooManyRequests"] = 429] = "TooManyRequests";
|
|
272
|
-
HTTPStatusCode2[HTTPStatusCode2["RequestHeaderFieldsTooLarge"] = 431] = "RequestHeaderFieldsTooLarge";
|
|
273
|
-
HTTPStatusCode2[HTTPStatusCode2["UnavailableForLegalReasons"] = 451] = "UnavailableForLegalReasons";
|
|
274
|
-
HTTPStatusCode2[HTTPStatusCode2["InternalServerError"] = 500] = "InternalServerError";
|
|
275
|
-
HTTPStatusCode2[HTTPStatusCode2["NotImplemented"] = 501] = "NotImplemented";
|
|
276
|
-
HTTPStatusCode2[HTTPStatusCode2["BadGateway"] = 502] = "BadGateway";
|
|
277
|
-
HTTPStatusCode2[HTTPStatusCode2["ServiceUnavailable"] = 503] = "ServiceUnavailable";
|
|
278
|
-
HTTPStatusCode2[HTTPStatusCode2["GatewayTimeout"] = 504] = "GatewayTimeout";
|
|
279
|
-
HTTPStatusCode2[HTTPStatusCode2["HttpVersionNotSupported"] = 505] = "HttpVersionNotSupported";
|
|
280
|
-
HTTPStatusCode2[HTTPStatusCode2["VariantAlsoNegotiates"] = 506] = "VariantAlsoNegotiates";
|
|
281
|
-
HTTPStatusCode2[HTTPStatusCode2["InsufficientStorage"] = 507] = "InsufficientStorage";
|
|
282
|
-
HTTPStatusCode2[HTTPStatusCode2["LoopDetected"] = 508] = "LoopDetected";
|
|
283
|
-
HTTPStatusCode2[HTTPStatusCode2["NotExtended"] = 510] = "NotExtended";
|
|
284
|
-
HTTPStatusCode2[HTTPStatusCode2["NetworkAuthenticationRequired"] = 511] = "NetworkAuthenticationRequired";
|
|
285
|
-
return HTTPStatusCode2;
|
|
286
|
-
})(HTTPStatusCode || {});
|
|
287
|
-
class XHRHTTPImplementation {
|
|
288
|
-
send(request) {
|
|
289
|
-
return new rxjs.Observable((observer) => {
|
|
290
|
-
const xhr = new XMLHttpRequest();
|
|
291
|
-
xhr.open(request.method, request.getUrlWithParams());
|
|
292
|
-
if (request.withCredentials) {
|
|
293
|
-
xhr.withCredentials = true;
|
|
294
|
-
}
|
|
295
|
-
request.headers.forEach((key, value) => xhr.setRequestHeader(key, value.join(",")));
|
|
296
|
-
if (!request.headers.has("Accept")) {
|
|
297
|
-
xhr.setRequestHeader("Accept", "application/json, text/plain, */*");
|
|
298
|
-
}
|
|
299
|
-
if (!request.headers.has("Content-Type")) {
|
|
300
|
-
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
|
|
301
|
-
}
|
|
302
|
-
const buildResponseHeader = () => {
|
|
303
|
-
const statusText = xhr.statusText || "OK";
|
|
304
|
-
const headers = new HTTPHeaders(xhr.getAllResponseHeaders());
|
|
305
|
-
return new ResponseHeader(headers, xhr.status, statusText);
|
|
306
|
-
};
|
|
307
|
-
const onLoadHandler = () => {
|
|
308
|
-
const { headers, statusText, status } = buildResponseHeader();
|
|
309
|
-
const { responseType } = request;
|
|
310
|
-
let body2 = null;
|
|
311
|
-
let error = null;
|
|
312
|
-
if (status !== HTTPStatusCode.NoContent) {
|
|
313
|
-
body2 = typeof xhr.response === "undefined" ? xhr.responseText : xhr.response;
|
|
314
|
-
}
|
|
315
|
-
let success = status >= SuccessStatusCodeLowerBound && status < ErrorStatusCodeLowerBound;
|
|
316
|
-
if (responseType === "json" && typeof body2 === "string") {
|
|
317
|
-
const originalBody = body2;
|
|
318
|
-
try {
|
|
319
|
-
body2 = body2 ? JSON.parse(body2) : null;
|
|
320
|
-
} catch (e) {
|
|
321
|
-
success = false;
|
|
322
|
-
body2 = originalBody;
|
|
323
|
-
error = e;
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
if (success) {
|
|
327
|
-
observer.next(
|
|
328
|
-
new HTTPResponse({
|
|
329
|
-
body: body2,
|
|
330
|
-
headers,
|
|
331
|
-
status,
|
|
332
|
-
statusText
|
|
333
|
-
})
|
|
334
|
-
);
|
|
335
|
-
} else {
|
|
336
|
-
observer.next(
|
|
337
|
-
new HTTPResponseError({
|
|
338
|
-
error,
|
|
339
|
-
headers,
|
|
340
|
-
status,
|
|
341
|
-
statusText
|
|
342
|
-
})
|
|
343
|
-
);
|
|
344
|
-
}
|
|
345
|
-
};
|
|
346
|
-
const onErrorHandler = (error) => {
|
|
347
|
-
const res = new HTTPResponseError({
|
|
348
|
-
error,
|
|
349
|
-
status: xhr.status || 0,
|
|
350
|
-
statusText: xhr.statusText || "Unknown Error",
|
|
351
|
-
headers: buildResponseHeader().headers
|
|
352
|
-
});
|
|
353
|
-
observer.next(res);
|
|
354
|
-
};
|
|
355
|
-
xhr.addEventListener("load", onLoadHandler);
|
|
356
|
-
xhr.addEventListener("error", onErrorHandler);
|
|
357
|
-
xhr.addEventListener("abort", onErrorHandler);
|
|
358
|
-
xhr.addEventListener("timeout", onErrorHandler);
|
|
359
|
-
const body = request.getBody();
|
|
360
|
-
xhr.send(body);
|
|
361
|
-
return () => {
|
|
362
|
-
if (xhr.readyState !== xhr.DONE) {
|
|
363
|
-
xhr.abort();
|
|
364
|
-
}
|
|
365
|
-
xhr.removeEventListener("load", onLoadHandler);
|
|
366
|
-
xhr.removeEventListener("error", onErrorHandler);
|
|
367
|
-
xhr.removeEventListener("abort", onErrorHandler);
|
|
368
|
-
xhr.removeEventListener("timeout", onErrorHandler);
|
|
369
|
-
};
|
|
370
|
-
});
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
const ISocketService = redi.createIdentifier("univer.socket");
|
|
374
|
-
class WebSocketService extends core.Disposable {
|
|
375
|
-
createSocket(URL) {
|
|
376
|
-
try {
|
|
377
|
-
const connection = new WebSocket(URL);
|
|
378
|
-
const disposables = new core.DisposableCollection();
|
|
379
|
-
const webSocket = {
|
|
380
|
-
URL,
|
|
381
|
-
close: (code, reason) => {
|
|
382
|
-
connection.close(code, reason);
|
|
383
|
-
disposables.dispose();
|
|
384
|
-
},
|
|
385
|
-
send: (data) => {
|
|
386
|
-
connection.send(data);
|
|
387
|
-
},
|
|
388
|
-
open$: new rxjs.Observable((subscriber) => {
|
|
389
|
-
const callback = (event) => subscriber.next(event);
|
|
390
|
-
connection.addEventListener("open", callback);
|
|
391
|
-
disposables.add(core.toDisposable(() => connection.removeEventListener("open", callback)));
|
|
392
|
-
}).pipe(operators.share()),
|
|
393
|
-
close$: new rxjs.Observable((subscriber) => {
|
|
394
|
-
const callback = (event) => subscriber.next(event);
|
|
395
|
-
connection.addEventListener("close", callback);
|
|
396
|
-
disposables.add(core.toDisposable(() => connection.removeEventListener("close", callback)));
|
|
397
|
-
}).pipe(operators.share()),
|
|
398
|
-
error$: new rxjs.Observable((subscriber) => {
|
|
399
|
-
const callback = (event) => subscriber.next(event);
|
|
400
|
-
connection.addEventListener("error", callback);
|
|
401
|
-
disposables.add(core.toDisposable(() => connection.removeEventListener("error", callback)));
|
|
402
|
-
}).pipe(operators.share()),
|
|
403
|
-
message$: new rxjs.Observable((subscriber) => {
|
|
404
|
-
const callback = (event) => subscriber.next(event);
|
|
405
|
-
connection.addEventListener("message", callback);
|
|
406
|
-
disposables.add(core.toDisposable(() => connection.removeEventListener("message", callback)));
|
|
407
|
-
}).pipe(operators.share())
|
|
408
|
-
};
|
|
409
|
-
return webSocket;
|
|
410
|
-
} catch (e) {
|
|
411
|
-
console.error(e);
|
|
412
|
-
return null;
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
exports.HTTPHeaders = HTTPHeaders;
|
|
417
|
-
exports.HTTPRequest = HTTPRequest;
|
|
418
|
-
exports.HTTPResponse = HTTPResponse;
|
|
419
|
-
exports.IHTTPImplementation = IHTTPImplementation;
|
|
420
|
-
exports.ISocketService = ISocketService;
|
|
421
|
-
exports.WebSocketService = WebSocketService;
|
|
422
|
-
exports.XHRHTTPImplementation = XHRHTTPImplementation;
|
|
1
|
+
"use strict";var A=Object.defineProperty;var U=(e,t,n)=>t in e?A(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var c=(e,t,n)=>(U(e,typeof t!="symbol"?t+"":t,n),n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@univerjs/core"),y=require("rxjs"),v=require("rxjs/operators"),q=require("@wendellhu/redi"),E="application/json";class f{constructor(t){c(this,"_headers",new Map);typeof t=="string"?t.split(`
|
|
2
|
+
`).forEach(n=>{const[r,i]=n.split(":");r&&i&&this._setHeader(r,i)}):t&&Object.keys(t).forEach(([n,r])=>{this._setHeader(n,r)})}forEach(t){this._headers.forEach((n,r)=>t(r,n))}has(t){return!!this._headers.has(t.toLowerCase())}get(t){const n=t.toLowerCase();return this._headers.has(n)?this._headers.get(n):null}_setHeader(t,n){const r=t.toLowerCase();this._headers.has(r)?this._headers.get(r).push(n.toString()):this._headers.set(r,[n.toString()])}}const L=q.createIdentifier("univer-pro.network.http-implementation");class O{constructor(t){this.params=t}toString(){return this.params?Object.keys(this.params).map(t=>`${t}=${this.params[t]}`).join("&"):""}}class u{constructor(t,n,r){this.method=t,this.url=n,this.requestParams=r}get headers(){return this.requestParams.headers}get withCredentials(){return this.requestParams.withCredentials}get responseType(){return this.requestParams.responseType}getUrlWithParams(){var n,r;const t=(r=(n=this.requestParams)==null?void 0:n.params)==null?void 0:r.toString();return t?`${this.url}${this.url.includes("?")?"&":"?"}${t}`:this.url}getBody(){var r,i;const t=(r=this.headers.get("Content-Type"))!=null?r:E,n=(i=this.requestParams)==null?void 0:i.body;return t===E&&n&&typeof n=="object"?JSON.stringify(n):n?`${n}`:null}}class g{constructor({body:t,headers:n,status:r,statusText:i}){c(this,"body");c(this,"headers");c(this,"status");c(this,"statusText");this.body=t,this.headers=n,this.status=r,this.statusText=i}}class _{constructor({headers:t,status:n,statusText:r,error:i}){c(this,"headers");c(this,"status");c(this,"statusText");c(this,"error");this.headers=t,this.status=n,this.statusText=r,this.error=i}}class M{constructor(t,n,r){this.headers=t,this.status=n,this.statusText=r}}var F=Object.defineProperty,k=Object.getOwnPropertyDescriptor,D=(e,t,n,r)=>{for(var i=r>1?void 0:r?k(t,n):t,o=e.length-1,s;o>=0;o--)(s=e[o])&&(i=(r?s(t,n,i):s(i))||i);return r&&i&&F(t,n,i),i},j=(e,t)=>(n,r)=>t(n,r,e);exports.HTTPService=class extends d.Disposable{constructor(n){super();c(this,"_interceptors",[]);c(this,"_pipe");this._http=n}registerHTTPInterceptor(n){if(this._interceptors.indexOf(n)!==-1)throw new Error("[HTTPService]: The interceptor has already been registered!");return this._interceptors.push(n),this._interceptors=this._interceptors.sort((r,i)=>{var o,s;return((o=r.priority)!=null?o:0)-((s=i.priority)!=null?s:0)}),this._pipe=null,d.toDisposable(()=>d.remove(this._interceptors,n))}get(n,r){return this._request("GET",n,r)}post(n,r){return this._request("POST",n,r)}put(n,r){return this._request("PUT",n,r)}delete(n,r){return this._request("DELETE",n,r)}async _request(n,r,i){var m,b;const o=new f(i==null?void 0:i.headers),s=new O(i==null?void 0:i.params),l=new u(n,r,{headers:o,params:s,withCredentials:(m=i==null?void 0:i.withCredentials)!=null?m:!1,responseType:(b=i==null?void 0:i.responseType)!=null?b:"json",body:i==null?void 0:i.body}),p=y.of(l).pipe(v.concatMap(h=>this._runInterceptorsAndImplementation(h))),a=await y.firstValueFrom(p);if(a instanceof g)return a;throw new Error(`${a.error}`)}_runInterceptorsAndImplementation(n){return this._pipe||(this._pipe=this._interceptors.map(r=>r.interceptor).reduceRight((r,i)=>$(r,i),(r,i)=>i(r))),this._pipe(n,r=>this._http.send(r))}};exports.HTTPService=D([j(0,L)],exports.HTTPService);function $(e,t){return(n,r)=>t(n,i=>e(i,r))}const B=200,G=300;var x=(e=>(e[e.Continue=100]="Continue",e[e.SwitchingProtocols=101]="SwitchingProtocols",e[e.Processing=102]="Processing",e[e.EarlyHints=103]="EarlyHints",e[e.Ok=200]="Ok",e[e.Created=201]="Created",e[e.Accepted=202]="Accepted",e[e.NonAuthoritativeInformation=203]="NonAuthoritativeInformation",e[e.NoContent=204]="NoContent",e[e.ResetContent=205]="ResetContent",e[e.PartialContent=206]="PartialContent",e[e.MultiStatus=207]="MultiStatus",e[e.AlreadyReported=208]="AlreadyReported",e[e.ImUsed=226]="ImUsed",e[e.MultipleChoices=300]="MultipleChoices",e[e.MovedPermanently=301]="MovedPermanently",e[e.Found=302]="Found",e[e.SeeOther=303]="SeeOther",e[e.NotModified=304]="NotModified",e[e.UseProxy=305]="UseProxy",e[e.Unused=306]="Unused",e[e.TemporaryRedirect=307]="TemporaryRedirect",e[e.PermanentRedirect=308]="PermanentRedirect",e[e.BadRequest=400]="BadRequest",e[e.Unauthorized=401]="Unauthorized",e[e.PaymentRequired=402]="PaymentRequired",e[e.Forbidden=403]="Forbidden",e[e.NotFound=404]="NotFound",e[e.MethodNotAllowed=405]="MethodNotAllowed",e[e.NotAcceptable=406]="NotAcceptable",e[e.ProxyAuthenticationRequired=407]="ProxyAuthenticationRequired",e[e.RequestTimeout=408]="RequestTimeout",e[e.Conflict=409]="Conflict",e[e.Gone=410]="Gone",e[e.LengthRequired=411]="LengthRequired",e[e.PreconditionFailed=412]="PreconditionFailed",e[e.PayloadTooLarge=413]="PayloadTooLarge",e[e.UriTooLong=414]="UriTooLong",e[e.UnsupportedMediaType=415]="UnsupportedMediaType",e[e.RangeNotSatisfiable=416]="RangeNotSatisfiable",e[e.ExpectationFailed=417]="ExpectationFailed",e[e.ImATeapot=418]="ImATeapot",e[e.MisdirectedRequest=421]="MisdirectedRequest",e[e.UnprocessableEntity=422]="UnprocessableEntity",e[e.Locked=423]="Locked",e[e.FailedDependency=424]="FailedDependency",e[e.TooEarly=425]="TooEarly",e[e.UpgradeRequired=426]="UpgradeRequired",e[e.PreconditionRequired=428]="PreconditionRequired",e[e.TooManyRequests=429]="TooManyRequests",e[e.RequestHeaderFieldsTooLarge=431]="RequestHeaderFieldsTooLarge",e[e.UnavailableForLegalReasons=451]="UnavailableForLegalReasons",e[e.InternalServerError=500]="InternalServerError",e[e.NotImplemented=501]="NotImplemented",e[e.BadGateway=502]="BadGateway",e[e.ServiceUnavailable=503]="ServiceUnavailable",e[e.GatewayTimeout=504]="GatewayTimeout",e[e.HttpVersionNotSupported=505]="HttpVersionNotSupported",e[e.VariantAlsoNegotiates=506]="VariantAlsoNegotiates",e[e.InsufficientStorage=507]="InsufficientStorage",e[e.LoopDetected=508]="LoopDetected",e[e.NotExtended=510]="NotExtended",e[e.NetworkAuthenticationRequired=511]="NetworkAuthenticationRequired",e))(x||{});class V{send(t){return new y.Observable(n=>{const r=new XMLHttpRequest;r.open(t.method,t.getUrlWithParams()),t.withCredentials&&(r.withCredentials=!0),t.headers.forEach((p,a)=>r.setRequestHeader(p,a.join(","))),t.headers.has("Accept")||r.setRequestHeader("Accept","application/json, text/plain, */*"),t.headers.has("Content-Type")||r.setRequestHeader("Content-Type","application/json;charset=UTF-8");const i=()=>{const p=r.statusText||"OK",a=new f(r.getAllResponseHeaders());return new M(a,r.status,p)},o=()=>{const{headers:p,statusText:a,status:m}=i(),{responseType:b}=t;let h=null,w=null;m!==x.NoContent&&(h=typeof r.response>"u"?r.responseText:r.response);let R=m>=B&&m<G;if(b==="json"&&typeof h=="string"){const N=h;try{h=h?JSON.parse(h):null}catch(I){R=!1,h=N,w=I}}R?n.next(new g({body:h,headers:p,status:m,statusText:a})):n.next(new _({error:w,headers:p,status:m,statusText:a}))},s=p=>{const a=new _({error:p,status:r.status||0,statusText:r.statusText||"Unknown Error",headers:i().headers});n.next(a)};r.addEventListener("load",o),r.addEventListener("error",s),r.addEventListener("abort",s),r.addEventListener("timeout",s);const l=t.getBody();return r.send(l),()=>{r.readyState!==r.DONE&&r.abort(),r.removeEventListener("load",o),r.removeEventListener("error",s),r.removeEventListener("abort",s),r.removeEventListener("timeout",s)}})}}const W=q.createIdentifier("univer.socket");class J extends d.Disposable{createSocket(t){try{const n=new WebSocket(t),r=new d.DisposableCollection;return{URL:t,close:(o,s)=>{n.close(o,s),r.dispose()},send:o=>{n.send(o)},open$:new y.Observable(o=>{const s=l=>o.next(l);n.addEventListener("open",s),r.add(d.toDisposable(()=>n.removeEventListener("open",s)))}).pipe(v.share()),close$:new y.Observable(o=>{const s=l=>o.next(l);n.addEventListener("close",s),r.add(d.toDisposable(()=>n.removeEventListener("close",s)))}).pipe(v.share()),error$:new y.Observable(o=>{const s=l=>o.next(l);n.addEventListener("error",s),r.add(d.toDisposable(()=>n.removeEventListener("error",s)))}).pipe(v.share()),message$:new y.Observable(o=>{const s=l=>o.next(l);n.addEventListener("message",s),r.add(d.toDisposable(()=>n.removeEventListener("message",s)))}).pipe(v.share())}}catch(n){return console.error(n),null}}}exports.HTTPHeaders=f;exports.HTTPRequest=u;exports.HTTPResponse=g;exports.IHTTPImplementation=L;exports.ISocketService=W;exports.WebSocketService=J;exports.XHRHTTPImplementation=V;
|