import {z} from 'zod' export type RequestId = string; export type QuoteId = string; export type GetApiRequestsRequestIdQuotesQuoteIdArgs = {request_id: RequestId, quote_id: QuoteId}; export type GetApiRequestsRequestIdQuotesQuoteIdOptions = {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 }; /** * Get quote * * GET /requests/{request_id}/quotes/{quote_id} */ export const getApiRequestsRequestIdQuotesQuoteId = async (args: GetApiRequestsRequestIdQuotesQuoteIdArgs, options?: GetApiRequestsRequestIdQuotesQuoteIdOptions) => { const {baseUrl = "http://localhost:8000", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/requests/${encodeURIComponent(String(args.request_id))}/quotes/${encodeURIComponent(String(args.quote_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) 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()) };