import {z} from 'zod' export type CreateUpSellOrderDto = {parentOrderId: number, productIds: Array, upSellSource?: ('ORDER' | 'EMAIL' | 'ADMIN' | 'Document Link') | undefined, orderLng?: ('en' | 'fr' | 'de' | 'es') | undefined, suburb?: string | undefined, shippingAddress1?: string | undefined, shippingAddress2?: string | undefined, shippingCity?: string | undefined, shippingPostcode?: string | undefined, shippingCounty?: string | undefined, shippingPhone?: string | undefined, shippingFirstName?: string | undefined, shippingLastName?: string | undefined, hasSwimmingPool?: boolean | undefined, applicantFirstName?: string | undefined, applicantLastName?: string | undefined, instrumentCopyNumber?: string | undefined, productType?: ('lim' | 'pf' | 'both') | undefined}; export type CreateApiOrdersUpsellArgs = {body: CreateUpSellOrderDto}; export type CreateApiOrdersUpsellOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const orderCreateUpdateResponseDto = z.object({orderId: z.number(), customerEmail: z.string(), emailValid: z.boolean(), emailValidationStatus: z.string().optional(), isNewCustomer: z.boolean().optional(), documentAccessLink: z.string().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 customer upSell order * * POST /orders/upsell */ export const createApiOrdersUpsell = async (args: CreateApiOrdersUpsellArgs, options?: CreateApiOrdersUpsellOptions) => { const {baseUrl = "https://api.findmydocument.net", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/orders/upsell${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(orderCreateUpdateResponseDto, await res.json()) };