import {z} from 'zod' export type GetApiApiV2ReposSearchArgs = {repo_type?: ('program' | 'dataset' | null) | undefined, q?: string | undefined, tags?: string | undefined, sort?: string | undefined, order?: string | undefined, page?: number | undefined, limit?: number | undefined, license?: (string | null) | undefined, task?: (string | null) | undefined, autonomy_min?: (number | null) | undefined, autonomy_max?: (number | null) | undefined, authorization?: (string | null) | undefined, modaic_session?: (string | null) | undefined}; export type GetApiApiV2ReposSearchOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiApiV2ReposSearchResponse = 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 }; /** * Search Repositories * * GET /api/v2/repos/search */ export const getApiApiV2ReposSearch = async (args: GetApiApiV2ReposSearchArgs, options: GetApiApiV2ReposSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() const cookies: string[] = [] headers.set('accept', "application/json") if (args.repo_type != null) query.append("repo_type", String(args.repo_type)) if (args.q != null) query.append("q", String(args.q)) if (args.tags != null) query.append("tags", String(args.tags)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.order != null) query.append("order", String(args.order)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.license != null) query.append("license", String(args.license)) if (args.task != null) query.append("task", String(args.task)) if (args.autonomy_min != null) query.append("autonomy_min", String(args.autonomy_min)) if (args.autonomy_max != null) query.append("autonomy_max", String(args.autonomy_max)) if (args.authorization != null) headers.set("authorization", String(args.authorization)) if (args.modaic_session != null) cookies.push("modaic_session=" + encodeURIComponent(String(args.modaic_session))) if (cookies.length > 0) headers.set('cookie', cookies.join('; ')) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/repos/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(getApiApiV2ReposSearchResponse, await res.json()) };