1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-07-20 21:59:40 +02:00
mealie/frontend/api/_base.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-08-01 19:24:47 -08:00
import { ApiRequestInstance } from "~/types/api";
export interface CrudAPIInterface {
requests: ApiRequestInstance;
// Route Properties / Methods
baseRoute: string;
itemRoute(itemId: string | number): string;
// Methods
2021-08-01 19:24:47 -08:00
}
export abstract class BaseAPI {
requests: ApiRequestInstance;
constructor(requests: ApiRequestInstance) {
this.requests = requests;
}
}
2022-05-21 21:22:02 +02:00
export abstract class BaseCRUDAPI<CreateType, ReadType, UpdateType=CreateType> extends BaseAPI implements CrudAPIInterface {
abstract baseRoute: string;
abstract itemRoute(itemId: string | number): string;
async getAll(start = 0, limit = 9999, params = {} as any) {
2022-05-21 21:22:02 +02:00
return await this.requests.get<ReadType[]>(this.baseRoute, {
params: { start, limit, ...params },
});
}
2022-05-21 21:22:02 +02:00
async createOne(payload: CreateType) {
return await this.requests.post<ReadType>(this.baseRoute, payload);
}
async getOne(itemId: string | number) {
2022-05-21 21:22:02 +02:00
return await this.requests.get<ReadType>(this.itemRoute(itemId));
}
2022-05-21 21:22:02 +02:00
async updateOne(itemId: string | number, payload: UpdateType) {
return await this.requests.put<ReadType, UpdateType>(this.itemRoute(itemId), payload);
}
2022-05-21 21:22:02 +02:00
async patchOne(itemId: string, payload: Partial<UpdateType>) {
return await this.requests.patch<ReadType, Partial<UpdateType>>(this.itemRoute(itemId), payload);
}
async deleteOne(itemId: string | number) {
2022-05-21 21:22:02 +02:00
return await this.requests.delete<ReadType>(this.itemRoute(itemId));
}
}