homebox/frontend/lib/api/classes/items.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

import { BaseAPI, route } from "../base";
import { parseDate } from "../base/base-api";
import { ItemCreate, ItemOut, ItemSummary, ItemUpdate } from "../types/data-contracts";
import { Results } from "./types";
2022-09-03 09:17:57 +00:00
export class ItemsApi extends BaseAPI {
getAll() {
return this.http.get<Results<ItemOut>>({ url: route("/items") });
2022-09-03 09:17:57 +00:00
}
create(item: ItemCreate) {
return this.http.post<ItemCreate, ItemSummary>({ url: route("/items"), body: item });
2022-09-03 09:17:57 +00:00
}
async get(id: string) {
const payload = await this.http.get<ItemOut>({ url: route(`/items/${id}`) });
2022-09-07 05:58:59 +00:00
if (!payload.data) {
return payload;
}
// Parse Date Types
payload.data = parseDate(payload.data, ["purchaseTime", "soldTime", "warrantyExpires"]);
2022-09-07 05:58:59 +00:00
return payload;
2022-09-03 09:17:57 +00:00
}
delete(id: string) {
2022-09-09 06:05:23 +00:00
return this.http.delete<void>({ url: route(`/items/${id}`) });
2022-09-03 09:17:57 +00:00
}
async update(id: string, item: ItemUpdate) {
const payload = await this.http.put<ItemCreate, ItemOut>({
url: route(`/items/${id}`),
body: this.dropFields(item),
});
if (!payload.data) {
return payload;
}
payload.data = parseDate(payload.data, ["purchaseTime", "soldTime", "warrantyExpires"]);
return payload;
2022-09-09 06:05:23 +00:00
}
import(file: File) {
2022-09-09 06:05:23 +00:00
const formData = new FormData();
formData.append("csv", file);
2022-09-09 06:05:23 +00:00
return this.http.post<FormData, void>({ url: route("/items/import"), data: formData });
2022-09-03 09:17:57 +00:00
}
}