import {z} from 'zod' export type UpdateApiV3WorkflowsWorkflowIdArgs = {body: {delay?: number | undefined, is_enabled?: boolean | undefined, name?: string | undefined, template_id?: string | undefined, trigger_event?: ('booking.cancelled' | 'booking.created' | 'booking.pending' | 'booking.reminder' | 'booking.rescheduled' | 'calendar.created' | 'calendar.deleted' | 'calendar.updated' | 'contact.deleted' | 'contact.updated' | 'event.created' | 'event.deleted' | 'event.updated' | 'folder.created' | 'folder.deleted' | 'folder.updated' | 'grant.created' | 'grant.deleted' | 'grant.expired' | 'grant.imap_sync_completed' | 'grant.updated' | 'message.bounce_detected' | 'message.link_clicked' | 'message.opened' | 'message.send_failed' | 'message.send_success' | 'notetaker.created' | 'notetaker.deleted' | 'notetaker.media' | 'notetaker.meeting_state' | 'notetaker.updated' | 'thread.replied') | undefined, from?: ({email?: string | undefined, name?: string | undefined} | null) | undefined}}; export type UpdateApiV3WorkflowsWorkflowIdOptions = {baseUrl?: string | undefined, NYLASAPIKEY?: string | undefined, fetch?: typeof fetch | undefined}; export const workflow = z.object({app_id: z.string().nullable().optional(), date_created: z.number().int(), delay: z.number().int(), grant_id: z.string().nullable().optional(), id: z.string(), is_enabled: z.boolean(), name: z.string(), template_id: z.string(), trigger_event: z.enum(["booking.cancelled", "booking.created", "booking.pending", "booking.reminder", "booking.rescheduled", "calendar.created", "calendar.deleted", "calendar.updated", "contact.deleted", "contact.updated", "event.created", "event.deleted", "event.updated", "folder.created", "folder.deleted", "folder.updated", "grant.created", "grant.deleted", "grant.expired", "grant.imap_sync_completed", "grant.updated", "message.bounce_detected", "message.link_clicked", "message.opened", "message.send_failed", "message.send_success", "notetaker.created", "notetaker.deleted", "notetaker.media", "notetaker.meeting_state", "notetaker.updated", "thread.replied"]), from: z.object({email: z.string().optional(), name: z.string().optional()}).nullable().optional()}); export const updateApiV3WorkflowsWorkflowIdResponse = z.object({request_id: z.string(), data: workflow}); 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 a workflow * * PUT /v3/workflows/{workflow_id} */ export const updateApiV3WorkflowsWorkflowId = async (args: UpdateApiV3WorkflowsWorkflowIdArgs, options?: UpdateApiV3WorkflowsWorkflowIdOptions) => { const {baseUrl = "https://api.us.nylas.com", fetch: fetchFn = fetch, NYLASAPIKEY} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (NYLASAPIKEY !== undefined) headers.set('authorization', `Bearer ${NYLASAPIKEY}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/workflows/${encodeURIComponent(String(args.workflow_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(updateApiV3WorkflowsWorkflowIdResponse, await res.json()) };