add parameter 'useURLSearchParams' to use JS build-in UrlSearchParams, instead of deprecated npm lib 'querystring' (#19949)

This commit is contained in:
Tobias Brauneis 2024-10-31 07:48:45 +01:00 committed by GitHub
parent c70b07808e
commit ae4e2515dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 1 deletions

View File

@ -43,6 +43,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sourceFolder|source folder for generated code| |src| |sourceFolder|source folder for generated code| |src|
|useInheritance|use JavaScript prototype chains & delegation for inheritance| |true| |useInheritance|use JavaScript prototype chains & delegation for inheritance| |true|
|usePromises|use Promises as return values from the client API, instead of superagent callbacks| |false| |usePromises|use Promises as return values from the client API, instead of superagent callbacks| |false|
|useURLSearchParams|use JS build-in UrlSearchParams, instead of deprecated npm lib 'querystring'| |false|
## IMPORT MAPPING ## IMPORT MAPPING

View File

@ -57,6 +57,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
public static final String EMIT_JS_DOC = "emitJSDoc"; public static final String EMIT_JS_DOC = "emitJSDoc";
public static final String USE_ES6 = "useES6"; public static final String USE_ES6 = "useES6";
public static final String NPM_REPOSITORY = "npmRepository"; public static final String NPM_REPOSITORY = "npmRepository";
public static final String USE_URL_SEARCH_PARAMS = "useURLSearchParams";
public static final String LIBRARY_JAVASCRIPT = "javascript"; public static final String LIBRARY_JAVASCRIPT = "javascript";
public static final String LIBRARY_APOLLO = "apollo"; public static final String LIBRARY_APOLLO = "apollo";
@ -80,6 +81,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
protected boolean useES6 = true; // default is ES6 protected boolean useES6 = true; // default is ES6
@Setter protected String npmRepository = null; @Setter protected String npmRepository = null;
@Getter private String modelPropertyNaming = "camelCase"; @Getter private String modelPropertyNaming = "camelCase";
@Setter protected boolean useURLSearchParams = false;
public JavascriptClientCodegen() { public JavascriptClientCodegen() {
super(); super();
@ -190,6 +192,10 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
.defaultValue(Boolean.TRUE.toString())); .defaultValue(Boolean.TRUE.toString()));
cliOptions.add(new CliOption(CodegenConstants.MODEL_PROPERTY_NAMING, CodegenConstants.MODEL_PROPERTY_NAMING_DESC).defaultValue("camelCase")); cliOptions.add(new CliOption(CodegenConstants.MODEL_PROPERTY_NAMING, CodegenConstants.MODEL_PROPERTY_NAMING_DESC).defaultValue("camelCase"));
cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json")); cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json"));
cliOptions.add(new CliOption(USE_URL_SEARCH_PARAMS,
"use JS build-in UrlSearchParams, instead of deprecated npm lib 'querystring'")
.defaultValue(Boolean.FALSE.toString())
);
supportedLibraries.put(LIBRARY_JAVASCRIPT, "JavaScript client library"); supportedLibraries.put(LIBRARY_JAVASCRIPT, "JavaScript client library");
supportedLibraries.put(LIBRARY_APOLLO, "Apollo REST DataSource"); supportedLibraries.put(LIBRARY_APOLLO, "Apollo REST DataSource");
@ -267,6 +273,9 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
if (additionalProperties.containsKey(NPM_REPOSITORY)) { if (additionalProperties.containsKey(NPM_REPOSITORY)) {
setNpmRepository(((String) additionalProperties.get(NPM_REPOSITORY))); setNpmRepository(((String) additionalProperties.get(NPM_REPOSITORY)));
} }
if (additionalProperties.containsKey(USE_URL_SEARCH_PARAMS)) {
setUseURLSearchParams(convertPropertyToBooleanAndWriteBack(USE_URL_SEARCH_PARAMS));
}
if (additionalProperties.containsKey(CodegenConstants.LIBRARY)) { if (additionalProperties.containsKey(CodegenConstants.LIBRARY)) {
setLibrary((String) additionalProperties.get(CodegenConstants.LIBRARY)); setLibrary((String) additionalProperties.get(CodegenConstants.LIBRARY));
} }
@ -334,6 +343,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
additionalProperties.put(EMIT_JS_DOC, emitJSDoc); additionalProperties.put(EMIT_JS_DOC, emitJSDoc);
additionalProperties.put(USE_ES6, useES6); additionalProperties.put(USE_ES6, useES6);
additionalProperties.put(NPM_REPOSITORY, npmRepository); additionalProperties.put(NPM_REPOSITORY, npmRepository);
additionalProperties.put(USE_URL_SEARCH_PARAMS, useURLSearchParams);
// make api and model doc path available in mustache template // make api and model doc path available in mustache template
additionalProperties.put("apiDocPath", apiDocPath); additionalProperties.put("apiDocPath", apiDocPath);

View File

@ -1,7 +1,10 @@
{{>licenseInfo}} {{>licenseInfo}}
import superagent from "superagent"; import superagent from "superagent";
{{^useURLSearchParams}}
import querystring from "querystring"; import querystring from "querystring";
{{! see https://www.npmjs.com/package/querystring && https://github.com/Gozala/querystring }}
{{/useURLSearchParams}}
{{#emitJSDoc}}/** {{#emitJSDoc}}/**
* @module {{#invokerPackage}}{{.}}/{{/invokerPackage}}ApiClient * @module {{#invokerPackage}}{{.}}/{{/invokerPackage}}ApiClient
@ -455,7 +458,15 @@ class ApiClient {
} }
if (contentType === 'application/x-www-form-urlencoded') { if (contentType === 'application/x-www-form-urlencoded') {
request.send(querystring.stringify(this.normalizeParams(formParams))); {{^useURLSearchParams}}
let queryString = querystring.stringify(this.normalizeParams(formParams));
{{/useURLSearchParams}}
{{#useURLSearchParams}}
let normalizedParams = this.normalizeParams(formParams)
let urlSearchParams = new URLSearchParams(normalizedParams);
let queryString = urlSearchParams.toString();
{{/useURLSearchParams}}
request.send(queryString);
} else if (contentType == 'multipart/form-data') { } else if (contentType == 'multipart/form-data') {
var _formParams = this.normalizeParams(formParams); var _formParams = this.normalizeParams(formParams);
for (var key in _formParams) { for (var key in _formParams) {