export interface Product { id : string ; name : string ; price : number ; description : string ; imageURL : string ; imageAlt : string ; imageCredit : string ; } export async function getProducts ( ) { const results = await fetch ( "/products.json" ) ; const products = results . json ( ) ; return products ; } export type CartItems = { [ productID ] : number ; } ; export type CheckoutResponse = { success : boolean ; error ? : string ; } ; export async function checkout ( items ) { const modifier = Object . keys ( items ) . length > 0 ? "success" : "error" ; const url = `/checkout-${ modifier }.json` ; await sleep ( 500 ) ; const response = await fetch ( url , { method : "POST" , body : JSON . stringify ( items ) , } ) ; const data = await response . json ( ) ; if ( ! data . success ) { throw new Error ( data . error ) ; } return data as CheckoutResponse ; } const sleep = ( time ) => new Promise ( ( res ) => setTimeout ( res , time ) ) ;