Skip to content

Commit 916f493

Browse files
committed
Migrate typescript-fetch v2 to v3
1 parent 1149e58 commit 916f493

File tree

14 files changed

+847
-0
lines changed

14 files changed

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

src/main/resources/META-INF/services/io.swagger.codegen.v3.CodegenConfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ io.swagger.codegen.v3.generators.swift.Swift3Codegen
3333
io.swagger.codegen.v3.generators.swift.Swift4Codegen
3434
io.swagger.codegen.v3.generators.swift.Swift5Codegen
3535
io.swagger.codegen.v3.generators.typescript.TypeScriptAngularClientCodegen
36+
io.swagger.codegen.v3.generators.typescript.TypeScriptFetchClientCodegen
3637
io.swagger.codegen.v3.generators.javascript.JavaScriptClientCodegen
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## {{npmName}}@{{npmVersion}}
2+
3+
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:
4+
5+
Environment
6+
* Node.js
7+
* Webpack
8+
* Browserify
9+
10+
Language level
11+
* ES5 - you must have a Promises/A+ library installed
12+
* ES6
13+
14+
Module system
15+
* CommonJS
16+
* ES6 module system
17+
18+
It can be used in both TypeScript and JavaScript. In TypeScript, the definition should be automatically resolved via `package.json`. ([Reference](http://www.typescriptlang.org/docs/handbook/typings-for-npm-packages.html))
19+
20+
### Building
21+
22+
To build an compile the typescript sources to javascript use:
23+
```
24+
npm install
25+
npm run build
26+
```
27+
28+
### Publishing
29+
30+
First build the package then run ```npm publish```
31+
32+
### Consuming
33+
34+
navigate to the folder of your consuming project and run one of the following commands.
35+
36+
_published:_
37+
38+
```
39+
npm install {{npmName}}@{{npmVersion}} --save
40+
```
41+
42+
_unPublished (not recommended):_
43+
44+
```
45+
npm install PATH_TO_GENERATED_PACKAGE --save

0 commit comments

Comments
 (0)