import {z} from 'zod' export type CreateApiAssetsArgs = {body: {name: string, type: 'email' | 'landing', campaign_id: string, from_name?: (string | null) | undefined, from_email?: (string | null) | undefined, reply_email?: (string | null) | undefined, subject?: (string | null) | undefined, preview_text?: (string | null) | undefined, theme_id?: (string | null) | undefined, custom_fields?: (Array<{name?: string | undefined, value?: string | undefined}>) | undefined, tags?: Array | undefined}}; export type CreateApiAssetsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const asset = z.object({type: z.string(), id: z.string(), attributes: z.object({id: z.string(), creator_id: z.string(), subject: z.string(), from_name: z.string().nullable(), from_email: z.string().nullable(), reply_email: z.string().nullable(), type: z.enum(["email", "landing"]), name: z.string(), folder_path: z.string(), campaign: z.string(), brand: z.string(), language_code: z.string().nullable(), version: z.string(), parent_asset_id: z.string().nullable(), ai_generation_status: z.enum(["started", "completed", "failed"]).nullable().optional(), ai_generation_error: z.string().nullable().optional(), deleted: z.boolean().optional(), approval_status: z.enum(["draft", "review", "rejected", "approved", "completed"]).nullable()})}); export const assetItem = z.object({data: asset.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 an asset * * POST /assets */ export const createApiAssets = async (args: CreateApiAssetsArgs, options?: CreateApiAssetsOptions) => { const {baseUrl = "https://enterprise.knak.io/api/published/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/assets${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(assetItem, await res.json()) };