2021-08-01 19:24:47 -08:00
|
|
|
import { ApiRequestInstance } from "~/types/api";
|
|
|
|
|
2021-08-02 22:15:11 -08:00
|
|
|
export interface CrudAPIInterface {
|
|
|
|
requests: ApiRequestInstance;
|
|
|
|
|
|
|
|
// Route Properties / Methods
|
|
|
|
baseRoute: string;
|
2021-08-06 16:28:12 -08:00
|
|
|
itemRoute(itemId: string | number): string;
|
2021-08-02 22:15:11 -08:00
|
|
|
|
|
|
|
// Methods
|
2021-08-01 19:24:47 -08:00
|
|
|
}
|
|
|
|
|
2021-08-07 15:12:25 -08:00
|
|
|
export abstract class BaseAPI {
|
2021-08-02 22:15:11 -08:00
|
|
|
requests: ApiRequestInstance;
|
|
|
|
|
|
|
|
constructor(requests: ApiRequestInstance) {
|
|
|
|
this.requests = requests;
|
|
|
|
}
|
2021-08-07 15:12:25 -08:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:22:02 +02:00
|
|
|
export abstract class BaseCRUDAPI<CreateType, ReadType, UpdateType=CreateType> extends BaseAPI implements CrudAPIInterface {
|
2021-08-07 15:12:25 -08:00
|
|
|
abstract baseRoute: string;
|
|
|
|
abstract itemRoute(itemId: string | number): string;
|
2021-08-02 22:15:11 -08:00
|
|
|
|
2021-12-05 11:55:46 -09:00
|
|
|
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, {
|
2021-09-12 11:05:09 -08:00
|
|
|
params: { start, limit, ...params },
|
2021-08-02 22:15:11 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:22:02 +02:00
|
|
|
async createOne(payload: CreateType) {
|
|
|
|
return await this.requests.post<ReadType>(this.baseRoute, payload);
|
2021-08-06 16:28:12 -08:00
|
|
|
}
|
|
|
|
|
2021-08-22 15:23:45 -08:00
|
|
|
async getOne(itemId: string | number) {
|
2022-05-21 21:22:02 +02:00
|
|
|
return await this.requests.get<ReadType>(this.itemRoute(itemId));
|
2021-08-02 22:15:11 -08:00
|
|
|
}
|
|
|
|
|
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);
|
2021-08-02 22:15:11 -08:00
|
|
|
}
|
|
|
|
|
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);
|
2021-08-02 22:15:11 -08:00
|
|
|
}
|
|
|
|
|
2021-08-06 16:28:12 -08:00
|
|
|
async deleteOne(itemId: string | number) {
|
2022-05-21 21:22:02 +02:00
|
|
|
return await this.requests.delete<ReadType>(this.itemRoute(itemId));
|
2021-08-02 22:15:11 -08:00
|
|
|
}
|
|
|
|
}
|