aboutsummaryrefslogtreecommitdiffstats
path: root/src/wwwroot/scripts/api/products-api.ts
blob: 60698309cf8d564982e3bf5611204ac905053f56 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {CreateProductPayload, Product} from "./products-api.types";

export function createProduct(payload: CreateProductPayload): Promise<Response> {
    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<Response> {
    return fetch("/api/products/" + id, {
        method: "get",
        credentials: "include"
    });
}

export function getProducts(): Promise<Response> {
    return fetch("/api/products", {
        method: "get",
        credentials: "include"
    });
}

export function uploadProductImages(files: Array<File>): Promise<Response> {
    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<Response> {
    return fetch("/api/products/" + id + "/delete", {
        method: "delete",
        credentials: "include"
    });
}

export function updateProduct(data: Product): Promise<Response> {
    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"
    });
}