import {z} from 'zod' export type CreateApiUnassignedStopsImportArgs = {body: {unassignedStops: Array<{address: {addressName?: (string | null) | undefined, addressLineOne?: (string | null) | undefined, addressLineTwo?: (string | null) | undefined, city?: (string | null) | undefined, state?: (string | null) | undefined, zip?: (string | null) | undefined, country?: (string | null) | undefined, latitude?: (number | null) | undefined, longitude?: (number | null) | undefined}, recipient?: ({externalId?: (string | null) | undefined, email?: (string | null) | undefined, phone?: (string | null) | undefined, name?: (string | null) | undefined} | null) | undefined, orderInfo?: ({products?: Array | undefined, invoiceNumber?: (string | null) | undefined, sellerOrderId?: (string | null) | undefined, sellerName?: (string | null) | undefined, sellerWebsite?: (string | null) | undefined} | null) | undefined, paymentOnDelivery?: ({amount?: (number | null) | undefined, currency?: ('AED' | 'ARS' | 'AUD' | 'BRL' | 'CAD' | 'CHF' | 'CLP' | 'CNY' | 'COP' | 'DKK' | 'EGP' | 'EUR' | 'GBP' | 'HKD' | 'HUF' | 'ILS' | 'INR' | 'JPY' | 'KRW' | 'MYR' | 'MXN' | 'NOK' | 'NZD' | 'PEN' | 'RON' | 'RUB' | 'SAR' | 'SEK' | 'SGD' | 'TRY' | 'USD' | 'UYU' | 'ZAR' | null) | undefined} | null) | undefined, proofOfAttemptRequirements?: ({enabled?: (boolean | null) | undefined} | null) | undefined, allowedDrivers?: (Array | null) | undefined, activity?: ('delivery' | 'pickup' | null) | undefined, optimizationOrder?: ('first' | 'last' | 'default' | null) | undefined, packageCount?: (number | null) | undefined, weight?: ({amount: number, unit: 'kilogram' | 'pound' | 'metric-ton'} | null) | undefined, notes?: (string | null) | undefined, clientId?: (string | null) | undefined, barcodes?: Array | undefined, customProperties?: ({[key: string]: string | null} | null) | undefined, timing?: ({timeWindows?: (Array<{earliestAttemptTime: {hour: number, minute: number} | null, latestAttemptTime: {hour: number, minute: number} | null}>) | undefined, earliestAttemptTime?: ({hour: number, minute: number} | null) | undefined, latestAttemptTime?: ({hour: number, minute: number} | null) | undefined, estimatedAttemptDuration?: (number | null) | undefined} | null) | undefined}>, depot?: (string | null) | undefined}}; export type CreateApiUnassignedStopsImportOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiUnassignedStopsImportResponse = z.object({success: z.array(z.string()), failed: z.array(z.object({error: z.object({message: z.string(), code: z.string().optional(), param: z.string().optional()}), unassignedStop: z.object({address: z.object({addressName: z.string().min(1).max(255).nullable(), addressLineOne: z.string().min(1).max(255).nullable(), addressLineTwo: z.string().min(1).max(255).nullable(), city: z.string().min(1).max(100).nullable(), state: z.string().min(1).max(100).nullable(), zip: z.string().min(1).max(100).nullable(), country: z.string().min(1).max(100).nullable(), latitude: z.number().gte(-90).lte(90).nullable(), longitude: z.number().gte(-180).lte(180).nullable()}), recipient: z.object({externalId: z.string().min(1).max(255).nullable(), email: z.string().max(255).nullable(), phone: z.string().min(1).max(255).nullable(), name: z.string().min(1).max(255).nullable()}).nullable()})}))}); 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 }; /** * Batch import unassigned stops * * POST /unassignedStops:import */ export const createApiUnassignedStopsImport = async (args: CreateApiUnassignedStopsImportArgs, options?: CreateApiUnassignedStopsImportOptions) => { const {baseUrl = "https://api.spoke.com/public/v1", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/unassignedStops:import${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(createApiUnassignedStopsImportResponse, await res.json()) };