allow passing axios config in CRUD

This commit is contained in:
Michael Genson
2026-03-28 16:42:09 +00:00
parent 8f06889662
commit e9ca789a1d

View File

@@ -1,3 +1,4 @@
import type { AxiosRequestConfig } from "axios";
import type { Recipe } from "../types/recipe"; import type { Recipe } from "../types/recipe";
import type { ApiRequestInstance, PaginationData } from "~/lib/api/types/non-generated"; import type { ApiRequestInstance, PaginationData } from "~/lib/api/types/non-generated";
import { type QueryValue, route } from "~/lib/api/base/route"; import { type QueryValue, route } from "~/lib/api/base/route";
@@ -44,38 +45,38 @@ export abstract class BaseCRUDAPIReadOnly<ReadType>
return this.itemRouteFn(itemId); return this.itemRouteFn(itemId);
} }
async getAll(page = 1, perPage = -1, params = {} as Record<string, QueryValue>) { async getAll(page = 1, perPage = -1, params = {} as Record<string, QueryValue>, config?: AxiosRequestConfig) {
params = Object.fromEntries(Object.entries(params).filter(([_, v]) => v !== null && v !== undefined)); params = Object.fromEntries(Object.entries(params).filter(([_, v]) => v !== null && v !== undefined));
return await this.requests.get<PaginationData<ReadType>>(route(this.baseRoute, { page, perPage, ...params })); return await this.requests.get<PaginationData<ReadType>>(route(this.baseRoute, { page, perPage, ...params }), undefined, config);
} }
async getOne(itemId: string | number) { async getOne(itemId: string | number, config?: AxiosRequestConfig) {
return await this.requests.get<ReadType>(this.itemRoute(itemId)); return await this.requests.get<ReadType>(this.itemRoute(itemId), undefined, config);
} }
} }
export abstract class BaseCRUDAPI<CreateType, ReadType, UpdateType = CreateType> export abstract class BaseCRUDAPI<CreateType, ReadType, UpdateType = CreateType>
extends BaseCRUDAPIReadOnly<ReadType> extends BaseCRUDAPIReadOnly<ReadType>
implements CrudAPIInterface { implements CrudAPIInterface {
async createOne(payload: CreateType) { async createOne(payload: CreateType, config?: AxiosRequestConfig) {
return await this.requests.post<ReadType>(this.baseRoute, payload); return await this.requests.post<ReadType>(this.baseRoute, payload, config);
} }
async updateOne(itemId: string | number, payload: UpdateType) { async updateOne(itemId: string | number, payload: UpdateType, config?: AxiosRequestConfig) {
return await this.requests.put<ReadType, UpdateType>(this.itemRoute(itemId), payload); return await this.requests.put<ReadType, UpdateType>(this.itemRoute(itemId), payload, config);
} }
async patchOne(itemId: string, payload: Partial<UpdateType>) { async patchOne(itemId: string, payload: Partial<UpdateType>, config?: AxiosRequestConfig) {
return await this.requests.patch<ReadType, Partial<UpdateType>>(this.itemRoute(itemId), payload); return await this.requests.patch<ReadType, Partial<UpdateType>>(this.itemRoute(itemId), payload, config);
} }
async deleteOne(itemId: string | number) { async deleteOne(itemId: string | number, config?: AxiosRequestConfig) {
return await this.requests.delete<ReadType>(this.itemRoute(itemId)); return await this.requests.delete<ReadType>(this.itemRoute(itemId), config);
} }
async duplicateOne(itemId: string | number, newName: string | undefined) { async duplicateOne(itemId: string | number, newName: string | undefined, config?: AxiosRequestConfig) {
return await this.requests.post<Recipe>(`${this.itemRoute(itemId)}/duplicate`, { return await this.requests.post<Recipe>(`${this.itemRoute(itemId)}/duplicate`, {
name: newName, name: newName,
}); }, config);
} }
} }