@jetlinks-web/core 2.2.10 → 2.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +192 -0
- package/dist/index.mjs +4 -0
- package/package.json +8 -6
- package/src/axios.ts +19 -5
- package/src/fetch.ts +1 -1
- package/src/axios.ts~ +0 -419
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
|
|
2
|
+
import { AxiosResponseRewrite } from '@jetlinks-web/types';
|
|
3
|
+
import { Observable } from 'rxjs';
|
|
4
|
+
|
|
5
|
+
interface Options {
|
|
6
|
+
tokenExpiration: (err?: AxiosError<any>, response?: AxiosResponse) => void;
|
|
7
|
+
handleReconnect: () => Promise<any>;
|
|
8
|
+
filter_url?: Array<string>;
|
|
9
|
+
code?: number;
|
|
10
|
+
codeKey?: string;
|
|
11
|
+
timeout?: number;
|
|
12
|
+
handleRequest?: () => void;
|
|
13
|
+
/**
|
|
14
|
+
* 用以获取localstorage中的lang
|
|
15
|
+
*/
|
|
16
|
+
langKey?: string;
|
|
17
|
+
/**
|
|
18
|
+
* response处理函数
|
|
19
|
+
* @param response AxiosResponse实例
|
|
20
|
+
*/
|
|
21
|
+
handleResponse?: (response: AxiosResponse) => void;
|
|
22
|
+
/**
|
|
23
|
+
* 错误处理函数
|
|
24
|
+
* @param msg 错误消息
|
|
25
|
+
* @param status 错误code
|
|
26
|
+
* @param error 错误实例
|
|
27
|
+
*/
|
|
28
|
+
handleError?: (msg: string, status: string | number, error: AxiosError<any>) => void;
|
|
29
|
+
requestOptions?: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Record<string, any>;
|
|
30
|
+
isCreateTokenRefresh?: boolean;
|
|
31
|
+
}
|
|
32
|
+
interface RequestOptions {
|
|
33
|
+
url?: string;
|
|
34
|
+
method?: string;
|
|
35
|
+
params?: any;
|
|
36
|
+
data?: any;
|
|
37
|
+
[key: string]: any;
|
|
38
|
+
}
|
|
39
|
+
declare const abortAllRequests: () => void;
|
|
40
|
+
declare const crateAxios: (options: Options) => void;
|
|
41
|
+
declare const post: <T = any>(url: string, data?: any, ext?: any) => Promise<AxiosResponseRewrite<T>>;
|
|
42
|
+
declare const get: <T = any>(url: string, params?: any, ext?: any) => Promise<AxiosResponseRewrite<T>>;
|
|
43
|
+
declare const put: <T = any>(url: string, data?: any, ext?: any) => Promise<AxiosResponseRewrite<T>>;
|
|
44
|
+
declare const patch: <T = any>(url: string, data?: any, ext?: any) => Promise<AxiosResponseRewrite<T>>;
|
|
45
|
+
declare const remove: <T = any>(url: string, params?: any, ext?: any) => Promise<AxiosResponseRewrite<T>>;
|
|
46
|
+
declare const getStream: (url: string, params?: any, ext?: any) => Promise<AxiosResponseRewrite<any>>;
|
|
47
|
+
declare const postStream: (url: string, data: any, ext?: any) => Promise<AxiosResponseRewrite<any>>;
|
|
48
|
+
declare const request: {
|
|
49
|
+
post: <T = any>(url: string, data?: any, ext?: any) => Promise<AxiosResponseRewrite<T>>;
|
|
50
|
+
get: <T = any>(url: string, params?: any, ext?: any) => Promise<AxiosResponseRewrite<T>>;
|
|
51
|
+
put: <T = any>(url: string, data?: any, ext?: any) => Promise<AxiosResponseRewrite<T>>;
|
|
52
|
+
patch: <T = any>(url: string, data?: any, ext?: any) => Promise<AxiosResponseRewrite<T>>;
|
|
53
|
+
remove: <T = any>(url: string, params?: any, ext?: any) => Promise<AxiosResponseRewrite<T>>;
|
|
54
|
+
getStream: (url: string, params?: any, ext?: any) => Promise<AxiosResponseRewrite<any>>;
|
|
55
|
+
postStream: (url: string, data: any, ext?: any) => Promise<AxiosResponseRewrite<any>>;
|
|
56
|
+
};
|
|
57
|
+
declare class Request {
|
|
58
|
+
modulePath: string;
|
|
59
|
+
constructor(modulePath: string);
|
|
60
|
+
/**
|
|
61
|
+
* 分页查询
|
|
62
|
+
* @param {object} data 查询参数
|
|
63
|
+
* @param {object} options 请求配置
|
|
64
|
+
* @returns {Promise<AxiosResponse<any>>} 分页查询结果
|
|
65
|
+
*/
|
|
66
|
+
page(data?: any, options?: RequestOptions): any;
|
|
67
|
+
/**
|
|
68
|
+
* 不分页查询
|
|
69
|
+
* @param {object} data 查询参数
|
|
70
|
+
* @param {object} options 请求配置
|
|
71
|
+
* @returns {Promise<AxiosResponse<any>>} 不分页查询结果
|
|
72
|
+
*/
|
|
73
|
+
noPage(data?: any, options?: RequestOptions): any;
|
|
74
|
+
/**
|
|
75
|
+
* 详情查询
|
|
76
|
+
* @param {string} id 详情ID
|
|
77
|
+
* @param {object} params 查询参数
|
|
78
|
+
* @param {object} options 请求配置
|
|
79
|
+
* @returns {Promise<AxiosResponse<any>>} 详情查询结果
|
|
80
|
+
*/
|
|
81
|
+
detail(id: string, params?: any, options?: RequestOptions): any;
|
|
82
|
+
/**
|
|
83
|
+
* 保存
|
|
84
|
+
* @param {object} data 保存参数
|
|
85
|
+
* @param {object} options 请求配置
|
|
86
|
+
* @returns {Promise<AxiosResponse<any>>} 保存结果
|
|
87
|
+
*/
|
|
88
|
+
save(data?: any, options?: RequestOptions): any;
|
|
89
|
+
/**
|
|
90
|
+
* 更新
|
|
91
|
+
* @param {object} data 更新参数
|
|
92
|
+
* @param {object} options 请求配置
|
|
93
|
+
* @returns {Promise<AxiosResponse<any>>} 更新结果
|
|
94
|
+
*/
|
|
95
|
+
update(data?: any, options?: RequestOptions): Promise<AxiosResponseRewrite<any>>;
|
|
96
|
+
/**
|
|
97
|
+
* 删除
|
|
98
|
+
* @param {string} id 删除ID
|
|
99
|
+
* @param {object} options 请求配置
|
|
100
|
+
* @returns {Promise<AxiosResponse<any>>} 删除结果
|
|
101
|
+
*/
|
|
102
|
+
delete(id: string, params?: any, options?: RequestOptions): Promise<AxiosResponseRewrite<any>>;
|
|
103
|
+
post(...args: any[]): Promise<AxiosResponseRewrite<any>>;
|
|
104
|
+
get(...args: any[]): Promise<AxiosResponseRewrite<any>>;
|
|
105
|
+
put(...args: any[]): Promise<AxiosResponseRewrite<any>>;
|
|
106
|
+
patch(...args: any[]): Promise<AxiosResponseRewrite<any>>;
|
|
107
|
+
remove(...args: any[]): Promise<AxiosResponseRewrite<any>>;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
declare class NdJson {
|
|
111
|
+
options: any;
|
|
112
|
+
isRead: boolean;
|
|
113
|
+
controller: any;
|
|
114
|
+
constructor();
|
|
115
|
+
create(options: any): void;
|
|
116
|
+
getUrl(url: any): any;
|
|
117
|
+
get(url: any, data?: string, extra?: {}): Observable<unknown>;
|
|
118
|
+
post(url: any, data?: BodyInit | any, extra?: {}): Observable<unknown>;
|
|
119
|
+
handleRequest(url: any): RequestInit;
|
|
120
|
+
handleResponse(response: any): any;
|
|
121
|
+
cancel(): void;
|
|
122
|
+
}
|
|
123
|
+
declare const ndJson: NdJson;
|
|
124
|
+
|
|
125
|
+
interface WebSocketMessage {
|
|
126
|
+
type: string;
|
|
127
|
+
id?: string;
|
|
128
|
+
topic?: string;
|
|
129
|
+
parameter?: Record<string, any>;
|
|
130
|
+
requestId?: string;
|
|
131
|
+
message?: string;
|
|
132
|
+
[key: string]: any;
|
|
133
|
+
}
|
|
134
|
+
type WS_Options = {
|
|
135
|
+
onError?: (message: WebSocketMessage) => void;
|
|
136
|
+
};
|
|
137
|
+
declare class WebSocketClient {
|
|
138
|
+
private ws;
|
|
139
|
+
private subscriptions;
|
|
140
|
+
private pendingSubscriptions;
|
|
141
|
+
private heartbeatSubscription;
|
|
142
|
+
private reconnectAttempts;
|
|
143
|
+
private readonly maxReconnectAttempts;
|
|
144
|
+
private isConnected;
|
|
145
|
+
private tempQueue;
|
|
146
|
+
private url;
|
|
147
|
+
private options;
|
|
148
|
+
private wsClient;
|
|
149
|
+
constructor(options?: WS_Options);
|
|
150
|
+
setOptions(options: WS_Options): void;
|
|
151
|
+
initWebSocket(url: string): void;
|
|
152
|
+
private setupConnectionMonitor;
|
|
153
|
+
private getReconnectDelay;
|
|
154
|
+
private setupWebSocket;
|
|
155
|
+
private startHeartbeat;
|
|
156
|
+
private stopHeartbeat;
|
|
157
|
+
private handleMessage;
|
|
158
|
+
private processTempQueue;
|
|
159
|
+
private cacheSubscriptions;
|
|
160
|
+
private restoreSubscriptions;
|
|
161
|
+
private reconnect;
|
|
162
|
+
connect(): void;
|
|
163
|
+
disconnect(): void;
|
|
164
|
+
send(message: WebSocketMessage): void;
|
|
165
|
+
getWebSocket(id: string, topic: string, parameter?: Record<string, any>): Observable<WebSocketMessage>;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* 创建单例
|
|
169
|
+
* @example
|
|
170
|
+
* wsClient.initWebSocket('ws://example.com/ws');
|
|
171
|
+
* wsClient.connect();
|
|
172
|
+
* const subscription = wsClient.getWebSocket('id1', 'topic1', { param: 'value' })
|
|
173
|
+
* .subscribe(
|
|
174
|
+
* message => console.log('Received:', message)
|
|
175
|
+
* );
|
|
176
|
+
*
|
|
177
|
+
* // 清理
|
|
178
|
+
* subscription.unsubscribe();
|
|
179
|
+
*
|
|
180
|
+
*/
|
|
181
|
+
declare const wsClient: WebSocketClient;
|
|
182
|
+
|
|
183
|
+
declare let router: any;
|
|
184
|
+
declare const installRouter: (r: any) => void;
|
|
185
|
+
|
|
186
|
+
declare let stores: {};
|
|
187
|
+
declare const installStores: (_s?: {}) => void;
|
|
188
|
+
|
|
189
|
+
declare let locales: any;
|
|
190
|
+
declare const installLocales: (l: any) => void;
|
|
191
|
+
|
|
192
|
+
export { NdJson, Request, WebSocketClient, abortAllRequests, crateAxios, get, getStream, installLocales, installRouter, installStores, locales, ndJson, patch, post, postStream, put, remove, request, router, stores, wsClient };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import{TOKEN_KEY as S,BASE_API as B,LOCAL_BASE_API as U}from"@jetlinks-web/constants";import{getToken as j,randomString as J}from"@jetlinks-web/utils";import G from"axios";import{isFunction as T,isObject as Y}from"lodash-es";var h,a={filter_url:[],code:200,codeKey:"status",timeout:1e3*15,handleRequest:void 0,handleResponse:void 0,handleError:void 0,langKey:"lang",requestOptions:s=>({}),tokenExpiration:()=>{},handleReconnect:()=>Promise.resolve(),isCreateTokenRefresh:!1},w=[],E=!1,be=window.__MICRO_APP_ENVIRONMENT__,b=new Map,F=s=>{let e=J(32);b.has(e)&&b.get(e)?.abort();let t=new AbortController;s.signal=t.signal,s.__requestKey=e,b.set(e,t)},V=s=>{F(s);let e=j(),t=localStorage.getItem(a.langKey),r=localStorage.getItem(U);if(t&&(s.headers[a.langKey]=t),r&&!s.baseURL){let n=s.url.startsWith("/")?s.url:`/${s.url}`;s.url=r+n}if(!e&&!a.filter_url?.some(n=>s.url?.includes(n)))return a.tokenExpiration?.(),s;if(s.headers[S]||(s.headers[S]=e),a.requestOptions&&T(a.requestOptions)){let n=a.requestOptions(s);if(n&&Y(n))for(let i in n)s[i]=n[i]}return s},z=s=>{if(a.handleResponse&&T(a.handleResponse))return a.handleResponse(s);let e=s.config?.__requestKey;if(e&&b.delete(e),s.data instanceof ArrayBuffer)return s;let t=s.data[a.codeKey||"status"];return typeof s.data=="object"&&typeof s.data.success>"u"&&(s.data.success=t===a.code),s.data},X=async s=>{let e=s.config;if(E)return new Promise((t,r)=>{w.push({resolve:t,reject:r})}).then(t=>(e.headers[S]=t,h(e))).catch(t=>Promise.reject(t));e._retry=!0,E=!0;try{if(await a.handleReconnect?.()){let r=j();return e.headers[S]=r,w.forEach(n=>n.resolve(r)),h(e)}}catch(t){throw w.forEach(r=>r.reject(t)),t}finally{w=[],E=!1}},M=async s=>{let e=s.response?.message||"Error",t=0;if(s.response){let{data:r,status:n}=s.response;switch(t=n,n){case 400:case 403:case 500:e=`${r?.message}`.substring(0,90);break;case 401:if(e=s.response.data.result?.text||"\u7528\u6237\u672A\u767B\u5F55",a.tokenExpiration?.(s),a.isCreateTokenRefresh)return X(s);break;case 404:e=s?.response?.data?.message||`${r?.error} ${r?.path}`;break;default:break}}else s.response===void 0&&(e=s.message.includes("timeout")?"\u63A5\u53E3\u54CD\u5E94\u8D85\u65F6":s.message,t="timeout");return a.handleError&&T(a.handleError)&&a.handleError(e,t,s),Promise.reject(s)},ye=()=>{b.forEach(s=>s.abort()),b.clear()},Re=s=>{s&&(a=Object.assign(a,s)),h=G.create({withCredentials:!1,timeout:a.timeout,baseURL:B}),h.interceptors.request.use(V,M),h.interceptors.response.use(z,M)},W=(s,e={},t)=>h({method:"POST",url:s,data:e,...t}),P=(s,e=void 0,t)=>h({method:"GET",url:s,params:e,...t}),N=(s,e={},t)=>h({method:"PUT",url:s,data:e,...t}),_=(s,e={},t)=>h({method:"patch",url:s,data:e,...t}),q=(s,e=void 0,t)=>h({method:"DELETE",url:s,params:e,...t}),Z=(s,e,t)=>P(s,e,{responseType:"arraybuffer",...t}),ee=(s,e,t)=>W(s,e,{responseType:"arraybuffer",...t}),x={post:W,get:P,put:N,patch:_,remove:q,getStream:Z,postStream:ee},I=class{modulePath;constructor(e){this.modulePath=e}page(e={},t={url:void 0,method:void 0}){let{url:r="/_query",method:n="post",...i}=t;return x[n](`${this.modulePath}${r}`,e,i)}noPage(e={},t={url:void 0,method:void 0}){let{url:r="/_query/no-page",method:n="post",...i}=t;return x[n](`${this.modulePath}${r}`,{paging:!1,...e},i)}detail(e,t,r={url:void 0,method:void 0}){let{url:n=`/${e}/detail`,method:i="get",...d}=r;return x[i](`${this.modulePath}${n}`,t,d)}save(e={},t={url:void 0,method:void 0}){let{url:r="/_create",method:n="post",...i}=t;return x[n](`${this.modulePath}${r}`,e,i)}update(e={},t={url:void 0,method:void 0}){let{url:r="/_update",method:n="patch",...i}=t;return _(`${this.modulePath}${r}`,e,i)}delete(e,t,r={url:void 0,method:void 0}){let{url:n=`/${e}`,method:i="post",...d}=r;return q(`${this.modulePath}${n}`,t,d)}post(...e){let[t,r,n]=e;return W(`${this.modulePath}${t}`,r,n)}get(...e){let[t,r,n]=e;return P(`${this.modulePath}${t}`,r,n)}put(...e){let[t,r,n]=e;return N(`${this.modulePath}${t}`,r,n)}patch(...e){let[t,r,n]=e;return _(`${this.modulePath}${t}`,r,n)}remove(...e){let[t,r,n]=e;return q(`${this.modulePath}${t}`,r,n)}};import{getToken as te}from"@jetlinks-web/utils";import{BASE_API as se,TOKEN_KEY as K}from"@jetlinks-web/constants";import{isFunction as L,isObject as H}from"lodash-es";import{Observable as Q}from"rxjs";var v=class{options={code:200,codeKey:"status"};isRead=!1;controller=null;constructor(){}create(e){this.options=Object.assign(this.options,e)}getUrl(e){return se+e}get(e,t="{}",r={}){let n=this.getUrl(e),i=this,d=this.controller=new AbortController;return new Q(o=>{let u;return fetch(n,{method:"GET",signal:d.signal,keepalive:!0,...r,...this.handleRequest(n)}).then(m=>{u=m.body?.getReader();let k=new TextDecoder,l="";if(!u){o.error(new Error("No readable stream available"));return}let y=()=>{if(!i.isRead){u.cancel(),o.complete();return}u.read().then(({done:g,value:A})=>{if(g){if(l.trim().length>0)try{o.next(JSON.parse(l.trim()))}catch(p){o.error(p)}o.complete();return}let C=k.decode(A,{stream:!0});l+=C;let f=l.split(`
|
|
2
|
+
`);for(let p=0;p<f.length-1;++p){let R=f[p].trim();if(R.length>0)try{o.next(JSON.parse(R))}catch(O){o.error(O),u.cancel();return}}l=f[f.length-1],y()}).catch(g=>o.error(g))};i.isRead=!0,y()}).catch(m=>{o.error(m)}),()=>{i.cancel()}})}post(e,t={},r={}){let n=this.getUrl(e),i=this,d=this.controller=new AbortController;return new Q(o=>{let u;return fetch(n,{method:"POST",signal:d.signal,keepalive:!0,body:H(t)?JSON.stringify(t):t,...r,...this.handleRequest(n)}).then(async m=>{u=m.body?.getReader();let k=new TextDecoder,l="";if(!u){o.error(new Error("No readable stream available"));return}let y=()=>{if(!i.isRead){u.cancel(),o.complete();return}u.read().then(({done:g,value:A})=>{if(g){if(l.trim().length>0)try{o.next(JSON.parse(l.trim()))}catch(p){o.error(p)}o.complete();return}let C=k.decode(A,{stream:!0});l+=C;let f=l.split(`
|
|
3
|
+
`);for(let p=0;p<f.length-1;++p){let R=f[p].trim();if(R.length>0)try{o.next(JSON.parse(R))}catch(O){o.error(O),u.cancel();return}}l=f[f.length-1],y()}).catch(g=>o.error(g))};i.isRead=!0,y()}).catch(m=>{o.error(m)}),()=>{i.cancel()}})}handleRequest(e){let t={headers:{"Content-Type":"application/x-ndjson"}},r=te();if(!r&&this.options.filter_url?.some(n=>n.includes(e)))return this.options.tokenExpiration?.(),t;if(t.headers[K]||(t.headers[K]=r),this.options.requestOptions&&L(this.options.requestOptions)){let n=this.options.requestOptions(t);if(n&&H(n))for(let i in n)t[i]=n[i]}return t}handleResponse(e){return this.options.handleResponse&&L(this.options.handleResponse)?this.options.handleResponse(e):e}cancel(){this.isRead&&(this.isRead=!1),this.controller.abort()}},Ce=new v;import{webSocket as ne}from"rxjs/webSocket";import{Observable as re,Subject as ie,timer as D,EMPTY as oe}from"rxjs";import{retry as ae,catchError as ce}from"rxjs/operators";import{notification as ue}from"ant-design-vue";var c=window.__MICRO_APP_ENVIRONMENT__,$=class{ws=null;subscriptions=new Map;pendingSubscriptions=new Map;heartbeatSubscription=null;reconnectAttempts=0;maxReconnectAttempts=2;isConnected=!1;tempQueue=[];url="";options={};wsClient;constructor(e){this.setOptions(e),this.setupConnectionMonitor(),c&&window.microApp.addGlobalDataListener(t=>{this.wsClient=t.wsClient})}setOptions(e){this.options=e||{}}initWebSocket(e){this.url=e}setupConnectionMonitor(){c||(window.addEventListener("online",()=>{console.log("Network is online, attempting to reconnect..."),this.reconnect()}),window.addEventListener("offline",()=>{console.log("Network is offline, caching subscriptions..."),this.cacheSubscriptions()}),window.addEventListener("beforeunload",()=>{this.disconnect()}))}getReconnectDelay(){return this.reconnectAttempts<=10?5e3:this.reconnectAttempts<=20?15e3:6e4}setupWebSocket(){if(c&&this.wsClient){this.wsClient.setupWebSocket();return}this.ws||!this.url||(this.ws=ne({url:this.url,openObserver:{next:()=>{console.log("WebSocket connected"),this.isConnected=!0,this.reconnectAttempts=0,this.startHeartbeat(),this.restoreSubscriptions(),this.processTempQueue()}},closeObserver:{next:()=>{console.log("WebSocket disconnected"),this.isConnected=!1;let e=this.getReconnectDelay();setTimeout(()=>{this.reconnectAttempts+=1,!(this.reconnectAttempts>this.maxReconnectAttempts)&&(this.cacheSubscriptions(),this.stopHeartbeat(),this.reconnect())},e)}}}),this.ws.pipe(ce(e=>(console.error("WebSocket error:",e),oe)),ae({delay:(e,t)=>{if(this.reconnectAttempts=t,t>this.maxReconnectAttempts)throw new Error("Max reconnection attempts reached");return D(this.getReconnectDelay())}})).subscribe(e=>this.handleMessage(e),e=>console.error("WebSocket error:",e)))}startHeartbeat(){if(c&&this.wsClient){this.wsClient.startHeartbeat();return}this.stopHeartbeat(),this.heartbeatSubscription=D(0,2e3).subscribe(()=>{this.send({type:"ping"})})}stopHeartbeat(){if(c&&this.wsClient){this.wsClient.stopHeartbeat();return}this.heartbeatSubscription&&(this.heartbeatSubscription.unsubscribe(),this.heartbeatSubscription=null)}handleMessage(e){if(c&&this.wsClient){this.wsClient.handleMessage(e);return}if(e.type==="pong")return;if(e.type==="error"){this.options.onError?this.options.onError(e):ue.error({key:"error",message:e.message});return}let t=this.subscriptions.get(e.requestId||"");t&&(e.type==="complete"?(t.complete(),this.subscriptions.delete(e.requestId||"")):e.type==="result"&&t.next(e))}processTempQueue(){if(c&&this.wsClient){this.wsClient.processTempQueue();return}for(;this.tempQueue.length>0;){let e=this.tempQueue.shift();e&&this.send(e)}}cacheSubscriptions(){if(c&&this.wsClient){this.wsClient.cacheSubscriptions();return}this.pendingSubscriptions=new Map(this.subscriptions),this.subscriptions.clear()}restoreSubscriptions(){if(c&&this.wsClient){this.wsClient.restoreSubscriptions();return}this.pendingSubscriptions.forEach((e,t)=>{this.subscriptions.set(t,e)}),this.pendingSubscriptions.clear()}reconnect(){if(c&&this.wsClient){this.wsClient.reconnect();return}!this.isConnected&&navigator.onLine&&(this.ws=null,this.setupWebSocket())}connect(){if(c&&this.wsClient){this.wsClient.connect();return}this.setupWebSocket()}disconnect(){if(c&&this.wsClient){this.wsClient.disconnect();return}this.ws&&(this.ws.complete(),this.ws=null),this.stopHeartbeat(),this.subscriptions.clear(),this.pendingSubscriptions.clear(),this.tempQueue=[]}send(e){if(c&&this.wsClient){this.wsClient.send(e);return}this.ws&&this.isConnected?this.ws.next(e):this.tempQueue.push(e)}getWebSocket(e,t,r={}){if(console.log("getWebSocket",this.wsClient,e),c&&this.wsClient)return this.wsClient.getWebSocket(e,t,r);let n=new ie;this.subscriptions.set(e,n);let i={id:e,topic:t,parameter:r,type:"sub"};return this.send(i),new re(d=>{let o=n.subscribe(d);return()=>{o.unsubscribe(),this.send({id:e,type:"unsub"}),this.subscriptions.delete(e)}})}},Pe=new $;var le,$e=s=>{le=s};var pe={},Ie=(s={})=>{pe=s};var he,Ne=s=>{he=s};
|
|
4
|
+
export{v as NdJson,I as Request,$ as WebSocketClient,ye as abortAllRequests,Re as crateAxios,P as get,Z as getStream,Ne as installLocales,$e as installRouter,Ie as installStores,he as locales,Ce as ndJson,_ as patch,W as post,ee as postStream,N as put,q as remove,x as request,le as router,pe as stores,Pe as wsClient};
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jetlinks-web/core",
|
|
3
|
-
"version": "2.2.
|
|
4
|
-
"main": "index.
|
|
5
|
-
"module": "index.
|
|
3
|
+
"version": "2.2.12",
|
|
4
|
+
"main": "dist/core/index.mjs",
|
|
5
|
+
"module": "dist/core/index.mjs",
|
|
6
|
+
"types": "dist/core/index.d.ts",
|
|
6
7
|
"keywords": [
|
|
7
8
|
"jetlinks",
|
|
8
9
|
"jetlinks-web",
|
|
@@ -13,7 +14,8 @@
|
|
|
13
14
|
],
|
|
14
15
|
"files": [
|
|
15
16
|
"src",
|
|
16
|
-
"index.ts"
|
|
17
|
+
"index.ts",
|
|
18
|
+
"dist"
|
|
17
19
|
],
|
|
18
20
|
"sideEffects": [
|
|
19
21
|
"./src/axios.ts",
|
|
@@ -27,8 +29,8 @@
|
|
|
27
29
|
"axios": "^1.7.4",
|
|
28
30
|
"rxjs": "^7.8.1",
|
|
29
31
|
"@jetlinks-web/constants": "^1.0.9",
|
|
30
|
-
"@jetlinks-web/
|
|
31
|
-
"@jetlinks-web/
|
|
32
|
+
"@jetlinks-web/types": "^1.0.2",
|
|
33
|
+
"@jetlinks-web/utils": "^1.2.11"
|
|
32
34
|
},
|
|
33
35
|
"publishConfig": {
|
|
34
36
|
"registry": "https://registry.npmjs.org/",
|
package/src/axios.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { message } from 'ant-design-vue';
|
|
1
2
|
import { TOKEN_KEY, BASE_API, LOCAL_BASE_API } from '@jetlinks-web/constants'
|
|
2
3
|
import {getToken, randomString} from '@jetlinks-web/utils'
|
|
3
4
|
import axios from 'axios'
|
|
@@ -12,7 +13,7 @@ import {isFunction, isObject} from 'lodash-es'
|
|
|
12
13
|
|
|
13
14
|
interface Options {
|
|
14
15
|
|
|
15
|
-
tokenExpiration: (err
|
|
16
|
+
tokenExpiration: (err?: AxiosError<any>, response?: AxiosResponse) => void
|
|
16
17
|
handleReconnect: () => Promise<any>
|
|
17
18
|
filter_url?: Array<string>
|
|
18
19
|
code?: number
|
|
@@ -39,6 +40,19 @@ interface Options {
|
|
|
39
40
|
isCreateTokenRefresh?: boolean
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
interface typeRequestConfig extends InternalAxiosRequestConfig {
|
|
44
|
+
__requestKey?: string
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface typeAxiosResponse extends AxiosResponse {
|
|
48
|
+
config: typeRequestConfig
|
|
49
|
+
message: string
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface typeAxiosError<T> extends AxiosError<T> {
|
|
53
|
+
response: typeAxiosResponse
|
|
54
|
+
}
|
|
55
|
+
|
|
42
56
|
interface RequestOptions {
|
|
43
57
|
url?: string
|
|
44
58
|
method?: string
|
|
@@ -69,7 +83,7 @@ let isRefreshing = false;
|
|
|
69
83
|
const isApp = (window as any).__MICRO_APP_ENVIRONMENT__
|
|
70
84
|
|
|
71
85
|
const pendingRequests = new Map<string, AbortController>();
|
|
72
|
-
const requestRecords = (config:
|
|
86
|
+
const requestRecords = (config: typeRequestConfig) => {
|
|
73
87
|
const key = randomString(32)
|
|
74
88
|
|
|
75
89
|
// 取消重复请求
|
|
@@ -83,7 +97,7 @@ const requestRecords = (config: InternalAxiosRequestConfig) => {
|
|
|
83
97
|
|
|
84
98
|
pendingRequests.set(key, controller)
|
|
85
99
|
}
|
|
86
|
-
const handleRequest = (config:
|
|
100
|
+
const handleRequest = (config: typeRequestConfig) => {
|
|
87
101
|
requestRecords(config)
|
|
88
102
|
const token = getToken();
|
|
89
103
|
const lang = localStorage.getItem(_options.langKey)
|
|
@@ -121,7 +135,7 @@ const handleRequest = (config: InternalAxiosRequestConfig) => {
|
|
|
121
135
|
return config
|
|
122
136
|
}
|
|
123
137
|
|
|
124
|
-
const handleResponse = (response:
|
|
138
|
+
const handleResponse = (response: typeAxiosResponse) => {
|
|
125
139
|
if (_options.handleResponse && isFunction(_options.handleResponse)) {
|
|
126
140
|
return _options.handleResponse(response)
|
|
127
141
|
}
|
|
@@ -176,7 +190,7 @@ const createTokenRefreshHandler = async (err) => {
|
|
|
176
190
|
isRefreshing = false;
|
|
177
191
|
}
|
|
178
192
|
}
|
|
179
|
-
const errorHandler = async (err:
|
|
193
|
+
const errorHandler = async (err: typeAxiosError<any>) => {
|
|
180
194
|
let description = err.response?.message || 'Error'
|
|
181
195
|
let _status: string | number = 0
|
|
182
196
|
if (err.response) {
|
package/src/fetch.ts
CHANGED
package/src/axios.ts~
DELETED
|
@@ -1,419 +0,0 @@
|
|
|
1
|
-
import { TOKEN_KEY, BASE_API } from '@jetlinks-web/constants'
|
|
2
|
-
import {getToken, randomString} from '@jetlinks-web/utils'
|
|
3
|
-
import axios from 'axios'
|
|
4
|
-
import type {
|
|
5
|
-
AxiosInstance,
|
|
6
|
-
AxiosResponse,
|
|
7
|
-
AxiosError,
|
|
8
|
-
InternalAxiosRequestConfig,
|
|
9
|
-
} from 'axios'
|
|
10
|
-
import type { AxiosResponseRewrite } from '@jetlinks-web/types'
|
|
11
|
-
import {isFunction, isObject} from 'lodash-es'
|
|
12
|
-
|
|
13
|
-
interface Options {
|
|
14
|
-
|
|
15
|
-
tokenExpiration: (err: AxiosError<any>, response: AxiosResponse) => void
|
|
16
|
-
handleReconnect: () => Promise<any>
|
|
17
|
-
filter_url?: Array<string>
|
|
18
|
-
code?: number
|
|
19
|
-
codeKey?: string
|
|
20
|
-
timeout?: number
|
|
21
|
-
handleRequest?: () => void
|
|
22
|
-
/**
|
|
23
|
-
* 用以获取localstorage中的lang
|
|
24
|
-
*/
|
|
25
|
-
langKey?: string
|
|
26
|
-
/**
|
|
27
|
-
* response处理函数
|
|
28
|
-
* @param response AxiosResponse实例
|
|
29
|
-
*/
|
|
30
|
-
handleResponse?: (response: AxiosResponse) => void
|
|
31
|
-
/**
|
|
32
|
-
* 错误处理函数
|
|
33
|
-
* @param msg 错误消息
|
|
34
|
-
* @param status 错误code
|
|
35
|
-
* @param error 错误实例
|
|
36
|
-
*/
|
|
37
|
-
handleError?: (msg: string, status: string | number, error: AxiosError<any>) => void
|
|
38
|
-
requestOptions?: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Record<string, any>
|
|
39
|
-
isCreateTokenRefresh?: boolean
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
interface RequestOptions {
|
|
43
|
-
url?: string
|
|
44
|
-
method?: string
|
|
45
|
-
params?: any
|
|
46
|
-
data?: any
|
|
47
|
-
[key: string]: any
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
let instance: AxiosInstance
|
|
51
|
-
let _options: Options = {
|
|
52
|
-
filter_url: [],
|
|
53
|
-
code: 200,
|
|
54
|
-
codeKey: 'status',
|
|
55
|
-
timeout: 1000 * 15,
|
|
56
|
-
handleRequest: undefined,
|
|
57
|
-
handleResponse: undefined,
|
|
58
|
-
handleError: undefined,
|
|
59
|
-
langKey: 'lang',
|
|
60
|
-
requestOptions: (config) => ({}),
|
|
61
|
-
tokenExpiration: () => {},
|
|
62
|
-
handleReconnect: () => Promise.resolve(),
|
|
63
|
-
isCreateTokenRefresh: false
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
let failedQueue = [];
|
|
67
|
-
let isRefreshing = false;
|
|
68
|
-
|
|
69
|
-
const isApp = (window as any).__MICRO_APP_ENVIRONMENT__
|
|
70
|
-
|
|
71
|
-
const pendingRequests = new Map<string, AbortController>();
|
|
72
|
-
const requestRecords = (config: InternalAxiosRequestConfig) => {
|
|
73
|
-
const key = randomString(32)
|
|
74
|
-
|
|
75
|
-
// 取消重复请求
|
|
76
|
-
if (pendingRequests.has(key)) {
|
|
77
|
-
pendingRequests.get(key)?.abort()
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const controller = new AbortController()
|
|
81
|
-
config.signal = controller.signal;
|
|
82
|
-
config.__requestKey = key;
|
|
83
|
-
|
|
84
|
-
pendingRequests.set(key, controller)
|
|
85
|
-
}
|
|
86
|
-
const handleRequest = (config: InternalAxiosRequestConfig) => {
|
|
87
|
-
requestRecords(config)
|
|
88
|
-
const token = getToken();
|
|
89
|
-
const lang = localStorage.getItem(_options.langKey)
|
|
90
|
-
const localBaseApi = localStorage.getItem('')
|
|
91
|
-
|
|
92
|
-
if (lang) {
|
|
93
|
-
config.headers[_options.langKey] = lang
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (localBaseApi && !config.baseURL) {
|
|
97
|
-
const _url = config.url.startsWith('/') ? config.url : `/${config.url}`
|
|
98
|
-
config.url = localBaseApi + _url
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// 没有token,并且该接口需要token校验
|
|
102
|
-
if (!token && !_options.filter_url?.some((url) => config.url?.includes(url))) {
|
|
103
|
-
// 跳转登录页
|
|
104
|
-
_options.tokenExpiration?.()
|
|
105
|
-
return config
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (!config.headers[TOKEN_KEY]) {
|
|
109
|
-
config.headers[TOKEN_KEY] = token
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (_options.requestOptions && isFunction(_options.requestOptions)) {
|
|
113
|
-
const extraOptions = _options.requestOptions(config)
|
|
114
|
-
if (extraOptions && isObject(extraOptions)) {
|
|
115
|
-
for (const key in extraOptions) {
|
|
116
|
-
config[key] = extraOptions[key]
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return config
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const handleResponse = (response: AxiosResponse) => {
|
|
125
|
-
if (_options.handleResponse && isFunction(_options.handleResponse)) {
|
|
126
|
-
return _options.handleResponse(response)
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const __key = response.config?.__requestKey
|
|
130
|
-
if(__key){
|
|
131
|
-
pendingRequests.delete(__key)
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
if (response.data instanceof ArrayBuffer) {
|
|
135
|
-
return response
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const status = response.data[_options.codeKey || 'status']
|
|
139
|
-
|
|
140
|
-
// 增加业务接口处理成功判断方式,只需要判断返回参数包含:success为true
|
|
141
|
-
if (
|
|
142
|
-
typeof response.data === 'object' &&
|
|
143
|
-
typeof response.data.success === 'undefined'
|
|
144
|
-
) {
|
|
145
|
-
response.data.success = status === _options.code
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return response.data
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
const createTokenRefreshHandler = async (err) => {
|
|
152
|
-
const originalRequest = err.config;
|
|
153
|
-
if (isRefreshing) { // 记录之后失败的请求
|
|
154
|
-
return new Promise((resolve, reject) => {
|
|
155
|
-
failedQueue.push({ resolve, reject });
|
|
156
|
-
}).then((_token) => {
|
|
157
|
-
originalRequest.headers[TOKEN_KEY] = _token;
|
|
158
|
-
return instance(originalRequest)
|
|
159
|
-
}).catch(err => Promise.reject(err))
|
|
160
|
-
}
|
|
161
|
-
originalRequest._retry = true;
|
|
162
|
-
isRefreshing = true;
|
|
163
|
-
try {
|
|
164
|
-
const loginResult = await _options.handleReconnect?.()
|
|
165
|
-
if(loginResult){
|
|
166
|
-
const token = getToken() // 更新请求头, 修改全部的token
|
|
167
|
-
originalRequest.headers[TOKEN_KEY] = token;
|
|
168
|
-
failedQueue.forEach(a => a.resolve(token));
|
|
169
|
-
return instance(originalRequest);
|
|
170
|
-
}
|
|
171
|
-
} catch (err) {
|
|
172
|
-
failedQueue.forEach(cb => cb.reject(err));
|
|
173
|
-
throw err;
|
|
174
|
-
} finally {
|
|
175
|
-
failedQueue = [];
|
|
176
|
-
isRefreshing = false;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
const errorHandler = async (err: AxiosError<any>) => {
|
|
180
|
-
let description = err.response?.message || 'Error'
|
|
181
|
-
let _status: string | number = 0
|
|
182
|
-
if (err.response) {
|
|
183
|
-
const {data, status} = err.response
|
|
184
|
-
_status = status
|
|
185
|
-
switch (status) {
|
|
186
|
-
case 400:
|
|
187
|
-
case 403:
|
|
188
|
-
case 500:
|
|
189
|
-
description = (`${data?.message}`).substring(0, 90)
|
|
190
|
-
break;
|
|
191
|
-
case 401:
|
|
192
|
-
description = err.response.data.result?.text || '用户未登录';
|
|
193
|
-
_options.tokenExpiration?.(err)
|
|
194
|
-
if(_options.isCreateTokenRefresh){
|
|
195
|
-
return createTokenRefreshHandler(err)
|
|
196
|
-
}
|
|
197
|
-
break;
|
|
198
|
-
case 404:
|
|
199
|
-
description = err?.response?.data?.message || `${data?.error} ${data?.path}`
|
|
200
|
-
break;
|
|
201
|
-
default:
|
|
202
|
-
break;
|
|
203
|
-
}
|
|
204
|
-
} else if (err.response === undefined) {
|
|
205
|
-
description = err.message.includes('timeout') ? '接口响应超时' : err.message
|
|
206
|
-
_status = 'timeout'
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
if (_options.handleError && isFunction(_options.handleError)) {
|
|
210
|
-
_options.handleError(description, _status, err)
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
return Promise.reject(err)
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
export const abortAllRequests = () => {
|
|
217
|
-
pendingRequests.forEach(controller => controller.abort())
|
|
218
|
-
pendingRequests.clear()
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
export const crateAxios = (options: Options) => {
|
|
222
|
-
if (options) {
|
|
223
|
-
_options = Object.assign(_options, options)
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
instance = axios.create({
|
|
227
|
-
withCredentials: false,
|
|
228
|
-
timeout: _options.timeout,
|
|
229
|
-
baseURL: BASE_API
|
|
230
|
-
})
|
|
231
|
-
|
|
232
|
-
instance.interceptors.request.use(
|
|
233
|
-
handleRequest,
|
|
234
|
-
errorHandler
|
|
235
|
-
)
|
|
236
|
-
|
|
237
|
-
instance.interceptors.response.use(
|
|
238
|
-
handleResponse,
|
|
239
|
-
errorHandler
|
|
240
|
-
)
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
export const post = <T = any>(url: string, data: any = {}, ext?: any) => {
|
|
244
|
-
return (instance<any, AxiosResponseRewrite<T>>({
|
|
245
|
-
method: 'POST',
|
|
246
|
-
url,
|
|
247
|
-
data,
|
|
248
|
-
...ext,
|
|
249
|
-
}))
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
export const get = <T = any>(url: string, params: any = undefined, ext?: any) => {
|
|
253
|
-
return instance<any, AxiosResponseRewrite<T>>({
|
|
254
|
-
method: 'GET',
|
|
255
|
-
url,
|
|
256
|
-
params,
|
|
257
|
-
...ext,
|
|
258
|
-
})
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
export const put = <T = any>(url: string, data: any = {}, ext?: any) => {
|
|
262
|
-
return instance<any, AxiosResponseRewrite<T>>({
|
|
263
|
-
method: 'PUT',
|
|
264
|
-
url,
|
|
265
|
-
data,
|
|
266
|
-
...ext,
|
|
267
|
-
})
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
export const patch = <T = any>(url: string, data: any = {}, ext?: any) => {
|
|
271
|
-
return instance<any, AxiosResponseRewrite<T>>({
|
|
272
|
-
method: 'patch',
|
|
273
|
-
url,
|
|
274
|
-
data,
|
|
275
|
-
...ext,
|
|
276
|
-
})
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
export const remove = <T = any>(url: string, params: any = undefined, ext?: any) => {
|
|
280
|
-
return instance<any, AxiosResponseRewrite<T>>({
|
|
281
|
-
method: 'DELETE',
|
|
282
|
-
url,
|
|
283
|
-
params,
|
|
284
|
-
...ext,
|
|
285
|
-
})
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
export const getStream = (url: string, params?: any, ext?: any) => {
|
|
289
|
-
return get(url, params, { responseType: 'arraybuffer', ...ext })
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
export const postStream = (url: string, data: any, ext?: any) => {
|
|
293
|
-
return post(url, data, { responseType: 'arraybuffer', ...ext })
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
export const request = {
|
|
297
|
-
post, get, put, patch, remove, getStream, postStream
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
export class Request {
|
|
301
|
-
modulePath: string
|
|
302
|
-
|
|
303
|
-
constructor(modulePath: string) {
|
|
304
|
-
this.modulePath = modulePath
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* 分页查询
|
|
309
|
-
* @param {object} data 查询参数
|
|
310
|
-
* @param {object} options 请求配置
|
|
311
|
-
* @returns {Promise<AxiosResponse<any>>} 分页查询结果
|
|
312
|
-
*/
|
|
313
|
-
page(data: any={}, options: RequestOptions= {
|
|
314
|
-
url: undefined,
|
|
315
|
-
method: undefined,
|
|
316
|
-
}) {
|
|
317
|
-
const { url='/_query', method = 'post', ...rest } = options
|
|
318
|
-
return request[method](`${this.modulePath}${url}`, data, rest)
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* 不分页查询
|
|
323
|
-
* @param {object} data 查询参数
|
|
324
|
-
* @param {object} options 请求配置
|
|
325
|
-
* @returns {Promise<AxiosResponse<any>>} 不分页查询结果
|
|
326
|
-
*/
|
|
327
|
-
noPage(data: any={}, options: RequestOptions = {
|
|
328
|
-
url: undefined,
|
|
329
|
-
method: undefined,
|
|
330
|
-
}) {
|
|
331
|
-
const { url='/_query/no-page', method = 'post', ...rest } = options
|
|
332
|
-
return request[method](`${this.modulePath}${url}`, { paging: false, ...data}, rest)
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* 详情查询
|
|
337
|
-
* @param {string} id 详情ID
|
|
338
|
-
* @param {object} params 查询参数
|
|
339
|
-
* @param {object} options 请求配置
|
|
340
|
-
* @returns {Promise<AxiosResponse<any>>} 详情查询结果
|
|
341
|
-
*/
|
|
342
|
-
detail(id: string, params?: any, options: RequestOptions= {
|
|
343
|
-
url: undefined,
|
|
344
|
-
method: undefined,
|
|
345
|
-
}) {
|
|
346
|
-
const { url=`/${id}/detail`, method = 'get', ...rest } = options
|
|
347
|
-
return request[method](`${this.modulePath}${url}`, params, rest)
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
/**
|
|
351
|
-
* 保存
|
|
352
|
-
* @param {object} data 保存参数
|
|
353
|
-
* @param {object} options 请求配置
|
|
354
|
-
* @returns {Promise<AxiosResponse<any>>} 保存结果
|
|
355
|
-
*/
|
|
356
|
-
save(data: any={}, options: RequestOptions = {
|
|
357
|
-
url: undefined,
|
|
358
|
-
method: undefined,
|
|
359
|
-
}) {
|
|
360
|
-
const { url=`/_create`, method = 'post', ...rest } = options
|
|
361
|
-
return request[method](`${this.modulePath}${url}`, data, rest)
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
/**
|
|
365
|
-
* 更新
|
|
366
|
-
* @param {object} data 更新参数
|
|
367
|
-
* @param {object} options 请求配置
|
|
368
|
-
* @returns {Promise<AxiosResponse<any>>} 更新结果
|
|
369
|
-
*/
|
|
370
|
-
update(data: any={}, options: RequestOptions = {
|
|
371
|
-
url: undefined,
|
|
372
|
-
method: undefined,
|
|
373
|
-
}) {
|
|
374
|
-
const { url=`/_update`, method = 'patch', ...rest } = options
|
|
375
|
-
return patch(`${this.modulePath}${url}`, data, rest)
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
/**
|
|
379
|
-
* 删除
|
|
380
|
-
* @param {string} id 删除ID
|
|
381
|
-
* @param {object} options 请求配置
|
|
382
|
-
* @returns {Promise<AxiosResponse<any>>} 删除结果
|
|
383
|
-
*/
|
|
384
|
-
delete(id: string, params?: any, options: RequestOptions = {
|
|
385
|
-
url: undefined,
|
|
386
|
-
method: undefined,
|
|
387
|
-
}) {
|
|
388
|
-
const { url=`/${id}`, method = 'post', ...rest } = options
|
|
389
|
-
return remove(`${this.modulePath}${url}`, params, rest)
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
post(...args) {
|
|
393
|
-
const [url, data, options] = args
|
|
394
|
-
return post(`${this.modulePath}${url}`, data, options)
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
get(...args) {
|
|
398
|
-
const [url, params, options] = args
|
|
399
|
-
return get(`${this.modulePath}${url}`, params, options)
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
put(...args) {
|
|
403
|
-
const [url, data, options] = args
|
|
404
|
-
return put(`${this.modulePath}${url}`, data, options)
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
patch(...args) {
|
|
408
|
-
const [url, data, options] = args
|
|
409
|
-
return patch(`${this.modulePath}${url}`, data, options)
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
remove(...args) {
|
|
413
|
-
const [url, params, options] = args
|
|
414
|
-
return remove(`${this.modulePath}${url}`, params, options)
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|