import {z} from 'zod' export type GetApiApiV3MetaBasesBaseIdHooksHookIdArgs = {baseId: string, hookId: string}; export type GetApiApiV3MetaBasesBaseIdHooksHookIdOptions = {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 }; /** * Get hook * * GET /api/v3/meta/bases/{baseId}/hooks/{hookId} * * Also accepts auth: bearerAuth */ export const getApiApiV3MetaBasesBaseIdHooksHookId = async (args: GetApiApiV3MetaBasesBaseIdHooksHookIdArgs, options?: GetApiApiV3MetaBasesBaseIdHooksHookIdOptions) => { const {baseUrl = "https://app.nocodb.com", fetch: fetchFn = fetch, xcToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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))}/hooks/${encodeURIComponent(String(args.hookId))}${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(hookV3, await res.json()) };