import {z} from 'zod' export type OrderProductDto = {productId: number}; export type CreateUpdateOrderDto = {titleNumber?: string | undefined, instrumentCopyNumber?: string | undefined, customerEmail?: string | undefined, customerId: number, tenure?: string | undefined, address1?: string | undefined, flatUnit?: string | undefined, municipality?: string | undefined, registry?: string | undefined, cruIdufir?: string | undefined, cadastralRef?: string | undefined, propertyType?: string | undefined, ownerName?: string | undefined, floor?: string | undefined, door?: string | undefined, requestReason?: string | undefined, identificationType?: ('None' | 'NIE' | 'NIF') | undefined, identificationNumber?: string | undefined, nameAsPerIdentification?: string | undefined, city?: string | undefined, suburb?: string | undefined, county?: string | undefined, postCode?: string | undefined, country?: string | undefined, state?: string | undefined, currency?: string | undefined, totalAmount?: number | undefined, orderStatusId?: number | undefined, products: Array, shippingAddress1?: string | undefined, shippingAddress2?: string | undefined, shippingCity?: string | undefined, shippingPostcode?: string | undefined, shippingCounty?: string | undefined, shippingPhone?: string | undefined, shippingFirstName?: string | undefined, shippingLastName?: string | undefined, updateItWithAllLinkedOrders?: boolean | undefined, isExpressDelivery?: boolean | undefined, isRequestedPaperCopy?: boolean | undefined, utmCampaign?: string | undefined, utmTerm?: string | undefined, utmContent?: string | undefined, utmSource?: string | undefined, utmMedium?: string | undefined, hasSwimmingPool?: boolean | undefined, applicantFirstName?: string | undefined, applicantLastName?: string | undefined, orderLng?: ('en' | 'fr' | 'de' | 'es') | undefined, productType?: ('lim' | 'pf' | 'both') | undefined}; export type CreateApiOrdersArgs = {body: CreateUpdateOrderDto}; export type CreateApiOrdersOptions = {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 order * * POST /orders */ export const createApiOrders = async (args: CreateApiOrdersArgs, options?: CreateApiOrdersOptions) => { 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${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()) };