import {z} from 'zod' export type CreateApiApiPreviewSnapshotsIdRestoreArgs = {id: string, body: {confirmation_token?: string | undefined, dryRun?: boolean | undefined}}; export type CreateApiApiPreviewSnapshotsIdRestoreOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiPreviewSnapshotsIdRestoreResponse = z.object({confirmation_expires_at: z.string().max(128).optional(), confirmation_token: z.string().max(200).optional(), stats: z.object({added: z.object({capsules: z.number().int().gte(0).lte(9007199254740991), collections: z.number().int().gte(0).lte(9007199254740991), projects: z.number().int().gte(0).lte(9007199254740991), types: z.number().int().gte(0).lte(9007199254740991)}).and(z.record(z.string(), z.unknown())).optional(), archived_over_cap: z.number().int().gte(0).lte(9007199254740991).optional(), binaries_missing: z.number().int().gte(0).lte(9007199254740991), binaries_restored: z.number().int().gte(0).lte(9007199254740991), changed: z.object({capsules: z.number().int().gte(0).lte(9007199254740991), collections: z.number().int().gte(0).lte(9007199254740991), projects: z.number().int().gte(0).lte(9007199254740991), types: z.number().int().gte(0).lte(9007199254740991)}).and(z.record(z.string(), z.unknown())).optional(), dry_run: z.boolean(), links_resumed: z.array(z.object({id: z.string().max(36), links: z.number().int().gte(0).lte(9007199254740991), resource_type: z.enum(["capsule", "type", "collection", "project"]), visibility: z.enum(["public", "link"]).nullable()}).and(z.record(z.string(), z.unknown()))).optional(), not_restored: z.array(z.object({code: z.string().max(128), detail: z.string().max(4000)}).and(z.record(z.string(), z.unknown()))), over_cap: z.object({active_after: z.number().int().gte(0).lte(9007199254740991), limit: z.number().int().gte(0).lte(9007199254740991), will_archive: z.number().int().gte(0).lte(9007199254740991)}).and(z.record(z.string(), z.unknown())).nullable().optional(), preview: z.object({added: z.array(z.object({id: z.string().max(36), kind: z.enum(["capsule", "type", "project", "collection"]), label: z.string().max(2000), ref: z.string().max(402).nullable()}).and(z.record(z.string(), z.unknown()))).optional(), changed: z.array(z.object({id: z.string().max(36), kind: z.enum(["capsule", "type", "project", "collection"]), label: z.string().max(2000), ref: z.string().max(402).nullable()}).and(z.record(z.string(), z.unknown()))), removed: z.array(z.object({id: z.string().max(36), kind: z.enum(["capsule", "type", "project", "collection"]), label: z.string().max(2000), ref: z.string().max(402).nullable()}).and(z.record(z.string(), z.unknown())))}).and(z.record(z.string(), z.unknown())).optional(), protected_types: z.object({change: z.object({id: z.string().max(36), items: z.number().int().gte(0).lte(9007199254740991), review_url: z.string().max(128), revision: z.number().int().gt(0).lte(9007199254740991)}).and(z.record(z.string(), z.unknown())).optional(), detail: z.string().max(2000).optional(), maximum_items: z.number().int().gt(0).lte(9007199254740991).optional(), protected_types: z.number().int().gte(0).lte(9007199254740991), reason: z.string().max(128).optional(), restored_in_place: z.number().int().gte(0).lte(9007199254740991), status: z.enum(["staged", "unchanged", "restored", "not_staged"]), waiting_change: z.object({id: z.string().max(36), review_url: z.string().max(128)}).and(z.record(z.string(), z.unknown())).optional()}).and(z.record(z.string(), z.unknown())).optional(), recently_deleted_count: z.number().int().gte(0).lte(9007199254740991).optional(), removed: z.record(z.string(), z.number().int().gte(-9007199254740991).lte(9007199254740991)), removed_files: z.object({attachments: z.number().int().gte(0).lte(9007199254740991), bytes: z.number().int().gte(0).lte(9007199254740991), capsule_images: z.number().int().gte(0).lte(9007199254740991), type_images: z.number().int().gte(0).lte(9007199254740991)}).and(z.record(z.string(), z.unknown())), restored: z.record(z.string(), z.number().int().gte(-9007199254740991).lte(9007199254740991)), resumes_exposure: z.array(z.object({id: z.string().max(36), links: z.number().int().gte(0).lte(9007199254740991), ref: z.string().max(200).nullable(), resource_type: z.enum(["capsule", "type", "collection", "project"]), returns_from: z.enum(["archive", "deletion"]), skill: z.boolean(), title: z.string().max(2000), visibility: z.enum(["public", "link"]).nullable()}).and(z.record(z.string(), z.unknown()))), safety_snapshot: z.object({expires_at: z.string().max(128).nullable(), id: z.string().max(36)}).and(z.record(z.string(), z.unknown())).nullable().optional(), undo: z.boolean().optional()}).and(z.record(z.string(), z.unknown()))}).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 }; /** * Restore the personal workspace to a snapshot, or preview it with dryRun * * POST /api/preview/snapshots/{id}/restore * * Also accepts auth: cookieAuth, oauth2 */ export const createApiApiPreviewSnapshotsIdRestore = async (args: CreateApiApiPreviewSnapshotsIdRestoreArgs, options: CreateApiApiPreviewSnapshotsIdRestoreOptions) => { 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/snapshots/${encodeURIComponent(String(args.id))}/restore${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(createApiApiPreviewSnapshotsIdRestoreResponse, await res.json()) };