@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
package/lib/index.mjs
CHANGED
|
@@ -604,6 +604,7 @@ var EndpointInfoExtractor = class {
|
|
|
604
604
|
const { queryParams, pathParams, isVoidArg, hasRequestBody } = this.extractParameters(operationDefinition);
|
|
605
605
|
const contentType = this.extractContentType(operation);
|
|
606
606
|
const tags = Array.isArray(operation.tags) ? operation.tags : [];
|
|
607
|
+
const isBlobResponse = this.isBlobResponseEndpoint(operation);
|
|
607
608
|
return {
|
|
608
609
|
operationName: finalOperationName,
|
|
609
610
|
argTypeName,
|
|
@@ -618,7 +619,8 @@ var EndpointInfoExtractor = class {
|
|
|
618
619
|
summary,
|
|
619
620
|
contentType,
|
|
620
621
|
hasRequestBody,
|
|
621
|
-
tags
|
|
622
|
+
tags,
|
|
623
|
+
isBlobResponse
|
|
622
624
|
};
|
|
623
625
|
}
|
|
624
626
|
/**
|
|
@@ -652,6 +654,28 @@ var EndpointInfoExtractor = class {
|
|
|
652
654
|
if (!parameters) return [];
|
|
653
655
|
return Array.isArray(parameters) ? parameters : [parameters];
|
|
654
656
|
}
|
|
657
|
+
/**
|
|
658
|
+
* 偵測是否為 Blob 下載端點
|
|
659
|
+
* 當 response content type 為 binary 類型或 schema format 為 binary 時回傳 true
|
|
660
|
+
*/
|
|
661
|
+
isBlobResponseEndpoint(operation) {
|
|
662
|
+
if (!operation.responses) return false;
|
|
663
|
+
const successResponse = operation.responses["200"] || operation.responses["201"];
|
|
664
|
+
if (!successResponse?.content) return false;
|
|
665
|
+
const contentTypes = Object.keys(successResponse.content);
|
|
666
|
+
const textContentTypes = ["application/json", "text/", "*/*"];
|
|
667
|
+
const hasNonTextContentType = contentTypes.some(
|
|
668
|
+
(ct) => !textContentTypes.some((textCt) => ct === textCt || ct.startsWith(textCt))
|
|
669
|
+
);
|
|
670
|
+
if (hasNonTextContentType) return true;
|
|
671
|
+
for (const ct of contentTypes) {
|
|
672
|
+
const schema = successResponse.content[ct]?.schema;
|
|
673
|
+
if (schema && schema.type === "string" && schema.format === "binary") {
|
|
674
|
+
return true;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
return false;
|
|
678
|
+
}
|
|
655
679
|
/**
|
|
656
680
|
* 提取操作的 content type
|
|
657
681
|
* @param operation - 操作對象
|
|
@@ -692,10 +716,15 @@ function generateTypesFile(endpointInfos, _options, schemaInterfaces, operationD
|
|
|
692
716
|
}
|
|
693
717
|
});
|
|
694
718
|
}
|
|
719
|
+
const hasBlobResponse = endpointInfos.some((info) => info.isBlobResponse);
|
|
695
720
|
let importStatement = `/* eslint-disable */
|
|
696
|
-
// [Warning] Generated automatically - do not edit manually
|
|
697
|
-
|
|
721
|
+
// [Warning] Generated automatically - do not edit manually
|
|
722
|
+
|
|
723
|
+
`;
|
|
724
|
+
if (hasBlobResponse) {
|
|
725
|
+
importStatement += `import { HttpResponse } from '@angular/common/http';
|
|
698
726
|
`;
|
|
727
|
+
}
|
|
699
728
|
const hasSchemaTypes = schemaInterfaces && Object.keys(schemaInterfaces).length > 0;
|
|
700
729
|
if (hasSchemaTypes) {
|
|
701
730
|
importStatement += `import * as Schema from "../schema";
|
|
@@ -716,11 +745,15 @@ function generateTypesFile(endpointInfos, _options, schemaInterfaces, operationD
|
|
|
716
745
|
}
|
|
717
746
|
}
|
|
718
747
|
if (resTypeName) {
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
endpointTypes.push(`export type ${resTypeName} = void;`, ``);
|
|
748
|
+
if (endpoint.isBlobResponse) {
|
|
749
|
+
endpointTypes.push(`export type ${resTypeName} = HttpResponse<Blob>;`, ``);
|
|
722
750
|
} else {
|
|
723
|
-
|
|
751
|
+
const responseTypeContent = generateResponseTypeContent(endpoint, operationDefinitions, schemaTypeMap);
|
|
752
|
+
if (responseTypeContent.trim() === "") {
|
|
753
|
+
endpointTypes.push(`export type ${resTypeName} = void;`, ``);
|
|
754
|
+
} else {
|
|
755
|
+
endpointTypes.push(`export type ${resTypeName} = {`, responseTypeContent, `};`, ``);
|
|
756
|
+
}
|
|
724
757
|
}
|
|
725
758
|
}
|
|
726
759
|
});
|
|
@@ -1035,10 +1068,13 @@ ${endpointInfos.map((info) => {
|
|
|
1035
1068
|
const paramsLines = info.queryParams.map((param) => `${param.name}: args.variables.${param.name},`).join("\n");
|
|
1036
1069
|
paramsSection = `params: {${paramsLines}},`;
|
|
1037
1070
|
}
|
|
1071
|
+
const blobOptions = info.isBlobResponse ? `
|
|
1072
|
+
responseType: 'blob',
|
|
1073
|
+
observe: 'response',` : "";
|
|
1038
1074
|
return `{
|
|
1039
1075
|
...args?.fetchOptions,
|
|
1040
1076
|
headers: { 'Content-Type': '${info.contentType}', ...args?.fetchOptions?.headers },
|
|
1041
|
-
${paramsSection}${info.hasRequestBody ? `body: args.variables.body,` : ""}
|
|
1077
|
+
${paramsSection}${info.hasRequestBody ? `body: args.variables.body,` : ""}${blobOptions}
|
|
1042
1078
|
}`;
|
|
1043
1079
|
};
|
|
1044
1080
|
return `
|