import {z} from 'zod' 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 DeliveryRequest = {id: string, nonce: string, pickup_location: Location, dropoff_location: Location, pickup_time: string, dropoff_time: string, pickup_instructions?: string | undefined, dropoff_instructions?: string | undefined}; export type CreateApiRequestsArgs = {body: DeliveryRequest}; export type CreateApiRequestsOptions = {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 deliveryRequest = z.object({id: z.string(), nonce: z.string(), pickup_location: location, dropoff_location: location, pickup_time: z.string(), dropoff_time: z.string(), pickup_instructions: z.string().optional(), dropoff_instructions: 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 delivery request * * POST /requests */ export const createApiRequests = async (args: CreateApiRequestsArgs, options?: CreateApiRequestsOptions) => { 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${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(deliveryRequest, await res.json()) };