import {z} from 'zod' export type EventPost = {event_id?: string | undefined, event_type_id: string, customer_id?: string | undefined, customer_identifier_type?: ('email' | 'phone' | 'external_id') | undefined, customer_identifier_value?: (string | {customer_source_id: string, external_id: string}) | undefined, external_id: string, data?: (string | Array | Record) | undefined, created_at?: string | undefined, secondary_external_id?: string | undefined, state?: (0 | 1 | 2) | undefined}; export type PropertyRecord = {property_record_id?: string | undefined, property_id: string, property_name?: string | undefined, property_value?: (string | number | number | boolean | Array | Record) | undefined, last_change?: string | undefined}; export type CreateApiEventsArgs = {'Accept-Language'?: string | undefined, body: {event: EventPost, property_records?: Array | undefined}}; export type CreateApiEventsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiEventsResponse = z.object({data: z.object({event_id: z.string().optional()}).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 a new event * * POST /events * * Also accepts auth: basicAuth */ export const createApiEvents = async (args: CreateApiEventsArgs, options?: CreateApiEventsOptions) => { const {baseUrl = "https://sandbox.crmcarecloud.com/webservice/rest-api/enterprise-interface/v1.0", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(createApiEventsResponse, await res.json()) };