import {z} from 'zod' export type GetApiAssetsArgs = {page?: number | undefined, per_page?: number | undefined, 'filter[parent_asset_id]'?: string | undefined, 'filter[approval_status]'?: ('draft' | 'review' | 'rejected' | 'approved' | 'completed') | undefined}; export type GetApiAssetsOptions = {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 assetList = z.object({data: z.array(asset).optional(), links: z.object({first: z.string().optional(), last: z.string().optional(), prev: z.string().nullable().optional(), next: z.string().nullable().optional()}).optional(), meta: z.object({current_page: z.number().int().optional(), from: z.number().int().optional(), last_page: z.number().int().optional(), links: z.array(z.object({url: z.string().nullable().optional(), label: z.string().optional(), active: z.boolean().optional()})).optional(), path: z.string().optional(), per_page: z.number().int().optional(), to: z.number().int().optional(), total: z.number().int().optional()}).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 }; /** * List all assets * * GET /assets */ export const getApiAssets = async (args: GetApiAssetsArgs, options?: GetApiAssetsOptions) => { const {baseUrl = "https://enterprise.knak.io/api/published/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args['filter[parent_asset_id]'] != null) query.append("filter[parent_asset_id]", String(args['filter[parent_asset_id]'])) if (args['filter[approval_status]'] != null) query.append("filter[approval_status]", String(args['filter[approval_status]'])) const url = `${baseUrl.replace(/\/+$/, '')}/assets${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(assetList, await res.json()) };