@acrool/rtk-query-codegen-openapi 1.1.0 → 1.1.2
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 +26 -26
- package/lib/bin/cli.mjs.map +1 -1
- package/lib/index.d.mts +9 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +42 -17
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +42 -17
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/generators/rtk-enhance-endpoints-generator.ts +13 -5
- package/src/generators/rtk-query-generator.ts +3 -3
- package/src/generators/types-generator.ts +75 -43
- package/src/types.ts +27 -18
package/lib/index.js
CHANGED
|
@@ -767,7 +767,7 @@ function generateRequestTypeContent(endpoint, operationDefinitions, schemaTypeMa
|
|
|
767
767
|
if (operationDef?.operation?.requestBody) {
|
|
768
768
|
const requestBody = operationDef.operation.requestBody;
|
|
769
769
|
const content = requestBody.content;
|
|
770
|
-
const jsonContent = content["application/json"];
|
|
770
|
+
const jsonContent = content["application/json"] || content["*/*"];
|
|
771
771
|
const formContent = content["multipart/form-data"] || content["application/x-www-form-urlencoded"];
|
|
772
772
|
if (jsonContent?.schema) {
|
|
773
773
|
const bodyType = getTypeFromSchema(jsonContent.schema, schemaTypeMap, 1);
|
|
@@ -776,7 +776,13 @@ function generateRequestTypeContent(endpoint, operationDefinitions, schemaTypeMa
|
|
|
776
776
|
const bodyType = getTypeFromSchema(formContent.schema, schemaTypeMap, 1);
|
|
777
777
|
properties.push(` body: ${bodyType};`);
|
|
778
778
|
} else {
|
|
779
|
-
|
|
779
|
+
const firstContent = Object.values(content)[0];
|
|
780
|
+
if (firstContent?.schema) {
|
|
781
|
+
const bodyType = getTypeFromSchema(firstContent.schema, schemaTypeMap, 1);
|
|
782
|
+
properties.push(` body: ${bodyType};`);
|
|
783
|
+
} else {
|
|
784
|
+
properties.push(` body?: any; // Request body from OpenAPI`);
|
|
785
|
+
}
|
|
780
786
|
}
|
|
781
787
|
}
|
|
782
788
|
if (properties.length === 0) {
|
|
@@ -793,7 +799,7 @@ function generateResponseTypeContent(endpoint, operationDefinitions, schemaTypeM
|
|
|
793
799
|
if (operationDef?.operation?.responses) {
|
|
794
800
|
const successResponse = operationDef.operation.responses["200"] || operationDef.operation.responses["201"];
|
|
795
801
|
if (successResponse?.content) {
|
|
796
|
-
const jsonContent = successResponse.content["application/json"];
|
|
802
|
+
const jsonContent = successResponse.content["application/json"] || successResponse.content["*/*"] || Object.values(successResponse.content)[0];
|
|
797
803
|
if (jsonContent?.schema) {
|
|
798
804
|
const responseProps = parseSchemaProperties(jsonContent.schema, schemaTypeMap);
|
|
799
805
|
properties.push(...responseProps);
|
|
@@ -816,10 +822,12 @@ function parseSchemaProperties(schema, schemaTypeMap = {}) {
|
|
|
816
822
|
const isRequired = required.includes(propName);
|
|
817
823
|
const optional = isRequired ? "" : "?";
|
|
818
824
|
const propType = getTypeFromSchema(propSchema, schemaTypeMap, 1);
|
|
819
|
-
const description = propSchema.description ? ` // ${propSchema.description}` : "";
|
|
820
825
|
const needsQuotes = /[^a-zA-Z0-9_$]/.test(propName);
|
|
821
826
|
const quotedPropName = needsQuotes ? `"${propName}"` : propName;
|
|
822
|
-
|
|
827
|
+
if (propSchema.description) {
|
|
828
|
+
properties.push(` /** ${propSchema.description} */`);
|
|
829
|
+
}
|
|
830
|
+
properties.push(` ${quotedPropName}${optional}: ${propType};`);
|
|
823
831
|
});
|
|
824
832
|
}
|
|
825
833
|
return properties;
|
|
@@ -863,22 +871,34 @@ function getTypeFromSchema(schema, schemaTypeMap = {}, indentLevel = 0) {
|
|
|
863
871
|
if (schema.properties) {
|
|
864
872
|
const entries = Object.entries(schema.properties);
|
|
865
873
|
if (entries.length === 0) {
|
|
866
|
-
|
|
874
|
+
if (schema.additionalProperties) {
|
|
875
|
+
const valueType = schema.additionalProperties === true ? "any" : getTypeFromSchema(schema.additionalProperties, schemaTypeMap, indentLevel);
|
|
876
|
+
baseType = `Record<string, ${valueType}>`;
|
|
877
|
+
} else {
|
|
878
|
+
baseType = "{}";
|
|
879
|
+
}
|
|
867
880
|
} else {
|
|
868
881
|
const nextIndent = " ".repeat(indentLevel + 1);
|
|
869
882
|
const currentIndent = " ".repeat(indentLevel);
|
|
870
|
-
const props =
|
|
883
|
+
const props = [];
|
|
884
|
+
entries.forEach(([key, propSchema]) => {
|
|
871
885
|
const required = schema.required || [];
|
|
872
886
|
const optional = required.includes(key) ? "" : "?";
|
|
873
887
|
const type = getTypeFromSchema(propSchema, schemaTypeMap, indentLevel + 1);
|
|
874
888
|
const needsQuotes = /[^a-zA-Z0-9_$]/.test(key);
|
|
875
889
|
const quotedKey = needsQuotes ? `"${key}"` : key;
|
|
876
|
-
|
|
877
|
-
|
|
890
|
+
if (propSchema.description) {
|
|
891
|
+
props.push(`${nextIndent}/** ${propSchema.description} */`);
|
|
892
|
+
}
|
|
893
|
+
props.push(`${nextIndent}${quotedKey}${optional}: ${type};`);
|
|
894
|
+
});
|
|
878
895
|
baseType = `{
|
|
879
|
-
${props}
|
|
896
|
+
${props.join("\n")}
|
|
880
897
|
${currentIndent}}`;
|
|
881
898
|
}
|
|
899
|
+
} else if (schema.additionalProperties) {
|
|
900
|
+
const valueType = schema.additionalProperties === true ? "any" : getTypeFromSchema(schema.additionalProperties, schemaTypeMap, indentLevel);
|
|
901
|
+
baseType = `Record<string, ${valueType}>`;
|
|
882
902
|
} else {
|
|
883
903
|
baseType = "any";
|
|
884
904
|
}
|
|
@@ -942,10 +962,10 @@ ${paramsLines}
|
|
|
942
962
|
])
|
|
943
963
|
].filter((type) => type !== "VoidApiArg");
|
|
944
964
|
const typeImportStatement = typeImports.length > 0 ? `import type { ${typeImports.join(", ")} } from "./types";` : "";
|
|
945
|
-
const apiImport = options.apiConfiguration ? `import {${options.apiConfiguration.importName} as api
|
|
965
|
+
const apiImport = options.apiConfiguration ? `import {${options.apiConfiguration.importName} as api} from "${options.apiConfiguration.file.replace(/\.ts$/, "")}";
|
|
946
966
|
` : `import {baseApi as api} from "../../../library/redux/baseApi";
|
|
947
967
|
`;
|
|
948
|
-
const httpClientImport = options.httpClient ? `import {${options.httpClient.importReturnTypeName
|
|
968
|
+
const httpClientImport = options.httpClient ? `import {${options.httpClient.importReturnTypeName}} from "${options.httpClient.file}";
|
|
949
969
|
` : `import {IRestFulEndpointsQueryReturn} from "@acrool/react-fetcher";
|
|
950
970
|
`;
|
|
951
971
|
return `/* eslint-disable */
|
|
@@ -987,7 +1007,7 @@ export default injectedRtkApi;
|
|
|
987
1007
|
// src/generators/rtk-enhance-endpoints-generator.ts
|
|
988
1008
|
init_cjs_shims();
|
|
989
1009
|
function generateRtkEnhanceEndpointsFile(endpointInfos, options) {
|
|
990
|
-
const { groupKey } = options;
|
|
1010
|
+
const { groupKey, cacheTagTypes } = options;
|
|
991
1011
|
const endpointConfigs = endpointInfos.map((info) => {
|
|
992
1012
|
if (info.isQuery) {
|
|
993
1013
|
return ` ${info.operationName}: {
|
|
@@ -999,12 +1019,17 @@ function generateRtkEnhanceEndpointsFile(endpointInfos, options) {
|
|
|
999
1019
|
},`;
|
|
1000
1020
|
}
|
|
1001
1021
|
}).join("\n");
|
|
1002
|
-
|
|
1022
|
+
let importStatements = `/* eslint-disable */
|
|
1003
1023
|
// [Warning] Generated automatically - do not edit manually
|
|
1004
1024
|
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1025
|
+
`;
|
|
1026
|
+
if (cacheTagTypes) {
|
|
1027
|
+
importStatements += `import { ${cacheTagTypes.importReturnTypeName} } from "${cacheTagTypes.file}";
|
|
1028
|
+
`;
|
|
1029
|
+
}
|
|
1030
|
+
importStatements += `import api from "./query.generated";
|
|
1031
|
+
`;
|
|
1032
|
+
return `${importStatements}
|
|
1008
1033
|
const enhancedApi = api.enhanceEndpoints({
|
|
1009
1034
|
endpoints: {
|
|
1010
1035
|
${endpointConfigs}
|