@acrool/rtk-query-codegen-openapi 1.2.0 → 1.3.0-alpha.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/lib/bin/cli.mjs +36 -36
- package/lib/bin/cli.mjs.map +1 -1
- package/lib/index.js +20 -13
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +20 -13
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/generators/types-generator.ts +33 -17
|
@@ -102,18 +102,24 @@ export function generateTypesFile(
|
|
|
102
102
|
|
|
103
103
|
// 生成 Response 類型(總是生成)
|
|
104
104
|
if (resTypeName) {
|
|
105
|
-
const
|
|
106
|
-
if (
|
|
105
|
+
const responseTypeResult = generateResponseTypeContent(endpoint, operationDefinitions, schemaTypeMap);
|
|
106
|
+
if (responseTypeResult.content.trim() === '') {
|
|
107
107
|
// 如果沒有實際內容,使用 void
|
|
108
108
|
endpointTypes.push(
|
|
109
109
|
`export type ${resTypeName} = void;`,
|
|
110
110
|
``
|
|
111
111
|
);
|
|
112
|
+
} else if (responseTypeResult.isDirectType) {
|
|
113
|
+
// 直接類型引用(如 $ref、array、primitive),使用 type alias
|
|
114
|
+
endpointTypes.push(
|
|
115
|
+
`export type ${resTypeName} = ${responseTypeResult.content};`,
|
|
116
|
+
``
|
|
117
|
+
);
|
|
112
118
|
} else {
|
|
113
|
-
//
|
|
119
|
+
// 有 object 屬性內容,使用 type 定義
|
|
114
120
|
endpointTypes.push(
|
|
115
121
|
`export type ${resTypeName} = {`,
|
|
116
|
-
|
|
122
|
+
responseTypeResult.content,
|
|
117
123
|
`};`,
|
|
118
124
|
``
|
|
119
125
|
);
|
|
@@ -206,12 +212,16 @@ function generateRequestTypeContent(endpoint: EndpointInfo, operationDefinitions
|
|
|
206
212
|
return properties.join('\n');
|
|
207
213
|
}
|
|
208
214
|
|
|
215
|
+
interface ResponseTypeResult {
|
|
216
|
+
content: string;
|
|
217
|
+
/** true when content is a direct type (e.g. Schema.Pet, string[]) rather than object properties */
|
|
218
|
+
isDirectType: boolean;
|
|
219
|
+
}
|
|
220
|
+
|
|
209
221
|
/**
|
|
210
222
|
* 生成 Response 類型的內容
|
|
211
223
|
*/
|
|
212
|
-
function generateResponseTypeContent(endpoint: EndpointInfo, operationDefinitions?: any[], schemaTypeMap: Record<string, string> = {}):
|
|
213
|
-
const properties: string[] = [];
|
|
214
|
-
|
|
224
|
+
function generateResponseTypeContent(endpoint: EndpointInfo, operationDefinitions?: any[], schemaTypeMap: Record<string, string> = {}): ResponseTypeResult {
|
|
215
225
|
// 嘗試從 operationDefinitions 中獲取響應結構
|
|
216
226
|
const operationDef = operationDefinitions?.find(op => {
|
|
217
227
|
// 嘗試多種匹配方式
|
|
@@ -233,21 +243,27 @@ function generateResponseTypeContent(endpoint: EndpointInfo, operationDefinition
|
|
|
233
243
|
Object.values(successResponse.content)[0]; // fallback 到第一個可用的 content-type
|
|
234
244
|
|
|
235
245
|
if (jsonContent?.schema) {
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
246
|
+
const schema = jsonContent.schema;
|
|
247
|
+
|
|
248
|
+
// 如果 schema 是 $ref 引用、array、或 primitive,直接使用 getTypeFromSchema
|
|
249
|
+
if (schema.$ref || schema.type !== 'object' || !schema.properties) {
|
|
250
|
+
const directType = getTypeFromSchema(schema, schemaTypeMap, 0);
|
|
251
|
+
if (directType && directType !== 'any') {
|
|
252
|
+
return { content: directType, isDirectType: true };
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// 如果是有 properties 的 object,展開為屬性列表
|
|
257
|
+
const responseProps = parseSchemaProperties(schema, schemaTypeMap);
|
|
258
|
+
if (responseProps.length > 0) {
|
|
259
|
+
return { content: responseProps.join('\n'), isDirectType: false };
|
|
260
|
+
}
|
|
241
261
|
}
|
|
242
262
|
}
|
|
243
263
|
}
|
|
244
264
|
|
|
245
265
|
// 如果沒有響應定義,返回空內容(將由調用方處理為 void)
|
|
246
|
-
|
|
247
|
-
return ''; // 返回空字串,讓調用方決定使用 void
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
return properties.join('\n');
|
|
266
|
+
return { content: '', isDirectType: false };
|
|
251
267
|
}
|
|
252
268
|
|
|
253
269
|
/**
|