@wener/common 1.0.5 → 2.0.1

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.
Files changed (152) hide show
  1. package/lib/cn/DivisionCode.js +55 -32
  2. package/lib/cn/DivisionCode.test.js +140 -0
  3. package/lib/cn/Mod11Checksum.js +80 -37
  4. package/lib/cn/Mod31Checksum.js +89 -40
  5. package/lib/cn/ResidentIdentityCardNumber.js +16 -16
  6. package/lib/cn/ResidentIdentityCardNumber.test.js +21 -0
  7. package/lib/cn/UnifiedSocialCreditCode.js +32 -26
  8. package/lib/cn/UnifiedSocialCreditCode.test.js +16 -0
  9. package/lib/cn/formatDate.js +5 -7
  10. package/lib/cn/index.js +0 -1
  11. package/lib/cn/parseSex.js +0 -2
  12. package/lib/cn/types.d.js +0 -2
  13. package/lib/consola/createStandardConsolaReporter.js +6 -6
  14. package/lib/consola/formatLogObject.js +133 -32
  15. package/lib/consola/index.js +0 -1
  16. package/lib/data/formatSort.js +5 -6
  17. package/lib/data/formatSort.test.js +34 -0
  18. package/lib/data/index.js +0 -1
  19. package/lib/data/maybeNumber.js +11 -5
  20. package/lib/data/parseSort.js +28 -22
  21. package/lib/data/parseSort.test.js +188 -0
  22. package/lib/data/resolvePagination.js +27 -16
  23. package/lib/data/resolvePagination.test.js +232 -0
  24. package/lib/data/types.d.js +0 -2
  25. package/lib/index.js +0 -1
  26. package/lib/jsonschema/JsonSchema.js +78 -52
  27. package/lib/jsonschema/JsonSchema.test.js +137 -0
  28. package/lib/jsonschema/index.js +0 -1
  29. package/lib/jsonschema/types.d.js +5 -3
  30. package/lib/meta/defineFileType.js +103 -20
  31. package/lib/meta/defineInit.js +250 -31
  32. package/lib/meta/defineMetadata.js +140 -24
  33. package/lib/meta/defineMetadata.test.js +13 -0
  34. package/lib/meta/index.js +0 -1
  35. package/lib/password/PHC.js +87 -63
  36. package/lib/password/PHC.test.js +539 -0
  37. package/lib/password/Password.js +291 -29
  38. package/lib/password/Password.test.js +362 -0
  39. package/lib/password/createArgon2PasswordAlgorithm.js +191 -35
  40. package/lib/password/createBase64PasswordAlgorithm.js +141 -8
  41. package/lib/password/createBcryptPasswordAlgorithm.js +168 -13
  42. package/lib/password/createPBKDF2PasswordAlgorithm.js +228 -46
  43. package/lib/password/createScryptPasswordAlgorithm.js +211 -55
  44. package/lib/password/index.js +0 -1
  45. package/lib/password/server/index.js +0 -1
  46. package/lib/resource/Identifiable.js +1 -0
  47. package/lib/resource/getTitleOfResource.js +10 -0
  48. package/lib/resource/index.js +1 -0
  49. package/lib/resource/schema/AnyResourceSchema.js +89 -0
  50. package/lib/resource/schema/BaseResourceSchema.js +29 -0
  51. package/lib/resource/schema/ResourceActionType.js +118 -0
  52. package/lib/resource/schema/ResourceStatus.js +93 -0
  53. package/lib/resource/schema/ResourceType.js +24 -0
  54. package/lib/resource/schema/SchemaRegistry.js +38 -0
  55. package/lib/resource/schema/SexType.js +10 -0
  56. package/lib/resource/schema/types.js +89 -0
  57. package/lib/resource/schema/types.test.js +14 -0
  58. package/lib/schema/TypeSchema.d.js +1 -0
  59. package/lib/schema/createSchemaData.js +173 -0
  60. package/lib/schema/findJsonSchemaByPath.js +49 -0
  61. package/lib/schema/getSchemaCache.js +11 -0
  62. package/lib/schema/getSchemaOptions.js +24 -0
  63. package/lib/schema/index.js +5 -0
  64. package/lib/schema/toJsonSchema.js +441 -0
  65. package/lib/schema/toJsonSchema.test.js +27 -0
  66. package/lib/schema/validate.js +124 -0
  67. package/lib/search/AdvanceSearch.js +0 -1
  68. package/lib/search/AdvanceSearch.test.js +435 -0
  69. package/lib/search/formatAdvanceSearch.js +41 -27
  70. package/lib/search/index.js +0 -1
  71. package/lib/search/optimizeAdvanceSearch.js +79 -25
  72. package/lib/search/parseAdvanceSearch.js +5 -5
  73. package/lib/search/parser.d.js +0 -2
  74. package/lib/search/parser.js +97 -74
  75. package/lib/search/types.d.js +0 -2
  76. package/lib/tools/generateSchema.js +197 -39
  77. package/lib/tools/renderJsonSchemaToMarkdownDoc.js +143 -55
  78. package/package.json +23 -11
  79. package/src/data/maybeNumber.ts +5 -1
  80. package/src/data/resolvePagination.test.ts +1 -1
  81. package/src/data/resolvePagination.ts +18 -7
  82. package/src/data/types.d.ts +12 -0
  83. package/src/jsonschema/JsonSchema.test.ts +17 -0
  84. package/src/jsonschema/JsonSchema.ts +2 -2
  85. package/src/jsonschema/types.d.ts +63 -12
  86. package/src/resource/Identifiable.ts +3 -0
  87. package/src/resource/getTitleOfResource.tsx +6 -0
  88. package/src/resource/index.ts +3 -0
  89. package/src/resource/schema/AnyResourceSchema.ts +113 -0
  90. package/src/resource/schema/BaseResourceSchema.ts +32 -0
  91. package/src/resource/schema/ResourceActionType.ts +123 -0
  92. package/src/resource/schema/ResourceStatus.ts +94 -0
  93. package/src/resource/schema/ResourceType.ts +25 -0
  94. package/src/resource/schema/SchemaRegistry.ts +42 -0
  95. package/src/resource/schema/SexType.ts +13 -0
  96. package/src/resource/schema/types.test.ts +18 -0
  97. package/src/resource/schema/types.ts +105 -0
  98. package/src/schema/TypeSchema.d.ts +32 -0
  99. package/src/schema/createSchemaData.ts +81 -0
  100. package/src/schema/findJsonSchemaByPath.ts +37 -0
  101. package/src/schema/getSchemaCache.ts +21 -0
  102. package/src/schema/getSchemaOptions.ts +24 -0
  103. package/src/schema/index.ts +6 -0
  104. package/src/schema/toJsonSchema.test.ts +37 -0
  105. package/src/schema/toJsonSchema.ts +200 -0
  106. package/src/schema/validate.ts +135 -0
  107. package/src/tools/generateSchema.ts +28 -28
  108. package/lib/cn/DivisionCode.js.map +0 -1
  109. package/lib/cn/Mod11Checksum.js.map +0 -1
  110. package/lib/cn/Mod31Checksum.js.map +0 -1
  111. package/lib/cn/ResidentIdentityCardNumber.js.map +0 -1
  112. package/lib/cn/UnifiedSocialCreditCode.js.map +0 -1
  113. package/lib/cn/formatDate.js.map +0 -1
  114. package/lib/cn/index.js.map +0 -1
  115. package/lib/cn/parseSex.js.map +0 -1
  116. package/lib/cn/types.d.js.map +0 -1
  117. package/lib/consola/createStandardConsolaReporter.js.map +0 -1
  118. package/lib/consola/formatLogObject.js.map +0 -1
  119. package/lib/consola/index.js.map +0 -1
  120. package/lib/data/formatSort.js.map +0 -1
  121. package/lib/data/index.js.map +0 -1
  122. package/lib/data/maybeNumber.js.map +0 -1
  123. package/lib/data/parseSort.js.map +0 -1
  124. package/lib/data/resolvePagination.js.map +0 -1
  125. package/lib/data/types.d.js.map +0 -1
  126. package/lib/index.js.map +0 -1
  127. package/lib/jsonschema/JsonSchema.js.map +0 -1
  128. package/lib/jsonschema/index.js.map +0 -1
  129. package/lib/jsonschema/types.d.js.map +0 -1
  130. package/lib/meta/defineFileType.js.map +0 -1
  131. package/lib/meta/defineInit.js.map +0 -1
  132. package/lib/meta/defineMetadata.js.map +0 -1
  133. package/lib/meta/index.js.map +0 -1
  134. package/lib/password/PHC.js.map +0 -1
  135. package/lib/password/Password.js.map +0 -1
  136. package/lib/password/createArgon2PasswordAlgorithm.js.map +0 -1
  137. package/lib/password/createBase64PasswordAlgorithm.js.map +0 -1
  138. package/lib/password/createBcryptPasswordAlgorithm.js.map +0 -1
  139. package/lib/password/createPBKDF2PasswordAlgorithm.js.map +0 -1
  140. package/lib/password/createScryptPasswordAlgorithm.js.map +0 -1
  141. package/lib/password/index.js.map +0 -1
  142. package/lib/password/server/index.js.map +0 -1
  143. package/lib/search/AdvanceSearch.js.map +0 -1
  144. package/lib/search/formatAdvanceSearch.js.map +0 -1
  145. package/lib/search/index.js.map +0 -1
  146. package/lib/search/optimizeAdvanceSearch.js.map +0 -1
  147. package/lib/search/parseAdvanceSearch.js.map +0 -1
  148. package/lib/search/parser.d.js.map +0 -1
  149. package/lib/search/parser.js.map +0 -1
  150. package/lib/search/types.d.js.map +0 -1
  151. package/lib/tools/generateSchema.js.map +0 -1
  152. package/lib/tools/renderJsonSchemaToMarkdownDoc.js.map +0 -1
