new API class method for patch operations

This commit is contained in:
Hayden 2023-06-02 11:08:33 -08:00
parent f5daf96e1e
commit 00b60eb005
No known key found for this signature in database
GPG key ID: 17CF79474E257545
3 changed files with 26 additions and 1 deletions

View file

@ -4,6 +4,7 @@ import {
ItemAttachmentUpdate,
ItemCreate,
ItemOut,
ItemPatch,
ItemSummary,
ItemUpdate,
MaintenanceEntry,
@ -138,6 +139,20 @@ export class ItemsApi extends BaseAPI {
return payload;
}
async patch(id: string, item: ItemPatch) {
const resp = await this.http.patch<ItemPatch, ItemOut>({
url: route(`/items/${id}`),
body: this.dropFields(item),
});
if (!resp.data) {
return resp;
}
resp.data = parseDate(resp.data, ["purchaseTime", "soldTime", "warrantyExpires"]);
return resp;
}
import(file: File | Blob) {
const formData = new FormData();
formData.append("csv", file);

View file

@ -119,6 +119,11 @@ export interface ItemOut {
warrantyExpires: Date | string;
}
export interface ItemPatch {
id: string;
quantity: number | null;
}
export interface ItemSummary {
archived: boolean;
createdAt: Date | string;

View file

@ -3,6 +3,7 @@ export enum Method {
POST = "POST",
PUT = "PUT",
DELETE = "DELETE",
PATCH = "PATCH",
}
export type ResponseInterceptor = (r: Response, rq?: RequestInit) => void;
@ -57,12 +58,16 @@ export class Requests {
return this.do<U>(Method.PUT, args);
}
public patch<T, U>(args: RequestArgs<T>): Promise<TResponse<U>> {
return this.do<U>(Method.PATCH, args);
}
public delete<T>(args: RequestArgs<T>): Promise<TResponse<T>> {
return this.do<T>(Method.DELETE, args);
}
private methodSupportsBody(method: Method): boolean {
return method === Method.POST || method === Method.PUT;
return method === Method.POST || method === Method.PUT || method === Method.PATCH;
}
private async do<T>(method: Method, rargs: RequestArgs<unknown>): Promise<TResponse<T>> {