Skip to content

Commit 8c337f0

Browse files
Add optional AxiosRequestConfig parameter to typescript-nestjs service functions (#20222)
* feat: add options to service functions * fix: fix type error * refactor: change options parameter * chore: update samples * fix: fix options use order * refactor: rename options * refactor: import type
1 parent f1c1567 commit 8c337f0

File tree

8 files changed

+220
-136
lines changed

8 files changed

+220
-136
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { HttpService } from '@nestjs/axios';
88
{{^useAxiosHttpModule}}
99
import { HttpService, Injectable, Optional } from '@nestjs/common';
1010
{{/useAxiosHttpModule}}
11-
import { AxiosResponse } from 'axios';
11+
import type { AxiosRequestConfig, AxiosResponse } from 'axios';
1212
import { Observable, from, of, switchMap } from 'rxjs';
1313
{{#imports}}
1414
import { {{classname}} } from '../{{filename}}';
@@ -93,14 +93,15 @@ export class {{classname}} {
9393
{{/allParams}}* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
9494
* @param reportProgress flag to report request and response progress.
9595
{{/useSingleRequestParameter}}
96+
* @param {*} [{{nickname}}Opts.config] Override http request option.
9697
*/
9798
{{#useSingleRequestParameter}}
98-
public {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}): Observable<AxiosResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
99-
public {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}): Observable<any> {
99+
public {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}{{nickname}}Opts?: { config?: AxiosRequestConfig }): Observable<AxiosResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
100+
public {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}{{nickname}}Opts?: { config?: AxiosRequestConfig }): Observable<any> {
100101
{{/useSingleRequestParameter}}
101102
{{^useSingleRequestParameter}}
102-
public {{nickname}}({{#allParams}}{{^isConstEnumParam}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/isConstEnumParam}}{{/allParams}}): Observable<AxiosResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
103-
public {{nickname}}({{#allParams}}{{^isConstEnumParam}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/isConstEnumParam}}{{/allParams}}): Observable<any> {
103+
public {{nickname}}({{#allParams}}{{^isConstEnumParam}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/isConstEnumParam}}{{/allParams}}{{nickname}}Opts?: { config?: AxiosRequestConfig }): Observable<AxiosResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
104+
public {{nickname}}({{#allParams}}{{^isConstEnumParam}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/isConstEnumParam}}{{/allParams}}{{nickname}}Opts?: { config?: AxiosRequestConfig }): Observable<any> {
104105
{{/useSingleRequestParameter}}
105106
{{#allParams.0}}
106107
{{#useSingleRequestParameter}}
@@ -291,7 +292,8 @@ export class {{classname}} {
291292
responseType: "blob",
292293
{{/isResponseFile}}
293294
withCredentials: this.configuration.withCredentials,
294-
headers: headers
295+
...{{nickname}}Opts?.config,
296+
headers: {...headers, ...{{nickname}}Opts?.config?.headers},
295297
}
296298
);
297299
})

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

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/* tslint:disable:no-unused-variable member-ordering */
1313

1414
import { HttpService, Injectable, Optional } from '@nestjs/common';
15-
import { AxiosResponse } from 'axios';
15+
import type { AxiosRequestConfig, AxiosResponse } from 'axios';
1616
import { Observable, from, of, switchMap } from 'rxjs';
1717
import { ApiResponse } from '../model/apiResponse';
1818
import { Pet } from '../model/pet';
@@ -49,9 +49,10 @@ export class PetService {
4949
* @param pet Pet object that needs to be added to the store
5050
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
5151
* @param reportProgress flag to report request and response progress.
52+
* @param {*} [addPetOpts.config] Override http request option.
5253
*/
53-
public addPet(pet: Pet, ): Observable<AxiosResponse<Pet>>;
54-
public addPet(pet: Pet, ): Observable<any> {
54+
public addPet(pet: Pet, addPetOpts?: { config?: AxiosRequestConfig }): Observable<AxiosResponse<Pet>>;
55+
public addPet(pet: Pet, addPetOpts?: { config?: AxiosRequestConfig }): Observable<any> {
5556
if (pet === null || pet === undefined) {
5657
throw new Error('Required parameter pet was null or undefined when calling addPet.');
5758
}
@@ -96,7 +97,8 @@ export class PetService {
9697
pet,
9798
{
9899
withCredentials: this.configuration.withCredentials,
99-
headers: headers
100+
...addPetOpts?.config,
101+
headers: {...headers, ...addPetOpts?.config?.headers},
100102
}
101103
);
102104
})
@@ -109,9 +111,10 @@ export class PetService {
109111
* @param apiKey
110112
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
111113
* @param reportProgress flag to report request and response progress.
114+
* @param {*} [deletePetOpts.config] Override http request option.
112115
*/
113-
public deletePet(petId: number, apiKey?: string, ): Observable<AxiosResponse<any>>;
114-
public deletePet(petId: number, apiKey?: string, ): Observable<any> {
116+
public deletePet(petId: number, apiKey?: string, deletePetOpts?: { config?: AxiosRequestConfig }): Observable<AxiosResponse<any>>;
117+
public deletePet(petId: number, apiKey?: string, deletePetOpts?: { config?: AxiosRequestConfig }): Observable<any> {
115118
if (petId === null || petId === undefined) {
116119
throw new Error('Required parameter petId was null or undefined when calling deletePet.');
117120
}
@@ -150,7 +153,8 @@ export class PetService {
150153
return this.httpClient.delete<any>(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`,
151154
{
152155
withCredentials: this.configuration.withCredentials,
153-
headers: headers
156+
...deletePetOpts?.config,
157+
headers: {...headers, ...deletePetOpts?.config?.headers},
154158
}
155159
);
156160
})
@@ -162,9 +166,10 @@ export class PetService {
162166
* @param status Status values that need to be considered for filter
163167
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
164168
* @param reportProgress flag to report request and response progress.
169+
* @param {*} [findPetsByStatusOpts.config] Override http request option.
165170
*/
166-
public findPetsByStatus(status: Array<'available' | 'pending' | 'sold'>, ): Observable<AxiosResponse<Array<Pet>>>;
167-
public findPetsByStatus(status: Array<'available' | 'pending' | 'sold'>, ): Observable<any> {
171+
public findPetsByStatus(status: Array<'available' | 'pending' | 'sold'>, findPetsByStatusOpts?: { config?: AxiosRequestConfig }): Observable<AxiosResponse<Array<Pet>>>;
172+
public findPetsByStatus(status: Array<'available' | 'pending' | 'sold'>, findPetsByStatusOpts?: { config?: AxiosRequestConfig }): Observable<any> {
168173
if (status === null || status === undefined) {
169174
throw new Error('Required parameter status was null or undefined when calling findPetsByStatus.');
170175
}
@@ -208,7 +213,8 @@ export class PetService {
208213
{
209214
params: queryParameters,
210215
withCredentials: this.configuration.withCredentials,
211-
headers: headers
216+
...findPetsByStatusOpts?.config,
217+
headers: {...headers, ...findPetsByStatusOpts?.config?.headers},
212218
}
213219
);
214220
})
@@ -220,9 +226,10 @@ export class PetService {
220226
* @param tags Tags to filter by
221227
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
222228
* @param reportProgress flag to report request and response progress.
229+
* @param {*} [findPetsByTagsOpts.config] Override http request option.
223230
*/
224-
public findPetsByTags(tags: Array<string>, ): Observable<AxiosResponse<Array<Pet>>>;
225-
public findPetsByTags(tags: Array<string>, ): Observable<any> {
231+
public findPetsByTags(tags: Array<string>, findPetsByTagsOpts?: { config?: AxiosRequestConfig }): Observable<AxiosResponse<Array<Pet>>>;
232+
public findPetsByTags(tags: Array<string>, findPetsByTagsOpts?: { config?: AxiosRequestConfig }): Observable<any> {
226233
if (tags === null || tags === undefined) {
227234
throw new Error('Required parameter tags was null or undefined when calling findPetsByTags.');
228235
}
@@ -266,7 +273,8 @@ export class PetService {
266273
{
267274
params: queryParameters,
268275
withCredentials: this.configuration.withCredentials,
269-
headers: headers
276+
...findPetsByTagsOpts?.config,
277+
headers: {...headers, ...findPetsByTagsOpts?.config?.headers},
270278
}
271279
);
272280
})
@@ -278,9 +286,10 @@ export class PetService {
278286
* @param petId ID of pet to return
279287
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
280288
* @param reportProgress flag to report request and response progress.
289+
* @param {*} [getPetByIdOpts.config] Override http request option.
281290
*/
282-
public getPetById(petId: number, ): Observable<AxiosResponse<Pet>>;
283-
public getPetById(petId: number, ): Observable<any> {
291+
public getPetById(petId: number, getPetByIdOpts?: { config?: AxiosRequestConfig }): Observable<AxiosResponse<Pet>>;
292+
public getPetById(petId: number, getPetByIdOpts?: { config?: AxiosRequestConfig }): Observable<any> {
284293
if (petId === null || petId === undefined) {
285294
throw new Error('Required parameter petId was null or undefined when calling getPetById.');
286295
}
@@ -316,7 +325,8 @@ export class PetService {
316325
return this.httpClient.get<Pet>(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`,
317326
{
318327
withCredentials: this.configuration.withCredentials,
319-
headers: headers
328+
...getPetByIdOpts?.config,
329+
headers: {...headers, ...getPetByIdOpts?.config?.headers},
320330
}
321331
);
322332
})
@@ -328,9 +338,10 @@ export class PetService {
328338
* @param pet Pet object that needs to be added to the store
329339
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
330340
* @param reportProgress flag to report request and response progress.
341+
* @param {*} [updatePetOpts.config] Override http request option.
331342
*/
332-
public updatePet(pet: Pet, ): Observable<AxiosResponse<Pet>>;
333-
public updatePet(pet: Pet, ): Observable<any> {
343+
public updatePet(pet: Pet, updatePetOpts?: { config?: AxiosRequestConfig }): Observable<AxiosResponse<Pet>>;
344+
public updatePet(pet: Pet, updatePetOpts?: { config?: AxiosRequestConfig }): Observable<any> {
334345
if (pet === null || pet === undefined) {
335346
throw new Error('Required parameter pet was null or undefined when calling updatePet.');
336347
}
@@ -375,7 +386,8 @@ export class PetService {
375386
pet,
376387
{
377388
withCredentials: this.configuration.withCredentials,
378-
headers: headers
389+
...updatePetOpts?.config,
390+
headers: {...headers, ...updatePetOpts?.config?.headers},
379391
}
380392
);
381393
})
@@ -389,9 +401,10 @@ export class PetService {
389401
* @param status Updated status of the pet
390402
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
391403
* @param reportProgress flag to report request and response progress.
404+
* @param {*} [updatePetWithFormOpts.config] Override http request option.
392405
*/
393-
public updatePetWithForm(petId: number, name?: string, status?: string, ): Observable<AxiosResponse<any>>;
394-
public updatePetWithForm(petId: number, name?: string, status?: string, ): Observable<any> {
406+
public updatePetWithForm(petId: number, name?: string, status?: string, updatePetWithFormOpts?: { config?: AxiosRequestConfig }): Observable<AxiosResponse<any>>;
407+
public updatePetWithForm(petId: number, name?: string, status?: string, updatePetWithFormOpts?: { config?: AxiosRequestConfig }): Observable<any> {
395408
if (petId === null || petId === undefined) {
396409
throw new Error('Required parameter petId was null or undefined when calling updatePetWithForm.');
397410
}
@@ -449,7 +462,8 @@ export class PetService {
449462
convertFormParamsToString ? formParams!.toString() : formParams!,
450463
{
451464
withCredentials: this.configuration.withCredentials,
452-
headers: headers
465+
...updatePetWithFormOpts?.config,
466+
headers: {...headers, ...updatePetWithFormOpts?.config?.headers},
453467
}
454468
);
455469
})
@@ -463,9 +477,10 @@ export class PetService {
463477
* @param file file to upload
464478
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
465479
* @param reportProgress flag to report request and response progress.
480+
* @param {*} [uploadFileOpts.config] Override http request option.
466481
*/
467-
public uploadFile(petId: number, additionalMetadata?: string, file?: Blob, ): Observable<AxiosResponse<ApiResponse>>;
468-
public uploadFile(petId: number, additionalMetadata?: string, file?: Blob, ): Observable<any> {
482+
public uploadFile(petId: number, additionalMetadata?: string, file?: Blob, uploadFileOpts?: { config?: AxiosRequestConfig }): Observable<AxiosResponse<ApiResponse>>;
483+
public uploadFile(petId: number, additionalMetadata?: string, file?: Blob, uploadFileOpts?: { config?: AxiosRequestConfig }): Observable<any> {
469484
if (petId === null || petId === undefined) {
470485
throw new Error('Required parameter petId was null or undefined when calling uploadFile.');
471486
}
@@ -528,7 +543,8 @@ export class PetService {
528543
convertFormParamsToString ? formParams!.toString() : formParams!,
529544
{
530545
withCredentials: this.configuration.withCredentials,
531-
headers: headers
546+
...uploadFileOpts?.config,
547+
headers: {...headers, ...uploadFileOpts?.config?.headers},
532548
}
533549
);
534550
})

0 commit comments

Comments
 (0)