Skip to content

Commit

Permalink
Merge pull request #653 from johnteske/typescript-fetch
Browse files Browse the repository at this point in the history
Migrate typescript-fetch from v2 to v3
  • Loading branch information
HugoMario committed Apr 20, 2020
2 parents 632e0e7 + 3bbb02a commit c1ab184
Show file tree
Hide file tree
Showing 29 changed files with 1,754 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package io.swagger.codegen.v3.generators.typescript;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import io.swagger.codegen.v3.CliOption;
import io.swagger.codegen.v3.CodegenModel;
import io.swagger.codegen.v3.SupportingFile;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.BinarySchema;
import io.swagger.v3.oas.models.media.FileSchema;
import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.parser.util.SchemaTypeUtil;

import org.apache.commons.lang3.StringUtils;

public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodegen {
private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm");

public static final String NPM_NAME = "npmName";
public static final String NPM_VERSION = "npmVersion";
public static final String NPM_REPOSITORY = "npmRepository";
public static final String SNAPSHOT = "snapshot";
public static final String WITH_INTERFACES = "withInterfaces";

protected String npmName = null;
protected String npmVersion = "1.0.0";
protected String npmRepository = null;

public TypeScriptFetchClientCodegen() {
super();
this.outputFolder = "generated-code" + File.separator + "typescript-fetch";

this.cliOptions.add(new CliOption(NPM_NAME, "The name under which you want to publish generated npm package"));
this.cliOptions.add(new CliOption(NPM_VERSION, "The version of your npm package"));
this.cliOptions.add(new CliOption(NPM_REPOSITORY,
"Use this property to set an url your private npmRepo in the package.json"));
this.cliOptions.add(new CliOption(SNAPSHOT,
"When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm",
SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(WITH_INTERFACES,
"Setting this property to true will generate interfaces next to the default class implementations.",
SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
}

@Override
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) {
if (schema instanceof MapSchema && hasSchemaProperties(schema)) {
codegenModel.additionalPropertiesType = getTypeDeclaration((Schema) schema.getAdditionalProperties());
addImport(codegenModel, codegenModel.additionalPropertiesType);
} else if (schema instanceof MapSchema && hasTrueAdditionalProperties(schema)) {
codegenModel.additionalPropertiesType = getTypeDeclaration(new ObjectSchema());
}
}

@Override
public String getName() {
return "typescript-fetch";
}

@Override
public String getHelp() {
return "Generates a TypeScript client library using Fetch API (beta).";
}

@Override
public void processOpts() {
super.processOpts();

if (StringUtils.isBlank(templateDir)) {
embeddedTemplateDir = templateDir = getTemplateDir();
}

supportingFiles.add(new SupportingFile("index.mustache", "", "index.ts"));
supportingFiles.add(new SupportingFile("api.mustache", "", "api.ts"));
supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.ts"));
supportingFiles.add(new SupportingFile("custom.d.mustache", "", "custom.d.ts"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore"));

if (additionalProperties.containsKey(NPM_NAME)) {
addNpmPackageGeneration();
}
}

@Override
public String getDefaultTemplateDir() {
return "typescript-fetch";
}

@Override
public String getTypeDeclaration(Schema propertySchema) {
Schema inner;
if (propertySchema instanceof ArraySchema) {
ArraySchema arraySchema = (ArraySchema) propertySchema;
inner = arraySchema.getItems();
return this.getSchemaType(propertySchema) + "<" + this.getTypeDeclaration(inner) + ">";
} else if (propertySchema instanceof MapSchema && hasSchemaProperties(propertySchema)) {
inner = (Schema) propertySchema.getAdditionalProperties();
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
} else if (propertySchema instanceof MapSchema && hasTrueAdditionalProperties(propertySchema)) {
inner = new ObjectSchema();
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
} else if (propertySchema instanceof FileSchema || propertySchema instanceof BinarySchema) {
return "Blob";
} else if (propertySchema instanceof ObjectSchema) {
return "any";
} else {
return super.getTypeDeclaration(propertySchema);
}
}

@Override
public String getSchemaType(Schema schema) {
String swaggerType = super.getSchemaType(schema);
if (isLanguagePrimitive(swaggerType) || isLanguageGenericType(swaggerType)) {
return swaggerType;
}
applyLocalTypeMapping(swaggerType);
return swaggerType;
}

private String applyLocalTypeMapping(String type) {
if (typeMapping.containsKey(type)) {
type = typeMapping.get(type);
}
return type;
}

private boolean isLanguagePrimitive(String type) {
return languageSpecificPrimitives.contains(type);
}

private boolean isLanguageGenericType(String type) {
for (String genericType : languageGenericTypes) {
if (type.startsWith(genericType + "<")) {
return true;
}
}
return false;
}

private void addNpmPackageGeneration() {
if (additionalProperties.containsKey(NPM_NAME)) {
this.setNpmName(additionalProperties.get(NPM_NAME).toString());
}

if (additionalProperties.containsKey(NPM_VERSION)) {
this.setNpmVersion(additionalProperties.get(NPM_VERSION).toString());
}

if (additionalProperties.containsKey(SNAPSHOT)
&& Boolean.valueOf(additionalProperties.get(SNAPSHOT).toString())) {
this.setNpmVersion(npmVersion + "-SNAPSHOT." + SNAPSHOT_SUFFIX_FORMAT.format(new Date()));
}
additionalProperties.put(NPM_VERSION, npmVersion);

if (additionalProperties.containsKey(NPM_REPOSITORY)) {
this.setNpmRepository(additionalProperties.get(NPM_REPOSITORY).toString());
}

// Files for building our lib
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("package.mustache", "", "package.json"));
supportingFiles.add(new SupportingFile("tsconfig.mustache", "", "tsconfig.json"));
}

public String getNpmName() {
return npmName;
}

public void setNpmName(String npmName) {
this.npmName = npmName;
}

public String getNpmVersion() {
return npmVersion;
}

public void setNpmVersion(String npmVersion) {
this.npmVersion = npmVersion;
}

public String getNpmRepository() {
return npmRepository;
}

public void setNpmRepository(String npmRepository) {
this.npmRepository = npmRepository;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ io.swagger.codegen.v3.generators.swift.Swift3Codegen
io.swagger.codegen.v3.generators.swift.Swift4Codegen
io.swagger.codegen.v3.generators.swift.Swift5Codegen
io.swagger.codegen.v3.generators.typescript.TypeScriptAngularClientCodegen
io.swagger.codegen.v3.generators.typescript.TypeScriptFetchClientCodegen
io.swagger.codegen.v3.generators.javascript.JavaScriptClientCodegen
45 changes: 45 additions & 0 deletions src/main/resources/handlebars/typescript-fetch/README.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
## {{npmName}}@{{npmVersion}}

This generator creates TypeScript/JavaScript client that utilizes [Fetch API](https://fetch.spec.whatwg.org/). The generated Node module can be used in the following environments:

Environment
* Node.js
* Webpack
* Browserify

Language level
* ES5 - you must have a Promises/A+ library installed
* ES6

Module system
* CommonJS
* ES6 module system

It can be used in both TypeScript and JavaScript. In TypeScript, the definition should be automatically resolved via `package.json`. ([Reference](http:https://www.typescriptlang.org/docs/handbook/typings-for-npm-packages.html))

### Building

To build an compile the typescript sources to javascript use:
```
npm install
npm run build
```

### Publishing

First build the package then run ```npm publish```

### Consuming

navigate to the folder of your consuming project and run one of the following commands.

_published:_

```
npm install {{npmName}}@{{npmVersion}} --save
```

_unPublished (not recommended):_

```
npm install PATH_TO_GENERATED_PACKAGE --save
Loading

0 comments on commit c1ab184

Please sign in to comment.