import {z} from 'zod' export type UpdateTriggerRequest = {runWithTokenId?: number | undefined, component?: string | undefined, configurationId?: string | undefined, coolDownPeriodMinutes?: number | undefined, tableIds?: Array | undefined}; export type UpdateApiV2StorageTriggersTriggerIdArgs = {triggerId: number, body: UpdateTriggerRequest}; export type UpdateApiV2StorageTriggersTriggerIdOptions = {baseUrl?: string | undefined, StorageKey?: string | undefined, fetch?: typeof fetch | undefined}; export const actorResponse = z.object({type: z.enum(["token", "session", "pat", "system"]), id: z.string(), name: z.string(), adminId: z.number().int().nullable()}); export const triggerResponse = z.object({id: z.string(), runWithTokenId: z.number().int(), component: z.string(), configurationId: z.string(), lastRun: z.string().nullable(), creatorToken: z.object({id: z.number().int(), description: z.string()}), actor: actorResponse, coolDownPeriodMinutes: z.number().int(), tables: z.array(z.object({tableId: 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 * * PUT /v2/storage/triggers/{triggerId}/ * * Also accepts auth: ManageKey, BearerAuth */ export const updateApiV2StorageTriggersTriggerId = async (args: UpdateApiV2StorageTriggersTriggerIdArgs, options?: UpdateApiV2StorageTriggersTriggerIdOptions) => { const {baseUrl = "https://connection.keboola.com", fetch: fetchFn = fetch, StorageKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (StorageKey !== undefined) headers.set("X-StorageApi-Token", StorageKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/storage/triggers/${encodeURIComponent(String(args.triggerId))}/${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()) };