Skip to content

Commit a4cf255

Browse files
authored
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
1 parent e361720 commit a4cf255

6 files changed

Lines changed: 53 additions & 11 deletions

File tree

docs/generators/typescript-nestjs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
4545
|stringEnums|Generate string enums instead of objects for enum values.| |false|
4646
|supportsES6|Generate code that conforms to ES6.| |false|
4747
|taggedUnions|Use discriminators to create tagged unions instead of extending interfaces.| |false|
48+
|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|
4849
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|
4950

5051
## IMPORT MAPPING

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptNestjsClientCodegen.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class TypeScriptNestjsClientCodegen extends AbstractTypeScriptClientCodeg
5353
public static final String FILE_NAMING = "fileNaming";
5454
public static final String STRING_ENUMS = "stringEnums";
5555
public static final String STRING_ENUMS_DESC = "Generate string enums instead of objects for enum values.";
56+
public static final String USE_SINGLE_REQUEST_PARAMETER = "useSingleRequestParameter";
5657

5758
protected String nestVersion = "8.0.0";
5859
protected String npmRepository = null;
@@ -99,6 +100,7 @@ public TypeScriptNestjsClientCodegen() {
99100
this.cliOptions.add(new CliOption(MODEL_FILE_SUFFIX, "The suffix of the file of the generated model (model<suffix>.ts)."));
100101
this.cliOptions.add(new CliOption(FILE_NAMING, "Naming convention for the output files: 'camelCase', 'kebab-case'.").defaultValue(this.fileNaming));
101102
this.cliOptions.add(new CliOption(STRING_ENUMS, STRING_ENUMS_DESC).defaultValue(String.valueOf(this.stringEnums)));
103+
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()));
102104
}
103105

104106
@Override

modules/openapi-generator/src/main/resources/typescript-nestjs/api.service.mustache

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,31 @@ import { {{classname}}Interface } from './{{classFilename}}Interface';
2020
{{/withInterfaces}}
2121

