import {z} from 'zod' export type CreateApiApiPreviewCapsulesBatchEditFieldsArgs = {body: {action: 'set' | 'clear', capsuleIds: Array, dry_run?: boolean | undefined, field: string, value?: (string | number | boolean | Array | Array<{[key: string]: string | number | boolean | null}> | {alt?: string | undefined, attachment_id: string} | {attachment_id: string, label?: string | undefined} | null) | undefined}}; export type CreateApiApiPreviewCapsulesBatchEditFieldsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiPreviewCapsulesBatchEditFieldsResponse = z.object({batch_id: z.string().max(36).nullable().optional(), dry_run: z.boolean(), 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({changed: z.boolean(), id: z.string().max(36), impact: z.object({changed_fields: z.array(z.string().max(200)), dependents: z.object({items: z.array(z.object({id: z.string().max(128), ref: z.string().max(200), title: z.string().max(500), type_slug: z.string().max(200)}).and(z.record(z.string(), z.unknown()))), next_cursor: z.string().max(128).nullable(), total: z.number().int().gte(-9007199254740991).lte(9007199254740991)}).and(z.record(z.string(), z.unknown())), diff: z.object({text: z.string().max(8000), truncated: z.boolean()}).and(z.record(z.string(), z.unknown())).nullable()}).and(z.record(z.string(), z.unknown())).optional(), republish: z.union([z.object({message: z.string().max(4000), status: z.literal("started")}), z.object({message: z.string().max(4000), reason: z.enum(["opted_out", "caller_default", "blocked", "name_changed", "no_refresh_right", "rate_limited", "check_failed"]), status: z.literal("not_started")})]).optional(), version_skip_reason: z.string().max(64).nullable().optional(), version_skipped: z.boolean().optional()})), summary: z.object({changed: z.number().int().gte(-9007199254740991).lte(9007199254740991), refused: z.number().int().gte(-9007199254740991).lte(9007199254740991), unchanged: z.number().int().gte(-9007199254740991).lte(9007199254740991)})}); 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 }; /** * Set or clear one field across many capsules, with a dry run that counts first * * POST /api/preview/capsules/batch/edit-fields * * Also accepts auth: cookieAuth, oauth2 */ export const createApiApiPreviewCapsulesBatchEditFields = async (args: CreateApiApiPreviewCapsulesBatchEditFieldsArgs, options: CreateApiApiPreviewCapsulesBatchEditFieldsOptions) => { 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/edit-fields${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(createApiApiPreviewCapsulesBatchEditFieldsResponse, await res.json()) };