import {z} from 'zod' export type CreateApiShippingOptionsArgs = {body: {currency?: string | undefined, line_items: Array<{page_count: number, pod_package_id: string, quantity: number}>, shipping_address: {country: string, city?: string | undefined, is_business?: boolean | undefined, is_postbox?: boolean | undefined, name?: string | undefined, organization?: string | undefined, phone_number?: string | undefined, postcode?: string | undefined, state?: string | undefined, street1?: string | undefined, street2?: string | undefined}}}; export type CreateApiShippingOptionsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiShippingOptionsResponse = z.array(z.object({business_only: z.boolean(), cost_excl_tax: z.string().optional(), currency: z.string().optional(), home_only: z.boolean(), id: z.number(), level: z.enum(["MAIL", "PRIORITY_MAIL", "GROUND_HD", "GROUND_BUS", "GROUND", "EXPEDITED", "EXPRESS"]), max_delivery_date: z.string(), max_dispatch_date: z.string().optional(), min_delivery_date: z.string(), min_dispatch_date: z.string().optional(), postbox_ok: z.boolean(), total_days_max: z.number().optional(), total_days_min: z.number().optional(), traceable: z.boolean(), transit_time: z.number()})); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Retrieve List of Shipping Options * * POST /shipping-options/ */ export const createApiShippingOptions = async (args: CreateApiShippingOptionsArgs, options?: CreateApiShippingOptionsOptions) => { const {baseUrl = "https://api.lulu.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/shipping-options/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiShippingOptionsResponse, await res.json()) };