import {z} from 'zod' export type CreateApiXlistRestoreArgs = {keep?: boolean | undefined, list_id_prefix?: string | undefined, list_id_suffix?: string | undefined, src?: string | undefined, src_file?: string | undefined}; export type CreateApiXlistRestoreOptions = {baseUrl: string, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const rankdbRestoreresult = z.object({errors: z.record(z.string(), z.unknown()), restored: z.number().int(), skipped: z.number().int()}); 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 }; /** * Restore lists * * POST /xlist/restore */ export const createApiXlistRestore = async (args: CreateApiXlistRestoreArgs, options: CreateApiXlistRestoreOptions) => { const {baseUrl, fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.rankdb.restoreresult+json") if (args.keep != null) query.append("keep", String(args.keep)) if (args.list_id_prefix != null) query.append("list_id_prefix", String(args.list_id_prefix)) if (args.list_id_suffix != null) query.append("list_id_suffix", String(args.list_id_suffix)) if (args.src != null) query.append("src", String(args.src)) if (args.src_file != null) query.append("src_file", String(args.src_file)) if (jwt !== undefined) headers.set("Authorization", jwt) const url = `${baseUrl.replace(/\/+$/, '')}/xlist/restore${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(rankdbRestoreresult, await res.json()) };