import {z} from 'zod' export type CreateApiApiPreviewCapsulesBatchMoveImpactArgs = {body: {capsuleIds: Array, targetTypeId: string}}; export type CreateApiApiPreviewCapsulesBatchMoveImpactOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiPreviewCapsulesBatchMoveImpactResponse = z.object({constraint_breaks: 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(), dropped_collections: z.array(z.object({capsule_id: z.string().max(36), collections: 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())))})), exposure: z.array(z.object({id: z.string().max(36), kind: z.enum(["type", "collection"]), name: z.string().max(200), slug: z.string().max(200), visibility: z.enum(["link", "public"])}).and(z.record(z.string(), z.unknown()))), log_type_target: z.boolean(), owned_log_effects: z.array(z.object({capsule_id: z.string().max(36), effect: z.literal("detached")})).optional(), refusals: z.array(z.object({capsule_id: z.string().max(36), code: z.enum(["encryption_incompatible", "archived", "owned_log_type_mismatch", "log_capsule_source"])})), required_fields: z.array(z.object({capsule_id: z.string().max(36), missing: z.array(z.string().max(200))})).optional(), required_fields_unknown: z.array(z.object({capsule_id: z.string().max(36)})).optional(), template_effects: z.array(z.object({capsule_id: z.string().max(36), effect: z.enum(["template_changes", "body_becomes_blank", "renders_structured"])})), will_publish_count: 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 }; /** * Preview the side effects (exposure, dropped collections, template changes, refusals) of moving capsules into a Type * * POST /api/preview/capsules/batch/move-impact * * Also accepts auth: cookieAuth, oauth2 */ export const createApiApiPreviewCapsulesBatchMoveImpact = async (args: CreateApiApiPreviewCapsulesBatchMoveImpactArgs, options: CreateApiApiPreviewCapsulesBatchMoveImpactOptions) => { 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-impact${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(createApiApiPreviewCapsulesBatchMoveImpactResponse, await res.json()) };