import {z} from 'zod' export type TriggerBatchWindow = {max_events?: (number | null) | undefined, window?: (string | null) | undefined}; export type TriggerSession = {continue?: (boolean | null) | undefined, idle_timeout?: (string | null) | undefined}; export type TriggerUpdate = {agent_id?: (string | null) | undefined, batch?: (TriggerBatchWindow | null) | undefined, conditions?: ({[key: string]: unknown} | null) | undefined, cron_expression?: (string | null) | undefined, description?: (string | null) | undefined, enabled?: (boolean | null) | undefined, event_filter?: ({[key: string]: unknown} | null) | undefined, failure_threshold?: (number | null) | undefined, name?: (string | null) | undefined, session?: (TriggerSession | null) | undefined, task_definition_id?: (string | null) | undefined, task_parameters?: ({[key: string]: unknown} | null) | undefined, timezone?: (string | null) | undefined}; export type UpdateApiV1WorkspacesWorkspaceTriggersTriggerIdArgs = {trigger_id: string, workspace: string, body: TriggerUpdate}; export type UpdateApiV1WorkspacesWorkspaceTriggersTriggerIdOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const triggerBatchWindow = z.object({max_events: z.number().int().gte(2).nullable().optional(), window: z.string().nullable().optional()}); export const triggerSession = z.object({continue: z.boolean().nullable().optional(), idle_timeout: z.string().nullable().optional()}); export const triggerResponse = z.object({agent_id: z.string(), batch: triggerBatchWindow.nullable().optional(), conditions: z.record(z.string(), z.unknown()), consecutive_failures: z.number().int(), created_at: z.string(), created_by: z.string(), cron_expression: z.string().nullable().optional(), description: z.string(), event_filter: z.record(z.string(), z.unknown()).nullable().optional(), failure_threshold: z.number().int(), id: z.string(), is_active: z.boolean(), last_event_at: z.string().nullable().optional(), last_execution_at: z.string().nullable().optional(), name: z.string(), needs_new_owner_at: z.string().nullable().optional(), next_run_time: z.string().nullable().optional(), session: triggerSession.nullable().optional(), status: z.enum(["active", "inactive", "needs_owner"]), stream_id: z.string().nullable().optional(), task_definition_id: z.string().nullable().optional(), task_parameters: z.record(z.string(), z.unknown()), timezone: z.string().nullable().optional(), trigger_type: z.string(), updated_at: z.string()}); 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 }; /** * Update Trigger * * PUT /v1/workspaces/{workspace}/triggers/{trigger_id} */ export const updateApiV1WorkspacesWorkspaceTriggersTriggerId = async (args: UpdateApiV1WorkspacesWorkspaceTriggersTriggerIdArgs, options: UpdateApiV1WorkspacesWorkspaceTriggersTriggerIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workspaces/${encodeURIComponent(String(args.workspace))}/triggers/${encodeURIComponent(String(args.trigger_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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()) };