import {z} from 'zod' export type DspLocationPauseEventDto = {type: string, location_id: string, until_ms?: number | undefined}; export type DspLocationResumeEventDto = {type: string, location_id: string}; export type DspLocationMenuEventDto = {type: string, location_id: string, download_url?: string | undefined}; export type ObjectUpdateDto = {stream_id: string, is_active: boolean}; export type ObjectUpdatesDto = {items: Array, variations: Array, modifiers: Array}; export type DspLocationMenuUpdateEventDto = {type: string, location_id: string, object_updates: ObjectUpdatesDto}; export type DspLocationOrderAcceptedEventDto = {type: string, location_id: string, order_id?: string | undefined, estimated_ready_at_ms?: number | undefined}; export type OrderFailureDetails = {invalid_item_ids: Array, error_message?: string | undefined}; export type DspLocationOrderFailedEventDto = {type: string, location_id: string, order_id?: string | undefined, failure_reason?: ('restaurant_not_accepting_orders' | 'location_not_found' | 'pos_missing_items' | 'items_unavailable' | 'order_validation_error' | 'unknown') | undefined, failure_details?: OrderFailureDetails | undefined}; export type DspLocationOrderUpdateEventDto = {type: string, location_id: string, order_id?: string | undefined, order_status_update?: ('ready_for_pickup' | 'completed') | undefined}; export type DspLocationOrderCanceledEventDto = {type: string, location_id: string, order_id?: string | undefined}; export type CreateApiStreamDspV1EventArgs = {Authorization?: string | undefined, body: DspLocationPauseEventDto | DspLocationResumeEventDto | DspLocationMenuEventDto | DspLocationMenuUpdateEventDto | DspLocationOrderAcceptedEventDto | DspLocationOrderFailedEventDto | DspLocationOrderUpdateEventDto | DspLocationOrderCanceledEventDto}; export type CreateApiStreamDspV1EventOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApiStreamDspV1EventResponse = z.object({}); 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 }; /** * New Webhook Event (Stream –> DSP via Webhook) * * POST /stream-dsp/v1/event */ export const createApiStreamDspV1Event = async (args: CreateApiStreamDspV1EventArgs, options: CreateApiStreamDspV1EventOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/stream-dsp/v1/event${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(createApiStreamDspV1EventResponse, await res.json()) };