import {z} from 'zod' export type PointLocationGeometry = {type: 'Point', coordinates: Array}; export type PointLocation1 = {geometry: PointLocationGeometry, accuracy?: (number | null) | undefined}; export type ImageAttachmentUpload = {type: 'image', caption: string, data: string, attachment_id?: (string | null) | undefined}; export type NoteAttachmentUpload = {type: 'note', data: string, attachment_id?: (string | null) | undefined}; export type SignatureAttachmentUpload = {type: 'signature', data: string, attachment_id?: (string | null) | undefined}; export type CustomOrderEvent = {type: string, recorded_at: string, metadata?: ({[key: string]: unknown} | null) | undefined, location?: (PointLocation1 | unknown) | undefined, address?: (string | null) | undefined, attachments?: (Array | null) | undefined}; export type CustomOrderEventRequest = {events: Array}; export type CreateApiOrdersOrderHandleEventsArgs = {order_handle: string, body: CustomOrderEventRequest}; export type CreateApiOrdersOrderHandleEventsOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiOrdersOrderHandleEventsResponse = z.unknown(); 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 }; /** * Add Custom Order Events * * POST /orders/{order_handle}/events * * Also accepts auth: TokenAuth */ export const createApiOrdersOrderHandleEvents = async (args: CreateApiOrdersOrderHandleEventsArgs, options?: CreateApiOrdersOrderHandleEventsOptions) => { const {baseUrl = "https://v3.api.hypertrack.com", 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(/\/+$/, '')}/orders/${encodeURIComponent(String(args.order_handle))}/events${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(createApiOrdersOrderHandleEventsResponse, await res.json()) };