Skip to content

Commit

Permalink
feat(types & deepclone): 全局类型声明及深克隆函数
Browse files Browse the repository at this point in the history
  • Loading branch information
dyggod committed Jul 21, 2022
1 parent 4f73056 commit cc92289
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 41 deletions.
45 changes: 11 additions & 34 deletions src/types/common.d.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,20 @@
import { MessageApi } from 'ant-design-vue';

export interface Options {
label?: string;
value?: string | number;
}

export interface StrOptions extends Options {
value: string
}

export interface NumOptions extends Options {
value: number
}

export type Func = () => void;

export interface Uncertainty {
[key: string]: any;
}

export interface UncertaintyStr {
[key: string]: string;
}

export interface BootStrapOptions {
message: MessageApi
}
message: MessageApi
}

export interface ReqInterceptor {
onFulfilled: (config: AxiosRequestConfig, options: BootStrapOptions) => void;
onRejected: (error: Error, options: BootStrapOptions) => void;
}
onFulfilled: (config: AxiosRequestConfig, options: BootStrapOptions) => void;
onRejected: (error: Error, options: BootStrapOptions) => void;
}

export interface ResInterceptor {
onFulfilled: (response: AxiosResponse, options: BootStrapOptions) => void;
onRejected: (error: Error, options: BootStrapOptions) => void;
}
onFulfilled: (response: AxiosResponse, options: BootStrapOptions) => void;
onRejected: (error: Error, options: BootStrapOptions) => void;
}

export interface Interceptors {
reqInterceptors: ReqInterceptor[];
resInterceptors: ResInterceptor[];
}
reqInterceptors: ReqInterceptor[];
resInterceptors: ResInterceptor[];
}
7 changes: 0 additions & 7 deletions src/types/datastructure.ts

This file was deleted.

28 changes: 28 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 选择项类型
declare interface Options {
label?: string;
value?: string | number;
}

// 值是字符串的选择项类型
declare interface StrOptions extends Options {
value: string;
}

// 值是数字的选择项类型
declare interface NumOptions extends Options {
value: number;
}

// 无返回值的函数类型
declare type Func = () => void;

// 不定值的对象类型
declare interface Uncertainty {
[key: string]: any;
}

// 定字符串值的对象类型
declare interface UncertaintyStr {
[key: string]: string;
}
37 changes: 37 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 深度复制
* @param {*} obj
* @returns object
*/
export function deepClone(params: Uncertainty): Uncertainty | Uncertainty[] {
if (!params) {
throw new Error('params is empty');
}

if (typeof params !== 'object') {
throw new Error('params is not object');
}

if (params.constructor === Date) {
return new Date(params);
}

if (params.constructor === RegExp) {
return new RegExp(params);
}

const newObject: Uncertainty | Uncertainty[] = Array.isArray(params) ? [] : {};

Object.keys(params).forEach((key) => {
if (Object.prototype.hasOwnProperty.call(params, key)) {
// @ts-ignore
newObject[key] = typeof params[key] === 'object' ? deepClone(params[key]) : params[key];
}
});

return newObject;
}

export const common = {
deepClone,
};
13 changes: 13 additions & 0 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import { Interceptors, BootStrapOptions } from '#/common';

/**
* 对参数对象做uri编码
* @param {object} params
* @returns string
*/
function encodeUriParams(params: Uncertainty): string {
return Object.keys(params).filter(
(it) => Object.prototype.hasOwnProperty.call(params, it),
).reduce((pre, curr) => (
params[curr] ? `${(pre ? `${pre}&` : '') + curr}=${encodeURIComponent(params[curr])}` : pre), '');
}

// 请求配置
axios.defaults.baseURL = String(import.meta.env.VITE_API_BASE_URL);
axios.defaults.withCredentials = true;
axios.defaults.timeout = 5000;
axios.defaults.paramsSerializer = encodeUriParams; // 重载get参数编码方式

// HTTP状态码
export enum HttpStatus {
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"noEmit": true,
"baseUrl": ".",
"jsx": "react",
"typeRoots": [
"./node_modules/@types",
"./src/types",
],
"paths": {
"@/*": [
"src/*"
Expand Down

0 comments on commit cc92289

Please sign in to comment.