@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/index.mjs CHANGED
@@ -752,7 +752,7 @@ function generateRequestTypeContent(endpoint, operationDefinitions, schemaTypeMa
752
752
  if (operationDef?.operation?.requestBody) {
753
753
  const requestBody = operationDef.operation.requestBody;
754
754
  const content = requestBody.content;
755
- const jsonContent = content["application/json"];
755
+ const jsonContent = content["application/json"] || content["*/*"];
756
756
  const formContent = content["multipart/form-data"] || content["application/x-www-form-urlencoded"];
757
757
  if (jsonContent?.schema) {
758
758
  const bodyType = getTypeFromSchema(jsonContent.schema, schemaTypeMap, 1);
@@ -761,7 +761,13 @@ function generateRequestTypeContent(endpoint, operationDefinitions, schemaTypeMa
761
761
  const bodyType = getTypeFromSchema(formContent.schema, schemaTypeMap, 1);
762
762
  properties.push(` body: ${bodyType};`);
763
763
  } else {
764
- properties.push(` body?: any; // Request body from OpenAPI`);
764
+ const firstContent = Object.values(content)[0];
765
+ if (firstContent?.schema) {
766
+ const bodyType = getTypeFromSchema(firstContent.schema, schemaTypeMap, 1);
767
+ properties.push(` body: ${bodyType};`);
768
+ } else {
769
+ properties.push(` body?: any; // Request body from OpenAPI`);
770
+ }
765
771
  }
766
772
  }
767
773
  if (properties.length === 0) {
@@ -778,7 +784,7 @@ function generateResponseTypeContent(endpoint, operationDefinitions, schemaTypeM
778
784
  if (operationDef?.operation?.responses) {
779
785
  const successResponse = operationDef.operation.responses["200"] || operationDef.operation.responses["201"];
780
786
  if (successResponse?.content) {
781
- const jsonContent = successResponse.content["application/json"];
787
+ const jsonContent = successResponse.content["application/json"] || successResponse.content["*/*"] || Object.values(successResponse.content)[0];
782
788
  if (jsonContent?.schema) {
783
789
  const responseProps = parseSchemaProperties(jsonContent.schema, schemaTypeMap);
784
790
  properties.push(...responseProps);
@@ -801,10 +807,12 @@ function parseSchemaProperties(schema, schemaTypeMap = {}) {
801
807
  const isRequired = required.includes(propName);
802
808
  const optional = isRequired ? "" : "?";
803
809
  const propType = getTypeFromSchema(propSchema, schemaTypeMap, 1);
804
- const description = propSchema.description ? ` // ${propSchema.description}` : "";
805
810
  const needsQuotes = /[^a-zA-Z0-9_$]/.test(propName);
806
811
  const quotedPropName = needsQuotes ? `"${propName}"` : propName;
807
- properties.push(` ${quotedPropName}${optional}: ${propType};${description}`);
812
+ if (propSchema.description) {
813
+ properties.push(` /** ${propSchema.description} */`);
814
+ }
815
+ properties.push(` ${quotedPropName}${optional}: ${propType};`);
808
816
  });
809
817
  }
810
818
  return properties;
@@ -848,22 +856,34 @@ function getTypeFromSchema(schema, schemaTypeMap = {}, indentLevel = 0) {
848
856
  if (schema.properties) {
849
857
  const entries = Object.entries(schema.properties);
850
858
  if (entries.length === 0) {
851
- baseType = "{}";
859
+ if (schema.additionalProperties) {
860
+ const valueType = schema.additionalProperties === true ? "any" : getTypeFromSchema(schema.additionalProperties, schemaTypeMap, indentLevel);
861
+ baseType = `Record<string, ${valueType}>`;
862
+ } else {
863
+ baseType = "{}";
864
+ }
852
865
  } else {
853
866
  const nextIndent = " ".repeat(indentLevel + 1);
854
867
  const currentIndent = " ".repeat(indentLevel);
855
- const props = entries.map(([key, propSchema]) => {
868
+ const props = [];
869
+ entries.forEach(([key, propSchema]) => {
856
870
  const required = schema.required || [];
857
871
  const optional = required.includes(key) ? "" : "?";
858
872
  const type = getTypeFromSchema(propSchema, schemaTypeMap, indentLevel + 1);
859
873
  const needsQuotes = /[^a-zA-Z0-9_$]/.test(key);
860
874
  const quotedKey = needsQuotes ? `"${key}"` : key;
861
- return `${nextIndent}${quotedKey}${optional}: ${type};`;
862
- }).join("\n");
875
+ if (propSchema.description) {
876
+ props.push(`${nextIndent}/** ${propSchema.description} */`);
877
+ }
878
+ props.push(`${nextIndent}${quotedKey}${optional}: ${type};`);
879
+ });
863
880
  baseType = `{
864
- ${props}
881
+ ${props.join("\n")}
865
882
  ${currentIndent}}`;
866
883
  }
884
+ } else if (schema.additionalProperties) {
885
+ const valueType = schema.additionalProperties === true ? "any" : getTypeFromSchema(schema.additionalProperties, schemaTypeMap, indentLevel);
886
+ baseType = `Record<string, ${valueType}>`;
867
887
  } else {
868
888
  baseType = "any";
869
889
  }
@@ -927,10 +947,10 @@ ${paramsLines}
927
947
  ])
928
948
  ].filter((type) => type !== "VoidApiArg");
929
949
  const typeImportStatement = typeImports.length > 0 ? `import type { ${typeImports.join(", ")} } from "./types";` : "";
930
- const apiImport = options.apiConfiguration ? `import {${options.apiConfiguration.importName} as api } from "${options.apiConfiguration.file.replace(/\.ts$/, "")}";
950
+ const apiImport = options.apiConfiguration ? `import {${options.apiConfiguration.importName} as api} from "${options.apiConfiguration.file.replace(/\.ts$/, "")}";
931
951
  ` : `import {baseApi as api} from "../../../library/redux/baseApi";
932
952
  `;
933
- const httpClientImport = options.httpClient ? `import {${options.httpClient.importReturnTypeName || options.httpClient.importName}} from "${options.httpClient.file}";
953
+ const httpClientImport = options.httpClient ? `import {${options.httpClient.importReturnTypeName}} from "${options.httpClient.file}";
934
954
  ` : `import {IRestFulEndpointsQueryReturn} from "@acrool/react-fetcher";
935
955
  `;
936
956
  return `/* eslint-disable */
@@ -972,7 +992,7 @@ export default injectedRtkApi;
972
992
  // src/generators/rtk-enhance-endpoints-generator.ts
973
993
  init_esm_shims();
974
994
  function generateRtkEnhanceEndpointsFile(endpointInfos, options) {
975
- const { groupKey } = options;
995
+ const { groupKey, cacheTagTypes } = options;
976
996
  const endpointConfigs = endpointInfos.map((info) => {
977
997
  if (info.isQuery) {
978
998
  return ` ${info.operationName}: {
@@ -984,12 +1004,17 @@ function generateRtkEnhanceEndpointsFile(endpointInfos, options) {
984
1004
  },`;
985
1005
  }
986
1006
  }).join("\n");
987
- return `/* eslint-disable */
1007
+ let importStatements = `/* eslint-disable */
988
1008
  // [Warning] Generated automatically - do not edit manually
989
1009
 
990
- import { ECacheTagTypes } from "@/store/tagTypes";
991
- import api from "./query.generated";
992
-
1010
+ `;
1011
+ if (cacheTagTypes) {
1012
+ importStatements += `import { ${cacheTagTypes.importReturnTypeName} } from "${cacheTagTypes.file}";
1013
+ `;
1014
+ }
1015
+ importStatements += `import api from "./query.generated";
1016
+ `;
1017
+ return `${importStatements}
993
1018
  const enhancedApi = api.enhanceEndpoints({
994
1019
  endpoints: {
995
1020
  ${endpointConfigs}