@univerjs/network 1.0.0-rc.0 → 1.0.0
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/lib/cjs/facade.js +14 -14
- package/lib/cjs/index.js +24 -16
- package/lib/es/facade.js +14 -14
- package/lib/es/index.js +24 -16
- package/lib/facade.js +14 -14
- package/lib/index.js +24 -16
- package/lib/types/facade/f-network.d.ts +12 -12
- package/lib/umd/index.js +2 -2
- package/package.json +4 -4
package/lib/cjs/facade.js
CHANGED
|
@@ -2,7 +2,7 @@ let _univerjs_core_facade = require("@univerjs/core/facade");
|
|
|
2
2
|
let _univerjs_network = require("@univerjs/network");
|
|
3
3
|
let _univerjs_core = require("@univerjs/core");
|
|
4
4
|
|
|
5
|
-
//#region \0@oxc-project+runtime@0.
|
|
5
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorateParam.js
|
|
6
6
|
function __decorateParam(paramIndex, decorator) {
|
|
7
7
|
return function(target, key) {
|
|
8
8
|
decorator(target, key, paramIndex);
|
|
@@ -10,7 +10,7 @@ function __decorateParam(paramIndex, decorator) {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
//#endregion
|
|
13
|
-
//#region \0@oxc-project+runtime@0.
|
|
13
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorate.js
|
|
14
14
|
function __decorate(decorators, target, key, desc) {
|
|
15
15
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
16
16
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -29,8 +29,8 @@ let FNetwork = class FNetwork extends _univerjs_core_facade.FBase {
|
|
|
29
29
|
/**
|
|
30
30
|
* Send a GET request to the server.
|
|
31
31
|
* @param {string} url - The requested URL.
|
|
32
|
-
* @param {IRequestParams} [params] -
|
|
33
|
-
* @returns {Promise<HTTPResponse
|
|
32
|
+
* @param {IRequestParams} [params] - Request options, including query parameters, headers, and response type.
|
|
33
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
34
34
|
*/
|
|
35
35
|
get(url, params) {
|
|
36
36
|
return this._httpService.get(url, params);
|
|
@@ -38,8 +38,8 @@ let FNetwork = class FNetwork extends _univerjs_core_facade.FBase {
|
|
|
38
38
|
/**
|
|
39
39
|
* Send a POST request to the server.
|
|
40
40
|
* @param {string} url - The requested URL.
|
|
41
|
-
* @param {IPostRequestParams} [params] -
|
|
42
|
-
* @returns {Promise<HTTPResponse
|
|
41
|
+
* @param {IPostRequestParams} [params] - Request options, including body, query parameters, headers, and response type.
|
|
42
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
43
43
|
*/
|
|
44
44
|
post(url, params) {
|
|
45
45
|
return this._httpService.post(url, params);
|
|
@@ -47,8 +47,8 @@ let FNetwork = class FNetwork extends _univerjs_core_facade.FBase {
|
|
|
47
47
|
/**
|
|
48
48
|
* Send a PUT request to the server.
|
|
49
49
|
* @param {string} url - The requested URL
|
|
50
|
-
* @param {IPostRequestParams} [params] -
|
|
51
|
-
* @returns {Promise<HTTPResponse
|
|
50
|
+
* @param {IPostRequestParams} [params] - Request options, including body, query parameters, headers, and response type
|
|
51
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
52
52
|
*/
|
|
53
53
|
put(url, params) {
|
|
54
54
|
return this._httpService.put(url, params);
|
|
@@ -56,8 +56,8 @@ let FNetwork = class FNetwork extends _univerjs_core_facade.FBase {
|
|
|
56
56
|
/**
|
|
57
57
|
* Send DELETE request to the server.
|
|
58
58
|
* @param {string} url - The requested URL
|
|
59
|
-
* @param {IRequestParams} [params] -
|
|
60
|
-
* @returns {Promise<HTTPResponse
|
|
59
|
+
* @param {IRequestParams} [params] - Request options, including query parameters, headers, and response type
|
|
60
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
61
61
|
*/
|
|
62
62
|
delete(url, params) {
|
|
63
63
|
return this._httpService.delete(url, params);
|
|
@@ -65,8 +65,8 @@ let FNetwork = class FNetwork extends _univerjs_core_facade.FBase {
|
|
|
65
65
|
/**
|
|
66
66
|
* Send PATCH request to the server.
|
|
67
67
|
* @param {string} url - The requested URL
|
|
68
|
-
* @param {IPostRequestParams} [params] -
|
|
69
|
-
* @returns {Promise<HTTPResponse
|
|
68
|
+
* @param {IPostRequestParams} [params] - Request options, including body, query parameters, headers, and response type
|
|
69
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
70
70
|
*/
|
|
71
71
|
patch(url, params) {
|
|
72
72
|
return this._httpService.patch(url, params);
|
|
@@ -76,8 +76,8 @@ let FNetwork = class FNetwork extends _univerjs_core_facade.FBase {
|
|
|
76
76
|
* Univer wraps the stream in an [`Observable`](https://rxjs.dev/guide/observable) which you can call `subscribe` on.
|
|
77
77
|
* @param {HTTPRequestMethod} method - HTTP request method
|
|
78
78
|
* @param {string} url - The requested URL
|
|
79
|
-
* @param {IPostRequestParams} [params] - params
|
|
80
|
-
* @returns {Observable<HTTPEvent
|
|
79
|
+
* @param {IPostRequestParams} [params] - params Request options, including body, query parameters, headers, and response type
|
|
80
|
+
* @returns {Observable<HTTPEvent<T>>} An observable of HTTP events with response data of type `T`.
|
|
81
81
|
*/
|
|
82
82
|
getSSE(method, url, params) {
|
|
83
83
|
return this._httpService.stream(method, url, params);
|
package/lib/cjs/index.js
CHANGED
|
@@ -5,7 +5,7 @@ let rxjs_operators = require("rxjs/operators");
|
|
|
5
5
|
|
|
6
6
|
//#region package.json
|
|
7
7
|
var name = "@univerjs/network";
|
|
8
|
-
var version = "1.0.0
|
|
8
|
+
var version = "1.0.0";
|
|
9
9
|
|
|
10
10
|
//#endregion
|
|
11
11
|
//#region src/config/config.ts
|
|
@@ -14,7 +14,7 @@ const configSymbol = Symbol(NETWORK_PLUGIN_CONFIG_KEY);
|
|
|
14
14
|
const defaultPluginConfig = {};
|
|
15
15
|
|
|
16
16
|
//#endregion
|
|
17
|
-
//#region \0@oxc-project+runtime@0.
|
|
17
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/typeof.js
|
|
18
18
|
function _typeof(o) {
|
|
19
19
|
"@babel/helpers - typeof";
|
|
20
20
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -25,7 +25,7 @@ function _typeof(o) {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
//#endregion
|
|
28
|
-
//#region \0@oxc-project+runtime@0.
|
|
28
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/toPrimitive.js
|
|
29
29
|
function toPrimitive(t, r) {
|
|
30
30
|
if ("object" != _typeof(t) || !t) return t;
|
|
31
31
|
var e = t[Symbol.toPrimitive];
|
|
@@ -38,14 +38,14 @@ function toPrimitive(t, r) {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
//#endregion
|
|
41
|
-
//#region \0@oxc-project+runtime@0.
|
|
41
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/toPropertyKey.js
|
|
42
42
|
function toPropertyKey(t) {
|
|
43
43
|
var i = toPrimitive(t, "string");
|
|
44
44
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
//#endregion
|
|
48
|
-
//#region \0@oxc-project+runtime@0.
|
|
48
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/defineProperty.js
|
|
49
49
|
function _defineProperty(e, r, t) {
|
|
50
50
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
51
51
|
value: t,
|
|
@@ -178,7 +178,7 @@ var HTTPRequest = class {
|
|
|
178
178
|
};
|
|
179
179
|
|
|
180
180
|
//#endregion
|
|
181
|
-
//#region \0@oxc-project+runtime@0.
|
|
181
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorateParam.js
|
|
182
182
|
function __decorateParam(paramIndex, decorator) {
|
|
183
183
|
return function(target, key) {
|
|
184
184
|
decorator(target, key, paramIndex);
|
|
@@ -186,7 +186,7 @@ function __decorateParam(paramIndex, decorator) {
|
|
|
186
186
|
}
|
|
187
187
|
|
|
188
188
|
//#endregion
|
|
189
|
-
//#region \0@oxc-project+runtime@0.
|
|
189
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorate.js
|
|
190
190
|
function __decorate(decorators, target, key, desc) {
|
|
191
191
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
192
192
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -240,13 +240,17 @@ let HTTPService = class HTTPService extends _univerjs_core.Disposable {
|
|
|
240
240
|
* @returns A promise that resolves to the HTTP response.
|
|
241
241
|
*/
|
|
242
242
|
async request(method, url, options) {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
243
|
+
const headers = new HTTPHeaders(options === null || options === void 0 ? void 0 : options.headers);
|
|
244
|
+
const params = new HTTPParams(options === null || options === void 0 ? void 0 : options.params);
|
|
245
|
+
const request = new HTTPRequest(method, url, {
|
|
246
|
+
headers,
|
|
247
|
+
params,
|
|
246
248
|
withCredentials: (options === null || options === void 0 ? void 0 : options.withCredentials) ?? false,
|
|
247
249
|
responseType: (options === null || options === void 0 ? void 0 : options.responseType) ?? "json",
|
|
248
250
|
body: ["GET", "DELETE"].includes(method) ? void 0 : options === null || options === void 0 ? void 0 : options.body
|
|
249
|
-
})
|
|
251
|
+
});
|
|
252
|
+
const events$ = (0, rxjs.of)(request).pipe((0, rxjs_operators.concatMap)((request) => this._runInterceptorsAndImplementation(request)));
|
|
253
|
+
return await (0, rxjs.firstValueFrom)(events$);
|
|
250
254
|
}
|
|
251
255
|
/**
|
|
252
256
|
* Send an HTTP request. It returns an observable that emits HTTP events. For example, it can be used to
|
|
@@ -257,14 +261,17 @@ let HTTPService = class HTTPService extends _univerjs_core.Disposable {
|
|
|
257
261
|
* @returns An observable of the HTTP event.
|
|
258
262
|
*/
|
|
259
263
|
stream(method, url, _params) {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
264
|
+
const headers = new HTTPHeaders(_params === null || _params === void 0 ? void 0 : _params.headers);
|
|
265
|
+
const params = new HTTPParams(_params === null || _params === void 0 ? void 0 : _params.params);
|
|
266
|
+
const request = new HTTPRequest(method, url, {
|
|
267
|
+
headers,
|
|
268
|
+
params,
|
|
263
269
|
withCredentials: (_params === null || _params === void 0 ? void 0 : _params.withCredentials) ?? false,
|
|
264
270
|
reportProgress: true,
|
|
265
271
|
responseType: (_params === null || _params === void 0 ? void 0 : _params.responseType) ?? "json",
|
|
266
272
|
body: ["GET", "DELETE"].includes(method) ? void 0 : _params === null || _params === void 0 ? void 0 : _params.body
|
|
267
|
-
})
|
|
273
|
+
});
|
|
274
|
+
return (0, rxjs.of)(request).pipe((0, rxjs_operators.concatMap)((request) => this._runInterceptorsAndImplementation(request)));
|
|
268
275
|
}
|
|
269
276
|
_runInterceptorsAndImplementation(request) {
|
|
270
277
|
if (!this._pipe) this._pipe = this._interceptors.map((handler) => handler.interceptor).reduceRight((nextHandlerFunction, interceptorFunction) => chainInterceptorFn(nextHandlerFunction, interceptorFunction), (requestFromPrevInterceptor, finalHandler) => finalHandler(requestFromPrevInterceptor));
|
|
@@ -559,7 +566,8 @@ let XHRHTTPImplementation = class XHRHTTPImplementation {
|
|
|
559
566
|
if (fetchParams.headers) Object.entries(fetchParams.headers).forEach(([key, value]) => xhr.setRequestHeader(key, value));
|
|
560
567
|
const buildResponseHeader = () => {
|
|
561
568
|
const statusText = xhr.statusText || "OK";
|
|
562
|
-
|
|
569
|
+
const headers = new HTTPHeaders(xhr.getAllResponseHeaders());
|
|
570
|
+
return new ResponseHeader(headers, xhr.status, statusText);
|
|
563
571
|
};
|
|
564
572
|
const onLoadHandler = () => {
|
|
565
573
|
const { headers, statusText, status } = buildResponseHeader();
|
package/lib/es/facade.js
CHANGED
|
@@ -2,7 +2,7 @@ import { FBase, FUniver } from "@univerjs/core/facade";
|
|
|
2
2
|
import { HTTPService, WebSocketService } from "@univerjs/network";
|
|
3
3
|
import { Inject, Injector } from "@univerjs/core";
|
|
4
4
|
|
|
5
|
-
//#region \0@oxc-project+runtime@0.
|
|
5
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorateParam.js
|
|
6
6
|
function __decorateParam(paramIndex, decorator) {
|
|
7
7
|
return function(target, key) {
|
|
8
8
|
decorator(target, key, paramIndex);
|
|
@@ -10,7 +10,7 @@ function __decorateParam(paramIndex, decorator) {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
//#endregion
|
|
13
|
-
//#region \0@oxc-project+runtime@0.
|
|
13
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorate.js
|
|
14
14
|
function __decorate(decorators, target, key, desc) {
|
|
15
15
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
16
16
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -29,8 +29,8 @@ let FNetwork = class FNetwork extends FBase {
|
|
|
29
29
|
/**
|
|
30
30
|
* Send a GET request to the server.
|
|
31
31
|
* @param {string} url - The requested URL.
|
|
32
|
-
* @param {IRequestParams} [params] -
|
|
33
|
-
* @returns {Promise<HTTPResponse
|
|
32
|
+
* @param {IRequestParams} [params] - Request options, including query parameters, headers, and response type.
|
|
33
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
34
34
|
*/
|
|
35
35
|
get(url, params) {
|
|
36
36
|
return this._httpService.get(url, params);
|
|
@@ -38,8 +38,8 @@ let FNetwork = class FNetwork extends FBase {
|
|
|
38
38
|
/**
|
|
39
39
|
* Send a POST request to the server.
|
|
40
40
|
* @param {string} url - The requested URL.
|
|
41
|
-
* @param {IPostRequestParams} [params] -
|
|
42
|
-
* @returns {Promise<HTTPResponse
|
|
41
|
+
* @param {IPostRequestParams} [params] - Request options, including body, query parameters, headers, and response type.
|
|
42
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
43
43
|
*/
|
|
44
44
|
post(url, params) {
|
|
45
45
|
return this._httpService.post(url, params);
|
|
@@ -47,8 +47,8 @@ let FNetwork = class FNetwork extends FBase {
|
|
|
47
47
|
/**
|
|
48
48
|
* Send a PUT request to the server.
|
|
49
49
|
* @param {string} url - The requested URL
|
|
50
|
-
* @param {IPostRequestParams} [params] -
|
|
51
|
-
* @returns {Promise<HTTPResponse
|
|
50
|
+
* @param {IPostRequestParams} [params] - Request options, including body, query parameters, headers, and response type
|
|
51
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
52
52
|
*/
|
|
53
53
|
put(url, params) {
|
|
54
54
|
return this._httpService.put(url, params);
|
|
@@ -56,8 +56,8 @@ let FNetwork = class FNetwork extends FBase {
|
|
|
56
56
|
/**
|
|
57
57
|
* Send DELETE request to the server.
|
|
58
58
|
* @param {string} url - The requested URL
|
|
59
|
-
* @param {IRequestParams} [params] -
|
|
60
|
-
* @returns {Promise<HTTPResponse
|
|
59
|
+
* @param {IRequestParams} [params] - Request options, including query parameters, headers, and response type
|
|
60
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
61
61
|
*/
|
|
62
62
|
delete(url, params) {
|
|
63
63
|
return this._httpService.delete(url, params);
|
|
@@ -65,8 +65,8 @@ let FNetwork = class FNetwork extends FBase {
|
|
|
65
65
|
/**
|
|
66
66
|
* Send PATCH request to the server.
|
|
67
67
|
* @param {string} url - The requested URL
|
|
68
|
-
* @param {IPostRequestParams} [params] -
|
|
69
|
-
* @returns {Promise<HTTPResponse
|
|
68
|
+
* @param {IPostRequestParams} [params] - Request options, including body, query parameters, headers, and response type
|
|
69
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
70
70
|
*/
|
|
71
71
|
patch(url, params) {
|
|
72
72
|
return this._httpService.patch(url, params);
|
|
@@ -76,8 +76,8 @@ let FNetwork = class FNetwork extends FBase {
|
|
|
76
76
|
* Univer wraps the stream in an [`Observable`](https://rxjs.dev/guide/observable) which you can call `subscribe` on.
|
|
77
77
|
* @param {HTTPRequestMethod} method - HTTP request method
|
|
78
78
|
* @param {string} url - The requested URL
|
|
79
|
-
* @param {IPostRequestParams} [params] - params
|
|
80
|
-
* @returns {Observable<HTTPEvent
|
|
79
|
+
* @param {IPostRequestParams} [params] - params Request options, including body, query parameters, headers, and response type
|
|
80
|
+
* @returns {Observable<HTTPEvent<T>>} An observable of HTTP events with response data of type `T`.
|
|
81
81
|
*/
|
|
82
82
|
getSSE(method, url, params) {
|
|
83
83
|
return this._httpService.stream(method, url, params);
|
package/lib/es/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import { concatMap, retry, share } from "rxjs/operators";
|
|
|
4
4
|
|
|
5
5
|
//#region package.json
|
|
6
6
|
var name = "@univerjs/network";
|
|
7
|
-
var version = "1.0.0
|
|
7
|
+
var version = "1.0.0";
|
|
8
8
|
|
|
9
9
|
//#endregion
|
|
10
10
|
//#region src/config/config.ts
|
|
@@ -13,7 +13,7 @@ const configSymbol = Symbol(NETWORK_PLUGIN_CONFIG_KEY);
|
|
|
13
13
|
const defaultPluginConfig = {};
|
|
14
14
|
|
|
15
15
|
//#endregion
|
|
16
|
-
//#region \0@oxc-project+runtime@0.
|
|
16
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/typeof.js
|
|
17
17
|
function _typeof(o) {
|
|
18
18
|
"@babel/helpers - typeof";
|
|
19
19
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -24,7 +24,7 @@ function _typeof(o) {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
//#endregion
|
|
27
|
-
//#region \0@oxc-project+runtime@0.
|
|
27
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/toPrimitive.js
|
|
28
28
|
function toPrimitive(t, r) {
|
|
29
29
|
if ("object" != _typeof(t) || !t) return t;
|
|
30
30
|
var e = t[Symbol.toPrimitive];
|
|
@@ -37,14 +37,14 @@ function toPrimitive(t, r) {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
//#endregion
|
|
40
|
-
//#region \0@oxc-project+runtime@0.
|
|
40
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/toPropertyKey.js
|
|
41
41
|
function toPropertyKey(t) {
|
|
42
42
|
var i = toPrimitive(t, "string");
|
|
43
43
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
//#endregion
|
|
47
|
-
//#region \0@oxc-project+runtime@0.
|
|
47
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/defineProperty.js
|
|
48
48
|
function _defineProperty(e, r, t) {
|
|
49
49
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
50
50
|
value: t,
|
|
@@ -177,7 +177,7 @@ var HTTPRequest = class {
|
|
|
177
177
|
};
|
|
178
178
|
|
|
179
179
|
//#endregion
|
|
180
|
-
//#region \0@oxc-project+runtime@0.
|
|
180
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorateParam.js
|
|
181
181
|
function __decorateParam(paramIndex, decorator) {
|
|
182
182
|
return function(target, key) {
|
|
183
183
|
decorator(target, key, paramIndex);
|
|
@@ -185,7 +185,7 @@ function __decorateParam(paramIndex, decorator) {
|
|
|
185
185
|
}
|
|
186
186
|
|
|
187
187
|
//#endregion
|
|
188
|
-
//#region \0@oxc-project+runtime@0.
|
|
188
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorate.js
|
|
189
189
|
function __decorate(decorators, target, key, desc) {
|
|
190
190
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
191
191
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -239,13 +239,17 @@ let HTTPService = class HTTPService extends Disposable {
|
|
|
239
239
|
* @returns A promise that resolves to the HTTP response.
|
|
240
240
|
*/
|
|
241
241
|
async request(method, url, options) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
242
|
+
const headers = new HTTPHeaders(options === null || options === void 0 ? void 0 : options.headers);
|
|
243
|
+
const params = new HTTPParams(options === null || options === void 0 ? void 0 : options.params);
|
|
244
|
+
const request = new HTTPRequest(method, url, {
|
|
245
|
+
headers,
|
|
246
|
+
params,
|
|
245
247
|
withCredentials: (options === null || options === void 0 ? void 0 : options.withCredentials) ?? false,
|
|
246
248
|
responseType: (options === null || options === void 0 ? void 0 : options.responseType) ?? "json",
|
|
247
249
|
body: ["GET", "DELETE"].includes(method) ? void 0 : options === null || options === void 0 ? void 0 : options.body
|
|
248
|
-
})
|
|
250
|
+
});
|
|
251
|
+
const events$ = of(request).pipe(concatMap((request) => this._runInterceptorsAndImplementation(request)));
|
|
252
|
+
return await firstValueFrom(events$);
|
|
249
253
|
}
|
|
250
254
|
/**
|
|
251
255
|
* Send an HTTP request. It returns an observable that emits HTTP events. For example, it can be used to
|
|
@@ -256,14 +260,17 @@ let HTTPService = class HTTPService extends Disposable {
|
|
|
256
260
|
* @returns An observable of the HTTP event.
|
|
257
261
|
*/
|
|
258
262
|
stream(method, url, _params) {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
263
|
+
const headers = new HTTPHeaders(_params === null || _params === void 0 ? void 0 : _params.headers);
|
|
264
|
+
const params = new HTTPParams(_params === null || _params === void 0 ? void 0 : _params.params);
|
|
265
|
+
const request = new HTTPRequest(method, url, {
|
|
266
|
+
headers,
|
|
267
|
+
params,
|
|
262
268
|
withCredentials: (_params === null || _params === void 0 ? void 0 : _params.withCredentials) ?? false,
|
|
263
269
|
reportProgress: true,
|
|
264
270
|
responseType: (_params === null || _params === void 0 ? void 0 : _params.responseType) ?? "json",
|
|
265
271
|
body: ["GET", "DELETE"].includes(method) ? void 0 : _params === null || _params === void 0 ? void 0 : _params.body
|
|
266
|
-
})
|
|
272
|
+
});
|
|
273
|
+
return of(request).pipe(concatMap((request) => this._runInterceptorsAndImplementation(request)));
|
|
267
274
|
}
|
|
268
275
|
_runInterceptorsAndImplementation(request) {
|
|
269
276
|
if (!this._pipe) this._pipe = this._interceptors.map((handler) => handler.interceptor).reduceRight((nextHandlerFunction, interceptorFunction) => chainInterceptorFn(nextHandlerFunction, interceptorFunction), (requestFromPrevInterceptor, finalHandler) => finalHandler(requestFromPrevInterceptor));
|
|
@@ -558,7 +565,8 @@ let XHRHTTPImplementation = class XHRHTTPImplementation {
|
|
|
558
565
|
if (fetchParams.headers) Object.entries(fetchParams.headers).forEach(([key, value]) => xhr.setRequestHeader(key, value));
|
|
559
566
|
const buildResponseHeader = () => {
|
|
560
567
|
const statusText = xhr.statusText || "OK";
|
|
561
|
-
|
|
568
|
+
const headers = new HTTPHeaders(xhr.getAllResponseHeaders());
|
|
569
|
+
return new ResponseHeader(headers, xhr.status, statusText);
|
|
562
570
|
};
|
|
563
571
|
const onLoadHandler = () => {
|
|
564
572
|
const { headers, statusText, status } = buildResponseHeader();
|
package/lib/facade.js
CHANGED
|
@@ -2,7 +2,7 @@ import { FBase, FUniver } from "@univerjs/core/facade";
|
|
|
2
2
|
import { HTTPService, WebSocketService } from "@univerjs/network";
|
|
3
3
|
import { Inject, Injector } from "@univerjs/core";
|
|
4
4
|
|
|
5
|
-
//#region \0@oxc-project+runtime@0.
|
|
5
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorateParam.js
|
|
6
6
|
function __decorateParam(paramIndex, decorator) {
|
|
7
7
|
return function(target, key) {
|
|
8
8
|
decorator(target, key, paramIndex);
|
|
@@ -10,7 +10,7 @@ function __decorateParam(paramIndex, decorator) {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
//#endregion
|
|
13
|
-
//#region \0@oxc-project+runtime@0.
|
|
13
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorate.js
|
|
14
14
|
function __decorate(decorators, target, key, desc) {
|
|
15
15
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
16
16
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -29,8 +29,8 @@ let FNetwork = class FNetwork extends FBase {
|
|
|
29
29
|
/**
|
|
30
30
|
* Send a GET request to the server.
|
|
31
31
|
* @param {string} url - The requested URL.
|
|
32
|
-
* @param {IRequestParams} [params] -
|
|
33
|
-
* @returns {Promise<HTTPResponse
|
|
32
|
+
* @param {IRequestParams} [params] - Request options, including query parameters, headers, and response type.
|
|
33
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
34
34
|
*/
|
|
35
35
|
get(url, params) {
|
|
36
36
|
return this._httpService.get(url, params);
|
|
@@ -38,8 +38,8 @@ let FNetwork = class FNetwork extends FBase {
|
|
|
38
38
|
/**
|
|
39
39
|
* Send a POST request to the server.
|
|
40
40
|
* @param {string} url - The requested URL.
|
|
41
|
-
* @param {IPostRequestParams} [params] -
|
|
42
|
-
* @returns {Promise<HTTPResponse
|
|
41
|
+
* @param {IPostRequestParams} [params] - Request options, including body, query parameters, headers, and response type.
|
|
42
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
43
43
|
*/
|
|
44
44
|
post(url, params) {
|
|
45
45
|
return this._httpService.post(url, params);
|
|
@@ -47,8 +47,8 @@ let FNetwork = class FNetwork extends FBase {
|
|
|
47
47
|
/**
|
|
48
48
|
* Send a PUT request to the server.
|
|
49
49
|
* @param {string} url - The requested URL
|
|
50
|
-
* @param {IPostRequestParams} [params] -
|
|
51
|
-
* @returns {Promise<HTTPResponse
|
|
50
|
+
* @param {IPostRequestParams} [params] - Request options, including body, query parameters, headers, and response type
|
|
51
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
52
52
|
*/
|
|
53
53
|
put(url, params) {
|
|
54
54
|
return this._httpService.put(url, params);
|
|
@@ -56,8 +56,8 @@ let FNetwork = class FNetwork extends FBase {
|
|
|
56
56
|
/**
|
|
57
57
|
* Send DELETE request to the server.
|
|
58
58
|
* @param {string} url - The requested URL
|
|
59
|
-
* @param {IRequestParams} [params] -
|
|
60
|
-
* @returns {Promise<HTTPResponse
|
|
59
|
+
* @param {IRequestParams} [params] - Request options, including query parameters, headers, and response type
|
|
60
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
61
61
|
*/
|
|
62
62
|
delete(url, params) {
|
|
63
63
|
return this._httpService.delete(url, params);
|
|
@@ -65,8 +65,8 @@ let FNetwork = class FNetwork extends FBase {
|
|
|
65
65
|
/**
|
|
66
66
|
* Send PATCH request to the server.
|
|
67
67
|
* @param {string} url - The requested URL
|
|
68
|
-
* @param {IPostRequestParams} [params] -
|
|
69
|
-
* @returns {Promise<HTTPResponse
|
|
68
|
+
* @param {IPostRequestParams} [params] - Request options, including body, query parameters, headers, and response type
|
|
69
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
70
70
|
*/
|
|
71
71
|
patch(url, params) {
|
|
72
72
|
return this._httpService.patch(url, params);
|
|
@@ -76,8 +76,8 @@ let FNetwork = class FNetwork extends FBase {
|
|
|
76
76
|
* Univer wraps the stream in an [`Observable`](https://rxjs.dev/guide/observable) which you can call `subscribe` on.
|
|
77
77
|
* @param {HTTPRequestMethod} method - HTTP request method
|
|
78
78
|
* @param {string} url - The requested URL
|
|
79
|
-
* @param {IPostRequestParams} [params] - params
|
|
80
|
-
* @returns {Observable<HTTPEvent
|
|
79
|
+
* @param {IPostRequestParams} [params] - params Request options, including body, query parameters, headers, and response type
|
|
80
|
+
* @returns {Observable<HTTPEvent<T>>} An observable of HTTP events with response data of type `T`.
|
|
81
81
|
*/
|
|
82
82
|
getSSE(method, url, params) {
|
|
83
83
|
return this._httpService.stream(method, url, params);
|
package/lib/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import { concatMap, retry, share } from "rxjs/operators";
|
|
|
4
4
|
|
|
5
5
|
//#region package.json
|
|
6
6
|
var name = "@univerjs/network";
|
|
7
|
-
var version = "1.0.0
|
|
7
|
+
var version = "1.0.0";
|
|
8
8
|
|
|
9
9
|
//#endregion
|
|
10
10
|
//#region src/config/config.ts
|
|
@@ -13,7 +13,7 @@ const configSymbol = Symbol(NETWORK_PLUGIN_CONFIG_KEY);
|
|
|
13
13
|
const defaultPluginConfig = {};
|
|
14
14
|
|
|
15
15
|
//#endregion
|
|
16
|
-
//#region \0@oxc-project+runtime@0.
|
|
16
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/typeof.js
|
|
17
17
|
function _typeof(o) {
|
|
18
18
|
"@babel/helpers - typeof";
|
|
19
19
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -24,7 +24,7 @@ function _typeof(o) {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
//#endregion
|
|
27
|
-
//#region \0@oxc-project+runtime@0.
|
|
27
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/toPrimitive.js
|
|
28
28
|
function toPrimitive(t, r) {
|
|
29
29
|
if ("object" != _typeof(t) || !t) return t;
|
|
30
30
|
var e = t[Symbol.toPrimitive];
|
|
@@ -37,14 +37,14 @@ function toPrimitive(t, r) {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
//#endregion
|
|
40
|
-
//#region \0@oxc-project+runtime@0.
|
|
40
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/toPropertyKey.js
|
|
41
41
|
function toPropertyKey(t) {
|
|
42
42
|
var i = toPrimitive(t, "string");
|
|
43
43
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
//#endregion
|
|
47
|
-
//#region \0@oxc-project+runtime@0.
|
|
47
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/defineProperty.js
|
|
48
48
|
function _defineProperty(e, r, t) {
|
|
49
49
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
50
50
|
value: t,
|
|
@@ -177,7 +177,7 @@ var HTTPRequest = class {
|
|
|
177
177
|
};
|
|
178
178
|
|
|
179
179
|
//#endregion
|
|
180
|
-
//#region \0@oxc-project+runtime@0.
|
|
180
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorateParam.js
|
|
181
181
|
function __decorateParam(paramIndex, decorator) {
|
|
182
182
|
return function(target, key) {
|
|
183
183
|
decorator(target, key, paramIndex);
|
|
@@ -185,7 +185,7 @@ function __decorateParam(paramIndex, decorator) {
|
|
|
185
185
|
}
|
|
186
186
|
|
|
187
187
|
//#endregion
|
|
188
|
-
//#region \0@oxc-project+runtime@0.
|
|
188
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/decorate.js
|
|
189
189
|
function __decorate(decorators, target, key, desc) {
|
|
190
190
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
191
191
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -239,13 +239,17 @@ let HTTPService = class HTTPService extends Disposable {
|
|
|
239
239
|
* @returns A promise that resolves to the HTTP response.
|
|
240
240
|
*/
|
|
241
241
|
async request(method, url, options) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
242
|
+
const headers = new HTTPHeaders(options === null || options === void 0 ? void 0 : options.headers);
|
|
243
|
+
const params = new HTTPParams(options === null || options === void 0 ? void 0 : options.params);
|
|
244
|
+
const request = new HTTPRequest(method, url, {
|
|
245
|
+
headers,
|
|
246
|
+
params,
|
|
245
247
|
withCredentials: (options === null || options === void 0 ? void 0 : options.withCredentials) ?? false,
|
|
246
248
|
responseType: (options === null || options === void 0 ? void 0 : options.responseType) ?? "json",
|
|
247
249
|
body: ["GET", "DELETE"].includes(method) ? void 0 : options === null || options === void 0 ? void 0 : options.body
|
|
248
|
-
})
|
|
250
|
+
});
|
|
251
|
+
const events$ = of(request).pipe(concatMap((request) => this._runInterceptorsAndImplementation(request)));
|
|
252
|
+
return await firstValueFrom(events$);
|
|
249
253
|
}
|
|
250
254
|
/**
|
|
251
255
|
* Send an HTTP request. It returns an observable that emits HTTP events. For example, it can be used to
|
|
@@ -256,14 +260,17 @@ let HTTPService = class HTTPService extends Disposable {
|
|
|
256
260
|
* @returns An observable of the HTTP event.
|
|
257
261
|
*/
|
|
258
262
|
stream(method, url, _params) {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
263
|
+
const headers = new HTTPHeaders(_params === null || _params === void 0 ? void 0 : _params.headers);
|
|
264
|
+
const params = new HTTPParams(_params === null || _params === void 0 ? void 0 : _params.params);
|
|
265
|
+
const request = new HTTPRequest(method, url, {
|
|
266
|
+
headers,
|
|
267
|
+
params,
|
|
262
268
|
withCredentials: (_params === null || _params === void 0 ? void 0 : _params.withCredentials) ?? false,
|
|
263
269
|
reportProgress: true,
|
|
264
270
|
responseType: (_params === null || _params === void 0 ? void 0 : _params.responseType) ?? "json",
|
|
265
271
|
body: ["GET", "DELETE"].includes(method) ? void 0 : _params === null || _params === void 0 ? void 0 : _params.body
|
|
266
|
-
})
|
|
272
|
+
});
|
|
273
|
+
return of(request).pipe(concatMap((request) => this._runInterceptorsAndImplementation(request)));
|
|
267
274
|
}
|
|
268
275
|
_runInterceptorsAndImplementation(request) {
|
|
269
276
|
if (!this._pipe) this._pipe = this._interceptors.map((handler) => handler.interceptor).reduceRight((nextHandlerFunction, interceptorFunction) => chainInterceptorFn(nextHandlerFunction, interceptorFunction), (requestFromPrevInterceptor, finalHandler) => finalHandler(requestFromPrevInterceptor));
|
|
@@ -558,7 +565,8 @@ let XHRHTTPImplementation = class XHRHTTPImplementation {
|
|
|
558
565
|
if (fetchParams.headers) Object.entries(fetchParams.headers).forEach(([key, value]) => xhr.setRequestHeader(key, value));
|
|
559
566
|
const buildResponseHeader = () => {
|
|
560
567
|
const statusText = xhr.statusText || "OK";
|
|
561
|
-
|
|
568
|
+
const headers = new HTTPHeaders(xhr.getAllResponseHeaders());
|
|
569
|
+
return new ResponseHeader(headers, xhr.status, statusText);
|
|
562
570
|
};
|
|
563
571
|
const onLoadHandler = () => {
|
|
564
572
|
const { headers, statusText, status } = buildResponseHeader();
|
|
@@ -32,36 +32,36 @@ export declare class FNetwork extends FBase {
|
|
|
32
32
|
/**
|
|
33
33
|
* Send a GET request to the server.
|
|
34
34
|
* @param {string} url - The requested URL.
|
|
35
|
-
* @param {IRequestParams} [params] -
|
|
36
|
-
* @returns {Promise<HTTPResponse
|
|
35
|
+
* @param {IRequestParams} [params] - Request options, including query parameters, headers, and response type.
|
|
36
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
37
37
|
*/
|
|
38
38
|
get<T>(url: string, params?: IRequestParams): Promise<HTTPResponse<T>>;
|
|
39
39
|
/**
|
|
40
40
|
* Send a POST request to the server.
|
|
41
41
|
* @param {string} url - The requested URL.
|
|
42
|
-
* @param {IPostRequestParams} [params] -
|
|
43
|
-
* @returns {Promise<HTTPResponse
|
|
42
|
+
* @param {IPostRequestParams} [params] - Request options, including body, query parameters, headers, and response type.
|
|
43
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
44
44
|
*/
|
|
45
45
|
post<T>(url: string, params?: IPostRequestParams): Promise<HTTPResponse<T>>;
|
|
46
46
|
/**
|
|
47
47
|
* Send a PUT request to the server.
|
|
48
48
|
* @param {string} url - The requested URL
|
|
49
|
-
* @param {IPostRequestParams} [params] -
|
|
50
|
-
* @returns {Promise<HTTPResponse
|
|
49
|
+
* @param {IPostRequestParams} [params] - Request options, including body, query parameters, headers, and response type
|
|
50
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
51
51
|
*/
|
|
52
52
|
put<T>(url: string, params?: IPostRequestParams): Promise<HTTPResponse<T>>;
|
|
53
53
|
/**
|
|
54
54
|
* Send DELETE request to the server.
|
|
55
55
|
* @param {string} url - The requested URL
|
|
56
|
-
* @param {IRequestParams} [params] -
|
|
57
|
-
* @returns {Promise<HTTPResponse
|
|
56
|
+
* @param {IRequestParams} [params] - Request options, including query parameters, headers, and response type
|
|
57
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
58
58
|
*/
|
|
59
59
|
delete<T>(url: string, params?: IRequestParams): Promise<HTTPResponse<T>>;
|
|
60
60
|
/**
|
|
61
61
|
* Send PATCH request to the server.
|
|
62
62
|
* @param {string} url - The requested URL
|
|
63
|
-
* @param {IPostRequestParams} [params] -
|
|
64
|
-
* @returns {Promise<HTTPResponse
|
|
63
|
+
* @param {IPostRequestParams} [params] - Request options, including body, query parameters, headers, and response type
|
|
64
|
+
* @returns {Promise<HTTPResponse<T>>} A promise resolving to the HTTP response with a body of type `T`.
|
|
65
65
|
*/
|
|
66
66
|
patch<T>(url: string, params?: IPostRequestParams): Promise<HTTPResponse<T>>;
|
|
67
67
|
/**
|
|
@@ -69,8 +69,8 @@ export declare class FNetwork extends FBase {
|
|
|
69
69
|
* Univer wraps the stream in an [`Observable`](https://rxjs.dev/guide/observable) which you can call `subscribe` on.
|
|
70
70
|
* @param {HTTPRequestMethod} method - HTTP request method
|
|
71
71
|
* @param {string} url - The requested URL
|
|
72
|
-
* @param {IPostRequestParams} [params] - params
|
|
73
|
-
* @returns {Observable<HTTPEvent
|
|
72
|
+
* @param {IPostRequestParams} [params] - params Request options, including body, query parameters, headers, and response type
|
|
73
|
+
* @returns {Observable<HTTPEvent<T>>} An observable of HTTP events with response data of type `T`.
|
|
74
74
|
*/
|
|
75
75
|
getSSE<T>(method: HTTPRequestMethod, url: string, params?: IPostRequestParams): Observable<HTTPEvent<T>>;
|
|
76
76
|
}
|
package/lib/umd/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("rxjs"),require("rxjs/operators")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`rxjs`,`rxjs/operators`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverNetwork={},e.UniverCore,e.rxjs,e.rxjs.operators))})(this,function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var i=`@univerjs/network`,a=`1.0.0
|
|
2
|
-
`).forEach(e=>{let[t,n]=e.split(`:`);t&&n&&this._setHeader(t,n)})}_handleHeadersConstructorProps(e){Object.entries(e).forEach(([e,t])=>this._setHeader(e,t))}_handleHeaders(e){e.forEach((e,t)=>this._setHeader(t,e))}};let m=(0,t.createIdentifier)(`network.http-implementation`);var h=class{constructor(e){this.params=e}toString(){return this.params?Object.keys(this.params).map(e=>{let t=this.params[e];return Array.isArray(t)?t.map(t=>`${e}=${t}`).join(`&`):`${e}=${t}`}).join(`&`):``}};let g=0;var _=class{get headers(){return this.requestParams.headers}get withCredentials(){return this.requestParams.withCredentials}get responseType(){return this.requestParams.responseType}constructor(e,t,n){this.method=e,this.url=t,this.requestParams=n,u(this,`uid`,g++)}getUrlWithParams(){var e;let t=(e=this.requestParams)==null||(e=e.params)==null?void 0:e.toString();return t?`${this.url}${this.url.includes(`?`)?`&`:`?`}${t}`:this.url}getBody(){var e;let t=this.headers.get(`Content-Type`)??`application/json`,n=(e=this.requestParams)==null?void 0:e.body;return n instanceof FormData?n:f(t)&&n&&typeof n==`object`?JSON.stringify(n):n?`${n}`:null}getHeadersInit(){var e;return this.headers.toHeadersInit((e=this.requestParams)==null?void 0:e.body)}};function v(e,t){return function(n,r){t(n,r,e)}}function y(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let b=class extends t.Disposable{constructor(e){super(),this._http=e,u(this,`_interceptors`,[]),u(this,`_pipe`,void 0)}registerHTTPInterceptor(e){if(this._interceptors.indexOf(e)!==-1)throw Error(`[HTTPService]: The interceptor has already been registered!`);return this._interceptors.push(e),this._interceptors=this._interceptors.sort((e,t)=>(e.priority??0)-(t.priority??0)),this._pipe=null,(0,t.toDisposable)(()=>(0,t.remove)(this._interceptors,e))}get(e,t){return this.request(`GET`,e,t)}post(e,t){return this.request(`POST`,e,t)}put(e,t){return this.request(`PUT`,e,t)}delete(e,t){return this.request(`DELETE`,e,t)}patch(e,t){return this.request(`PATCH`,e,t)}async request(e,t,i){return await(0,n.firstValueFrom)((0,n.of)(new _(e,t,{headers:new p(i==null?void 0:i.headers),params:new h(i==null?void 0:i.params),withCredentials:(i==null?void 0:i.withCredentials)??!1,responseType:(i==null?void 0:i.responseType)??`json`,body:[`GET`,`DELETE`].includes(e)||i==null?void 0:i.body})).pipe((0,r.concatMap)(e=>this._runInterceptorsAndImplementation(e))))}stream(e,t,i){return(0,n.of)(new _(e,t,{headers:new p(i==null?void 0:i.headers),params:new h(i==null?void 0:i.params),withCredentials:(i==null?void 0:i.withCredentials)??!1,reportProgress:!0,responseType:(i==null?void 0:i.responseType)??`json`,body:[`GET`,`DELETE`].includes(e)||i==null?void 0:i.body})).pipe((0,r.concatMap)(e=>this._runInterceptorsAndImplementation(e)))}_runInterceptorsAndImplementation(e){return this._pipe||=this._interceptors.map(e=>e.interceptor).reduceRight((e,t)=>x(e,t),(e,t)=>t(e)),this._pipe(e,e=>this._http.send(e))}};b=y([v(0,m)],b);function x(e,t){return(n,r)=>t(n,t=>e(t,r))}let S=function(e){return 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}({}),C=function(e){return e[e.DownloadProgress=0]=`DownloadProgress`,e[e.Response=1]=`Response`,e}({});var w=class{constructor({body:e,headers:t,status:n,statusText:r}){u(this,`type`,1),u(this,`body`,void 0),u(this,`headers`,void 0),u(this,`status`,void 0),u(this,`statusText`,void 0),this.body=e,this.headers=t,this.status=n,this.statusText=r}},T=class{constructor(e,t,n){this.total=e,this.loaded=t,this.partialText=n,u(this,`type`,0)}},E=class{constructor(e,t,n){this.headers=e,this.status=t,this.statusText=n}},D=class{constructor({request:e,headers:t,status:n,statusText:r,error:i}){u(this,`request`,void 0),u(this,`headers`,void 0),u(this,`status`,void 0),u(this,`statusText`,void 0),u(this,`error`,void 0),this.request=e,this.headers=t,this.status=n,this.statusText=r,this.error=i}};function O(e){return{method:e.method,headers:e.getHeadersInit(),body:e.getBody(),credentials:e.withCredentials?`include`:void 0}}let k=class{constructor(e){this._logService=e}send(e){return new n.Observable(t=>{let n=new AbortController;return this._send(e,t,n).catch(n=>{t.error(new D({error:n,request:e}))}),()=>n.abort()})}async _send(e,t,n){let r;try{let t=O(e),i=e.getUrlWithParams(),a=fetch(i,{signal:n.signal,...t});this._logService.debug(`[FetchHTTPImplementation]: sending request to url ${i} with params ${t}`),r=await a}catch(n){let r=new D({request:e,error:n,status:n.status??0,statusText:n.statusText??`Unknown Error`,headers:n.headers});this._logService.error(`[FetchHTTPImplementation]: network error`,r),t.error(r);return}let i=new p(r.headers),a=r.status,o=r.statusText,s=null;if(r.body&&(s=await this._readBody(e,r,t)),a>=200&&a<300)t.next(new w({body:s,headers:i,status:a,statusText:o}));else{let n=new D({request:e,error:s,status:a,statusText:o,headers:i});this._logService.error(`[FetchHTTPImplementation]: network error`,n),t.error(n)}t.complete()}async _readBody(e,t,n){var r;let i=[],a=t.body.getReader(),o=t.headers.get(`content-length`),s=0,c=(r=e.requestParams)==null?void 0:r.reportProgress,l=e.responseType,u,d;for(;;){let{done:e,value:t}=await a.read();if(e)break;i.push(t),s+=t.length,c&&l===`text`&&(u=(u??``)+(d??=new TextDecoder).decode(t,{stream:!0}),n.next(new T(o?Number.parseInt(o,10):void 0,s,u)))}let f=A(i,s);try{return M(e,f,t.headers.get(`content-type`)??``)}catch(r){let i=new D({request:e,error:r,status:t.status,statusText:t.statusText,headers:new p(t.headers)});return this._logService.error(`[FetchHTTPImplementation]: network error`,i),n.error(i),null}}};k=y([v(0,t.ILogService)],k);function A(e,t){let n=new Uint8Array(t),r=0;for(let t of e)n.set(t,r),r+=t.length;return n}let j=/^\)\]\}',?\n/;function M(e,t,n){switch(e.responseType){case`json`:let r=new TextDecoder().decode(t).replace(j,``);return r===``?null:JSON.parse(r);case`text`:return new TextDecoder().decode(t);case`blob`:return new Blob([t.buffer],{type:n});case`arraybuffer`:return t.buffer;default:throw Error(`[FetchHTTPImplementation]: unknown response type: ${e.responseType}.`)}}let N=class{constructor(e){this._logService=e}send(e){return new n.Observable(t=>{let n=new XMLHttpRequest,r=e.getUrlWithParams(),i=O(e),{responseType:a}=e;n.open(e.method,r),e.withCredentials&&(n.withCredentials=!0),i.headers&&Object.entries(i.headers).forEach(([e,t])=>n.setRequestHeader(e,t));let o=()=>{let e=n.statusText||`OK`;return new E(new p(n.getAllResponseHeaders()),n.status,e)},s=()=>{let{headers:r,statusText:i,status:s}=o(),c=null,l=null;s!==204&&(c=n.response===void 0?n.responseText:n.response);let u=s>=200&&s<300;if(a===`json`&&typeof c==`string`){let e=c;try{c=c?JSON.parse(c):null}catch(t){u=!1,c=e,l=t}}if(a===`blob`&&!(c instanceof Blob)&&(u=!1,l=Error(`Response is not a Blob object`)),u)t.next(new w({body:c,headers:r,status:s,statusText:i}));else{let n=new D({request:e,error:l,headers:r,status:s,statusText:i});this._logService.error(`[XHRHTTPImplementation]: network error`,n),t.error(n)}},c=r=>{let i=new D({request:e,error:r,status:n.status||0,statusText:n.statusText||`Unknown Error`,headers:o().headers});this._logService.error(`[XHRHTTPImplementation]: network error`,i),t.error(i)};n.responseType=a||``,n.addEventListener(`load`,s),n.addEventListener(`error`,c),n.addEventListener(`abort`,c),n.addEventListener(`timeout`,c);let l=e.getBody();return n.send(l),this._logService.debug(`[XHRHTTPImplementation]`,`sending request to url ${r} with params ${i}`),()=>{n.readyState!==n.DONE&&n.abort(),n.removeEventListener(`load`,s),n.removeEventListener(`error`,c),n.removeEventListener(`abort`,c),n.removeEventListener(`timeout`,c)}})}};N=y([v(0,t.ILogService)],N);let P=class extends t.Plugin{constructor(e=o,n,r,i){super(),this._config=e,this._logger=n,this._injector=r,this._configService=i;let{...a}=(0,t.merge)({},o,this._config);this._configService.setConfig(`network.config`,a)}onStarting(){var e,n,r;if(this._injector.get(b,t.Quantity.OPTIONAL,t.LookUp.SKIP_SELF)&&!((e=this._config)!=null&&e.forceUseNewInstance)){this._logger.warn(`[UniverNetworkPlugin]`,`HTTPService is already registered in an ancestor interceptor. Skipping registration. If you want to force a new instance, set "forceUseNewInstance" to true in the plugin configuration.`);return}let i=(n=this._config)!=null&&n.useFetchImpl?k:typeof window<`u`?N:k;(0,t.registerDependencies)(this._injector,(0,t.mergeOverrideWithDependencies)([[b],[m,{useClass:i}]],(r=this._config)==null?void 0:r.override))}};u(P,`pluginName`,`UNIVER_NETWORK_PLUGIN`),u(P,`packageName`,i),u(P,`version`,a),P=y([v(1,t.ILogService),v(2,(0,t.Inject)(t.Injector)),v(3,t.IConfigService)],P);let F=e=>{let{errorStatusCodes:t,onAuthError:r}=e;return(e,i)=>i(e).pipe((0,n.catchError)(e=>(e instanceof D&&t.some(t=>t===e.status)&&r(),(0,n.throwError)(()=>e))))},I=(e=300)=>{let n=t.noop;return t=>new Promise(t=>{n();let r=setTimeout(()=>{t(!0)},e);n=()=>{clearTimeout(r),t(!1)}})},L=()=>(e,t)=>t.map(t=>({config:t,result:e})),R=(e,r={})=>{let{isMatch:i,getParamsFromRequest:a,mergeParamsToRequest:o}=e,{fetchCheck:s=I(300),distributeResult:c=L()}=r,l=[],u=e=>e.map(e=>e.config);return(e,r)=>i(e)?new n.Observable(n=>{let i={next:e=>n.next(e),error:e=>n.error(e),config:a(e),active:!0};l.push(i);let d=u(l);return s(e).then(t=>{if(t){let t=[];d.forEach(e=>{let n=l.findIndex(t=>t.config===e);if(n>=0){let[e]=l.splice(n,1);t.push(e)}});let i=u(t);if(!i.length)return;let a={hooks:t};t.forEach(e=>e.batch=a),a.subscription=r(o(i,e)).subscribe({next:e=>{if(e.type===1){let n=e.body,r=c(n,i);t.forEach(t=>{let n=r.find(e=>e.config===t.config);if(n){let r=new w({body:n.result,headers:e.headers,status:e.status,statusText:e.statusText});t.next(r)}else t.error(`batch error`)})}},complete:()=>n.complete(),error:e=>n.error(e)})}}),()=>{var e;if(i.active=!1,(0,t.remove)(l,i),(e=i.batch)!=null&&e.hooks.every(e=>!e.active)){var n;(n=i.batch.subscription)==null||n.unsubscribe()}}}):r(e)},z=e=>{let t=(e==null?void 0:e.maxRetryAttempts)??3,n=(e==null?void 0:e.delayInterval)??1e3;return(e,i)=>i(e).pipe((0,r.retry)({delay:n,count:t}))},B=e=>{let r=[],i=new Set,a=()=>{for(;i.size<((e==null?void 0:e.maxParallel)??1)&&r.length>0;){let e=r.shift();i.add(e),e()}};return(e,o)=>new n.Observable(n=>{let s,c=()=>{s=o(e).subscribe({next:e=>n.next(e),error:e=>n.error(e),complete:()=>n.complete()})};return r.push(c),a(),()=>{s==null||s.unsubscribe(),i.delete(c),(0,t.remove)(r,c),a()}})},V=(0,t.createIdentifier)(`univer.network.socket.service`);var H=class extends t.Disposable{createSocket(e){try{let i=new WebSocket(e),a=new t.DisposableCollection;return{URL:e,close:(e,t)=>{i.close(e,t),a.dispose()},send:e=>{i.send(e)},open$:new n.Observable(e=>{let n=t=>e.next(t);i.addEventListener(`open`,n),a.add((0,t.toDisposable)(()=>i.removeEventListener(`open`,n)))}).pipe((0,r.share)()),close$:new n.Observable(e=>{let n=t=>e.next(t);i.addEventListener(`close`,n),a.add((0,t.toDisposable)(()=>i.removeEventListener(`close`,n)))}).pipe((0,r.share)()),error$:new n.Observable(e=>{let n=t=>e.next(t);i.addEventListener(`error`,n),a.add((0,t.toDisposable)(()=>i.removeEventListener(`error`,n)))}).pipe((0,r.share)()),message$:new n.Observable(e=>{let n=t=>e.next(t);i.addEventListener(`message`,n),a.add((0,t.toDisposable)(()=>i.removeEventListener(`message`,n)))}).pipe((0,r.share)())}}catch(e){return console.error(e),null}}};e.AuthInterceptorFactory=F,Object.defineProperty(e,"FetchHTTPImplementation",{enumerable:!0,get:function(){return k}}),e.HTTPEventType=C,e.HTTPHeaders=p,e.HTTPProgress=T,e.HTTPRequest=_,e.HTTPResponse=w,e.HTTPResponseError=D,Object.defineProperty(e,"HTTPService",{enumerable:!0,get:function(){return b}}),e.HTTPStatusCode=S,e.IHTTPImplementation=m,e.ISocketService=V,e.MergeInterceptorFactory=R,e.ResponseHeader=E,e.RetryInterceptorFactory=z,e.ThresholdInterceptorFactory=B,Object.defineProperty(e,"UniverNetworkPlugin",{enumerable:!0,get:function(){return P}}),e.WebSocketService=H,Object.defineProperty(e,"XHRHTTPImplementation",{enumerable:!0,get:function(){return N}})});
|
|
1
|
+
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("rxjs"),require("rxjs/operators")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`rxjs`,`rxjs/operators`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverNetwork={},e.UniverCore,e.rxjs,e.rxjs.operators))})(this,function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var i=`@univerjs/network`,a=`1.0.0`;let o={};function s(e){"@babel/helpers - typeof";return s=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},s(e)}function c(e,t){if(s(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(s(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function l(e){var t=c(e,`string`);return s(t)==`symbol`?t:t+``}function u(e,t,n){return(t=l(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}let d=`application/json`;function f(e){return Array.isArray(e)?e.some(e=>e.includes(d)):e.includes(d)}var p=class{constructor(e){u(this,`_headers`,new Map),typeof e==`string`?this._handleHeadersString(e):e instanceof Headers?this._handleHeaders(e):e&&this._handleHeadersConstructorProps(e)}forEach(e){this._headers.forEach((t,n)=>e(n,t))}has(e){return!!this._headers.has(e.toLowerCase())}get(e){let t=e.toLowerCase();return this._headers.has(t)?this._headers.get(t):null}set(e,t){this._setHeader(e,t)}toHeadersInit(e){let t={};return this._headers.forEach((e,n)=>{t[n]=e.join(`,`)}),t.accept??=`application/json, text/plain, */*`,e instanceof FormData||(t[`content-type`]??=`application/json;charset=UTF-8`),t}_setHeader(e,t){let n=e.toLowerCase();this._headers.has(n)?this._headers.get(n).push(t.toString()):this._headers.set(n,[t.toString()])}_handleHeadersString(e){e.split(`
|
|
2
|
+
`).forEach(e=>{let[t,n]=e.split(`:`);t&&n&&this._setHeader(t,n)})}_handleHeadersConstructorProps(e){Object.entries(e).forEach(([e,t])=>this._setHeader(e,t))}_handleHeaders(e){e.forEach((e,t)=>this._setHeader(t,e))}};let m=(0,t.createIdentifier)(`network.http-implementation`);var h=class{constructor(e){this.params=e}toString(){return this.params?Object.keys(this.params).map(e=>{let t=this.params[e];return Array.isArray(t)?t.map(t=>`${e}=${t}`).join(`&`):`${e}=${t}`}).join(`&`):``}};let g=0;var _=class{get headers(){return this.requestParams.headers}get withCredentials(){return this.requestParams.withCredentials}get responseType(){return this.requestParams.responseType}constructor(e,t,n){this.method=e,this.url=t,this.requestParams=n,u(this,`uid`,g++)}getUrlWithParams(){var e;let t=(e=this.requestParams)==null||(e=e.params)==null?void 0:e.toString();return t?`${this.url}${this.url.includes(`?`)?`&`:`?`}${t}`:this.url}getBody(){var e;let t=this.headers.get(`Content-Type`)??`application/json`,n=(e=this.requestParams)==null?void 0:e.body;return n instanceof FormData?n:f(t)&&n&&typeof n==`object`?JSON.stringify(n):n?`${n}`:null}getHeadersInit(){var e;return this.headers.toHeadersInit((e=this.requestParams)==null?void 0:e.body)}};function v(e,t){return function(n,r){t(n,r,e)}}function y(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let b=class extends t.Disposable{constructor(e){super(),this._http=e,u(this,`_interceptors`,[]),u(this,`_pipe`,void 0)}registerHTTPInterceptor(e){if(this._interceptors.indexOf(e)!==-1)throw Error(`[HTTPService]: The interceptor has already been registered!`);return this._interceptors.push(e),this._interceptors=this._interceptors.sort((e,t)=>(e.priority??0)-(t.priority??0)),this._pipe=null,(0,t.toDisposable)(()=>(0,t.remove)(this._interceptors,e))}get(e,t){return this.request(`GET`,e,t)}post(e,t){return this.request(`POST`,e,t)}put(e,t){return this.request(`PUT`,e,t)}delete(e,t){return this.request(`DELETE`,e,t)}patch(e,t){return this.request(`PATCH`,e,t)}async request(e,t,i){let a=new _(e,t,{headers:new p(i==null?void 0:i.headers),params:new h(i==null?void 0:i.params),withCredentials:(i==null?void 0:i.withCredentials)??!1,responseType:(i==null?void 0:i.responseType)??`json`,body:[`GET`,`DELETE`].includes(e)||i==null?void 0:i.body}),o=(0,n.of)(a).pipe((0,r.concatMap)(e=>this._runInterceptorsAndImplementation(e)));return await(0,n.firstValueFrom)(o)}stream(e,t,i){let a=new _(e,t,{headers:new p(i==null?void 0:i.headers),params:new h(i==null?void 0:i.params),withCredentials:(i==null?void 0:i.withCredentials)??!1,reportProgress:!0,responseType:(i==null?void 0:i.responseType)??`json`,body:[`GET`,`DELETE`].includes(e)||i==null?void 0:i.body});return(0,n.of)(a).pipe((0,r.concatMap)(e=>this._runInterceptorsAndImplementation(e)))}_runInterceptorsAndImplementation(e){return this._pipe||=this._interceptors.map(e=>e.interceptor).reduceRight((e,t)=>x(e,t),(e,t)=>t(e)),this._pipe(e,e=>this._http.send(e))}};b=y([v(0,m)],b);function x(e,t){return(n,r)=>t(n,t=>e(t,r))}let S=function(e){return 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}({}),C=function(e){return e[e.DownloadProgress=0]=`DownloadProgress`,e[e.Response=1]=`Response`,e}({});var w=class{constructor({body:e,headers:t,status:n,statusText:r}){u(this,`type`,1),u(this,`body`,void 0),u(this,`headers`,void 0),u(this,`status`,void 0),u(this,`statusText`,void 0),this.body=e,this.headers=t,this.status=n,this.statusText=r}},T=class{constructor(e,t,n){this.total=e,this.loaded=t,this.partialText=n,u(this,`type`,0)}},E=class{constructor(e,t,n){this.headers=e,this.status=t,this.statusText=n}},D=class{constructor({request:e,headers:t,status:n,statusText:r,error:i}){u(this,`request`,void 0),u(this,`headers`,void 0),u(this,`status`,void 0),u(this,`statusText`,void 0),u(this,`error`,void 0),this.request=e,this.headers=t,this.status=n,this.statusText=r,this.error=i}};function O(e){return{method:e.method,headers:e.getHeadersInit(),body:e.getBody(),credentials:e.withCredentials?`include`:void 0}}let k=class{constructor(e){this._logService=e}send(e){return new n.Observable(t=>{let n=new AbortController;return this._send(e,t,n).catch(n=>{t.error(new D({error:n,request:e}))}),()=>n.abort()})}async _send(e,t,n){let r;try{let t=O(e),i=e.getUrlWithParams(),a=fetch(i,{signal:n.signal,...t});this._logService.debug(`[FetchHTTPImplementation]: sending request to url ${i} with params ${t}`),r=await a}catch(n){let r=new D({request:e,error:n,status:n.status??0,statusText:n.statusText??`Unknown Error`,headers:n.headers});this._logService.error(`[FetchHTTPImplementation]: network error`,r),t.error(r);return}let i=new p(r.headers),a=r.status,o=r.statusText,s=null;if(r.body&&(s=await this._readBody(e,r,t)),a>=200&&a<300)t.next(new w({body:s,headers:i,status:a,statusText:o}));else{let n=new D({request:e,error:s,status:a,statusText:o,headers:i});this._logService.error(`[FetchHTTPImplementation]: network error`,n),t.error(n)}t.complete()}async _readBody(e,t,n){var r;let i=[],a=t.body.getReader(),o=t.headers.get(`content-length`),s=0,c=(r=e.requestParams)==null?void 0:r.reportProgress,l=e.responseType,u,d;for(;;){let{done:e,value:t}=await a.read();if(e)break;i.push(t),s+=t.length,c&&l===`text`&&(u=(u??``)+(d??=new TextDecoder).decode(t,{stream:!0}),n.next(new T(o?Number.parseInt(o,10):void 0,s,u)))}let f=A(i,s);try{return M(e,f,t.headers.get(`content-type`)??``)}catch(r){let i=new D({request:e,error:r,status:t.status,statusText:t.statusText,headers:new p(t.headers)});return this._logService.error(`[FetchHTTPImplementation]: network error`,i),n.error(i),null}}};k=y([v(0,t.ILogService)],k);function A(e,t){let n=new Uint8Array(t),r=0;for(let t of e)n.set(t,r),r+=t.length;return n}let j=/^\)\]\}',?\n/;function M(e,t,n){switch(e.responseType){case`json`:let r=new TextDecoder().decode(t).replace(j,``);return r===``?null:JSON.parse(r);case`text`:return new TextDecoder().decode(t);case`blob`:return new Blob([t.buffer],{type:n});case`arraybuffer`:return t.buffer;default:throw Error(`[FetchHTTPImplementation]: unknown response type: ${e.responseType}.`)}}let N=class{constructor(e){this._logService=e}send(e){return new n.Observable(t=>{let n=new XMLHttpRequest,r=e.getUrlWithParams(),i=O(e),{responseType:a}=e;n.open(e.method,r),e.withCredentials&&(n.withCredentials=!0),i.headers&&Object.entries(i.headers).forEach(([e,t])=>n.setRequestHeader(e,t));let o=()=>{let e=n.statusText||`OK`;return new E(new p(n.getAllResponseHeaders()),n.status,e)},s=()=>{let{headers:r,statusText:i,status:s}=o(),c=null,l=null;s!==204&&(c=n.response===void 0?n.responseText:n.response);let u=s>=200&&s<300;if(a===`json`&&typeof c==`string`){let e=c;try{c=c?JSON.parse(c):null}catch(t){u=!1,c=e,l=t}}if(a===`blob`&&!(c instanceof Blob)&&(u=!1,l=Error(`Response is not a Blob object`)),u)t.next(new w({body:c,headers:r,status:s,statusText:i}));else{let n=new D({request:e,error:l,headers:r,status:s,statusText:i});this._logService.error(`[XHRHTTPImplementation]: network error`,n),t.error(n)}},c=r=>{let i=new D({request:e,error:r,status:n.status||0,statusText:n.statusText||`Unknown Error`,headers:o().headers});this._logService.error(`[XHRHTTPImplementation]: network error`,i),t.error(i)};n.responseType=a||``,n.addEventListener(`load`,s),n.addEventListener(`error`,c),n.addEventListener(`abort`,c),n.addEventListener(`timeout`,c);let l=e.getBody();return n.send(l),this._logService.debug(`[XHRHTTPImplementation]`,`sending request to url ${r} with params ${i}`),()=>{n.readyState!==n.DONE&&n.abort(),n.removeEventListener(`load`,s),n.removeEventListener(`error`,c),n.removeEventListener(`abort`,c),n.removeEventListener(`timeout`,c)}})}};N=y([v(0,t.ILogService)],N);let P=class extends t.Plugin{constructor(e=o,n,r,i){super(),this._config=e,this._logger=n,this._injector=r,this._configService=i;let{...a}=(0,t.merge)({},o,this._config);this._configService.setConfig(`network.config`,a)}onStarting(){var e,n,r;if(this._injector.get(b,t.Quantity.OPTIONAL,t.LookUp.SKIP_SELF)&&!((e=this._config)!=null&&e.forceUseNewInstance)){this._logger.warn(`[UniverNetworkPlugin]`,`HTTPService is already registered in an ancestor interceptor. Skipping registration. If you want to force a new instance, set "forceUseNewInstance" to true in the plugin configuration.`);return}let i=(n=this._config)!=null&&n.useFetchImpl?k:typeof window<`u`?N:k;(0,t.registerDependencies)(this._injector,(0,t.mergeOverrideWithDependencies)([[b],[m,{useClass:i}]],(r=this._config)==null?void 0:r.override))}};u(P,`pluginName`,`UNIVER_NETWORK_PLUGIN`),u(P,`packageName`,i),u(P,`version`,a),P=y([v(1,t.ILogService),v(2,(0,t.Inject)(t.Injector)),v(3,t.IConfigService)],P);let F=e=>{let{errorStatusCodes:t,onAuthError:r}=e;return(e,i)=>i(e).pipe((0,n.catchError)(e=>(e instanceof D&&t.some(t=>t===e.status)&&r(),(0,n.throwError)(()=>e))))},I=(e=300)=>{let n=t.noop;return t=>new Promise(t=>{n();let r=setTimeout(()=>{t(!0)},e);n=()=>{clearTimeout(r),t(!1)}})},L=()=>(e,t)=>t.map(t=>({config:t,result:e})),R=(e,r={})=>{let{isMatch:i,getParamsFromRequest:a,mergeParamsToRequest:o}=e,{fetchCheck:s=I(300),distributeResult:c=L()}=r,l=[],u=e=>e.map(e=>e.config);return(e,r)=>i(e)?new n.Observable(n=>{let i={next:e=>n.next(e),error:e=>n.error(e),config:a(e),active:!0};l.push(i);let d=u(l);return s(e).then(t=>{if(t){let t=[];d.forEach(e=>{let n=l.findIndex(t=>t.config===e);if(n>=0){let[e]=l.splice(n,1);t.push(e)}});let i=u(t);if(!i.length)return;let a={hooks:t};t.forEach(e=>e.batch=a),a.subscription=r(o(i,e)).subscribe({next:e=>{if(e.type===1){let n=e.body,r=c(n,i);t.forEach(t=>{let n=r.find(e=>e.config===t.config);if(n){let r=new w({body:n.result,headers:e.headers,status:e.status,statusText:e.statusText});t.next(r)}else t.error(`batch error`)})}},complete:()=>n.complete(),error:e=>n.error(e)})}}),()=>{var e;if(i.active=!1,(0,t.remove)(l,i),(e=i.batch)!=null&&e.hooks.every(e=>!e.active)){var n;(n=i.batch.subscription)==null||n.unsubscribe()}}}):r(e)},z=e=>{let t=(e==null?void 0:e.maxRetryAttempts)??3,n=(e==null?void 0:e.delayInterval)??1e3;return(e,i)=>i(e).pipe((0,r.retry)({delay:n,count:t}))},B=e=>{let r=[],i=new Set,a=()=>{for(;i.size<((e==null?void 0:e.maxParallel)??1)&&r.length>0;){let e=r.shift();i.add(e),e()}};return(e,o)=>new n.Observable(n=>{let s,c=()=>{s=o(e).subscribe({next:e=>n.next(e),error:e=>n.error(e),complete:()=>n.complete()})};return r.push(c),a(),()=>{s==null||s.unsubscribe(),i.delete(c),(0,t.remove)(r,c),a()}})},V=(0,t.createIdentifier)(`univer.network.socket.service`);var H=class extends t.Disposable{createSocket(e){try{let i=new WebSocket(e),a=new t.DisposableCollection;return{URL:e,close:(e,t)=>{i.close(e,t),a.dispose()},send:e=>{i.send(e)},open$:new n.Observable(e=>{let n=t=>e.next(t);i.addEventListener(`open`,n),a.add((0,t.toDisposable)(()=>i.removeEventListener(`open`,n)))}).pipe((0,r.share)()),close$:new n.Observable(e=>{let n=t=>e.next(t);i.addEventListener(`close`,n),a.add((0,t.toDisposable)(()=>i.removeEventListener(`close`,n)))}).pipe((0,r.share)()),error$:new n.Observable(e=>{let n=t=>e.next(t);i.addEventListener(`error`,n),a.add((0,t.toDisposable)(()=>i.removeEventListener(`error`,n)))}).pipe((0,r.share)()),message$:new n.Observable(e=>{let n=t=>e.next(t);i.addEventListener(`message`,n),a.add((0,t.toDisposable)(()=>i.removeEventListener(`message`,n)))}).pipe((0,r.share)())}}catch(e){return console.error(e),null}}};e.AuthInterceptorFactory=F,Object.defineProperty(e,"FetchHTTPImplementation",{enumerable:!0,get:function(){return k}}),e.HTTPEventType=C,e.HTTPHeaders=p,e.HTTPProgress=T,e.HTTPRequest=_,e.HTTPResponse=w,e.HTTPResponseError=D,Object.defineProperty(e,"HTTPService",{enumerable:!0,get:function(){return b}}),e.HTTPStatusCode=S,e.IHTTPImplementation=m,e.ISocketService=V,e.MergeInterceptorFactory=R,e.ResponseHeader=E,e.RetryInterceptorFactory=z,e.ThresholdInterceptorFactory=B,Object.defineProperty(e,"UniverNetworkPlugin",{enumerable:!0,get:function(){return P}}),e.WebSocketService=H,Object.defineProperty(e,"XHRHTTPImplementation",{enumerable:!0,get:function(){return N}})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@univerjs/network",
|
|
3
|
-
"version": "1.0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Network service abstractions for Univer runtime integrations.",
|
|
6
6
|
"author": "DreamNum Co., Ltd. <developer@univer.ai>",
|
|
@@ -62,13 +62,13 @@
|
|
|
62
62
|
"rxjs": ">=7.0.0"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@univerjs/core": "1.0.0
|
|
65
|
+
"@univerjs/core": "1.0.0"
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
|
+
"@univerjs-infra/shared": "1.0.0",
|
|
68
69
|
"rxjs": "^7.8.2",
|
|
69
70
|
"typescript": "^6.0.3",
|
|
70
|
-
"vitest": "^5.0.0"
|
|
71
|
-
"@univerjs-infra/shared": "1.0.0-rc.0"
|
|
71
|
+
"vitest": "^5.0.0"
|
|
72
72
|
},
|
|
73
73
|
"scripts": {
|
|
74
74
|
"test": "vitest run",
|