Add useSingleRequestParameter to typescript-nestjs generator (#18476)

* feat: change template to use useSingleRequestParameter

* chore: add useSingleRequestParameter option

* chore: generate-samples, docs and fix spacing in template

* chore: remove random white space
This commit is contained in:
Arivanandan
2024-04-30 16:07:57 +09:00
committed by GitHub
parent e36172090e
commit a4cf255dce
6 changed files with 53 additions and 11 deletions

View File

@@ -45,6 +45,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|stringEnums|Generate string enums instead of objects for enum values.| |false|
|supportsES6|Generate code that conforms to ES6.| |false|
|taggedUnions|Use discriminators to create tagged unions instead of extending interfaces.| |false|
|useSingleRequestParameter|Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.| |false|
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|
## IMPORT MAPPING

View File

@@ -53,6 +53,7 @@ public class TypeScriptNestjsClientCodegen extends AbstractTypeScriptClientCodeg
public static final String FILE_NAMING = "fileNaming";
public static final String STRING_ENUMS = "stringEnums";
public static final String STRING_ENUMS_DESC = "Generate string enums instead of objects for enum values.";
public static final String USE_SINGLE_REQUEST_PARAMETER = "useSingleRequestParameter";
protected String nestVersion = "8.0.0";
protected String npmRepository = null;
@@ -99,6 +100,7 @@ public class TypeScriptNestjsClientCodegen extends AbstractTypeScriptClientCodeg
this.cliOptions.add(new CliOption(MODEL_FILE_SUFFIX, "The suffix of the file of the generated model (model<suffix>.ts)."));
this.cliOptions.add(new CliOption(FILE_NAMING, "Naming convention for the output files: 'camelCase', 'kebab-case'.").defaultValue(this.fileNaming));
this.cliOptions.add(new CliOption(STRING_ENUMS, STRING_ENUMS_DESC).defaultValue(String.valueOf(this.stringEnums)));
this.cliOptions.add(new CliOption(USE_SINGLE_REQUEST_PARAMETER, "Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.").defaultValue(Boolean.FALSE.toString()));
}
@Override

View File

@@ -20,6 +20,31 @@ import { {{classname}}Interface } from './{{classFilename}}Interface';
{{/withInterfaces}}
{{#operations}}
{{#useSingleRequestParameter}}
{{#operation}}
{{#allParams.0}}
/**
* Request parameters for {{nickname}} operation in {{classname}}.
* @export
* @interface {{classname}}{{operationIdCamelCase}}Request
*/
export interface {{classname}}{{operationIdCamelCase}}Request {
{{#allParams}}
/**
* {{description}}
* @type {{=<% %>=}}{<%&dataType%>}<%={{ }}=%>
* @memberof {{classname}}{{operationIdCamelCase}}
*/
readonly {{paramName}}{{^required}}?{{/required}}: {{{dataType}}}
{{^-last}}
{{/-last}}
{{/allParams}}
}
{{/allParams.0}}
{{/operation}}
{{/useSingleRequestParameter}}
{{#description}}
/**
@@ -56,14 +81,36 @@ export class {{classname}} {
/**
* {{summary}}
* {{notes}}
{{#useSingleRequestParameter}}
{{#allParams.0}}
* @param {{=<% %>=}}{<%& classname %><%& operationIdCamelCase %>Request}<%={{ }}=%> requestParameters Request parameters.
*/
{{/allParams.0}}
{{/useSingleRequestParameter}}
{{^useSingleRequestParameter}}
{{#allParams}}* @param {{paramName}} {{description}}
{{/allParams}}* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
* @param reportProgress flag to report request and response progress.
*/
{{/useSingleRequestParameter}}
{{#useSingleRequestParameter}}
public {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}): Observable<AxiosResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
public {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}): Observable<any> {
{{/useSingleRequestParameter}}
{{^useSingleRequestParameter}}
public {{nickname}}({{#allParams}}{{^isConstEnumParam}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/isConstEnumParam}}{{/allParams}}): Observable<AxiosResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
public {{nickname}}({{#allParams}}{{^isConstEnumParam}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/isConstEnumParam}}{{/allParams}}): Observable<any> {
{{/useSingleRequestParameter}}
{{#useSingleRequestParameter}}
const {
{{#allParams}}
{{paramName}},
{{/allParams}}
} = requestParameters;
{{/useSingleRequestParameter}}
{{#allParams}}
{{#required}}
{{#isConstEnumParam}}
let {{paramName}} = {{{dataType}}};
@@ -72,10 +119,10 @@ export class {{classname}} {
if ({{paramName}} === null || {{paramName}} === undefined) {
throw new Error('Required parameter {{paramName}} was null or undefined when calling {{nickname}}.');
}
{{/isConstEnumParam}}
{{/required}}
{{/allParams}}
{{#hasQueryParams}}
let queryParameters = new URLSearchParams();
{{#queryParams}}

View File

@@ -48,6 +48,7 @@ public class TypeScriptNestjsClientOptionsProvider implements OptionsProvider {
public static String MODEL_FILE_SUFFIX = "";
public static final String ENUM_UNKNOWN_DEFAULT_CASE_VALUE = "false";
public static final String ENUM_PROPERTY_NAMING_REPLACE_SPECIAL_CHAR_VALUE = "false";
public static final String USE_SINGLE_REQUEST_PARAMETER = "false";
@Override
public String getLanguage() {
@@ -88,6 +89,7 @@ public class TypeScriptNestjsClientOptionsProvider implements OptionsProvider {
.put(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT, "true")
.put(TypeScriptNestjsClientCodegen.FILE_NAMING, FILE_NAMING_VALUE)
.put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, ENUM_UNKNOWN_DEFAULT_CASE_VALUE)
.put(CodegenConstants.USE_SINGLE_REQUEST_PARAMETER, USE_SINGLE_REQUEST_PARAMETER)
.build();
}

View File

@@ -116,7 +116,6 @@ export class PetService {
throw new Error('Required parameter petId was null or undefined when calling deletePet.');
}
let headers = {...this.defaultHeaders};
if (apiKey !== undefined && apiKey !== null) {
headers['api_key'] = String(apiKey);
@@ -402,8 +401,6 @@ export class PetService {
throw new Error('Required parameter petId was null or undefined when calling updatePetWithForm.');
}
let headers = {...this.defaultHeaders};
let accessTokenObservable: Observable<any> = of(null);
@@ -479,8 +476,6 @@ export class PetService {
throw new Error('Required parameter petId was null or undefined when calling uploadFile.');
}
let headers = {...this.defaultHeaders};
let accessTokenObservable: Observable<any> = of(null);

View File

@@ -117,7 +117,6 @@ export class PetService {
throw new Error('Required parameter petId was null or undefined when calling deletePet.');
}
let headers = {...this.defaultHeaders};
if (apiKey !== undefined && apiKey !== null) {
headers['api_key'] = String(apiKey);
@@ -403,8 +402,6 @@ export class PetService {
throw new Error('Required parameter petId was null or undefined when calling updatePetWithForm.');
}
let headers = {...this.defaultHeaders};
let accessTokenObservable: Observable<any> = of(null);
@@ -480,8 +477,6 @@ export class PetService {
throw new Error('Required parameter petId was null or undefined when calling uploadFile.');
}
let headers = {...this.defaultHeaders};
let accessTokenObservable: Observable<any> = of(null);