@bitstack/ng-query-codegen-openapi 0.0.38-alpha.3 → 0.0.38-alpha.4
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 +64 -61
- package/lib/bin/cli.mjs.map +1 -1
- package/lib/index.js +44 -8
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +44 -8
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/generators/rtk-enhance-endpoints-generator.ts +4 -1
- package/src/generators/rtk-query-generator.ts +1 -0
- package/src/generators/types-generator.ts +22 -8
- package/src/services/endpoint-info-extractor.ts +35 -0
|
@@ -20,6 +20,7 @@ export interface EndpointInfo {
|
|
|
20
20
|
pathParams: any[];
|
|
21
21
|
isVoidArg: boolean;
|
|
22
22
|
summary: string;
|
|
23
|
+
isBlobResponse?: boolean;
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
export function generateTypesFile(
|
|
@@ -50,12 +51,20 @@ export function generateTypesFile(
|
|
|
50
51
|
});
|
|
51
52
|
}
|
|
52
53
|
|
|
54
|
+
// 檢查是否有 blob response 端點
|
|
55
|
+
const hasBlobResponse = endpointInfos.some((info) => info.isBlobResponse);
|
|
56
|
+
|
|
53
57
|
// 生成 import 語句
|
|
54
58
|
let importStatement = `/* eslint-disable */
|
|
55
|
-
// [Warning] Generated automatically - do not edit manually
|
|
56
|
-
|
|
59
|
+
// [Warning] Generated automatically - do not edit manually
|
|
60
|
+
|
|
57
61
|
`;
|
|
58
62
|
|
|
63
|
+
// 如果有 blob response,加入 HttpResponse import
|
|
64
|
+
if (hasBlobResponse) {
|
|
65
|
+
importStatement += `import { HttpResponse } from '@angular/common/http';\n`;
|
|
66
|
+
}
|
|
67
|
+
|
|
59
68
|
// 檢查是否需要引入 schema.ts
|
|
60
69
|
const hasSchemaTypes = schemaInterfaces && Object.keys(schemaInterfaces).length > 0;
|
|
61
70
|
if (hasSchemaTypes) {
|
|
@@ -93,13 +102,18 @@ export function generateTypesFile(
|
|
|
93
102
|
|
|
94
103
|
// 生成 Response 類型(總是生成)
|
|
95
104
|
if (resTypeName) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
endpointTypes.push(`export type ${resTypeName} = void;`, ``);
|
|
105
|
+
if (endpoint.isBlobResponse) {
|
|
106
|
+
// Blob 下載端點:使用 HttpResponse<Blob>
|
|
107
|
+
endpointTypes.push(`export type ${resTypeName} = HttpResponse<Blob>;`, ``);
|
|
100
108
|
} else {
|
|
101
|
-
|
|
102
|
-
|
|
109
|
+
const responseTypeContent = generateResponseTypeContent(endpoint, operationDefinitions, schemaTypeMap);
|
|
110
|
+
if (responseTypeContent.trim() === '') {
|
|
111
|
+
// 如果沒有實際內容,使用 void
|
|
112
|
+
endpointTypes.push(`export type ${resTypeName} = void;`, ``);
|
|
113
|
+
} else {
|
|
114
|
+
// 有實際內容,使用 type 定義
|
|
115
|
+
endpointTypes.push(`export type ${resTypeName} = {`, responseTypeContent, `};`, ``);
|
|
116
|
+
}
|
|
103
117
|
}
|
|
104
118
|
}
|
|
105
119
|
});
|
|
@@ -21,6 +21,7 @@ export interface EndpointInfo {
|
|
|
21
21
|
contentType: string;
|
|
22
22
|
hasRequestBody: boolean;
|
|
23
23
|
tags: string[];
|
|
24
|
+
isBlobResponse: boolean;
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
/**
|
|
@@ -84,6 +85,9 @@ export class EndpointInfoExtractor {
|
|
|
84
85
|
// 提取 tags
|
|
85
86
|
const tags = Array.isArray(operation.tags) ? operation.tags : [];
|
|
86
87
|
|
|
88
|
+
// 偵測是否為 Blob 下載端點
|
|
89
|
+
const isBlobResponse = this.isBlobResponseEndpoint(operation);
|
|
90
|
+
|
|
87
91
|
return {
|
|
88
92
|
operationName: finalOperationName,
|
|
89
93
|
argTypeName,
|
|
@@ -99,6 +103,7 @@ export class EndpointInfoExtractor {
|
|
|
99
103
|
contentType,
|
|
100
104
|
hasRequestBody,
|
|
101
105
|
tags,
|
|
106
|
+
isBlobResponse,
|
|
102
107
|
};
|
|
103
108
|
}
|
|
104
109
|
|
|
@@ -144,6 +149,36 @@ export class EndpointInfoExtractor {
|
|
|
144
149
|
return Array.isArray(parameters) ? parameters : [parameters];
|
|
145
150
|
}
|
|
146
151
|
|
|
152
|
+
/**
|
|
153
|
+
* 偵測是否為 Blob 下載端點
|
|
154
|
+
* 當 response content type 為 binary 類型或 schema format 為 binary 時回傳 true
|
|
155
|
+
*/
|
|
156
|
+
private isBlobResponseEndpoint(operation: any): boolean {
|
|
157
|
+
if (!operation.responses) return false;
|
|
158
|
+
|
|
159
|
+
const successResponse = operation.responses['200'] || operation.responses['201'];
|
|
160
|
+
if (!successResponse?.content) return false;
|
|
161
|
+
|
|
162
|
+
const contentTypes = Object.keys(successResponse.content);
|
|
163
|
+
|
|
164
|
+
// 非 JSON/text 類型的 content type 視為 binary(如 application/zip, application/pdf, image/*, application/vnd.* 等)
|
|
165
|
+
const textContentTypes = ['application/json', 'text/', '*/*'];
|
|
166
|
+
const hasNonTextContentType = contentTypes.some(
|
|
167
|
+
(ct) => !textContentTypes.some((textCt) => ct === textCt || ct.startsWith(textCt))
|
|
168
|
+
);
|
|
169
|
+
if (hasNonTextContentType) return true;
|
|
170
|
+
|
|
171
|
+
// 檢查 schema 是否為 binary format
|
|
172
|
+
for (const ct of contentTypes) {
|
|
173
|
+
const schema = successResponse.content[ct]?.schema;
|
|
174
|
+
if (schema && schema.type === 'string' && schema.format === 'binary') {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
|
|
147
182
|
/**
|
|
148
183
|
* 提取操作的 content type
|
|
149
184
|
* @param operation - 操作對象
|