import {z} from 'zod' export type RequestId = string; export type PostalAddress = {extended_address?: string | undefined, street_address?: string | undefined, address_locality?: string | undefined, address_region?: string | undefined, address_country?: string | undefined, postal_code?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, phone_number?: string | undefined}; export type Coordinates = {latitude: number, longitude: number}; export type Location = {postal_address: PostalAddress, coordinates?: Coordinates | undefined} | {coordinates: Coordinates, postal_address?: PostalAddress | undefined}; export type PaymentCredential = {type: string} | {[key: string]: unknown}; export type PaymentInstrument = {id: string, handler_id: string, type: string, billing_address?: ({extended_address?: string | undefined, street_address?: string | undefined, address_locality?: string | undefined, address_region?: string | undefined, address_country?: string | undefined, postal_code?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, phone_number?: string | undefined}) | undefined, credential?: PaymentCredential | undefined, display?: {[key: string]: unknown} | undefined} | {[key: string]: unknown}; export type Payment = {instruments: Array}; export type DeliveryQuote = {id: string, nonce: string, price: number, currency: string, pickup_location: Location, dropoff_location: Location, pickup_estimate: string, dropoff_estimate: string, expires_at?: string | undefined, payment: Payment}; export type CreateApiRequestsRequestIdQuotesArgs = {request_id: RequestId, body: DeliveryQuote}; export type CreateApiRequestsRequestIdQuotesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const postalAddress = z.object({extended_address: z.string().optional(), street_address: z.string().optional(), address_locality: z.string().optional(), address_region: z.string().optional(), address_country: z.string().optional(), postal_code: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), phone_number: z.string().optional()}); export const coordinates = z.object({latitude: z.number().gte(-90).lte(90), longitude: z.number().gte(-180).lte(180)}); export const location = z.union([z.object({postal_address: postalAddress, coordinates: coordinates.optional()}), z.object({coordinates: coordinates, postal_address: postalAddress.optional()})]); export const paymentCredential = z.object({type: z.string()}).and(z.record(z.string(), z.unknown())); export const paymentInstrument = z.object({id: z.string(), handler_id: z.string(), type: z.string(), billing_address: z.object({extended_address: z.string().optional(), street_address: z.string().optional(), address_locality: z.string().optional(), address_region: z.string().optional(), address_country: z.string().optional(), postal_code: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), phone_number: z.string().optional()}).optional(), credential: paymentCredential.optional(), display: z.record(z.string(), z.unknown()).optional()}).and(z.record(z.string(), z.unknown())); export const payment = z.object({instruments: z.array(paymentInstrument)}); export const deliveryQuote = z.object({id: z.string(), nonce: z.string(), price: z.number().int().gte(0), currency: z.string(), pickup_location: location, dropoff_location: location, pickup_estimate: z.string(), dropoff_estimate: z.string(), expires_at: z.string().optional(), payment: payment}); 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 quote * * POST /requests/{request_id}/quotes */ export const createApiRequestsRequestIdQuotes = async (args: CreateApiRequestsRequestIdQuotesArgs, options?: CreateApiRequestsRequestIdQuotesOptions) => { const {baseUrl = "http://localhost:8000", 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(/\/+$/, '')}/requests/${encodeURIComponent(String(args.request_id))}/quotes${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(deliveryQuote, await res.json()) };