@anjianshi/utils 2.4.17 → 2.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/README.md +10 -0
- package/eslint.config.cjs +33 -0
- package/package.json +34 -23
- package/publish-prepare.cjs +16 -0
- package/src/env-browser/device.ts +62 -0
- package/src/env-browser/global.ts +21 -0
- package/src/env-browser/load-script.ts +13 -0
- package/src/env-browser/logging.ts +58 -0
- package/src/env-browser/manage-vconsole.ts +54 -0
- package/src/env-node/crypto-random.ts +30 -0
- package/src/env-node/fs.ts +50 -0
- package/src/env-node/index.ts +6 -0
- package/src/env-node/logging/handlers.ts +190 -0
- package/src/env-node/logging/index.ts +16 -0
- package/src/env-node/safe-request.ts +66 -0
- package/src/env-react/emotion.tsx +42 -0
- package/src/env-service/controllers.ts +93 -0
- package/src/env-service/env-reader.ts +141 -0
- package/src/env-service/index.ts +6 -0
- package/src/env-service/prisma/adapt-logging.ts +39 -0
- package/src/env-service/prisma/extensions/exist.ts +21 -0
- package/src/env-service/prisma/extensions/find-and-count.ts +24 -0
- package/src/env-service/prisma/extensions/soft-delete.ts +162 -0
- package/src/env-service/prisma/extensions/with-transaction.ts +65 -0
- package/src/env-service/prisma/index.ts +6 -0
- package/src/env-service/prisma/transaction-contexted.ts +80 -0
- package/src/env-service/redis-cache.ts +142 -0
- package/src/env-service/tasks.ts +45 -0
- package/src/index.ts +3 -0
- package/src/init-dayjs.ts +8 -0
- package/src/lang/async.ts +47 -0
- package/src/lang/color.ts +119 -0
- package/src/lang/index.ts +7 -0
- package/src/lang/may-success.ts +57 -0
- package/src/lang/object.ts +39 -0
- package/src/lang/random.ts +25 -0
- package/src/lang/string.ts +95 -0
- package/src/lang/time.ts +19 -0
- package/{lang/types.d.ts → src/lang/types.ts} +43 -23
- package/src/logging/adapt.ts +49 -0
- package/src/logging/formatters.ts +23 -0
- package/src/logging/index.ts +106 -0
- package/src/md5.ts +318 -0
- package/src/url.ts +185 -0
- package/src/validators/array.ts +97 -0
- package/src/validators/base.ts +145 -0
- package/src/validators/boolean.ts +21 -0
- package/src/validators/datetime.ts +39 -0
- package/src/validators/factory.ts +244 -0
- package/src/validators/index.ts +9 -0
- package/src/validators/number.ts +54 -0
- package/src/validators/object.ts +101 -0
- package/src/validators/one-of.ts +33 -0
- package/src/validators/string.ts +72 -0
- package/env-browser/device.d.ts +0 -24
- package/env-browser/device.js +0 -50
- package/env-browser/global.d.ts +0 -10
- package/env-browser/global.js +0 -15
- package/env-browser/load-script.d.ts +0 -5
- package/env-browser/load-script.js +0 -13
- package/env-browser/logging.d.ts +0 -18
- package/env-browser/logging.js +0 -49
- package/env-browser/manage-vconsole.d.ts +0 -16
- package/env-browser/manage-vconsole.js +0 -38
- package/env-node/crypto-random.d.ts +0 -13
- package/env-node/crypto-random.js +0 -28
- package/env-node/fs.d.ts +0 -19
- package/env-node/fs.js +0 -48
- package/env-node/index.d.ts +0 -5
- package/env-node/index.js +0 -5
- package/env-node/logging/handlers.d.ts +0 -58
- package/env-node/logging/handlers.js +0 -154
- package/env-node/logging/index.d.ts +0 -11
- package/env-node/logging/index.js +0 -14
- package/env-react/emotion.d.ts +0 -20
- package/env-react/emotion.jsx +0 -34
- package/env-service/controllers.d.ts +0 -30
- package/env-service/controllers.js +0 -41
- package/env-service/env-reader.d.ts +0 -55
- package/env-service/env-reader.js +0 -79
- package/env-service/index.d.ts +0 -6
- package/env-service/index.js +0 -6
- package/env-service/prisma/adapt-logging.d.ts +0 -21
- package/env-service/prisma/adapt-logging.js +0 -30
- package/env-service/prisma/extensions/exist.d.ts +0 -10
- package/env-service/prisma/extensions/exist.js +0 -16
- package/env-service/prisma/extensions/find-and-count.d.ts +0 -7
- package/env-service/prisma/extensions/find-and-count.js +0 -19
- package/env-service/prisma/extensions/soft-delete.d.ts +0 -52
- package/env-service/prisma/extensions/soft-delete.js +0 -123
- package/env-service/prisma/extensions/with-transaction.d.ts +0 -9
- package/env-service/prisma/extensions/with-transaction.js +0 -52
- package/env-service/prisma/index.d.ts +0 -6
- package/env-service/prisma/index.js +0 -6
- package/env-service/prisma/transaction-contexted.d.ts +0 -11
- package/env-service/prisma/transaction-contexted.js +0 -52
- package/env-service/redis-cache.d.ts +0 -39
- package/env-service/redis-cache.js +0 -117
- package/env-service/tasks.d.ts +0 -12
- package/env-service/tasks.js +0 -37
- package/index.d.ts +0 -3
- package/index.js +0 -3
- package/init-dayjs.d.ts +0 -2
- package/init-dayjs.js +0 -7
- package/lang/async.d.ts +0 -19
- package/lang/async.js +0 -34
- package/lang/index.d.ts +0 -7
- package/lang/index.js +0 -7
- package/lang/may-success.d.ts +0 -40
- package/lang/may-success.js +0 -27
- package/lang/object.d.ts +0 -5
- package/lang/object.js +0 -31
- package/lang/random.d.ts +0 -13
- package/lang/random.js +0 -24
- package/lang/string.d.ts +0 -29
- package/lang/string.js +0 -92
- package/lang/time.d.ts +0 -10
- package/lang/time.js +0 -18
- package/lang/types.js +0 -28
- package/logging/adapt.d.ts +0 -10
- package/logging/adapt.js +0 -43
- package/logging/formatters.d.ts +0 -10
- package/logging/formatters.js +0 -22
- package/logging/index.d.ts +0 -45
- package/logging/index.js +0 -90
- package/md5.d.ts +0 -30
- package/md5.js +0 -308
- package/url.d.ts +0 -77
- package/url.js +0 -149
- package/validators/array.d.ts +0 -30
- package/validators/array.js +0 -47
- package/validators/base.d.ts +0 -82
- package/validators/base.js +0 -42
- package/validators/boolean.d.ts +0 -3
- package/validators/boolean.js +0 -22
- package/validators/factory.d.ts +0 -66
- package/validators/factory.js +0 -109
- package/validators/index.d.ts +0 -8
- package/validators/index.js +0 -8
- package/validators/number.d.ts +0 -19
- package/validators/number.js +0 -26
- package/validators/object.d.ts +0 -28
- package/validators/object.js +0 -49
- package/validators/oneOf.d.ts +0 -10
- package/validators/oneOf.js +0 -15
- package/validators/string.d.ts +0 -22
- package/validators/string.js +0 -35
package/validators/object.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import isPlainObject from 'lodash/isPlainObject.js';
|
|
2
|
-
import { success, failed } from '../lang/index.js';
|
|
3
|
-
import { getValidatorGenerator, } from './base.js';
|
|
4
|
-
export function getStructValidator(options) {
|
|
5
|
-
return getValidatorGenerator(function validate(field, value, options) {
|
|
6
|
-
if (!isPlainObject(value))
|
|
7
|
-
return failed(`${field} should be a plain object`);
|
|
8
|
-
const formatted = {};
|
|
9
|
-
for (const [key, itemValidator] of Object.entries(options.struct)) {
|
|
10
|
-
const itemResult = itemValidator(`${field}["${key}"]`, value[key]);
|
|
11
|
-
if (itemResult.success) {
|
|
12
|
-
if (itemResult.data !== undefined)
|
|
13
|
-
formatted[key] = itemResult.data;
|
|
14
|
-
}
|
|
15
|
-
else {
|
|
16
|
-
return itemResult;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
return success(formatted);
|
|
20
|
-
})(options);
|
|
21
|
-
}
|
|
22
|
-
export function getRecordValidator(options) {
|
|
23
|
-
return getValidatorGenerator(function validate(field, value, options) {
|
|
24
|
-
if (!isPlainObject(value))
|
|
25
|
-
return failed(`${field} should be a plain object`);
|
|
26
|
-
const formatted = {};
|
|
27
|
-
for (const [key, itemValue] of Object.entries(value)) {
|
|
28
|
-
// record 场景下,值为 undefined 的项目视为不存在,不保留在验证结果里,
|
|
29
|
-
// 不然一些因为不想赋值而填充了 undefined 值的项目可能意外触发验证失败,或意外得到了默认值。
|
|
30
|
-
// (因此 validator 的 required 选项和 defaults 选项也没有意义了)
|
|
31
|
-
if (itemValue === undefined)
|
|
32
|
-
continue;
|
|
33
|
-
const itemResult = options.record(`${field}["${key}"]`, itemValue);
|
|
34
|
-
if (itemResult.success) {
|
|
35
|
-
if (itemResult.data !== undefined)
|
|
36
|
-
formatted[key] = itemResult.data;
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
return itemResult;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
const length = Object.keys(formatted).length;
|
|
43
|
-
if (typeof options.min === 'number' && length < options.min)
|
|
44
|
-
return failed(`size of ${field} should >= ${options.min}`);
|
|
45
|
-
if (typeof options.max === 'number' && length > options.max)
|
|
46
|
-
return failed(`size of ${field} should <= ${options.max}`);
|
|
47
|
-
return success(formatted);
|
|
48
|
-
})(options);
|
|
49
|
-
}
|
package/validators/oneOf.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { type CommonOptions, type Validator } from './base.js';
|
|
2
|
-
/** 要求返回值通过其中一个验证器的检查 */
|
|
3
|
-
export interface OneOfOptions extends CommonOptions {
|
|
4
|
-
/** 验证数组各元素 */
|
|
5
|
-
validators: Validator<unknown, CommonOptions>[];
|
|
6
|
-
}
|
|
7
|
-
export type OneOfValue<Options extends OneOfOptions> = Options extends {
|
|
8
|
-
validators: Validator<infer T, CommonOptions>[];
|
|
9
|
-
} ? T : never;
|
|
10
|
-
export declare function getOneOfValidator<const Options extends OneOfOptions>(options?: Options): Validator<OneOfValue<Options>, Options>;
|
package/validators/oneOf.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { failed } from '../lang/index.js';
|
|
2
|
-
import { getValidatorGenerator, } from './base.js';
|
|
3
|
-
export function getOneOfValidator(options = {}) {
|
|
4
|
-
return getValidatorGenerator(function validate(field, value) {
|
|
5
|
-
const errors = [];
|
|
6
|
-
for (const validator of options.validators) {
|
|
7
|
-
const result = validator(field, value);
|
|
8
|
-
if (result.success)
|
|
9
|
-
return result;
|
|
10
|
-
else
|
|
11
|
-
errors.push(result.message);
|
|
12
|
-
}
|
|
13
|
-
return failed(`${field} do not match any valid format:\n- ` + errors.join('\n- '));
|
|
14
|
-
})(options);
|
|
15
|
-
}
|
package/validators/string.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { type CommonOptions } from './base.js';
|
|
2
|
-
export interface StringOptions extends CommonOptions<string> {
|
|
3
|
-
/** 字符串最小长度。defaults='' 时默认为 0,否则默认为 1 */
|
|
4
|
-
min?: number;
|
|
5
|
-
/** 字符串最大长度。 */
|
|
6
|
-
max?: number;
|
|
7
|
-
/** 字符串需匹配此正则,也可传入关键词使用预置的正则。正则通常应以 ^ 和 $ 开头结尾。 */
|
|
8
|
-
pattern?: RegExp | 'uuid' | 'mobile';
|
|
9
|
-
/**
|
|
10
|
-
* 指定一个数组或 TypeScript enum,字段值必须在此 enum 之中。
|
|
11
|
-
* 若指定,前几个选项将不再生效。
|
|
12
|
-
*/
|
|
13
|
-
choices?: string[] | Record<string, string>;
|
|
14
|
-
/** 验证之前,是否先清除两侧空白字符 @default true */
|
|
15
|
-
trim?: boolean;
|
|
16
|
-
}
|
|
17
|
-
export type StringValueWithChoices<Options extends StringOptions> = Options extends {
|
|
18
|
-
choices: (infer T)[];
|
|
19
|
-
} ? T : Options extends {
|
|
20
|
-
choices: Record<string, infer T>;
|
|
21
|
-
} ? T : string;
|
|
22
|
-
export declare function getStringValidator<const Options extends StringOptions>(options?: Options): import("./base.js").Validator<StringValueWithChoices<Options>, Options>;
|
package/validators/string.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { success, failed } from '../lang/index.js';
|
|
2
|
-
import { getValidatorGenerator } from './base.js';
|
|
3
|
-
export function getStringValidator(options = {}) {
|
|
4
|
-
return getValidatorGenerator(function validate(field, value) {
|
|
5
|
-
if (typeof value !== 'string')
|
|
6
|
-
return failed(`${field} must be a string`);
|
|
7
|
-
const trim = options.trim ?? true;
|
|
8
|
-
const formatted = trim ? value.trim() : value;
|
|
9
|
-
if ('choices' in options && options.choices) {
|
|
10
|
-
const validValues = Array.isArray(options.choices)
|
|
11
|
-
? options.choices
|
|
12
|
-
: Object.values(options.choices);
|
|
13
|
-
if (!validValues.includes(formatted))
|
|
14
|
-
return failed(`${field} can only be one of ${validValues.join(', ')}.`);
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
const { min = options.defaults === '' ? 0 : 1, max, pattern } = options;
|
|
18
|
-
if (typeof min === 'number' && formatted.length < min)
|
|
19
|
-
return failed(`${field}'s length must >= ${min}`);
|
|
20
|
-
if (typeof max === 'number' && formatted.length > max)
|
|
21
|
-
return failed(`${field}'s length must <= ${max}`);
|
|
22
|
-
if (pattern !== undefined) {
|
|
23
|
-
if (pattern instanceof RegExp && !pattern.exec(formatted))
|
|
24
|
-
return failed(`${field} does not match the pattern`);
|
|
25
|
-
if (pattern === 'mobile' && !/^1\d{10}$/.test(formatted))
|
|
26
|
-
return failed(`${field} is not a valid mobile number`);
|
|
27
|
-
if (pattern === 'uuid') {
|
|
28
|
-
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(formatted))
|
|
29
|
-
return failed(`${field} is not a valid uuid`);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return success(formatted);
|
|
34
|
-
})(options);
|
|
35
|
-
}
|