import {z} from 'zod' export type GetApiVaultsIdViewsArgs = {id: string}; export type GetApiVaultsIdViewsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const vaultViewSort = z.object({field: z.string().min(1).max(100), direction: z.enum(["asc", "desc"])}).nullable(); export const vaultViewFilter = z.object({field_key: z.string().min(1).max(100), operator: z.enum(["contains", "equals", "date_range", "in", "boolean", "number_range", "party_contains", "list_length", "equals_only", "not_contains", "not_equals", "is_empty", "is_not_empty", "is_before", "is_after", "has_notes", "has_no_notes"]), value: z.unknown()}); export const vaultViewState = z.object({hidden_field_keys: z.array(z.string()), field_order: z.array(z.string()), sort: vaultViewSort, filters: z.array(vaultViewFilter)}); export const vaultViewResponse = z.object({id: z.string(), vault_id: z.string(), name: z.string(), position: z.number().int(), created_by_user_id: z.number().int().nullable(), view_state: vaultViewState, created_at: z.string(), updated_at: z.string()}); export const vaultViewListResponse = z.object({views: z.array(vaultViewResponse)}); 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 a vault's saved views * * GET /vaults/{id}/views */ export const getApiVaultsIdViews = async (args: GetApiVaultsIdViewsArgs, options?: GetApiVaultsIdViewsOptions) => { const {baseUrl = "https://app.gc.ai/api/external/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/vaults/${encodeURIComponent(String(args.id))}/views${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(vaultViewListResponse, await res.json()) };