import {z} from 'zod' export type GetApiVaultsIdIngotsSearchArgs = {id: string, q?: string | undefined, file_type?: string | undefined, folder_id?: string | undefined, min_size?: number | undefined, max_size?: number | undefined, limit?: number | undefined, cursor?: string | undefined, sort_order?: ('asc' | 'desc') | undefined}; export type GetApiVaultsIdIngotsSearchOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, VaultAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const ingot = z.object({ingot_id: z.string(), name: z.string(), download_filename: z.string(), size_bytes: z.number().int(), content_type: z.string(), status: z.enum(["uploading", "active"]), folder_id: z.string().nullable(), managed_by: z.string().nullable(), upload_source: z.string().nullable(), created_at: z.number().int(), updated_at: z.number().int(), access_count: z.number().int(), total_bandwidth_bytes: z.number().int(), content_hmac: z.string().nullable(), content_version: z.string().nullable()}); export const ingotSearchPage = z.object({ingots: z.array(ingot), count: z.number().int(), has_more: z.boolean(), next_cursor: z.string().optional(), filters: z.object({})}); export const responseMeta = z.object({api_version: z.string().optional(), request_id: z.string().optional(), response_ms: z.number().int().optional(), timestamp: z.number().int().optional(), pools: z.object({}).optional(), billing: z.object({past_due: z.boolean().optional()}).optional(), quota: z.object({limit: z.number().int().optional(), used: z.number().int().optional(), remaining: z.number().int().optional(), resets_at: z.number().int().optional()}).optional()}); export const getApiVaultsIdIngotsSearchResponse = z.object({data: ingotSearchPage, meta: responseMeta.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 }; /** * Search a vault's ingots * * GET /vaults/{id}/ingots/search * * Also accepts auth: BearerAuth + VaultAccessToken */ export const getApiVaultsIdIngotsSearch = async (args: GetApiVaultsIdIngotsSearchArgs, options?: GetApiVaultsIdIngotsSearchOptions) => { const {baseUrl = "https://api.sparkvault.com/v1", fetch: fetchFn = fetch, ApiKeyAuth, VaultAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", String(args.q)) if (args.file_type != null) query.append("file_type", String(args.file_type)) if (args.folder_id != null) query.append("folder_id", String(args.folder_id)) if (args.min_size != null) query.append("min_size", String(args.min_size)) if (args.max_size != null) query.append("max_size", String(args.max_size)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) if (VaultAccessToken !== undefined) headers.set("X-Vault-Access-Token", VaultAccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/vaults/${encodeURIComponent(String(args.id))}/ingots/search${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(getApiVaultsIdIngotsSearchResponse, await res.json()) };