import {z} from 'zod' export type CreateApiApiPreviewImportRunsIdFinalizeArgs = {id: string, body: {revision: number, selection_digest: string}}; export type CreateApiApiPreviewImportRunsIdFinalizeOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiPreviewImportRunsIdFinalizeResponse = z.object({change: z.object({id: z.string().max(36), operation: z.literal("structural"), review_url: z.string().max(2000), revision: z.number().int().gte(-9007199254740991).lte(9007199254740991), status: z.literal("pending"), type_id: z.string().max(36).nullable(), workspace_id: z.string().max(36).nullable().optional()}).optional(), detail: z.string().max(4000).optional(), import_summary: z.record(z.string(), z.unknown()).optional(), pending_approval: z.boolean().optional(), protection_turned_on: z.object({type_ids: z.array(z.string().max(36))}).optional(), reason: z.literal("pending_approval").optional()}); 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 }; /** * Validate every selected chunk and atomically save one held change, releasing no live content * * POST /api/preview/import/runs/{id}/finalize * * Also accepts auth: cookieAuth, oauth2 */ export const createApiApiPreviewImportRunsIdFinalize = async (args: CreateApiApiPreviewImportRunsIdFinalizeArgs, options: CreateApiApiPreviewImportRunsIdFinalizeOptions) => { 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/import/runs/${encodeURIComponent(String(args.id))}/finalize${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(createApiApiPreviewImportRunsIdFinalizeResponse, await res.json()) };