import {z} from 'zod' export type GetApiV4PointersArgs = {pipeline_name?: string | undefined, skip?: number | undefined, limit?: number | undefined, order_by?: string | undefined}; export type GetApiV4PointersOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const pointerGet = z.object({id: z.string(), pointer: z.string(), pipeline_id: z.string(), locked: z.boolean()}); export const paginatedPointerGet = z.object({skip: z.number().int().gte(0), limit: z.number().int().gte(1).lte(1000), total: z.number().int().gte(0), data: z.array(pointerGet)}); 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 }; /** * Index * * GET /v4/pointers * * Also accepts auth: APIKeyCookie */ export const getApiV4Pointers = async (args: GetApiV4PointersArgs, options?: GetApiV4PointersOptions) => { const {baseUrl = "https://www.mystic.ai", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pipeline_name != null) query.append("pipeline_name", String(args.pipeline_name)) if (args.skip != null) query.append("skip", String(args.skip)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v4/pointers${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(paginatedPointerGet, await res.json()) };