diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java index 5521d72e7b36c4413a534b96f27d74e1bec9473d..f663409561046bf0724f0ea57bc5d62093df9518 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java @@ -36,6 +36,9 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege protected String npmRepository = null; private boolean useSingleRequestParameter = true; + protected boolean addedApiIndex = false; + protected boolean addedModelIndex = false; + public TypeScriptFetchClientCodegen() { super(); @@ -83,8 +86,6 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege additionalProperties.put("modelPropertyNaming", getModelPropertyNaming()); supportingFiles.add(new SupportingFile("index.mustache", "", "index.ts")); supportingFiles.add(new SupportingFile("runtime.mustache", "", "runtime.ts")); - supportingFiles.add(new SupportingFile("apis.index.mustache", apiPackage().replace('.', File.separatorChar), "index.ts")); - supportingFiles.add(new SupportingFile("models.index.mustache", modelPackage().replace('.', File.separatorChar), "index.ts")); supportingFiles.add(new SupportingFile("tsconfig.mustache", "", "tsconfig.json")); supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore")); @@ -127,8 +128,9 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege @Override public Map<String, Object> postProcessModels(Map<String, Object> objs) { - // process enum in models List<Object> models = (List<Object>) postProcessModelsEnum(objs).get("models"); + + // process enum in models for (Object _mo : models) { Map<String, Object> mo = (Map<String, Object>) _mo; CodegenModel cm = (CodegenModel) mo.get("model"); @@ -190,8 +192,21 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege @Override public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> operations, List<Object> allModels) { + // Add supporting file only if we plan to generate files in /apis + if (operations.size() > 0 && !addedApiIndex) { + addedApiIndex = true; + supportingFiles.add(new SupportingFile("apis.index.mustache", apiPackage().replace('.', File.separatorChar), "index.ts")); + } + + // Add supporting file only if we plan to generate files in /models + if (allModels.size() > 0 && !addedModelIndex) { + addedModelIndex = true; + supportingFiles.add(new SupportingFile("models.index.mustache", modelPackage().replace('.', File.separatorChar), "index.ts")); + } + this.addOperationModelImportInfomation(operations); this.updateOperationParameterEnumInformation(operations); + this.addOperationObjectResponseInformation(operations); return operations; } @@ -224,6 +239,20 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege operations.put("hasEnums", hasEnum); } + private void addOperationObjectResponseInformation(Map<String, Object> operations) { + // This method will modify the infomation on the operations' return type. + // The api template uses this infomation to know when to return a text + // response for a given simple response operation. + Map<String, Object> _operations = (Map<String, Object>) operations.get("operations"); + List<CodegenOperation> operationList = (List<CodegenOperation>) _operations.get("operation"); + for (CodegenOperation op : operationList) { + if(op.returnType == "object") { + op.isMapContainer = true; + op.returnSimpleType = false; + } + } + } + private void addExtraReservedWords() { this.reservedWords.add("BASE_PATH"); this.reservedWords.add("BaseAPI"); diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/index.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/index.mustache index 848ecfa4d100d22f5b444ab2a0e8fc7bdec067b4..e548c9df6b2c7b1d1048f1e37b978b64db4260f4 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/index.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/index.mustache @@ -1,3 +1,9 @@ export * from './runtime'; +{{#apiInfo}} +{{#apis.0}} export * from './apis'; +{{/apis.0}} +{{/apiInfo}} +{{#models.0}} export * from './models'; +{{/models.0}} \ No newline at end of file diff --git a/samples/client/petstore/typescript-fetch/builds/multiple-parameters/runtime.ts b/samples/client/petstore/typescript-fetch/builds/multiple-parameters/runtime.ts index 5b07f81118625f40e86b47d8825df3a83c38f6db..c95a101fbac7c4f706b22e869a9b9410636936a3 100644 --- a/samples/client/petstore/typescript-fetch/builds/multiple-parameters/runtime.ts +++ b/samples/client/petstore/typescript-fetch/builds/multiple-parameters/runtime.ts @@ -58,7 +58,7 @@ export class BaseAPI { // only add the querystring to the URL if there are query parameters. // this is done to avoid urls ending with a "?" character which buggy webservers // do not handle correctly sometimes. - url += '?' + querystring(context.query); + url += '?' + this.configuration.queryParamsStringify(context.query); } const body = (context.body instanceof FormData || isBlob(context.body)) ? context.body @@ -127,6 +127,7 @@ export interface ConfigurationParameters { basePath?: string; // override base path fetchApi?: FetchAPI; // override for fetch implementation middleware?: Middleware[]; // middleware to apply before/after fetch requests + queryParamsStringify?: (params: HTTPQuery) => string; // stringify function for query strings username?: string; // parameter for basic security password?: string; // parameter for basic security apiKey?: string | ((name: string) => string); // parameter for apiKey security @@ -148,6 +149,10 @@ export class Configuration { return this.configuration.middleware || []; } + get queryParamsStringify(): (params: HTTPQuery) => string { + return this.configuration.queryParamsStringify || querystring; + } + get username(): string | undefined { return this.configuration.username; }