import {z} from 'zod' export type GetApiApiPreviewSearchArgs = {q: string, tags?: string | undefined, tagMode?: ('any' | 'all') | undefined, limit?: number | undefined, offset?: number | undefined, collectionId?: string | undefined, projectId?: string | undefined, sort?: ('best' | 'newest' | 'oldest') | undefined, typeId?: string | undefined, lang?: string | undefined, skill?: ('0' | '1' | 'true' | 'false') | undefined, kinds?: string | undefined, kindLimit?: number | undefined, archived?: ('true' | 'false') | undefined, context?: string | undefined, after?: string | undefined, before?: string | undefined, created_within?: string | undefined, created_period?: ('today' | 'yesterday' | 'this_week' | 'last_week' | 'this_month' | 'last_month' | 'this_year') | undefined, created_after?: string | undefined, created_before?: string | undefined, updated_within?: string | undefined, updated_period?: ('today' | 'yesterday' | 'this_week' | 'last_week' | 'this_month' | 'last_month' | 'this_year') | undefined, updated_after?: string | undefined, updated_before?: string | undefined, tz?: string | undefined, ws?: string | undefined}; export type GetApiApiPreviewSearchOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiPreviewSearchResponse = z.object({best_match: z.record(z.string(), z.unknown()).nullable().optional(), delivery: z.object({content_level: z.string().max(64).optional(), hasMore: z.boolean().optional(), nextCursor: z.union([z.string().max(4096), z.number().int().gte(-9007199254740991).lte(9007199254740991)]).nullable().optional(), overBudget: z.boolean().optional(), reason: z.string().max(64).nullable().optional(), returned: z.number().int().gte(-9007199254740991).lte(9007199254740991).optional(), total: z.number().int().gte(-9007199254740991).lte(9007199254740991).nullable().optional(), truncatedBy: z.string().max(64).nullable().optional()}).and(z.record(z.string(), z.unknown())), facets: z.record(z.string(), z.unknown()), kind_counts: z.record(z.string(), z.unknown()).optional(), kind_results: z.record(z.string(), z.unknown()).optional(), limit: z.number().int().gte(-9007199254740991).lte(9007199254740991), match_kind: z.literal("identifier").optional(), offset: z.number().int().gte(-9007199254740991).lte(9007199254740991), query: z.string().max(2000), results: z.array(z.record(z.string(), z.unknown())), search_id: z.string().max(128).optional(), suggestions: z.array(z.string().max(2000)).optional(), total: z.number().int().gte(-9007199254740991).lte(9007199254740991)}).and(z.record(z.string(), z.unknown())); 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 }; /** * Full-text search across capsules visible to the caller * * GET /api/preview/search * * Also accepts auth: cookieAuth, oauth2 */ export const getApiApiPreviewSearch = async (args: GetApiApiPreviewSearchArgs, options: GetApiApiPreviewSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = 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.tags != null) query.append("tags", String(args.tags)) if (args.tagMode != null) query.append("tagMode", String(args.tagMode)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.collectionId != null) query.append("collectionId", String(args.collectionId)) if (args.projectId != null) query.append("projectId", String(args.projectId)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.typeId != null) query.append("typeId", String(args.typeId)) if (args.lang != null) query.append("lang", String(args.lang)) if (args.skill != null) query.append("skill", String(args.skill)) if (args.kinds != null) query.append("kinds", String(args.kinds)) if (args.kindLimit != null) query.append("kindLimit", String(args.kindLimit)) if (args.archived != null) query.append("archived", String(args.archived)) if (args.context != null) query.append("context", String(args.context)) if (args.after != null) query.append("after", String(args.after)) if (args.before != null) query.append("before", String(args.before)) if (args.created_within != null) query.append("created_within", String(args.created_within)) if (args.created_period != null) query.append("created_period", String(args.created_period)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.updated_within != null) query.append("updated_within", String(args.updated_within)) if (args.updated_period != null) query.append("updated_period", String(args.updated_period)) if (args.updated_after != null) query.append("updated_after", String(args.updated_after)) if (args.updated_before != null) query.append("updated_before", String(args.updated_before)) if (args.tz != null) query.append("tz", String(args.tz)) if (args.ws != null) query.append("ws", String(args.ws)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/preview/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(getApiApiPreviewSearchResponse, await res.json()) };