import {z} from 'zod' export type GetApiBusinessesIdEmailTemplatesArgs = {id: string, include_project_email_templates?: 'true' | undefined}; export type GetApiBusinessesIdEmailTemplatesOptions = {baseUrl?: string | undefined, HttpBasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const emailTemplate = z.object({id: z.string(), name: z.string(), subject: z.string(), body: z.string().nullable(), kind: z.enum(["html", "unlayer", "old_editor", "editor"]), template_html: z.string().nullable(), template_json: z.unknown(), new_template_json: z.unknown(), new_template_html: z.string().nullable(), attachments: z.array(z.object({id: z.string(), name: z.string()})), metadata: z.record(z.string(), z.unknown()), created_at: z.string(), enabled_at: z.string().nullable().optional()}); export const getApiBusinessesIdEmailTemplatesResponse = z.object({email_templates: z.array(emailTemplate)}); 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 }; /** * List all email templates for a business * * GET /businesses/{id}/email_templates */ export const getApiBusinessesIdEmailTemplates = async (args: GetApiBusinessesIdEmailTemplatesArgs, options?: GetApiBusinessesIdEmailTemplatesOptions) => { const {baseUrl = "https://api.pocketflows.com", fetch: fetchFn = fetch, HttpBasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.include_project_email_templates != null) query.append("include_project_email_templates", String(args.include_project_email_templates)) if (HttpBasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${HttpBasicAuth.username}:${HttpBasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/businesses/${encodeURIComponent(String(args.id))}/email_templates${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(getApiBusinessesIdEmailTemplatesResponse, await res.json()) };