2222
{{#operations}}
23+
{{#useSingleRequestParameter}}
24+
{{#operation}}
25+
{{#allParams.0}}
26+
/**
27+
* Request parameters for {{nickname}} operation in {{classname}}.
28+
* @export
29+
* @interface {{classname}}{{operationIdCamelCase}}Request
30+
*/
31+
export interface {{classname}}{{operationIdCamelCase}}Request {
32+
{{#allParams}}
33+
/**
34+
* {{description}}
35+
* @type {{=<% %>=}}{<%&dataType%>}<%={{ }}=%>
36+
* @memberof {{classname}}{{operationIdCamelCase}}
37+
*/
38+
readonly {{paramName}}{{^required}}?{{/required}}: {{{dataType}}}
39+
{{^-last}}
40+
41+
{{/-last}}
42+
{{/allParams}}
43+
}
44+
45+
{{/allParams.0}}
46+
{{/operation}}
47+
{{/useSingleRequestParameter}}
2348

2449
{{#description}}
2550
/**
@@ -56,14 +81,36 @@ export class {{classname}} {
5681
/**
5782
* {{summary}}
5883
* {{notes}}
84+
{{#useSingleRequestParameter}}
85+
{{#allParams.0}}
86+
* @param {{=<% %>=}}{<%& classname %><%& operationIdCamelCase %>Request}<%={{ }}=%> requestParameters Request parameters.
87+
*/
88+
{{/allParams.0}}
89+
{{/useSingleRequestParameter}}
90+
{{^useSingleRequestParameter}}
5991
{{#allParams}}* @param {{paramName}} {{description}}
6092
{{/allParams}}* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
6193
* @param reportProgress flag to report request and response progress.
6294
*/
95+
{{/useSingleRequestParameter}}
96+
{{#useSingleRequestParameter}}
97+
public {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}): Observable<AxiosResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
98+
public {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}): Observable<any> {
99+
{{/useSingleRequestParameter}}
100+
{{^useSingleRequestParameter}}
63101
public {{nickname}}({{#allParams}}{{^isConstEnumParam}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/isConstEnumParam}}{{/allParams}}): Observable<AxiosResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
64102
public {{nickname}}({{#allParams}}{{^isConstEnumParam}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/isConstEnumParam}}{{/allParams}}): Observable<any> {
103+
{{/useSingleRequestParameter}}
104+
{{#useSingleRequestParameter}}
105+
const {
65106
{{#allParams}}
107+
{{paramName}},
108+
{{/allParams}}
109+
} = requestParameters;
110+
111+
{{/useSingleRequestParameter}}
66112

113+
{{#allParams}}
67114
{{#required}}
68115
{{#isConstEnumParam}}
69116
let {{paramName}} = {{{dataType}}};
@@ -72,10 +119,10 @@ export class {{classname}} {
72119
if ({{paramName}} === null || {{paramName}} === undefined) {
73120
throw new Error('Required parameter {{paramName}} was null or undefined when calling {{nickname}}.');
74121
}
122+
75123
{{/isConstEnumParam}}
76124
{{/required}}
77125
{{/allParams}}
78-
79126
{{#hasQueryParams}}
80127
let queryParameters = new URLSearchParams();
81128
{{#queryParams}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptNestjsClientOptionsProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class TypeScriptNestjsClientOptionsProvider implements OptionsProvider {
4848
public static String MODEL_FILE_SUFFIX = "";
4949
public static final String ENUM_UNKNOWN_DEFAULT_CASE_VALUE = "false";
5050
public static final String ENUM_PROPERTY_NAMING_REPLACE_SPECIAL_CHAR_VALUE = "false";
51+
public static final String USE_SINGLE_REQUEST_PARAMETER = "false";
5152

5253
@Override
5354
public String getLanguage() {
@@ -88,6 +89,7 @@ public Map<String, String> createOptions() {
8889
.put(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT, "true")
8990
.put(TypeScriptNestjsClientCodegen.FILE_NAMING, FILE_NAMING_VALUE)
9091
.put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, ENUM_UNKNOWN_DEFAULT_CASE_VALUE)
92+
.put(CodegenConstants.USE_SINGLE_REQUEST_PARAMETER, USE_SINGLE_REQUEST_PARAMETER)
9193
.build();
9294
}
9395

samples/client/petstore/typescript-nestjs-v6-provided-in-root/builds/default/api/pet.service.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ export class PetService {
116116
throw new Error('Required parameter petId was null or undefined when calling deletePet.');
117117
}
118118

119-
120119
let headers = {...this.defaultHeaders};
121120
if (apiKey !== undefined && apiKey !== null) {
122121
headers['api_key'] = String(apiKey);
@@ -402,8 +401,6 @@ export class PetService {
402401
throw new Error('Required parameter petId was null or undefined when calling updatePetWithForm.');
403402
}
404403

405-
406-
407404
let headers = {...this.defaultHeaders};
408405

409406
let accessTokenObservable: Observable<any> = of(null);
@@ -479,8 +476,6 @@ export class PetService {
479476
throw new Error('Required parameter petId was null or undefined when calling uploadFile.');
480477
}
481478

482-
483-
484479
let headers = {...this.defaultHeaders};
485480

486481
let accessTokenObservable: Observable<any> = of(null);

samples/client/petstore/typescript-nestjs-v8-provided-in-root/builds/default/api/pet.service.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ export class PetService {
117117
throw new Error('Required parameter petId was null or undefined when calling deletePet.');
118118
}
119119

120-
121120
let headers = {...this.defaultHeaders};
122121
if (apiKey !== undefined && apiKey !== null) {
123122
headers['api_key'] = String(apiKey);
@@ -403,8 +402,6 @@ export class PetService {
403402
throw new Error('Required parameter petId was null or undefined when calling updatePetWithForm.');
404403
}
405404

406-
407-
408405
let headers = {...this.defaultHeaders};
409406

410407
let accessTokenObservable: Observable<any> = of(null);
@@ -480,8 +477,6 @@ export class PetService {
480477
throw new Error('Required parameter petId was null or undefined when calling uploadFile.');
481478
}
482479

483-
484-
485480
let headers = {...this.defaultHeaders};
486481

487482
let accessTokenObservable: Observable<any> = of(null);

0 commit comments

Comments
 (0)