Help and docs
Sign in

demo/acme-web code browser

main 72fe66b
TypeScript · 23 lines · 841 B
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
export type Money = { minor: number; currency: string };

export type Charge = { id: string; amount: Money };

export type Product = { id: string; name: string; price: Money; imageURL: string };

async function request<T>(method: string, path: string, body?: unknown): Promise<T> {
  const res = await fetch("/api" + path, {
    method,
    headers: body ? { "Content-Type": "application/json" } : undefined,
    body: body ? JSON.stringify(body) : undefined,
  });
  if (!res.ok) throw new Error(method + " " + path + ": " + res.status);
  return res.json() as Promise<T>;
}

export function charge(cardToken: string, amount: Money): Promise<Charge> {
  return request<Charge>("POST", "/charges", { card_token: cardToken, amount });
}

export function listProducts(): Promise<Product[]> {
  return request<Product[]>("GET", "/products");
}