import {z} from 'zod' export type ImportMode = 'normal' | 'force_reimport' | 'IMPORT_ONLY_MISSING_DAYS' | 'REIMPORT_ENTIRE_PERIOD'; export type PostSyncFlowEventData = {is_preview?: boolean | undefined, force_restart?: boolean | undefined, start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, force_from_date?: (string | null) | undefined, import_mode?: (ImportMode | null) | undefined} | {[key: string]: unknown}; export type PostSyncFlowEvent = {consumers?: (Array) | undefined, data?: PostSyncFlowEventData | undefined}; export type CreateApiSyncsSyncidFlowsFlowidEventArgs = {syncid: string, flowid: string, body: PostSyncFlowEvent}; export type CreateApiSyncsSyncidFlowsFlowidEventOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const triggerResponse = z.object({status: z.string(), message: z.string(), data: z.record(z.string(), z.unknown()).nullable().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 }; /** * Send a custom event for a specific flow (deprecated) * * POST /syncs/{syncid}/flows/{flowid}/event */ export const createApiSyncsSyncidFlowsFlowidEvent = async (args: CreateApiSyncsSyncidFlowsFlowidEventArgs, options?: CreateApiSyncsSyncidFlowsFlowidEventOptions) => { const {baseUrl = "https://api.chift.eu", 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/syncs/${encodeURIComponent(String(args.syncid))}/flows/${encodeURIComponent(String(args.flowid))}/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(triggerResponse, await res.json()) };