@hyusuyi/rq 0.1.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/es/fetch.d.ts +138 -0
- package/es/fetch.d.ts.map +1 -0
- package/es/fetch.js +555 -0
- package/es/index.d.ts +2 -0
- package/es/index.d.ts.map +1 -0
- package/es/index.js +1 -0
- package/lib/fetch.d.ts +138 -0
- package/lib/fetch.d.ts.map +1 -0
- package/lib/fetch.js +353 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +23 -0
- package/package.json +36 -0
package/es/fetch.d.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
export declare const isObject: (oj: unknown) => boolean;
|
|
2
|
+
export declare const isFunction: (oj: unknown) => boolean;
|
|
3
|
+
export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS" | "get" | "post" | "put" | "delete" | "patch" | "head" | "options" | undefined;
|
|
4
|
+
export type JsonValue = string | number | boolean | null | JsonObject | JsonArray;
|
|
5
|
+
export type JsonObject = {
|
|
6
|
+
[key: string]: JsonValue;
|
|
7
|
+
};
|
|
8
|
+
export type JsonArray = JsonValue[];
|
|
9
|
+
export type RequestData = unknown;
|
|
10
|
+
export interface HttpClientConfig {
|
|
11
|
+
/** 请求的基础 URL */
|
|
12
|
+
baseUrl?: string;
|
|
13
|
+
/** 流数据响应头类型关键字 */
|
|
14
|
+
blobContentTypes?: readonly string[];
|
|
15
|
+
/** 请求头配置 */
|
|
16
|
+
headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
|
|
17
|
+
/** 响应数据状态码的 key,默认: 'code' */
|
|
18
|
+
codeKey?: string;
|
|
19
|
+
/** 响应数据的 data key,默认: 'data' */
|
|
20
|
+
dataKey?: string;
|
|
21
|
+
/** 响应消息的 key,默认: 'message' */
|
|
22
|
+
messageKey?: string;
|
|
23
|
+
/** 是否默认返回 data 层数据,默认: false */
|
|
24
|
+
returnData?: boolean;
|
|
25
|
+
/** 默认请求方法,默认: 'GET' */
|
|
26
|
+
defaultMethod?: HttpMethod;
|
|
27
|
+
/** 请求超时时间(ms),默认: 30000 */
|
|
28
|
+
timeout?: number;
|
|
29
|
+
/** 状态码配置 */
|
|
30
|
+
codes?: {
|
|
31
|
+
/** 成功状态码列表 */
|
|
32
|
+
success?: readonly number[];
|
|
33
|
+
/** 需要登出的状态码列表 */
|
|
34
|
+
logout?: readonly number[];
|
|
35
|
+
/** 忽略错误的状态码列表 */
|
|
36
|
+
ignoreError?: readonly number[];
|
|
37
|
+
};
|
|
38
|
+
/** 错误处理回调 */
|
|
39
|
+
onError?: (error: HttpError) => void | Promise<void>;
|
|
40
|
+
/** 登出回调 */
|
|
41
|
+
onLogout?: (error: HttpError) => void | Promise<void>;
|
|
42
|
+
onSuccess?: (data: any) => void | Promise<void>;
|
|
43
|
+
/** 请求拦截器 */
|
|
44
|
+
requestInterceptor?: (url: string, options: RequestInit) => Promise<void> | void;
|
|
45
|
+
/** 响应拦截器 */
|
|
46
|
+
responseInterceptor?: <T>(response: HttpResponse<T>) => Promise<HttpResponse<T>> | HttpResponse<T>;
|
|
47
|
+
}
|
|
48
|
+
export interface RequestOptions extends Omit<RequestInit, "body" | "method"> {
|
|
49
|
+
/** 请求方法 */
|
|
50
|
+
method?: HttpMethod;
|
|
51
|
+
/** 请求体数据(对象会自动转为 JSON) */
|
|
52
|
+
json?: RequestData;
|
|
53
|
+
/** URL 查询参数 */
|
|
54
|
+
params?: Record<string, string | number | boolean | null | undefined>;
|
|
55
|
+
/** 是否忽略当前请求的错误,不触发 onError */
|
|
56
|
+
ignoreError?: boolean;
|
|
57
|
+
/** 是否返回 data 层数据 */
|
|
58
|
+
returnData?: boolean;
|
|
59
|
+
/** 请求超时时间(ms) */
|
|
60
|
+
timeout?: number;
|
|
61
|
+
/** 自定义错误回调(仅本次请求) */
|
|
62
|
+
onError?: (error: HttpError) => void;
|
|
63
|
+
/** 自定义登出回调(仅本次请求) */
|
|
64
|
+
onLogout?: (error: HttpError) => void;
|
|
65
|
+
/** 自定义成功回调(仅本次请求) */
|
|
66
|
+
onSuccess?: (data: any) => void;
|
|
67
|
+
}
|
|
68
|
+
export interface HttpResponse<T = any> {
|
|
69
|
+
[key: string]: any;
|
|
70
|
+
data?: T;
|
|
71
|
+
}
|
|
72
|
+
export interface BlobResponse {
|
|
73
|
+
data: Blob;
|
|
74
|
+
filename?: string;
|
|
75
|
+
response: Response;
|
|
76
|
+
}
|
|
77
|
+
export declare class HttpError extends Error {
|
|
78
|
+
code?: number | undefined;
|
|
79
|
+
response?: any;
|
|
80
|
+
statusCode?: number | undefined;
|
|
81
|
+
constructor(message: string, code?: number | undefined, response?: any, statusCode?: number | undefined);
|
|
82
|
+
}
|
|
83
|
+
export declare class HttpClient {
|
|
84
|
+
private configure;
|
|
85
|
+
constructor(configure?: HttpClientConfig);
|
|
86
|
+
/**
|
|
87
|
+
* 更新客户端配置
|
|
88
|
+
*/
|
|
89
|
+
config(configure: HttpClientConfig): void;
|
|
90
|
+
get<T = any>(url: string, options?: Omit<RequestOptions, "method">): Promise<T>;
|
|
91
|
+
post<T = any>(url: string, options?: Omit<RequestOptions, "method">): Promise<T>;
|
|
92
|
+
put<T = any>(url: string, options?: Omit<RequestOptions, "method">): Promise<T>;
|
|
93
|
+
delete<T = any>(url: string, options?: Omit<RequestOptions, "method">): Promise<T>;
|
|
94
|
+
patch<T = any>(url: string, options?: Omit<RequestOptions, "method">): Promise<T>;
|
|
95
|
+
request<T = any>(url: string, options?: RequestOptions): Promise<T>;
|
|
96
|
+
/**
|
|
97
|
+
* 构建完整 URL
|
|
98
|
+
*/
|
|
99
|
+
private buildUrl;
|
|
100
|
+
/**
|
|
101
|
+
* 添加查询参数
|
|
102
|
+
*/
|
|
103
|
+
private appendParams;
|
|
104
|
+
/**
|
|
105
|
+
* 构建请求头
|
|
106
|
+
*/
|
|
107
|
+
private buildHeaders;
|
|
108
|
+
/**
|
|
109
|
+
* 带超时的 fetch 请求
|
|
110
|
+
*/
|
|
111
|
+
private fetchWithTimeout;
|
|
112
|
+
/**
|
|
113
|
+
* 处理响应数据
|
|
114
|
+
*/
|
|
115
|
+
private handleResponse;
|
|
116
|
+
/**
|
|
117
|
+
* 从响应头提取文件名
|
|
118
|
+
*/
|
|
119
|
+
private extractFilename;
|
|
120
|
+
/**
|
|
121
|
+
* 统一错误处理
|
|
122
|
+
*/
|
|
123
|
+
private handleError;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* 下载文件(用于 Blob 响应)
|
|
127
|
+
* @example
|
|
128
|
+
* const response = await http.get('/api/download');
|
|
129
|
+
* downloadfile(response);
|
|
130
|
+
*/
|
|
131
|
+
export declare function downloadfile(response: BlobResponse | {
|
|
132
|
+
data: Blob;
|
|
133
|
+
filename?: string;
|
|
134
|
+
}): void;
|
|
135
|
+
export declare function createHttpClient(config?: HttpClientConfig): HttpClient;
|
|
136
|
+
export declare const rq: HttpClient;
|
|
137
|
+
export default HttpClient;
|
|
138
|
+
//# sourceMappingURL=fetch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["fetch.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,QAAQ,OAAQ,OAAO,YAA6D,CAAC;AAClG,eAAO,MAAM,UAAU,OAAQ,OAAO,YACsB,CAAC;AAY7D,MAAM,MAAM,UAAU,GAClB,KAAK,GACL,MAAM,GACN,KAAK,GACL,QAAQ,GACR,OAAO,GACP,MAAM,GACN,SAAS,GACT,KAAK,GACL,MAAM,GACN,KAAK,GACL,QAAQ,GACR,OAAO,GACP,MAAM,GACN,SAAS,GACT,SAAS,CAAC;AACd,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,UAAU,GAAG,SAAS,CAAC;AAClF,MAAM,MAAM,UAAU,GAAG;IAAE,CAAC,G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,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,CAAC,CAAC;IA8F7E;;OAEG;IACH,OAAO,CAAC,QAAQ;IAmBhB;;OAEG;IACH,OAAO,CAAC,YAAY;IAoBpB;;OAEG;YACW,YAAY;IA8B1B;;OAEG;YACW,gBAAgB;IAwB9B;;OAEG;YACW,cAAc;IA2D5B;;OAEG;IACH,OAAO,CAAC,eAAe;IAiBvB;;OAEG;IACH,OAAO,CAAC,WAAW;CA2BpB;AAID;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,YAAY,GAAG;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAsB7F;AAED,wBAAgB,gBAAgB,CAAC,MAAM,CAAC,EAAE,gBAAgB,GAAG,UAAU,CAEtE;AACD,eAAO,MAAM,EAAE,YAAmB,CAAC;AACnC,eAAe,UAAU,CAAC"}
|
package/es/fetch.js
ADDED
|
@@ -0,0 +1,555 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
var _excluded = ["json", "params", "ignoreError", "returnData", "timeout", "onError", "onLogout", "onSuccess"];
|
|
3
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
4
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
5
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
6
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
7
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
8
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
9
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
10
|
+
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
11
|
+
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
12
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
13
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
14
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
15
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
16
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
17
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
18
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
19
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
20
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
21
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
22
|
+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
23
|
+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
24
|
+
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
25
|
+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
26
|
+
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
|
|
27
|
+
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
|
|
28
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
29
|
+
function _isNativeFunction(fn) { try { return Function.toString.call(fn).indexOf("[native code]") !== -1; } catch (e) { return typeof fn === "function"; } }
|
|
30
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
31
|
+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
32
|
+
export var isObject = function isObject(oj) {
|
|
33
|
+
return Object.prototype.toString.call(oj) === "[object Object]";
|
|
34
|
+
};
|
|
35
|
+
export var isFunction = function isFunction(oj) {
|
|
36
|
+
return Object.prototype.toString.call(oj) === "[object Function]";
|
|
37
|
+
};
|
|
38
|
+
var DEFAULT_SUCCESS_CODES = [200];
|
|
39
|
+
var DEFAULT_LOGOUT_CODES = [401, 403];
|
|
40
|
+
var DEFAULT_METHOD = "GET";
|
|
41
|
+
var DEFAULT_CODE_KEY = "code";
|
|
42
|
+
var DEFAULT_DATA_KEY = "data";
|
|
43
|
+
var DEFAULT_MESSAGE_KEY = "message";
|
|
44
|
+
var DEFAULT_CONTENT_TYPE = "application/json;charset=UTF-8";
|
|
45
|
+
var BLOB_CONTENT_TYPES = ["stream", "excel", "download", "blob", "octet-stream"];
|
|
46
|
+
var METHODS_WITHOUT_BODY = ["GET", "HEAD"];
|
|
47
|
+
export var HttpError = /*#__PURE__*/function (_Error) {
|
|
48
|
+
_inherits(HttpError, _Error);
|
|
49
|
+
var _super = _createSuper(HttpError);
|
|
50
|
+
function HttpError(message, code, response, statusCode) {
|
|
51
|
+
var _this;
|
|
52
|
+
_classCallCheck(this, HttpError);
|
|
53
|
+
_this = _super.call(this, message);
|
|
54
|
+
_this.code = code;
|
|
55
|
+
_this.response = response;
|
|
56
|
+
_this.statusCode = statusCode;
|
|
57
|
+
_this.name = "HttpError";
|
|
58
|
+
return _this;
|
|
59
|
+
}
|
|
60
|
+
return _createClass(HttpError);
|
|
61
|
+
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
62
|
+
export var HttpClient = /*#__PURE__*/function () {
|
|
63
|
+
function HttpClient() {
|
|
64
|
+
var _configure$baseUrl, _configure$blobConten, _configure$codeKey, _configure$dataKey, _configure$messageKey, _configure$returnData, _configure$defaultMet, _configure$timeout, _configure$codes$succ, _configure$codes, _configure$codes$logo, _configure$codes2, _configure$codes$igno, _configure$codes3;
|
|
65
|
+
var configure = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
66
|
+
_classCallCheck(this, HttpClient);
|
|
67
|
+
_defineProperty(this, "configure", void 0);
|
|
68
|
+
this.configure = {
|
|
69
|
+
baseUrl: (_configure$baseUrl = configure.baseUrl) !== null && _configure$baseUrl !== void 0 ? _configure$baseUrl : "",
|
|
70
|
+
blobContentTypes: (_configure$blobConten = configure.blobContentTypes) !== null && _configure$blobConten !== void 0 ? _configure$blobConten : [].concat(BLOB_CONTENT_TYPES),
|
|
71
|
+
headers: configure.headers,
|
|
72
|
+
codeKey: (_configure$codeKey = configure.codeKey) !== null && _configure$codeKey !== void 0 ? _configure$codeKey : DEFAULT_CODE_KEY,
|
|
73
|
+
dataKey: (_configure$dataKey = configure.dataKey) !== null && _configure$dataKey !== void 0 ? _configure$dataKey : DEFAULT_DATA_KEY,
|
|
74
|
+
messageKey: (_configure$messageKey = configure.messageKey) !== null && _configure$messageKey !== void 0 ? _configure$messageKey : DEFAULT_MESSAGE_KEY,
|
|
75
|
+
returnData: (_configure$returnData = configure.returnData) !== null && _configure$returnData !== void 0 ? _configure$returnData : false,
|
|
76
|
+
defaultMethod: (_configure$defaultMet = configure.defaultMethod) !== null && _configure$defaultMet !== void 0 ? _configure$defaultMet : DEFAULT_METHOD,
|
|
77
|
+
timeout: (_configure$timeout = configure.timeout) !== null && _configure$timeout !== void 0 ? _configure$timeout : 30000,
|
|
78
|
+
codes: {
|
|
79
|
+
success: (_configure$codes$succ = (_configure$codes = configure.codes) === null || _configure$codes === void 0 ? void 0 : _configure$codes.success) !== null && _configure$codes$succ !== void 0 ? _configure$codes$succ : [].concat(DEFAULT_SUCCESS_CODES),
|
|
80
|
+
logout: (_configure$codes$logo = (_configure$codes2 = configure.codes) === null || _configure$codes2 === void 0 ? void 0 : _configure$codes2.logout) !== null && _configure$codes$logo !== void 0 ? _configure$codes$logo : [].concat(DEFAULT_LOGOUT_CODES),
|
|
81
|
+
ignoreError: (_configure$codes$igno = (_configure$codes3 = configure.codes) === null || _configure$codes3 === void 0 ? void 0 : _configure$codes3.ignoreError) !== null && _configure$codes$igno !== void 0 ? _configure$codes$igno : []
|
|
82
|
+
},
|
|
83
|
+
onError: configure.onError,
|
|
84
|
+
onLogout: configure.onLogout,
|
|
85
|
+
onSuccess: configure.onSuccess,
|
|
86
|
+
requestInterceptor: configure.requestInterceptor,
|
|
87
|
+
responseInterceptor: configure.responseInterceptor
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 更新客户端配置
|
|
93
|
+
*/
|
|
94
|
+
_createClass(HttpClient, [{
|
|
95
|
+
key: "config",
|
|
96
|
+
value: function config(configure) {
|
|
97
|
+
if (!isObject(configure)) {
|
|
98
|
+
throw new TypeError("Config must be an object");
|
|
99
|
+
}
|
|
100
|
+
this.configure = _objectSpread(_objectSpread({}, this.configure), configure);
|
|
101
|
+
}
|
|
102
|
+
}, {
|
|
103
|
+
key: "get",
|
|
104
|
+
value: function get(url, options) {
|
|
105
|
+
return this.request(url, _objectSpread(_objectSpread({}, options), {}, {
|
|
106
|
+
method: "GET"
|
|
107
|
+
}));
|
|
108
|
+
}
|
|
109
|
+
}, {
|
|
110
|
+
key: "post",
|
|
111
|
+
value: function post(url, options) {
|
|
112
|
+
return this.request(url, _objectSpread(_objectSpread({}, options), {}, {
|
|
113
|
+
method: "POST"
|
|
114
|
+
}));
|
|
115
|
+
}
|
|
116
|
+
}, {
|
|
117
|
+
key: "put",
|
|
118
|
+
value: function put(url, options) {
|
|
119
|
+
return this.request(url, _objectSpread(_objectSpread({}, options), {}, {
|
|
120
|
+
method: "PUT"
|
|
121
|
+
}));
|
|
122
|
+
}
|
|
123
|
+
}, {
|
|
124
|
+
key: "delete",
|
|
125
|
+
value: function _delete(url, options) {
|
|
126
|
+
return this.request(url, _objectSpread(_objectSpread({}, options), {}, {
|
|
127
|
+
method: "DELETE"
|
|
128
|
+
}));
|
|
129
|
+
}
|
|
130
|
+
}, {
|
|
131
|
+
key: "patch",
|
|
132
|
+
value: function patch(url, options) {
|
|
133
|
+
return this.request(url, _objectSpread(_objectSpread({}, options), {}, {
|
|
134
|
+
method: "PATCH"
|
|
135
|
+
}));
|
|
136
|
+
}
|
|
137
|
+
}, {
|
|
138
|
+
key: "request",
|
|
139
|
+
value: function () {
|
|
140
|
+
var _request = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(url) {
|
|
141
|
+
var options,
|
|
142
|
+
json,
|
|
143
|
+
params,
|
|
144
|
+
_options$ignoreError,
|
|
145
|
+
ignoreError,
|
|
146
|
+
_options$returnData,
|
|
147
|
+
returnData,
|
|
148
|
+
_options$timeout,
|
|
149
|
+
timeout,
|
|
150
|
+
onError,
|
|
151
|
+
onLogout,
|
|
152
|
+
onSuccess,
|
|
153
|
+
restOptions,
|
|
154
|
+
fetchOptions,
|
|
155
|
+
_ref,
|
|
156
|
+
_options$method,
|
|
157
|
+
_ref2,
|
|
158
|
+
fullUrl,
|
|
159
|
+
method,
|
|
160
|
+
_response,
|
|
161
|
+
result,
|
|
162
|
+
_args = arguments;
|
|
163
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
164
|
+
while (1) switch (_context.prev = _context.next) {
|
|
165
|
+
case 0:
|
|
166
|
+
options = _args.length > 1 && _args[1] !== undefined ? _args[1] : {};
|
|
167
|
+
json = options.json, params = options.params, _options$ignoreError = options.ignoreError, ignoreError = _options$ignoreError === void 0 ? false : _options$ignoreError, _options$returnData = options.returnData, returnData = _options$returnData === void 0 ? this.configure.returnData : _options$returnData, _options$timeout = options.timeout, timeout = _options$timeout === void 0 ? this.configure.timeout : _options$timeout, onError = options.onError, onLogout = options.onLogout, onSuccess = options.onSuccess, restOptions = _objectWithoutProperties(options, _excluded);
|
|
168
|
+
fetchOptions = _objectSpread({}, restOptions);
|
|
169
|
+
_context.prev = 3;
|
|
170
|
+
// 构建完整 URL
|
|
171
|
+
fullUrl = this.buildUrl(url); // 处理查询参数
|
|
172
|
+
if (params) {
|
|
173
|
+
fullUrl = this.appendParams(fullUrl, params);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// 设置请求方法
|
|
177
|
+
method = ((_ref = (_options$method = options.method) !== null && _options$method !== void 0 ? _options$method : this.configure.defaultMethod) !== null && _ref !== void 0 ? _ref : DEFAULT_METHOD).toUpperCase();
|
|
178
|
+
fetchOptions.method = method;
|
|
179
|
+
|
|
180
|
+
// 设置请求头
|
|
181
|
+
_context.next = 10;
|
|
182
|
+
return this.buildHeaders(options.headers);
|
|
183
|
+
case 10:
|
|
184
|
+
fetchOptions.headers = _context.sent;
|
|
185
|
+
// 处理请求体
|
|
186
|
+
if (json) {
|
|
187
|
+
if (METHODS_WITHOUT_BODY.includes(method)) {
|
|
188
|
+
// GET/HEAD 请求将 json 转为查询参数
|
|
189
|
+
if (isObject(json) && !(json instanceof FormData)) {
|
|
190
|
+
fullUrl = this.appendParams(fullUrl, json);
|
|
191
|
+
}
|
|
192
|
+
} else {
|
|
193
|
+
// 其他方法处理 body
|
|
194
|
+
if (json instanceof FormData) {
|
|
195
|
+
fetchOptions.headers.delete("Content-Type");
|
|
196
|
+
fetchOptions.body = json;
|
|
197
|
+
} else {
|
|
198
|
+
fetchOptions.body = JSON.stringify(json);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// 执行请求拦截器
|
|
204
|
+
if (!this.configure.requestInterceptor) {
|
|
205
|
+
_context.next = 15;
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
_context.next = 15;
|
|
209
|
+
return this.configure.requestInterceptor(fullUrl, fetchOptions);
|
|
210
|
+
case 15:
|
|
211
|
+
_context.next = 17;
|
|
212
|
+
return this.fetchWithTimeout(fullUrl, fetchOptions, (_ref2 = timeout !== null && timeout !== void 0 ? timeout : this.configure.timeout) !== null && _ref2 !== void 0 ? _ref2 : Number.MAX_SAFE_INTEGER);
|
|
213
|
+
case 17:
|
|
214
|
+
_response = _context.sent;
|
|
215
|
+
if (_response.ok) {
|
|
216
|
+
_context.next = 20;
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
throw new HttpError(_response.statusText || "Request failed", undefined, _response, _response.status);
|
|
220
|
+
case 20:
|
|
221
|
+
_context.next = 22;
|
|
222
|
+
return this.handleResponse(_response, returnData !== null && returnData !== void 0 ? returnData : false, ignoreError, onSuccess);
|
|
223
|
+
case 22:
|
|
224
|
+
result = _context.sent;
|
|
225
|
+
if (!(this.configure.responseInterceptor && isObject(result))) {
|
|
226
|
+
_context.next = 27;
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
_context.next = 26;
|
|
230
|
+
return this.configure.responseInterceptor(result);
|
|
231
|
+
case 26:
|
|
232
|
+
return _context.abrupt("return", _context.sent);
|
|
233
|
+
case 27:
|
|
234
|
+
return _context.abrupt("return", result);
|
|
235
|
+
case 30:
|
|
236
|
+
_context.prev = 30;
|
|
237
|
+
_context.t0 = _context["catch"](3);
|
|
238
|
+
return _context.abrupt("return", this.handleError(_context.t0, ignoreError, onError, onLogout));
|
|
239
|
+
case 33:
|
|
240
|
+
case "end":
|
|
241
|
+
return _context.stop();
|
|
242
|
+
}
|
|
243
|
+
}, _callee, this, [[3, 30]]);
|
|
244
|
+
}));
|
|
245
|
+
function request(_x) {
|
|
246
|
+
return _request.apply(this, arguments);
|
|
247
|
+
}
|
|
248
|
+
return request;
|
|
249
|
+
}()
|
|
250
|
+
/**
|
|
251
|
+
* 构建完整 URL
|
|
252
|
+
*/
|
|
253
|
+
}, {
|
|
254
|
+
key: "buildUrl",
|
|
255
|
+
value: function buildUrl(url) {
|
|
256
|
+
if (typeof url !== "string") {
|
|
257
|
+
throw new TypeError("URL must be a string");
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// 如果是完整 URL,直接返回
|
|
261
|
+
if (url.startsWith("http://") || url.startsWith("https://")) {
|
|
262
|
+
return url;
|
|
263
|
+
}
|
|
264
|
+
var baseUrl = this.configure.baseUrl;
|
|
265
|
+
if (!baseUrl) return url;
|
|
266
|
+
var normalizedBase = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
|
|
267
|
+
var normalizedUrl = url.startsWith("/") ? url : "/".concat(url);
|
|
268
|
+
return normalizedBase + normalizedUrl;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* 添加查询参数
|
|
273
|
+
*/
|
|
274
|
+
}, {
|
|
275
|
+
key: "appendParams",
|
|
276
|
+
value: function appendParams(url, params) {
|
|
277
|
+
if (!isObject(params)) {
|
|
278
|
+
throw new TypeError("Params must be an object");
|
|
279
|
+
}
|
|
280
|
+
var searchParams = new URLSearchParams();
|
|
281
|
+
Object.entries(params).forEach(function (_ref3) {
|
|
282
|
+
var _ref4 = _slicedToArray(_ref3, 2),
|
|
283
|
+
key = _ref4[0],
|
|
284
|
+
value = _ref4[1];
|
|
285
|
+
if (value !== undefined && value !== null) {
|
|
286
|
+
searchParams.append(key, String(value));
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
var queryString = searchParams.toString();
|
|
290
|
+
if (!queryString) return url;
|
|
291
|
+
var separator = url.includes("?") ? "&" : "?";
|
|
292
|
+
return "".concat(url).concat(separator).concat(queryString);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* 构建请求头
|
|
297
|
+
*/
|
|
298
|
+
}, {
|
|
299
|
+
key: "buildHeaders",
|
|
300
|
+
value: (function () {
|
|
301
|
+
var _buildHeaders = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(customHeaders) {
|
|
302
|
+
var baseHeaders, configureHeaders, headers, custom;
|
|
303
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
304
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
305
|
+
case 0:
|
|
306
|
+
configureHeaders = this.configure.headers; // 如果是函数,执行获取
|
|
307
|
+
if (!(configureHeaders && isFunction(configureHeaders))) {
|
|
308
|
+
_context2.next = 7;
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
_context2.next = 4;
|
|
312
|
+
return configureHeaders();
|
|
313
|
+
case 4:
|
|
314
|
+
baseHeaders = _context2.sent;
|
|
315
|
+
_context2.next = 8;
|
|
316
|
+
break;
|
|
317
|
+
case 7:
|
|
318
|
+
if (configureHeaders) {
|
|
319
|
+
baseHeaders = configureHeaders;
|
|
320
|
+
}
|
|
321
|
+
case 8:
|
|
322
|
+
headers = new Headers(baseHeaders); // 合并自定义请求头
|
|
323
|
+
if (customHeaders) {
|
|
324
|
+
custom = new Headers(customHeaders);
|
|
325
|
+
custom.forEach(function (value, key) {
|
|
326
|
+
headers.set(key, value);
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// 设置默认 Content-Type
|
|
331
|
+
if (!headers.has("Content-Type")) {
|
|
332
|
+
headers.set("Content-Type", DEFAULT_CONTENT_TYPE);
|
|
333
|
+
}
|
|
334
|
+
return _context2.abrupt("return", headers);
|
|
335
|
+
case 12:
|
|
336
|
+
case "end":
|
|
337
|
+
return _context2.stop();
|
|
338
|
+
}
|
|
339
|
+
}, _callee2, this);
|
|
340
|
+
}));
|
|
341
|
+
function buildHeaders(_x2) {
|
|
342
|
+
return _buildHeaders.apply(this, arguments);
|
|
343
|
+
}
|
|
344
|
+
return buildHeaders;
|
|
345
|
+
}()
|
|
346
|
+
/**
|
|
347
|
+
* 带超时的 fetch 请求
|
|
348
|
+
*/
|
|
349
|
+
)
|
|
350
|
+
}, {
|
|
351
|
+
key: "fetchWithTimeout",
|
|
352
|
+
value: (function () {
|
|
353
|
+
var _fetchWithTimeout = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(url, options, timeout) {
|
|
354
|
+
var controller, timeoutId, _response2;
|
|
355
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
356
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
357
|
+
case 0:
|
|
358
|
+
controller = new AbortController();
|
|
359
|
+
timeoutId = setTimeout(function () {
|
|
360
|
+
return controller.abort();
|
|
361
|
+
}, timeout);
|
|
362
|
+
_context3.prev = 2;
|
|
363
|
+
_context3.next = 5;
|
|
364
|
+
return fetch(url, _objectSpread(_objectSpread({}, options), {}, {
|
|
365
|
+
signal: controller.signal
|
|
366
|
+
}));
|
|
367
|
+
case 5:
|
|
368
|
+
_response2 = _context3.sent;
|
|
369
|
+
return _context3.abrupt("return", _response2);
|
|
370
|
+
case 9:
|
|
371
|
+
_context3.prev = 9;
|
|
372
|
+
_context3.t0 = _context3["catch"](2);
|
|
373
|
+
if (!(_context3.t0.name === "AbortError")) {
|
|
374
|
+
_context3.next = 13;
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
throw new HttpError("Request timeout after ".concat(timeout, "ms"), undefined, undefined, 408);
|
|
378
|
+
case 13:
|
|
379
|
+
throw _context3.t0;
|
|
380
|
+
case 14:
|
|
381
|
+
_context3.prev = 14;
|
|
382
|
+
clearTimeout(timeoutId);
|
|
383
|
+
return _context3.finish(14);
|
|
384
|
+
case 17:
|
|
385
|
+
case "end":
|
|
386
|
+
return _context3.stop();
|
|
387
|
+
}
|
|
388
|
+
}, _callee3, null, [[2, 9, 14, 17]]);
|
|
389
|
+
}));
|
|
390
|
+
function fetchWithTimeout(_x3, _x4, _x5) {
|
|
391
|
+
return _fetchWithTimeout.apply(this, arguments);
|
|
392
|
+
}
|
|
393
|
+
return fetchWithTimeout;
|
|
394
|
+
}()
|
|
395
|
+
/**
|
|
396
|
+
* 处理响应数据
|
|
397
|
+
*/
|
|
398
|
+
)
|
|
399
|
+
}, {
|
|
400
|
+
key: "handleResponse",
|
|
401
|
+
value: (function () {
|
|
402
|
+
var _handleResponse = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(response, returnData, ignoreError, onSuccess) {
|
|
403
|
+
var _response$headers$get, _response$headers$get2, _codes$success, _codes$logout, _codes$ignoreError, _this$configure$blobC;
|
|
404
|
+
var contentType, _this$configure, codeKey, dataKey, messageKey, codes, successCodes, logoutCodes, ignoreErrorCodes, blob, filename, data, code, message;
|
|
405
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
406
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
407
|
+
case 0:
|
|
408
|
+
contentType = (_response$headers$get = (_response$headers$get2 = response.headers.get("content-type")) === null || _response$headers$get2 === void 0 ? void 0 : _response$headers$get2.toLowerCase()) !== null && _response$headers$get !== void 0 ? _response$headers$get : "";
|
|
409
|
+
_this$configure = this.configure, codeKey = _this$configure.codeKey, dataKey = _this$configure.dataKey, messageKey = _this$configure.messageKey, codes = _this$configure.codes;
|
|
410
|
+
successCodes = (_codes$success = codes === null || codes === void 0 ? void 0 : codes.success) !== null && _codes$success !== void 0 ? _codes$success : DEFAULT_SUCCESS_CODES;
|
|
411
|
+
logoutCodes = (_codes$logout = codes === null || codes === void 0 ? void 0 : codes.logout) !== null && _codes$logout !== void 0 ? _codes$logout : DEFAULT_LOGOUT_CODES;
|
|
412
|
+
ignoreErrorCodes = (_codes$ignoreError = codes === null || codes === void 0 ? void 0 : codes.ignoreError) !== null && _codes$ignoreError !== void 0 ? _codes$ignoreError : []; // 处理文件下载(Blob)
|
|
413
|
+
if (!((_this$configure$blobC = this.configure.blobContentTypes) !== null && _this$configure$blobC !== void 0 && _this$configure$blobC.some(function (type) {
|
|
414
|
+
return contentType.includes(type);
|
|
415
|
+
}))) {
|
|
416
|
+
_context4.next = 11;
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
_context4.next = 8;
|
|
420
|
+
return response.blob();
|
|
421
|
+
case 8:
|
|
422
|
+
blob = _context4.sent;
|
|
423
|
+
filename = this.extractFilename(response.headers);
|
|
424
|
+
return _context4.abrupt("return", _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, codeKey, successCodes[0]), "data", blob), "filename", filename), "response", response));
|
|
425
|
+
case 11:
|
|
426
|
+
_context4.next = 13;
|
|
427
|
+
return response.json();
|
|
428
|
+
case 13:
|
|
429
|
+
data = _context4.sent;
|
|
430
|
+
if (data.hasOwnProperty(codeKey)) {
|
|
431
|
+
_context4.next = 16;
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
return _context4.abrupt("return", data);
|
|
435
|
+
case 16:
|
|
436
|
+
code = data[codeKey !== null && codeKey !== void 0 ? codeKey : DEFAULT_CODE_KEY];
|
|
437
|
+
message = data[messageKey !== null && messageKey !== void 0 ? messageKey : DEFAULT_MESSAGE_KEY]; // 成功响应
|
|
438
|
+
if (!successCodes.includes(code)) {
|
|
439
|
+
_context4.next = 23;
|
|
440
|
+
break;
|
|
441
|
+
}
|
|
442
|
+
if (!(returnData && data.hasOwnProperty(dataKey))) {
|
|
443
|
+
_context4.next = 21;
|
|
444
|
+
break;
|
|
445
|
+
}
|
|
446
|
+
return _context4.abrupt("return", data[dataKey !== null && dataKey !== void 0 ? dataKey : DEFAULT_DATA_KEY]);
|
|
447
|
+
case 21:
|
|
448
|
+
onSuccess === null || onSuccess === void 0 || onSuccess(data);
|
|
449
|
+
return _context4.abrupt("return", data);
|
|
450
|
+
case 23:
|
|
451
|
+
if (!(ignoreError || ignoreErrorCodes.includes(code))) {
|
|
452
|
+
_context4.next = 25;
|
|
453
|
+
break;
|
|
454
|
+
}
|
|
455
|
+
return _context4.abrupt("return", data);
|
|
456
|
+
case 25:
|
|
457
|
+
if (!logoutCodes.includes(code)) {
|
|
458
|
+
_context4.next = 27;
|
|
459
|
+
break;
|
|
460
|
+
}
|
|
461
|
+
throw new HttpError(message || "Unauthorized", code, data);
|
|
462
|
+
case 27:
|
|
463
|
+
throw new HttpError(message || "Request failed", code, data);
|
|
464
|
+
case 28:
|
|
465
|
+
case "end":
|
|
466
|
+
return _context4.stop();
|
|
467
|
+
}
|
|
468
|
+
}, _callee4, this);
|
|
469
|
+
}));
|
|
470
|
+
function handleResponse(_x6, _x7, _x8, _x9) {
|
|
471
|
+
return _handleResponse.apply(this, arguments);
|
|
472
|
+
}
|
|
473
|
+
return handleResponse;
|
|
474
|
+
}()
|
|
475
|
+
/**
|
|
476
|
+
* 从响应头提取文件名
|
|
477
|
+
*/
|
|
478
|
+
)
|
|
479
|
+
}, {
|
|
480
|
+
key: "extractFilename",
|
|
481
|
+
value: function extractFilename(headers) {
|
|
482
|
+
var disposition = headers.get("content-disposition");
|
|
483
|
+
if (!disposition) return undefined;
|
|
484
|
+
var matches = disposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
|
|
485
|
+
if (!(matches !== null && matches !== void 0 && matches[1])) return undefined;
|
|
486
|
+
var filename = matches[1].replace(/['"]/g, "");
|
|
487
|
+
try {
|
|
488
|
+
filename = decodeURIComponent(filename);
|
|
489
|
+
} catch (_unused) {
|
|
490
|
+
// 解码失败时使用原始文件名
|
|
491
|
+
}
|
|
492
|
+
return filename;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* 统一错误处理
|
|
497
|
+
*/
|
|
498
|
+
}, {
|
|
499
|
+
key: "handleError",
|
|
500
|
+
value: function handleError(error, ignoreError, customOnError, customOnLogout) {
|
|
501
|
+
var httpError = error instanceof HttpError ? error : new HttpError(error.message || "Unknown error", undefined, error);
|
|
502
|
+
|
|
503
|
+
// 不忽略错误时执行回调
|
|
504
|
+
if (!ignoreError) {
|
|
505
|
+
var _this$configure$codes, _this$configure$codes2;
|
|
506
|
+
// 登出错误
|
|
507
|
+
var logoutCodes = (_this$configure$codes = (_this$configure$codes2 = this.configure.codes) === null || _this$configure$codes2 === void 0 ? void 0 : _this$configure$codes2.logout) !== null && _this$configure$codes !== void 0 ? _this$configure$codes : DEFAULT_LOGOUT_CODES;
|
|
508
|
+
if (httpError.code && logoutCodes.includes(httpError.code)) {
|
|
509
|
+
var logoutHandler = customOnLogout !== null && customOnLogout !== void 0 ? customOnLogout : this.configure.onLogout;
|
|
510
|
+
logoutHandler === null || logoutHandler === void 0 || logoutHandler(httpError);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// 通用错误处理
|
|
514
|
+
var errorHandler = customOnError !== null && customOnError !== void 0 ? customOnError : this.configure.onError;
|
|
515
|
+
errorHandler === null || errorHandler === void 0 || errorHandler(httpError);
|
|
516
|
+
}
|
|
517
|
+
throw httpError;
|
|
518
|
+
}
|
|
519
|
+
}]);
|
|
520
|
+
return HttpClient;
|
|
521
|
+
}();
|
|
522
|
+
|
|
523
|
+
// ==================== 工具函数 ====================
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* 下载文件(用于 Blob 响应)
|
|
527
|
+
* @example
|
|
528
|
+
* const response = await http.get('/api/download');
|
|
529
|
+
* downloadfile(response);
|
|
530
|
+
*/
|
|
531
|
+
export function downloadfile(response) {
|
|
532
|
+
var data = response.data,
|
|
533
|
+
filename = response.filename;
|
|
534
|
+
if (!(data instanceof Blob)) {
|
|
535
|
+
throw new TypeError("Data must be a Blob");
|
|
536
|
+
}
|
|
537
|
+
var url = URL.createObjectURL(data);
|
|
538
|
+
var link = document.createElement("a");
|
|
539
|
+
link.href = url;
|
|
540
|
+
link.download = filename || "download-".concat(Date.now());
|
|
541
|
+
link.style.display = "none";
|
|
542
|
+
document.body.appendChild(link);
|
|
543
|
+
link.click();
|
|
544
|
+
|
|
545
|
+
// 清理
|
|
546
|
+
setTimeout(function () {
|
|
547
|
+
document.body.removeChild(link);
|
|
548
|
+
URL.revokeObjectURL(url);
|
|
549
|
+
}, 100);
|
|
550
|
+
}
|
|
551
|
+
export function createHttpClient(config) {
|
|
552
|
+
return new HttpClient(config);
|
|
553
|
+
}
|
|
554
|
+
export var rq = new HttpClient();
|
|
555
|
+
export default HttpClient;
|
package/es/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC"}
|
package/es/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./fetch";
|
package/lib/fetch.d.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
export declare const isObject: (oj: unknown) => boolean;
|
|
2
|
+
export declare const isFunction: (oj: unknown) => boolean;
|
|
3
|
+
export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS" | "get" | "post" | "put" | "delete" | "patch" | "head" | "options" | undefined;
|
|
4
|
+
export type JsonValue = string | number | boolean | null | JsonObject | JsonArray;
|
|
5
|
+
export type JsonObject = {
|
|
6
|
+
[key: string]: JsonValue;
|
|
7
|
+
};
|
|
8
|
+
export type JsonArray = JsonValue[];
|
|
9
|
+
export type RequestData = unknown;
|
|
10
|
+
export interface HttpClientConfig {
|
|
11
|
+
/** 请求的基础 URL */
|
|
12
|
+
baseUrl?: string;
|
|
13
|
+
/** 流数据响应头类型关键字 */
|
|
14
|
+
blobContentTypes?: readonly string[];
|
|
15
|
+
/** 请求头配置 */
|
|
16
|
+
headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
|
|
17
|
+
/** 响应数据状态码的 key,默认: 'code' */
|
|
18
|
+
codeKey?: string;
|
|
19
|
+
/** 响应数据的 data key,默认: 'data' */
|
|
20
|
+
dataKey?: string;
|
|
21
|
+
/** 响应消息的 key,默认: 'message' */
|
|
22
|
+
messageKey?: string;
|
|
23
|
+
/** 是否默认返回 data 层数据,默认: false */
|
|
24
|
+
returnData?: boolean;
|
|
25
|
+
/** 默认请求方法,默认: 'GET' */
|
|
26
|
+
defaultMethod?: HttpMethod;
|
|
27
|
+
/** 请求超时时间(ms),默认: 30000 */
|
|
28
|
+
timeout?: number;
|
|
29
|
+
/** 状态码配置 */
|
|
30
|
+
codes?: {
|
|
31
|
+
/** 成功状态码列表 */
|
|
32
|
+
success?: readonly number[];
|
|
33
|
+
/** 需要登出的状态码列表 */
|
|
34
|
+
logout?: readonly number[];
|
|
35
|
+
/** 忽略错误的状态码列表 */
|
|
36
|
+
ignoreError?: readonly number[];
|
|
37
|
+
};
|
|
38
|
+
/** 错误处理回调 */
|
|
39
|
+
onError?: (error: HttpError) => void | Promise<void>;
|
|
40
|
+
/** 登出回调 */
|
|
41
|
+
onLogout?: (error: HttpError) => void | Promise<void>;
|
|
42
|
+
onSuccess?: (data: any) => void | Promise<void>;
|
|
43
|
+
/** 请求拦截器 */
|
|
44
|
+
requestInterceptor?: (url: string, options: RequestInit) => Promise<void> | void;
|
|
45
|
+
/** 响应拦截器 */
|
|
46
|
+
responseInterceptor?: <T>(response: HttpResponse<T>) => Promise<HttpResponse<T>> | HttpResponse<T>;
|
|
47
|
+
}
|
|
48
|
+
export interface RequestOptions extends Omit<RequestInit, "body" | "method"> {
|
|
49
|
+
/** 请求方法 */
|
|
50
|
+
method?: HttpMethod;
|
|
51
|
+
/** 请求体数据(对象会自动转为 JSON) */
|
|
52
|
+
json?: RequestData;
|
|
53
|
+
/** URL 查询参数 */
|
|
54
|
+
params?: Record<string, string | number | boolean | null | undefined>;
|
|
55
|
+
/** 是否忽略当前请求的错误,不触发 onError */
|
|
56
|
+
ignoreError?: boolean;
|
|
57
|
+
/** 是否返回 data 层数据 */
|
|
58
|
+
returnData?: boolean;
|
|
59
|
+
/** 请求超时时间(ms) */
|
|
60
|
+
timeout?: number;
|
|
61
|
+
/** 自定义错误回调(仅本次请求) */
|
|
62
|
+
onError?: (error: HttpError) => void;
|
|
63
|
+
/** 自定义登出回调(仅本次请求) */
|
|
64
|
+
onLogout?: (error: HttpError) => void;
|
|
65
|
+
/** 自定义成功回调(仅本次请求) */
|
|
66
|
+
onSuccess?: (data: any) => void;
|
|
67
|
+
}
|
|
68
|
+
export interface HttpResponse<T = any> {
|
|
69
|
+
[key: string]: any;
|
|
70
|
+
data?: T;
|
|
71
|
+
}
|
|
72
|
+
export interface BlobResponse {
|
|
73
|
+
data: Blob;
|
|
74
|
+
filename?: string;
|
|
75
|
+
response: Response;
|
|
76
|
+
}
|
|
77
|
+
export declare class HttpError extends Error {
|
|
78
|
+
code?: number | undefined;
|
|
79
|
+
response?: any;
|
|
80
|
+
statusCode?: number | undefined;
|
|
81
|
+
constructor(message: string, code?: number | undefined, response?: any, statusCode?: number | undefined);
|
|
82
|
+
}
|
|
83
|
+
export declare class HttpClient {
|
|
84
|
+
private configure;
|
|
85
|
+
constructor(configure?: HttpClientConfig);
|
|
86
|
+
/**
|
|
87
|
+
* 更新客户端配置
|
|
88
|
+
*/
|
|
89
|
+
config(configure: HttpClientConfig): void;
|
|
90
|
+
get<T = any>(url: string, options?: Omit<RequestOptions, "method">): Promise<T>;
|
|
91
|
+
post<T = any>(url: string, options?: Omit<RequestOptions, "method">): Promise<T>;
|
|
92
|
+
put<T = any>(url: string, options?: Omit<RequestOptions, "method">): Promise<T>;
|
|
93
|
+
delete<T = any>(url: string, options?: Omit<RequestOptions, "method">): Promise<T>;
|
|
94
|
+
patch<T = any>(url: string, options?: Omit<RequestOptions, "method">): Promise<T>;
|
|
95
|
+
request<T = any>(url: string, options?: RequestOptions): Promise<T>;
|
|
96
|
+
/**
|
|
97
|
+
* 构建完整 URL
|
|
98
|
+
*/
|
|
99
|
+
private buildUrl;
|
|
100
|
+
/**
|
|
101
|
+
* 添加查询参数
|
|
102
|
+
*/
|
|
103
|
+
private appendParams;
|
|
104
|
+
/**
|
|
105
|
+
* 构建请求头
|
|
106
|
+
*/
|
|
107
|
+
private buildHeaders;
|
|
108
|
+
/**
|
|
109
|
+
* 带超时的 fetch 请求
|
|
110
|
+
*/
|
|
111
|
+
private fetchWithTimeout;
|
|
112
|
+
/**
|
|
113
|
+
* 处理响应数据
|
|
114
|
+
*/
|
|
115
|
+
private handleResponse;
|
|
116
|
+
/**
|
|
117
|
+
* 从响应头提取文件名
|
|
118
|
+
*/
|
|
119
|
+
private extractFilename;
|
|
120
|
+
/**
|
|
121
|
+
* 统一错误处理
|
|
122
|
+
*/
|
|
123
|
+
private handleError;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* 下载文件(用于 Blob 响应)
|
|
127
|
+
* @example
|
|
128
|
+
* const response = await http.get('/api/download');
|
|
129
|
+
* downloadfile(response);
|
|
130
|
+
*/
|
|
131
|
+
export declare function downloadfile(response: BlobResponse | {
|
|
132
|
+
data: Blob;
|
|
133
|
+
filename?: string;
|
|
134
|
+
}): void;
|
|
135
|
+
export declare function createHttpClient(config?: HttpClientConfig): HttpClient;
|
|
136
|
+
export declare const rq: HttpClient;
|
|
137
|
+
export default HttpClient;
|
|
138
|
+
//# sourceMappingURL=fetch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["fetch.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,QAAQ,OAAQ,OAAO,YAA6D,CAAC;AAClG,eAAO,MAAM,UAAU,OAAQ,OAAO,YACsB,CAAC;A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GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,cAAc,EAAE,QAAQ,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAIlF,KAAK,CAAC,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,cAAc,EAAE,QAAQ,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAI3E,OAAO,CAAC,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,CAAC,CAAC;IA8F7E;;OAEG;IACH,OAAO,CAAC,QAAQ;IAmBhB;;OAEG;IACH,OAAO,CAAC,YAAY;IAoBpB;;OAEG;YACW,YAAY;IA8B1B;;OAEG;YACW,gBAAgB;IAwB9B;;OAEG;YACW,cAAc;IA2D5B;;OAEG;IACH,OAAO,CAAC,eAAe;IAiBvB;;OAEG;IACH,OAAO,CAAC,WAAW;CA2BpB;AAID;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,YAAY,GAAG;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAsB7F;AAED,wBAAgB,gBAAgB,CAAC,MAAM,CAAC,EAAE,gBAAgB,GAAG,UAAU,CAEtE;AACD,eAAO,MAAM,EAAE,YAAmB,CAAC;AACnC,eAAe,UAAU,CAAC"}
|
package/lib/fetch.js
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/fetch.ts
|
|
20
|
+
var fetch_exports = {};
|
|
21
|
+
__export(fetch_exports, {
|
|
22
|
+
HttpClient: () => HttpClient,
|
|
23
|
+
HttpError: () => HttpError,
|
|
24
|
+
createHttpClient: () => createHttpClient,
|
|
25
|
+
default: () => fetch_default,
|
|
26
|
+
downloadfile: () => downloadfile,
|
|
27
|
+
isFunction: () => isFunction,
|
|
28
|
+
isObject: () => isObject,
|
|
29
|
+
rq: () => rq
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(fetch_exports);
|
|
32
|
+
var isObject = (oj) => Object.prototype.toString.call(oj) === "[object Object]";
|
|
33
|
+
var isFunction = (oj) => Object.prototype.toString.call(oj) === "[object Function]";
|
|
34
|
+
var DEFAULT_SUCCESS_CODES = [200];
|
|
35
|
+
var DEFAULT_LOGOUT_CODES = [401, 403];
|
|
36
|
+
var DEFAULT_METHOD = "GET";
|
|
37
|
+
var DEFAULT_CODE_KEY = "code";
|
|
38
|
+
var DEFAULT_DATA_KEY = "data";
|
|
39
|
+
var DEFAULT_MESSAGE_KEY = "message";
|
|
40
|
+
var DEFAULT_CONTENT_TYPE = "application/json;charset=UTF-8";
|
|
41
|
+
var BLOB_CONTENT_TYPES = ["stream", "excel", "download", "blob", "octet-stream"];
|
|
42
|
+
var METHODS_WITHOUT_BODY = ["GET", "HEAD"];
|
|
43
|
+
var HttpError = class extends Error {
|
|
44
|
+
constructor(message, code, response, statusCode) {
|
|
45
|
+
super(message);
|
|
46
|
+
this.code = code;
|
|
47
|
+
this.response = response;
|
|
48
|
+
this.statusCode = statusCode;
|
|
49
|
+
this.name = "HttpError";
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var HttpClient = class {
|
|
53
|
+
constructor(configure = {}) {
|
|
54
|
+
var _a, _b, _c;
|
|
55
|
+
this.configure = {
|
|
56
|
+
baseUrl: configure.baseUrl ?? "",
|
|
57
|
+
blobContentTypes: configure.blobContentTypes ?? [...BLOB_CONTENT_TYPES],
|
|
58
|
+
headers: configure.headers,
|
|
59
|
+
codeKey: configure.codeKey ?? DEFAULT_CODE_KEY,
|
|
60
|
+
dataKey: configure.dataKey ?? DEFAULT_DATA_KEY,
|
|
61
|
+
messageKey: configure.messageKey ?? DEFAULT_MESSAGE_KEY,
|
|
62
|
+
returnData: configure.returnData ?? false,
|
|
63
|
+
defaultMethod: configure.defaultMethod ?? DEFAULT_METHOD,
|
|
64
|
+
timeout: configure.timeout ?? 3e4,
|
|
65
|
+
codes: {
|
|
66
|
+
success: ((_a = configure.codes) == null ? void 0 : _a.success) ?? [...DEFAULT_SUCCESS_CODES],
|
|
67
|
+
logout: ((_b = configure.codes) == null ? void 0 : _b.logout) ?? [...DEFAULT_LOGOUT_CODES],
|
|
68
|
+
ignoreError: ((_c = configure.codes) == null ? void 0 : _c.ignoreError) ?? []
|
|
69
|
+
},
|
|
70
|
+
onError: configure.onError,
|
|
71
|
+
onLogout: configure.onLogout,
|
|
72
|
+
onSuccess: configure.onSuccess,
|
|
73
|
+
requestInterceptor: configure.requestInterceptor,
|
|
74
|
+
responseInterceptor: configure.responseInterceptor
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* 更新客户端配置
|
|
79
|
+
*/
|
|
80
|
+
config(configure) {
|
|
81
|
+
if (!isObject(configure)) {
|
|
82
|
+
throw new TypeError("Config must be an object");
|
|
83
|
+
}
|
|
84
|
+
this.configure = { ...this.configure, ...configure };
|
|
85
|
+
}
|
|
86
|
+
get(url, options) {
|
|
87
|
+
return this.request(url, { ...options, method: "GET" });
|
|
88
|
+
}
|
|
89
|
+
post(url, options) {
|
|
90
|
+
return this.request(url, { ...options, method: "POST" });
|
|
91
|
+
}
|
|
92
|
+
put(url, options) {
|
|
93
|
+
return this.request(url, { ...options, method: "PUT" });
|
|
94
|
+
}
|
|
95
|
+
delete(url, options) {
|
|
96
|
+
return this.request(url, { ...options, method: "DELETE" });
|
|
97
|
+
}
|
|
98
|
+
patch(url, options) {
|
|
99
|
+
return this.request(url, { ...options, method: "PATCH" });
|
|
100
|
+
}
|
|
101
|
+
async request(url, options = {}) {
|
|
102
|
+
const {
|
|
103
|
+
json,
|
|
104
|
+
params,
|
|
105
|
+
ignoreError = false,
|
|
106
|
+
returnData = this.configure.returnData,
|
|
107
|
+
timeout = this.configure.timeout,
|
|
108
|
+
onError,
|
|
109
|
+
onLogout,
|
|
110
|
+
onSuccess,
|
|
111
|
+
...restOptions
|
|
112
|
+
} = options;
|
|
113
|
+
const fetchOptions = { ...restOptions };
|
|
114
|
+
try {
|
|
115
|
+
let fullUrl = this.buildUrl(url);
|
|
116
|
+
if (params) {
|
|
117
|
+
fullUrl = this.appendParams(fullUrl, params);
|
|
118
|
+
}
|
|
119
|
+
const method = (options.method ?? this.configure.defaultMethod ?? DEFAULT_METHOD).toUpperCase();
|
|
120
|
+
fetchOptions.method = method;
|
|
121
|
+
fetchOptions.headers = await this.buildHeaders(options.headers);
|
|
122
|
+
if (json) {
|
|
123
|
+
if (METHODS_WITHOUT_BODY.includes(method)) {
|
|
124
|
+
if (isObject(json) && !(json instanceof FormData)) {
|
|
125
|
+
fullUrl = this.appendParams(fullUrl, json);
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
if (json instanceof FormData) {
|
|
129
|
+
fetchOptions.headers.delete("Content-Type");
|
|
130
|
+
fetchOptions.body = json;
|
|
131
|
+
} else {
|
|
132
|
+
fetchOptions.body = JSON.stringify(json);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (this.configure.requestInterceptor) {
|
|
137
|
+
await this.configure.requestInterceptor(fullUrl, fetchOptions);
|
|
138
|
+
}
|
|
139
|
+
const response = await this.fetchWithTimeout(
|
|
140
|
+
fullUrl,
|
|
141
|
+
fetchOptions,
|
|
142
|
+
timeout ?? this.configure.timeout ?? Number.MAX_SAFE_INTEGER
|
|
143
|
+
);
|
|
144
|
+
if (!response.ok) {
|
|
145
|
+
throw new HttpError(
|
|
146
|
+
response.statusText || "Request failed",
|
|
147
|
+
void 0,
|
|
148
|
+
response,
|
|
149
|
+
response.status
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
const result = await this.handleResponse(
|
|
153
|
+
response,
|
|
154
|
+
returnData ?? false,
|
|
155
|
+
ignoreError,
|
|
156
|
+
onSuccess
|
|
157
|
+
);
|
|
158
|
+
if (this.configure.responseInterceptor && isObject(result)) {
|
|
159
|
+
return await this.configure.responseInterceptor(result);
|
|
160
|
+
}
|
|
161
|
+
return result;
|
|
162
|
+
} catch (error) {
|
|
163
|
+
return this.handleError(error, ignoreError, onError, onLogout);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* 构建完整 URL
|
|
168
|
+
*/
|
|
169
|
+
buildUrl(url) {
|
|
170
|
+
if (typeof url !== "string") {
|
|
171
|
+
throw new TypeError("URL must be a string");
|
|
172
|
+
}
|
|
173
|
+
if (url.startsWith("http://") || url.startsWith("https://")) {
|
|
174
|
+
return url;
|
|
175
|
+
}
|
|
176
|
+
const baseUrl = this.configure.baseUrl;
|
|
177
|
+
if (!baseUrl)
|
|
178
|
+
return url;
|
|
179
|
+
const normalizedBase = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
|
|
180
|
+
const normalizedUrl = url.startsWith("/") ? url : `/${url}`;
|
|
181
|
+
return normalizedBase + normalizedUrl;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* 添加查询参数
|
|
185
|
+
*/
|
|
186
|
+
appendParams(url, params) {
|
|
187
|
+
if (!isObject(params)) {
|
|
188
|
+
throw new TypeError("Params must be an object");
|
|
189
|
+
}
|
|
190
|
+
const searchParams = new URLSearchParams();
|
|
191
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
192
|
+
if (value !== void 0 && value !== null) {
|
|
193
|
+
searchParams.append(key, String(value));
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
const queryString = searchParams.toString();
|
|
197
|
+
if (!queryString)
|
|
198
|
+
return url;
|
|
199
|
+
const separator = url.includes("?") ? "&" : "?";
|
|
200
|
+
return `${url}${separator}${queryString}`;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* 构建请求头
|
|
204
|
+
*/
|
|
205
|
+
async buildHeaders(customHeaders) {
|
|
206
|
+
let baseHeaders;
|
|
207
|
+
const configureHeaders = this.configure.headers;
|
|
208
|
+
if (configureHeaders && isFunction(configureHeaders)) {
|
|
209
|
+
baseHeaders = await configureHeaders();
|
|
210
|
+
} else if (configureHeaders) {
|
|
211
|
+
baseHeaders = configureHeaders;
|
|
212
|
+
}
|
|
213
|
+
const headers = new Headers(baseHeaders);
|
|
214
|
+
if (customHeaders) {
|
|
215
|
+
const custom = new Headers(customHeaders);
|
|
216
|
+
custom.forEach((value, key) => {
|
|
217
|
+
headers.set(key, value);
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
if (!headers.has("Content-Type")) {
|
|
221
|
+
headers.set("Content-Type", DEFAULT_CONTENT_TYPE);
|
|
222
|
+
}
|
|
223
|
+
return headers;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* 带超时的 fetch 请求
|
|
227
|
+
*/
|
|
228
|
+
async fetchWithTimeout(url, options, timeout) {
|
|
229
|
+
const controller = new AbortController();
|
|
230
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
231
|
+
try {
|
|
232
|
+
const response = await fetch(url, {
|
|
233
|
+
...options,
|
|
234
|
+
signal: controller.signal
|
|
235
|
+
});
|
|
236
|
+
return response;
|
|
237
|
+
} catch (error) {
|
|
238
|
+
if (error.name === "AbortError") {
|
|
239
|
+
throw new HttpError(`Request timeout after ${timeout}ms`, void 0, void 0, 408);
|
|
240
|
+
}
|
|
241
|
+
throw error;
|
|
242
|
+
} finally {
|
|
243
|
+
clearTimeout(timeoutId);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* 处理响应数据
|
|
248
|
+
*/
|
|
249
|
+
async handleResponse(response, returnData, ignoreError, onSuccess) {
|
|
250
|
+
var _a, _b;
|
|
251
|
+
const contentType = ((_a = response.headers.get("content-type")) == null ? void 0 : _a.toLowerCase()) ?? "";
|
|
252
|
+
const { codeKey, dataKey, messageKey, codes } = this.configure;
|
|
253
|
+
const successCodes = (codes == null ? void 0 : codes.success) ?? DEFAULT_SUCCESS_CODES;
|
|
254
|
+
const logoutCodes = (codes == null ? void 0 : codes.logout) ?? DEFAULT_LOGOUT_CODES;
|
|
255
|
+
const ignoreErrorCodes = (codes == null ? void 0 : codes.ignoreError) ?? [];
|
|
256
|
+
if ((_b = this.configure.blobContentTypes) == null ? void 0 : _b.some((type) => contentType.includes(type))) {
|
|
257
|
+
const blob = await response.blob();
|
|
258
|
+
const filename = this.extractFilename(response.headers);
|
|
259
|
+
return {
|
|
260
|
+
[codeKey]: successCodes[0],
|
|
261
|
+
data: blob,
|
|
262
|
+
filename,
|
|
263
|
+
response
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
const data = await response.json();
|
|
267
|
+
if (!data.hasOwnProperty(codeKey)) {
|
|
268
|
+
return data;
|
|
269
|
+
}
|
|
270
|
+
const code = data[codeKey ?? DEFAULT_CODE_KEY];
|
|
271
|
+
const message = data[messageKey ?? DEFAULT_MESSAGE_KEY];
|
|
272
|
+
if (successCodes.includes(code)) {
|
|
273
|
+
if (returnData && data.hasOwnProperty(dataKey)) {
|
|
274
|
+
return data[dataKey ?? DEFAULT_DATA_KEY];
|
|
275
|
+
}
|
|
276
|
+
onSuccess == null ? void 0 : onSuccess(data);
|
|
277
|
+
return data;
|
|
278
|
+
}
|
|
279
|
+
if (ignoreError || ignoreErrorCodes.includes(code)) {
|
|
280
|
+
return data;
|
|
281
|
+
}
|
|
282
|
+
if (logoutCodes.includes(code)) {
|
|
283
|
+
throw new HttpError(message || "Unauthorized", code, data);
|
|
284
|
+
}
|
|
285
|
+
throw new HttpError(message || "Request failed", code, data);
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* 从响应头提取文件名
|
|
289
|
+
*/
|
|
290
|
+
extractFilename(headers) {
|
|
291
|
+
const disposition = headers.get("content-disposition");
|
|
292
|
+
if (!disposition)
|
|
293
|
+
return void 0;
|
|
294
|
+
const matches = disposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
|
|
295
|
+
if (!(matches == null ? void 0 : matches[1]))
|
|
296
|
+
return void 0;
|
|
297
|
+
let filename = matches[1].replace(/['"]/g, "");
|
|
298
|
+
try {
|
|
299
|
+
filename = decodeURIComponent(filename);
|
|
300
|
+
} catch {
|
|
301
|
+
}
|
|
302
|
+
return filename;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* 统一错误处理
|
|
306
|
+
*/
|
|
307
|
+
handleError(error, ignoreError, customOnError, customOnLogout) {
|
|
308
|
+
var _a;
|
|
309
|
+
const httpError = error instanceof HttpError ? error : new HttpError(error.message || "Unknown error", void 0, error);
|
|
310
|
+
if (!ignoreError) {
|
|
311
|
+
const logoutCodes = ((_a = this.configure.codes) == null ? void 0 : _a.logout) ?? DEFAULT_LOGOUT_CODES;
|
|
312
|
+
if (httpError.code && logoutCodes.includes(httpError.code)) {
|
|
313
|
+
const logoutHandler = customOnLogout ?? this.configure.onLogout;
|
|
314
|
+
logoutHandler == null ? void 0 : logoutHandler(httpError);
|
|
315
|
+
}
|
|
316
|
+
const errorHandler = customOnError ?? this.configure.onError;
|
|
317
|
+
errorHandler == null ? void 0 : errorHandler(httpError);
|
|
318
|
+
}
|
|
319
|
+
throw httpError;
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
function downloadfile(response) {
|
|
323
|
+
const { data, filename } = response;
|
|
324
|
+
if (!(data instanceof Blob)) {
|
|
325
|
+
throw new TypeError("Data must be a Blob");
|
|
326
|
+
}
|
|
327
|
+
const url = URL.createObjectURL(data);
|
|
328
|
+
const link = document.createElement("a");
|
|
329
|
+
link.href = url;
|
|
330
|
+
link.download = filename || `download-${Date.now()}`;
|
|
331
|
+
link.style.display = "none";
|
|
332
|
+
document.body.appendChild(link);
|
|
333
|
+
link.click();
|
|
334
|
+
setTimeout(() => {
|
|
335
|
+
document.body.removeChild(link);
|
|
336
|
+
URL.revokeObjectURL(url);
|
|
337
|
+
}, 100);
|
|
338
|
+
}
|
|
339
|
+
function createHttpClient(config) {
|
|
340
|
+
return new HttpClient(config);
|
|
341
|
+
}
|
|
342
|
+
var rq = new HttpClient();
|
|
343
|
+
var fetch_default = HttpClient;
|
|
344
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
345
|
+
0 && (module.exports = {
|
|
346
|
+
HttpClient,
|
|
347
|
+
HttpError,
|
|
348
|
+
createHttpClient,
|
|
349
|
+
downloadfile,
|
|
350
|
+
isFunction,
|
|
351
|
+
isObject,
|
|
352
|
+
rq
|
|
353
|
+
});
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
|
|
16
|
+
// src/index.ts
|
|
17
|
+
var src_exports = {};
|
|
18
|
+
module.exports = __toCommonJS(src_exports);
|
|
19
|
+
__reExport(src_exports, require("./fetch"), module.exports);
|
|
20
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
21
|
+
0 && (module.exports = {
|
|
22
|
+
...require("./fetch")
|
|
23
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hyusuyi/rq",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "hyu",
|
|
5
|
+
"module": "./es/index",
|
|
6
|
+
"main": "./lib/index",
|
|
7
|
+
"types": "./es/index.d.ts",
|
|
8
|
+
"repository": "https://github.com/hyusuyi/rq.git",
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"dev": "father dev",
|
|
14
|
+
"build": "sudo father build",
|
|
15
|
+
"build:deps": "father prebundle",
|
|
16
|
+
"prepublishOnly": "father doctor && npm run build"
|
|
17
|
+
},
|
|
18
|
+
"keywords": ["fetch", "request", "rq", "hyu"],
|
|
19
|
+
"authors": [
|
|
20
|
+
"hyu"
|
|
21
|
+
],
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"files": [
|
|
24
|
+
"es",
|
|
25
|
+
"lib",
|
|
26
|
+
"fetch.ts"
|
|
27
|
+
],
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@rc-component/father-plugin": "^2.1.3",
|
|
30
|
+
"father": "^4.6.12",
|
|
31
|
+
"typescript": "^5.9.3"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"prettier": "^3.7.4"
|
|
35
|
+
}
|
|
36
|
+
}
|