Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generators/javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|sourceFolder|source folder for generated code| |src|
|useInheritance|use JavaScript prototype chains & delegation for inheritance| |true|
|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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
public static final String EMIT_JS_DOC = "emitJSDoc";
public static final String USE_ES6 = "useES6";
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_APOLLO = "apollo";
Expand All @@ -80,6 +81,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
protected boolean useES6 = true; // default is ES6
@Setter protected String npmRepository = null;
@Getter private String modelPropertyNaming = "camelCase";
@Setter protected boolean useURLSearchParams = false;

public JavascriptClientCodegen() {
super();
Expand Down Expand Up @@ -190,6 +192,10 @@ public JavascriptClientCodegen() {
.defaultValue(Boolean.TRUE.toString()));
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(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_APOLLO, "Apollo REST DataSource");
Expand Down Expand Up @@ -267,6 +273,9 @@ public void processOpts() {
if (additionalProperties.containsKey(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)) {
setLibrary((String) additionalProperties.get(CodegenConstants.LIBRARY));
}
Expand Down Expand Up @@ -334,6 +343,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
additionalProperties.put(EMIT_JS_DOC, emitJSDoc);
additionalProperties.put(USE_ES6, useES6);
additionalProperties.put(NPM_REPOSITORY, npmRepository);
additionalProperties.put(USE_URL_SEARCH_PARAMS, useURLSearchParams);

// make api and model doc path available in mustache template
additionalProperties.put("apiDocPath", apiDocPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{{>licenseInfo}}

import superagent from "superagent";
{{^useURLSearchParams}}
import querystring from "querystring";
{{! see https://www.npmjs.com/package/querystring && https://github.com/Gozala/querystring }}
{{/useURLSearchParams}}

{{#emitJSDoc}}/**
* @module {{#invokerPackage}}{{.}}/{{/invokerPackage}}ApiClient
Expand Down Expand Up @@ -455,7 +458,15 @@ class ApiClient {
}

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') {
var _formParams = this.normalizeParams(formParams);
for (var key in _formParams) {
Expand Down
Loading