import {z} from 'zod' export type TemplateScope = 'system' | 'organization' | 'user' | 'public'; export type ListTemplatesRequest = {filters?: ({[key: string]: unknown} | null) | undefined, sort?: ({[key: string]: string} | null) | undefined, search?: (string | null) | undefined, scope?: (TemplateScope | null) | undefined, category?: (string | null) | undefined, is_active?: boolean | undefined, tags?: (Array | null) | undefined}; export type CreateApiV1TemplatesRetrieversArgs = {body: ListTemplatesRequest}; export type CreateApiV1TemplatesRetrieversOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, NamespaceHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const templateType = z.enum(["namespace", "retriever", "cluster", "collection", "bucket", "taxonomy", "scaffold"]); export const templateMode = z.enum(["scaffold", "config", "clone", "manifest"]); export const templateScope = z.enum(["system", "organization", "user", "public"]); export const baseTemplateModel = z.object({template_id: z.string(), template_type: templateType, mode: templateMode.optional(), scope: templateScope, internal_id: z.string(), name: z.string().min(1).max(100), description: z.string().min(1).max(1000), category: z.string().nullable().optional(), configuration: z.record(z.string(), z.unknown()), tags: z.array(z.string()).optional(), is_active: z.boolean().optional(), is_public: z.boolean().optional(), use_cases: z.array(z.string()).optional(), requirements: z.array(z.string()).optional(), created_by: z.string().nullable().optional(), source_resource_id: z.string().nullable().optional(), sample_namespace_id: z.string().nullable().optional(), example_queries: z.array(z.string()).optional(), whats_inside: z.string().nullable().optional(), materialize_estimate: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const listTemplatesResponse = z.object({results: z.array(baseTemplateModel), total_count: z.number().int()}); 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 Retriever Templates * * POST /v1/templates/retrievers */ export const createApiV1TemplatesRetrievers = async (args: CreateApiV1TemplatesRetrieversArgs, options?: CreateApiV1TemplatesRetrieversOptions) => { const {baseUrl = "https://api.mixpeek.com", fetch: fetchFn = fetch, BearerAuth, NamespaceHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) if (NamespaceHeader !== undefined) headers.set("X-Namespace", NamespaceHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/templates/retrievers${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(listTemplatesResponse, await res.json()) };