Skip to content

Add optional AxiosRequestConfig parameter to typescript-nestjs service functions #20222

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { HttpService } from '@nestjs/axios';
{{^useAxiosHttpModule}}
import { HttpService, Injectable, Optional } from '@nestjs/common';
{{/useAxiosHttpModule}}
import { AxiosResponse } from 'axios';
import { AxiosRequestConfig, AxiosResponse } from 'axios';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed: should this be be a type only import if we assume TS > 3.8?

refs #20064

import { Observable, from, of, switchMap } from 'rxjs';
{{#imports}}
import { {{classname}} } from '../{{filename}}';
Expand Down Expand Up @@ -93,14 +93,15 @@ export class {{classname}} {
{{/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}}
* @param {*} [{{nickname}}RequestConfig.options] Override http request option.
*/
{{#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> {
public {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}{{nickname}}RequestConfig?: { options?: AxiosRequestConfig }): Observable<AxiosResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a better name, now that it is a pojo with an options config object?

Suggested change
public {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}{{nickname}}RequestConfig?: { options?: AxiosRequestConfig }): Observable<AxiosResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
public {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}{{nickname}}Opts?: { options?: AxiosRequestConfig }): Observable<AxiosResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
Suggested change
public {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}{{nickname}}RequestConfig?: { options?: AxiosRequestConfig }): Observable<AxiosResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
public {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}{{nickname}}Config?: { options?: AxiosRequestConfig }): Observable<AxiosResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;

or something? Given the AxiosRequestConfig is now only one of potentially many items?

Also:

Suggested change
public {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}{{nickname}}RequestConfig?: { options?: AxiosRequestConfig }): Observable<AxiosResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
public {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}{{nickname}}RequestConfig?: { config?: AxiosRequestConfig }): Observable<AxiosResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;

given the name of the class/interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's a good idea, so more like this?

{{nickname}}Opts?: { config?: AxiosRequestConfig }

@macjohnny wdyt ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

