@univerjs/network 0.2.2 → 0.2.4-alpha.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/README.md +11 -2
- package/lib/cjs/index.js +2 -2
- package/lib/es/index.js +242 -186
- package/lib/types/index.d.ts +1 -0
- package/lib/types/services/http/__testing__/http-testing-utils.d.ts +1 -2
- package/lib/types/services/http/http.service.d.ts +1 -2
- package/lib/types/services/http/implementations/implementation.d.ts +1 -1
- package/lib/types/services/http/interceptors/__tests__/merge-interceptor.spec.d.ts +16 -0
- package/lib/types/services/http/interceptors/merge-interceptor.d.ts +33 -0
- package/lib/types/services/http/request.d.ts +1 -1
- package/lib/types/services/web-socket/web-socket.service.d.ts +1 -1
- package/lib/umd/index.js +2 -2
- package/package.json +6 -8
package/README.md
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
# @univerjs/network
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
## Package Overview
|
|
4
|
+
|
|
5
|
+
| Package Name | UMD Namespace | Version | License | Downloads | Contains CSS | Contains i18n locales |
|
|
6
|
+
| --- | --- | --- | --- | --- | :---: | :---: |
|
|
7
|
+
| `@univerjs/network` | `UniverNetwork` | [![][npm-version-shield]][npm-version-link] | ![][npm-license-shield] | ![][npm-downloads-shield] | ❌ | ❌ |
|
|
5
8
|
|
|
6
9
|
## Introduction
|
|
7
10
|
|
|
@@ -22,3 +25,9 @@ pnpm add @univerjs/network
|
|
|
22
25
|
## Credit
|
|
23
26
|
|
|
24
27
|
This package is inspired by [Angular's implementation](https://github.com/angular/angular/tree/main/packages/common/http). Removed zone.js dependency and features that are not necessary for Univer.
|
|
28
|
+
|
|
29
|
+
<!-- Links -->
|
|
30
|
+
[npm-version-shield]: https://img.shields.io/npm/v/@univerjs/network?style=flat-square
|
|
31
|
+
[npm-version-link]: https://npmjs.com/package/@univerjs/network
|
|
32
|
+
[npm-license-shield]: https://img.shields.io/npm/l/@univerjs/network?style=flat-square
|
|
33
|
+
[npm-downloads-shield]: https://img.shields.io/npm/dm/@univerjs/network?style=flat-square
|
package/lib/cjs/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`).forEach(r=>{const[n,s]=r.split(":");n&&s&&this._setHeader(n,s)})}_handleHeadersConstructorProps(t){Object.entries(t).forEach(([r,n])=>this._setHeader(r,n))}_handleHeaders(t){t.forEach((r,n)=>this._setHeader(n,r))}}const F=q.createIdentifier("network.http-implementation");class A{constructor(t){this.params=t}toString(){return this.params?Object.keys(this.params).map(t=>`${t}=${this.params[t]}`).join("&"):""}}let M=0;class I{constructor(t,r,n){p(this,"uid",M++);this.method=t,this.url=r,this.requestParams=n}get headers(){return this.requestParams.headers}get withCredentials(){return this.requestParams.withCredentials}get responseType(){return this.requestParams.responseType}getUrlWithParams(){var r,n;const t=(n=(r=this.requestParams)==null?void 0:r.params)==null?void 0:n.toString();return t?`${this.url}${this.url.includes("?")?"&":"?"}${t}`:this.url}getBody(){var n,s;const t=(n=this.headers.get("Content-Type"))!=null?n:L,r=(s=this.requestParams)==null?void 0:s.body;return t===L&&r&&typeof r=="object"?JSON.stringify(r):r?`${r}`:null}getHeadersInit(){return this.headers.toHeadersInit()}}var j=Object.defineProperty,B=Object.getOwnPropertyDescriptor,$=(e,t,r,n)=>{for(var s=n>1?void 0:n?B(t,r):t,i=e.length-1,o;i>=0;i--)(o=e[i])&&(s=(n?o(t,r,s):o(s))||s);return n&&s&&j(t,r,s),s},G=(e,t)=>(r,n)=>t(r,n,e);exports.HTTPService=class extends m.Disposable{constructor(r){super();p(this,"_interceptors",[]);p(this,"_pipe");this._http=r}registerHTTPInterceptor(r){if(this._interceptors.indexOf(r)!==-1)throw new Error("[HTTPService]: The interceptor has already been registered!");return this._interceptors.push(r),this._interceptors=this._interceptors.sort((n,s)=>{var i,o;return((i=n.priority)!=null?i:0)-((o=s.priority)!=null?o:0)}),this._pipe=null,m.toDisposable(()=>m.remove(this._interceptors,r))}get(r,n){return this._request("GET",r,n)}post(r,n){return this._request("POST",r,n)}put(r,n){return this._request("PUT",r,n)}delete(r,n){return this._request("DELETE",r,n)}patch(r,n){return this._request("PATCH",r,n)}getSSE(r,n,s){var c,l;const i=new E(s==null?void 0:s.headers),o=new A(s==null?void 0:s.params),a=new I(r,n,{headers:i,params:o,withCredentials:(c=s==null?void 0:s.withCredentials)!=null?c:!1,reportProgress:!0,responseType:(l=s==null?void 0:s.responseType)!=null?l:"json",body:["GET","DELETE"].includes(r)||s==null?void 0:s.body});return y.of(a).pipe(g.concatMap(h=>this._runInterceptorsAndImplementation(h)))}async _request(r,n,s){var h,u;const i=new E(s==null?void 0:s.headers),o=new A(s==null?void 0:s.params),a=new I(r,n,{headers:i,params:o,withCredentials:(h=s==null?void 0:s.withCredentials)!=null?h:!1,responseType:(u=s==null?void 0:s.responseType)!=null?u:"json",body:["GET","DELETE"].includes(r)||s==null?void 0:s.body}),c=y.of(a).pipe(g.concatMap(d=>this._runInterceptorsAndImplementation(d)));return await y.firstValueFrom(c)}_runInterceptorsAndImplementation(r){return this._pipe||(this._pipe=this._interceptors.map(n=>n.interceptor).reduceRight((n,s)=>V(n,s),(n,s)=>s(n))),this._pipe(r,n=>this._http.send(n))}};exports.HTTPService=$([G(0,F)],exports.HTTPService);function V(e,t){return(r,n)=>t(r,s=>e(s,n))}var N=(e=>(e[e.DownloadProgress=0]="DownloadProgress",e[e.Response=1]="Response",e))(N||{});class x{constructor({body:t,headers:r,status:n,statusText:s}){p(this,"type",1);p(this,"body");p(this,"headers");p(this,"status");p(this,"statusText");this.body=t,this.headers=r,this.status=n,this.statusText=s}}class U{constructor(t,r,n){p(this,"type",0);this.total=t,this.loaded=r,this.partialText=n}}class k{constructor(t,r,n){this.headers=t,this.status=r,this.statusText=n}}class w{constructor({headers:t,status:r,statusText:n,error:s}){p(this,"headers");p(this,"status");p(this,"statusText");p(this,"error");this.headers=t,this.status=r,this.statusText=n,this.error=s}}const W=200,X=300;var _=(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))(_||{});class z{send(t){return new y.Observable(r=>{const n=new AbortController;return this._send(t,r,n).then(()=>{},s=>{r.error(new w({error:s}))}),()=>n.abort()})}async _send(t,r,n){var h,u;let s;try{const d=this._parseFetchParamsFromRequest(t);s=await fetch(t.getUrlWithParams(),{signal:n.signal,...d})}catch(d){r.error(new w({error:d,status:(h=d.status)!=null?h:0,statusText:(u=d.statusText)!=null?u:"Unknown Error",headers:d.headers}));return}const i=new E(s.headers),o=s.status,a=s.statusText;let c=null;s.body&&(c=await this._readBody(t,s,r)),o>=_.Ok&&o<_.MultipleChoices?r.next(new x({body:c,headers:i,status:o,statusText:a})):r.error(new w({error:c,status:o,statusText:a,headers:i})),r.complete()}async _readBody(t,r,n){var v,R;const s=[],i=r.body.getReader(),o=r.headers.get("content-length");let a=0;const c=(v=t.requestParams)==null?void 0:v.reportProgress,l=t.responseType;let h,u;for(;;){const{done:f,value:b}=await i.read();if(f)break;s.push(b),a+=b.length,c&&l==="text"&&(h=(h!=null?h:"")+(u!=null?u:u=new TextDecoder).decode(b,{stream:!0}),n.next(new U(o?Number.parseInt(o,10):void 0,a,h)))}const d=J(s,a);try{const f=(R=r.headers.get("content-type"))!=null?R:"";return K(t,d,f)}catch(f){return n.error(new w({error:f,status:r.status,statusText:r.statusText,headers:new E(r.headers)})),null}}_parseFetchParamsFromRequest(t){return{method:t.method,headers:t.getHeadersInit(),body:t.getBody(),credentials:t.withCredentials?"include":void 0}}}function J(e,t){const r=new Uint8Array(t);let n=0;for(const s of e)r.set(s,n),n+=s.length;return r}const Y=/^\)\]\}',?\n/;function K(e,t,r){switch(e.responseType){case"json":const n=new TextDecoder().decode(t).replace(Y,"");return n===""?null:JSON.parse(n);case"text":return new TextDecoder().decode(t);case"blob":return new Blob([t],{type:r});case"arraybuffer":return t.buffer;default:throw new Error(`[FetchHTTPImplementation]: unknown response type: ${e.responseType}.`)}}class Q{send(t){return new y.Observable(r=>{const n=new XMLHttpRequest;n.open(t.method,t.getUrlWithParams()),t.withCredentials&&(n.withCredentials=!0),t.headers.forEach((c,l)=>n.setRequestHeader(c,l.join(","))),t.headers.has("Accept")||n.setRequestHeader("Accept","application/json, text/plain, */*"),t.headers.has("Content-Type")||n.setRequestHeader("Content-Type","application/json;charset=UTF-8");const s=()=>{const c=n.statusText||"OK",l=new E(n.getAllResponseHeaders());return new k(l,n.status,c)},i=()=>{const{headers:c,statusText:l,status:h}=s(),{responseType:u}=t;let d=null,v=null;h!==_.NoContent&&(d=typeof n.response>"u"?n.responseText:n.response);let R=h>=W&&h<X;if(u==="json"&&typeof d=="string"){const f=d;try{d=d?JSON.parse(d):null}catch(b){R=!1,d=f,v=b}}R?r.next(new x({body:d,headers:c,status:h,statusText:l})):r.error(new w({error:v,headers:c,status:h,statusText:l}))},o=c=>{const l=new w({error:c,status:n.status||0,statusText:n.statusText||"Unknown Error",headers:s().headers});r.error(l)};n.addEventListener("load",i),n.addEventListener("error",o),n.addEventListener("abort",o),n.addEventListener("timeout",o);const a=t.getBody();return n.send(a),()=>{n.readyState!==n.DONE&&n.abort(),n.removeEventListener("load",i),n.removeEventListener("error",o),n.removeEventListener("abort",o),n.removeEventListener("timeout",o)}})}}const Z=q.createIdentifier("univer.socket");class P extends m.Disposable{createSocket(t){try{const r=new WebSocket(t),n=new m.DisposableCollection;return{URL:t,close:(i,o)=>{r.close(i,o),n.dispose()},send:i=>{r.send(i)},open$:new y.Observable(i=>{const o=a=>i.next(a);r.addEventListener("open",o),n.add(m.toDisposable(()=>r.removeEventListener("open",o)))}).pipe(g.share()),close$:new y.Observable(i=>{const o=a=>i.next(a);r.addEventListener("close",o),n.add(m.toDisposable(()=>r.removeEventListener("close",o)))}).pipe(g.share()),error$:new y.Observable(i=>{const o=a=>i.next(a);r.addEventListener("error",o),n.add(m.toDisposable(()=>r.removeEventListener("error",o)))}).pipe(g.share()),message$:new y.Observable(i=>{const o=a=>i.next(a);r.addEventListener("message",o),n.add(m.toDisposable(()=>r.removeEventListener("message",o)))}).pipe(g.share())}}catch(r){return console.error(r),null}}}const H=3,S=1e3,C=(e,t)=>{var s,i;const r=(s=t==null?void 0:t.maxRetryAttempts)!=null?s:H,n=(i=t==null?void 0:t.delayInterval)!=null?i:S;return(o,a)=>a(o).pipe(g.retry({delay:n,count:r}))},T=e=>{const t=[],r=new Set,n=()=>{var s;for(;r.size<((s=e==null?void 0:e.maxParallel)!=null?s:1)&&t.length>0;){const i=t.shift();r.add(i),i()}};return(s,i)=>new y.Observable(o=>{const a=()=>i(s).subscribe({next:l=>o.next(l),error:l=>o.next(l),complete:()=>o.complete()}),c=()=>{r.delete(a),m.remove(t,a),n()};return t.push(a),n(),c})},ee=e=>{const{errorStatusCodes:t,onAuthError:r}=e;return(s,i)=>i(s).pipe(y.catchError(o=>(o instanceof w&&t.some(a=>a===o.status)&&r(),y.throwError(()=>o))))};exports.AuthInterceptorFactory=ee;exports.FetchHTTPImplementation=z;exports.HTTPEventType=N;exports.HTTPHeaders=E;exports.HTTPProgress=U;exports.HTTPRequest=I;exports.HTTPResponse=x;exports.HTTPResponseError=w;exports.HTTPStatusCode=_;exports.IHTTPImplementation=F;exports.ISocketService=Z;exports.ResponseHeader=k;exports.RetryInterceptorFactory=C;exports.ThresholdInterceptorFactory=T;exports.WebSocketService=P;exports.XHRHTTPImplementation=Q;
|
|
1
|
+
"use strict";var B=Object.defineProperty;var $=(e,t,r)=>t in e?B(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;var y=(e,t,r)=>$(e,typeof t!="symbol"?t+"":t,r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("@univerjs/core"),m=require("rxjs"),v=require("rxjs/operators"),U="application/json";class E{constructor(t){y(this,"_headers",new Map);typeof t=="string"?this._handleHeadersString(t):t instanceof Headers?this._handleHeaders(t):t&&this._handleHeadersConstructorProps(t)}forEach(t){this._headers.forEach((r,n)=>t(n,r))}has(t){return!!this._headers.has(t.toLowerCase())}get(t){const r=t.toLowerCase();return this._headers.has(r)?this._headers.get(r):null}toHeadersInit(){var r,n;const t={};return this._headers.forEach((s,c)=>{t[c]=s.join(",")}),(r=t.accept)!=null||(t.accept="application/json, text/plain, */*"),(n=t["content-type"])!=null||(t["content-type"]="application/json;charset=UTF-8"),t}_setHeader(t,r){const n=t.toLowerCase();this._headers.has(n)?this._headers.get(n).push(r.toString()):this._headers.set(n,[r.toString()])}_handleHeadersString(t){t.split(`
|
|
2
|
+
`).forEach(r=>{const[n,s]=r.split(":");n&&s&&this._setHeader(n,s)})}_handleHeadersConstructorProps(t){Object.entries(t).forEach(([r,n])=>this._setHeader(r,n))}_handleHeaders(t){t.forEach((r,n)=>this._setHeader(n,r))}}const O=f.createIdentifier("network.http-implementation");class D{constructor(t){this.params=t}toString(){return this.params?Object.keys(this.params).map(t=>`${t}=${this.params[t]}`).join("&"):""}}let G=0;class q{constructor(t,r,n){y(this,"uid",G++);this.method=t,this.url=r,this.requestParams=n}get headers(){return this.requestParams.headers}get withCredentials(){return this.requestParams.withCredentials}get responseType(){return this.requestParams.responseType}getUrlWithParams(){var r,n;const t=(n=(r=this.requestParams)==null?void 0:r.params)==null?void 0:n.toString();return t?`${this.url}${this.url.includes("?")?"&":"?"}${t}`:this.url}getBody(){var n,s;const t=(n=this.headers.get("Content-Type"))!=null?n:U,r=(s=this.requestParams)==null?void 0:s.body;return t===U&&r&&typeof r=="object"?JSON.stringify(r):r?`${r}`:null}getHeadersInit(){return this.headers.toHeadersInit()}}var V=Object.defineProperty,W=Object.getOwnPropertyDescriptor,X=(e,t,r,n)=>{for(var s=n>1?void 0:n?W(t,r):t,c=e.length-1,o;c>=0;c--)(o=e[c])&&(s=(n?o(t,r,s):o(s))||s);return n&&s&&V(t,r,s),s},z=(e,t)=>(r,n)=>t(r,n,e);exports.HTTPService=class extends f.Disposable{constructor(r){super();y(this,"_interceptors",[]);y(this,"_pipe");this._http=r}registerHTTPInterceptor(r){if(this._interceptors.indexOf(r)!==-1)throw new Error("[HTTPService]: The interceptor has already been registered!");return this._interceptors.push(r),this._interceptors=this._interceptors.sort((n,s)=>{var c,o;return((c=n.priority)!=null?c:0)-((o=s.priority)!=null?o:0)}),this._pipe=null,f.toDisposable(()=>f.remove(this._interceptors,r))}get(r,n){return this._request("GET",r,n)}post(r,n){return this._request("POST",r,n)}put(r,n){return this._request("PUT",r,n)}delete(r,n){return this._request("DELETE",r,n)}patch(r,n){return this._request("PATCH",r,n)}getSSE(r,n,s){var h,a;const c=new E(s==null?void 0:s.headers),o=new D(s==null?void 0:s.params),i=new q(r,n,{headers:c,params:o,withCredentials:(h=s==null?void 0:s.withCredentials)!=null?h:!1,reportProgress:!0,responseType:(a=s==null?void 0:s.responseType)!=null?a:"json",body:["GET","DELETE"].includes(r)||s==null?void 0:s.body});return m.of(i).pipe(v.concatMap(l=>this._runInterceptorsAndImplementation(l)))}async _request(r,n,s){var l,d;const c=new E(s==null?void 0:s.headers),o=new D(s==null?void 0:s.params),i=new q(r,n,{headers:c,params:o,withCredentials:(l=s==null?void 0:s.withCredentials)!=null?l:!1,responseType:(d=s==null?void 0:s.responseType)!=null?d:"json",body:["GET","DELETE"].includes(r)||s==null?void 0:s.body}),h=m.of(i).pipe(v.concatMap(p=>this._runInterceptorsAndImplementation(p)));return await m.firstValueFrom(h)}_runInterceptorsAndImplementation(r){return this._pipe||(this._pipe=this._interceptors.map(n=>n.interceptor).reduceRight((n,s)=>J(n,s),(n,s)=>s(n))),this._pipe(r,n=>this._http.send(n))}};exports.HTTPService=X([z(0,O)],exports.HTTPService);function J(e,t){return(r,n)=>t(r,s=>e(s,n))}var N=(e=>(e[e.DownloadProgress=0]="DownloadProgress",e[e.Response=1]="Response",e))(N||{});class L{constructor({body:t,headers:r,status:n,statusText:s}){y(this,"type",1);y(this,"body");y(this,"headers");y(this,"status");y(this,"statusText");this.body=t,this.headers=r,this.status=n,this.statusText=s}}class M{constructor(t,r,n){y(this,"type",0);this.total=t,this.loaded=r,this.partialText=n}}class j{constructor(t,r,n){this.headers=t,this.status=r,this.statusText=n}}class R{constructor({headers:t,status:r,statusText:n,error:s}){y(this,"headers");y(this,"status");y(this,"statusText");y(this,"error");this.headers=t,this.status=r,this.statusText=n,this.error=s}}const Y=200,K=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 Q{send(t){return new m.Observable(r=>{const n=new AbortController;return this._send(t,r,n).then(()=>{},s=>{r.error(new R({error:s}))}),()=>n.abort()})}async _send(t,r,n){var l,d;let s;try{const p=this._parseFetchParamsFromRequest(t);s=await fetch(t.getUrlWithParams(),{signal:n.signal,...p})}catch(p){r.error(new R({error:p,status:(l=p.status)!=null?l:0,statusText:(d=p.statusText)!=null?d:"Unknown Error",headers:p.headers}));return}const c=new E(s.headers),o=s.status,i=s.statusText;let h=null;s.body&&(h=await this._readBody(t,s,r)),o>=x.Ok&&o<x.MultipleChoices?r.next(new L({body:h,headers:c,status:o,statusText:i})):r.error(new R({error:h,status:o,statusText:i,headers:c})),r.complete()}async _readBody(t,r,n){var g,w;const s=[],c=r.body.getReader(),o=r.headers.get("content-length");let i=0;const h=(g=t.requestParams)==null?void 0:g.reportProgress,a=t.responseType;let l,d;for(;;){const{done:b,value:u}=await c.read();if(b)break;s.push(u),i+=u.length,h&&a==="text"&&(l=(l!=null?l:"")+(d!=null?d:d=new TextDecoder).decode(u,{stream:!0}),n.next(new M(o?Number.parseInt(o,10):void 0,i,l)))}const p=Z(s,i);try{const b=(w=r.headers.get("content-type"))!=null?w:"";return H(t,p,b)}catch(b){return n.error(new R({error:b,status:r.status,statusText:r.statusText,headers:new E(r.headers)})),null}}_parseFetchParamsFromRequest(t){return{method:t.method,headers:t.getHeadersInit(),body:t.getBody(),credentials:t.withCredentials?"include":void 0}}}function Z(e,t){const r=new Uint8Array(t);let n=0;for(const s of e)r.set(s,n),n+=s.length;return r}const P=/^\)\]\}',?\n/;function H(e,t,r){switch(e.responseType){case"json":const n=new TextDecoder().decode(t).replace(P,"");return n===""?null:JSON.parse(n);case"text":return new TextDecoder().decode(t);case"blob":return new Blob([t],{type:r});case"arraybuffer":return t.buffer;default:throw new Error(`[FetchHTTPImplementation]: unknown response type: ${e.responseType}.`)}}class S{send(t){return new m.Observable(r=>{const n=new XMLHttpRequest;n.open(t.method,t.getUrlWithParams()),t.withCredentials&&(n.withCredentials=!0),t.headers.forEach((h,a)=>n.setRequestHeader(h,a.join(","))),t.headers.has("Accept")||n.setRequestHeader("Accept","application/json, text/plain, */*"),t.headers.has("Content-Type")||n.setRequestHeader("Content-Type","application/json;charset=UTF-8");const s=()=>{const h=n.statusText||"OK",a=new E(n.getAllResponseHeaders());return new j(a,n.status,h)},c=()=>{const{headers:h,statusText:a,status:l}=s(),{responseType:d}=t;let p=null,g=null;l!==x.NoContent&&(p=typeof n.response>"u"?n.responseText:n.response);let w=l>=Y&&l<K;if(d==="json"&&typeof p=="string"){const b=p;try{p=p?JSON.parse(p):null}catch(u){w=!1,p=b,g=u}}w?r.next(new L({body:p,headers:h,status:l,statusText:a})):r.error(new R({error:g,headers:h,status:l,statusText:a}))},o=h=>{const a=new R({error:h,status:n.status||0,statusText:n.statusText||"Unknown Error",headers:s().headers});r.error(a)};n.addEventListener("load",c),n.addEventListener("error",o),n.addEventListener("abort",o),n.addEventListener("timeout",o);const i=t.getBody();return n.send(i),()=>{n.readyState!==n.DONE&&n.abort(),n.removeEventListener("load",c),n.removeEventListener("error",o),n.removeEventListener("abort",o),n.removeEventListener("timeout",o)}})}}const C=f.createIdentifier("univer.socket");class T extends f.Disposable{createSocket(t){try{const r=new WebSocket(t),n=new f.DisposableCollection;return{URL:t,close:(c,o)=>{r.close(c,o),n.dispose()},send:c=>{r.send(c)},open$:new m.Observable(c=>{const o=i=>c.next(i);r.addEventListener("open",o),n.add(f.toDisposable(()=>r.removeEventListener("open",o)))}).pipe(v.share()),close$:new m.Observable(c=>{const o=i=>c.next(i);r.addEventListener("close",o),n.add(f.toDisposable(()=>r.removeEventListener("close",o)))}).pipe(v.share()),error$:new m.Observable(c=>{const o=i=>c.next(i);r.addEventListener("error",o),n.add(f.toDisposable(()=>r.removeEventListener("error",o)))}).pipe(v.share()),message$:new m.Observable(c=>{const o=i=>c.next(i);r.addEventListener("message",o),n.add(f.toDisposable(()=>r.removeEventListener("message",o)))}).pipe(v.share())}}catch(r){return console.error(r),null}}}const ee=3,re=1e3,te=(e,t)=>{var s,c;const r=(s=t==null?void 0:t.maxRetryAttempts)!=null?s:ee,n=(c=t==null?void 0:t.delayInterval)!=null?c:re;return(o,i)=>i(o).pipe(v.retry({delay:n,count:r}))},ne=e=>{const t=[],r=new Set,n=()=>{var s;for(;r.size<((s=e==null?void 0:e.maxParallel)!=null?s:1)&&t.length>0;){const c=t.shift();r.add(c),c()}};return(s,c)=>new m.Observable(o=>{const i=()=>c(s).subscribe({next:a=>o.next(a),error:a=>o.next(a),complete:()=>o.complete()}),h=()=>{r.delete(i),f.remove(t,i),n()};return t.push(i),n(),h})},se=e=>{const{errorStatusCodes:t,onAuthError:r}=e;return(s,c)=>c(s).pipe(m.catchError(o=>(o instanceof R&&t.some(i=>i===o.status)&&r(),m.throwError(()=>o))))},oe=(e=300)=>{let r=()=>{};return n=>new Promise(s=>{r();const c=setTimeout(()=>{s(!0)},e);r=()=>{clearTimeout(c),s(!1)}})},ce=()=>(e,t)=>t.map(r=>({config:r,result:e})),ie=(e,t={})=>{const{isMatch:r,getParamsFromRequest:n,mergeParamsToRequest:s}=e,{fetchCheck:c=oe(300),distributeResult:o=ce()}=t,i=[],h=a=>a.map(l=>l.config);return(a,l)=>r(a)?new m.Observable(d=>{const p=n(a);i.push({next:w=>d.next(w),error:w=>d.error(w),config:p});const g=h(i);c(a).then(w=>{if(w){const b=[];g.forEach(u=>{const I=i.findIndex(_=>_.config===u);if(I>=0){const[_]=i.splice(I,1);b.push(_)}}),l(s(g,a)).subscribe({next:u=>{if(u.type===N.Response){const I=u.body,_=o(I,g);b.forEach(F=>{const k=_.find(A=>A.config===F.config);if(k){const A=new L({body:k.result,headers:u.headers,status:u.status,statusText:u.statusText});F.next(A)}else F.error("batch error")})}},complete:()=>d.complete(),error:u=>d.error(u)})}})}):l(a)};exports.AuthInterceptorFactory=se;exports.FetchHTTPImplementation=Q;exports.HTTPEventType=N;exports.HTTPHeaders=E;exports.HTTPProgress=M;exports.HTTPRequest=q;exports.HTTPResponse=L;exports.HTTPResponseError=R;exports.HTTPStatusCode=x;exports.IHTTPImplementation=O;exports.ISocketService=C;exports.MergeInterceptorFactory=ie;exports.ResponseHeader=j;exports.RetryInterceptorFactory=te;exports.ThresholdInterceptorFactory=ne;exports.WebSocketService=T;exports.XHRHTTPImplementation=S;
|
package/lib/es/index.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { Disposable as
|
|
5
|
-
import { of as
|
|
6
|
-
import { concatMap as
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class R {
|
|
1
|
+
var V = Object.defineProperty;
|
|
2
|
+
var W = (e, r, n) => r in e ? V(e, r, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[r] = n;
|
|
3
|
+
var y = (e, r, n) => W(e, typeof r != "symbol" ? r + "" : r, n);
|
|
4
|
+
import { createIdentifier as j, Disposable as B, toDisposable as _, remove as $, DisposableCollection as X } from "@univerjs/core";
|
|
5
|
+
import { of as U, firstValueFrom as z, Observable as g, catchError as J, throwError as Y } from "rxjs";
|
|
6
|
+
import { concatMap as k, share as v, retry as K } from "rxjs/operators";
|
|
7
|
+
const q = "application/json";
|
|
8
|
+
class x {
|
|
10
9
|
constructor(r) {
|
|
11
|
-
|
|
10
|
+
y(this, "_headers", /* @__PURE__ */ new Map());
|
|
12
11
|
typeof r == "string" ? this._handleHeadersString(r) : r instanceof Headers ? this._handleHeaders(r) : r && this._handleHeadersConstructorProps(r);
|
|
13
12
|
}
|
|
14
13
|
forEach(r) {
|
|
@@ -24,8 +23,8 @@ class R {
|
|
|
24
23
|
toHeadersInit() {
|
|
25
24
|
var n, t;
|
|
26
25
|
const r = {};
|
|
27
|
-
return this._headers.forEach((s,
|
|
28
|
-
r[
|
|
26
|
+
return this._headers.forEach((s, i) => {
|
|
27
|
+
r[i] = s.join(",");
|
|
29
28
|
}), (n = r.accept) != null || (r.accept = "application/json, text/plain, */*"), (t = r["content-type"]) != null || (r["content-type"] = "application/json;charset=UTF-8"), r;
|
|
30
29
|
}
|
|
31
30
|
_setHeader(r, n) {
|
|
@@ -46,8 +45,8 @@ class R {
|
|
|
46
45
|
r.forEach((n, t) => this._setHeader(t, n));
|
|
47
46
|
}
|
|
48
47
|
}
|
|
49
|
-
const
|
|
50
|
-
class
|
|
48
|
+
const Q = j("network.http-implementation");
|
|
49
|
+
class M {
|
|
51
50
|
constructor(r) {
|
|
52
51
|
this.params = r;
|
|
53
52
|
}
|
|
@@ -55,10 +54,10 @@ class A {
|
|
|
55
54
|
return this.params ? Object.keys(this.params).map((r) => `${r}=${this.params[r]}`).join("&") : "";
|
|
56
55
|
}
|
|
57
56
|
}
|
|
58
|
-
let
|
|
59
|
-
class
|
|
57
|
+
let Z = 0;
|
|
58
|
+
class D {
|
|
60
59
|
constructor(r, n, t) {
|
|
61
|
-
|
|
60
|
+
y(this, "uid", Z++);
|
|
62
61
|
this.method = r, this.url = n, this.requestParams = t;
|
|
63
62
|
}
|
|
64
63
|
get headers() {
|
|
@@ -77,24 +76,24 @@ class N {
|
|
|
77
76
|
}
|
|
78
77
|
getBody() {
|
|
79
78
|
var t, s;
|
|
80
|
-
const r = (t = this.headers.get("Content-Type")) != null ? t :
|
|
81
|
-
return r ===
|
|
79
|
+
const r = (t = this.headers.get("Content-Type")) != null ? t : q, n = (s = this.requestParams) == null ? void 0 : s.body;
|
|
80
|
+
return r === q && n && typeof n == "object" ? JSON.stringify(n) : n ? `${n}` : null;
|
|
82
81
|
}
|
|
83
82
|
getHeadersInit() {
|
|
84
83
|
return this.headers.toHeadersInit();
|
|
85
84
|
}
|
|
86
85
|
}
|
|
87
|
-
var
|
|
88
|
-
for (var s = t > 1 ? void 0 : t ?
|
|
89
|
-
(
|
|
90
|
-
return t && s &&
|
|
91
|
-
},
|
|
92
|
-
let
|
|
86
|
+
var P = Object.defineProperty, S = Object.getOwnPropertyDescriptor, H = (e, r, n, t) => {
|
|
87
|
+
for (var s = t > 1 ? void 0 : t ? S(r, n) : r, i = e.length - 1, o; i >= 0; i--)
|
|
88
|
+
(o = e[i]) && (s = (t ? o(r, n, s) : o(s)) || s);
|
|
89
|
+
return t && s && P(r, n, s), s;
|
|
90
|
+
}, C = (e, r) => (n, t) => r(n, t, e);
|
|
91
|
+
let O = class extends B {
|
|
93
92
|
constructor(r) {
|
|
94
93
|
super();
|
|
95
|
-
|
|
94
|
+
y(this, "_interceptors", []);
|
|
96
95
|
// eslint-disable-next-line ts/no-explicit-any
|
|
97
|
-
|
|
96
|
+
y(this, "_pipe");
|
|
98
97
|
this._http = r;
|
|
99
98
|
}
|
|
100
99
|
/**
|
|
@@ -107,9 +106,9 @@ let F = class extends q {
|
|
|
107
106
|
if (this._interceptors.indexOf(r) !== -1)
|
|
108
107
|
throw new Error("[HTTPService]: The interceptor has already been registered!");
|
|
109
108
|
return this._interceptors.push(r), this._interceptors = this._interceptors.sort((n, t) => {
|
|
110
|
-
var s,
|
|
111
|
-
return ((s = n.priority) != null ? s : 0) - ((
|
|
112
|
-
}), this._pipe = null, _(() =>
|
|
109
|
+
var s, i;
|
|
110
|
+
return ((s = n.priority) != null ? s : 0) - ((i = t.priority) != null ? i : 0);
|
|
111
|
+
}), this._pipe = null, _(() => $(this._interceptors, r));
|
|
113
112
|
}
|
|
114
113
|
get(r, n) {
|
|
115
114
|
return this._request("GET", r, n);
|
|
@@ -127,36 +126,36 @@ let F = class extends q {
|
|
|
127
126
|
return this._request("PATCH", r, n);
|
|
128
127
|
}
|
|
129
128
|
getSSE(r, n, t) {
|
|
130
|
-
var
|
|
131
|
-
const s = new
|
|
129
|
+
var c, l;
|
|
130
|
+
const s = new x(t == null ? void 0 : t.headers), i = new M(t == null ? void 0 : t.params), o = new D(r, n, {
|
|
132
131
|
headers: s,
|
|
133
|
-
params:
|
|
134
|
-
withCredentials: (
|
|
132
|
+
params: i,
|
|
133
|
+
withCredentials: (c = t == null ? void 0 : t.withCredentials) != null ? c : !1,
|
|
135
134
|
reportProgress: !0,
|
|
136
|
-
responseType: (
|
|
135
|
+
responseType: (l = t == null ? void 0 : t.responseType) != null ? l : "json",
|
|
137
136
|
body: ["GET", "DELETE"].includes(r) || t == null ? void 0 : t.body
|
|
138
137
|
});
|
|
139
|
-
return
|
|
138
|
+
return U(o).pipe(k((a) => this._runInterceptorsAndImplementation(a)));
|
|
140
139
|
}
|
|
141
140
|
/** The HTTP request implementations */
|
|
142
141
|
async _request(r, n, t) {
|
|
143
|
-
var
|
|
144
|
-
const s = new
|
|
142
|
+
var a, h;
|
|
143
|
+
const s = new x(t == null ? void 0 : t.headers), i = new M(t == null ? void 0 : t.params), o = new D(r, n, {
|
|
145
144
|
headers: s,
|
|
146
|
-
params:
|
|
147
|
-
withCredentials: (
|
|
145
|
+
params: i,
|
|
146
|
+
withCredentials: (a = t == null ? void 0 : t.withCredentials) != null ? a : !1,
|
|
148
147
|
// default value for withCredentials is false by MDN
|
|
149
148
|
responseType: (h = t == null ? void 0 : t.responseType) != null ? h : "json",
|
|
150
149
|
body: ["GET", "DELETE"].includes(r) || t == null ? void 0 : t.body
|
|
151
|
-
}),
|
|
152
|
-
|
|
150
|
+
}), c = U(o).pipe(
|
|
151
|
+
k((u) => this._runInterceptorsAndImplementation(u))
|
|
153
152
|
);
|
|
154
|
-
return await
|
|
153
|
+
return await z(c);
|
|
155
154
|
}
|
|
156
155
|
// eslint-disable-next-line ts/no-explicit-any
|
|
157
156
|
_runInterceptorsAndImplementation(r) {
|
|
158
157
|
return this._pipe || (this._pipe = this._interceptors.map((n) => n.interceptor).reduceRight(
|
|
159
|
-
(n, t) =>
|
|
158
|
+
(n, t) => T(n, t),
|
|
160
159
|
(n, t) => t(n)
|
|
161
160
|
)), this._pipe(
|
|
162
161
|
r,
|
|
@@ -165,69 +164,69 @@ let F = class extends q {
|
|
|
165
164
|
);
|
|
166
165
|
}
|
|
167
166
|
};
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
],
|
|
171
|
-
function
|
|
167
|
+
O = H([
|
|
168
|
+
C(0, Q)
|
|
169
|
+
], O);
|
|
170
|
+
function T(e, r) {
|
|
172
171
|
return (n, t) => r(n, (s) => e(s, t));
|
|
173
172
|
}
|
|
174
|
-
var
|
|
175
|
-
class
|
|
173
|
+
var G = /* @__PURE__ */ ((e) => (e[e.DownloadProgress = 0] = "DownloadProgress", e[e.Response = 1] = "Response", e))(G || {});
|
|
174
|
+
class F {
|
|
176
175
|
constructor({
|
|
177
176
|
body: r,
|
|
178
177
|
headers: n,
|
|
179
178
|
status: t,
|
|
180
179
|
statusText: s
|
|
181
180
|
}) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
181
|
+
y(this, "type", 1);
|
|
182
|
+
y(this, "body");
|
|
183
|
+
y(this, "headers");
|
|
184
|
+
y(this, "status");
|
|
185
|
+
y(this, "statusText");
|
|
187
186
|
this.body = r, this.headers = n, this.status = t, this.statusText = s;
|
|
188
187
|
}
|
|
189
188
|
}
|
|
190
|
-
class
|
|
189
|
+
class ee {
|
|
191
190
|
constructor(r, n, t) {
|
|
192
|
-
|
|
191
|
+
y(this, "type", 0);
|
|
193
192
|
this.total = r, this.loaded = n, this.partialText = t;
|
|
194
193
|
}
|
|
195
194
|
}
|
|
196
|
-
class
|
|
195
|
+
class re {
|
|
197
196
|
constructor(r, n, t) {
|
|
198
197
|
this.headers = r, this.status = n, this.statusText = t;
|
|
199
198
|
}
|
|
200
199
|
}
|
|
201
|
-
class
|
|
200
|
+
class E {
|
|
202
201
|
constructor({
|
|
203
202
|
headers: r,
|
|
204
203
|
status: n,
|
|
205
204
|
statusText: t,
|
|
206
205
|
error: s
|
|
207
206
|
}) {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
207
|
+
y(this, "headers");
|
|
208
|
+
y(this, "status");
|
|
209
|
+
y(this, "statusText");
|
|
210
|
+
y(this, "error");
|
|
212
211
|
this.headers = r, this.status = n, this.statusText = t, this.error = s;
|
|
213
212
|
}
|
|
214
213
|
}
|
|
215
|
-
const
|
|
216
|
-
var
|
|
217
|
-
class
|
|
214
|
+
const te = 200, ne = 300;
|
|
215
|
+
var L = /* @__PURE__ */ ((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))(L || {});
|
|
216
|
+
class me {
|
|
218
217
|
send(r) {
|
|
219
|
-
return new
|
|
218
|
+
return new g((n) => {
|
|
220
219
|
const t = new AbortController();
|
|
221
220
|
return this._send(r, n, t).then(() => {
|
|
222
221
|
}, (s) => {
|
|
223
|
-
n.error(new
|
|
222
|
+
n.error(new E({
|
|
224
223
|
error: s
|
|
225
224
|
}));
|
|
226
225
|
}), () => t.abort();
|
|
227
226
|
});
|
|
228
227
|
}
|
|
229
228
|
async _send(r, n, t) {
|
|
230
|
-
var h,
|
|
229
|
+
var h, u;
|
|
231
230
|
let s;
|
|
232
231
|
try {
|
|
233
232
|
const d = this._parseFetchParamsFromRequest(r);
|
|
@@ -236,53 +235,53 @@ class le {
|
|
|
236
235
|
...d
|
|
237
236
|
});
|
|
238
237
|
} catch (d) {
|
|
239
|
-
n.error(new
|
|
238
|
+
n.error(new E({
|
|
240
239
|
error: d,
|
|
241
240
|
status: (h = d.status) != null ? h : 0,
|
|
242
|
-
statusText: (
|
|
241
|
+
statusText: (u = d.statusText) != null ? u : "Unknown Error",
|
|
243
242
|
headers: d.headers
|
|
244
243
|
}));
|
|
245
244
|
return;
|
|
246
245
|
}
|
|
247
|
-
const
|
|
248
|
-
let
|
|
249
|
-
s.body && (
|
|
250
|
-
body:
|
|
251
|
-
headers:
|
|
252
|
-
status:
|
|
253
|
-
statusText:
|
|
254
|
-
})) : n.error(new
|
|
255
|
-
error:
|
|
256
|
-
status:
|
|
257
|
-
statusText:
|
|
258
|
-
headers:
|
|
246
|
+
const i = new x(s.headers), o = s.status, c = s.statusText;
|
|
247
|
+
let l = null;
|
|
248
|
+
s.body && (l = await this._readBody(r, s, n)), o >= L.Ok && o < L.MultipleChoices ? n.next(new F({
|
|
249
|
+
body: l,
|
|
250
|
+
headers: i,
|
|
251
|
+
status: o,
|
|
252
|
+
statusText: c
|
|
253
|
+
})) : n.error(new E({
|
|
254
|
+
error: l,
|
|
255
|
+
status: o,
|
|
256
|
+
statusText: c,
|
|
257
|
+
headers: i
|
|
259
258
|
})), n.complete();
|
|
260
259
|
}
|
|
261
260
|
async _readBody(r, n, t) {
|
|
262
|
-
var f,
|
|
263
|
-
const s = [],
|
|
264
|
-
let
|
|
265
|
-
const
|
|
266
|
-
let h,
|
|
261
|
+
var f, m;
|
|
262
|
+
const s = [], i = n.body.getReader(), o = n.headers.get("content-length");
|
|
263
|
+
let c = 0;
|
|
264
|
+
const l = (f = r.requestParams) == null ? void 0 : f.reportProgress, a = r.responseType;
|
|
265
|
+
let h, u;
|
|
267
266
|
for (; ; ) {
|
|
268
|
-
const { done:
|
|
269
|
-
if (
|
|
270
|
-
s.push(
|
|
271
|
-
|
|
272
|
-
|
|
267
|
+
const { done: w, value: p } = await i.read();
|
|
268
|
+
if (w) break;
|
|
269
|
+
s.push(p), c += p.length, l && a === "text" && (h = (h != null ? h : "") + (u != null ? u : u = new TextDecoder()).decode(p, { stream: !0 }), t.next(new ee(
|
|
270
|
+
o ? Number.parseInt(o, 10) : void 0,
|
|
271
|
+
c,
|
|
273
272
|
h
|
|
274
273
|
)));
|
|
275
274
|
}
|
|
276
|
-
const d =
|
|
275
|
+
const d = se(s, c);
|
|
277
276
|
try {
|
|
278
|
-
const
|
|
279
|
-
return
|
|
280
|
-
} catch (
|
|
281
|
-
return t.error(new
|
|
282
|
-
error:
|
|
277
|
+
const w = (m = n.headers.get("content-type")) != null ? m : "";
|
|
278
|
+
return ie(r, d, w);
|
|
279
|
+
} catch (w) {
|
|
280
|
+
return t.error(new E({
|
|
281
|
+
error: w,
|
|
283
282
|
status: n.status,
|
|
284
283
|
statusText: n.statusText,
|
|
285
|
-
headers: new
|
|
284
|
+
headers: new x(n.headers)
|
|
286
285
|
})), null;
|
|
287
286
|
}
|
|
288
287
|
}
|
|
@@ -295,18 +294,18 @@ class le {
|
|
|
295
294
|
};
|
|
296
295
|
}
|
|
297
296
|
}
|
|
298
|
-
function
|
|
297
|
+
function se(e, r) {
|
|
299
298
|
const n = new Uint8Array(r);
|
|
300
299
|
let t = 0;
|
|
301
300
|
for (const s of e)
|
|
302
301
|
n.set(s, t), t += s.length;
|
|
303
302
|
return n;
|
|
304
303
|
}
|
|
305
|
-
const
|
|
306
|
-
function
|
|
304
|
+
const oe = /^\)\]\}',?\n/;
|
|
305
|
+
function ie(e, r, n) {
|
|
307
306
|
switch (e.responseType) {
|
|
308
307
|
case "json":
|
|
309
|
-
const t = new TextDecoder().decode(r).replace(
|
|
308
|
+
const t = new TextDecoder().decode(r).replace(oe, "");
|
|
310
309
|
return t === "" ? null : JSON.parse(t);
|
|
311
310
|
case "text":
|
|
312
311
|
return new TextDecoder().decode(r);
|
|
@@ -318,87 +317,87 @@ function re(e, r, n) {
|
|
|
318
317
|
throw new Error(`[FetchHTTPImplementation]: unknown response type: ${e.responseType}.`);
|
|
319
318
|
}
|
|
320
319
|
}
|
|
321
|
-
class
|
|
320
|
+
class fe {
|
|
322
321
|
send(r) {
|
|
323
|
-
return new
|
|
322
|
+
return new g((n) => {
|
|
324
323
|
const t = new XMLHttpRequest();
|
|
325
|
-
t.open(r.method, r.getUrlWithParams()), r.withCredentials && (t.withCredentials = !0), r.headers.forEach((
|
|
324
|
+
t.open(r.method, r.getUrlWithParams()), r.withCredentials && (t.withCredentials = !0), r.headers.forEach((l, a) => t.setRequestHeader(l, a.join(","))), r.headers.has("Accept") || t.setRequestHeader("Accept", "application/json, text/plain, */*"), r.headers.has("Content-Type") || t.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
|
|
326
325
|
const s = () => {
|
|
327
|
-
const
|
|
328
|
-
return new
|
|
329
|
-
},
|
|
330
|
-
const { headers:
|
|
326
|
+
const l = t.statusText || "OK", a = new x(t.getAllResponseHeaders());
|
|
327
|
+
return new re(a, t.status, l);
|
|
328
|
+
}, i = () => {
|
|
329
|
+
const { headers: l, statusText: a, status: h } = s(), { responseType: u } = r;
|
|
331
330
|
let d = null, f = null;
|
|
332
|
-
h !==
|
|
333
|
-
let
|
|
334
|
-
if (
|
|
335
|
-
const
|
|
331
|
+
h !== L.NoContent && (d = typeof t.response > "u" ? t.responseText : t.response);
|
|
332
|
+
let m = h >= te && h < ne;
|
|
333
|
+
if (u === "json" && typeof d == "string") {
|
|
334
|
+
const w = d;
|
|
336
335
|
try {
|
|
337
336
|
d = d ? JSON.parse(d) : null;
|
|
338
|
-
} catch (
|
|
339
|
-
|
|
337
|
+
} catch (p) {
|
|
338
|
+
m = !1, d = w, f = p;
|
|
340
339
|
}
|
|
341
340
|
}
|
|
342
|
-
|
|
343
|
-
new
|
|
341
|
+
m ? n.next(
|
|
342
|
+
new F({
|
|
344
343
|
body: d,
|
|
345
|
-
headers:
|
|
344
|
+
headers: l,
|
|
346
345
|
status: h,
|
|
347
|
-
statusText:
|
|
346
|
+
statusText: a
|
|
348
347
|
})
|
|
349
348
|
) : n.error(
|
|
350
|
-
new
|
|
349
|
+
new E({
|
|
351
350
|
error: f,
|
|
352
|
-
headers:
|
|
351
|
+
headers: l,
|
|
353
352
|
status: h,
|
|
354
|
-
statusText:
|
|
353
|
+
statusText: a
|
|
355
354
|
})
|
|
356
355
|
);
|
|
357
|
-
},
|
|
358
|
-
const
|
|
359
|
-
error:
|
|
356
|
+
}, o = (l) => {
|
|
357
|
+
const a = new E({
|
|
358
|
+
error: l,
|
|
360
359
|
status: t.status || 0,
|
|
361
360
|
statusText: t.statusText || "Unknown Error",
|
|
362
361
|
headers: s().headers
|
|
363
362
|
});
|
|
364
|
-
n.error(
|
|
363
|
+
n.error(a);
|
|
365
364
|
};
|
|
366
|
-
t.addEventListener("load",
|
|
367
|
-
const
|
|
368
|
-
return t.send(
|
|
369
|
-
t.readyState !== t.DONE && t.abort(), t.removeEventListener("load",
|
|
365
|
+
t.addEventListener("load", i), t.addEventListener("error", o), t.addEventListener("abort", o), t.addEventListener("timeout", o);
|
|
366
|
+
const c = r.getBody();
|
|
367
|
+
return t.send(c), () => {
|
|
368
|
+
t.readyState !== t.DONE && t.abort(), t.removeEventListener("load", i), t.removeEventListener("error", o), t.removeEventListener("abort", o), t.removeEventListener("timeout", o);
|
|
370
369
|
};
|
|
371
370
|
});
|
|
372
371
|
}
|
|
373
372
|
}
|
|
374
|
-
const
|
|
375
|
-
class
|
|
373
|
+
const we = j("univer.socket");
|
|
374
|
+
class ge extends B {
|
|
376
375
|
createSocket(r) {
|
|
377
376
|
try {
|
|
378
|
-
const n = new WebSocket(r), t = new
|
|
377
|
+
const n = new WebSocket(r), t = new X();
|
|
379
378
|
return {
|
|
380
379
|
URL: r,
|
|
381
|
-
close: (
|
|
382
|
-
n.close(
|
|
380
|
+
close: (i, o) => {
|
|
381
|
+
n.close(i, o), t.dispose();
|
|
383
382
|
},
|
|
384
|
-
send: (
|
|
385
|
-
n.send(
|
|
383
|
+
send: (i) => {
|
|
384
|
+
n.send(i);
|
|
386
385
|
},
|
|
387
|
-
open$: new
|
|
388
|
-
const
|
|
389
|
-
n.addEventListener("open",
|
|
386
|
+
open$: new g((i) => {
|
|
387
|
+
const o = (c) => i.next(c);
|
|
388
|
+
n.addEventListener("open", o), t.add(_(() => n.removeEventListener("open", o)));
|
|
390
389
|
}).pipe(v()),
|
|
391
|
-
close$: new
|
|
392
|
-
const
|
|
393
|
-
n.addEventListener("close",
|
|
390
|
+
close$: new g((i) => {
|
|
391
|
+
const o = (c) => i.next(c);
|
|
392
|
+
n.addEventListener("close", o), t.add(_(() => n.removeEventListener("close", o)));
|
|
394
393
|
}).pipe(v()),
|
|
395
|
-
error$: new
|
|
396
|
-
const
|
|
397
|
-
n.addEventListener("error",
|
|
394
|
+
error$: new g((i) => {
|
|
395
|
+
const o = (c) => i.next(c);
|
|
396
|
+
n.addEventListener("error", o), t.add(_(() => n.removeEventListener("error", o)));
|
|
398
397
|
}).pipe(v()),
|
|
399
|
-
message$: new
|
|
400
|
-
const
|
|
401
|
-
n.addEventListener("message",
|
|
398
|
+
message$: new g((i) => {
|
|
399
|
+
const o = (c) => i.next(c);
|
|
400
|
+
n.addEventListener("message", o), t.add(_(() => n.removeEventListener("message", o)));
|
|
402
401
|
}).pipe(v())
|
|
403
402
|
};
|
|
404
403
|
} catch (n) {
|
|
@@ -406,50 +405,107 @@ class pe extends q {
|
|
|
406
405
|
}
|
|
407
406
|
}
|
|
408
407
|
}
|
|
409
|
-
const
|
|
410
|
-
var s,
|
|
411
|
-
const n = (s = r == null ? void 0 : r.maxRetryAttempts) != null ? s :
|
|
412
|
-
return (
|
|
413
|
-
},
|
|
408
|
+
const ce = 3, ae = 1e3, Ee = (e, r) => {
|
|
409
|
+
var s, i;
|
|
410
|
+
const n = (s = r == null ? void 0 : r.maxRetryAttempts) != null ? s : ce, t = (i = r == null ? void 0 : r.delayInterval) != null ? i : ae;
|
|
411
|
+
return (o, c) => c(o).pipe(K({ delay: t, count: n }));
|
|
412
|
+
}, Re = (e) => {
|
|
414
413
|
const r = [], n = /* @__PURE__ */ new Set(), t = () => {
|
|
415
414
|
var s;
|
|
416
415
|
for (; n.size < ((s = e == null ? void 0 : e.maxParallel) != null ? s : 1) && r.length > 0; ) {
|
|
417
|
-
const
|
|
418
|
-
n.add(
|
|
416
|
+
const i = r.shift();
|
|
417
|
+
n.add(i), i();
|
|
419
418
|
}
|
|
420
419
|
};
|
|
421
|
-
return (s,
|
|
422
|
-
const
|
|
423
|
-
next: (
|
|
424
|
-
error: (
|
|
425
|
-
complete: () =>
|
|
426
|
-
}),
|
|
427
|
-
n.delete(
|
|
420
|
+
return (s, i) => new g((o) => {
|
|
421
|
+
const c = () => i(s).subscribe({
|
|
422
|
+
next: (a) => o.next(a),
|
|
423
|
+
error: (a) => o.next(a),
|
|
424
|
+
complete: () => o.complete()
|
|
425
|
+
}), l = () => {
|
|
426
|
+
n.delete(c), $(r, c), t();
|
|
428
427
|
};
|
|
429
|
-
return r.push(
|
|
428
|
+
return r.push(c), t(), l;
|
|
430
429
|
});
|
|
431
|
-
},
|
|
430
|
+
}, _e = (e) => {
|
|
432
431
|
const { errorStatusCodes: r, onAuthError: n } = e;
|
|
433
|
-
return (s,
|
|
434
|
-
|
|
432
|
+
return (s, i) => i(s).pipe(
|
|
433
|
+
J((o) => (o instanceof E && r.some((c) => c === o.status) && n(), Y(() => o)))
|
|
435
434
|
);
|
|
435
|
+
}, le = (e = 300) => {
|
|
436
|
+
let n = () => {
|
|
437
|
+
};
|
|
438
|
+
return (t) => new Promise((s) => {
|
|
439
|
+
n();
|
|
440
|
+
const i = setTimeout(() => {
|
|
441
|
+
s(!0);
|
|
442
|
+
}, e);
|
|
443
|
+
n = () => {
|
|
444
|
+
clearTimeout(i), s(!1);
|
|
445
|
+
};
|
|
446
|
+
});
|
|
447
|
+
}, he = () => (e, r) => r.map((n) => ({ config: n, result: e })), xe = (e, r = {}) => {
|
|
448
|
+
const { isMatch: n, getParamsFromRequest: t, mergeParamsToRequest: s } = e, { fetchCheck: i = le(300), distributeResult: o = he() } = r, c = [], l = (a) => a.map((h) => h.config);
|
|
449
|
+
return (a, h) => n(a) ? new g((u) => {
|
|
450
|
+
const d = t(a);
|
|
451
|
+
c.push({
|
|
452
|
+
next: (m) => u.next(m),
|
|
453
|
+
error: (m) => u.error(m),
|
|
454
|
+
config: d
|
|
455
|
+
});
|
|
456
|
+
const f = l(c);
|
|
457
|
+
i(a).then((m) => {
|
|
458
|
+
if (m) {
|
|
459
|
+
const w = [];
|
|
460
|
+
f.forEach((p) => {
|
|
461
|
+
const b = c.findIndex((R) => R.config === p);
|
|
462
|
+
if (b >= 0) {
|
|
463
|
+
const [R] = c.splice(b, 1);
|
|
464
|
+
w.push(R);
|
|
465
|
+
}
|
|
466
|
+
}), h(s(f, a)).subscribe({
|
|
467
|
+
next: (p) => {
|
|
468
|
+
if (p.type === G.Response) {
|
|
469
|
+
const b = p.body, R = o(b, f);
|
|
470
|
+
w.forEach((I) => {
|
|
471
|
+
const N = R.find((A) => A.config === I.config);
|
|
472
|
+
if (N) {
|
|
473
|
+
const A = new F({
|
|
474
|
+
body: N.result,
|
|
475
|
+
headers: p.headers,
|
|
476
|
+
status: p.status,
|
|
477
|
+
statusText: p.statusText
|
|
478
|
+
});
|
|
479
|
+
I.next(A);
|
|
480
|
+
} else
|
|
481
|
+
I.error("batch error");
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
},
|
|
485
|
+
complete: () => u.complete(),
|
|
486
|
+
error: (p) => u.error(p)
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
}) : h(a);
|
|
436
491
|
};
|
|
437
492
|
export {
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
493
|
+
_e as AuthInterceptorFactory,
|
|
494
|
+
me as FetchHTTPImplementation,
|
|
495
|
+
G as HTTPEventType,
|
|
496
|
+
x as HTTPHeaders,
|
|
497
|
+
ee as HTTPProgress,
|
|
498
|
+
D as HTTPRequest,
|
|
499
|
+
F as HTTPResponse,
|
|
500
|
+
E as HTTPResponseError,
|
|
501
|
+
O as HTTPService,
|
|
502
|
+
L as HTTPStatusCode,
|
|
503
|
+
Q as IHTTPImplementation,
|
|
504
|
+
we as ISocketService,
|
|
505
|
+
xe as MergeInterceptorFactory,
|
|
506
|
+
re as ResponseHeader,
|
|
507
|
+
Ee as RetryInterceptorFactory,
|
|
508
|
+
Re as ThresholdInterceptorFactory,
|
|
509
|
+
ge as WebSocketService,
|
|
510
|
+
fe as XHRHTTPImplementation
|
|
455
511
|
};
|
package/lib/types/index.d.ts
CHANGED
|
@@ -27,3 +27,4 @@ export type { HTTPHandlerFn, HTTPInterceptorFn, HTTPInterceptorFnFactory } from
|
|
|
27
27
|
export { RetryInterceptorFactory, type IRetryInterceptorFactoryParams } from './services/http/interceptors/retry-interceptor';
|
|
28
28
|
export { ThresholdInterceptorFactory } from './services/http/interceptors/threshold-interceptor';
|
|
29
29
|
export { AuthInterceptorFactory, type IAuthInterceptorParams } from './services/http/interceptors/auth-interceptor';
|
|
30
|
+
export { MergeInterceptorFactory } from './services/http/interceptors/merge-interceptor';
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
|
-
import { Disposable } from '@univerjs/core';
|
|
3
|
-
import { Injector } from '@wendellhu/redi';
|
|
2
|
+
import { Disposable, Injector } from '@univerjs/core';
|
|
4
3
|
import { IHTTPImplementation } from '../implementations/implementation';
|
|
5
4
|
import { HTTPRequest } from '../request';
|
|
6
5
|
import { HTTPEvent, HTTPResponse, HTTPResponseError } from '../response';
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Disposable } from '@univerjs/core';
|
|
2
|
-
import { IDisposable } from '@wendellhu/redi';
|
|
1
|
+
import { IDisposable, Disposable } from '@univerjs/core';
|
|
3
2
|
import { Observable } from 'rxjs';
|
|
4
3
|
import { HTTPResponseType } from './http';
|
|
5
4
|
import { IHTTPImplementation } from './implementations/implementation';
|
|
@@ -12,4 +12,4 @@ export interface IHTTPImplementation {
|
|
|
12
12
|
*/
|
|
13
13
|
send(request: HTTPRequest): Observable<HTTPEvent<any>>;
|
|
14
14
|
}
|
|
15
|
-
export declare const IHTTPImplementation: import('@
|
|
15
|
+
export declare const IHTTPImplementation: import('@univerjs/core').IdentifierDecorator<IHTTPImplementation>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023-present DreamNum Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { HTTPRequest } from '../request';
|
|
3
|
+
import { HTTPHandlerFn } from '../interceptor';
|
|
4
|
+
|
|
5
|
+
export declare const MergeInterceptorFactory: <T, C>(config: {
|
|
6
|
+
/**
|
|
7
|
+
* Filter requests that need to be merged
|
|
8
|
+
*/
|
|
9
|
+
isMatch: (requestConfig: HTTPRequest) => boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Pre-process request parameters, the return value will be used as input parameters for subsequent operations
|
|
12
|
+
* The result is used as an index key
|
|
13
|
+
*/
|
|
14
|
+
getParamsFromRequest: (requestConfig: HTTPRequest) => T;
|
|
15
|
+
/**
|
|
16
|
+
* The request parameters are merged to initiate the request
|
|
17
|
+
*/
|
|
18
|
+
mergeParamsToRequest: (list: T[], requestConfig: HTTPRequest) => HTTPRequest;
|
|
19
|
+
}, options?: {
|
|
20
|
+
/**
|
|
21
|
+
* Determine when to initiate a request
|
|
22
|
+
* By default, requests up to 300ms are automatically aggregated
|
|
23
|
+
*/
|
|
24
|
+
fetchCheck?: (currentConfig: HTTPRequest) => Promise<boolean>;
|
|
25
|
+
/**
|
|
26
|
+
* The result of the request is dispatched based on the request parameters.
|
|
27
|
+
* By default each request gets the full result of the batch request
|
|
28
|
+
*/
|
|
29
|
+
distributeResult?: (result: C, list: T[]) => {
|
|
30
|
+
config: T;
|
|
31
|
+
result: C;
|
|
32
|
+
}[];
|
|
33
|
+
}) => (requestConfig: HTTPRequest, next: HTTPHandlerFn) => Observable<import('../response').HTTPEvent<unknown>>;
|
|
@@ -21,7 +21,7 @@ export declare class HTTPRequest {
|
|
|
21
21
|
readonly requestParams?: IHTTPRequestParams | undefined;
|
|
22
22
|
get headers(): HTTPHeaders;
|
|
23
23
|
get withCredentials(): boolean;
|
|
24
|
-
get responseType():
|
|
24
|
+
get responseType(): HTTPResponseType;
|
|
25
25
|
readonly uid: number;
|
|
26
26
|
constructor(method: HTTPRequestMethod, url: string, requestParams?: IHTTPRequestParams | undefined);
|
|
27
27
|
getUrlWithParams(): string;
|
|
@@ -5,7 +5,7 @@ export type SocketBodyType = string | ArrayBufferLike | Blob | ArrayBufferView;
|
|
|
5
5
|
/**
|
|
6
6
|
* This service is responsible for establishing bidi-directional connection to a remote server.
|
|
7
7
|
*/
|
|
8
|
-
export declare const ISocketService: import('@
|
|
8
|
+
export declare const ISocketService: import('@univerjs/core').IdentifierDecorator<ISocketService>;
|
|
9
9
|
export interface ISocketService {
|
|
10
10
|
createSocket(url: string): Nullable<ISocket>;
|
|
11
11
|
}
|
package/lib/umd/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(
|
|
2
|
-
`).forEach(t=>{const[r,s]=t.split(":");r&&s&&this._setHeader(r,s)})}_handleHeadersConstructorProps(n){Object.entries(n).forEach(([t,r])=>this._setHeader(t,r))}_handleHeaders(n){n.forEach((t,r)=>this._setHeader(r,t))}}const N=q.createIdentifier("network.http-implementation");class U{constructor(n){this.params=n}toString(){return this.params?Object.keys(this.params).map(n=>`${n}=${this.params[n]}`).join("&"):""}}let D=0;class L{constructor(n,t,r){m(this,"uid",D++);this.method=n,this.url=t,this.requestParams=r}get headers(){return this.requestParams.headers}get withCredentials(){return this.requestParams.withCredentials}get responseType(){return this.requestParams.responseType}getUrlWithParams(){var t,r;const n=(r=(t=this.requestParams)==null?void 0:t.params)==null?void 0:r.toString();return n?`${this.url}${this.url.includes("?")?"&":"?"}${n}`:this.url}getBody(){var r,s;const n=(r=this.headers.get("Content-Type"))!=null?r:F,t=(s=this.requestParams)==null?void 0:s.body;return n===F&&t&&typeof t=="object"?JSON.stringify(t):t?`${t}`:null}getHeadersInit(){return this.headers.toHeadersInit()}}var M=Object.defineProperty,j=Object.getOwnPropertyDescriptor,B=(e,n,t,r)=>{for(var s=r>1?void 0:r?j(n,t):n,a=e.length-1,i;a>=0;a--)(i=e[a])&&(s=(r?i(n,t,s):i(s))||s);return r&&s&&M(n,t,s),s},$=(e,n)=>(t,r)=>n(t,r,e);o.HTTPService=class extends l.Disposable{constructor(t){super();m(this,"_interceptors",[]);m(this,"_pipe");this._http=t}registerHTTPInterceptor(t){if(this._interceptors.indexOf(t)!==-1)throw new Error("[HTTPService]: The interceptor has already been registered!");return this._interceptors.push(t),this._interceptors=this._interceptors.sort((r,s)=>{var a,i;return((a=r.priority)!=null?a:0)-((i=s.priority)!=null?i:0)}),this._pipe=null,l.toDisposable(()=>l.remove(this._interceptors,t))}get(t,r){return this._request("GET",t,r)}post(t,r){return this._request("POST",t,r)}put(t,r){return this._request("PUT",t,r)}delete(t,r){return this._request("DELETE",t,r)}patch(t,r){return this._request("PATCH",t,r)}getSSE(t,r,s){var h,d;const a=new E(s==null?void 0:s.headers),i=new U(s==null?void 0:s.params),c=new L(t,r,{headers:a,params:i,withCredentials:(h=s==null?void 0:s.withCredentials)!=null?h:!1,reportProgress:!0,responseType:(d=s==null?void 0:s.responseType)!=null?d:"json",body:["GET","DELETE"].includes(t)||s==null?void 0:s.body});return y.of(c).pipe(g.concatMap(p=>this._runInterceptorsAndImplementation(p)))}async _request(t,r,s){var p,f;const a=new E(s==null?void 0:s.headers),i=new U(s==null?void 0:s.params),c=new L(t,r,{headers:a,params:i,withCredentials:(p=s==null?void 0:s.withCredentials)!=null?p:!1,responseType:(f=s==null?void 0:s.responseType)!=null?f:"json",body:["GET","DELETE"].includes(t)||s==null?void 0:s.body}),h=y.of(c).pipe(g.concatMap(u=>this._runInterceptorsAndImplementation(u)));return await y.firstValueFrom(h)}_runInterceptorsAndImplementation(t){return this._pipe||(this._pipe=this._interceptors.map(r=>r.interceptor).reduceRight((r,s)=>G(r,s),(r,s)=>s(r))),this._pipe(t,r=>this._http.send(r))}},o.HTTPService=B([$(0,N)],o.HTTPService);function G(e,n){return(t,r)=>n(t,s=>e(s,r))}var x=(e=>(e[e.DownloadProgress=0]="DownloadProgress",e[e.Response=1]="Response",e))(x||{});class A{constructor({body:n,headers:t,status:r,statusText:s}){m(this,"type",1);m(this,"body");m(this,"headers");m(this,"status");m(this,"statusText");this.body=n,this.headers=t,this.status=r,this.statusText=s}}class k{constructor(n,t,r){m(this,"type",0);this.total=n,this.loaded=t,this.partialText=r}}class O{constructor(n,t,r){this.headers=n,this.status=t,this.statusText=r}}class w{constructor({headers:n,status:t,statusText:r,error:s}){m(this,"headers");m(this,"status");m(this,"statusText");m(this,"error");this.headers=n,this.status=t,this.statusText=r,this.error=s}}const V=200,W=300;var _=(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))(_||{});class X{send(n){return new y.Observable(t=>{const r=new AbortController;return this._send(n,t,r).then(()=>{},s=>{t.error(new w({error:s}))}),()=>r.abort()})}async _send(n,t,r){var p,f;let s;try{const u=this._parseFetchParamsFromRequest(n);s=await fetch(n.getUrlWithParams(),{signal:r.signal,...u})}catch(u){t.error(new w({error:u,status:(p=u.status)!=null?p:0,statusText:(f=u.statusText)!=null?f:"Unknown Error",headers:u.headers}));return}const a=new E(s.headers),i=s.status,c=s.statusText;let h=null;s.body&&(h=await this._readBody(n,s,t)),i>=_.Ok&&i<_.MultipleChoices?t.next(new A({body:h,headers:a,status:i,statusText:c})):t.error(new w({error:h,status:i,statusText:c,headers:a})),t.complete()}async _readBody(n,t,r){var R,I;const s=[],a=t.body.getReader(),i=t.headers.get("content-length");let c=0;const h=(R=n.requestParams)==null?void 0:R.reportProgress,d=n.responseType;let p,f;for(;;){const{done:v,value:b}=await a.read();if(v)break;s.push(b),c+=b.length,h&&d==="text"&&(p=(p!=null?p:"")+(f!=null?f:f=new TextDecoder).decode(b,{stream:!0}),r.next(new k(i?Number.parseInt(i,10):void 0,c,p)))}const u=z(s,c);try{const v=(I=t.headers.get("content-type"))!=null?I:"";return Y(n,u,v)}catch(v){return r.error(new w({error:v,status:t.status,statusText:t.statusText,headers:new E(t.headers)})),null}}_parseFetchParamsFromRequest(n){return{method:n.method,headers:n.getHeadersInit(),body:n.getBody(),credentials:n.withCredentials?"include":void 0}}}function z(e,n){const t=new Uint8Array(n);let r=0;for(const s of e)t.set(s,r),r+=s.length;return t}const J=/^\)\]\}',?\n/;function Y(e,n,t){switch(e.responseType){case"json":const r=new TextDecoder().decode(n).replace(J,"");return r===""?null:JSON.parse(r);case"text":return new TextDecoder().decode(n);case"blob":return new Blob([n],{type:t});case"arraybuffer":return n.buffer;default:throw new Error(`[FetchHTTPImplementation]: unknown response type: ${e.responseType}.`)}}class K{send(n){return new y.Observable(t=>{const r=new XMLHttpRequest;r.open(n.method,n.getUrlWithParams()),n.withCredentials&&(r.withCredentials=!0),n.headers.forEach((h,d)=>r.setRequestHeader(h,d.join(","))),n.headers.has("Accept")||r.setRequestHeader("Accept","application/json, text/plain, */*"),n.headers.has("Content-Type")||r.setRequestHeader("Content-Type","application/json;charset=UTF-8");const s=()=>{const h=r.statusText||"OK",d=new E(r.getAllResponseHeaders());return new O(d,r.status,h)},a=()=>{const{headers:h,statusText:d,status:p}=s(),{responseType:f}=n;let u=null,R=null;p!==_.NoContent&&(u=typeof r.response>"u"?r.responseText:r.response);let I=p>=V&&p<W;if(f==="json"&&typeof u=="string"){const v=u;try{u=u?JSON.parse(u):null}catch(b){I=!1,u=v,R=b}}I?t.next(new A({body:u,headers:h,status:p,statusText:d})):t.error(new w({error:R,headers:h,status:p,statusText:d}))},i=h=>{const d=new w({error:h,status:r.status||0,statusText:r.statusText||"Unknown Error",headers:s().headers});t.error(d)};r.addEventListener("load",a),r.addEventListener("error",i),r.addEventListener("abort",i),r.addEventListener("timeout",i);const c=n.getBody();return r.send(c),()=>{r.readyState!==r.DONE&&r.abort(),r.removeEventListener("load",a),r.removeEventListener("error",i),r.removeEventListener("abort",i),r.removeEventListener("timeout",i)}})}}const Q=q.createIdentifier("univer.socket");class Z extends l.Disposable{createSocket(n){try{const t=new WebSocket(n),r=new l.DisposableCollection;return{URL:n,close:(a,i)=>{t.close(a,i),r.dispose()},send:a=>{t.send(a)},open$:new y.Observable(a=>{const i=c=>a.next(c);t.addEventListener("open",i),r.add(l.toDisposable(()=>t.removeEventListener("open",i)))}).pipe(g.share()),close$:new y.Observable(a=>{const i=c=>a.next(c);t.addEventListener("close",i),r.add(l.toDisposable(()=>t.removeEventListener("close",i)))}).pipe(g.share()),error$:new y.Observable(a=>{const i=c=>a.next(c);t.addEventListener("error",i),r.add(l.toDisposable(()=>t.removeEventListener("error",i)))}).pipe(g.share()),message$:new y.Observable(a=>{const i=c=>a.next(c);t.addEventListener("message",i),r.add(l.toDisposable(()=>t.removeEventListener("message",i)))}).pipe(g.share())}}catch(t){return console.error(t),null}}}const P=3,H=1e3,S=(e,n)=>{var s,a;const t=(s=n==null?void 0:n.maxRetryAttempts)!=null?s:P,r=(a=n==null?void 0:n.delayInterval)!=null?a:H;return(i,c)=>c(i).pipe(g.retry({delay:r,count:t}))},C=e=>{const n=[],t=new Set,r=()=>{var s;for(;t.size<((s=e==null?void 0:e.maxParallel)!=null?s:1)&&n.length>0;){const a=n.shift();t.add(a),a()}};return(s,a)=>new y.Observable(i=>{const c=()=>a(s).subscribe({next:d=>i.next(d),error:d=>i.next(d),complete:()=>i.complete()}),h=()=>{t.delete(c),l.remove(n,c),r()};return n.push(c),r(),h})},T=e=>{const{errorStatusCodes:n,onAuthError:t}=e;return(s,a)=>a(s).pipe(y.catchError(i=>(i instanceof w&&n.some(c=>c===i.status)&&t(),y.throwError(()=>i))))};o.AuthInterceptorFactory=T,o.FetchHTTPImplementation=X,o.HTTPEventType=x,o.HTTPHeaders=E,o.HTTPProgress=k,o.HTTPRequest=L,o.HTTPResponse=A,o.HTTPResponseError=w,o.HTTPStatusCode=_,o.IHTTPImplementation=N,o.ISocketService=Q,o.ResponseHeader=O,o.RetryInterceptorFactory=S,o.ThresholdInterceptorFactory=C,o.WebSocketService=Z,o.XHRHTTPImplementation=K,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(a,h){typeof exports=="object"&&typeof module<"u"?h(exports,require("@univerjs/core"),require("rxjs"),require("rxjs/operators")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","rxjs","rxjs/operators"],h):(a=typeof globalThis<"u"?globalThis:a||self,h(a.UniverNetwork={},a.UniverCore,a.rxjs,a.rxjs.operators))})(this,function(a,h,y,E){"use strict";var ae=Object.defineProperty;var ce=(a,h,y)=>h in a?ae(a,h,{enumerable:!0,configurable:!0,writable:!0,value:y}):a[h]=y;var w=(a,h,y)=>ce(a,typeof h!="symbol"?h+"":h,y);const k="application/json";class _{constructor(n){w(this,"_headers",new Map);typeof n=="string"?this._handleHeadersString(n):n instanceof Headers?this._handleHeaders(n):n&&this._handleHeadersConstructorProps(n)}forEach(n){this._headers.forEach((r,t)=>n(t,r))}has(n){return!!this._headers.has(n.toLowerCase())}get(n){const r=n.toLowerCase();return this._headers.has(r)?this._headers.get(r):null}toHeadersInit(){var r,t;const n={};return this._headers.forEach((s,o)=>{n[o]=s.join(",")}),(r=n.accept)!=null||(n.accept="application/json, text/plain, */*"),(t=n["content-type"])!=null||(n["content-type"]="application/json;charset=UTF-8"),n}_setHeader(n,r){const t=n.toLowerCase();this._headers.has(t)?this._headers.get(t).push(r.toString()):this._headers.set(t,[r.toString()])}_handleHeadersString(n){n.split(`
|
|
2
|
+
`).forEach(r=>{const[t,s]=r.split(":");t&&s&&this._setHeader(t,s)})}_handleHeadersConstructorProps(n){Object.entries(n).forEach(([r,t])=>this._setHeader(r,t))}_handleHeaders(n){n.forEach((r,t)=>this._setHeader(t,r))}}const D=h.createIdentifier("network.http-implementation");class O{constructor(n){this.params=n}toString(){return this.params?Object.keys(this.params).map(n=>`${n}=${this.params[n]}`).join("&"):""}}let $=0;class x{constructor(n,r,t){w(this,"uid",$++);this.method=n,this.url=r,this.requestParams=t}get headers(){return this.requestParams.headers}get withCredentials(){return this.requestParams.withCredentials}get responseType(){return this.requestParams.responseType}getUrlWithParams(){var r,t;const n=(t=(r=this.requestParams)==null?void 0:r.params)==null?void 0:t.toString();return n?`${this.url}${this.url.includes("?")?"&":"?"}${n}`:this.url}getBody(){var t,s;const n=(t=this.headers.get("Content-Type"))!=null?t:k,r=(s=this.requestParams)==null?void 0:s.body;return n===k&&r&&typeof r=="object"?JSON.stringify(r):r?`${r}`:null}getHeadersInit(){return this.headers.toHeadersInit()}}var G=Object.defineProperty,V=Object.getOwnPropertyDescriptor,W=(e,n,r,t)=>{for(var s=t>1?void 0:t?V(n,r):n,o=e.length-1,i;o>=0;o--)(i=e[o])&&(s=(t?i(n,r,s):i(s))||s);return t&&s&&G(n,r,s),s},X=(e,n)=>(r,t)=>n(r,t,e);a.HTTPService=class extends h.Disposable{constructor(r){super();w(this,"_interceptors",[]);w(this,"_pipe");this._http=r}registerHTTPInterceptor(r){if(this._interceptors.indexOf(r)!==-1)throw new Error("[HTTPService]: The interceptor has already been registered!");return this._interceptors.push(r),this._interceptors=this._interceptors.sort((t,s)=>{var o,i;return((o=t.priority)!=null?o:0)-((i=s.priority)!=null?i:0)}),this._pipe=null,h.toDisposable(()=>h.remove(this._interceptors,r))}get(r,t){return this._request("GET",r,t)}post(r,t){return this._request("POST",r,t)}put(r,t){return this._request("PUT",r,t)}delete(r,t){return this._request("DELETE",r,t)}patch(r,t){return this._request("PATCH",r,t)}getSSE(r,t,s){var p,l;const o=new _(s==null?void 0:s.headers),i=new O(s==null?void 0:s.params),c=new x(r,t,{headers:o,params:i,withCredentials:(p=s==null?void 0:s.withCredentials)!=null?p:!1,reportProgress:!0,responseType:(l=s==null?void 0:s.responseType)!=null?l:"json",body:["GET","DELETE"].includes(r)||s==null?void 0:s.body});return y.of(c).pipe(E.concatMap(d=>this._runInterceptorsAndImplementation(d)))}async _request(r,t,s){var d,m;const o=new _(s==null?void 0:s.headers),i=new O(s==null?void 0:s.params),c=new x(r,t,{headers:o,params:i,withCredentials:(d=s==null?void 0:s.withCredentials)!=null?d:!1,responseType:(m=s==null?void 0:s.responseType)!=null?m:"json",body:["GET","DELETE"].includes(r)||s==null?void 0:s.body}),p=y.of(c).pipe(E.concatMap(u=>this._runInterceptorsAndImplementation(u)));return await y.firstValueFrom(p)}_runInterceptorsAndImplementation(r){return this._pipe||(this._pipe=this._interceptors.map(t=>t.interceptor).reduceRight((t,s)=>z(t,s),(t,s)=>s(t))),this._pipe(r,t=>this._http.send(t))}},a.HTTPService=W([X(0,D)],a.HTTPService);function z(e,n){return(r,t)=>n(r,s=>e(s,t))}var N=(e=>(e[e.DownloadProgress=0]="DownloadProgress",e[e.Response=1]="Response",e))(N||{});class F{constructor({body:n,headers:r,status:t,statusText:s}){w(this,"type",1);w(this,"body");w(this,"headers");w(this,"status");w(this,"statusText");this.body=n,this.headers=r,this.status=t,this.statusText=s}}class M{constructor(n,r,t){w(this,"type",0);this.total=n,this.loaded=r,this.partialText=t}}class j{constructor(n,r,t){this.headers=n,this.status=r,this.statusText=t}}class R{constructor({headers:n,status:r,statusText:t,error:s}){w(this,"headers");w(this,"status");w(this,"statusText");w(this,"error");this.headers=n,this.status=r,this.statusText=t,this.error=s}}const J=200,Y=300;var I=(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))(I||{});class K{send(n){return new y.Observable(r=>{const t=new AbortController;return this._send(n,r,t).then(()=>{},s=>{r.error(new R({error:s}))}),()=>t.abort()})}async _send(n,r,t){var d,m;let s;try{const u=this._parseFetchParamsFromRequest(n);s=await fetch(n.getUrlWithParams(),{signal:t.signal,...u})}catch(u){r.error(new R({error:u,status:(d=u.status)!=null?d:0,statusText:(m=u.statusText)!=null?m:"Unknown Error",headers:u.headers}));return}const o=new _(s.headers),i=s.status,c=s.statusText;let p=null;s.body&&(p=await this._readBody(n,s,r)),i>=I.Ok&&i<I.MultipleChoices?r.next(new F({body:p,headers:o,status:i,statusText:c})):r.error(new R({error:p,status:i,statusText:c,headers:o})),r.complete()}async _readBody(n,r,t){var b,g;const s=[],o=r.body.getReader(),i=r.headers.get("content-length");let c=0;const p=(b=n.requestParams)==null?void 0:b.reportProgress,l=n.responseType;let d,m;for(;;){const{done:v,value:f}=await o.read();if(v)break;s.push(f),c+=f.length,p&&l==="text"&&(d=(d!=null?d:"")+(m!=null?m:m=new TextDecoder).decode(f,{stream:!0}),t.next(new M(i?Number.parseInt(i,10):void 0,c,d)))}const u=Q(s,c);try{const v=(g=r.headers.get("content-type"))!=null?g:"";return P(n,u,v)}catch(v){return t.error(new R({error:v,status:r.status,statusText:r.statusText,headers:new _(r.headers)})),null}}_parseFetchParamsFromRequest(n){return{method:n.method,headers:n.getHeadersInit(),body:n.getBody(),credentials:n.withCredentials?"include":void 0}}}function Q(e,n){const r=new Uint8Array(n);let t=0;for(const s of e)r.set(s,t),t+=s.length;return r}const Z=/^\)\]\}',?\n/;function P(e,n,r){switch(e.responseType){case"json":const t=new TextDecoder().decode(n).replace(Z,"");return t===""?null:JSON.parse(t);case"text":return new TextDecoder().decode(n);case"blob":return new Blob([n],{type:r});case"arraybuffer":return n.buffer;default:throw new Error(`[FetchHTTPImplementation]: unknown response type: ${e.responseType}.`)}}class H{send(n){return new y.Observable(r=>{const t=new XMLHttpRequest;t.open(n.method,n.getUrlWithParams()),n.withCredentials&&(t.withCredentials=!0),n.headers.forEach((p,l)=>t.setRequestHeader(p,l.join(","))),n.headers.has("Accept")||t.setRequestHeader("Accept","application/json, text/plain, */*"),n.headers.has("Content-Type")||t.setRequestHeader("Content-Type","application/json;charset=UTF-8");const s=()=>{const p=t.statusText||"OK",l=new _(t.getAllResponseHeaders());return new j(l,t.status,p)},o=()=>{const{headers:p,statusText:l,status:d}=s(),{responseType:m}=n;let u=null,b=null;d!==I.NoContent&&(u=typeof t.response>"u"?t.responseText:t.response);let g=d>=J&&d<Y;if(m==="json"&&typeof u=="string"){const v=u;try{u=u?JSON.parse(u):null}catch(f){g=!1,u=v,b=f}}g?r.next(new F({body:u,headers:p,status:d,statusText:l})):r.error(new R({error:b,headers:p,status:d,statusText:l}))},i=p=>{const l=new R({error:p,status:t.status||0,statusText:t.statusText||"Unknown Error",headers:s().headers});r.error(l)};t.addEventListener("load",o),t.addEventListener("error",i),t.addEventListener("abort",i),t.addEventListener("timeout",i);const c=n.getBody();return t.send(c),()=>{t.readyState!==t.DONE&&t.abort(),t.removeEventListener("load",o),t.removeEventListener("error",i),t.removeEventListener("abort",i),t.removeEventListener("timeout",i)}})}}const S=h.createIdentifier("univer.socket");class C extends h.Disposable{createSocket(n){try{const r=new WebSocket(n),t=new h.DisposableCollection;return{URL:n,close:(o,i)=>{r.close(o,i),t.dispose()},send:o=>{r.send(o)},open$:new y.Observable(o=>{const i=c=>o.next(c);r.addEventListener("open",i),t.add(h.toDisposable(()=>r.removeEventListener("open",i)))}).pipe(E.share()),close$:new y.Observable(o=>{const i=c=>o.next(c);r.addEventListener("close",i),t.add(h.toDisposable(()=>r.removeEventListener("close",i)))}).pipe(E.share()),error$:new y.Observable(o=>{const i=c=>o.next(c);r.addEventListener("error",i),t.add(h.toDisposable(()=>r.removeEventListener("error",i)))}).pipe(E.share()),message$:new y.Observable(o=>{const i=c=>o.next(c);r.addEventListener("message",i),t.add(h.toDisposable(()=>r.removeEventListener("message",i)))}).pipe(E.share())}}catch(r){return console.error(r),null}}}const T=3,ee=1e3,re=(e,n)=>{var s,o;const r=(s=n==null?void 0:n.maxRetryAttempts)!=null?s:T,t=(o=n==null?void 0:n.delayInterval)!=null?o:ee;return(i,c)=>c(i).pipe(E.retry({delay:t,count:r}))},te=e=>{const n=[],r=new Set,t=()=>{var s;for(;r.size<((s=e==null?void 0:e.maxParallel)!=null?s:1)&&n.length>0;){const o=n.shift();r.add(o),o()}};return(s,o)=>new y.Observable(i=>{const c=()=>o(s).subscribe({next:l=>i.next(l),error:l=>i.next(l),complete:()=>i.complete()}),p=()=>{r.delete(c),h.remove(n,c),t()};return n.push(c),t(),p})},ne=e=>{const{errorStatusCodes:n,onAuthError:r}=e;return(s,o)=>o(s).pipe(y.catchError(i=>(i instanceof R&&n.some(c=>c===i.status)&&r(),y.throwError(()=>i))))},se=(e=300)=>{let r=()=>{};return t=>new Promise(s=>{r();const o=setTimeout(()=>{s(!0)},e);r=()=>{clearTimeout(o),s(!1)}})},ie=()=>(e,n)=>n.map(r=>({config:r,result:e})),oe=(e,n={})=>{const{isMatch:r,getParamsFromRequest:t,mergeParamsToRequest:s}=e,{fetchCheck:o=se(300),distributeResult:i=ie()}=n,c=[],p=l=>l.map(d=>d.config);return(l,d)=>r(l)?new y.Observable(m=>{const u=t(l);c.push({next:g=>m.next(g),error:g=>m.error(g),config:u});const b=p(c);o(l).then(g=>{if(g){const v=[];b.forEach(f=>{const A=c.findIndex(L=>L.config===f);if(A>=0){const[L]=c.splice(A,1);v.push(L)}}),d(s(b,l)).subscribe({next:f=>{if(f.type===N.Response){const A=f.body,L=i(A,b);v.forEach(q=>{const B=L.find(U=>U.config===q.config);if(B){const U=new F({body:B.result,headers:f.headers,status:f.status,statusText:f.statusText});q.next(U)}else q.error("batch error")})}},complete:()=>m.complete(),error:f=>m.error(f)})}})}):d(l)};a.AuthInterceptorFactory=ne,a.FetchHTTPImplementation=K,a.HTTPEventType=N,a.HTTPHeaders=_,a.HTTPProgress=M,a.HTTPRequest=x,a.HTTPResponse=F,a.HTTPResponseError=R,a.HTTPStatusCode=I,a.IHTTPImplementation=D,a.ISocketService=S,a.MergeInterceptorFactory=oe,a.ResponseHeader=j,a.RetryInterceptorFactory=re,a.ThresholdInterceptorFactory=te,a.WebSocketService=C,a.XHRHTTPImplementation=H,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@univerjs/network",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4-alpha.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"author": "DreamNum <developer@univer.ai>",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -45,18 +45,16 @@
|
|
|
45
45
|
"lib"
|
|
46
46
|
],
|
|
47
47
|
"peerDependencies": {
|
|
48
|
-
"@wendellhu/redi": "0.15.5",
|
|
49
48
|
"rxjs": ">=7.0.0",
|
|
50
|
-
"@univerjs/core": "0.2.
|
|
49
|
+
"@univerjs/core": "0.2.4-alpha.0"
|
|
51
50
|
},
|
|
52
51
|
"devDependencies": {
|
|
53
|
-
"@wendellhu/redi": "0.15.5",
|
|
54
52
|
"rxjs": "^7.8.1",
|
|
55
53
|
"typescript": "^5.5.3",
|
|
56
|
-
"vite": "^5.3.
|
|
57
|
-
"vitest": "^
|
|
58
|
-
"@univerjs/core": "0.2.
|
|
59
|
-
"@univerjs/shared": "0.2.
|
|
54
|
+
"vite": "^5.3.4",
|
|
55
|
+
"vitest": "^2.0.3",
|
|
56
|
+
"@univerjs/core": "0.2.4-alpha.0",
|
|
57
|
+
"@univerjs/shared": "0.2.4-alpha.0"
|
|
60
58
|
},
|
|
61
59
|
"univerSpace": {
|
|
62
60
|
".": {
|