Initial commit: redefined-designs storefront
This commit is contained in:
Executable
+69
@@ -0,0 +1,69 @@
|
||||
export interface Customer {
|
||||
id: number;
|
||||
email: string;
|
||||
name: string | null;
|
||||
email_verified: boolean;
|
||||
marketing_consent: boolean;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface OrderHistoryItem {
|
||||
id: number;
|
||||
processor: string;
|
||||
amount_cents: number;
|
||||
status: string;
|
||||
created_at: string;
|
||||
item_name: string;
|
||||
}
|
||||
|
||||
async function handle<T>(res: Response): Promise<T> {
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
throw new Error(data.error || 'Request failed');
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export function registerCustomer(email: string, password: string, name: string, marketingConsent: boolean): Promise<Customer> {
|
||||
return fetch('/api/customers/register', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ email, password, name, marketingConsent })
|
||||
}).then(res => handle<Customer>(res));
|
||||
}
|
||||
|
||||
export function loginCustomer(email: string, password: string): Promise<Customer> {
|
||||
return fetch('/api/customers/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ email, password })
|
||||
}).then(res => handle<Customer>(res));
|
||||
}
|
||||
|
||||
export function logoutCustomer(): Promise<void> {
|
||||
return fetch('/api/customers/logout', { method: 'POST' }).then(() => undefined);
|
||||
}
|
||||
|
||||
export function fetchMe(): Promise<Customer | null> {
|
||||
return fetch('/api/customers/me').then(res => (res.ok ? res.json() : null));
|
||||
}
|
||||
|
||||
export function updateConsent(marketingConsent: boolean): Promise<void> {
|
||||
return fetch('/api/customers/me/consent', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ marketingConsent })
|
||||
}).then(() => undefined);
|
||||
}
|
||||
|
||||
export function fetchMyOrders(): Promise<OrderHistoryItem[]> {
|
||||
return fetch('/api/customers/me/orders').then(res => handle<OrderHistoryItem[]>(res));
|
||||
}
|
||||
|
||||
export function deleteMyAccount(): Promise<void> {
|
||||
return fetch('/api/customers/me', { method: 'DELETE' }).then(() => undefined);
|
||||
}
|
||||
|
||||
export function exportMyData(): void {
|
||||
window.location.href = '/api/customers/me/export';
|
||||
}
|
||||
Reference in New Issue
Block a user