import {z} from 'zod' export type HookNotificationV3Url = {type: 'URL', payload: {method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE', path: string, body?: string | undefined, headers?: (Array<{name?: string | undefined, value?: string | undefined, enabled?: boolean | undefined}>) | undefined, parameters?: (Array<{name?: string | undefined, value?: string | undefined, enabled?: boolean | undefined}>) | undefined, auth?: string | undefined}, trigger_form?: boolean | undefined, trigger_form_id?: string | undefined, include_user?: boolean | undefined}; export type HookNotificationV3Email = {type: 'Email', payload: {to: string, subject: string, body: string}, trigger_form?: boolean | undefined, trigger_form_id?: string | undefined, include_user?: boolean | undefined}; export type HookNotificationV3Script = {type: 'Script', payload: {scriptId: string, method?: string | undefined, path?: string | undefined}, trigger_form?: boolean | undefined, trigger_form_id?: string | undefined, include_user?: boolean | undefined}; export type HookNotificationV3Messaging = {type: 'Slack' | 'Discord' | 'Telegram' | 'Whatsapp' | 'Twilio', payload: {body: string}, trigger_form?: boolean | undefined, trigger_form_id?: string | undefined, include_user?: boolean | undefined}; export type HookNotificationV3 = HookNotificationV3Url | HookNotificationV3Email | HookNotificationV3Script | HookNotificationV3Messaging; export type HookV3Create = {title: string, description?: string | undefined, event?: ('record' | 'manual') | undefined, operation: Array<'insert' | 'update' | 'delete'>, notification: HookNotificationV3, active?: boolean | undefined, trigger_fields?: Array | undefined}; export type CreateApiApiV3MetaBasesBaseIdTablesTableIdHooksArgs = {baseId: string, tableId: string, body: HookV3Create}; export type CreateApiApiV3MetaBasesBaseIdTablesTableIdHooksOptions = {baseUrl?: string | undefined, xcToken?: string | undefined, fetch?: typeof fetch | undefined}; export const hookNotificationV3Url = z.object({type: z.literal("URL"), payload: z.object({method: z.enum(["GET", "POST", "PUT", "PATCH", "DELETE"]), path: z.string(), body: z.string().optional(), headers: z.array(z.object({name: z.string().optional(), value: z.string().optional(), enabled: z.boolean().optional()})).optional(), parameters: z.array(z.object({name: z.string().optional(), value: z.string().optional(), enabled: z.boolean().optional()})).optional(), auth: z.string().optional()}), trigger_form: z.boolean().optional(), trigger_form_id: z.string().optional(), include_user: z.boolean().optional()}); export const hookNotificationV3Email = z.object({type: z.literal("Email"), payload: z.object({to: z.string(), subject: z.string(), body: z.string()}), trigger_form: z.boolean().optional(), trigger_form_id: z.string().optional(), include_user: z.boolean().optional()}); export const hookNotificationV3Script = z.object({type: z.literal("Script"), payload: z.object({scriptId: z.string(), method: z.string().optional(), path: z.string().optional()}), trigger_form: z.boolean().optional(), trigger_form_id: z.string().optional(), include_user: z.boolean().optional()}); export const hookNotificationV3Messaging = z.object({type: z.enum(["Slack", "Discord", "Telegram", "Whatsapp", "Twilio"]), payload: z.object({body: z.string()}), trigger_form: z.boolean().optional(), trigger_form_id: z.string().optional(), include_user: z.boolean().optional()}); export const hookNotificationV3 = z.discriminatedUnion("type", [hookNotificationV3Url, hookNotificationV3Email, hookNotificationV3Script, hookNotificationV3Messaging]); export const hookV3 = z.object({id: z.string(), table_id: z.string(), title: z.string(), description: z.string().nullable().optional(), event: z.enum(["record", "manual"]), operation: z.array(z.enum(["insert", "update", "delete"])), notification: hookNotificationV3.optional(), active: z.boolean().optional(), trigger_fields: z.array(z.string()).optional(), created_at: z.string().optional(), updated_at: z.string().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 }; /** * Create hook * * POST /api/v3/meta/bases/{baseId}/tables/{tableId}/hooks * * Also accepts auth: bearerAuth */ export const createApiApiV3MetaBasesBaseIdTablesTableIdHooks = async (args: CreateApiApiV3MetaBasesBaseIdTablesTableIdHooksArgs, options?: CreateApiApiV3MetaBasesBaseIdTablesTableIdHooksOptions) => { const {baseUrl = "https://app.nocodb.com", fetch: fetchFn = fetch, xcToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (xcToken !== undefined) headers.set("xc-token", xcToken) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/meta/bases/${encodeURIComponent(String(args.baseId))}/tables/${encodeURIComponent(String(args.tableId))}/hooks${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(hookV3, await res.json()) };