import {z} from 'zod' export type IngestionProfile = {id: string, version: string, required_capabilities: Array, enrichment_schema: {[key: string]: unknown}, segmentation_policy: {[key: string]: unknown}, metadata_schema: {[key: string]: unknown}}; export type CreateLibraryRequest = {name: string, ingestion_profile: IngestionProfile}; export type CreateApiV1LibrariesArgs = {'Idempotency-Key': string, body: CreateLibraryRequest}; export type CreateApiV1LibrariesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const requestId = z.string(); export const runtimeLocation = z.enum(["local", "cloud"]); export const execution = z.object({location: runtimeLocation, capability_id: z.string().nullable().optional(), capability_version: z.string().nullable().optional()}); export const usage = z.object({billable: z.boolean(), quantity: z.number().gte(0), unit: z.string(), credits: z.number().gte(0).optional()}); export const libraryId = z.string(); export const workspaceId = z.string(); export const ingestionProfile = z.object({id: z.string(), version: z.string(), required_capabilities: z.array(z.string()), enrichment_schema: z.record(z.string(), z.unknown()), segmentation_policy: z.record(z.string(), z.unknown()), metadata_schema: z.record(z.string(), z.unknown())}); export const library = z.object({id: libraryId, workspace_id: workspaceId.nullable().optional(), name: z.string().min(1), ingestion_profile: ingestionProfile, created_at: z.string(), updated_at: z.string()}); export const libraryResponse = z.object({request_id: requestId, execution: execution, usage: usage, warnings: z.array(z.string()), data: library}); 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 a library * * POST /v1/libraries * * Also accepts auth: apiKeyAuth */ export const createApiV1Libraries = async (args: CreateApiV1LibrariesArgs, options?: CreateApiV1LibrariesOptions) => { const {baseUrl = "https://api.cerul.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/libraries${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(libraryResponse, await res.json()) };