public {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request, {{/allParams.0}}{{nickname}}RequestConfig?: { options?: AxiosRequestConfig }): 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> {
public {{nickname}}({{#allParams}}{{^isConstEnumParam}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/isConstEnumParam}}{{/allParams}}{{nickname}}RequestConfig?: { options?: AxiosRequestConfig }): Observable<AxiosResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
public {{nickname}}({{#allParams}}{{^isConstEnumParam}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/isConstEnumParam}}{{/allParams}}{{nickname}}RequestConfig?: { options?: AxiosRequestConfig }): Observable<any> {
{{/useSingleRequestParameter}}
{{#allParams.0}}
{{#useSingleRequestParameter}}
Expand Down Expand Up @@ -291,7 +292,8 @@ export class {{classname}} {
responseType: "blob",
{{/isResponseFile}}
withCredentials: this.configuration.withCredentials,
headers: headers
headers: {...headers, ...{{nickname}}RequestConfig?.options?.headers},
...{{nickname}}RequestConfig?.options,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldnt the lines be swapped to make sure the original headers are not completely overwritten if some headers are set in the options?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes right !

}
);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/* tslint:disable:no-unused-variable member-ordering */

import { HttpService, Injectable, Optional } from '@nestjs/common';
import { AxiosResponse } from 'axios';
import { AxiosRequestConfig, AxiosResponse } from 'axios';
import { Observable, from, of, switchMap } from 'rxjs';
import { ApiResponse } from '../model/apiResponse';
import { Pet } from '../model/pet';
Expand Down Expand Up @@ -49,9 +49,10 @@ export class PetService {
* @param pet Pet object that needs to be added to the store
* @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.
* @param {*} [addPetRequestConfig.options] Override http request option.
*/
public addPet(pet: Pet, ): Observable<AxiosResponse<Pet>>;
public addPet(pet: Pet, ): Observable<any> {
public addPet(pet: Pet, addPetRequestConfig?: { options?: AxiosRequestConfig }): Observable<AxiosResponse<Pet>>;
public addPet(pet: Pet, addPetRequestConfig?: { options?: AxiosRequestConfig }): Observable<any> {
if (pet === null || pet === undefined) {
throw new Error('Required parameter pet was null or undefined when calling addPet.');
}
Expand Down Expand Up @@ -96,7 +97,8 @@ export class PetService {
pet,
{
withCredentials: this.configuration.withCredentials,
headers: headers
headers: {...headers, ...addPetRequestConfig?.options?.headers},
...addPetRequestConfig?.options,
}
);
})
Expand All @@ -109,9 +111,10 @@ export class PetService {
* @param apiKey
* @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.
* @param {*} [deletePetRequestConfig.options] Override http request option.
*/
public deletePet(petId: number, apiKey?: string, ): Observable<AxiosResponse<any>>;
public deletePet(petId: number, apiKey?: string, ): Observable<any> {
public deletePet(petId: number, apiKey?: string, deletePetRequestConfig?: { options?: AxiosRequestConfig }): Observable<AxiosResponse<any>>;
public deletePet(petId: number, apiKey?: string, deletePetRequestConfig?: { options?: AxiosRequestConfig }): Observable<any> {
if (petId === null || petId === undefined) {
throw new Error('Required parameter petId was null or undefined when calling deletePet.');
}
Expand Down Expand Up @@ -150,7 +153,8 @@ export class PetService {
return this.httpClient.delete<any>(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`,
{
withCredentials: this.configuration.withCredentials,
headers: headers
headers: {...headers, ...deletePetRequestConfig?.options?.headers},
...deletePetRequestConfig?.options,
}
);
})
Expand All @@ -162,9 +166,10 @@ export class PetService {
* @param status Status values that need to be considered for filter
* @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.
* @param {*} [findPetsByStatusRequestConfig.options] Override http request option.
*/
public findPetsByStatus(status: Array<'available' | 'pending' | 'sold'>, ): Observable<AxiosResponse<Array<Pet>>>;
public findPetsByStatus(status: Array<'available' | 'pending' | 'sold'>, ): Observable<any> {
public findPetsByStatus(status: Array<'available' | 'pending' | 'sold'>, findPetsByStatusRequestConfig?: { options?: AxiosRequestConfig }): Observable<AxiosResponse<Array<Pet>>>;
public findPetsByStatus(status: Array<'available' | 'pending' | 'sold'>, findPetsByStatusRequestConfig?: { options?: AxiosRequestConfig }): Observable<any> {
if (status === null || status === undefined) {
throw new Error('Required parameter status was null or undefined when calling findPetsByStatus.');
}
Expand Down Expand Up @@ -208,7 +213,8 @@ export class PetService {
{
params: queryParameters,
withCredentials: this.configuration.withCredentials,
headers: headers
headers: {...headers, ...findPetsByStatusRequestConfig?.options?.headers},
...findPetsByStatusRequestConfig?.options,
}
);
})
Expand All @@ -220,9 +226,10 @@ export class PetService {
* @param tags Tags to filter by
* @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.
* @param {*} [findPetsByTagsRequestConfig.options] Override http request option.
*/
public findPetsByTags(tags: Array<string>, ): Observable<AxiosResponse<Array<Pet>>>;
public findPetsByTags(tags: Array<string>, ): Observable<any> {
public findPetsByTags(tags: Array<string>, findPetsByTagsRequestConfig?: { options?: AxiosRequestConfig }): Observable<AxiosResponse<Array<Pet>>>;
public findPetsByTags(tags: Array<string>, findPetsByTagsRequestConfig?: { options?: AxiosRequestConfig }): Observable<any> {
if (tags === null || tags === undefined) {
throw new Error('Required parameter tags was null or undefined when calling findPetsByTags.');
}
Expand Down Expand Up @@ -266,7 +273,8 @@ export class PetService {
{
params: queryParameters,
withCredentials: this.configuration.withCredentials,
headers: headers
headers: {...headers, ...findPetsByTagsRequestConfig?.options?.headers},
...findPetsByTagsRequestConfig?.options,
}
);
})
Expand All @@ -278,9 +286,10 @@ export class PetService {
* @param petId ID of pet to return
* @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.
* @param {*} [getPetByIdRequestConfig.options] Override http request option.
*/
public getPetById(petId: number, ): Observable<AxiosResponse<Pet>>;
public getPetById(petId: number, ): Observable<any> {
public getPetById(petId: number, getPetByIdRequestConfig?: { options?: AxiosRequestConfig }): Observable<AxiosResponse<Pet>>;
public getPetById(petId: number, getPetByIdRequestConfig?: { options?: AxiosRequestConfig }): Observable<any> {
if (petId === null || petId === undefined) {
throw new Error('Required parameter petId was null or undefined when calling getPetById.');
}
Expand Down Expand Up @@ -316,7 +325,8 @@ export class PetService {
return this.httpClient.get<Pet>(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`,
{
withCredentials: this.configuration.withCredentials,
headers: headers
headers: {...headers, ...getPetByIdRequestConfig?.options?.headers},
...getPetByIdRequestConfig?.options,
}
);
})
Expand All @@ -328,9 +338,10 @@ export class PetService {
* @param pet Pet object that needs to be added to the store
* @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.
* @param {*} [updatePetRequestConfig.options] Override http request option.
*/
public updatePet(pet: Pet, ): Observable<AxiosResponse<Pet>>;
public updatePet(pet: Pet, ): Observable<any> {
public updatePet(pet: Pet, updatePetRequestConfig?: { options?: AxiosRequestConfig }): Observable<AxiosResponse<Pet>>;
public updatePet(pet: Pet, updatePetRequestConfig?: { options?: AxiosRequestConfig }): Observable<any> {
if (pet === null || pet === undefined) {
throw new Error('Required parameter pet was null or undefined when calling updatePet.');
}
Expand Down Expand Up @@ -375,7 +386,8 @@ export class PetService {
pet,
{
withCredentials: this.configuration.withCredentials,
headers: headers
headers: {...headers, ...updatePetRequestConfig?.options?.headers},
...updatePetRequestConfig?.options,
}
);
})
Expand All @@ -389,9 +401,10 @@ export class PetService {
* @param status Updated status of the pet
* @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.
* @param {*} [updatePetWithFormRequestConfig.options] Override http request option.
*/
public updatePetWithForm(petId: number, name?: string, status?: string, ): Observable<AxiosResponse<any>>;
public updatePetWithForm(petId: number, name?: string, status?: string, ): Observable<any> {
public updatePetWithForm(petId: number, name?: string, status?: string, updatePetWithFormRequestConfig?: { options?: AxiosRequestConfig }): Observable<AxiosResponse<any>>;
public updatePetWithForm(petId: number, name?: string, status?: string, updatePetWithFormRequestConfig?: { options?: AxiosRequestConfig }): Observable<any> {
if (petId === null || petId === undefined) {
throw new Error('Required parameter petId was null or undefined when calling updatePetWithForm.');
}
Expand Down Expand Up @@ -449,7 +462,8 @@ export class PetService {
convertFormParamsToString ? formParams!.toString() : formParams!,
{
withCredentials: this.configuration.withCredentials,
headers: headers
headers: {...headers, ...updatePetWithFormRequestConfig?.options?.headers},
...updatePetWithFormRequestConfig?.options,
}
);
})
Expand All @@ -463,9 +477,10 @@ export class PetService {
* @param file file to upload
* @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.
* @param {*} [uploadFileRequestConfig.options] Override http request option.
*/
public uploadFile(petId: number, additionalMetadata?: string, file?: Blob, ): Observable<AxiosResponse<ApiResponse>>;
public uploadFile(petId: number, additionalMetadata?: string, file?: Blob, ): Observable<any> {
public uploadFile(petId: number, additionalMetadata?: string, file?: Blob, uploadFileRequestConfig?: { options?: AxiosRequestConfig }): Observable<AxiosResponse<ApiResponse>>;
public uploadFile(petId: number, additionalMetadata?: string, file?: Blob, uploadFileRequestConfig?: { options?: AxiosRequestConfig }): Observable<any> {
if (petId === null || petId === undefined) {
throw new Error('Required parameter petId was null or undefined when calling uploadFile.');
}
Expand Down Expand Up @@ -528,7 +543,8 @@ export class PetService {
convertFormParamsToString ? formParams!.toString() : formParams!,
{
withCredentials: this.configuration.withCredentials,
headers: headers
headers: {...headers, ...uploadFileRequestConfig?.options?.headers},
...uploadFileRequestConfig?.options,
}
);
})
Expand Down
Loading
Loading