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 type { ApiRequestInstance, RequestResponse } from "~/lib/api/types/non-generated";
import { AdminAPI, PublicApi, UserApi } from "~/lib/api"; import { AdminAPI, PublicApi, UserApi } from "~/lib/api";
import { PublicExploreApi } from "~/lib/api/client-public"; import { PublicExploreApi } from "~/lib/api/client-public";
const request = { const request = {
async safe<T, U>( async safe<T, U>(
funcCall: (url: string, data: U) => Promise<AxiosResponse<T>>, funcCall: (url: string, data: U, config?: AxiosRequestConfig) => Promise<AxiosResponse<T>>,
url: string, url: string,
data: U, data: U,
config?: AxiosRequestConfig,
): Promise<RequestResponse<T>> { ): Promise<RequestResponse<T>> {
let error = null; 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); console.log(e);
// Insert Generic Error Handling Here // Insert Generic Error Handling Here
error = e; error = e;
@ -22,9 +23,9 @@ const request = {
function getRequests(axiosInstance: AxiosInstance): ApiRequestInstance { function getRequests(axiosInstance: AxiosInstance): ApiRequestInstance {
return { return {
async get<T>(url: string, params = {}): Promise<RequestResponse<T>> { async get<T>(url: string, params = {}, config?: AxiosRequestConfig): Promise<RequestResponse<T>> {
let error = null; 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; error = e;
}); });
if (response != null) { if (response != null) {
@ -33,20 +34,20 @@ function getRequests(axiosInstance: AxiosInstance): ApiRequestInstance {
return { response: null, error, data: null }; return { response: null, error, data: null };
}, },
async post<T, U>(url: string, data: U) { async post<T, U>(url: string, data: U, config?: AxiosRequestConfig) {
return await request.safe<T, U>(axiosInstance.post, url, data); return await request.safe<T, U>(axiosInstance.post, url, data, config);
}, },
async put<T, U = T>(url: string, data: U) { async put<T, U = T>(url: string, data: U, config?: AxiosRequestConfig) {
return await request.safe<T, U>(axiosInstance.put, url, data); return await request.safe<T, U>(axiosInstance.put, url, data, config);
}, },
async patch<T, U = Partial<T>>(url: string, data: U) { async patch<T, U = Partial<T>>(url: string, data: U, config?: AxiosRequestConfig) {
return await request.safe<T, U>(axiosInstance.patch, url, data); return await request.safe<T, U>(axiosInstance.patch, url, data, config);
}, },
async delete<T>(url: string) { async delete<T>(url: string, config?: AxiosRequestConfig) {
return await request.safe<T, undefined>(axiosInstance.delete, url, undefined); 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]>> }; export type NoUndefinedField<T> = { [P in keyof T]-?: NoUndefinedField<NonNullable<T[P]>> };
@ -9,11 +9,11 @@ export interface RequestResponse<T> {
} }
export interface ApiRequestInstance { export interface ApiRequestInstance {
get<T>(url: string, data?: unknown): Promise<RequestResponse<T>>; get<T>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<RequestResponse<T>>;
post<T>(url: string, data: unknown): Promise<RequestResponse<T>>; post<T>(url: string, data: unknown, config?: AxiosRequestConfig): Promise<RequestResponse<T>>;
put<T, U = T>(url: string, data: U): 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): Promise<RequestResponse<T>>; patch<T, U = Partial<T>>(url: string, data: U, config?: AxiosRequestConfig): Promise<RequestResponse<T>>;
delete<T>(url: string): Promise<RequestResponse<T>>; delete<T>(url: string, config?: AxiosRequestConfig): Promise<RequestResponse<T>>;
} }
export interface PaginationData<T> { export interface PaginationData<T> {

View file

@ -169,7 +169,7 @@ export class RecipeAPI extends BaseCRUDAPI<CreateRecipe, Recipe, Recipe> {
apiRoute = `${apiRoute}?translateLanguage=${translateLanguage}`; 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>) { async parseIngredients(parser: Parser, ingredients: Array<string>) {