import {z} from 'zod' export type CreateApiPrintJobCostCalculationsArgs = {body: {line_items: Array<{page_count: number, pod_package_id: string, quantity: number}>, shipping_address: {city: string, country_code: string, email?: string | undefined, is_business?: boolean | undefined, name?: string | undefined, organization?: string | undefined, phone_number: string, postcode: string, state_code?: string | undefined, street1: string, street2?: string | undefined, title?: ('MR' | 'MISS' | 'MRS' | 'MS' | 'DR') | undefined}, shipping_option: 'MAIL' | 'PRIORITY_MAIL' | 'GROUND_HD' | 'GROUND_BUS' | 'GROUND' | 'EXPEDITED' | 'EXPRESS'}}; export type CreateApiPrintJobCostCalculationsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiPrintJobCostCalculationsResponse = z.object({shipping_address: z.object({city: z.string().optional(), country_code: z.string().min(2).max(2).optional(), email: z.string().optional(), is_business: z.boolean().optional(), name: z.string().optional(), organization: z.string().optional(), phone_number: z.string().max(20).optional(), postcode: z.string().max(64).optional(), state_code: z.string().optional(), street1: z.string().optional(), street2: z.string().optional(), title: z.enum(["MR", "MISS", "MRS", "MS", "DR"]).optional(), warnings: z.array(z.object({type: z.string().optional(), code: z.string().optional(), path: z.string().optional(), message: z.string().optional()})).optional(), suggested_address: z.object({country_code: z.string().optional(), state_code: z.string().optional(), postcode: z.string().optional(), city: z.string().optional(), street1: z.string().optional(), street2: z.string().optional()}).optional()}).optional(), currency: z.string(), line_item_costs: z.array(z.object({cost_excl_discounts: z.string(), discounts: z.array(z.object({amount: z.string(), description: z.string()})), quantity: z.number(), tax_rate: z.string(), total_cost_excl_discounts: z.string(), total_cost_excl_tax: z.string(), total_cost_incl_tax: z.string(), total_tax: z.string(), unit_tier_cost: z.string().optional()})), shipping_cost: z.object({tax_rate: z.string().optional(), total_cost_excl_tax: z.string().optional(), total_cost_incl_tax: z.string().optional(), total_tax: z.string().optional()}), fulfillment_cost: z.object({total_cost_excl_tax: z.string().optional(), total_cost_incl_tax: z.string().optional(), total_tax: z.string().optional(), tax_rate: z.unknown()}).optional(), total_cost_excl_tax: z.string(), total_cost_incl_tax: z.string(), total_discount_amount: z.string(), total_tax: z.string(), fees: z.array(z.object({currency: z.string().optional(), fee_type: z.string().optional(), sku: z.string().optional(), tax_rate: z.string().optional(), total_cost_excl_tax: z.string().optional(), total_cost_incl_tax: z.string().optional(), total_tax: z.string().optional()})).optional()}); 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 }; /** * Create a Print-Job cost calculation * * POST /print-job-cost-calculations/ */ export const createApiPrintJobCostCalculations = async (args: CreateApiPrintJobCostCalculationsArgs, options?: CreateApiPrintJobCostCalculationsOptions) => { 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(/\/+$/, '')}/print-job-cost-calculations/${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(createApiPrintJobCostCalculationsResponse, await res.json()) };