@wener/common 1.0.3 → 1.0.5
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/lib/cn/DivisionCode.js.map +1 -1
- package/lib/cn/Mod11Checksum.js.map +1 -1
- package/lib/cn/Mod31Checksum.js.map +1 -1
- package/lib/cn/ResidentIdentityCardNumber.js.map +1 -1
- package/lib/cn/UnifiedSocialCreditCode.js.map +1 -1
- package/lib/cn/formatDate.js.map +1 -1
- package/lib/cn/parseSex.js.map +1 -1
- package/lib/cn/types.d.js.map +1 -1
- package/lib/consola/createStandardConsolaReporter.js +18 -0
- package/lib/consola/createStandardConsolaReporter.js.map +1 -0
- package/lib/consola/formatLogObject.js +125 -0
- package/lib/consola/formatLogObject.js.map +1 -0
- package/lib/consola/index.js +3 -0
- package/lib/consola/index.js.map +1 -0
- package/lib/data/formatSort.js +15 -0
- package/lib/data/formatSort.js.map +1 -0
- package/lib/data/index.js +4 -0
- package/lib/data/index.js.map +1 -0
- package/lib/data/maybeNumber.js +22 -0
- package/lib/data/maybeNumber.js.map +1 -0
- package/lib/data/parseSort.js +95 -0
- package/lib/data/parseSort.js.map +1 -0
- package/lib/data/resolvePagination.js +36 -0
- package/lib/data/resolvePagination.js.map +1 -0
- package/lib/data/types.d.js +3 -0
- package/lib/data/types.d.js.map +1 -0
- package/lib/index.js +6 -2
- package/lib/index.js.map +1 -1
- package/lib/jsonschema/JsonSchema.js +6 -6
- package/lib/jsonschema/JsonSchema.js.map +1 -1
- package/lib/jsonschema/types.d.js.map +1 -1
- package/lib/meta/defineFileType.js.map +1 -1
- package/lib/meta/defineInit.js.map +1 -1
- package/lib/meta/defineMetadata.js.map +1 -1
- package/lib/password/PHC.js +8 -8
- package/lib/password/PHC.js.map +1 -1
- package/lib/password/Password.js.map +1 -1
- package/lib/password/createArgon2PasswordAlgorithm.js.map +1 -1
- package/lib/password/createBase64PasswordAlgorithm.js.map +1 -1
- package/lib/password/createBcryptPasswordAlgorithm.js.map +1 -1
- package/lib/password/createPBKDF2PasswordAlgorithm.js.map +1 -1
- package/lib/password/createScryptPasswordAlgorithm.js.map +1 -1
- package/lib/search/AdvanceSearch.js.map +1 -1
- package/lib/search/formatAdvanceSearch.js.map +1 -1
- package/lib/search/optimizeAdvanceSearch.js.map +1 -1
- package/lib/search/parseAdvanceSearch.js.map +1 -1
- package/lib/search/parser.d.js.map +1 -1
- package/lib/search/types.d.js.map +1 -1
- package/lib/tools/generateSchema.js +43 -0
- package/lib/tools/generateSchema.js.map +1 -0
- package/lib/tools/renderJsonSchemaToMarkdownDoc.js.map +1 -1
- package/package.json +19 -5
- package/src/cn/DivisionCode.test.ts +38 -45
- package/src/cn/DivisionCode.ts +140 -184
- package/src/cn/Mod11Checksum.ts +17 -17
- package/src/cn/Mod31Checksum.ts +25 -25
- package/src/cn/ResidentIdentityCardNumber.test.ts +12 -16
- package/src/cn/ResidentIdentityCardNumber.ts +82 -82
- package/src/cn/UnifiedSocialCreditCode.test.ts +11 -11
- package/src/cn/UnifiedSocialCreditCode.ts +115 -120
- package/src/cn/__snapshots__/ResidentIdentityCardNumber.test.ts.snap +1 -1
- package/src/cn/formatDate.ts +10 -10
- package/src/cn/parseSex.ts +11 -25
- package/src/cn/types.d.ts +43 -43
- package/src/consola/createStandardConsolaReporter.ts +31 -0
- package/src/consola/formatLogObject.ts +171 -0
- package/src/consola/index.ts +2 -0
- package/src/data/formatSort.test.ts +13 -0
- package/src/data/formatSort.ts +18 -0
- package/src/data/index.ts +5 -0
- package/src/data/maybeNumber.ts +23 -0
- package/src/data/parseSort.test.ts +67 -0
- package/src/data/parseSort.ts +108 -0
- package/src/data/resolvePagination.test.ts +58 -0
- package/src/data/resolvePagination.ts +60 -0
- package/src/data/types.d.ts +33 -0
- package/src/index.ts +8 -2
- package/src/jsonschema/JsonSchema.test.ts +13 -22
- package/src/jsonschema/JsonSchema.ts +146 -178
- package/src/jsonschema/types.d.ts +151 -161
- package/src/meta/defineFileType.tsx +54 -54
- package/src/meta/defineInit.ts +32 -53
- package/src/meta/defineMetadata.test.ts +5 -7
- package/src/meta/defineMetadata.ts +28 -46
- package/src/password/PHC.test.ts +186 -277
- package/src/password/PHC.ts +243 -243
- package/src/password/Password.test.ts +38 -50
- package/src/password/Password.ts +73 -95
- package/src/password/createArgon2PasswordAlgorithm.ts +65 -69
- package/src/password/createBase64PasswordAlgorithm.ts +9 -9
- package/src/password/createBcryptPasswordAlgorithm.ts +20 -22
- package/src/password/createPBKDF2PasswordAlgorithm.ts +49 -61
- package/src/password/createScryptPasswordAlgorithm.ts +48 -59
- package/src/search/AdvanceSearch.test.ts +136 -143
- package/src/search/AdvanceSearch.ts +6 -6
- package/src/search/formatAdvanceSearch.ts +44 -53
- package/src/search/optimizeAdvanceSearch.ts +70 -83
- package/src/search/parseAdvanceSearch.ts +16 -19
- package/src/search/parser.d.ts +3 -3
- package/src/search/types.d.ts +28 -54
- package/src/tools/generateSchema.ts +39 -0
- package/src/tools/renderJsonSchemaToMarkdownDoc.ts +69 -69
- package/lib/normalizePagination.js +0 -14
- package/lib/normalizePagination.js.map +0 -1
- package/lib/parseSort.js +0 -106
- package/lib/parseSort.js.map +0 -1
- package/src/normalizePagination.ts +0 -25
- package/src/parseSort.test.ts +0 -42
- package/src/parseSort.ts +0 -133
|
@@ -1,173 +1,163 @@
|
|
|
1
1
|
type JsonSchemaTypeName =
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
| 'string' //
|
|
3
|
+
| 'number'
|
|
4
|
+
| 'integer'
|
|
5
|
+
| 'boolean'
|
|
6
|
+
| 'object'
|
|
7
|
+
| 'array'
|
|
8
|
+
| 'null';
|
|
9
9
|
|
|
10
10
|
type JsonValue =
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
| JsonValue[]
|
|
18
|
-
| null;
|
|
11
|
+
| string //
|
|
12
|
+
| number
|
|
13
|
+
| boolean
|
|
14
|
+
| { [key: string]: JsonValue }
|
|
15
|
+
| JsonValue[]
|
|
16
|
+
| null;
|
|
19
17
|
|
|
20
18
|
type JsonSchemaVersion =
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
| 'http://json-schema.org/schema' // latest
|
|
20
|
+
| 'https://json-schema.org/draft/2020-12/schema'
|
|
21
|
+
// draft-07
|
|
22
|
+
| 'http://json-schema.org/draft-07/schema#' //
|
|
23
|
+
| 'http://json-schema.org/draft-07/hyper-schema#';
|
|
26
24
|
|
|
27
25
|
type JsonSchemaFormatName =
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
26
|
+
//
|
|
27
|
+
| 'date-time'
|
|
28
|
+
| 'date'
|
|
29
|
+
| 'time'
|
|
30
|
+
| 'duration'
|
|
31
|
+
//
|
|
32
|
+
| 'email'
|
|
33
|
+
| 'idn-email'
|
|
34
|
+
//
|
|
35
|
+
| 'hostname'
|
|
36
|
+
| 'idn-hostname'
|
|
37
|
+
| 'ipv4'
|
|
38
|
+
| 'ipv6'
|
|
39
|
+
//
|
|
40
|
+
| 'uri'
|
|
41
|
+
| 'uri-reference'
|
|
42
|
+
| 'iri'
|
|
43
|
+
| 'iri-reference'
|
|
44
|
+
| 'uuid'
|
|
45
|
+
| 'uri-template'
|
|
46
|
+
| 'regex'
|
|
47
|
+
| 'json-pointer'
|
|
48
|
+
| 'relative-json-pointer';
|
|
51
49
|
|
|
52
50
|
export type JsonSchemaDef = {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
title?: string;
|
|
163
|
-
description?: string;
|
|
164
|
-
default?: JsonValue;
|
|
165
|
-
readOnly?: boolean;
|
|
166
|
-
writeOnly?: boolean;
|
|
167
|
-
examples?: JsonValue;
|
|
168
|
-
deprecated?: boolean;
|
|
51
|
+
$id?: string;
|
|
52
|
+
$ref?: string;
|
|
53
|
+
/**
|
|
54
|
+
* Meta schema
|
|
55
|
+
*
|
|
56
|
+
* Recommended values:
|
|
57
|
+
* - 'http://json-schema.org/schema#'
|
|
58
|
+
* - 'http://json-schema.org/hyper-schema#'
|
|
59
|
+
* - 'http://json-schema.org/draft-07/schema#'
|
|
60
|
+
* - 'http://json-schema.org/draft-07/hyper-schema#'
|
|
61
|
+
*
|
|
62
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
|
|
63
|
+
*/
|
|
64
|
+
$schema?: JsonSchemaVersion | string;
|
|
65
|
+
$comment?: string;
|
|
66
|
+
|
|
67
|
+
$defs?: { [key: string]: JsonSchemaDef };
|
|
68
|
+
|
|
69
|
+
type?: JsonSchemaTypeName | JsonSchemaTypeName[];
|
|
70
|
+
enum?: JsonValue[];
|
|
71
|
+
const?: JsonValue;
|
|
72
|
+
|
|
73
|
+
//region Numeric Validation
|
|
74
|
+
|
|
75
|
+
multipleOf?: number;
|
|
76
|
+
maximum?: number;
|
|
77
|
+
exclusiveMaximum?: number;
|
|
78
|
+
minimum?: number;
|
|
79
|
+
exclusiveMinimum?: number;
|
|
80
|
+
|
|
81
|
+
//endregion
|
|
82
|
+
|
|
83
|
+
//region String Validation
|
|
84
|
+
|
|
85
|
+
maxLength?: number;
|
|
86
|
+
minLength?: number;
|
|
87
|
+
pattern?: string;
|
|
88
|
+
|
|
89
|
+
//endregion
|
|
90
|
+
|
|
91
|
+
//region Array Validation
|
|
92
|
+
|
|
93
|
+
items?: JsonSchemaDef | JsonSchemaDef[];
|
|
94
|
+
additionalItems?: JsonSchemaDef;
|
|
95
|
+
maxItems?: number;
|
|
96
|
+
minItems?: number;
|
|
97
|
+
uniqueItems?: boolean;
|
|
98
|
+
contains?: JsonSchemaDef;
|
|
99
|
+
//endregion
|
|
100
|
+
|
|
101
|
+
//region Object Validation
|
|
102
|
+
|
|
103
|
+
maxProperties?: number;
|
|
104
|
+
minProperties?: number;
|
|
105
|
+
required?: string[];
|
|
106
|
+
properties?: { [key: string]: JsonSchemaDef };
|
|
107
|
+
patternProperties?: { [key: string]: JsonSchemaDef };
|
|
108
|
+
additionalProperties?: JsonSchemaDef;
|
|
109
|
+
dependencies?: { [key: string]: JsonSchemaDef | string[] };
|
|
110
|
+
propertyNames?: JsonSchemaDef;
|
|
111
|
+
//endregion
|
|
112
|
+
|
|
113
|
+
//region Conditional
|
|
114
|
+
|
|
115
|
+
if?: JsonSchemaDef;
|
|
116
|
+
then?: JsonSchemaDef;
|
|
117
|
+
else?: JsonSchemaDef;
|
|
118
|
+
|
|
119
|
+
//endregion
|
|
120
|
+
|
|
121
|
+
//region Boolean Logic
|
|
122
|
+
|
|
123
|
+
allOf?: JsonSchemaDef[];
|
|
124
|
+
anyOf?: JsonSchemaDef[];
|
|
125
|
+
oneOf?: JsonSchemaDef[];
|
|
126
|
+
not?: JsonSchemaDef;
|
|
127
|
+
//endregion
|
|
128
|
+
|
|
129
|
+
//region Semantic
|
|
130
|
+
|
|
131
|
+
format?: JsonSchemaFormatName | string;
|
|
132
|
+
|
|
133
|
+
//endregion
|
|
134
|
+
|
|
135
|
+
//region String-Encoding Non-JSON Data
|
|
136
|
+
|
|
137
|
+
contentMediaType?: string;
|
|
138
|
+
contentEncoding?: string;
|
|
139
|
+
contentSchema?: JsonSchemaDef;
|
|
140
|
+
//endregion
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* use {@link $defs} instead
|
|
144
|
+
*/
|
|
145
|
+
definitions?: Record<string, JsonSchemaDef>;
|
|
146
|
+
|
|
147
|
+
//region Meta-Data Annotations
|
|
148
|
+
/**
|
|
149
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
|
|
150
|
+
*/
|
|
151
|
+
|
|
152
|
+
title?: string;
|
|
153
|
+
description?: string;
|
|
154
|
+
default?: JsonValue;
|
|
155
|
+
readOnly?: boolean;
|
|
156
|
+
writeOnly?: boolean;
|
|
157
|
+
examples?: JsonValue;
|
|
158
|
+
deprecated?: boolean;
|
|
169
159
|
|
|
170
|
-
|
|
160
|
+
//endregion
|
|
171
161
|
|
|
172
|
-
|
|
162
|
+
nullable?: boolean;
|
|
173
163
|
};
|
|
@@ -1,68 +1,68 @@
|
|
|
1
1
|
type DefineFileTypeOptions = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
name: string;
|
|
3
|
+
title?: string;
|
|
4
|
+
description?: string;
|
|
5
|
+
type?: string;
|
|
6
|
+
types?: string[];
|
|
7
|
+
extension?: string;
|
|
8
|
+
extensions?: string[];
|
|
9
|
+
tags?: string[];
|
|
10
|
+
metadata?: Record<string, any>;
|
|
11
11
|
};
|
|
12
12
|
export type FileTypeDef = {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
13
|
+
name: string;
|
|
14
|
+
title: string;
|
|
15
|
+
description?: string;
|
|
16
|
+
type: string; // primary type
|
|
17
|
+
extension: string; // primary extension
|
|
18
|
+
types: string[];
|
|
19
|
+
extensions: string[];
|
|
20
|
+
tags: string[];
|
|
21
|
+
metadata: Record<string, any>;
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
let _all: FileTypeDef[] = [];
|
|
25
25
|
|
|
26
26
|
export function defineFileType({
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
type = '',
|
|
28
|
+
types = [],
|
|
29
|
+
extension = '',
|
|
30
|
+
extensions = [],
|
|
31
|
+
...opts
|
|
32
32
|
}: DefineFileTypeOptions): FileTypeDef {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
33
|
+
if (!type) {
|
|
34
|
+
type = types[0] || '';
|
|
35
|
+
}
|
|
36
|
+
if (!extension) {
|
|
37
|
+
extension = extensions[0] || '';
|
|
38
|
+
}
|
|
39
|
+
if (!types.includes(type)) {
|
|
40
|
+
types = [type, ...types];
|
|
41
|
+
}
|
|
42
|
+
if (!extensions.includes(extension)) {
|
|
43
|
+
extensions = [extension, ...extensions];
|
|
44
|
+
}
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
46
|
+
const def: FileTypeDef = {
|
|
47
|
+
title: opts.title || opts.name,
|
|
48
|
+
type,
|
|
49
|
+
extension,
|
|
50
|
+
types,
|
|
51
|
+
extensions,
|
|
52
|
+
tags: [],
|
|
53
|
+
metadata: {},
|
|
54
|
+
...opts,
|
|
55
|
+
};
|
|
56
|
+
let idx = _all.findIndex((v) => v.name === def.name);
|
|
57
|
+
if (idx >= 0) {
|
|
58
|
+
_all[idx] = def;
|
|
59
|
+
console.warn(`File type ${def.name} is redefined`);
|
|
60
|
+
} else {
|
|
61
|
+
_all.push(def);
|
|
62
|
+
}
|
|
63
|
+
return def;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
export function getFileType(): FileTypeDef[] {
|
|
67
|
-
|
|
67
|
+
return _all;
|
|
68
68
|
}
|
package/src/meta/defineInit.ts
CHANGED
|
@@ -1,68 +1,47 @@
|
|
|
1
1
|
import type { MaybePromise } from '@wener/utils';
|
|
2
2
|
import { startCase } from 'es-toolkit';
|
|
3
3
|
|
|
4
|
-
type DefineInitOptions = {
|
|
5
|
-
|
|
6
|
-
title?: string;
|
|
7
|
-
onInit?: () => any;
|
|
8
|
-
metadata?: Record<string, any>;
|
|
9
|
-
};
|
|
10
|
-
export type InitDef = {
|
|
11
|
-
name: string;
|
|
12
|
-
title: string;
|
|
13
|
-
onInit?: () => any;
|
|
14
|
-
metadata: Record<string, any>;
|
|
15
|
-
};
|
|
4
|
+
type DefineInitOptions = { name: string; title?: string; onInit?: () => any; metadata?: Record<string, any> };
|
|
5
|
+
export type InitDef = { name: string; title: string; onInit?: () => any; metadata: Record<string, any> };
|
|
16
6
|
let _all: InitDef[] = [];
|
|
17
7
|
|
|
18
8
|
export function defineInit(o: DefineInitOptions) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
console.warn(`skip redefined init: ${def.name}`);
|
|
27
|
-
} else {
|
|
28
|
-
_all.push(def);
|
|
29
|
-
}
|
|
9
|
+
const def = { title: startCase(o.name), metadata: {}, ...o };
|
|
10
|
+
let idx = _all.findIndex((v) => v.name === def.name);
|
|
11
|
+
if (idx >= 0) {
|
|
12
|
+
console.warn(`skip redefined init: ${def.name}`);
|
|
13
|
+
} else {
|
|
14
|
+
_all.push(def);
|
|
15
|
+
}
|
|
30
16
|
|
|
31
|
-
|
|
17
|
+
return def;
|
|
32
18
|
}
|
|
33
19
|
|
|
34
20
|
let result = new Map<any, MaybePromise<InitResult>>();
|
|
35
21
|
|
|
36
|
-
type InitResult = {
|
|
37
|
-
name: string;
|
|
38
|
-
success: boolean;
|
|
39
|
-
error?: any;
|
|
40
|
-
};
|
|
22
|
+
type InitResult = { name: string; success: boolean; error?: any };
|
|
41
23
|
|
|
42
24
|
export async function runInit(inits = _all) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}),
|
|
64
|
-
);
|
|
65
|
-
}
|
|
25
|
+
for (let init of inits) {
|
|
26
|
+
if (result.get(init)) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
result.set(
|
|
30
|
+
init,
|
|
31
|
+
Promise.resolve().then(async () => {
|
|
32
|
+
let out: InitResult = { name: init.name, success: true };
|
|
33
|
+
try {
|
|
34
|
+
await init.onInit?.();
|
|
35
|
+
} catch (e) {
|
|
36
|
+
console.error(`Failed to init ${init.name}`, e);
|
|
37
|
+
out.success = false;
|
|
38
|
+
out.error = e;
|
|
39
|
+
}
|
|
40
|
+
result.set(init, out);
|
|
41
|
+
return out;
|
|
42
|
+
}),
|
|
43
|
+
);
|
|
44
|
+
}
|
|
66
45
|
|
|
67
|
-
|
|
46
|
+
return Promise.all(result.values());
|
|
68
47
|
}
|
|
@@ -2,14 +2,12 @@ import { expect, test } from 'vitest';
|
|
|
2
2
|
import { createMetadataKey, defineMetadata, getMetadata } from './defineMetadata';
|
|
3
3
|
|
|
4
4
|
test('defineMetadata', () => {
|
|
5
|
-
|
|
5
|
+
const key = createMetadataKey<string>('name');
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
metadata: {},
|
|
9
|
-
};
|
|
7
|
+
const user = { metadata: {} };
|
|
10
8
|
|
|
11
|
-
|
|
9
|
+
defineMetadata(user, key, 'wener');
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
expect(user.metadata).toEqual({ name: 'wener' });
|
|
12
|
+
expect(getMetadata(user, key)).toEqual('wener');
|
|
15
13
|
});
|
|
@@ -1,70 +1,52 @@
|
|
|
1
1
|
import { startCase } from 'es-toolkit';
|
|
2
2
|
import { get, set } from 'es-toolkit/compat';
|
|
3
3
|
|
|
4
|
-
type HasMetadata = {
|
|
5
|
-
metadata?: Record<string, any>;
|
|
6
|
-
};
|
|
4
|
+
type HasMetadata = { metadata?: Record<string, any> };
|
|
7
5
|
|
|
8
|
-
type MetadataKey<T> = {
|
|
9
|
-
key: string;
|
|
10
|
-
type: T;
|
|
11
|
-
title: string;
|
|
12
|
-
description?: string;
|
|
13
|
-
};
|
|
6
|
+
type MetadataKey<T> = { key: string; type: T; title: string; description?: string };
|
|
14
7
|
|
|
15
8
|
interface CreateMetadataKeyOptions {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
9
|
+
key: string;
|
|
10
|
+
title?: string;
|
|
11
|
+
description?: string;
|
|
19
12
|
}
|
|
20
13
|
|
|
21
14
|
export function createMetadataKey<T = never>(key: string, opts?: Omit<CreateMetadataKeyOptions, 'key'>): MetadataKey<T>;
|
|
22
15
|
export function createMetadataKey<T = never>(opts: CreateMetadataKeyOptions): MetadataKey<T>;
|
|
23
16
|
export function createMetadataKey<T = never>(a: any, b?: any): MetadataKey<T> {
|
|
24
|
-
|
|
25
|
-
typeof a === 'string'
|
|
26
|
-
? {
|
|
27
|
-
key: a,
|
|
28
|
-
...b,
|
|
29
|
-
}
|
|
30
|
-
: a;
|
|
17
|
+
const opts: CreateMetadataKeyOptions = typeof a === 'string' ? { key: a, ...b } : a;
|
|
31
18
|
|
|
32
|
-
|
|
33
|
-
|
|
19
|
+
const { key } = opts;
|
|
20
|
+
opts.title ||= startCase(key);
|
|
34
21
|
|
|
35
|
-
|
|
36
|
-
...opts,
|
|
37
|
-
type: null as any,
|
|
38
|
-
} as MetadataKey<T>;
|
|
22
|
+
const k: MetadataKey<T> = { ...opts, type: null as any } as MetadataKey<T>;
|
|
39
23
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
});
|
|
44
|
-
}
|
|
24
|
+
if ('toStringTag' in Symbol && typeof Symbol.toStringTag === 'symbol') {
|
|
25
|
+
Object.defineProperty(k, Symbol.toStringTag, { value: key });
|
|
26
|
+
}
|
|
45
27
|
|
|
46
|
-
|
|
28
|
+
return k;
|
|
47
29
|
}
|
|
48
30
|
|
|
49
31
|
export function defineMetadata<T>(res: HasMetadata, key: MetadataKey<T>, opts: T): void;
|
|
50
32
|
export function defineMetadata<T>(key: MetadataKey<T>, items: Array<[HasMetadata, T]>): void;
|
|
51
33
|
export function defineMetadata<T>(a: any, b: any, c?: any) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
34
|
+
if (Array.isArray(b)) {
|
|
35
|
+
const key = a;
|
|
36
|
+
const items = b;
|
|
37
|
+
for (const [res, opts] of items) {
|
|
38
|
+
defineMetadata(res, key, opts);
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
const res = a;
|
|
42
|
+
const key = b;
|
|
43
|
+
const opts = c;
|
|
44
|
+
res.metadata = res.metadata || {};
|
|
45
|
+
set(res.metadata, key.key, opts);
|
|
46
|
+
}
|
|
65
47
|
}
|
|
66
48
|
|
|
67
49
|
export function getMetadata<T>(res: HasMetadata | undefined | null, key: MetadataKey<T>): T | undefined {
|
|
68
|
-
|
|
69
|
-
|
|
50
|
+
if (!res?.metadata) return undefined;
|
|
51
|
+
return get(res.metadata, key.key);
|
|
70
52
|
}
|