add option to skip setting user agent in js client (#20367)

This commit is contained in:
William Cheng 2024-12-22 23:11:54 +08:00 committed by GitHub
parent 5ba608ff8a
commit ab8d359dc3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 47 additions and 30 deletions

View File

@ -5,6 +5,7 @@ inputSpec: modules/openapi-generator/src/test/resources/3_0/javascript/petstore-
templateDir: modules/openapi-generator/src/main/resources/Javascript
additionalProperties:
appName: PetstoreClient
skipDefaultUserAgent: true
modelNameMappings:
HealthCheckResult: HealthCheckStatus
parameterNameMappings:

View File

@ -38,6 +38,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|projectDescription|description of the project (Default: using info.description or "Client library of <projectName>")| |null|
|projectName|name of the project (Default: generated from info.title or "openapi-js-client")| |null|
|projectVersion|version of the project (Default: using info.version or "1.0.0")| |null|
|skipDefaultUserAgent|Skip setting default user-agent in ApiClient.js| |false|
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|sourceFolder|source folder for generated code| |src|

View File

@ -58,30 +58,44 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
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 SKIP_DEFAULT_USER_AGENT = "skipDefaultUserAgent";
public static final String LIBRARY_JAVASCRIPT = "javascript";
public static final String LIBRARY_APOLLO = "apollo";
@Setter protected String projectName;
@Setter protected String moduleName;
@Setter protected String projectDescription;
@Setter protected String projectVersion;
@Setter protected String licenseName;
@Setter
protected String projectName;
@Setter
protected String moduleName;
@Setter
protected String projectDescription;
@Setter
protected String projectVersion;
@Setter
protected String licenseName;
@Getter @Setter
@Getter
@Setter
protected String invokerPackage;
@Setter protected String sourceFolder = "src";
@Setter protected boolean usePromises;
@Setter protected boolean emitModelMethods;
@Setter protected boolean emitJSDoc = true;
@Setter
protected String sourceFolder = "src";
@Setter
protected boolean usePromises;
@Setter
protected boolean emitModelMethods;
@Setter
protected boolean emitJSDoc = true;
protected String apiDocPath = "docs/";
protected String modelDocPath = "docs/";
protected String apiTestPath = "api/";
protected String modelTestPath = "model/";
protected boolean useES6 = true; // default is ES6
@Setter protected String npmRepository = null;
@Getter private String modelPropertyNaming = "camelCase";
@Setter protected boolean useURLSearchParams = true;
@Setter
protected String npmRepository = null;
@Getter
private String modelPropertyNaming = "camelCase";
@Setter
protected boolean useURLSearchParams = true;
public JavascriptClientCodegen() {
super();
@ -196,6 +210,9 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
"use JS build-in UrlSearchParams, instead of deprecated npm lib 'querystring'")
.defaultValue(Boolean.TRUE.toString())
);
cliOptions.add(new CliOption(SKIP_DEFAULT_USER_AGENT,
"Skip setting default user-agent in ApiClient.js")
.defaultValue(Boolean.FALSE.toString()));
supportedLibraries.put(LIBRARY_JAVASCRIPT, "JavaScript client library");
supportedLibraries.put(LIBRARY_APOLLO, "Apollo REST DataSource");
@ -1186,7 +1203,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
// only process files with js extension
if ("js".equals(FilenameUtils.getExtension(file.toString()))) {
this.executePostProcessor(new String[] {jsPostProcessFile, file.toString()});
this.executePostProcessor(new String[]{jsPostProcessFile, file.toString()});
}
}

View File

@ -55,15 +55,20 @@ class ApiClient {
</authMethods>
}
<={{ }}=>{{#emitJSDoc}}/**
<={{ }}=>
{{^skipDefaultUserAgent}}
{{#emitJSDoc}}
/**
* The default HTTP headers to be included for all API calls.
* @type {Array.<String>}
* @default {}
*/{{/emitJSDoc}}
*/
{{/emitJSDoc}}
this.defaultHeaders = {
'User-Agent': '{{{httpUserAgent}}}{{^httpUserAgent}}OpenAPI-Generator/{{projectVersion}}/Javascript{{/httpUserAgent}}'
};
{{/skipDefaultUserAgent}}
/**
* The default HTTP timeout for all API calls.
* @type {Number}
@ -79,11 +84,13 @@ class ApiClient {
*/
this.cache = true;
{{#emitJSDoc}}/**
{{#emitJSDoc}}
/**
* If set to true, the client will save the cookies from each server
* response, and return them in the next request.
* @default false
*/{{/emitJSDoc}}
*/
{{/emitJSDoc}}
this.enableCookies = false;
/*

View File

@ -52,15 +52,6 @@ class ApiClient {
'bearer_test': {type: 'bearer'}, // JWT
}
/**
* The default HTTP headers to be included for all API calls.
* @type {Array.<String>}
* @default {}
*/
this.defaultHeaders = {
'User-Agent': 'OpenAPI-Generator/1.0.0/Javascript'
};
/**
* The default HTTP timeout for all API calls.
* @type {Number}
@ -76,7 +67,7 @@ class ApiClient {
*/
this.cache = true;
/**
/**
* If set to true, the client will save the cookies from each server
* response, and return them in the next request.
* @default false

View File

@ -52,7 +52,7 @@ class ApiClient {
'bearer_test': {type: 'bearer'}, // JWT
}
/**
/**
* The default HTTP headers to be included for all API calls.
* @type {Array.<String>}
* @default {}
@ -76,7 +76,7 @@ class ApiClient {
*/
this.cache = true;
/**
/**
* If set to true, the client will save the cookies from each server
* response, and return them in the next request.
* @default false