CNCEC_APP/uni_modules/uview-pro/libs/request/auto-http.ts

77 lines
2.2 KiB
TypeScript
Raw Permalink Normal View History

2026-03-25 14:54:15 +08:00
import deepMerge from '../function/deepMerge';
export function isFunction(f: any): boolean {
return typeof f === 'function';
}
export function isPromise(p: any): boolean {
return !!(p && p.then && p.catch);
}
export function isArray(arr: any) {
return Object.prototype.toString.call(arr) === '[object Array]';
}
/**
*
*/
class Builder<T> {
instance: any;
constructor(instance: any) {
this.instance = instance;
}
/**
*
* @param urlConfig url
* @param extra
* @returns Object
*/
dispatch(urlConfig: Record<string, any>, extra: Record<string, any> = {}): Record<string, any> {
const builder: Record<string, any> = {};
// 创建 API
Object.keys(urlConfig).forEach(name => {
builder[name] = this.use.bind(this, urlConfig[name]);
});
return { ...builder, ...extra };
}
/**
*
* @param {*} urlConfig : url
* @demo urlConfig = { login: { url: '/user/login', method: 'GET', loading: true } }
* @param {*} config : 开放配置
* @demo api.login({ params: { username: "admin" } })
* @returns Promise
*/
use(urlConfig: Record<string, any>, config: Record<string, any> = {}): Promise<T> {
// 请求地址
let url = config?.url ?? urlConfig.url;
// 兼容 restful url如果是使用url为function则为restful格式
if (config.url && isFunction(config.url)) {
url = `${urlConfig.url}${config.url()}`;
}
// 请求类型get,post,put,delete
const method = config?.method ?? urlConfig?.method ?? 'GET';
// 如果有自定义的工厂函数基础类
const options = { ...deepMerge(urlConfig, config), url, method };
if (isFunction(this.instance) || isPromise(this.instance)) {
return this.instance(options);
}
// 如果是使用的 instance
// 默认的请求基础类
return this.instance.request(options);
}
}
/**
* Http
*/
class AutoHttp {
static get Builder() {
return Builder;
}
constructor() {}
}
export { AutoHttp };