@anjianshi/utils 3.0.0 → 3.0.2
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/env-browser/device.d.ts +24 -0
- package/env-browser/device.js +50 -0
- package/env-browser/global.d.ts +10 -0
- package/env-browser/global.js +15 -0
- package/env-browser/load-script.d.ts +5 -0
- package/env-browser/load-script.js +13 -0
- package/env-browser/logging.d.ts +18 -0
- package/env-browser/logging.js +49 -0
- package/env-browser/manage-vconsole.d.ts +16 -0
- package/env-browser/manage-vconsole.js +38 -0
- package/env-node/crypto-random.d.ts +13 -0
- package/env-node/crypto-random.js +28 -0
- package/env-node/fs.d.ts +19 -0
- package/env-node/fs.js +48 -0
- package/env-node/index.d.ts +5 -0
- package/env-node/index.js +5 -0
- package/env-node/logging/handlers.d.ts +58 -0
- package/env-node/logging/handlers.js +154 -0
- package/env-node/logging/index.d.ts +11 -0
- package/env-node/logging/index.js +14 -0
- package/{src/env-react/emotion-register-globals.ts → env-react/emotion-register-globals.d.ts} +2 -5
- package/env-react/emotion-register-globals.js +5 -0
- package/env-react/emotion.d.ts +20 -0
- package/env-react/emotion.jsx +34 -0
- package/env-react/hooks.d.ts +23 -0
- package/env-react/hooks.js +47 -0
- package/env-react/index.d.ts +1 -0
- package/env-react/index.js +1 -0
- package/env-react/react-register-globals.d.ts +21 -0
- package/env-react/react-register-globals.js +19 -0
- package/env-service/controllers.d.ts +30 -0
- package/env-service/controllers.js +41 -0
- package/env-service/env-reader.d.ts +55 -0
- package/env-service/env-reader.js +79 -0
- package/env-service/index.d.ts +6 -0
- package/env-service/index.js +6 -0
- package/env-service/prisma/adapt-logging.d.ts +21 -0
- package/env-service/prisma/adapt-logging.js +30 -0
- package/env-service/prisma/extensions/exist.d.ts +10 -0
- package/env-service/prisma/extensions/exist.js +16 -0
- package/env-service/prisma/extensions/find-and-count.d.ts +7 -0
- package/env-service/prisma/extensions/find-and-count.js +19 -0
- package/env-service/prisma/extensions/soft-delete.d.ts +52 -0
- package/env-service/prisma/extensions/soft-delete.js +123 -0
- package/env-service/prisma/extensions/with-transaction.d.ts +9 -0
- package/env-service/prisma/extensions/with-transaction.js +54 -0
- package/env-service/prisma/index.d.ts +6 -0
- package/env-service/prisma/index.js +6 -0
- package/env-service/prisma/transaction-contexted.d.ts +11 -0
- package/env-service/prisma/transaction-contexted.js +52 -0
- package/env-service/redis-cache.d.ts +39 -0
- package/env-service/redis-cache.js +116 -0
- package/env-service/tasks.d.ts +12 -0
- package/env-service/tasks.js +37 -0
- package/index.d.ts +4 -0
- package/index.js +4 -0
- package/init-dayjs.d.ts +2 -0
- package/init-dayjs.js +7 -0
- package/lang/async.d.ts +19 -0
- package/lang/async.js +34 -0
- package/lang/color.d.ts +37 -0
- package/lang/color.js +111 -0
- package/lang/index.d.ts +8 -0
- package/lang/index.js +8 -0
- package/lang/object.d.ts +12 -0
- package/lang/object.js +41 -0
- package/lang/random.d.ts +13 -0
- package/lang/random.js +24 -0
- package/lang/result.d.ts +47 -0
- package/lang/result.js +45 -0
- package/lang/string.d.ts +29 -0
- package/lang/string.js +92 -0
- package/lang/time.d.ts +10 -0
- package/lang/time.js +18 -0
- package/{src/lang/types.ts → lang/types.d.ts} +23 -43
- package/lang/types.js +28 -0
- package/logging/adapt.d.ts +10 -0
- package/logging/adapt.js +43 -0
- package/logging/formatters.d.ts +10 -0
- package/logging/formatters.js +22 -0
- package/logging/index.d.ts +45 -0
- package/logging/index.js +90 -0
- package/md5.d.ts +30 -0
- package/md5.js +308 -0
- package/package.json +10 -19
- package/safe-request.d.ts +53 -0
- package/safe-request.js +140 -0
- package/url.d.ts +77 -0
- package/url.js +149 -0
- package/validators/array.d.ts +30 -0
- package/validators/array.js +47 -0
- package/validators/base.d.ts +82 -0
- package/validators/base.js +42 -0
- package/validators/boolean.d.ts +3 -0
- package/validators/boolean.js +22 -0
- package/validators/datetime.d.ts +12 -0
- package/validators/datetime.js +30 -0
- package/validators/factory.d.ts +70 -0
- package/validators/factory.js +121 -0
- package/validators/index.d.ts +9 -0
- package/validators/index.js +9 -0
- package/validators/number.d.ts +19 -0
- package/validators/number.js +26 -0
- package/validators/object.d.ts +28 -0
- package/validators/object.js +49 -0
- package/validators/one-of.d.ts +10 -0
- package/validators/one-of.js +15 -0
- package/validators/string.d.ts +22 -0
- package/validators/string.js +35 -0
- package/README.md +0 -10
- package/eslint.config.cjs +0 -33
- package/publish-prepare.cjs +0 -16
- package/src/env-browser/device.ts +0 -62
- package/src/env-browser/global.ts +0 -21
- package/src/env-browser/load-script.ts +0 -13
- package/src/env-browser/logging.ts +0 -58
- package/src/env-browser/manage-vconsole.ts +0 -54
- package/src/env-node/crypto-random.ts +0 -30
- package/src/env-node/fs.ts +0 -50
- package/src/env-node/index.ts +0 -5
- package/src/env-node/logging/handlers.ts +0 -190
- package/src/env-node/logging/index.ts +0 -16
- package/src/env-react/emotion.tsx +0 -42
- package/src/env-react/hooks.ts +0 -59
- package/src/env-react/index.ts +0 -1
- package/src/env-react/react-register-globals.ts +0 -53
- package/src/env-service/controllers.ts +0 -93
- package/src/env-service/env-reader.ts +0 -141
- package/src/env-service/index.ts +0 -6
- package/src/env-service/prisma/adapt-logging.ts +0 -39
- package/src/env-service/prisma/extensions/exist.ts +0 -21
- package/src/env-service/prisma/extensions/find-and-count.ts +0 -24
- package/src/env-service/prisma/extensions/soft-delete.ts +0 -162
- package/src/env-service/prisma/extensions/with-transaction.ts +0 -65
- package/src/env-service/prisma/index.ts +0 -6
- package/src/env-service/prisma/transaction-contexted.ts +0 -80
- package/src/env-service/redis-cache.ts +0 -142
- package/src/env-service/tasks.ts +0 -45
- package/src/index.ts +0 -4
- package/src/init-dayjs.ts +0 -8
- package/src/lang/async.ts +0 -47
- package/src/lang/color.ts +0 -119
- package/src/lang/index.ts +0 -8
- package/src/lang/object.ts +0 -39
- package/src/lang/random.ts +0 -25
- package/src/lang/result.ts +0 -78
- package/src/lang/string.ts +0 -95
- package/src/lang/time.ts +0 -19
- package/src/logging/adapt.ts +0 -49
- package/src/logging/formatters.ts +0 -23
- package/src/logging/index.ts +0 -106
- package/src/md5.ts +0 -318
- package/src/safe-request.ts +0 -193
- package/src/url.ts +0 -185
- package/src/validators/array.ts +0 -97
- package/src/validators/base.ts +0 -145
- package/src/validators/boolean.ts +0 -21
- package/src/validators/datetime.ts +0 -39
- package/src/validators/factory.ts +0 -244
- package/src/validators/index.ts +0 -9
- package/src/validators/number.ts +0 -54
- package/src/validators/object.ts +0 -101
- package/src/validators/one-of.ts +0 -33
- package/src/validators/string.ts +0 -72
package/safe-request.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { sleep } from './lang/async.js';
|
|
2
|
+
import { failed, handleException } from './lang/result.js';
|
|
3
|
+
import { getLogger } from './logging/index.js';
|
|
4
|
+
import { combineUrl } from './url.js';
|
|
5
|
+
/**
|
|
6
|
+
* 建立一个请求发起器,并可预设部分选项。
|
|
7
|
+
* 可以继承此类来自定义默认的错误处理逻辑。
|
|
8
|
+
*
|
|
9
|
+
* 请求失败时的 Failed 对象,其 code 为 HTTP status,没有 status 时为 0
|
|
10
|
+
* data 为解析出的响应内容,没有或解析失败则为 undefined
|
|
11
|
+
*/
|
|
12
|
+
export class SafeRequestClient {
|
|
13
|
+
logger;
|
|
14
|
+
prefefinedOptions;
|
|
15
|
+
constructor(options = {}) {
|
|
16
|
+
this.logger = getLogger(options.loggerName ?? 'request');
|
|
17
|
+
this.prefefinedOptions = options;
|
|
18
|
+
}
|
|
19
|
+
/** 生成一个快捷方式函数,调用它相当于调用 client.request() */
|
|
20
|
+
asFunction() {
|
|
21
|
+
return async (inputUrl, inputOptions) => this.request(inputUrl, inputOptions);
|
|
22
|
+
}
|
|
23
|
+
async request(inputUrl, inputOptions) {
|
|
24
|
+
const options = await this.formatOptions({
|
|
25
|
+
url: inputUrl,
|
|
26
|
+
...(inputOptions ?? {}),
|
|
27
|
+
});
|
|
28
|
+
const { url, method, headers, body, timeout } = options;
|
|
29
|
+
try {
|
|
30
|
+
// 发起请求
|
|
31
|
+
const request = fetch(url, { method, headers, body });
|
|
32
|
+
let response;
|
|
33
|
+
try {
|
|
34
|
+
response = await (typeof timeout === 'number'
|
|
35
|
+
? Promise.race([request, sleep(timeout)])
|
|
36
|
+
: request);
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
// 处理“请求发起失败”
|
|
40
|
+
return this.onRequestError(error, url);
|
|
41
|
+
}
|
|
42
|
+
// 处理超时
|
|
43
|
+
if (response === undefined) {
|
|
44
|
+
return this.onTimeout(url);
|
|
45
|
+
}
|
|
46
|
+
// 处理“服务端返回失败状态”
|
|
47
|
+
if (!response.status.toString().startsWith('2')) {
|
|
48
|
+
// 此时服务端仍可能输出一些内容,试着解析出来
|
|
49
|
+
const responseDataRes = await this.parseResponse(options, response);
|
|
50
|
+
const responseData = responseDataRes.success ? responseDataRes.data : undefined;
|
|
51
|
+
return await this.onResponseError(url, response, responseData);
|
|
52
|
+
}
|
|
53
|
+
// 解析响应内容
|
|
54
|
+
return await this.parseResponse(options, response);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
this.logger.error('Unexpected error', error);
|
|
58
|
+
return failed('Request handle failed.');
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async formatOptions(input) {
|
|
62
|
+
const predefined = this.prefefinedOptions;
|
|
63
|
+
const { urlPrefix = predefined.urlPrefix ?? '', url: rawUrl, query = {}, method = predefined.method ?? 'GET', headers: rawHeaders = {}, body: rawBody = null, data, timeout = predefined.timeout ?? 0, } = input;
|
|
64
|
+
const headers = {
|
|
65
|
+
...(predefined.headers ?? {}),
|
|
66
|
+
...rawHeaders,
|
|
67
|
+
};
|
|
68
|
+
let body = rawBody;
|
|
69
|
+
if (data !== undefined) {
|
|
70
|
+
if (method === 'GET') {
|
|
71
|
+
Object.assign(query, data);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
body = data instanceof FormData ? data : JSON.stringify(data);
|
|
75
|
+
headers['Content-Type'] = 'application/json; charset=utf-8';
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const url = combineUrl(urlPrefix + (rawUrl ?? ''), query);
|
|
79
|
+
const options = {
|
|
80
|
+
method,
|
|
81
|
+
url,
|
|
82
|
+
headers,
|
|
83
|
+
body,
|
|
84
|
+
timeout,
|
|
85
|
+
};
|
|
86
|
+
Object.assign(options.headers, await this.getHeaders(options, input));
|
|
87
|
+
return options;
|
|
88
|
+
}
|
|
89
|
+
/** 请求发起前调用此方法补充 Headers 内容 */
|
|
90
|
+
getHeaders(
|
|
91
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
92
|
+
options,
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
94
|
+
inputOptions) {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
async parseResponse(options, response) {
|
|
98
|
+
let result;
|
|
99
|
+
result = await handleException(response.json());
|
|
100
|
+
if (result.success)
|
|
101
|
+
return result;
|
|
102
|
+
const contentType = (response.headers.get('Content-Type') ?? '').toLowerCase().trim();
|
|
103
|
+
if (contentType.startsWith('text/') || contentType === '') {
|
|
104
|
+
result = (await handleException(response.text()));
|
|
105
|
+
if (result.success)
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
result = (await handleException(response.blob()));
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
/** 若请求未成功发起,会触发此回调来生成失败信息 */
|
|
112
|
+
onRequestError(error, url) {
|
|
113
|
+
this.logger.error('Request Failed', { url, error });
|
|
114
|
+
return failed('Request Failed', 0, undefined);
|
|
115
|
+
}
|
|
116
|
+
/** 请求成功发起,但服务端返回失败状态(如 500),会触发此回调来生成失败信息 */
|
|
117
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
118
|
+
async onResponseError(url, response, responseData) {
|
|
119
|
+
this.logger.error('Response Error Status', {
|
|
120
|
+
url,
|
|
121
|
+
status: response.status,
|
|
122
|
+
data: responseData,
|
|
123
|
+
});
|
|
124
|
+
return failed(`Response Error Status - ${response.status}`, response.status, responseData);
|
|
125
|
+
}
|
|
126
|
+
/** 服务端返回内容解析失败时,会触发此回调来生成失败信息 */
|
|
127
|
+
onParseFailed(error, response, url) {
|
|
128
|
+
this.logger.error('Response Parse Failed', { url, response, error });
|
|
129
|
+
return failed('Response Parse Failed', response.status, undefined);
|
|
130
|
+
}
|
|
131
|
+
/** 处理超时 */
|
|
132
|
+
onTimeout(url) {
|
|
133
|
+
this.logger.warn('Request Timeout', url);
|
|
134
|
+
return failed('Request Timeout', 0, undefined);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* 模块自带一个可直接调用发起请求的函数,跳过初始化实例
|
|
139
|
+
*/
|
|
140
|
+
export const safeRequest = new SafeRequestClient().asFunction();
|
package/url.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 从 URL 中解析出 query 对象
|
|
3
|
+
* 注意:不带 ? 号的纯 query 内容需手动加上 ? 再传入。
|
|
4
|
+
*
|
|
5
|
+
* [array]
|
|
6
|
+
* 是否把重复出现的 key 保存为数组(默认不开启)
|
|
7
|
+
* a=1&a=2 => { a: [1,2] }
|
|
8
|
+
*
|
|
9
|
+
* [loose]
|
|
10
|
+
* 是否开启“宽松模式”(默认不开启)
|
|
11
|
+
* 1. hash 里的内容也会被解析,以兼容拼接错误的 URL(把 query 拼到了 hash 后面)。
|
|
12
|
+
* 2. 出现多个 ? 符号时,会把 ? 也当做 & 分隔符(index.html?a=1&b=2?c=3)
|
|
13
|
+
*
|
|
14
|
+
* [decode]
|
|
15
|
+
* 是否对 query 值进行 decode(默认开启)
|
|
16
|
+
*/
|
|
17
|
+
declare function parseQuery(url: string, options?: {
|
|
18
|
+
array?: false;
|
|
19
|
+
loose?: boolean;
|
|
20
|
+
decode?: boolean;
|
|
21
|
+
}): Record<string, string>;
|
|
22
|
+
declare function parseQuery(url: string, options: {
|
|
23
|
+
array: true;
|
|
24
|
+
loose?: boolean;
|
|
25
|
+
decode?: boolean;
|
|
26
|
+
}): Record<string, string | string[]>;
|
|
27
|
+
export { parseQuery };
|
|
28
|
+
/**
|
|
29
|
+
* 把对象合并成 query string。
|
|
30
|
+
* - 支持字符串、数值、布尔值、数组。
|
|
31
|
+
* - 布尔值会替换成 0 和 1。
|
|
32
|
+
* - 数组会多次赋值:{ a: [1,2,3] } => 'a=1&a=2&a=3',不支持嵌套数组
|
|
33
|
+
* - encode 为 true 时会对 value 执行 encodeURIComponent(默认为 true)
|
|
34
|
+
*/
|
|
35
|
+
export type StringifyVal = string | number | boolean;
|
|
36
|
+
export type StringifyQuery = Record<string, StringifyVal | StringifyVal[] | undefined>;
|
|
37
|
+
export declare function stringifyQuery(obj: StringifyQuery, encode?: boolean): string;
|
|
38
|
+
/**
|
|
39
|
+
* 拆分 URL 的各个部分
|
|
40
|
+
*
|
|
41
|
+
* bare 为 true,则 search 不带 '?',hash 不带 '#'
|
|
42
|
+
* 否则和 location.search / hash 一样
|
|
43
|
+
* (默认为 true)
|
|
44
|
+
*/
|
|
45
|
+
export declare function splitUrl(url: string, bare?: boolean): {
|
|
46
|
+
base: string;
|
|
47
|
+
search: string;
|
|
48
|
+
hash: string;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* 把 query 和 hash 内容合并到 url 上
|
|
52
|
+
*
|
|
53
|
+
* query object 与现有 search 合并,替换同名项(值为数组的,用新数组代替老的,不会合并数组)
|
|
54
|
+
* hash string 带不带开头的 '#' 皆可。会代替 url 已有的 hash。
|
|
55
|
+
*/
|
|
56
|
+
export declare function combineUrl(origUrl: string, query?: StringifyQuery, hash?: string): string;
|
|
57
|
+
/**
|
|
58
|
+
* 移除路径中所有非必须的 "/"
|
|
59
|
+
* 清理后的字符串只有这几种可能的格式:''、'abc'、'abc/def'
|
|
60
|
+
* 例如 /abc/def 和 abc/def/ 都会变成 abc/def
|
|
61
|
+
*
|
|
62
|
+
* 注意:此操作不会统一大小写,因此不保证处理后两个字符串在代码层面全等(a === b)
|
|
63
|
+
*/
|
|
64
|
+
export declare function clearSlash(path: string): string;
|
|
65
|
+
/**
|
|
66
|
+
* 合并几段路径,保证合并处只有一个斜杠
|
|
67
|
+
*/
|
|
68
|
+
export declare function joinPath(...nodes: string[]): string;
|
|
69
|
+
/**
|
|
70
|
+
* decodeURIComponent() 对于不规范编码的字符串可能会报错(例如字符串里出现了“%”)
|
|
71
|
+
* 用此函数代替可避免此问题
|
|
72
|
+
*/
|
|
73
|
+
export declare function safeDecode(content: string): string;
|
|
74
|
+
/**
|
|
75
|
+
* 将 URL 中的 http:// 协议改成 https://
|
|
76
|
+
*/
|
|
77
|
+
export declare function ensureHttps(url: string | undefined): string | undefined;
|
package/url.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* URL 工具函数
|
|
3
|
+
* 部分灵感来自:https://www.npmjs.com/package/qs
|
|
4
|
+
*
|
|
5
|
+
* [名词定义]
|
|
6
|
+
* query: 指 a=1&b=2 格式的“查询字符串”或此类字符串的解析结果。
|
|
7
|
+
* search: URL 中的 search 部分,与 location.search 一致,空字符串或以 '?' 开头。
|
|
8
|
+
* hash: URL 中的 hash 部分,与 location.hash 一直,空字符串或以 '#' 开头。
|
|
9
|
+
*/
|
|
10
|
+
import isPlainObject from 'lodash/isPlainObject.js';
|
|
11
|
+
function parseQuery(url, options) {
|
|
12
|
+
const { array = false, loose = false, decode = true } = options ?? {};
|
|
13
|
+
// 正常状态下,将仅剩 a=1&b=1(即不会再有 ? 和 #);loose 模式下,可能为 a=1&b=2#c=3?d=4
|
|
14
|
+
const queryString = (loose ? /(\?|#)(.+)/ : /(\?)(.+?)(#|$)/).exec(url)?.[2] ?? '';
|
|
15
|
+
if (!queryString)
|
|
16
|
+
return {};
|
|
17
|
+
const query = {};
|
|
18
|
+
const reg = /([^#?&]*)=([^#?&]*)/g;
|
|
19
|
+
let re = reg.exec(queryString);
|
|
20
|
+
while (re) {
|
|
21
|
+
const [name, rawValue] = [re[1], re[2]];
|
|
22
|
+
const value = decode ? safeDecode(rawValue) : rawValue;
|
|
23
|
+
if (array && query[name] !== undefined) {
|
|
24
|
+
const prev = query[name];
|
|
25
|
+
query[name] = Array.isArray(prev) ? [...prev, value] : [prev, value];
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
query[name] = value;
|
|
29
|
+
}
|
|
30
|
+
re = reg.exec(queryString);
|
|
31
|
+
}
|
|
32
|
+
return query;
|
|
33
|
+
}
|
|
34
|
+
export { parseQuery };
|
|
35
|
+
export function stringifyQuery(obj, encode = true) {
|
|
36
|
+
if (!isPlainObject(obj))
|
|
37
|
+
return '';
|
|
38
|
+
return (Object.entries(obj)
|
|
39
|
+
// 过滤值为 undefined 的项目,使其完全不出现在最终的 query 中
|
|
40
|
+
.filter((entry) => entry[1] !== undefined)
|
|
41
|
+
.map(([name, value]) => stringifyQueryItem(name, value, encode))
|
|
42
|
+
.join('&'));
|
|
43
|
+
}
|
|
44
|
+
function stringifyQueryItem(name, value, encode) {
|
|
45
|
+
if (Array.isArray(value))
|
|
46
|
+
return value.map(subValue => stringifyQueryItem(name, subValue, encode)).join('&');
|
|
47
|
+
if (typeof value === 'boolean')
|
|
48
|
+
value = value ? '1' : '0';
|
|
49
|
+
if (typeof value === 'number')
|
|
50
|
+
value = value.toString();
|
|
51
|
+
if (encode)
|
|
52
|
+
value = encodeURIComponent(value);
|
|
53
|
+
return `${name}=${value}`;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 拆分 URL 的各个部分
|
|
57
|
+
*
|
|
58
|
+
* bare 为 true,则 search 不带 '?',hash 不带 '#'
|
|
59
|
+
* 否则和 location.search / hash 一样
|
|
60
|
+
* (默认为 true)
|
|
61
|
+
*/
|
|
62
|
+
export function splitUrl(url, bare = true) {
|
|
63
|
+
let hashIndex = url.indexOf('#');
|
|
64
|
+
if (hashIndex === -1)
|
|
65
|
+
hashIndex = url.length;
|
|
66
|
+
const bareHash = url.slice(hashIndex + 1);
|
|
67
|
+
let searchIndex = url.slice(0, hashIndex).indexOf('?');
|
|
68
|
+
if (searchIndex === -1)
|
|
69
|
+
searchIndex = hashIndex;
|
|
70
|
+
const bareSearch = url.slice(searchIndex + 1, hashIndex);
|
|
71
|
+
return {
|
|
72
|
+
base: url.slice(0, searchIndex),
|
|
73
|
+
search: bare ? bareSearch : bareSearch ? '?' + bareSearch : '',
|
|
74
|
+
hash: bare ? bareHash : bareHash ? '#' + bareHash : '',
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* 把 query 和 hash 内容合并到 url 上
|
|
79
|
+
*
|
|
80
|
+
* query object 与现有 search 合并,替换同名项(值为数组的,用新数组代替老的,不会合并数组)
|
|
81
|
+
* hash string 带不带开头的 '#' 皆可。会代替 url 已有的 hash。
|
|
82
|
+
*/
|
|
83
|
+
export function combineUrl(origUrl, query = {}, hash = '') {
|
|
84
|
+
if (hash.startsWith('#'))
|
|
85
|
+
hash = hash.slice(1);
|
|
86
|
+
// 拆分原 url 的 search、hash
|
|
87
|
+
const { base, search: origSearch, hash: origHash } = splitUrl(origUrl);
|
|
88
|
+
// 拼接新 URL
|
|
89
|
+
let newUrl = base;
|
|
90
|
+
const newSearch = stringifyQuery({ ...parseQuery(origSearch), ...query });
|
|
91
|
+
const newHash = hash || origHash;
|
|
92
|
+
if (newSearch)
|
|
93
|
+
newUrl += `?${newSearch}`;
|
|
94
|
+
if (newHash)
|
|
95
|
+
newUrl += `#${newHash}`;
|
|
96
|
+
return newUrl;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* 移除路径中所有非必须的 "/"
|
|
100
|
+
* 清理后的字符串只有这几种可能的格式:''、'abc'、'abc/def'
|
|
101
|
+
* 例如 /abc/def 和 abc/def/ 都会变成 abc/def
|
|
102
|
+
*
|
|
103
|
+
* 注意:此操作不会统一大小写,因此不保证处理后两个字符串在代码层面全等(a === b)
|
|
104
|
+
*/
|
|
105
|
+
export function clearSlash(path) {
|
|
106
|
+
if (path.startsWith('/'))
|
|
107
|
+
path = path.slice(1);
|
|
108
|
+
if (path.endsWith('/'))
|
|
109
|
+
path = path.slice(0, -1);
|
|
110
|
+
path = path.replace(/\/+/g, '/');
|
|
111
|
+
return path;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* 合并几段路径,保证合并处只有一个斜杠
|
|
115
|
+
*/
|
|
116
|
+
export function joinPath(...nodes) {
|
|
117
|
+
// - node 为 '' 时忽略 node
|
|
118
|
+
// - path 可能的格式:'' 'a' 'a/' ‘/a/'
|
|
119
|
+
// - path 为 '',则 node 开头 '/' 保持原样
|
|
120
|
+
// - 否则,根据 path 结尾有没有 '/',决定 node 开头带不带 '/'
|
|
121
|
+
// - node 开头、结尾若有多个 '/' 均替换成单个
|
|
122
|
+
return nodes.reduce((path, node) => {
|
|
123
|
+
if (!node)
|
|
124
|
+
return path;
|
|
125
|
+
const [, origPrefix, content, origSuffix] = /^(\/*)(.*?)(\/*)$/.exec(node);
|
|
126
|
+
const prefix = (path === '' ? !!origPrefix : !path.endsWith('/')) ? '/' : '';
|
|
127
|
+
const suffix = origSuffix ? '/' : '';
|
|
128
|
+
const result = `${path}${prefix}${content}${suffix}`;
|
|
129
|
+
return result;
|
|
130
|
+
}, '');
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* decodeURIComponent() 对于不规范编码的字符串可能会报错(例如字符串里出现了“%”)
|
|
134
|
+
* 用此函数代替可避免此问题
|
|
135
|
+
*/
|
|
136
|
+
export function safeDecode(content) {
|
|
137
|
+
try {
|
|
138
|
+
return decodeURIComponent(content);
|
|
139
|
+
}
|
|
140
|
+
catch (e) {
|
|
141
|
+
return content;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* 将 URL 中的 http:// 协议改成 https://
|
|
146
|
+
*/
|
|
147
|
+
export function ensureHttps(url) {
|
|
148
|
+
return url?.replace(/http:\/\//g, 'https://');
|
|
149
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type CommonOptions, type Validator, type Validated } from './base.js';
|
|
2
|
+
/** 验证元素数量任意、元素类型相同的数组 */
|
|
3
|
+
export interface ArrayOptions extends CommonOptions {
|
|
4
|
+
/** 验证数组各元素 */
|
|
5
|
+
item: Validator<unknown, CommonOptions>;
|
|
6
|
+
/** 数组最小长度 */
|
|
7
|
+
min?: number;
|
|
8
|
+
/** 数组最大长度 */
|
|
9
|
+
max?: number;
|
|
10
|
+
/** 是否对数组元素进行去重 @defaults false */
|
|
11
|
+
unique?: boolean;
|
|
12
|
+
/** 如果传入的不是数组,是否要将其视为数组内元素,包裹成数组 */
|
|
13
|
+
toArray?: boolean;
|
|
14
|
+
}
|
|
15
|
+
type ArrayValues<Options extends ArrayOptions> = Validated<Options extends {
|
|
16
|
+
item: Validator<infer T, CommonOptions>;
|
|
17
|
+
} ? T : never, Options extends {
|
|
18
|
+
item: Validator<unknown, infer T>;
|
|
19
|
+
} ? T : never>[];
|
|
20
|
+
export declare function getArrayValidator<Options extends ArrayOptions>(options: Options): Validator<ArrayValues<Options>, Options>;
|
|
21
|
+
/** 验证元素数量固定、类型可以不同的数组 */
|
|
22
|
+
export interface TupleOptions extends CommonOptions {
|
|
23
|
+
/** 验证数组各元素(validator 与元素一一对应) */
|
|
24
|
+
tuple: Validator<unknown, CommonOptions>[];
|
|
25
|
+
}
|
|
26
|
+
type TupleValues<Options extends TupleOptions> = {
|
|
27
|
+
[Key in keyof Options['tuple']]: Options['tuple'][Key] extends Validator<infer Value, infer Options> ? Validated<Value, Options> : never;
|
|
28
|
+
};
|
|
29
|
+
export declare function getTupleValidator<const Options extends TupleOptions>(options: Options): Validator<TupleValues<Options>, Options>;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { success, failed } from '../lang/index.js';
|
|
2
|
+
import { getValidatorGenerator, } from './base.js';
|
|
3
|
+
export function getArrayValidator(options) {
|
|
4
|
+
return getValidatorGenerator(function validate(field, value, options) {
|
|
5
|
+
if (!Array.isArray(value)) {
|
|
6
|
+
if (options.toArray)
|
|
7
|
+
value = [value];
|
|
8
|
+
else
|
|
9
|
+
return failed(`${field} should be an array`);
|
|
10
|
+
}
|
|
11
|
+
let formatted = [];
|
|
12
|
+
if (typeof options.min === 'number' && value.length < options.min)
|
|
13
|
+
return failed(`array ${field}'s length should >= ${options.min}`);
|
|
14
|
+
if (typeof options.max === 'number' && value.length > options.max)
|
|
15
|
+
return failed(`array ${field}'s length should <= ${options.max}`);
|
|
16
|
+
const itemValidator = options.item;
|
|
17
|
+
for (let i = 0; i < value.length; i++) {
|
|
18
|
+
const itemResult = itemValidator(`${field}[${i}]`, value[i]);
|
|
19
|
+
if (itemResult.success)
|
|
20
|
+
formatted.push(itemResult.data);
|
|
21
|
+
else
|
|
22
|
+
return itemResult;
|
|
23
|
+
}
|
|
24
|
+
if (options.unique === true)
|
|
25
|
+
formatted = [...new Set(formatted)];
|
|
26
|
+
return success(formatted);
|
|
27
|
+
})(options);
|
|
28
|
+
}
|
|
29
|
+
export function getTupleValidator(options) {
|
|
30
|
+
return getValidatorGenerator(function validate(field, value, options) {
|
|
31
|
+
if (!Array.isArray(value))
|
|
32
|
+
return failed(`${field} should be an array`);
|
|
33
|
+
if (value.length > options.tuple.length)
|
|
34
|
+
return failed(`${field} should be a tuple with ${options.tuple.length} items`);
|
|
35
|
+
const formatted = [];
|
|
36
|
+
// 这种情况不能遍历 value,因为它的长度可能小于 opt.tuple
|
|
37
|
+
for (let i = 0; i < options.tuple.length; i++) {
|
|
38
|
+
const itemValidator = options.tuple[i];
|
|
39
|
+
const itemResult = itemValidator(`${field}[${i}]`, value[i]);
|
|
40
|
+
if (itemResult.success)
|
|
41
|
+
formatted.push(itemResult.data);
|
|
42
|
+
else
|
|
43
|
+
return itemResult;
|
|
44
|
+
}
|
|
45
|
+
return success(formatted);
|
|
46
|
+
})(options);
|
|
47
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { type Result } from '../lang/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* 支持传入进行验证的值类型
|
|
4
|
+
*/
|
|
5
|
+
export type AllowedInputValue = PrimitiveType | null | undefined;
|
|
6
|
+
/**
|
|
7
|
+
* JavaScript 基础值类型
|
|
8
|
+
*/
|
|
9
|
+
export type PrimitiveType = string | boolean | number | PrimitiveType[] | [...PrimitiveType[]] | {
|
|
10
|
+
[key: string]: PrimitiveType;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* validator 通用参数
|
|
14
|
+
*/
|
|
15
|
+
export interface CommonOptions<Value = any> {
|
|
16
|
+
/** 是否允许 null 值 @default false */
|
|
17
|
+
null?: boolean;
|
|
18
|
+
/** 字段是否必须有值(不能是 undefined) @default true */
|
|
19
|
+
required?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* 默认值,字段无值(或值为 undefined)时生效,值为 null 不会生效。
|
|
22
|
+
* 指定后 required 选项将失去作用。
|
|
23
|
+
*/
|
|
24
|
+
defaults?: Value;
|
|
25
|
+
custom?: (input: Value) => Result<Value>;
|
|
26
|
+
[key: string]: unknown;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 补全了的 Options
|
|
30
|
+
*/
|
|
31
|
+
type FullfiledOptions<Options extends Partial<CommonOptions>> = Omit<Options, keyof CommonOptions> & {
|
|
32
|
+
null: Options['null'] extends true ? true : false;
|
|
33
|
+
required: Options['required'] extends false ? false : true;
|
|
34
|
+
defaults: Options extends {
|
|
35
|
+
defaults: infer T;
|
|
36
|
+
} ? T : undefined;
|
|
37
|
+
custom: Options extends {
|
|
38
|
+
custom: infer T;
|
|
39
|
+
} ? T : undefined;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* 验证完成后能得到的值类型
|
|
43
|
+
*/
|
|
44
|
+
export type Validated<Value, InputOptions extends CommonOptions> = FullfiledOptions<InputOptions> extends {
|
|
45
|
+
defaults: undefined;
|
|
46
|
+
} ? FullfiledOptions<InputOptions> extends {
|
|
47
|
+
required: false;
|
|
48
|
+
null: false;
|
|
49
|
+
} ? Value | undefined : FullfiledOptions<InputOptions> extends {
|
|
50
|
+
required: false;
|
|
51
|
+
null: true;
|
|
52
|
+
} ? Value | undefined | null : FullfiledOptions<InputOptions> extends {
|
|
53
|
+
required: true;
|
|
54
|
+
null: false;
|
|
55
|
+
} ? Value : FullfiledOptions<InputOptions> extends {
|
|
56
|
+
required: true;
|
|
57
|
+
null: true;
|
|
58
|
+
} ? Value | null : Value : FullfiledOptions<InputOptions> extends {
|
|
59
|
+
defaults: infer T;
|
|
60
|
+
null: false;
|
|
61
|
+
} ? Value | T : FullfiledOptions<InputOptions> extends {
|
|
62
|
+
defaults: infer T;
|
|
63
|
+
null: true;
|
|
64
|
+
} ? Value | T | null : never;
|
|
65
|
+
/**
|
|
66
|
+
* 最终生成的 validator 函数类型
|
|
67
|
+
*/
|
|
68
|
+
export interface Validator<Value, InputOptions extends CommonOptions> {
|
|
69
|
+
(input: AllowedInputValue): Result<Validated<Value, InputOptions>>;
|
|
70
|
+
(field: string, input: AllowedInputValue): Result<Validated<Value, InputOptions>>;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* 返回支持指定格式的 options、并按照传入的逻辑进行验证的 validator 的生成器。
|
|
74
|
+
* 对 CommonOptions 相关内容的验证以自动包含在里面,只需要传入额外的验证逻辑即可。
|
|
75
|
+
*/
|
|
76
|
+
export declare function getValidatorGenerator<Value, Options extends CommonOptions>(validate: (field: string, input: PrimitiveType | Validated<Value, Options>, options: Options) => Result<Value>): <const InputOptions extends Options>(inputOptions: InputOptions) => Validator<Value, InputOptions>;
|
|
77
|
+
/**
|
|
78
|
+
* 返回只进行基本检查,不带定制的验证逻辑的 validator。
|
|
79
|
+
* 同时也是定制 validator 最小化实现的例子。
|
|
80
|
+
*/
|
|
81
|
+
export declare const getAnyValidator: <const InputOptions extends CommonOptions<any>>(inputOptions: InputOptions) => Validator<unknown, InputOptions>;
|
|
82
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { success, failed } from '../lang/index.js';
|
|
2
|
+
// -----------------------------------
|
|
3
|
+
/**
|
|
4
|
+
* 返回支持指定格式的 options、并按照传入的逻辑进行验证的 validator 的生成器。
|
|
5
|
+
* 对 CommonOptions 相关内容的验证以自动包含在里面,只需要传入额外的验证逻辑即可。
|
|
6
|
+
*/
|
|
7
|
+
export function getValidatorGenerator(validate) {
|
|
8
|
+
return function validatorGenerator(inputOptions) {
|
|
9
|
+
function validator(field, input) {
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
11
|
+
const { null: allowNull = false, required = true, defaults, custom } = inputOptions;
|
|
12
|
+
if (typeof field !== 'string') {
|
|
13
|
+
input = field;
|
|
14
|
+
field = 'value';
|
|
15
|
+
}
|
|
16
|
+
let value = input;
|
|
17
|
+
if (typeof value === 'undefined') {
|
|
18
|
+
if (typeof defaults !== 'undefined') {
|
|
19
|
+
value = defaults;
|
|
20
|
+
}
|
|
21
|
+
else if (required) {
|
|
22
|
+
return failed(`${field} is required`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (value === null && !allowNull)
|
|
26
|
+
return failed(`${field} cannot be null`);
|
|
27
|
+
if (value === null || value === undefined)
|
|
28
|
+
return success(value);
|
|
29
|
+
const validated = validate(field, value, inputOptions);
|
|
30
|
+
return validated.success && custom ? custom(validated.data) : validated;
|
|
31
|
+
}
|
|
32
|
+
return validator;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
// -----------------------------------
|
|
36
|
+
/**
|
|
37
|
+
* 返回只进行基本检查,不带定制的验证逻辑的 validator。
|
|
38
|
+
* 同时也是定制 validator 最小化实现的例子。
|
|
39
|
+
*/
|
|
40
|
+
export const getAnyValidator = getValidatorGenerator(function validate(field, input) {
|
|
41
|
+
return success(input);
|
|
42
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { success, failed } from '../lang/index.js';
|
|
2
|
+
import { getValidatorGenerator } from './base.js';
|
|
3
|
+
export const getBooleanValidator = getValidatorGenerator(function validate(field, input) {
|
|
4
|
+
let value = null;
|
|
5
|
+
if (typeof input === 'boolean') {
|
|
6
|
+
value = input;
|
|
7
|
+
}
|
|
8
|
+
else if (typeof input === 'string') {
|
|
9
|
+
const str = input.trim().toLowerCase();
|
|
10
|
+
if (['1', 'true', 'on', 'yes'].includes(str))
|
|
11
|
+
value = true;
|
|
12
|
+
else if (['0', 'false', 'off', 'no'].includes(str))
|
|
13
|
+
value = false;
|
|
14
|
+
}
|
|
15
|
+
else if (typeof input === 'number') {
|
|
16
|
+
if (input === 1)
|
|
17
|
+
value = true;
|
|
18
|
+
else if (input === 0)
|
|
19
|
+
value = false;
|
|
20
|
+
}
|
|
21
|
+
return value === null ? failed(`${field} must be true or false`) : success(value);
|
|
22
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 验证日期时间类型的值,依赖 dayjs
|
|
3
|
+
*/
|
|
4
|
+
import { type Dayjs } from 'dayjs';
|
|
5
|
+
import { type CommonOptions } from './base.js';
|
|
6
|
+
export interface DatetimeOptions extends CommonOptions<number> {
|
|
7
|
+
dayjs?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export type DatetimeValue<Options extends DatetimeOptions> = Options extends {
|
|
10
|
+
dayjs: true;
|
|
11
|
+
} ? Dayjs : Date;
|
|
12
|
+
export declare function getDatetimeValidator<const Options extends DatetimeOptions>(options?: Options): import("./base.js").Validator<DatetimeValue<Options>, Options>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 验证日期时间类型的值,依赖 dayjs
|
|
3
|
+
*/
|
|
4
|
+
import dayjs from 'dayjs';
|
|
5
|
+
import { success, failed } from '../lang/index.js';
|
|
6
|
+
import { getValidatorGenerator } from './base.js';
|
|
7
|
+
export function getDatetimeValidator(options = {}) {
|
|
8
|
+
return getValidatorGenerator(function validate(field, value) {
|
|
9
|
+
let dayjsValue;
|
|
10
|
+
if (typeof value === 'number') {
|
|
11
|
+
dayjsValue = dayjs.unix(value);
|
|
12
|
+
if (!dayjsValue.isValid())
|
|
13
|
+
return failed(`${field} must be a valid unix timestamp`);
|
|
14
|
+
}
|
|
15
|
+
else if (typeof value === 'string') {
|
|
16
|
+
dayjsValue = dayjs(value);
|
|
17
|
+
if (!dayjsValue.isValid())
|
|
18
|
+
return failed(`${field} must be a valid datetime string`);
|
|
19
|
+
}
|
|
20
|
+
else if (value instanceof Date || dayjs.isDayjs(value)) {
|
|
21
|
+
dayjsValue = dayjs(value);
|
|
22
|
+
if (!dayjsValue.isValid())
|
|
23
|
+
return failed(`${field} must be a valid Date or Dayjs object`);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
return failed(`${field} must be a datetime string or unix timestamp`);
|
|
27
|
+
}
|
|
28
|
+
return success((options.dayjs ? dayjsValue : dayjsValue.toDate()));
|
|
29
|
+
})(options);
|
|
30
|
+
}
|