httpquick 1.1.7 → 1.1.8
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/HttpQuickBase-Djux5ry4.cjs +1 -0
- package/dist/{HttpQuickBase-Cbl4MwCJ.js → HttpQuickBase-YasFsQ_r.js} +3 -3
- package/dist/ajax.js +1 -1
- package/dist/fetch.js +1 -1
- package/dist/fibjs.cjs +1 -1
- package/dist/fibjs.js +1 -1
- package/dist/httpquick.umd.cjs +2 -2
- package/dist/node.cjs +1 -1
- package/dist/node.js +1 -1
- package/dist/uniapp.js +40 -52
- package/dist/xcx.js +1 -1
- package/package.json +1 -1
- package/dist/HttpQuickBase-gLupfP4c.cjs +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var h=Object.defineProperty;var p=(o,e,t)=>e in o?h(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var a=(o,e,t)=>p(o,typeof e!="symbol"?e+"":e,t);class c{constructor(e={}){a(this,"config",{});a(this,"requestId",0);a(this,"middlewares",[]);a(this,"onTimeoutError",function(e,t,s){console.error("http timeout",s),t.status=-4,t.statusText="TIMEOUT",e.responseType=="text"&&e.dataType=="json"?t.body={errno:-1,errmsg:(t.response.status??t.status)+", 网络请求超时!"}:t.body="网络请求超时!"});Object.assign(this.config,e)}updateConfig(e={}){Object.assign(this.config,e)}addMiddleware(e){this.middlewares.push(e.bind(this))}getDefaultRequest(){return{id:0,baseUrl:"",method:"GET",url:"/",headers:{},query:null,json:null,body:null,responseType:"text",dataType:"json",timeout:6e4,fullUrl:""}}getDefaultResponse(){return{status:-1,statusText:"NONE",headers:{},body:null}}obj2query(e){return typeof URLSearchParams<"u"?new URLSearchParams(e).toString():Object.entries(e).map(([t,s])=>encodeURIComponent(t)+"="+encodeURIComponent(s)).join("&")}fillUrl(e){e.fullUrl=(/^https?:\/\//.test(e.url)?e.url:e.baseUrl+e.url)+(e.query?"?"+this.obj2query(e.query):"")}fillHeaders(e){e.responseType=="text"&&e.dataType=="json"&&(e.headers.Accept="application/json")}fillBody(e){var t;(e.method=="POST"||e.method=="PUT")&&(e.filePath||(e.body!==null&&typeof FormData<"u"&&e.body instanceof FormData?typeof process<"u"&&(((t=process==null?void 0:process.versions)==null?void 0:t.node)||"0.0.0")!=="0.0.0"&&(e.headers["content-type"]=e.body.getContentType(),e.body=e.body.toStream()):e.body!==null&&typeof URLSearchParams<"u"&&e.body instanceof URLSearchParams?(e.headers["content-type"]="application/x-www-form-urlencoded; charset=utf-8",e.body=e.body.toString()):e.body!==null&&Array.isArray(e.body)?(e.headers["content-type"]="application/x-www-form-urlencoded; charset=utf-8",e.body=e.body.map(s=>encodeURIComponent(s.key)+"="+encodeURIComponent(s.val)).join("&")):e.body!==null&&Object.isObject(e.body)?(e.headers["content-type"]="application/x-www-form-urlencoded; charset=utf-8",e.body=this.obj2query(e.body)):e.json!==null?(e.headers["content-type"]="application/json; charset=utf-8",e.body=JSON.stringify(e.json)):(e.headers["content-type"]="application/x-www-form-urlencoded; charset=utf-8",e.body="")))}get(e){return this.request({...e,method:"GET"})}post(e){return this.request({...e,method:"POST"})}put(e){return this.request({...e,method:"PUT"})}del(e){return this.request({...e,method:"DELETE"})}upload(e){return new Error("unreachable")}download(e){return new Error("unreachable")}onNetworkError(e,t,s=null){console.error("http error",s),t.status=-1,t.statusText="NETWORK FAIL",e.responseType=="text"&&e.dataType=="json"?t.body={errno:-2,errmsg:(t.response.status??t.status)+", 网络请求错误!"}:t.body="网络请求错误!"}async request(e){const t=Object.assign(this.getDefaultRequest(),this.config,e);let s=this.getDefaultResponse();t.method=t.method.toUpperCase(),Object.defineProperties(t,{response:{value:s,enumerable:!1,configurable:!0,writable:!0}}),Object.defineProperties(s,{request:{value:t,enumerable:!1,configurable:!0,writable:!0},response:{value:null,enumerable:!1,configurable:!0,writable:!0},parent:{value:null,enumerable:!1,configurable:!0,writable:!0}}),this.fillUrl(t),this.fillHeaders(t),this.fillBody(t);let l=async(n,i)=>await this.send(n,i);for(let n=this.middlewares.length-1;n>=0;n--){const i=l,r=this.middlewares[n].bind(this);l=async(d,u)=>await r(d,u,i)}return await l(t,s),s}send(e,t){}install(e,t={}){e.config.globalProperties.$http=this;for(const s in t)this[s]=t[s]}globalMethods(){globalThis.httpGet=this.get.bind(this),globalThis.httpPost=this.post.bind(this),globalThis.httpPut=this.put.bind(this),globalThis.httpDelete=this.del.bind(this),globalThis.httpUpload=this.upload.bind(this),globalThis.httpDownload=this.download.bind(this)}}exports.HttpQuickBase=c;
|
|
@@ -12,7 +12,7 @@ class b {
|
|
|
12
12
|
a(this, "middlewares", []);
|
|
13
13
|
// 超时错误处理
|
|
14
14
|
a(this, "onTimeoutError", function(e, t, s) {
|
|
15
|
-
console.error("http timeout", s), t.status = -4, t.statusText = "TIMEOUT", e.responseType == "text" && e.dataType == "json" ? t.body = { errno: -1, errmsg: t.response.status + ", 网络请求超时!" } : t.body = "网络请求超时!";
|
|
15
|
+
console.error("http timeout", s), t.status = -4, t.statusText = "TIMEOUT", e.responseType == "text" && e.dataType == "json" ? t.body = { errno: -1, errmsg: (t.response.status ?? t.status) + ", 网络请求超时!" } : t.body = "网络请求超时!";
|
|
16
16
|
});
|
|
17
17
|
Object.assign(this.config, e);
|
|
18
18
|
}
|
|
@@ -92,8 +92,8 @@ class b {
|
|
|
92
92
|
return new Error("unreachable");
|
|
93
93
|
}
|
|
94
94
|
// 网络错误处理
|
|
95
|
-
onNetworkError(e, t, s) {
|
|
96
|
-
console.error("http error", s), t.status = -1, t.statusText = "NETWORK FAIL", e.responseType == "text" && e.dataType == "json" ? t.body = { errno: -2, errmsg: t.response.status + ", 网络请求错误!" } : t.body = "网络请求错误!";
|
|
95
|
+
onNetworkError(e, t, s = null) {
|
|
96
|
+
console.error("http error", s), t.status = -1, t.statusText = "NETWORK FAIL", e.responseType == "text" && e.dataType == "json" ? t.body = { errno: -2, errmsg: (t.response.status ?? t.status) + ", 网络请求错误!" } : t.body = "网络请求错误!";
|
|
97
97
|
}
|
|
98
98
|
// 请求
|
|
99
99
|
async request(e) {
|
package/dist/ajax.js
CHANGED
package/dist/fetch.js
CHANGED
package/dist/fibjs.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./HttpQuickBase-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./HttpQuickBase-Djux5ry4.cjs"),n=require("http"),d=require("util");class i extends o.HttpQuickBase{send(e,s){try{const t=n.request(e.method,e.fullUrl,{headers:e.headers,body:e.body||""});if(t.status!==200)return this.onNetworkError(e,s,null);s.status=t.statusCode,s.statusText=t.statusMessage;for(let a of t.headers.keys()){const r=t.headers.all(a);a=a.toLowerCase();for(const u of r)s.headers[a]=u}e.responseType=="text"&&e.dataType=="json"?s.body=Buffer.isBuffer(t.data)?JSON.parse(t.data.toString("utf8")):typeof t.data=="string"?JSON.parse(t.data):t.data:e.responseType=="text"?s.body=t.data.toString("utf8"):s.body=t.data}catch(t){return this.onTimeoutError(e,s,t)}}request(e){return d.sync(async s=>await super.request(s))(e)}}exports.HttpQuickBase=o.HttpQuickBase;exports.HttpQuick=i;
|
package/dist/fibjs.js
CHANGED
package/dist/httpquick.umd.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(a,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(a=typeof globalThis<"u"?globalThis:a||self,i(a.httpquick={}))})(this,function(a){"use strict";var f=Object.defineProperty;var b=(a,i,
|
|
2
|
-
`).map(n=>[decodeURIComponent(n.substring(0,n.indexOf(": ")).toLowerCase()),decodeURIComponent(n.substring(n.indexOf(": ")+2))])),e.responseType=="text"&&e.dataType=="json"?t.body=(JSON.parse0||JSON.parse)(s.responseText):e.responseType=="text"?t.body=s.responseText:t.body=s.response,o()}},e.method=="POST"||e.method=="PUT"?s.send(e.body):s.send(),
|
|
1
|
+
(function(a,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(a=typeof globalThis<"u"?globalThis:a||self,i(a.httpquick={}))})(this,function(a){"use strict";var f=Object.defineProperty;var b=(a,i,l)=>i in a?f(a,i,{enumerable:!0,configurable:!0,writable:!0,value:l}):a[i]=l;var d=(a,i,l)=>b(a,typeof i!="symbol"?i+"":i,l);class i{constructor(e={}){d(this,"config",{});d(this,"requestId",0);d(this,"middlewares",[]);d(this,"onTimeoutError",function(e,t,o){console.error("http timeout",o),t.status=-4,t.statusText="TIMEOUT",e.responseType=="text"&&e.dataType=="json"?t.body={errno:-1,errmsg:(t.response.status??t.status)+", 网络请求超时!"}:t.body="网络请求超时!"});Object.assign(this.config,e)}updateConfig(e={}){Object.assign(this.config,e)}addMiddleware(e){this.middlewares.push(e.bind(this))}getDefaultRequest(){return{id:0,baseUrl:"",method:"GET",url:"/",headers:{},query:null,json:null,body:null,responseType:"text",dataType:"json",timeout:6e4,fullUrl:""}}getDefaultResponse(){return{status:-1,statusText:"NONE",headers:{},body:null}}obj2query(e){return typeof URLSearchParams<"u"?new URLSearchParams(e).toString():Object.entries(e).map(([t,o])=>encodeURIComponent(t)+"="+encodeURIComponent(o)).join("&")}fillUrl(e){e.fullUrl=(/^https?:\/\//.test(e.url)?e.url:e.baseUrl+e.url)+(e.query?"?"+this.obj2query(e.query):"")}fillHeaders(e){e.responseType=="text"&&e.dataType=="json"&&(e.headers.Accept="application/json")}fillBody(e){var t;(e.method=="POST"||e.method=="PUT")&&(e.filePath||(e.body!==null&&typeof FormData<"u"&&e.body instanceof FormData?typeof process<"u"&&(((t=process==null?void 0:process.versions)==null?void 0:t.node)||"0.0.0")!=="0.0.0"&&(e.headers["content-type"]=e.body.getContentType(),e.body=e.body.toStream()):e.body!==null&&typeof URLSearchParams<"u"&&e.body instanceof URLSearchParams?(e.headers["content-type"]="application/x-www-form-urlencoded; charset=utf-8",e.body=e.body.toString()):e.body!==null&&Array.isArray(e.body)?(e.headers["content-type"]="application/x-www-form-urlencoded; charset=utf-8",e.body=e.body.map(o=>encodeURIComponent(o.key)+"="+encodeURIComponent(o.val)).join("&")):e.body!==null&&Object.isObject(e.body)?(e.headers["content-type"]="application/x-www-form-urlencoded; charset=utf-8",e.body=this.obj2query(e.body)):e.json!==null?(e.headers["content-type"]="application/json; charset=utf-8",e.body=JSON.stringify(e.json)):(e.headers["content-type"]="application/x-www-form-urlencoded; charset=utf-8",e.body="")))}get(e){return this.request({...e,method:"GET"})}post(e){return this.request({...e,method:"POST"})}put(e){return this.request({...e,method:"PUT"})}del(e){return this.request({...e,method:"DELETE"})}upload(e){return new Error("unreachable")}download(e){return new Error("unreachable")}onNetworkError(e,t,o=null){console.error("http error",o),t.status=-1,t.statusText="NETWORK FAIL",e.responseType=="text"&&e.dataType=="json"?t.body={errno:-2,errmsg:(t.response.status??t.status)+", 网络请求错误!"}:t.body="网络请求错误!"}async request(e){const t=Object.assign(this.getDefaultRequest(),this.config,e);let o=this.getDefaultResponse();t.method=t.method.toUpperCase(),Object.defineProperties(t,{response:{value:o,enumerable:!1,configurable:!0,writable:!0}}),Object.defineProperties(o,{request:{value:t,enumerable:!1,configurable:!0,writable:!0},response:{value:null,enumerable:!1,configurable:!0,writable:!0},parent:{value:null,enumerable:!1,configurable:!0,writable:!0}}),this.fillUrl(t),this.fillHeaders(t),this.fillBody(t);let r=async(s,n)=>await this.send(s,n);for(let s=this.middlewares.length-1;s>=0;s--){const n=r,u=this.middlewares[s].bind(this);r=async(h,c)=>await u(h,c,n)}return await r(t,o),o}send(e,t){}install(e,t={}){e.config.globalProperties.$http=this;for(const o in t)this[o]=t[o]}globalMethods(){globalThis.httpGet=this.get.bind(this),globalThis.httpPost=this.post.bind(this),globalThis.httpPut=this.put.bind(this),globalThis.httpDelete=this.del.bind(this),globalThis.httpUpload=this.upload.bind(this),globalThis.httpDownload=this.download.bind(this)}}class l extends i{send(e,t){let o=null,r=new Promise(n=>o=n);const s=t.response=new XMLHttpRequest;s.open(e.method,e.fullUrl,!0),s.timeout=e.timeout,s.withCredentials=e.withCredentials||!1,s.responseType=e.responseType;for(const[n,u]of Object.entries(e.headers))s.setRequestHeader(n,u);return s.ontimeout=n=>{this.onTimeoutError(e,t,n),o()},s.onerror=n=>{this.onNetworkError(e,t,n),o()},e.onprocess&&s.upload.addEventListener("progress",e.onprocess),s.onreadystatechange=()=>{if(!(s.readyState!==4||s.status==0)){if(s.status!=200)return this.onNetworkError(e,t,null),o();t.status=s.status,t.statusText=s.statusText,t.headers=Object.fromEntries(s.getAllResponseHeaders().trim().replaceAll("\r","").split(`
|
|
2
|
+
`).map(n=>[decodeURIComponent(n.substring(0,n.indexOf(": ")).toLowerCase()),decodeURIComponent(n.substring(n.indexOf(": ")+2))])),e.responseType=="text"&&e.dataType=="json"?t.body=(JSON.parse0||JSON.parse)(s.responseText):e.responseType=="text"?t.body=s.responseText:t.body=s.response,o()}},e.method=="POST"||e.method=="PUT"?s.send(e.body):s.send(),r}}a.HttpQuick=l,a.HttpQuickBase=i,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
|
package/dist/node.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("./HttpQuickBase-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("./HttpQuickBase-Djux5ry4.cjs");class u extends i.HttpQuickBase{send(t,o){let n=null,r=new Promise(s=>n=s);const e=o.response=new XMLHttpRequest;e.open(t.method,t.fullUrl,!0),e.timeout=t.timeout,e.withCredentials=t.withCredentials||!1,e.responseType=t.responseType;for(const[s,a]of Object.entries(t.headers))e.setRequestHeader(s,a);return e.ontimeout=s=>{this.onTimeoutError(t,o,s),n()},e.onerror=s=>{this.onNetworkError(t,o,s),n()},e.onreadystatechange=()=>{if(!(e.readyState!==4||e.status==0)){if(e.status!=200)return this.onNetworkError(t,o,null),n();o.status=e.status,o.statusText=e.statusText,o.headers=Object.fromEntries(e.getAllResponseHeaders().trim().replaceAll("\r","").split(`
|
|
2
2
|
`).map(s=>[decodeURIComponent(s.substring(0,s.indexOf(": ")).toLowerCase()),decodeURIComponent(s.substring(s.indexOf(": ")+2))])),t.responseType=="text"&&t.dataType=="json"?o.body=JSON.parse(e.responseText):t.responseType=="text"?o.body=e.responseText:o.body=e.response,n()}},t.method=="POST"||t.method=="PUT"?e.send(t.body):e.send(),r}}exports.HttpQuickBase=i.HttpQuickBase;exports.HttpQuick=u;
|
package/dist/node.js
CHANGED
package/dist/uniapp.js
CHANGED
|
@@ -1,67 +1,55 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var i = (
|
|
4
|
-
import { H as f } from "./HttpQuickBase-
|
|
5
|
-
class
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
// 平台
|
|
10
|
-
i(this, "platform", null);
|
|
11
|
-
if (typeof uni < "u")
|
|
12
|
-
this.setPlatform(uni), this.setPlatform(uni.$h5);
|
|
13
|
-
else if (typeof wx < "u")
|
|
14
|
-
this.setPlatform(wx);
|
|
15
|
-
else
|
|
16
|
-
throw new Error("unknown platform");
|
|
17
|
-
}
|
|
18
|
-
// 设置平台
|
|
19
|
-
setPlatform(t = null) {
|
|
20
|
-
return this.platform = t, this;
|
|
1
|
+
var p = Object.defineProperty;
|
|
2
|
+
var l = (d, o, e) => o in d ? p(d, o, { enumerable: !0, configurable: !0, writable: !0, value: e }) : d[o] = e;
|
|
3
|
+
var i = (d, o, e) => l(d, typeof o != "symbol" ? o + "" : o, e);
|
|
4
|
+
import { H as f } from "./HttpQuickBase-YasFsQ_r.js";
|
|
5
|
+
class y extends f {
|
|
6
|
+
constructor() {
|
|
7
|
+
super(...arguments);
|
|
8
|
+
i(this, "methods", {});
|
|
21
9
|
}
|
|
22
10
|
// 上传文件
|
|
23
|
-
upload(
|
|
24
|
-
return this.request({ ...
|
|
11
|
+
upload(e) {
|
|
12
|
+
return this.request({ ...e, method: "POST", func: "uploadFile" });
|
|
25
13
|
}
|
|
26
14
|
// 下载文件
|
|
27
|
-
download(
|
|
28
|
-
return this.request({ ...
|
|
15
|
+
download(e) {
|
|
16
|
+
return this.request({ ...e, method: "POST", func: "downloadFile" });
|
|
29
17
|
}
|
|
30
18
|
// 发送请求
|
|
31
|
-
async send(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
19
|
+
async send(e, a) {
|
|
20
|
+
var n;
|
|
21
|
+
let r = null, u = new Promise((s) => r = s);
|
|
22
|
+
this.methods[e.func || "request"]({
|
|
23
|
+
method: e.method,
|
|
24
|
+
url: e.fullUrl,
|
|
25
|
+
header: e.headers,
|
|
26
|
+
data: e.body,
|
|
27
|
+
formData: e.formData,
|
|
39
28
|
// upload fromData
|
|
40
|
-
name:
|
|
29
|
+
name: e.name || "file",
|
|
41
30
|
// upload file param name
|
|
42
|
-
filePath:
|
|
31
|
+
filePath: e.filePath,
|
|
43
32
|
// upload file path
|
|
44
|
-
responseType:
|
|
45
|
-
dataType:
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
fail: (a) => n({ ...a, err: !0 })
|
|
33
|
+
responseType: e.responseType,
|
|
34
|
+
dataType: e.dataType,
|
|
35
|
+
timeout: e.timeout,
|
|
36
|
+
success: (s) => r({ ...s, err: !1 }),
|
|
37
|
+
fail: (s) => r({ ...s, err: !0 })
|
|
50
38
|
});
|
|
51
|
-
const
|
|
52
|
-
if (
|
|
53
|
-
return this.onTimeoutError(
|
|
54
|
-
if (
|
|
55
|
-
return this.onNetworkError(
|
|
56
|
-
if (
|
|
57
|
-
return this.onNetworkError(
|
|
58
|
-
|
|
59
|
-
for (const
|
|
60
|
-
|
|
61
|
-
|
|
39
|
+
const t = a.response = await u;
|
|
40
|
+
if (a.err = t.err, t.err && t.errMsg == "request:fail timeout")
|
|
41
|
+
return this.onTimeoutError(e, a);
|
|
42
|
+
if (t.err)
|
|
43
|
+
return this.onNetworkError(e, a);
|
|
44
|
+
if (t.statusCode != 200)
|
|
45
|
+
return this.onNetworkError(e, a);
|
|
46
|
+
a.status = t.statusCode, a.statusText = "OK";
|
|
47
|
+
for (const s in t.header)
|
|
48
|
+
a.headers[s.toLowerCase()] = t.header[s];
|
|
49
|
+
e.responseType == "text" && e.dataType == "json" && typeof t.data == "string" ? a.body = (JSON.parse0 || JSON.parse)(t.data) : e.responseType == "text" && e.dataType == "json" && typeof t.data == "object" ? a.body = ((n = t.data) == null ? void 0 : n.object2model()) ?? t.data : (e.responseType == "text", a.body = t.data);
|
|
62
50
|
}
|
|
63
51
|
}
|
|
64
52
|
export {
|
|
65
|
-
|
|
53
|
+
y as HttpQuick,
|
|
66
54
|
f as HttpQuickBase
|
|
67
55
|
};
|
package/dist/xcx.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var l = Object.defineProperty;
|
|
2
2
|
var d = (r, s, t) => s in r ? l(r, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[s] = t;
|
|
3
3
|
var i = (r, s, t) => d(r, typeof s != "symbol" ? s + "" : s, t);
|
|
4
|
-
import { H as f } from "./HttpQuickBase-
|
|
4
|
+
import { H as f } from "./HttpQuickBase-YasFsQ_r.js";
|
|
5
5
|
class c extends f {
|
|
6
6
|
// 构造函数
|
|
7
7
|
constructor(t = {}) {
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var h=Object.defineProperty;var p=(o,e,t)=>e in o?h(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var a=(o,e,t)=>p(o,typeof e!="symbol"?e+"":e,t);class c{constructor(e={}){a(this,"config",{});a(this,"requestId",0);a(this,"middlewares",[]);a(this,"onTimeoutError",function(e,t,s){console.error("http timeout",s),t.status=-4,t.statusText="TIMEOUT",e.responseType=="text"&&e.dataType=="json"?t.body={errno:-1,errmsg:t.response.status+", 网络请求超时!"}:t.body="网络请求超时!"});Object.assign(this.config,e)}updateConfig(e={}){Object.assign(this.config,e)}addMiddleware(e){this.middlewares.push(e.bind(this))}getDefaultRequest(){return{id:0,baseUrl:"",method:"GET",url:"/",headers:{},query:null,json:null,body:null,responseType:"text",dataType:"json",timeout:6e4,fullUrl:""}}getDefaultResponse(){return{status:-1,statusText:"NONE",headers:{},body:null}}obj2query(e){return typeof URLSearchParams<"u"?new URLSearchParams(e).toString():Object.entries(e).map(([t,s])=>encodeURIComponent(t)+"="+encodeURIComponent(s)).join("&")}fillUrl(e){e.fullUrl=(/^https?:\/\//.test(e.url)?e.url:e.baseUrl+e.url)+(e.query?"?"+this.obj2query(e.query):"")}fillHeaders(e){e.responseType=="text"&&e.dataType=="json"&&(e.headers.Accept="application/json")}fillBody(e){var t;(e.method=="POST"||e.method=="PUT")&&(e.filePath||(e.body!==null&&typeof FormData<"u"&&e.body instanceof FormData?typeof process<"u"&&(((t=process==null?void 0:process.versions)==null?void 0:t.node)||"0.0.0")!=="0.0.0"&&(e.headers["content-type"]=e.body.getContentType(),e.body=e.body.toStream()):e.body!==null&&typeof URLSearchParams<"u"&&e.body instanceof URLSearchParams?(e.headers["content-type"]="application/x-www-form-urlencoded; charset=utf-8",e.body=e.body.toString()):e.body!==null&&Array.isArray(e.body)?(e.headers["content-type"]="application/x-www-form-urlencoded; charset=utf-8",e.body=e.body.map(s=>encodeURIComponent(s.key)+"="+encodeURIComponent(s.val)).join("&")):e.body!==null&&Object.isObject(e.body)?(e.headers["content-type"]="application/x-www-form-urlencoded; charset=utf-8",e.body=this.obj2query(e.body)):e.json!==null?(e.headers["content-type"]="application/json; charset=utf-8",e.body=JSON.stringify(e.json)):(e.headers["content-type"]="application/x-www-form-urlencoded; charset=utf-8",e.body="")))}get(e){return this.request({...e,method:"GET"})}post(e){return this.request({...e,method:"POST"})}put(e){return this.request({...e,method:"PUT"})}del(e){return this.request({...e,method:"DELETE"})}upload(e){return new Error("unreachable")}download(e){return new Error("unreachable")}onNetworkError(e,t,s){console.error("http error",s),t.status=-1,t.statusText="NETWORK FAIL",e.responseType=="text"&&e.dataType=="json"?t.body={errno:-2,errmsg:t.response.status+", 网络请求错误!"}:t.body="网络请求错误!"}async request(e){const t=Object.assign(this.getDefaultRequest(),this.config,e);let s=this.getDefaultResponse();t.method=t.method.toUpperCase(),Object.defineProperties(t,{response:{value:s,enumerable:!1,configurable:!0,writable:!0}}),Object.defineProperties(s,{request:{value:t,enumerable:!1,configurable:!0,writable:!0},response:{value:null,enumerable:!1,configurable:!0,writable:!0},parent:{value:null,enumerable:!1,configurable:!0,writable:!0}}),this.fillUrl(t),this.fillHeaders(t),this.fillBody(t);let i=async(n,l)=>await this.send(n,l);for(let n=this.middlewares.length-1;n>=0;n--){const l=i,r=this.middlewares[n].bind(this);i=async(d,u)=>await r(d,u,l)}return await i(t,s),s}send(e,t){}install(e,t={}){e.config.globalProperties.$http=this;for(const s in t)this[s]=t[s]}globalMethods(){globalThis.httpGet=this.get.bind(this),globalThis.httpPost=this.post.bind(this),globalThis.httpPut=this.put.bind(this),globalThis.httpDelete=this.del.bind(this),globalThis.httpUpload=this.upload.bind(this),globalThis.httpDownload=this.download.bind(this)}}exports.HttpQuickBase=c;
|