118 lines
3.5 KiB
TypeScript
118 lines
3.5 KiB
TypeScript
import { medusaFetch } from "./medusa";
|
|
|
|
export interface MedusaCustomer {
|
|
id: string;
|
|
email: string;
|
|
first_name: string | null;
|
|
last_name: string | null;
|
|
}
|
|
|
|
export async function loginCustomer(email: string, password: string): Promise<string> {
|
|
const data = await medusaFetch("/auth/customer/emailpass", {
|
|
method: "POST",
|
|
body: JSON.stringify({ email, password }),
|
|
});
|
|
return data.token;
|
|
}
|
|
|
|
export async function registerCustomer(input: {
|
|
email: string;
|
|
password: string;
|
|
first_name: string;
|
|
last_name: string;
|
|
}): Promise<string> {
|
|
const registration = await medusaFetch("/auth/customer/emailpass/register", {
|
|
method: "POST",
|
|
body: JSON.stringify({ email: input.email, password: input.password }),
|
|
});
|
|
|
|
await medusaFetch("/store/customers", {
|
|
method: "POST",
|
|
headers: { Authorization: `Bearer ${registration.token}` },
|
|
body: JSON.stringify({ email: input.email, first_name: input.first_name, last_name: input.last_name }),
|
|
});
|
|
|
|
// The registration token isn't linked to the new customer yet (Medusa
|
|
// issues it before the auth identity <-> customer link exists), so it
|
|
// can't authenticate customer-scoped routes. Log in again to get a token
|
|
// that actually resolves to this customer.
|
|
return loginCustomer(input.email, input.password);
|
|
}
|
|
|
|
export async function getCustomerMe(token: string): Promise<MedusaCustomer> {
|
|
const data = await medusaFetch("/store/customers/me", {
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
});
|
|
return data.customer;
|
|
}
|
|
|
|
export interface MedusaOrderSummary {
|
|
id: string;
|
|
display_id: number;
|
|
status: string;
|
|
total: number;
|
|
currency_code: string;
|
|
created_at: string;
|
|
}
|
|
|
|
export async function listMyOrders(token: string): Promise<MedusaOrderSummary[]> {
|
|
const data = await medusaFetch("/store/orders?limit=50&order=-created_at", {
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
});
|
|
return data.orders;
|
|
}
|
|
|
|
export interface MedusaOrderDetailItem {
|
|
title: string;
|
|
quantity: number;
|
|
unit_price: number;
|
|
total: number;
|
|
thumbnail: string | null;
|
|
}
|
|
|
|
export interface MedusaOrderDetail {
|
|
id: string;
|
|
display_id: number;
|
|
email: string;
|
|
created_at: string;
|
|
currency_code: string;
|
|
items: MedusaOrderDetailItem[];
|
|
shipping_address: {
|
|
first_name?: string | null;
|
|
last_name?: string | null;
|
|
address_1?: string | null;
|
|
postal_code?: string | null;
|
|
city?: string | null;
|
|
country_code?: string | null;
|
|
} | null;
|
|
item_total: number;
|
|
shipping_total: number;
|
|
total: number;
|
|
fulfillment_status: string;
|
|
fulfillments: {
|
|
packed_at: string | null;
|
|
shipped_at: string | null;
|
|
delivered_at: string | null;
|
|
canceled_at: string | null;
|
|
labels: { tracking_number: string; tracking_url: string }[];
|
|
}[];
|
|
}
|
|
|
|
export async function getMyOrder(token: string, id: string): Promise<MedusaOrderDetail> {
|
|
const data = await medusaFetch(`/store/customers/me/orders/${id}`, {
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
});
|
|
return data.order;
|
|
}
|
|
|
|
// Links a (possibly guest-created) cart to the now-logged-in customer, so
|
|
// the resulting order shows up under "Meine Bestellungen" — carts only get
|
|
// a customer_id automatically at creation time if already authenticated,
|
|
// which isn't the case when someone adds to cart before logging in.
|
|
export async function transferCartToCustomer(cartId: string, token: string): Promise<void> {
|
|
await medusaFetch(`/store/carts/${cartId}/customer`, {
|
|
method: "POST",
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
});
|
|
}
|