@vtj/utils 0.5.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cdn/index.js +47 -16
- package/lib/index.cjs +51 -0
- package/lib/index.mjs +6844 -0
- package/package.json +22 -13
- package/types/Request.d.ts +115 -0
- package/types/Storage.d.ts +31 -0
- package/types/crypto.d.ts +1 -0
- package/types/dayjs.d.ts +2 -1
- package/types/index.d.ts +6 -4
- package/types/jsonp.d.ts +1 -1
- package/types/numeral.d.ts +3 -0
- package/types/regex.d.ts +11 -0
- package/types/url.d.ts +1 -23
- package/types/util.d.ts +45 -5
- package/types/version.d.ts +1 -0
- package/lib/index.js +0 -4410
- package/types/axios.d.ts +0 -4
- package/types/request.d.ts +0 -47
- package/types/storage.d.ts +0 -46
package/package.json
CHANGED
|
@@ -1,36 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vtj/utils",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.6.0",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
6
7
|
"scripts": {
|
|
7
8
|
"setup": "pnpm install --registry=https://registry.npmmirror.com",
|
|
8
|
-
"dev": "cross-env ENV_TYPE=dev vite",
|
|
9
9
|
"build": "vue-tsc && vite build && pnpm run build:cdn",
|
|
10
|
-
"build:cdn": "vue-tsc && cross-env CDN=true vite build"
|
|
11
|
-
"test:unit": "vitest --environment jsdom",
|
|
12
|
-
"coverage": "vitest run --coverage --environment jsdom"
|
|
10
|
+
"build:cdn": "vue-tsc && cross-env CDN=true vite build"
|
|
13
11
|
},
|
|
14
12
|
"devDependencies": {
|
|
15
|
-
"@
|
|
16
|
-
"@
|
|
13
|
+
"@types/crypto-js": "~4.1.1",
|
|
14
|
+
"@types/js-cookie": "~3.0.3",
|
|
15
|
+
"@types/lodash-es": "~4.17.8",
|
|
16
|
+
"@types/numeral": "~2.0.2",
|
|
17
|
+
"@vtj/cli": "^0.6.0",
|
|
18
|
+
"@vueuse/core": "~10.4.1",
|
|
19
|
+
"axios": "~1.4.0",
|
|
20
|
+
"crypto-js": "~4.1.1",
|
|
21
|
+
"dayjs": "~1.11.9",
|
|
22
|
+
"js-cookie": "~3.0.5",
|
|
23
|
+
"jsencrypt": "~3.3.2",
|
|
24
|
+
"lodash-es": "~4.17.21",
|
|
25
|
+
"numeral": "~2.0.6"
|
|
17
26
|
},
|
|
18
27
|
"files": [
|
|
19
28
|
"lib",
|
|
20
29
|
"types",
|
|
21
30
|
"cdn"
|
|
22
31
|
],
|
|
23
|
-
"main": "lib/index.
|
|
24
|
-
"module": "lib/index.
|
|
25
|
-
"types": "types/index.d.ts",
|
|
32
|
+
"main": "./lib/index.cjs",
|
|
33
|
+
"module": "./lib/index.mjs",
|
|
34
|
+
"types": "./types/index.d.ts",
|
|
26
35
|
"exports": {
|
|
27
36
|
".": {
|
|
28
37
|
"types": "./types/index.d.ts",
|
|
29
|
-
"import": "./lib/index.
|
|
30
|
-
"require": "./lib/index.
|
|
38
|
+
"import": "./lib/index.mjs",
|
|
39
|
+
"require": "./lib/index.cjs"
|
|
31
40
|
}
|
|
32
41
|
},
|
|
33
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "a565aefeb62ea6b2c0df0312fc26788bc180388a",
|
|
34
43
|
"publishConfig": {
|
|
35
44
|
"access": "public"
|
|
36
45
|
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { AxiosInstance, CreateAxiosDefaults, AxiosResponse, AxiosRequestConfig, RawAxiosRequestHeaders, InternalAxiosRequestConfig, CancelTokenSource } from 'axios';
|
|
2
|
+
declare const LOCAL_REQUEST_ID = "Local-Request-Id";
|
|
3
|
+
export interface IRequestSkipWarn {
|
|
4
|
+
executor: (resolve: (value: unknown) => void, reject: (reason?: any) => void) => void;
|
|
5
|
+
code: string | number;
|
|
6
|
+
name?: string;
|
|
7
|
+
callback?: (res: AxiosResponse) => void;
|
|
8
|
+
complete?: () => void;
|
|
9
|
+
}
|
|
10
|
+
export interface IRequestSkipWarnResponse extends AxiosResponse {
|
|
11
|
+
promise: any;
|
|
12
|
+
}
|
|
13
|
+
export interface IResultWrapper<T = any> {
|
|
14
|
+
code: number;
|
|
15
|
+
data: T;
|
|
16
|
+
msg: string;
|
|
17
|
+
success: boolean;
|
|
18
|
+
}
|
|
19
|
+
export type RequestOriginResponse<R = any, D = any> = AxiosResponse<IResultWrapper<R>, D>;
|
|
20
|
+
export interface IRequestSettings {
|
|
21
|
+
/**
|
|
22
|
+
* 发送数据类型
|
|
23
|
+
*/
|
|
24
|
+
type?: 'form' | 'json' | 'data';
|
|
25
|
+
/**
|
|
26
|
+
* 请求头
|
|
27
|
+
*/
|
|
28
|
+
headers?: RawAxiosRequestHeaders | ((config: AxiosRequestConfig) => RawAxiosRequestHeaders);
|
|
29
|
+
/**
|
|
30
|
+
* 是否显示 loading
|
|
31
|
+
*/
|
|
32
|
+
loading?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* 显示 loading
|
|
35
|
+
*/
|
|
36
|
+
showLoading?: () => void;
|
|
37
|
+
/**
|
|
38
|
+
* 关闭 loading
|
|
39
|
+
*/
|
|
40
|
+
hideLoading?: () => void;
|
|
41
|
+
/**
|
|
42
|
+
* 显示失败提示
|
|
43
|
+
*/
|
|
44
|
+
failMessage?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* 自定义失败提示
|
|
47
|
+
*/
|
|
48
|
+
showError?: (msg: string, e: any) => void;
|
|
49
|
+
/**
|
|
50
|
+
* 返回原始 axios 响应对象
|
|
51
|
+
*/
|
|
52
|
+
originResponse?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* 校验响应成功
|
|
55
|
+
*/
|
|
56
|
+
validSuccess?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* 自定义校验方法
|
|
59
|
+
*/
|
|
60
|
+
validate?: (res: AxiosResponse) => boolean;
|
|
61
|
+
/**
|
|
62
|
+
* 请求响应警告执行程序插件
|
|
63
|
+
*/
|
|
64
|
+
skipWarn?: IRequestSkipWarn;
|
|
65
|
+
}
|
|
66
|
+
export interface IRequestOptions extends CreateAxiosDefaults {
|
|
67
|
+
settings?: IRequestSettings;
|
|
68
|
+
}
|
|
69
|
+
export interface IRequestConfig<D = any> extends AxiosRequestConfig<D> {
|
|
70
|
+
settings?: IRequestSettings;
|
|
71
|
+
query?: Record<string, any>;
|
|
72
|
+
}
|
|
73
|
+
export interface IRequestRecord {
|
|
74
|
+
settings: IRequestSettings;
|
|
75
|
+
config: AxiosRequestConfig;
|
|
76
|
+
source: CancelTokenSource;
|
|
77
|
+
}
|
|
78
|
+
export declare class Request {
|
|
79
|
+
axios: AxiosInstance;
|
|
80
|
+
settings: IRequestSettings;
|
|
81
|
+
records: Record<string, IRequestRecord>;
|
|
82
|
+
private isLoading;
|
|
83
|
+
private stopSkipWarn?;
|
|
84
|
+
private showLoading;
|
|
85
|
+
private showError;
|
|
86
|
+
constructor(options?: IRequestOptions);
|
|
87
|
+
setConfig(options?: IRequestOptions): void;
|
|
88
|
+
cancel(id?: string, message?: string): void;
|
|
89
|
+
private createHeaders;
|
|
90
|
+
private isJsonType;
|
|
91
|
+
private toFormData;
|
|
92
|
+
private createSendData;
|
|
93
|
+
private createUrl;
|
|
94
|
+
private openLoading;
|
|
95
|
+
private closeLoading;
|
|
96
|
+
private _showError;
|
|
97
|
+
private validResponse;
|
|
98
|
+
private isSkipWarnResponse;
|
|
99
|
+
send<R = any, D = any>(options?: IRequestConfig<D>, isSkipWarn?: boolean): Promise<R>;
|
|
100
|
+
useResponse(onFulfilled?: ((value: AxiosResponse) => AxiosResponse | Promise<AxiosResponse>) | null, onRejected?: ((error: any) => any) | null): () => void;
|
|
101
|
+
useRequest(onFulfilled?: ((value: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>) | null, onRejected?: ((error: any) => any) | null): () => void;
|
|
102
|
+
private setupSkipWarn;
|
|
103
|
+
}
|
|
104
|
+
export interface IStaticRequest extends Request {
|
|
105
|
+
(options: IRequestConfig): Promise<AxiosResponse>;
|
|
106
|
+
instance: Request;
|
|
107
|
+
}
|
|
108
|
+
export declare function createRequest(options?: IRequestOptions): IStaticRequest;
|
|
109
|
+
export declare const request: IStaticRequest;
|
|
110
|
+
export declare function createApi<R = any, D = any>(config: string | IRequestConfig): (data?: D, opts?: IRequestConfig) => Promise<R>;
|
|
111
|
+
export interface IApiMap {
|
|
112
|
+
[name: string]: string | IRequestConfig;
|
|
113
|
+
}
|
|
114
|
+
export declare function createApis(map: IApiMap): Record<string, (data?: unknown, opts?: IRequestConfig<any> | undefined) => Promise<unknown>>;
|
|
115
|
+
export { LOCAL_REQUEST_ID };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface StorageOptions {
|
|
2
|
+
/**
|
|
3
|
+
* 存储类型
|
|
4
|
+
*/
|
|
5
|
+
type: 'cache' | 'local' | 'session';
|
|
6
|
+
/**
|
|
7
|
+
* 在多少毫秒后失效, 0为永不过期
|
|
8
|
+
*/
|
|
9
|
+
expired: number;
|
|
10
|
+
/**
|
|
11
|
+
* key前缀
|
|
12
|
+
*/
|
|
13
|
+
prefix: string;
|
|
14
|
+
}
|
|
15
|
+
export interface StorageTypes {
|
|
16
|
+
local: any;
|
|
17
|
+
session: any;
|
|
18
|
+
cache: any;
|
|
19
|
+
}
|
|
20
|
+
export declare class Storage {
|
|
21
|
+
options: StorageOptions;
|
|
22
|
+
private caches;
|
|
23
|
+
private types;
|
|
24
|
+
constructor(options?: Partial<StorageOptions>);
|
|
25
|
+
config(options?: Partial<StorageOptions>): void;
|
|
26
|
+
save(key: string, value: any, opts?: Partial<StorageOptions>): void;
|
|
27
|
+
get(key: string, opts?: Partial<StorageOptions>): any;
|
|
28
|
+
remove(key: string, opts?: Partial<StorageOptions>): void;
|
|
29
|
+
clear(opts?: Partial<StorageOptions>): void;
|
|
30
|
+
}
|
|
31
|
+
export declare const storage: Storage;
|
package/types/crypto.d.ts
CHANGED
package/types/dayjs.d.ts
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
export * from './version';
|
|
1
2
|
export * from './util';
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './request';
|
|
3
|
+
export * from './Request';
|
|
4
4
|
export * from './raf';
|
|
5
|
+
export * from './dayjs';
|
|
6
|
+
export * from './numeral';
|
|
7
|
+
export * from './Storage';
|
|
8
|
+
export * from './regex';
|
|
5
9
|
export * as cookie from './cookie';
|
|
6
|
-
export * as storage from './storage';
|
|
7
10
|
export * as crypto from './crypto';
|
|
8
11
|
export * as url from './url';
|
|
9
|
-
export { dayjs } from './dayjs';
|
|
10
12
|
export { jsonp } from './jsonp';
|
package/types/jsonp.d.ts
CHANGED
package/types/regex.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const URL_REGEX: RegExp;
|
|
2
|
+
export declare const EMAIL_REGEX: RegExp;
|
|
3
|
+
export declare const ID_NUMBER_REGEX: RegExp;
|
|
4
|
+
export declare const MOBILE_PHONE_REGEX: RegExp;
|
|
5
|
+
export declare const CAR_NUMBER_NEW_REGEX: RegExp;
|
|
6
|
+
export declare const CAR_NUMBER_REGEX: RegExp;
|
|
7
|
+
export declare function isUrl(str: string): boolean;
|
|
8
|
+
export declare function isEmail(str: string): boolean;
|
|
9
|
+
export declare function isIdNumber(str: string): boolean;
|
|
10
|
+
export declare function isMobilePhone(str: string): boolean;
|
|
11
|
+
export declare function isCarNo(str: string): boolean;
|
package/types/url.d.ts
CHANGED
|
@@ -2,43 +2,21 @@ export declare const UrlRegex: RegExp;
|
|
|
2
2
|
export declare function isUrl(txt: string): boolean;
|
|
3
3
|
/**
|
|
4
4
|
* 获取当前页面的 host
|
|
5
|
-
* @param {boolean} includePath 带上 pathname
|
|
6
|
-
* @return {string}
|
|
7
5
|
*/
|
|
8
|
-
export declare function getCurrentHost(includePath: boolean): string;
|
|
6
|
+
export declare function getCurrentHost(includePath: boolean): string | null;
|
|
9
7
|
/**
|
|
10
8
|
* 获取指定url的host
|
|
11
|
-
* @param {string} url
|
|
12
|
-
* @return {string} host
|
|
13
9
|
*/
|
|
14
10
|
export declare function getHost(url?: string): string;
|
|
15
11
|
/**
|
|
16
12
|
* 键值对转换成查询字符串
|
|
17
|
-
* @param {object} query 键值对,对象
|
|
18
|
-
* @returns {string} 查询参数字符串
|
|
19
13
|
*/
|
|
20
14
|
export declare function stringify(query: Record<string, any>): string;
|
|
21
15
|
/**
|
|
22
16
|
* 参数字符串转换成对象形式,如:a=1&b=2 转换成 {a:1, b:2}
|
|
23
|
-
* @param {String} str 需要转换的字符串
|
|
24
|
-
* @param {String} [sep=&] 连接符,可选,默认 &
|
|
25
|
-
* @param {String} [eq==] 键值间隔符,可选,默认 =
|
|
26
|
-
* @returns {Object}
|
|
27
17
|
*/
|
|
28
18
|
export declare function parse(str: string, sep?: string, eq?: string): Record<string, any>;
|
|
29
19
|
/**
|
|
30
20
|
* 在url追加参数
|
|
31
|
-
* @param {string} url 原本的url
|
|
32
|
-
* @param {string|object} query 需要追加的参数,Object|String
|
|
33
|
-
* @returns {string} 追加参数后的url
|
|
34
21
|
*/
|
|
35
22
|
export declare function append(url: string, query: string | Record<string, any>): string;
|
|
36
|
-
declare const _default: {
|
|
37
|
-
isUrl: typeof isUrl;
|
|
38
|
-
getCurrentHost: typeof getCurrentHost;
|
|
39
|
-
getHost: typeof getHost;
|
|
40
|
-
stringify: typeof stringify;
|
|
41
|
-
parse: typeof parse;
|
|
42
|
-
append: typeof append;
|
|
43
|
-
};
|
|
44
|
-
export default _default;
|
package/types/util.d.ts
CHANGED
|
@@ -1,17 +1,54 @@
|
|
|
1
1
|
import { upperFirst, camelCase } from 'lodash-es';
|
|
2
2
|
export { get, set, isPlainObject, cloneDeep, merge, debounce, throttle, isEqual, template, lowerFirst, kebabCase } from 'lodash-es';
|
|
3
3
|
export { upperFirst, camelCase };
|
|
4
|
+
/**
|
|
5
|
+
* 是否浏览器环境
|
|
6
|
+
*/
|
|
7
|
+
export declare const isClient: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* 已定义
|
|
10
|
+
* @param val
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
export declare const isDef: (val: unknown) => boolean;
|
|
14
|
+
/**
|
|
15
|
+
* 当前时间
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
export declare const now: () => number;
|
|
19
|
+
/**
|
|
20
|
+
* 时间戳
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
export declare const timestamp: () => number;
|
|
24
|
+
/**
|
|
25
|
+
* 随机数
|
|
26
|
+
* @param min
|
|
27
|
+
* @param max
|
|
28
|
+
* @returns
|
|
29
|
+
*/
|
|
30
|
+
export declare const rand: (min: number, max: number) => number;
|
|
4
31
|
export declare function uid(): string;
|
|
5
32
|
export declare function uuid(split?: boolean): string;
|
|
6
|
-
export declare function isFunction(val: any):
|
|
33
|
+
export declare function isFunction(val: any): val is (...args: any[]) => any;
|
|
34
|
+
export declare function isObject(value: any): value is Record<string, unknown>;
|
|
35
|
+
export declare function isString(value: any): value is string;
|
|
36
|
+
export declare function isUndefined(value: any): boolean;
|
|
7
37
|
export declare function upperFirstCamelCase(name: string): string;
|
|
8
38
|
/**
|
|
9
|
-
*
|
|
10
|
-
* @param
|
|
11
|
-
* @param
|
|
39
|
+
* 对象排除属性
|
|
40
|
+
* @param target 需要处理的对象
|
|
41
|
+
* @param keys 需要排除的属性名称
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
export declare function omit<T extends Record<string, any>, K extends Record<string, any>>(target: T, keys: string[] | ((k: string, v: any) => boolean)): K;
|
|
45
|
+
/**
|
|
46
|
+
* 对象提取属性
|
|
47
|
+
* @param target
|
|
48
|
+
* @param keys
|
|
12
49
|
* @returns
|
|
13
50
|
*/
|
|
14
|
-
export declare function pick
|
|
51
|
+
export declare function pick<T extends Record<string, any>, K extends Record<string, any>>(target: T, keys: string[] | ((k: string, v: any) => boolean)): K;
|
|
15
52
|
/**
|
|
16
53
|
* 递归对象或数组清除文本类型值的两边空格
|
|
17
54
|
* @param {Object|Array} obj
|
|
@@ -23,3 +60,6 @@ export declare function trim(obj: any): any;
|
|
|
23
60
|
*/
|
|
24
61
|
export declare function toFixed(value: number, number: number | undefined, round: boolean): number;
|
|
25
62
|
export declare function delay(val?: number): Promise<unknown>;
|
|
63
|
+
export declare function arrayToMap<T>(data: T[], prop: keyof T): Map<any, T>;
|
|
64
|
+
export declare function mapToObject<V = any>(map: Map<any, V>): Record<any, V>;
|
|
65
|
+
export declare function dedupArray<T>(array: any[], prop?: keyof T): any[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const version = "0.6.0";
|