import {z} from 'zod' export type GetApiV1AiInfoOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const aiInfoResponse = z.object({summary: z.string(), variables: z.array(z.object({variable: z.enum(["ai_hook", "ai_subject"]), description: z.string(), valueField: z.enum(["hook", "subject"]), endpoints: z.array(z.string())})), input: z.object({leadId: z.object({type: z.literal("string"), required: z.boolean(), format: z.literal("uuid").optional(), default: z.string().optional(), enum: z.array(z.string()).optional(), description: z.string()}), locale: z.object({type: z.literal("string"), required: z.boolean(), format: z.literal("uuid").optional(), default: z.string().optional(), enum: z.array(z.string()).optional(), description: z.string()}), mode: z.object({type: z.literal("string"), required: z.boolean(), format: z.literal("uuid").optional(), default: z.string().optional(), enum: z.array(z.string()).optional(), description: z.string()})}), batch: z.object({field: z.literal("leads"), minItems: z.number().int().gt(0).lte(9007199254740991), maxItems: z.number().int().gt(0).lte(9007199254740991), description: z.string()}), output: z.record(z.string(), z.string()), supportedLocales: z.array(z.string()), defaultLocale: z.string(), modes: z.array(z.enum(["native", "translate"])), rateLimit: z.object({perKeyPerHour: z.number().int().gt(0).lte(9007199254740991)}), billing: z.object({chargedFor: z.string(), pricingUrl: z.string()}), docsUrl: z.string(), yourPrice: z.object({currency: z.literal("GBP"), perCallPence: z.object({ai_hook: z.number().int().gt(0).lte(9007199254740991), ai_subject: z.number().int().gt(0).lte(9007199254740991)})}).optional()}); export const aiInfoEnvelope = z.object({success: z.unknown(), data: aiInfoResponse}); 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 }; /** * How to use the AI personalization endpoints (free; key optional) * * GET /v1/ai/info * * Also accepts auth: apiKeyAuth */ export const getApiV1AiInfo = async (options?: GetApiV1AiInfoOptions) => { const {baseUrl = "https://api.meirra.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/ai/info${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(aiInfoEnvelope, await res.json()) };