luch-request.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. import "./chunk-VX4GZB4L.js";
  2. // D:/CHB/出入金1/商城前端/前端/h5mall_uniapp/node_modules/luch-request/src/lib/utils.js
  3. var toString = Object.prototype.toString;
  4. function isArray(val) {
  5. return toString.call(val) === "[object Array]";
  6. }
  7. function isObject(val) {
  8. return val !== null && typeof val === "object";
  9. }
  10. function isDate(val) {
  11. return toString.call(val) === "[object Date]";
  12. }
  13. function isURLSearchParams(val) {
  14. return typeof URLSearchParams !== "undefined" && val instanceof URLSearchParams;
  15. }
  16. function forEach(obj, fn) {
  17. if (obj === null || typeof obj === "undefined") {
  18. return;
  19. }
  20. if (typeof obj !== "object") {
  21. obj = [obj];
  22. }
  23. if (isArray(obj)) {
  24. for (var i = 0, l = obj.length; i < l; i++) {
  25. fn.call(null, obj[i], i, obj);
  26. }
  27. } else {
  28. for (var key in obj) {
  29. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  30. fn.call(null, obj[key], key, obj);
  31. }
  32. }
  33. }
  34. }
  35. function isPlainObject(obj) {
  36. return Object.prototype.toString.call(obj) === "[object Object]";
  37. }
  38. function deepMerge() {
  39. let result = {};
  40. function assignValue(val, key) {
  41. if (typeof result[key] === "object" && typeof val === "object") {
  42. result[key] = deepMerge(result[key], val);
  43. } else if (typeof val === "object") {
  44. result[key] = deepMerge({}, val);
  45. } else {
  46. result[key] = val;
  47. }
  48. }
  49. for (let i = 0, l = arguments.length; i < l; i++) {
  50. forEach(arguments[i], assignValue);
  51. }
  52. return result;
  53. }
  54. function isUndefined(val) {
  55. return typeof val === "undefined";
  56. }
  57. // D:/CHB/出入金1/商城前端/前端/h5mall_uniapp/node_modules/luch-request/src/lib/helpers/buildURL.js
  58. function encode(val) {
  59. return encodeURIComponent(val).replace(/%40/gi, "@").replace(/%3A/gi, ":").replace(/%24/g, "$").replace(/%2C/gi, ",").replace(/%20/g, "+").replace(/%5B/gi, "[").replace(/%5D/gi, "]");
  60. }
  61. function buildURL(url, params, paramsSerializer) {
  62. if (!params) {
  63. return url;
  64. }
  65. var serializedParams;
  66. if (paramsSerializer) {
  67. serializedParams = paramsSerializer(params);
  68. } else if (isURLSearchParams(params)) {
  69. serializedParams = params.toString();
  70. } else {
  71. var parts = [];
  72. forEach(params, function serialize(val, key) {
  73. if (val === null || typeof val === "undefined") {
  74. return;
  75. }
  76. if (isArray(val)) {
  77. key = key + "[]";
  78. } else {
  79. val = [val];
  80. }
  81. forEach(val, function parseValue(v) {
  82. if (isDate(v)) {
  83. v = v.toISOString();
  84. } else if (isObject(v)) {
  85. v = JSON.stringify(v);
  86. }
  87. parts.push(encode(key) + "=" + encode(v));
  88. });
  89. });
  90. serializedParams = parts.join("&");
  91. }
  92. if (serializedParams) {
  93. var hashmarkIndex = url.indexOf("#");
  94. if (hashmarkIndex !== -1) {
  95. url = url.slice(0, hashmarkIndex);
  96. }
  97. url += (url.indexOf("?") === -1 ? "?" : "&") + serializedParams;
  98. }
  99. return url;
  100. }
  101. // D:/CHB/出入金1/商城前端/前端/h5mall_uniapp/node_modules/luch-request/src/lib/helpers/isAbsoluteURL.js
  102. function isAbsoluteURL(url) {
  103. return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(url);
  104. }
  105. // D:/CHB/出入金1/商城前端/前端/h5mall_uniapp/node_modules/luch-request/src/lib/helpers/combineURLs.js
  106. function combineURLs(baseURL, relativeURL) {
  107. return relativeURL ? baseURL.replace(/\/+$/, "") + "/" + relativeURL.replace(/^\/+/, "") : baseURL;
  108. }
  109. // D:/CHB/出入金1/商城前端/前端/h5mall_uniapp/node_modules/luch-request/src/lib/core/buildFullPath.js
  110. function buildFullPath(baseURL, requestedURL) {
  111. if (baseURL && !isAbsoluteURL(requestedURL)) {
  112. return combineURLs(baseURL, requestedURL);
  113. }
  114. return requestedURL;
  115. }
  116. // D:/CHB/出入金1/商城前端/前端/h5mall_uniapp/node_modules/luch-request/src/lib/core/settle.js
  117. function settle(resolve, reject, response) {
  118. const validateStatus2 = response.config.validateStatus;
  119. const status = response.statusCode;
  120. if (status && (!validateStatus2 || validateStatus2(status))) {
  121. resolve(response);
  122. } else {
  123. reject(response);
  124. }
  125. }
  126. // D:/CHB/出入金1/商城前端/前端/h5mall_uniapp/node_modules/luch-request/src/lib/adapters/index.js
  127. var mergeKeys = (keys, config2) => {
  128. let config = {};
  129. keys.forEach((prop) => {
  130. if (!isUndefined(config2[prop])) {
  131. config[prop] = config2[prop];
  132. }
  133. });
  134. return config;
  135. };
  136. var adapters_default = (config) => {
  137. return new Promise((resolve, reject) => {
  138. let fullPath = buildURL(buildFullPath(config.baseURL, config.url), config.params, config.paramsSerializer);
  139. const _config = {
  140. url: fullPath,
  141. header: config.header,
  142. complete: (response) => {
  143. config.fullPath = fullPath;
  144. response.config = config;
  145. response.rawData = response.data;
  146. try {
  147. let jsonParseHandle = false;
  148. const forcedJSONParsingType = typeof config.forcedJSONParsing;
  149. if (forcedJSONParsingType === "boolean") {
  150. jsonParseHandle = config.forcedJSONParsing;
  151. } else if (forcedJSONParsingType === "object") {
  152. const includesMethod = config.forcedJSONParsing.include || [];
  153. jsonParseHandle = includesMethod.includes(config.method);
  154. }
  155. if (jsonParseHandle && typeof response.data === "string") {
  156. response.data = JSON.parse(response.data);
  157. }
  158. } catch (e) {
  159. }
  160. settle(resolve, reject, response);
  161. }
  162. };
  163. let requestTask;
  164. if (config.method === "UPLOAD") {
  165. delete _config.header["content-type"];
  166. delete _config.header["Content-Type"];
  167. let otherConfig = {
  168. filePath: config.filePath,
  169. name: config.name
  170. };
  171. const optionalKeys = [
  172. "files",
  173. "file",
  174. "timeout",
  175. "formData"
  176. ];
  177. requestTask = uni.uploadFile({ ..._config, ...otherConfig, ...mergeKeys(optionalKeys, config) });
  178. } else if (config.method === "DOWNLOAD") {
  179. const optionalKeys = [
  180. "timeout"
  181. ];
  182. requestTask = uni.downloadFile({ ..._config, ...mergeKeys(optionalKeys, config) });
  183. } else {
  184. const optionalKeys = [
  185. "data",
  186. "method",
  187. "timeout",
  188. "dataType",
  189. "responseType",
  190. "withCredentials"
  191. ];
  192. requestTask = uni.request({ ..._config, ...mergeKeys(optionalKeys, config) });
  193. }
  194. if (config.getTask) {
  195. config.getTask(requestTask, config);
  196. }
  197. });
  198. };
  199. // D:/CHB/出入金1/商城前端/前端/h5mall_uniapp/node_modules/luch-request/src/lib/core/dispatchRequest.js
  200. var dispatchRequest_default = (config) => {
  201. return adapters_default(config);
  202. };
  203. // D:/CHB/出入金1/商城前端/前端/h5mall_uniapp/node_modules/luch-request/src/lib/core/InterceptorManager.js
  204. function InterceptorManager() {
  205. this.handlers = [];
  206. }
  207. InterceptorManager.prototype.use = function use(fulfilled, rejected) {
  208. this.handlers.push({
  209. fulfilled,
  210. rejected
  211. });
  212. return this.handlers.length - 1;
  213. };
  214. InterceptorManager.prototype.eject = function eject(id) {
  215. if (this.handlers[id]) {
  216. this.handlers[id] = null;
  217. }
  218. };
  219. InterceptorManager.prototype.forEach = function forEach2(fn) {
  220. this.handlers.forEach((h) => {
  221. if (h !== null) {
  222. fn(h);
  223. }
  224. });
  225. };
  226. var InterceptorManager_default = InterceptorManager;
  227. // D:/CHB/出入金1/商城前端/前端/h5mall_uniapp/node_modules/luch-request/src/lib/core/mergeConfig.js
  228. var mergeKeys2 = (keys, globalsConfig, config2) => {
  229. let config = {};
  230. keys.forEach((prop) => {
  231. if (!isUndefined(config2[prop])) {
  232. config[prop] = config2[prop];
  233. } else if (!isUndefined(globalsConfig[prop])) {
  234. config[prop] = globalsConfig[prop];
  235. }
  236. });
  237. return config;
  238. };
  239. var mergeConfig_default = (globalsConfig, config2 = {}) => {
  240. const method = config2.method || globalsConfig.method || "GET";
  241. let config = {
  242. baseURL: config2.baseURL || globalsConfig.baseURL || "",
  243. method,
  244. url: config2.url || "",
  245. params: config2.params || {},
  246. custom: { ...globalsConfig.custom || {}, ...config2.custom || {} },
  247. header: deepMerge(globalsConfig.header || {}, config2.header || {})
  248. };
  249. const defaultToConfig2Keys = ["getTask", "validateStatus", "paramsSerializer", "forcedJSONParsing"];
  250. config = { ...config, ...mergeKeys2(defaultToConfig2Keys, globalsConfig, config2) };
  251. if (method === "DOWNLOAD") {
  252. const downloadKeys = [
  253. "timeout"
  254. ];
  255. config = { ...config, ...mergeKeys2(downloadKeys, globalsConfig, config2) };
  256. } else if (method === "UPLOAD") {
  257. delete config.header["content-type"];
  258. delete config.header["Content-Type"];
  259. const uploadKeys = [
  260. "files",
  261. "file",
  262. "filePath",
  263. "name",
  264. "timeout",
  265. "formData"
  266. ];
  267. uploadKeys.forEach((prop) => {
  268. if (!isUndefined(config2[prop])) {
  269. config[prop] = config2[prop];
  270. }
  271. });
  272. if (isUndefined(config.timeout) && !isUndefined(globalsConfig.timeout)) {
  273. config["timeout"] = globalsConfig["timeout"];
  274. }
  275. } else {
  276. const defaultsKeys = [
  277. "data",
  278. "timeout",
  279. "dataType",
  280. "responseType",
  281. "withCredentials"
  282. ];
  283. config = { ...config, ...mergeKeys2(defaultsKeys, globalsConfig, config2) };
  284. }
  285. return config;
  286. };
  287. // D:/CHB/出入金1/商城前端/前端/h5mall_uniapp/node_modules/luch-request/src/lib/core/defaults.js
  288. var defaults_default = {
  289. baseURL: "",
  290. header: {},
  291. method: "GET",
  292. dataType: "json",
  293. paramsSerializer: null,
  294. responseType: "text",
  295. custom: {},
  296. timeout: 6e4,
  297. withCredentials: false,
  298. validateStatus: function validateStatus(status) {
  299. return status >= 200 && status < 300;
  300. },
  301. forcedJSONParsing: true
  302. };
  303. // D:/CHB/出入金1/商城前端/前端/h5mall_uniapp/node_modules/luch-request/src/lib/utils/clone.js
  304. var clone = function() {
  305. "use strict";
  306. function _instanceof(obj, type) {
  307. return type != null && obj instanceof type;
  308. }
  309. var nativeMap;
  310. try {
  311. nativeMap = Map;
  312. } catch (_) {
  313. nativeMap = function() {
  314. };
  315. }
  316. var nativeSet;
  317. try {
  318. nativeSet = Set;
  319. } catch (_) {
  320. nativeSet = function() {
  321. };
  322. }
  323. var nativePromise;
  324. try {
  325. nativePromise = Promise;
  326. } catch (_) {
  327. nativePromise = function() {
  328. };
  329. }
  330. function clone2(parent, circular, depth, prototype, includeNonEnumerable) {
  331. if (typeof circular === "object") {
  332. depth = circular.depth;
  333. prototype = circular.prototype;
  334. includeNonEnumerable = circular.includeNonEnumerable;
  335. circular = circular.circular;
  336. }
  337. var allParents = [];
  338. var allChildren = [];
  339. var useBuffer = typeof Buffer != "undefined";
  340. if (typeof circular == "undefined")
  341. circular = true;
  342. if (typeof depth == "undefined")
  343. depth = Infinity;
  344. function _clone(parent2, depth2) {
  345. if (parent2 === null)
  346. return null;
  347. if (depth2 === 0)
  348. return parent2;
  349. var child;
  350. var proto;
  351. if (typeof parent2 != "object") {
  352. return parent2;
  353. }
  354. if (_instanceof(parent2, nativeMap)) {
  355. child = new nativeMap();
  356. } else if (_instanceof(parent2, nativeSet)) {
  357. child = new nativeSet();
  358. } else if (_instanceof(parent2, nativePromise)) {
  359. child = new nativePromise(function(resolve, reject) {
  360. parent2.then(function(value) {
  361. resolve(_clone(value, depth2 - 1));
  362. }, function(err) {
  363. reject(_clone(err, depth2 - 1));
  364. });
  365. });
  366. } else if (clone2.__isArray(parent2)) {
  367. child = [];
  368. } else if (clone2.__isRegExp(parent2)) {
  369. child = new RegExp(parent2.source, __getRegExpFlags(parent2));
  370. if (parent2.lastIndex)
  371. child.lastIndex = parent2.lastIndex;
  372. } else if (clone2.__isDate(parent2)) {
  373. child = new Date(parent2.getTime());
  374. } else if (useBuffer && Buffer.isBuffer(parent2)) {
  375. if (Buffer.from) {
  376. child = Buffer.from(parent2);
  377. } else {
  378. child = new Buffer(parent2.length);
  379. parent2.copy(child);
  380. }
  381. return child;
  382. } else if (_instanceof(parent2, Error)) {
  383. child = Object.create(parent2);
  384. } else {
  385. if (typeof prototype == "undefined") {
  386. proto = Object.getPrototypeOf(parent2);
  387. child = Object.create(proto);
  388. } else {
  389. child = Object.create(prototype);
  390. proto = prototype;
  391. }
  392. }
  393. if (circular) {
  394. var index = allParents.indexOf(parent2);
  395. if (index != -1) {
  396. return allChildren[index];
  397. }
  398. allParents.push(parent2);
  399. allChildren.push(child);
  400. }
  401. if (_instanceof(parent2, nativeMap)) {
  402. parent2.forEach(function(value, key) {
  403. var keyChild = _clone(key, depth2 - 1);
  404. var valueChild = _clone(value, depth2 - 1);
  405. child.set(keyChild, valueChild);
  406. });
  407. }
  408. if (_instanceof(parent2, nativeSet)) {
  409. parent2.forEach(function(value) {
  410. var entryChild = _clone(value, depth2 - 1);
  411. child.add(entryChild);
  412. });
  413. }
  414. for (var i in parent2) {
  415. var attrs = Object.getOwnPropertyDescriptor(parent2, i);
  416. if (attrs) {
  417. child[i] = _clone(parent2[i], depth2 - 1);
  418. }
  419. try {
  420. var objProperty = Object.getOwnPropertyDescriptor(parent2, i);
  421. if (objProperty.set === "undefined") {
  422. continue;
  423. }
  424. child[i] = _clone(parent2[i], depth2 - 1);
  425. } catch (e) {
  426. if (e instanceof TypeError) {
  427. continue;
  428. } else if (e instanceof ReferenceError) {
  429. continue;
  430. }
  431. }
  432. }
  433. if (Object.getOwnPropertySymbols) {
  434. var symbols = Object.getOwnPropertySymbols(parent2);
  435. for (var i = 0; i < symbols.length; i++) {
  436. var symbol = symbols[i];
  437. var descriptor = Object.getOwnPropertyDescriptor(parent2, symbol);
  438. if (descriptor && !descriptor.enumerable && !includeNonEnumerable) {
  439. continue;
  440. }
  441. child[symbol] = _clone(parent2[symbol], depth2 - 1);
  442. Object.defineProperty(child, symbol, descriptor);
  443. }
  444. }
  445. if (includeNonEnumerable) {
  446. var allPropertyNames = Object.getOwnPropertyNames(parent2);
  447. for (var i = 0; i < allPropertyNames.length; i++) {
  448. var propertyName = allPropertyNames[i];
  449. var descriptor = Object.getOwnPropertyDescriptor(parent2, propertyName);
  450. if (descriptor && descriptor.enumerable) {
  451. continue;
  452. }
  453. child[propertyName] = _clone(parent2[propertyName], depth2 - 1);
  454. Object.defineProperty(child, propertyName, descriptor);
  455. }
  456. }
  457. return child;
  458. }
  459. return _clone(parent, depth);
  460. }
  461. clone2.clonePrototype = function clonePrototype(parent) {
  462. if (parent === null)
  463. return null;
  464. var c = function() {
  465. };
  466. c.prototype = parent;
  467. return new c();
  468. };
  469. function __objToStr(o) {
  470. return Object.prototype.toString.call(o);
  471. }
  472. clone2.__objToStr = __objToStr;
  473. function __isDate(o) {
  474. return typeof o === "object" && __objToStr(o) === "[object Date]";
  475. }
  476. clone2.__isDate = __isDate;
  477. function __isArray(o) {
  478. return typeof o === "object" && __objToStr(o) === "[object Array]";
  479. }
  480. clone2.__isArray = __isArray;
  481. function __isRegExp(o) {
  482. return typeof o === "object" && __objToStr(o) === "[object RegExp]";
  483. }
  484. clone2.__isRegExp = __isRegExp;
  485. function __getRegExpFlags(re) {
  486. var flags = "";
  487. if (re.global)
  488. flags += "g";
  489. if (re.ignoreCase)
  490. flags += "i";
  491. if (re.multiline)
  492. flags += "m";
  493. return flags;
  494. }
  495. clone2.__getRegExpFlags = __getRegExpFlags;
  496. return clone2;
  497. }();
  498. var clone_default = clone;
  499. // D:/CHB/出入金1/商城前端/前端/h5mall_uniapp/node_modules/luch-request/src/lib/core/Request.js
  500. var Request = class {
  501. constructor(arg = {}) {
  502. if (!isPlainObject(arg)) {
  503. arg = {};
  504. console.warn("\u8BBE\u7F6E\u5168\u5C40\u53C2\u6570\u5FC5\u987B\u63A5\u6536\u4E00\u4E2AObject");
  505. }
  506. this.config = clone_default({ ...defaults_default, ...arg });
  507. this.interceptors = {
  508. request: new InterceptorManager_default(),
  509. response: new InterceptorManager_default()
  510. };
  511. }
  512. setConfig(f) {
  513. this.config = f(this.config);
  514. }
  515. middleware(config) {
  516. config = mergeConfig_default(this.config, config);
  517. let chain = [dispatchRequest_default, void 0];
  518. let promise = Promise.resolve(config);
  519. this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
  520. chain.unshift(interceptor.fulfilled, interceptor.rejected);
  521. });
  522. this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
  523. chain.push(interceptor.fulfilled, interceptor.rejected);
  524. });
  525. while (chain.length) {
  526. promise = promise.then(chain.shift(), chain.shift());
  527. }
  528. return promise;
  529. }
  530. request(config = {}) {
  531. return this.middleware(config);
  532. }
  533. get(url, options = {}) {
  534. return this.middleware({
  535. url,
  536. method: "GET",
  537. ...options
  538. });
  539. }
  540. post(url, data, options = {}) {
  541. return this.middleware({
  542. url,
  543. data,
  544. method: "POST",
  545. ...options
  546. });
  547. }
  548. put(url, data, options = {}) {
  549. return this.middleware({
  550. url,
  551. data,
  552. method: "PUT",
  553. ...options
  554. });
  555. }
  556. delete(url, data, options = {}) {
  557. return this.middleware({
  558. url,
  559. data,
  560. method: "DELETE",
  561. ...options
  562. });
  563. }
  564. connect(url, data, options = {}) {
  565. return this.middleware({
  566. url,
  567. data,
  568. method: "CONNECT",
  569. ...options
  570. });
  571. }
  572. head(url, data, options = {}) {
  573. return this.middleware({
  574. url,
  575. data,
  576. method: "HEAD",
  577. ...options
  578. });
  579. }
  580. options(url, data, options = {}) {
  581. return this.middleware({
  582. url,
  583. data,
  584. method: "OPTIONS",
  585. ...options
  586. });
  587. }
  588. trace(url, data, options = {}) {
  589. return this.middleware({
  590. url,
  591. data,
  592. method: "TRACE",
  593. ...options
  594. });
  595. }
  596. upload(url, config = {}) {
  597. config.url = url;
  598. config.method = "UPLOAD";
  599. return this.middleware(config);
  600. }
  601. download(url, config = {}) {
  602. config.url = url;
  603. config.method = "DOWNLOAD";
  604. return this.middleware(config);
  605. }
  606. get version() {
  607. return "3.1.0";
  608. }
  609. };
  610. // D:/CHB/出入金1/商城前端/前端/h5mall_uniapp/node_modules/luch-request/src/lib/luch-request.js
  611. var luch_request_default = Request;
  612. export {
  613. luch_request_default as default
  614. };
  615. //# sourceMappingURL=luch-request.js.map