import {z} from 'zod' export type CreateApiApiPreviewCapsulesBatchMoveArgs = {body: {capsuleIds: Array, confirm_exposure?: boolean | undefined, targetTypeId: string}}; export type CreateApiApiPreviewCapsulesBatchMoveOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiPreviewCapsulesBatchMoveResponse = z.object({batch_id: z.string().max(36).nullable().optional(), failed: z.array(z.object({code: z.string().max(128), error: z.string().max(4000), id: z.string().max(36)})), succeeded: z.array(z.object({constraintBreaks: z.array(z.object({citing_count: z.number().int().gte(-9007199254740991).lte(9007199254740991), field: z.string().max(200), type_id: z.string().max(36), type_name: z.string().max(200), type_slug: z.string().max(200)}).and(z.record(z.string(), z.unknown()))).optional(), droppedCollections: z.array(z.object({id: z.string().max(36), name: z.string().max(200), slug: z.string().max(200)}).and(z.record(z.string(), z.unknown()))), id: z.string().max(36), incomplete: z.literal(true).optional(), missingRequiredFields: z.array(z.string().max(200)).optional(), newRefId: z.number().int().gte(-9007199254740991).lte(9007199254740991), newSlug: z.string().max(200).nullable(), ownedLog: z.literal("detached").optional(), previousRefId: z.number().int().gte(-9007199254740991).lte(9007199254740991), previousSlug: z.string().max(200).nullable()}))}).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 }; /** * Move many capsules to a different type in a single call * * POST /api/preview/capsules/batch/move * * Also accepts auth: cookieAuth, oauth2 */ export const createApiApiPreviewCapsulesBatchMove = async (args: CreateApiApiPreviewCapsulesBatchMoveArgs, options: CreateApiApiPreviewCapsulesBatchMoveOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/preview/capsules/batch/move${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiApiPreviewCapsulesBatchMoveResponse, await res.json()) };