import {z} from 'zod' export type GetApiApiPreviewHistoryBatchesArgs = {kind?: ('move' | 'tags' | 'tag_rename' | 'type_migrate') | undefined}; export type GetApiApiPreviewHistoryBatchesOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiPreviewHistoryBatchesResponse = z.object({batches: z.array(z.object({created_at: z.string().max(64), id: z.string().max(36), kind: z.enum(["move", "tags", "tag_rename", "type_migrate"]), made: z.number().int().gte(-9007199254740991).lte(9007199254740991), partly_expired: z.boolean(), reverted_at: z.string().max(64).nullable(), revertible: z.number().int().gte(-9007199254740991).lte(9007199254740991), summary: z.record(z.string(), z.unknown()), 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 }; /** * Your bulk moves, tag edits and tag renames from the last 30 days, with what can still be undone * * GET /api/preview/history/batches * * Also accepts auth: cookieAuth, oauth2 */ export const getApiApiPreviewHistoryBatches = async (args: GetApiApiPreviewHistoryBatchesArgs, options: GetApiApiPreviewHistoryBatchesOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.kind != null) query.append("kind", String(args.kind)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/preview/history/batches${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(getApiApiPreviewHistoryBatchesResponse, await res.json()) };