@cmtlyt/cl-utils
Version:
145 lines (144 loc) • 5.96 kB
JavaScript
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "./baseFunc"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createInstance = void 0;
// 基于fetch封装请求对象
const baseFunc_1 = require("./baseFunc");
var EMethods;
(function (EMethods) {
EMethods[EMethods["Get"] = 0] = "Get";
EMethods[EMethods["Post"] = 1] = "Post";
EMethods[EMethods["Put"] = 2] = "Put";
EMethods[EMethods["Delete"] = 3] = "Delete";
})(EMethods || (EMethods = {}));
const methodMap = {
[EMethods.Get]: 'get',
[EMethods.Post]: 'post',
[EMethods.Put]: 'put',
[EMethods.Delete]: 'delete',
};
const instanceMap = {};
class FetchRequest {
constructor(config) {
this.cacheMap = {};
this.requester = this.genRequest(config);
}
get intercept() {
return {
get request() {
const handle = [];
return {
handle,
use(...func) {
handle.push(...func.flat(Infinity));
},
};
},
get response() {
const handle = [];
return {
handle,
use(...func) {
handle.push(...func.flat(Infinity));
},
};
},
};
}
genRequest(config) {
let { cacheGet } = config, defalutConfig = __rest(config, ["cacheGet"]);
return (url, config_) => {
return new Promise((resolve, reject) => {
var _a;
const { cacheData, skipCached } = config_, currentConfig = __rest(config_, ["cacheData", "skipCached"]);
const { body } = currentConfig;
const hasCache = !skipCached && (cacheData || (cacheGet && ((_a = currentConfig.method) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'get'));
if (hasCache) {
const key = url + body;
const cacheData = this.cacheMap[key];
if (cacheData) {
return Promise.resolve(cacheData);
}
}
const defalutConfig_ = Object.create(defalutConfig);
fetch(url, Object.assign(defalutConfig_, currentConfig))
.then(res => {
const pres = res.json();
if (hasCache) {
pres.then(res => {
this.cacheMap[url + body] = res;
});
}
resolve(pres);
})
.catch(reject);
});
};
}
request(option) {
var _a;
option = this.intercept.request.handle.reduce((prev, handle) => handle(prev), option);
option = ((_a = option.beforeRequest) === null || _a === void 0 ? void 0 : _a.call(option)) || option;
let { url, method } = option, _b = option.config, { data, params } = _b, otherConfig = __rest(_b, ["data", "params"]);
otherConfig.method = methodMap[method];
if (method === EMethods.Post || method === EMethods.Put) {
otherConfig.body = JSON.stringify(data);
}
if (params) {
url += (0, baseFunc_1.encodeParams)(params);
}
return new Promise((resolve, reject) => {
this.requester(url, otherConfig)
.then(res => {
var _a;
res = this.intercept.response.handle.reduce((prev, handle) => handle(prev), res);
resolve(((_a = option.afterRequest) === null || _a === void 0 ? void 0 : _a.call(option)) || res);
})
.catch(reject);
});
}
get(url, config) {
return this.request({ method: EMethods.Get, url, config });
}
post(url, data, config) {
return this.request({ method: EMethods.Post, url, config: Object.assign({ data }, config) });
}
put(url, data, config) {
return this.request({ method: EMethods.Put, url, config: Object.assign({ data }, config) });
}
delete(url, config) {
return this.request({ method: EMethods.Delete, url, config });
}
}
/**
* 创建一个实例
* @param {IInitRequestConfig} config
* @param {string} name
* @returns {FetchRequest}
*/
function createInstance(config, name = '_defalut') {
var _a;
return ((_a = instanceMap[name]) !== null && _a !== void 0 ? _a : (instanceMap[name] = new FetchRequest(config)));
}
exports.createInstance = createInstance;
exports.default = createInstance({});
});