czh-api 1.0.4 → 1.0.6

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.
@@ -111,7 +111,7 @@ function getModuleName(path: string, pathPrefixes: Array<{ path: string; package
111
111
  return parts[0] || 'default';
112
112
  }
113
113
 
114
- function convertOpenApiTypeToTypeScript(openApiType: string | undefined): string {
114
+ function convertOpenApiTypeToTypeScript(openApiType: string | undefined): string {
115
115
  if (!openApiType) return 'any';
116
116
 
117
117
  switch (openApiType) {
@@ -125,12 +125,88 @@ function convertOpenApiTypeToTypeScript(openApiType: string | undefined): string
125
125
  return 'boolean';
126
126
  case 'string':
127
127
  return 'string';
128
- case 'number':
129
- return 'number';
130
- default:
131
- return 'any';
132
- }
133
- }
128
+ case 'number':
129
+ return 'number';
130
+ default:
131
+ return openApiType;
132
+ }
133
+ }
134
+
135
+ function resolveSchemaToTypeScript(
136
+ schema: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined,
137
+ allSchemas: { [key: string]: any }
138
+ ): string {
139
+ if (!schema) return 'any';
140
+
141
+ if (isReferenceObject(schema)) {
142
+ return getSchemaName(schema.$ref) || 'any';
143
+ }
144
+
145
+ if (schema.anyOf && Array.isArray(schema.anyOf) && schema.anyOf.length > 0) {
146
+ const unionTypes = schema.anyOf
147
+ .map(item => resolveSchemaToTypeScript(item as OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject, allSchemas))
148
+ .filter(Boolean);
149
+ if (unionTypes.length > 0) {
150
+ return [...new Set(unionTypes)].join(' | ');
151
+ }
152
+ }
153
+
154
+ if (schema.oneOf && Array.isArray(schema.oneOf) && schema.oneOf.length > 0) {
155
+ const unionTypes = schema.oneOf
156
+ .map(item => resolveSchemaToTypeScript(item as OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject, allSchemas))
157
+ .filter(Boolean);
158
+ if (unionTypes.length > 0) {
159
+ return [...new Set(unionTypes)].join(' | ');
160
+ }
161
+ }
162
+
163
+ if (schema.allOf && Array.isArray(schema.allOf) && schema.allOf.length > 0) {
164
+ const intersectionTypes = schema.allOf
165
+ .map(item => resolveSchemaToTypeScript(item as OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject, allSchemas))
166
+ .filter(Boolean);
167
+ if (intersectionTypes.length > 0) {
168
+ return [...new Set(intersectionTypes)].join(' & ');
169
+ }
170
+ }
171
+
172
+ if (schema.type === 'array') {
173
+ const itemType = resolveSchemaToTypeScript(
174
+ schema.items as OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined,
175
+ allSchemas
176
+ );
177
+ return `${itemType || 'any'}[]`;
178
+ }
179
+
180
+ if (schema.type === 'object') {
181
+ if (schema.properties && Object.keys(schema.properties).length > 0) {
182
+ const requiredSet = new Set(schema.required || []);
183
+ const inlineFields = Object.entries(schema.properties).map(([key, value]) => {
184
+ const fieldType = resolveSchemaToTypeScript(
185
+ value as OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject,
186
+ allSchemas
187
+ );
188
+ const optional = requiredSet.has(key) ? '' : '?';
189
+ return `${key}${optional}: ${fieldType || 'any'}`;
190
+ });
191
+ return `{ ${inlineFields.join('; ')} }`;
192
+ }
193
+ if (schema.additionalProperties) {
194
+ if (schema.additionalProperties === true) {
195
+ return 'Record<string, any>';
196
+ }
197
+ const valueType = resolveSchemaToTypeScript(
198
+ schema.additionalProperties as OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject,
199
+ allSchemas
200
+ );
201
+ return `Record<string, ${valueType || 'any'}>`;
202
+ }
203
+ return 'any';
204
+ }
205
+
206
+ const baseType = convertOpenApiTypeToTypeScript(schema.type);
207
+ const nullable = schema.nullable ? ' | null' : '';
208
+ return `${baseType}${nullable}`;
209
+ }
134
210
 
