From 9383a2fb09ffb60cfe63683106945bd688affa59 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Wed, 1 Jun 2022 21:13:43 +0200 Subject: feat: Initial commit after clean slate --- src/wwwroot/scripts/api/products-api.ts | 59 +++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/wwwroot/scripts/api/products-api.ts (limited to 'src/wwwroot/scripts/api/products-api.ts') diff --git a/src/wwwroot/scripts/api/products-api.ts b/src/wwwroot/scripts/api/products-api.ts new file mode 100644 index 0000000..6069830 --- /dev/null +++ b/src/wwwroot/scripts/api/products-api.ts @@ -0,0 +1,59 @@ +import {CreateProductPayload, Product} from "./products-api.types"; + +export function createProduct(payload: CreateProductPayload): Promise { + return fetch("/api/products/create", { + headers: { + "Content-Type": "application/json;charset=utf-8" + }, + body: JSON.stringify(payload), + method: "post", + credentials: "include" + }); +} + +export function getProduct(id: string): Promise { + return fetch("/api/products/" + id, { + method: "get", + credentials: "include" + }); +} + +export function getProducts(): Promise { + return fetch("/api/products", { + method: "get", + credentials: "include" + }); +} + +export function uploadProductImages(files: Array): Promise { + if (files.length <= 0) throw new Error("files.length was " + files.length); + const data = new FormData(); + for (const file of files) + data.append("files", file); + + return fetch("/api/products/upload-images", { + method: "post", + body: data + }); +} + +export function deleteProduct(id: string): Promise { + return fetch("/api/products/" + id + "/delete", { + method: "delete", + credentials: "include" + }); +} + +export function updateProduct(data: Product): Promise { + if (!data.id) { + throw new Error("data.id was undefined"); + } + return fetch("/api/products/" + data.id + "/update", { + method: "post", + headers: { + "Content-Type": "application/json;charset=utf-8" + }, + body: JSON.stringify(data), + credentials: "include" + }); +} \ No newline at end of file -- cgit v1.3