Skip to content

Commit 82f7664

Browse files
committed
copy new javascript flowtyped generator from swagger-api/swagger-codegen#8101
1 parent ad5d5f5 commit 82f7664

File tree

12 files changed

+733
-0
lines changed

12 files changed

+733
-0
lines changed
Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
package org.openapitools.codegen.languages;
2+
3+
import org.openapitools.codegen.*;
4+
import io.swagger.models.Info;
5+
import io.swagger.models.ModelImpl;
6+
import io.swagger.models.Swagger;
7+
import io.swagger.models.properties.*;
8+
import org.apache.commons.lang3.StringUtils;
9+
10+
import java.text.SimpleDateFormat;
11+
import java.util.*;
12+
13+
public class JavascriptFlowtypedCodegen extends AbstractTypeScriptClientCodegen {
14+
private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm");
15+
16+
public static final String NPM_NAME = "npmName";
17+
public static final String NPM_VERSION = "npmVersion";
18+
public static final String NPM_REPOSITORY = "npmRepository";
19+
public static final String SNAPSHOT = "snapshot";
20+
21+
protected String npmName = null;
22+
protected String npmVersion = "1.0.0";
23+
protected String npmRepository = null;
24+
25+
public JavascriptFlowtypedCodegen() {
26+
super();
27+
28+
// clear import mapping (from default generator) as TS does not use it
29+
// at the moment
30+
importMapping.clear();
31+
32+
setReservedWordsLowerCase(Arrays.asList(
33+
// local variable names used in API methods (endpoints)
34+
"varLocalPath", "queryParameters", "headerParams", "formParams", "useFormData", "varLocalDeferred",
35+
"requestOptions",
36+
// Typescript reserved words
37+
"abstract", "arguments", "boolean", "break", "byte",
38+
"case", "catch", "char", "class", "const",
39+
"continue", "debugger", "default", "delete", "do",
40+
"double", "else", "enum", "eval", "export",
41+
"extends", "false", "final", "finally", "float",
42+
"for", "function", "goto", "if", "implements",
43+
"import", "in", "instanceof", "int", "interface",
44+
"let", "long", "native", "new", "null",
45+
"package", "private", "protected", "public", "return",
46+
"short", "static", "super", "switch", "synchronized",
47+
"this", "throw", "throws", "transient", "true",
48+
"try", "typeof", "var", "void", "volatile",
49+
"while", "with", "yield",
50+
"Array", "Date", "eval", "function", "hasOwnProperty",
51+
"Infinity", "isFinite", "isNaN", "isPrototypeOf",
52+
"Math", "NaN", "Number", "Object",
53+
"prototype", "String", "toString", "undefined", "valueOf"));
54+
55+
languageSpecificPrimitives = new HashSet<String>(
56+
Arrays.asList("string", "Boolean", "number", "Array", "Object", "Date", "File", "Blob")
57+
);
58+
59+
instantiationTypes.put("array", "Array");
60+
instantiationTypes.put("list", "Array");
61+
instantiationTypes.put("map", "Object");
62+
typeMapping.clear();
63+
typeMapping.put("array", "Array");
64+
typeMapping.put("map", "Object");
65+
typeMapping.put("List", "Array");
66+
typeMapping.put("boolean", "Boolean");
67+
typeMapping.put("string", "string");
68+
typeMapping.put("int", "number");
69+
typeMapping.put("float", "number");
70+
typeMapping.put("number", "number");
71+
typeMapping.put("DateTime", "Date");
72+
typeMapping.put("date", "Date");
73+
typeMapping.put("long", "number");
74+
typeMapping.put("short", "number");
75+
typeMapping.put("char", "string");
76+
typeMapping.put("double", "number");
77+
typeMapping.put("object", "Object");
78+
typeMapping.put("integer", "number");
79+
// binary not supported in JavaScript client right now, using String as a workaround
80+
typeMapping.put("binary", "string");
81+
typeMapping.put("ByteArray", "string");
82+
typeMapping.put("UUID", "string");
83+
84+
defaultIncludes = new HashSet<String>(languageSpecificPrimitives);
85+
outputFolder = "generated-code/javascript-flowtyped";
86+
embeddedTemplateDir = templateDir = "Javascript-Flowtyped";
87+
88+
this.cliOptions.add(new CliOption(NPM_NAME, "The name under which you want to publish generated npm package"));
89+
this.cliOptions.add(new CliOption(NPM_VERSION, "The version of your npm package"));
90+
this.cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json"));
91+
this.cliOptions.add(new CliOption(SNAPSHOT, "When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm", BooleanProperty.TYPE).defaultValue(Boolean.FALSE.toString()));
92+
93+
}
94+
95+
@Override
96+
public CodegenType getTag() {
97+
return CodegenType.CLIENT;
98+
}
99+
100+
@Override
101+
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, ModelImpl swaggerModel) {
102+
codegenModel.additionalPropertiesType = getTypeDeclaration(swaggerModel.getAdditionalProperties());
103+
addImport(codegenModel, codegenModel.additionalPropertiesType);
104+
}
105+
106+
@Override
107+
public void processOpts() {
108+
super.processOpts();
109+
supportingFiles.add(new SupportingFile("index.mustache", "src", "index.js"));
110+
supportingFiles.add(new SupportingFile("api.mustache", "src", "api.js"));
111+
supportingFiles.add(new SupportingFile("configuration.mustache", "src", "configuration.js"));
112+
supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore"));
113+
114+
addNpmPackageGeneration();
115+
}
116+
117+
@Override
118+
public String getTypeDeclaration(Property p) {
119+
Property inner;
120+
if(p instanceof ArrayProperty) {
121+
ArrayProperty ap = (ArrayProperty)p;
122+
inner = ap.getItems();
123+
return this.getSwaggerType(p) + "<" + this.getTypeDeclaration(inner) + ">";
124+
} else if(p instanceof MapProperty) {
125+
MapProperty mp = (MapProperty)p;
126+
inner = mp.getAdditionalProperties();
127+
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
128+
} else if(p instanceof FileProperty || p instanceof ObjectProperty) {
129+
return "any";
130+
} else {
131+
return super.getTypeDeclaration(p);
132+
}
133+
}
134+
135+
private void addNpmPackageGeneration() {
136+
if (additionalProperties.containsKey(NPM_NAME)) {
137+
this.setNpmName(additionalProperties.get(NPM_NAME).toString());
138+
}
139+
140+
if (additionalProperties.containsKey(NPM_VERSION)) {
141+
this.setNpmVersion(additionalProperties.get(NPM_VERSION).toString());
142+
}
143+
144+
if (additionalProperties.containsKey(SNAPSHOT) && Boolean.valueOf(additionalProperties.get(SNAPSHOT).toString())) {
145+
this.setNpmVersion(npmVersion + "-SNAPSHOT." + SNAPSHOT_SUFFIX_FORMAT.format(new Date()));
146+
}
147+
additionalProperties.put(NPM_VERSION, npmVersion);
148+
149+
if (additionalProperties.containsKey(NPM_REPOSITORY)) {
150+
this.setNpmRepository(additionalProperties.get(NPM_REPOSITORY).toString());
151+
}
152+
153+
//Files for building our lib
154+
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
155+
supportingFiles.add(new SupportingFile("package.mustache", "", "package.json"));
156+
supportingFiles.add(new SupportingFile("flowconfig.mustache", "", ".flowconfig"));
157+
supportingFiles.add(new SupportingFile("babelrc", "", ".babelrc"));
158+
}
159+
160+
@Override
161+
public void preprocessSwagger(Swagger swagger) {
162+
if (swagger.getInfo() != null) {
163+
Info info = swagger.getInfo();
164+
if (StringUtils.isBlank(npmName) && info.getTitle() != null) {
165+
// when projectName is not specified, generate it from info.title
166+
npmName = sanitizeName(dashize(info.getTitle()));
167+
}
168+
if (StringUtils.isBlank(npmVersion)) {
169+
// when projectVersion is not specified, use info.version
170+
npmVersion = escapeUnsafeCharacters(escapeQuotationMark(info.getVersion()));
171+
}
172+
}
173+
174+
// default values
175+
if (StringUtils.isBlank(npmName)) {
176+
npmName = "swagger-js-client";
177+
}
178+
if (StringUtils.isBlank(npmVersion)) {
179+
npmVersion = "1.0.0";
180+
}
181+
182+
additionalProperties.put(NPM_NAME, npmName);
183+
additionalProperties.put(NPM_VERSION, npmVersion);
184+
additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage);
185+
}
186+
187+
@Override
188+
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
189+
// process enum in models
190+
List<Object> models = (List<Object>) postProcessModelsEnum(objs).get("models");
191+
for (Object _mo : models) {
192+
Map<String, Object> mo = (Map<String, Object>) _mo;
193+
CodegenModel cm = (CodegenModel) mo.get("model");
194+
cm.imports = new TreeSet(cm.imports);
195+
// name enum with model name, e.g. StatusEnum => Pet.StatusEnum
196+
for (CodegenProperty var : cm.vars) {
197+
if (Boolean.TRUE.equals(var.isEnum)) {
198+
var.datatypeWithEnum = var.datatypeWithEnum.replace(var.enumName, cm.classname + "" + var.enumName);
199+
}
200+
}
201+
if (cm.parent != null) {
202+
for (CodegenProperty var : cm.allVars) {
203+
if (Boolean.TRUE.equals(var.isEnum)) {
204+
var.datatypeWithEnum = var.datatypeWithEnum
205+
.replace(var.enumName, cm.classname + "" + var.enumName);
206+
}
207+
}
208+
}
209+
}
210+
211+
return objs;
212+
}
213+
214+
@Override
215+
public String escapeQuotationMark(String input) {
216+
// remove ', " to avoid code injection
217+
return input.replace("\"", "").replace("'", "");
218+
}
219+
220+
@Override
221+
public String escapeUnsafeCharacters(String input) {
222+
return input.replace("*/", "*_/").replace("/*", "/_*");
223+
}
224+
225+
@Override
226+
public String getName() {
227+
return "javascript-flowtyped";
228+
}
229+
230+
@Override
231+
public String getHelp() {
232+
return "Generates a Javascript client library using Flow types and Fetch API.";
233+
}
234+
235+
public String getNpmName() {
236+
return npmName;
237+
}
238+
239+
public void setNpmName(String npmName) {
240+
this.npmName = npmName;
241+
}
242+
243+
public String getNpmVersion() {
244+
return npmVersion;
245+
}
246+
247+
public void setNpmVersion(String npmVersion) {
248+
this.npmVersion = npmVersion;
249+
}
250+
251+
public String getNpmRepository() {
252+
return npmRepository;
253+
}
254+
255+
public void setNpmRepository(String npmRepository) {
256+
this.npmRepository = npmRepository;
257+
}
258+
259+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## {{npmName}}@{{npmVersion}}
2+
3+
This generator creates Flow typed 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+
* ES6
12+
13+
Module system
14+
* ES6 module system
15+
16+
### Building
17+
18+
To build an compile the flow typed sources to javascript use:
19+
```
20+
npm install
21+
npm run build
22+
```
23+
24+
### Publishing
25+
26+
First build the package then run ```npm publish```
27+
28+
### Consuming
29+
30+
navigate to the folder of your consuming project and run one of the following commands.
31+
32+
_published:_
33+
34+
```
35+
npm install {{npmName}}@{{npmVersion}} --save
36+
```
37+
38+
_unPublished (not recommended):_
39+
40+
```
41+
npm install PATH_TO_GENERATED_PACKAGE --save

0 commit comments

Comments
 (0)