@@ -55,7 +55,7 @@ type TypeOfSchema<S> = S extends TSchema ? Static<S> : any;
55
55
  export namespace JsonSchema {
56
56
  export let schemas: JsonSchemaDef[] = [];
57
57
 
58
- export const createAjv = _createAjv;
58
+ export let createAjv = _createAjv;
59
59
 
60
60
  export function addSchema(
61
61
  schema: JsonSchemaDef,
@@ -113,7 +113,7 @@ export namespace JsonSchema {
113
113
  // will not ensure value match the rule
114
114
  return match(schema as JsonSchemaDef)
115
115
  .returnType<any>()
116
- .with({ const: P.select() }, (v) => v)
116
+ .with({ const: P.nonNullable }, (v) => v)
117
117
  .with({ default: P.select() }, (v) => v)
118
118
  .with({ anyOf: P.nonNullable }, (schema) => {
119
119
  return create(schema.anyOf[0]);
@@ -5,7 +5,8 @@ type JsonSchemaTypeName =
5
5
  | 'boolean'
6
6
  | 'object'
7
7
  | 'array'
8
- | 'null';
8
+ | 'null'
9
+ | undefined;
9
10
 
10
11
  type JsonValue =
11
12
  | string //
@@ -13,15 +14,21 @@ type JsonValue =
13
14
  | boolean
14
15
  | { [key: string]: JsonValue }
15
16
  | JsonValue[]
16
- | null;
17
+ | null
18
+ | undefined; //
17
19
 
18
20
  type JsonSchemaVersion =
19
21
  | 'http://json-schema.org/schema' // latest
22
+ //
20
23
  | 'https://json-schema.org/draft/2020-12/schema'
24
+ | 'https://json-schema.org/draft/2019-09/schema'
21
25
  // draft-07
22
- | 'http://json-schema.org/draft-07/schema#' //
23
- | 'http://json-schema.org/draft-07/hyper-schema#';
24
-
26
+ | 'https://json-schema.org/draft-07/schema'
27
+ | 'http://json-schema.org/draft-07/schema'
28
+ | 'https://json-schema.org/draft-07/hyper-schema'
29
+ | 'http://json-schema.org/draft-07/hyper-schema'
30
+ //
31
+ | 'https://json-schema.org/draft-04/schema';
25
32
  type JsonSchemaFormatName =
26
33
  //
27
34
  | 'date-time'
@@ -47,7 +54,12 @@ type JsonSchemaFormatName =
47
54
  | 'json-pointer'
48
55
  | 'relative-json-pointer';
49
56
 
50
- export type JsonSchemaDef = {
57
+ /**
58
+ * JSON Schema Definition
59
+ *
60
+ * @see https://json-schema.org/specification-links.html
61
+ */
62
+ export type JsonSchemaDef<I = any, O = I> = {
51
63
  $id?: string;
52
64
  $ref?: string;
53
65
  /**
@@ -67,7 +79,7 @@ export type JsonSchemaDef = {
67
79
  $defs?: { [key: string]: JsonSchemaDef };
68
80
 
69
81
  type?: JsonSchemaTypeName | JsonSchemaTypeName[];
70
- enum?: JsonValue[];
82
+ enum?: JsonValue;
71
83
  const?: JsonValue;
72
84
 
73
85
  //region Numeric Validation
@@ -90,12 +102,23 @@ export type JsonSchemaDef = {
90
102
 
91
103
  //region Array Validation
92
104
 
105
+ /**
106
+ * schema for array items
107
+ * @see https://json-schema.org/understanding-json-schema/reference/array.html#items
108
+ */
93
109
  items?: JsonSchemaDef | JsonSchemaDef[];
94
- additionalItems?: JsonSchemaDef;
110
+ additionalItems?: JsonSchemaDef | boolean;
95
111
  maxItems?: number;
96
112
  minItems?: number;
97
113
  uniqueItems?: boolean;
98
114
  contains?: JsonSchemaDef;
115
+
116
+ //region Draft 2022-12
117
+ minContains?: number;
118
+ maxContains?: number;
119
+ prefixItems?: JsonSchemaDef[];
120
+ //endregion
121
+
99
122
  //endregion
100
123
 
101
124
  //region Object Validation
@@ -105,9 +128,25 @@ export type JsonSchemaDef = {
105
128
  required?: string[];
106
129
  properties?: { [key: string]: JsonSchemaDef };
107
130
  patternProperties?: { [key: string]: JsonSchemaDef };
108
- additionalProperties?: JsonSchemaDef;
109
- dependencies?: { [key: string]: JsonSchemaDef | string[] };
131
+ /**
132
+ * Replaced by {@link unevaluatedProperties} in Draft 2019-09+
133
+ */
134
+ additionalProperties?: JsonSchemaDef | boolean; // true = {}
135
+ /**
136
+ * Superseded by {@link dependentSchemas}, {@link dependentRequired} in Draft 2019-09+
137
+ */
138
+ dependencies?: { [key: string]: undefined | JsonSchemaDef | string[] };
110
139
  propertyNames?: JsonSchemaDef;
140
+
141
+ //region Draft 2019-09+
142
+
143
+ dependentSchemas?: { [key: string]: JsonSchemaDef };
144
+ dependentRequired?: { [key: string]: string[] };
145
+ unevaluatedProperties?: JsonSchemaDef | boolean;
146
+ unevaluatedItems?: JsonSchemaDef | boolean;
147
+
148
+ //endregion
149
+
111
150
  //endregion
112
151
 
113
152
  //region Conditional
@@ -120,9 +159,9 @@ export type JsonSchemaDef = {
120
159
 
121
160
  //region Boolean Logic
122
161
 
123
- allOf?: JsonSchemaDef[];
162
+ allOf?: JsonSchemaDef[]; // extends, merge
124
163
  anyOf?: JsonSchemaDef[];
125
- oneOf?: JsonSchemaDef[];
164
+ oneOf?: JsonSchemaDef[]; // polymorphism, enum
126
165
  not?: JsonSchemaDef;
127
166
  //endregion
128
167
 
@@ -159,5 +198,17 @@ export type JsonSchemaDef = {
159
198
 
160
199
  //endregion
161
200
 
201
+ //region OpenAPI Spec
202
+
162
203
  nullable?: boolean;
204
+
205
+ discriminator?: {
206
+ propertyName: string;
207
+ mapping?: { [key: string]: string };
208
+ };
209
+
210
+ //endregion
211
+
212
+ // [key: `x-${string}`]: JsonValue;
213
+ [key: string]: JsonValue;
163
214
  };
@@ -0,0 +1,3 @@
1
+ export type Identifiable = {
2
+ id: string;
3
+ };
@@ -0,0 +1,6 @@
1
+ export function getTitleOfResource(res?: any): string | undefined {
2
+ if (res && typeof res === 'object') {
3
+ return res.displayName || res.title || res.fullName || res.loginName || res.topic || res.code;
4
+ }
5
+ return undefined;
6
+ }
@@ -0,0 +1,3 @@
1
+ export { type AnyResource } from './schema/AnyResourceSchema';
2
+ export { type Identifiable } from './Identifiable';
3
+ export { getTitleOfResource } from './getTitleOfResource';
@@ -0,0 +1,113 @@
1
+ import { z } from 'zod/v4';
2
+ import { SexTypeSchema } from './SexType';
3
+ import { rz } from './types';
4
+
5
+ export type AnyResource = z.infer<typeof AnyResourceSchema>;
6
+ export const AnyResourceSchema = z
7
+ .object({
8
+ id: rz.resourceId.readonly().describe('ID'),
9
+ uid: z.guid().nullish().readonly().describe('唯一ID'),
10
+ tid: rz.resourceId.nullish().readonly().describe('租户ID'),
11
+ sid: z.coerce.number().nullish().readonly().describe('序号'),
12
+ eid: z.string().nullish().readonly().describe('外部ID'),
13
+ cid: z.string().nullish().readonly().describe('服务商ID'),
14
+ rid: z.string().nullish().readonly().describe('服务商资源ID'),
15
+
16
+ code: z.string().nullish().describe('编号'),
17
+
18
+ fullName: rz.friendlyName.optional().describe('名称'),
19
+ displayName: rz.friendlyName.nullish().describe('显示名'),
20
+ loginName: rz.loginName.nullish().describe('登录名'),
21
+ email: z.string().nullish().describe('邮箱'),
22
+ emailVerifiedAt: rz.date.nullish().describe('邮箱验证时间'),
23
+ phoneNumber: rz.phoneNumber.nullish().describe('电话号码'),
24
+ phoneNumberVerifiedAt: rz.date.nullish().describe('电话号码验证时间'),
25
+
26
+ jobNumber: z.string().nullish().describe('工号'),
27
+ jobTitle: z.string().nullish().describe('职位'),
28
+ birthDate: rz.date.nullish().describe('出生日期'),
29
+
30
+ password: rz.password.nullish().describe('密码'),
31
+ sex: SexTypeSchema.nullish().describe('性别'),
32
+
33
+ contactName: rz.friendlyName.nullish().describe('联系人'),
34
+ contactPhone: rz.phoneNumber.nullish().describe('联系电话'),
35
+ contactEmail: z.string().nullish().describe('联系邮箱'),
36
+ contactAddress: z.string().nullish().describe('联系地址'),
37
+ alternativeName: rz.friendlyName.nullish().describe('备用联系人'),
38
+ alternativeEmail: z.string().nullish().describe('备用联系邮箱'),
39
+ alternativePhone: rz.phoneNumber.nullish().describe('备用联系电话'),
40
+ alternativeAddress: z.string().nullish().describe('备用联系地址'),
41
+
42
+ title: z.string().trim().nullish().describe('标题'),
43
+ description: z.string().trim().nullish().describe('描述'),
44
+ topic: z.string().nullish().describe('主题'),
45
+ summary: z.string().nullish().describe('摘要'),
46
+
47
+ avatarUrl: z.string().nullish().describe('头像'),
48
+ photoUrl: z.string().nullish().describe('照片'),
49
+ imageUrl: z.string().nullish().describe('图片'),
50
+ bannerUrl: z.string().nullish().describe('横幅图片'),
51
+
52
+ type: z.string().nullish().describe('类型'),
53
+ data: z.record(z.string(), z.any()).nullish().describe('数据'),
54
+
55
+ licenseNumber: z.string().nullish().describe('证件号码'),
56
+ licenseStartDate: rz.date.nullish().describe('证件有效期开始日期'),
57
+ licenseEndDate: rz.date.nullish().describe('证件有效期结束日期'),
58
+ licenseAddress: z.string().nullish().describe('证件地址'),
59
+ licenseFrontUrl: z.string().nullish().describe('证件正面照片'),
60
+ licenseBackUrl: z.string().nullish().describe('证件背面照片'),
61
+
62
+ address: z.string().nullish().describe('地址'),
63
+ divisionCode: z.string().nullish().describe('行政区划代码'),
64
+ province: z.string().nullish().describe('省'),
65
+ city: z.string().nullish().describe('市'),
66
+
67
+ startDate: rz.date.nullish().describe('开始日期'),
68
+ endDate: rz.date.nullish().describe('结束日期'),
69
+ startTime: rz.dateTime.nullish().describe('开始时间'),
70
+ endTime: rz.dateTime.nullish().describe('结束时间'),
71
+
72
+ userId: rz.resourceIdOf('User').nullish(),
73
+ entityId: rz.resourceId.nullish().describe('关联的实体'),
74
+ entityType: z.string().nullish().describe('关联的实体类型'),
75
+ customerId: rz.resourceId.nullish().describe('客户'),
76
+ customerType: z.string().nullish().describe('客户类型'),
77
+ contactId: rz.resourceId.nullish().describe('联系人'),
78
+ accountId: rz.resourceId.nullish().describe('账户'),
79
+
80
+ ownerId: rz.resourceId.nullish(),
81
+ ownerType: z.string().nullish().describe('负责人类型'),
82
+ owningUserId: rz.resourceId.nullish().describe('负责人'),
83
+
84
+ parentId: rz.resourceId.nullish().describe('父级'),
85
+
86
+ metadata: z.record(z.string(), z.any()).nullish().describe('元数据'),
87
+ tags: z.array(z.string()).nullish().describe('标记'),
88
+ notes: z.string().nullish().describe('备注'),
89
+
90
+ state: z.string().nullish().describe('系统状态'),
91
+ status: z.string().nullish().describe('状态'),
92
+ statusReason: z.string().nullish().describe('状态原因'),
93
+ statusUpdatedAt: rz.dateTime.nullish().describe('状态更新时间'),
94
+ statusUpdatedById: rz.resourceId.nullish().describe('状态更新人'),
95
+
96
+ createdAt: rz.dateTime.nullish().readonly().describe('创建时间'),
97
+ updatedAt: rz.dateTime.nullish().readonly().describe('更新时间'),
98
+ deletedAt: rz.dateTime.nullish().readonly().describe('删除时间'),
99
+
100
+ createdById: z.string().nullish().readonly().describe('创建人'),
101
+ updatedById: z.string().nullish().readonly().describe('更新人'),
102
+ deletedById: z.string().nullish().readonly().describe('删除人'),
103
+
104
+ attributes: z.record(z.string(), z.any()).nullish().describe('自定义属性'),
105
+ properties: z.record(z.string(), z.any()).nullish().describe('属性'),
106
+ extensions: z.record(z.string(), z.any()).nullish().describe('扩展属性'),
107
+
108
+ __typename: z.coerce.string().nullish().describe('类型'),
109
+ })
110
+ .meta({
111
+ title: 'AnyResource',
112
+ description: '任意资源',
113
+ });
@@ -0,0 +1,32 @@
1
+ import { AnyResourceSchema } from './AnyResourceSchema';
2
+
3
+ export const BaseResourceSchema = AnyResourceSchema.pick({
4
+ id: true,
5
+ uid: true,
6
+ tid: true,
7
+ eid: true,
8
+
9
+ state: true,
10
+ status: true,
11
+ statusReason: true,
12
+ statusUpdatedAt: true,
13
+ statusUpdatedById: true,
14
+
15
+ createdAt: true,
16
+ updatedAt: true,
17
+ deletedAt: true,
18
+ createdById: true,
19
+ updatedById: true,
20
+ deletedById: true,
21
+ attributes: true,
22
+ properties: true,
23
+ }).required({
24
+ uid: true,
25
+ tid: true,
26
+ state: true,
27
+ status: true,
28
+ createdAt: true,
29
+ updatedAt: true,
30
+ attributes: true,
31
+ properties: true,
32
+ });
@@ -0,0 +1,123 @@
1
+ import { z } from 'zod/v4';
2
+ import type { EnumValues } from './types';
3
+
4
+ export const ResourceActionType = Object.freeze({
5
+ __proto__: null,
6
+ Approve: 'Approve',
7
+ AssignOwner: 'AssignOwner',
8
+ Bind: 'Bind',
9
+ BindCustomer: 'BindCustomer',
10
+ BindEntity: 'BindEntity',
11
+ BindUser: 'BindUser',
12
+ Cancel: 'Cancel',
13
+ ChangePassword: 'ChangePassword',
14
+ ClaimOwner: 'ClaimOwner',
15
+ Close: 'Close',
16
+ Communicate: 'Communicate',
17
+ Complete: 'Complete',
18
+ Create: 'Create',
19
+ Delete: 'Delete',
20
+ Deny: 'Deny',
21
+ Disable: 'Disable',
22
+ Download: 'Download',
23
+ Edit: 'Edit',
24
+ Enable: 'Enable',
25
+ List: 'List',
26
+ Login: 'Login',
27
+ Logout: 'Logout',
28
+ Manage: 'Manage',
29
+ Publish: 'Publish',
30
+ Purge: 'Purge',
31
+ Read: 'Read',
32
+ Reject: 'Reject',
33
+ ReleaseOwner: 'ReleaseOwner',
34
+ ResetPassword: 'ResetPassword',
35
+ ReturnForRevision: 'ReturnForRevision',
36
+ Revise: 'Revise',
37
+ SaveDraft: 'SaveDraft',
38
+ Signup: 'Signup',
39
+ Submit: 'Submit',
40
+ SubmitAgency: 'SubmitAgency',
41
+ SubmitVendor: 'SubmitVendor',
42
+ Suspend: 'Suspend',
43
+ Terminate: 'Terminate',
44
+ Timeout: 'Timeout',
45
+ Unbind: 'Unbind',
46
+ UnbindCustomer: 'UnbindCustomer',
47
+ UnbindEntity: 'UnbindEntity',
48
+ UnbindUser: 'UnbindUser',
49
+ Undelete: 'Undelete',
50
+ Unpublish: 'Unpublish',
51
+ Update: 'Update',
52
+ UpdateNotes: 'UpdateNotes',
53
+ UpdateTags: 'UpdateTags',
54
+ Upload: 'Upload',
55
+ Validate: 'Validate',
56
+ View: 'View',
57
+ Write: 'Write',
58
+ } as const);
59
+ export type ResourceActionType = EnumValues<typeof ResourceActionType>;
60
+ export const ResourceActionTypeSchema = z
61
+ .union([
62
+ z.literal(ResourceActionType.Approve).describe('批准'),
63
+ z.literal(ResourceActionType.AssignOwner).describe('分配负责人'),
64
+ z.literal(ResourceActionType.Bind).describe('绑定'),
65
+ z.literal(ResourceActionType.BindCustomer).describe('绑定客户'),
66
+ z.literal(ResourceActionType.BindEntity).describe('绑定实体'),
67
+ z.literal(ResourceActionType.BindUser).describe('绑定用户'),
68
+ z.literal(ResourceActionType.Cancel).describe('取消'),
69
+ z.literal(ResourceActionType.ChangePassword).describe('修改密码'),
70
+ z.literal(ResourceActionType.ClaimOwner).describe('认领负责人'),
71
+ z.literal(ResourceActionType.Close).describe('关闭'),
72
+ z.literal(ResourceActionType.Communicate).describe('沟通'),
73
+ z.literal(ResourceActionType.Complete).describe('完成'),
74
+ z.literal(ResourceActionType.Create).describe('创建'),
75
+ z.literal(ResourceActionType.Delete).describe('删除'),
76
+ z.literal(ResourceActionType.Deny).describe('拒绝'),
77
+ z.literal(ResourceActionType.Disable).describe('禁用'),
78
+ z.literal(ResourceActionType.Download).describe('下载'),
79
+ z.literal(ResourceActionType.Edit).describe('编辑'),
80
+ z.literal(ResourceActionType.Enable).describe('启用'),
81
+ z.literal(ResourceActionType.List).describe('列表'),
82
+ z.literal(ResourceActionType.Login).describe('登录'),
83
+ z.literal(ResourceActionType.Logout).describe('登出'),
84
+ z.literal(ResourceActionType.Manage).describe('管理'),
85
+ z.literal(ResourceActionType.Publish).describe('发布'),
86
+ z.literal(ResourceActionType.Purge).describe('彻底删除'),
87
+ z.literal(ResourceActionType.Read).describe('读取'),
88
+ z.literal(ResourceActionType.Reject).describe('驳回'),
89
+ z.literal(ResourceActionType.ReleaseOwner).describe('释放负责人'),
90
+ z.literal(ResourceActionType.ResetPassword).describe('重置密码'),
91
+ z.literal(ResourceActionType.ReturnForRevision).describe('退回修订'),
92
+ z.literal(ResourceActionType.Revise).describe('修订'),
93
+ z.literal(ResourceActionType.SaveDraft).describe('保存草稿'),
94
+ z.literal(ResourceActionType.Signup).describe('注册'),
95
+ z.literal(ResourceActionType.Submit).describe('提交'),
96
+ z.literal(ResourceActionType.SubmitAgency).describe('提交机构'),
97
+ z.literal(ResourceActionType.SubmitVendor).describe('提交供应商'),
98
+ z.literal(ResourceActionType.Suspend).describe('暂停'),
99
+ z.literal(ResourceActionType.Terminate).describe('终止'),
100
+ z.literal(ResourceActionType.Timeout).describe('超时'),
101
+ z.literal(ResourceActionType.Unbind).describe('解绑'),
102
+ z.literal(ResourceActionType.UnbindCustomer).describe('解绑客户'),
103
+ z.literal(ResourceActionType.UnbindEntity).describe('解绑实体'),
104
+ z.literal(ResourceActionType.UnbindUser).describe('解绑用户'),
105
+ z.literal(ResourceActionType.Undelete).describe('恢复删除'),
106
+ z.literal(ResourceActionType.Unpublish).describe('取消发布'),
107
+ z.literal(ResourceActionType.Update).describe('更新'),
108
+ z.literal(ResourceActionType.UpdateNotes).describe('修改备注'),
109
+ z.literal(ResourceActionType.UpdateNotes).describe('更新备注'),
110
+ z.literal(ResourceActionType.UpdateTags).describe('更新标签'),
111
+ z.literal(ResourceActionType.Upload).describe('上传'),
112
+ z.literal(ResourceActionType.Validate).describe('校验'),
113
+ z.literal(ResourceActionType.View).describe('查看'),
114
+ z.literal(ResourceActionType.Write).describe('写入'),
115
+ ])
116
+ .meta({
117
+ title: 'ResourceAction',
118
+ description: '资源操作',
119
+ type: 'string',
120
+ });
121
+ export const ResourceActionDataSchema = z.object({
122
+ reason: z.string().optional().describe('操作原因'),
123
+ });
@@ -0,0 +1,94 @@
1
+ import { z } from 'zod/v4';
2
+ import type { EnumValues } from './types';
3
+
4
+ export const ResourceStatus = Object.freeze({
5
+ __proto__: null,
6
+ Active: 'Active',
7
+ Approved: 'Approved',
8
+ Cancelled: 'Cancelled',
9
+ Closed: 'Closed',
10
+ Completed: 'Completed',
11
+ Confirmed: 'Confirmed',
12
+ Contacted: 'Contacted',
13
+ Contacting: 'Contacting',
14
+ Deleted: 'Deleted',
15
+ Denied: 'Denied',
16
+ Disabled: 'Disabled',
17
+ Disqualified: 'Disqualified',
18
+ Draft: 'Draft',
19
+ Enabled: 'Enabled',
20
+ Expired: 'Expired',
21
+ Failed: 'Failed',
22
+ InProgress: 'InProgress',
23
+ Inactive: 'Inactive',
24
+ Initial: 'Initial',
25
+ Lost: 'Lost',
26
+ New: 'New',
27
+ Nurturing: 'Nurturing',
28
+ OnHold: 'OnHold',
29
+ Open: 'Open',
30
+ Overdue: 'Overdue',
31
+ Paid: 'Paid',
32
+ PartiallyPaid: 'PartiallyPaid',
33
+ Pending: 'Pending',
34
+ PendingAcceptance: 'PendingAcceptance',
35
+ PendingApproval: 'PendingApproval',
36
+ PendingConfirmation: 'PendingConfirmation',
37
+ PendingResubmission: 'PendingResubmission',
38
+ Posted: 'Posted',
39
+ Published: 'Published',
40
+ Qualified: 'Qualified',
41
+ Rejected: 'Rejected',
42
+ Submitted: 'Submitted',
43
+ Suspended: 'Suspended',
44
+ Terminated: 'Terminated',
45
+ Unqualified: 'Unqualified',
46
+ Void: 'Void',
47
+ Won: 'Won',
48
+ } as const);
49
+ export type ResourceStatus = EnumValues<typeof ResourceStatus>;
50
+ export const ResourceStatusSchema = z
51
+ .union([
52
+ z.literal(ResourceStatus.Active).describe('活跃'),
53
+ z.literal(ResourceStatus.Approved).describe('已通过'),
54
+ z.literal(ResourceStatus.Cancelled).describe('已取消'),
55
+ z.literal(ResourceStatus.Closed).describe('已关闭'),
56
+ z.literal(ResourceStatus.Completed).describe('已完成'),
57
+ z.literal(ResourceStatus.Confirmed).describe('已确认'),
58
+ z.literal(ResourceStatus.Contacted).describe('已联系'),
59
+ z.literal(ResourceStatus.Contacting).describe('联系中'),
60
+ z.literal(ResourceStatus.Deleted).describe('已删除'),
61
+ z.literal(ResourceStatus.Denied).describe('已拒绝'),
62
+ z.literal(ResourceStatus.Disabled).describe('已禁用'),
63
+ z.literal(ResourceStatus.Disqualified).describe('不合格'),
64
+ z.literal(ResourceStatus.Draft).describe('草稿'),
65
+ z.literal(ResourceStatus.Enabled).describe('已启用'),
66
+ z.literal(ResourceStatus.Expired).describe('已过期'),
67
+ z.literal(ResourceStatus.InProgress).describe('进行中'),
68
+ z.literal(ResourceStatus.Inactive).describe('停用'),
69
+ z.literal(ResourceStatus.Initial).describe('初始'),
70
+ z.literal(ResourceStatus.Lost).describe('已失去'),
71
+ z.literal(ResourceStatus.New).describe('新建'),
72
+ z.literal(ResourceStatus.Nurturing).describe('培育中'),
73
+ z.literal(ResourceStatus.OnHold).describe('搁置'),
74
+ z.literal(ResourceStatus.Open).describe('开放'),
75
+ z.literal(ResourceStatus.Overdue).describe('逾期'),
76
+ z.literal(ResourceStatus.Paid).describe('已支付'),
77
+ z.literal(ResourceStatus.PartiallyPaid).describe('部分支付'),
78
+ z.literal(ResourceStatus.Pending).describe('待审核'),
79
+ z.literal(ResourceStatus.PendingAcceptance).describe('待接受'),
80
+ z.literal(ResourceStatus.PendingApproval).describe('待批准'),
81
+ z.literal(ResourceStatus.PendingConfirmation).describe('待确认'),
82
+ z.literal(ResourceStatus.PendingResubmission).describe('待重新提交'),
83
+ z.literal(ResourceStatus.Posted).describe('已过账'),
84
+ z.literal(ResourceStatus.Published).describe('已发布'),
85
+ z.literal(ResourceStatus.Qualified).describe('已合格'),
86
+ z.literal(ResourceStatus.Rejected).describe('已驳回'),
87
+ z.literal(ResourceStatus.Submitted).describe('已提交'),
88
+ z.literal(ResourceStatus.Suspended).describe('暂停'),
89
+ z.literal(ResourceStatus.Terminated).describe('终止'),
90
+ z.literal(ResourceStatus.Unqualified).describe('未达标'),
91
+ z.literal(ResourceStatus.Void).describe('作废'),
92
+ z.literal(ResourceStatus.Won).describe('已赢得'),
93
+ ])
94
+ .meta({ title: 'ResourceStatus', description: '资源状态', type: 'string' });
@@ -0,0 +1,25 @@
1
+ import { z } from 'zod/v4';
2
+ import type { EnumValues } from './types';
3
+
4
+ export const ResourceType = Object.freeze({
5
+ __proto__: null,
6
+ User: 'User',
7
+ Customer: 'Customer',
8
+ Contact: 'Contact',
9
+ Account: 'Account',
10
+ Order: 'Order',
11
+ Lead: 'Lead',
12
+ Opportunity: 'Opportunity',
13
+ } as const);
14
+ export type ResourceType = EnumValues<typeof ResourceType>;
15
+ export const ResourceTypeSchema = z
16
+ .union([
17
+ z.literal(ResourceType.User).describe('用户'),
18
+ z.literal(ResourceType.Customer).describe('客户'),
19
+ z.literal(ResourceType.Contact).describe('联系人'),
20
+ z.literal(ResourceType.Account).describe('账户'),
21
+ z.literal(ResourceType.Order).describe('订单'),
22
+ z.literal(ResourceType.Lead).describe('线索'),
23
+ z.literal(ResourceType.Opportunity).describe('商机'),
24
+ ])
25
+ .meta({ title: 'ResourceType', description: '资源类型', type: 'string' });
@@ -0,0 +1,42 @@
1
+ import { getGlobalStates } from '@wener/utils';
2
+ import type { JsonSchemaDef } from '../../jsonschema';
3
+ import { toJsonSchema, type SchemaOutput, type TypeSchema } from '../../schema';
4
+
5
+ const types = getGlobalStates('@wener/common/resource/schema/SchemaRegistry', () => new Map<string, JsonSchemaDef>());
6
+
7
+ export function get(name: string): JsonSchemaDef;
8
+ export function get<S extends TypeSchema>(schema: S): JsonSchemaDef<SchemaOutput<S>>;
9
+ export function get(needle: TypeSchema | string) {
10
+ let key = getKey(needle);
11
+ let found = types.get(key);
12
+ if (found) {
13
+ return found;
14
+ } else {
15
+ if (typeof needle !== 'string') {
16
+ return toJsonSchema(needle);
17
+ }
18
+ }
19
+ throw new Error(`Schema not found: ${key}`);
20
+ }
21
+
22
+ function getKey(s: TypeSchema | string) {
23
+ let key;
24
+ if (typeof s === 'string') {
25
+ key = s;
26
+ } else {
27
+ let js = toJsonSchema(s);
28
+ key = js.$id || js.title;
29
+ }
30
+ if (!key) {
31
+ throw new Error(`Schema must have $id or title`);
32
+ }
33
+ return key;
34
+ }
35
+
36
+ export function add(def: TypeSchema) {
37
+ types.set(getKey(def), toJsonSchema(def));
38
+ }
39
+
40
+ export function set(key: TypeSchema | string, def: TypeSchema) {
41
+ types.set(getKey(key), toJsonSchema(def));
42
+ }
@@ -0,0 +1,13 @@
1
+ import { z } from 'zod/v4';
2
+ import type { EnumValues } from './types';
3
+
4
+ export const SexType = Object.freeze({
5
+ __proto__: null,
6
+ Male: 'Male',
7
+ Female: 'Female',
8
+ // Intersex
9
+ } as const);
10
+ export type SexType = EnumValues<typeof SexType>;
11
+ export const SexTypeSchema = z
12
+ .union([z.literal(SexType.Male).describe('男'), z.literal(SexType.Female).describe('女')])
13
+ .describe('性别');
@@ -0,0 +1,18 @@
1
+ import { describe, it } from 'vitest';
2
+ import { createSchemaData, toJsonSchema } from '../../schema';
3
+ import { renderJsonSchemaToMarkdownDoc } from '../../tools/renderJsonSchemaToMarkdownDoc';
4
+ import { AnyResourceSchema } from './AnyResourceSchema';
5
+
6
+ describe('schema', () => {
7
+ it('should be convert to jsonschema', () => {
8
+ console.log(renderJsonSchemaToMarkdownDoc(toJsonSchema(AnyResourceSchema)));
9
+ });
10
+
11
+ it('should create from schema', () => {
12
+ console.log(
13
+ createSchemaData(AnyResourceSchema, {
14
+ all: true,
15
+ }),
16
+ );
17
+ });
18
+ });