import {z} from 'zod' export type GetApiConsumersConsumerIdSyncsSyncidFlowsFlowidExecutionsArgs = {consumer_id: string, syncid: string, flowid: string, date_to?: (string | null) | undefined, date_from?: (string | null) | undefined}; export type GetApiConsumersConsumerIdSyncsSyncidFlowsFlowidExecutionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const chainExecutionItem = z.object({id: z.string(), parentexecutionid: z.string().nullable(), start: z.string(), end: z.string().nullable(), status: z.string()}); export const getApiConsumersConsumerIdSyncsSyncidFlowsFlowidExecutionsResponse = z.array(chainExecutionItem); 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 }; /** * Get executions information for one consumer/flow/sync * * GET /consumers/{consumer_id}/syncs/{syncid}/flows/{flowid}/executions */ export const getApiConsumersConsumerIdSyncsSyncidFlowsFlowidExecutions = async (args: GetApiConsumersConsumerIdSyncsSyncidFlowsFlowidExecutionsArgs, options?: GetApiConsumersConsumerIdSyncsSyncidFlowsFlowidExecutionsOptions) => { const {baseUrl = "https://api.chift.eu", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.date_to != null) query.append("date_to", String(args.date_to)) if (args.date_from != null) query.append("date_from", String(args.date_from)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/consumers/${encodeURIComponent(String(args.consumer_id))}/syncs/${encodeURIComponent(String(args.syncid))}/flows/${encodeURIComponent(String(args.flowid))}/executions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiConsumersConsumerIdSyncsSyncidFlowsFlowidExecutionsResponse, await res.json()) };