6 changed files with 212 additions and 3 deletions
@ -0,0 +1,62 @@ |
|||
import axios, { AxiosInstance, CreateAxiosDefaults } from "axios"; |
|||
import { deepAssign } from "./utils"; |
|||
|
|||
export const enum IHttpMethod { |
|||
GET = "GET", |
|||
POST = "POST", |
|||
DELETE = "DELETE", |
|||
PUT = "PUT", |
|||
} |
|||
|
|||
export enum IHttpContentType { |
|||
FORM = "application/x-www-form-urlencoded", |
|||
JSON = "application/json", |
|||
} |
|||
|
|||
class Plugin { |
|||
beforeInit() { } |
|||
} |
|||
|
|||
export abstract class PluginsManager { |
|||
static #plugins: Plugin[] = [] |
|||
static use(plugin: Plugin) { |
|||
this.#plugins.push(plugin) |
|||
} |
|||
|
|||
callPlugin(key: keyof Plugin, ...argus: any[]) { |
|||
PluginsManager.#plugins.forEach(p => p[key].apply(this, argus as [])) |
|||
} |
|||
} |
|||
|
|||
export abstract class httpBase extends PluginsManager { |
|||
static get method() { |
|||
return { |
|||
GET: "GET", |
|||
POST: "POST", |
|||
DELETE: "DELETE", |
|||
PUT: "PUT", |
|||
} |
|||
} |
|||
|
|||
static get contentType() { |
|||
return { |
|||
FORM: "application/x-www-form-urlencoded", |
|||
JSON: "application/json", |
|||
} |
|||
} |
|||
|
|||
initDefaultConfig: CreateAxiosDefaults = { |
|||
baseURL: process.env.VUE_APP_BASEURL, |
|||
timeout: 10000, |
|||
headers: { |
|||
'Content-Type': httpBase.contentType.FORM, |
|||
} |
|||
} |
|||
|
|||
instance: AxiosInstance | null = null |
|||
|
|||
create<T>(config?: CreateAxiosDefaults<T>) { |
|||
this.instance = axios.create(deepAssign<CreateAxiosDefaults<T>>(axios.defaults, this.initDefaultConfig, config ?? {})) |
|||
return this.instance |
|||
} |
|||
} |
@ -0,0 +1,37 @@ |
|||
import { httpBase, PluginsManager } from "./base" |
|||
|
|||
export const enum IHttpMethod { |
|||
GET = "GET", |
|||
POST = "POST", |
|||
DELETE = "DELETE", |
|||
PUT = "PUT", |
|||
} |
|||
|
|||
export enum IHttpContentType { |
|||
FORM = "application/x-www-form-urlencoded", |
|||
JSON = "application/json", |
|||
} |
|||
|
|||
|
|||
const FlyPoll: Map<string, any> = new Map() |
|||
|
|||
class Fly extends httpBase { |
|||
constructor() { |
|||
super() |
|||
this.Init() |
|||
} |
|||
static get(name?: string): Fly { |
|||
if (!name) name = "$defalt" |
|||
if (!FlyPoll.has(name)) { |
|||
FlyPoll.set(name, new Fly()) |
|||
} |
|||
return FlyPoll.get(name) |
|||
} |
|||
Init() { |
|||
this.callPlugin("beforeInit") |
|||
this.create() |
|||
} |
|||
} |
|||
|
|||
Fly.get() |
|||
|
@ -0,0 +1,36 @@ |
|||
|
|||
/** |
|||
* 实现一个深度拷贝的 assign 函数 |
|||
* @param {Object} target 目标对象 |
|||
* @param {...Object} sources 源对象列表 |
|||
* @return {Object} 返回深度拷贝后的目标对象 |
|||
*/ |
|||
export function deepAssign<T = Record<string, any>>(target: T, ...sources: T[]) { |
|||
if(target === null) return target; |
|||
// 如果目标不是对象或数组,则直接返回以避免错误
|
|||
if (typeof target !== 'object' || Array.isArray(target)) { |
|||
return target; |
|||
} |
|||
// 遍历源对象列表
|
|||
sources.forEach(source => { |
|||
if (source && typeof source === 'object') { |
|||
for (let key in source) { |
|||
if (source.hasOwnProperty(key)) { |
|||
const value = source[key]; |
|||
// 如果值是对象或数组,则进行递归拷贝
|
|||
if (typeof value === 'object' && !Array.isArray(value) && value !== null) { |
|||
if (!target[key]) { |
|||
target[key] = (Array.isArray(value) ? ([] as any) : {}); |
|||
} |
|||
deepAssign(target[key], value); |
|||
} else { |
|||
// 否则直接赋值
|
|||
target[key] = value; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
}); |
|||
|
|||
return target; |
|||
} |
Loading…
Reference in new issue