add optional axios config to requests

This commit is contained in:
Kuchenpirat 2025-06-28 11:57:20 +00:00
commit 2cdb66684b
3 changed files with 21 additions and 20 deletions

View file

@ -1,16 +1,17 @@
import type { AxiosInstance, AxiosResponse } from "axios";
import type { AxiosInstance, AxiosResponse, AxiosRequestConfig } from "axios";
import type { ApiRequestInstance, RequestResponse } from "~/lib/api/types/non-generated";
import { AdminAPI, PublicApi, UserApi } from "~/lib/api";
import { PublicExploreApi } from "~/lib/api/client-public";
const request = {
async safe<T, U>(
funcCall: (url: string, data: U) => Promise<AxiosResponse<T>>,
funcCall: (url: string, data: U, config?: AxiosRequestConfig) => Promise<AxiosResponse<T>>,
url: string,
data: U,
config?: AxiosRequestConfig,
): Promise<RequestResponse<T>> {
let error = null;
const response = await funcCall(url, data).catch(function (e) {
const response = await funcCall(url, data, config).catch(function (e) {
console.log(e);
// Insert Generic Error Handling Here
error = e;
@ -22,9 +23,9 @@ const request = {
function getRequests(axiosInstance: AxiosInstance): ApiRequestInstance {
return {
async get<T>(url: string, params = {}): Promise<RequestResponse<T>> {
async get<T>(url: string, params = {}, config?: AxiosRequestConfig): Promise<RequestResponse<T>> {
let error = null;
const response = await axiosInstance.get<T>(url, params).catch((e) => {
const response = await axiosInstance.get<T>(url, { ...config, params }).catch((e) => {
error = e;
});
if (response != null) {
@ -33,20 +34,20 @@ function getRequests(axiosInstance: AxiosInstance): ApiRequestInstance {
return { response: null, error, data: null };
},
async post<T, U>(url: string, data: U) {
return await request.safe<T, U>(axiosInstance.post, url, data);
async post<T, U>(url: string, data: U, config?: AxiosRequestConfig) {
return await request.safe<T, U>(axiosInstance.post, url, data, config);
},
async put<T, U = T>(url: string, data: U) {
return await request.safe<T, U>(axiosInstance.put, url, data);
async put<T, U = T>(url: string, data: U, config?: AxiosRequestConfig) {
return await request.safe<T, U>(axiosInstance.put, url, data, config);
},
async patch<T, U = Partial<T>>(url: string, data: U) {
return await request.safe<T, U>(axiosInstance.patch, url, data);
async patch<T, U = Partial<T>>(url: string, data: U, config?: AxiosRequestConfig) {
return await request.safe<T, U>(axiosInstance.patch, url, data, config);
},
async delete<T>(url: string) {
return await request.safe<T, undefined>(axiosInstance.delete, url, undefined);
async delete<T>(url: string, config?: AxiosRequestConfig) {
return await request.safe<T, undefined>(axiosInstance.delete, url, undefined, config);
},
};
}

View file

@ -1,4 +1,4 @@
import type { AxiosResponse } from "axios";
import type { AxiosRequestConfig, AxiosResponse } from "axios";
export type NoUndefinedField<T> = { [P in keyof T]-?: NoUndefinedField<NonNullable<T[P]>> };
@ -9,11 +9,11 @@ export interface RequestResponse<T> {
}
export interface ApiRequestInstance {
get<T>(url: string, data?: unknown): Promise<RequestResponse<T>>;
post<T>(url: string, data: unknown): Promise<RequestResponse<T>>;
put<T, U = T>(url: string, data: U): Promise<RequestResponse<T>>;
patch<T, U = Partial<T>>(url: string, data: U): Promise<RequestResponse<T>>;
delete<T>(url: string): Promise<RequestResponse<T>>;
get<T>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<RequestResponse<T>>;
post<T>(url: string, data: unknown, config?: AxiosRequestConfig): Promise<RequestResponse<T>>;
put<T, U = T>(url: string, data: U, config?: AxiosRequestConfig): Promise<RequestResponse<T>>;
patch<T, U = Partial<T>>(url: string, data: U, config?: AxiosRequestConfig): Promise<RequestResponse<T>>;
delete<T>(url: string, config?: AxiosRequestConfig): Promise<RequestResponse<T>>;
}
export interface PaginationData<T> {

View file

@ -169,7 +169,7 @@ export class RecipeAPI extends BaseCRUDAPI<CreateRecipe, Recipe, Recipe> {
apiRoute = `${apiRoute}?translateLanguage=${translateLanguage}`;
}
return await this.requests.post<string>(apiRoute, formData);
return await this.requests.post<string>(apiRoute, formData, { timeout: 120000 });
}
async parseIngredients(parser: Parser, ingredients: Array<string>) {