utils-uniapp 1.1.2 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/http/index.d.ts +20 -21
- package/lib/http/index.js +1 -1
- package/package.json +1 -1
package/lib/http/index.d.ts
CHANGED
|
@@ -63,10 +63,7 @@ type UniAnyResult = UniRequestResult | UniUploadResult | UniDownloadResult;
|
|
|
63
63
|
* @param opts 请求参数,可以传入 cancelToken 用于请求取消
|
|
64
64
|
* @returns uni接口回调结果Promise
|
|
65
65
|
*/
|
|
66
|
-
export declare function promisifyRequest<T = UniAnyResult>(
|
|
67
|
-
type: 'request' | 'uploadFile' | 'downloadFile',
|
|
68
|
-
opts: Record<string, any>
|
|
69
|
-
): Promise<T>;
|
|
66
|
+
export declare function promisifyRequest<T = UniAnyResult>(type: 'request' | 'uploadFile' | 'downloadFile', opts: Record<string, any>): Promise<T>;
|
|
70
67
|
|
|
71
68
|
/**
|
|
72
69
|
* 开启loading计数
|
|
@@ -125,7 +122,9 @@ export interface NormalizeOpts {
|
|
|
125
122
|
showError: boolean;
|
|
126
123
|
/** 是否开启接口缓存 */
|
|
127
124
|
isCache: boolean;
|
|
128
|
-
/** 缓存key
|
|
125
|
+
/** 缓存key前缀;不传/空字符串/undefined默认使用api;设置cacheKey时此配置无效 */
|
|
126
|
+
cachePrefix?: string;
|
|
127
|
+
/** 完整缓存key,传入则完全覆盖,优先级高于cachePrefix */
|
|
129
128
|
cacheKey: string;
|
|
130
129
|
/** 缓存过期时间,单位ms */
|
|
131
130
|
cacheExpire: number;
|
|
@@ -148,16 +147,29 @@ export interface NormalizeOpts {
|
|
|
148
147
|
* 经过内部 normalizeOpts 之后转换为 NormalizeOpts
|
|
149
148
|
*/
|
|
150
149
|
export interface UserRequestOpts {
|
|
150
|
+
/** post请求是否使用urlencoded表单格式 */
|
|
151
151
|
qs?: boolean;
|
|
152
|
+
/** 是否开启默认错误弹窗提示 */
|
|
152
153
|
showError?: boolean;
|
|
154
|
+
/** 是否开启接口缓存 */
|
|
153
155
|
isCache?: boolean;
|
|
156
|
+
/** 缓存key前缀;不传/空字符串/undefined默认使用api;设置cacheKey时此配置无效 */
|
|
157
|
+
cachePrefix?: string;
|
|
158
|
+
/** 完整缓存key,传入则完全覆盖,优先级高于cachePrefix */
|
|
154
159
|
cacheKey?: string;
|
|
160
|
+
/** 缓存过期时间,单位ms */
|
|
155
161
|
cacheExpire?: number;
|
|
162
|
+
/** loading提示文案 */
|
|
156
163
|
loadingText?: string;
|
|
164
|
+
/** 是否开启loading */
|
|
157
165
|
loading?: boolean;
|
|
166
|
+
/** 请求前置拦截器,支持单个函数或函数数组 */
|
|
158
167
|
reqInterceptor?: UserReqInterceptor;
|
|
168
|
+
/** 响应拦截器,支持单个函数或函数数组 */
|
|
159
169
|
resInterceptor?: UserResInterceptor;
|
|
170
|
+
/** 错误拦截器,支持单个函数或函数数组 */
|
|
160
171
|
errInterceptor?: UserErrInterceptor;
|
|
172
|
+
/** 允许扩展自定义字段 */
|
|
161
173
|
[key: string]: any;
|
|
162
174
|
}
|
|
163
175
|
|
|
@@ -200,10 +212,7 @@ interface RequestInstance {
|
|
|
200
212
|
* @param defaultOpts 全局默认扩展配置 loading、缓存、拦截器等,拦截器支持数组
|
|
201
213
|
* @returns RequestInstance 请求实例
|
|
202
214
|
*/
|
|
203
|
-
export declare function createRequest(
|
|
204
|
-
defaultConfig: Record<string, any>,
|
|
205
|
-
defaultOpts?: UserRequestOpts
|
|
206
|
-
): RequestInstance;
|
|
215
|
+
export declare function createRequest(defaultConfig: Record<string, any>, defaultOpts?: UserRequestOpts): RequestInstance;
|
|
207
216
|
|
|
208
217
|
/**
|
|
209
218
|
* 创建上传文件实例
|
|
@@ -214,12 +223,7 @@ export declare function createRequest(
|
|
|
214
223
|
export declare function createUploadFile(
|
|
215
224
|
defaultConfig: Record<string, any>,
|
|
216
225
|
defaultOpts?: UserRequestOpts
|
|
217
|
-
): (
|
|
218
|
-
url: string,
|
|
219
|
-
params: { filePath: string; keyName?: string; [key: string]: any },
|
|
220
|
-
config?: Record<string, any>,
|
|
221
|
-
options?: UserRequestOpts
|
|
222
|
-
) => Promise<any>;
|
|
226
|
+
): (url: string, params: { filePath: string; keyName?: string; [key: string]: any }, config?: Record<string, any>, options?: UserRequestOpts) => Promise<any>;
|
|
223
227
|
|
|
224
228
|
/**
|
|
225
229
|
* 创建下载文件实例
|
|
@@ -230,9 +234,4 @@ export declare function createUploadFile(
|
|
|
230
234
|
export declare function createDownloadFile(
|
|
231
235
|
defaultConfig: Record<string, any>,
|
|
232
236
|
defaultOpts?: UserRequestOpts
|
|
233
|
-
): (
|
|
234
|
-
url: string,
|
|
235
|
-
params?: Record<string, any>,
|
|
236
|
-
config?: Record<string, any>,
|
|
237
|
-
options?: UserRequestOpts
|
|
238
|
-
) => Promise<any>;
|
|
237
|
+
): (url: string, params?: Record<string, any>, config?: Record<string, any>, options?: UserRequestOpts) => Promise<any>;
|
package/lib/http/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const C=require("../index-b81565a7.js"),A=require("../object/index.js"),O=Object.freeze({JSON:"application/json;charset=UTF-8",TEXT:"text/plain;charset=UTF-8",FORM_URLENCODED:"application/x-www-form-urlencoded;charset=UTF-8",FORM_DATA:"multipart/form-data"}),m={caches:Object.create(null),add(e,t){this.caches[e]=t},delete(e){Reflect.deleteProperty(this.caches,e)},_cancel(e){const t=this.caches[e];typeof(t==null?void 0:t.abort)=="function"&&t.abort()},cancelAll(){Object.values(this.caches).forEach(e=>{typeof(e==null?void 0:e.abort)=="function"&&e.abort()}),this.caches=Object.create(null)},source(){const e=`${Date.now()}-${C.getRandomStr(16)}`;return{token:e,cancel:()=>this._cancel(e)}}};function
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const C=require("../index-b81565a7.js"),A=require("../object/index.js"),O=Object.freeze({JSON:"application/json;charset=UTF-8",TEXT:"text/plain;charset=UTF-8",FORM_URLENCODED:"application/x-www-form-urlencoded;charset=UTF-8",FORM_DATA:"multipart/form-data"}),m={caches:Object.create(null),add(e,t){this.caches[e]=t},delete(e){Reflect.deleteProperty(this.caches,e)},_cancel(e){const t=this.caches[e];typeof(t==null?void 0:t.abort)=="function"&&t.abort()},cancelAll(){Object.values(this.caches).forEach(e=>{typeof(e==null?void 0:e.abort)=="function"&&e.abort()}),this.caches=Object.create(null)},source(){const e=`${Date.now()}-${C.getRandomStr(16)}`;return{token:e,cancel:()=>this._cancel(e)}}};function x(e,t){return new Promise((c,n)=>{const r=uni[e]({...t,success(o){if(o.statusCode>=200&&o.statusCode<300)return c(o);n({type:"fail",res:o})},fail(o){n({type:"fail",res:o})},complete(){t.cancelToken&&m.delete(t.cancelToken)}});t.cancelToken&&m.add(t.cancelToken,r)})}let R=0,v=!1;function N(e){v||(v=!0,uni.showLoading({title:e,mask:!0}))}function P(){v&&(v=!1,uni.hideLoading())}function j(){let e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:"加载中...";R++,R===1&&N(e)}function D(){R>0&&R--,R===0&&P()}function _(e,t){return function(c,n,r,o){return e(c,t,n,r,o)}}function w(e){return typeof e=="function"?e:()=>e||{}}function k(e){let t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},c=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{};const n=w(t.headers)(),r=w(c.headers)(),o={...t,...c,headers:{...n,...r}},{headers:a,...s}=o;return{urlRes:e.startsWith("http")?e:o.baseURL?o.baseURL+e:e,headers:a,configRes:s}}function U(e){return typeof e=="function"?[e]:Array.isArray(e)?e.filter(t=>typeof t=="function"):[]}function F(e){let t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},c=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{};const n={qs:!0,showError:!0,isCache:!1,cacheExpire:1e3*60*60*1,loadingText:e.loadingText,loading:!0,reqInterceptor:r=>r,resInterceptor:r=>r,errInterceptor:()=>{},...t,...c};if(!n.cacheKey){const r=(n.cachePrefix||"api").replace(/:$/,"");n.cacheKey=`${r}:${e.url}`}return["reqInterceptor","resInterceptor","errInterceptor"].forEach(r=>{const o=[...U(t[r]),...U(c[r])];n[r]=o.length?C.composeAsync(...o):a=>a}),n}function q(e,t){const c=Array.from(t);return()=>e(...A.cloneDeepWithDescriptors(c))}function E(e,t,c,n,r){return b.apply(this,arguments)}function b(){return b=C._asyncToGenerator(function*(e,t,c,n,r){let o;n.loading&&(j(n.loadingText),o=()=>{D(),o=null});try{var a;const u=yield n.reqInterceptor(c,n),l=yield e(t,u);return(a=o)===null||a===void 0||a(),yield n.resInterceptor(l,n,r)}catch(u){var s;(s=o)===null||s===void 0||s();const l=yield n.errInterceptor(u,n);return l!==void 0?l:Promise.reject(u)}}),b.apply(this,arguments)}function S(e,t){function c(n,r,o,a,s){const u=m.source(),l=q(c,arguments),i=F({url:n,loadingText:"加载中..."},t,s),{urlRes:y,headers:T,configRes:h}=k(n,e,a);if(i.isCache){const d=C.getCache(i.cacheKey);if(d)return Promise.resolve(d)}const p=r.toUpperCase()==="GET"?O.JSON:i.qs?O.FORM_URLENCODED:O.JSON,g={cancelToken:u.token,method:r,url:y,data:o,header:{"Content-Type":p,...T},...h};return E(x,"request",g,i,l).then(d=>(i.isCache&&C.setCache(i.cacheKey,d,i.cacheExpire),d))}return c.get=_(c,"get"),c.post=_(c,"post"),c}function L(e,t){return function c(n,r,o,a){const s=m.source(),u=q(c,arguments),l=F({loadingText:"上传中..."},t,a),{urlRes:i,headers:y,configRes:T}=k(n,e,o),{keyName:h="file",filePath:f,...p}={keyName:"file",...r};if(!f||!f.startsWith("http://tmp")&&!f.startsWith("wxfile://tmp"))return Promise.resolve({data:f});const g={cancelToken:s.token,url:i,filePath:f,name:h,formData:p,header:{...y},...T};return E(x,"uploadFile",g,l,u)}}function $(e,t){return function c(n,r,o,a){const s=m.source(),u=q(c,arguments),l=F({loadingText:"下载中..."},t,a),{urlRes:i,headers:y,configRes:T}=k(n,e,o);let h=i;if(r&&typeof r=="object"){const p=Object.entries(r).filter(g=>{let[,d]=g;return d!=null&&d!==""}).map(g=>{let[d,I]=g;return`${encodeURIComponent(d)}=${encodeURIComponent(I)}`}).join("&");p&&(h+=(h.includes("?")?"&":"?")+p)}const f={cancelToken:s.token,url:h,header:{...y},...T};return E(x,"downloadFile",f,l,u)}}exports.CONTENT_TYPES=O;exports.CancelToken=m;exports.createDownloadFile=$;exports.createRequest=S;exports.createUploadFile=L;exports.endLoading=D;exports.promisifyRequest=x;exports.startLoading=j;
|