135
211
  function extractJsdocParamsFromSchema(schema: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject, allSchemas: { [key: string]: any }): Required<Endpoint>['jsdocParams'] {
136
212
  const params: Required<Endpoint>['jsdocParams'] = [];
@@ -146,13 +222,15 @@ function extractJsdocParamsFromSchema(schema: OpenAPIV3.SchemaObject | OpenAPIV3
146
222
 
147
223
  if (targetSchema?.properties) {
148
224
  for (const propName in targetSchema.properties) {
149
- const prop = targetSchema.properties[propName] as OpenAPIV3.SchemaObject;
150
-
151
- let propType = 'any';
225
+ const prop = targetSchema.properties[propName] as OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject;
226
+ let propType = resolveSchemaToTypeScript(prop, allSchemas);
227
+ const propDescription = isReferenceObject(prop)
228
+ ? ((allSchemas[getSchemaName(prop.$ref)] as OpenAPIV3.SchemaObject | undefined)?.description || '')
229
+ : (prop?.description || '');
152
230
 
153
231
  // 处理 anyOf 数组 (FastAPI 常用的联合类型)
154
- if (prop.anyOf && Array.isArray(prop.anyOf)) {
155
- const types = prop.anyOf.map(item => {
232
+ if (false && (prop as OpenAPIV3.SchemaObject).anyOf && Array.isArray((prop as OpenAPIV3.SchemaObject).anyOf)) {
233
+ const types = (prop as OpenAPIV3.SchemaObject).anyOf!.map(item => {
156
234
  if (isReferenceObject(item)) {
157
235
  return getSchemaName(item.$ref);
158
236
  } else {
@@ -173,8 +251,8 @@ function extractJsdocParamsFromSchema(schema: OpenAPIV3.SchemaObject | OpenAPIV3
173
251
  }
174
252
  }
175
253
  // 处理 oneOf 数组
176
- else if (prop.oneOf && Array.isArray(prop.oneOf)) {
177
- const types = prop.oneOf.map(item => {
254
+ else if (false && (prop as OpenAPIV3.SchemaObject).oneOf && Array.isArray((prop as OpenAPIV3.SchemaObject).oneOf)) {
255
+ const types = (prop as OpenAPIV3.SchemaObject).oneOf!.map(item => {
178
256
  if (isReferenceObject(item)) {
179
257
  return getSchemaName(item.$ref);
180
258
  } else {
@@ -195,16 +273,26 @@ function extractJsdocParamsFromSchema(schema: OpenAPIV3.SchemaObject | OpenAPIV3
195
273
  }
196
274
  }
197
275
  // 处理普通类型
198
- else if (prop.type) {
199
- propType = convertOpenApiTypeToTypeScript(prop.type);
200
- }
276
+ else if (false && (prop as OpenAPIV3.SchemaObject).type) {
277
+ const propSchema: any = prop as any;
278
+ if (propSchema.type === 'array' && propSchema.items) {
279
+ if (isReferenceObject(propSchema.items)) {
280
+ propType = `${getSchemaName(propSchema.items.$ref)}[]`;
281
+ } else {
282
+ const itemType = convertOpenApiTypeToTypeScript((propSchema.items as OpenAPIV3.SchemaObject).type);
283
+ propType = `${itemType}[]`;
284
+ }
285
+ } else {
286
+ propType = convertOpenApiTypeToTypeScript(propSchema.type);
287
+ }
288
+ }
201
289
 
202
- params.push({
203
- name: propName,
204
- type: propType,
205
- description: prop?.description || '',
206
- required: targetSchema.required?.includes(propName)
207
- });
290
+ params.push({
291
+ name: propName,
292
+ type: propType || 'any',
293
+ description: propDescription,
294
+ required: targetSchema.required?.includes(propName)
295
+ });
208
296
  }
209
297
  }
210
298
  return params;