|
| 1 | +package io.swagger.codegen.v3.generators.typescript; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.text.SimpleDateFormat; |
| 5 | +import java.util.Date; |
| 6 | + |
| 7 | +import io.swagger.codegen.v3.CliOption; |
| 8 | +import io.swagger.codegen.v3.CodegenModel; |
| 9 | +import io.swagger.codegen.v3.SupportingFile; |
| 10 | +import io.swagger.v3.oas.models.media.ArraySchema; |
| 11 | +import io.swagger.v3.oas.models.media.BinarySchema; |
| 12 | +import io.swagger.v3.oas.models.media.FileSchema; |
| 13 | +import io.swagger.v3.oas.models.media.MapSchema; |
| 14 | +import io.swagger.v3.oas.models.media.ObjectSchema; |
| 15 | +import io.swagger.v3.oas.models.media.Schema; |
| 16 | +import io.swagger.v3.parser.util.SchemaTypeUtil; |
| 17 | + |
| 18 | +import org.apache.commons.lang3.StringUtils; |
| 19 | + |
| 20 | +public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodegen { |
| 21 | + private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm"); |
| 22 | + |
| 23 | + public static final String NPM_NAME = "npmName"; |
| 24 | + public static final String NPM_VERSION = "npmVersion"; |
| 25 | + public static final String NPM_REPOSITORY = "npmRepository"; |
| 26 | + public static final String SNAPSHOT = "snapshot"; |
| 27 | + public static final String WITH_INTERFACES = "withInterfaces"; |
| 28 | + |
| 29 | + protected String npmName = null; |
| 30 | + protected String npmVersion = "1.0.0"; |
| 31 | + protected String npmRepository = null; |
| 32 | + |
| 33 | + public TypeScriptFetchClientCodegen() { |
| 34 | + super(); |
| 35 | + this.outputFolder = "generated-code" + File.separator + "typescript-fetch"; |
| 36 | + |
| 37 | + this.cliOptions.add(new CliOption(NPM_NAME, "The name under which you want to publish generated npm package")); |
| 38 | + this.cliOptions.add(new CliOption(NPM_VERSION, "The version of your npm package")); |
| 39 | + this.cliOptions.add(new CliOption(NPM_REPOSITORY, |
| 40 | + "Use this property to set an url your private npmRepo in the package.json")); |
| 41 | + this.cliOptions.add(new CliOption(SNAPSHOT, |
| 42 | + "When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm", |
| 43 | + SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString())); |
| 44 | + this.cliOptions.add(new CliOption(WITH_INTERFACES, |
| 45 | + "Setting this property to true will generate interfaces next to the default class implementations.", |
| 46 | + SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString())); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) { |
| 51 | + if (schema instanceof MapSchema && hasSchemaProperties(schema)) { |
| 52 | + codegenModel.additionalPropertiesType = getTypeDeclaration((Schema) schema.getAdditionalProperties()); |
| 53 | + addImport(codegenModel, codegenModel.additionalPropertiesType); |
| 54 | + } else if (schema instanceof MapSchema && hasTrueAdditionalProperties(schema)) { |
| 55 | + codegenModel.additionalPropertiesType = getTypeDeclaration(new ObjectSchema()); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public String getName() { |
| 61 | + return "typescript-fetch"; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public String getHelp() { |
| 66 | + return "Generates a TypeScript client library using Fetch API (beta)."; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void processOpts() { |
| 71 | + super.processOpts(); |
| 72 | + |
| 73 | + if (StringUtils.isBlank(templateDir)) { |
| 74 | + embeddedTemplateDir = templateDir = getTemplateDir(); |
| 75 | + } |
| 76 | + |
| 77 | + supportingFiles.add(new SupportingFile("index.mustache", "", "index.ts")); |
| 78 | + supportingFiles.add(new SupportingFile("api.mustache", "", "api.ts")); |
| 79 | + supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.ts")); |
| 80 | + supportingFiles.add(new SupportingFile("custom.d.mustache", "", "custom.d.ts")); |
| 81 | + supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh")); |
| 82 | + supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore")); |
| 83 | + |
| 84 | + if (additionalProperties.containsKey(NPM_NAME)) { |
| 85 | + addNpmPackageGeneration(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String getDefaultTemplateDir() { |
| 91 | + return "typescript-fetch"; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public String getTypeDeclaration(Schema propertySchema) { |
| 96 | + Schema inner; |
| 97 | + if (propertySchema instanceof ArraySchema) { |
| 98 | + ArraySchema arraySchema = (ArraySchema) propertySchema; |
| 99 | + inner = arraySchema.getItems(); |
| 100 | + return this.getSchemaType(propertySchema) + "<" + this.getTypeDeclaration(inner) + ">"; |
| 101 | + } else if (propertySchema instanceof MapSchema && hasSchemaProperties(propertySchema)) { |
| 102 | + inner = (Schema) propertySchema.getAdditionalProperties(); |
| 103 | + return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }"; |
| 104 | + } else if (propertySchema instanceof MapSchema && hasTrueAdditionalProperties(propertySchema)) { |
| 105 | + inner = new ObjectSchema(); |
| 106 | + return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }"; |
| 107 | + } else if (propertySchema instanceof FileSchema || propertySchema instanceof BinarySchema) { |
| 108 | + return "Blob"; |
| 109 | + } else if (propertySchema instanceof ObjectSchema) { |
| 110 | + return "any"; |
| 111 | + } else { |
| 112 | + return super.getTypeDeclaration(propertySchema); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public String getSchemaType(Schema schema) { |
| 118 | + String swaggerType = super.getSchemaType(schema); |
| 119 | + if (isLanguagePrimitive(swaggerType) || isLanguageGenericType(swaggerType)) { |
| 120 | + return swaggerType; |
| 121 | + } |
| 122 | + applyLocalTypeMapping(swaggerType); |
| 123 | + return swaggerType; |
| 124 | + } |
| 125 | + |
| 126 | + private String applyLocalTypeMapping(String type) { |
| 127 | + if (typeMapping.containsKey(type)) { |
| 128 | + type = typeMapping.get(type); |
| 129 | + } |
| 130 | + return type; |
| 131 | + } |
| 132 | + |
| 133 | + private boolean isLanguagePrimitive(String type) { |
| 134 | + return languageSpecificPrimitives.contains(type); |
| 135 | + } |
| 136 | + |
| 137 | + private boolean isLanguageGenericType(String type) { |
| 138 | + for (String genericType : languageGenericTypes) { |
| 139 | + if (type.startsWith(genericType + "<")) { |
| 140 | + return true; |
| 141 | + } |
| 142 | + } |
| 143 | + return false; |
| 144 | + } |
| 145 | + |
| 146 | + private void addNpmPackageGeneration() { |
| 147 | + if (additionalProperties.containsKey(NPM_NAME)) { |
| 148 | + this.setNpmName(additionalProperties.get(NPM_NAME).toString()); |
| 149 | + } |
| 150 | + |
| 151 | + if (additionalProperties.containsKey(NPM_VERSION)) { |
| 152 | + this.setNpmVersion(additionalProperties.get(NPM_VERSION).toString()); |
| 153 | + } |
| 154 | + |
| 155 | + if (additionalProperties.containsKey(SNAPSHOT) |
| 156 | + && Boolean.valueOf(additionalProperties.get(SNAPSHOT).toString())) { |
| 157 | + this.setNpmVersion(npmVersion + "-SNAPSHOT." + SNAPSHOT_SUFFIX_FORMAT.format(new Date())); |
| 158 | + } |
| 159 | + additionalProperties.put(NPM_VERSION, npmVersion); |
| 160 | + |
| 161 | + if (additionalProperties.containsKey(NPM_REPOSITORY)) { |
| 162 | + this.setNpmRepository(additionalProperties.get(NPM_REPOSITORY).toString()); |
| 163 | + } |
| 164 | + |
| 165 | + // Files for building our lib |
| 166 | + supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); |
| 167 | + supportingFiles.add(new SupportingFile("package.mustache", "", "package.json")); |
| 168 | + supportingFiles.add(new SupportingFile("tsconfig.mustache", "", "tsconfig.json")); |
| 169 | + } |
| 170 | + |
| 171 | + public String getNpmName() { |
| 172 | + return npmName; |
| 173 | + } |
| 174 | + |
| 175 | + public void setNpmName(String npmName) { |
| 176 | + this.npmName = npmName; |
| 177 | + } |
| 178 | + |
| 179 | + public String getNpmVersion() { |
| 180 | + return npmVersion; |
| 181 | + } |
| 182 | + |
| 183 | + public void setNpmVersion(String npmVersion) { |
| 184 | + this.npmVersion = npmVersion; |
| 185 | + } |
| 186 | + |
| 187 | + public String getNpmRepository() { |
| 188 | + return npmRepository; |
| 189 | + } |
| 190 | + |
| 191 | + public void setNpmRepository(String npmRepository) { |
| 192 | + this.npmRepository = npmRepository; |
| 193 | + } |
| 194 | + |
| 195 | +} |
0 commit comments