import {z} from 'zod' export type CreateApiApiPreviewCapsulesBatchConvertToEntriesArgs = {body: {capsuleIds: Array, date_source: string, dry_run: boolean, field_map?: ({[key: string]: string | null}) | undefined, preview_token?: string | undefined, targetTypeId: string, target_log_capsule_id?: string | undefined}}; export type CreateApiApiPreviewCapsulesBatchConvertToEntriesOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiPreviewCapsulesBatchConvertToEntriesResponse = z.object({date_fallbacks: z.number().int().gte(0).lte(9007199254740991), dry_run: z.boolean(), effects: z.object({added_bytes: z.number().int().gte(0).lte(9007199254740991), attachments: z.number().int().gte(0).lte(9007199254740991), body_renders_as_plain_text: z.boolean(), collections: z.number().int().gte(0).lte(9007199254740991), grants: z.array(z.object({access: z.enum(["view", "edit"]), capsule_id: z.string().max(36), grant_id: z.string().max(36)})), likes: z.number().int().gte(0).lte(9007199254740991), paragraph_rendering_change: z.object({from: z.enum(["plain", "markdown"]), to: z.enum(["plain", "markdown"])}).nullable(), projects: z.number().int().gte(0).lte(9007199254740991), share_links: z.array(z.object({capsule_id: z.string().max(36), expires_at: z.string().max(64).nullable(), link_id: z.string().max(36), token_preview: z.string().max(64).nullable()})), template_dropped: z.boolean(), widened_audience: z.array(z.object({capsule_id: z.string().max(36), reason: z.enum(["private_draft", "type_share_link", "bucket_share_link", "bucket_public", "bucket_grant"])}))}), field_map: z.array(z.object({kind_from: z.string().max(32), kind_to: z.enum(["text", "paragraph", "number", "datetime", "date", "boolean", "enum", "multi_enum", "reference", "multi_reference", "list", "table", "image", "file"]).nullable(), source: z.string().max(65), target: z.string().max(64).nullable(), target_is_new: z.boolean()})), periods: z.array(z.object({bucket: z.enum(["new", "existing"]), entries: z.number().int().gte(0).lte(9007199254740991), key: z.string().max(200).nullable(), log_capsule_id: z.string().max(36).nullable()})), preview_token: z.string().max(64), refusals: z.array(z.object({capsule_id: z.string().max(36).optional(), code: z.enum(["type_is_log", "type_encrypted", "type_freeform", "type_binds_log", "archived_capsules_present", "capsules_not_permitted", "too_many_capsules", "log_requires_field_schema", "field_count_exceeded", "log_requires_field_content", "timezone_invalid", "target_not_log", "target_log_required", "target_log_invalid", "date_source_invalid", "field_map_invalid", "not_found", "not_permitted", "archived", "encrypted", "log_capsule_source", "workspace_mismatch", "source_owns_log", "period_overlap", "bucket_archived", "bucket_not_permitted", "attachment_limit", "field_invalid", "field_content_too_long", "entry_empty"]), field: z.string().max(64).optional(), message: z.string().max(2000)})), result: z.object({entries: z.array(z.object({capsule_id: z.string().max(36), entry_id: z.string().max(36), entry_number: z.number().int().gte(1).lte(9007199254740991), log_capsule_id: z.string().max(36)})), log_capsule_ids: z.array(z.string().max(36))}).optional(), summary: z.object({capsules: z.number().int().gte(0).lte(9007199254740991), entries: z.number().int().gte(0).lte(9007199254740991), existing_buckets: z.number().int().gte(0).lte(9007199254740991), new_buckets: z.number().int().gte(0).lte(9007199254740991)}), type_changes: z.object({fields_added: z.array(z.object({name: z.string().max(64), type: z.enum(["text", "paragraph", "number", "datetime", "date", "boolean", "enum", "multi_enum", "reference", "multi_reference", "list", "table", "image", "file"])})), log_timezone: z.string().max(100), rollover: z.enum(["daily", "weekly", "monthly"]), settings_reset: z.array(z.string().max(64)), structure: z.literal("log")}).optional(), unplaced: z.array(z.object({capsules_with_value: z.number().int().gte(0).lte(9007199254740991), source: z.string().max(65)}))}); 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 }; /** * Convert chosen capsules into entries of an existing log Type, one entry each. dry_run previews; a write needs the preview_token and is all-or-nothing. * * POST /api/preview/capsules/batch/convert-to-entries * * Also accepts auth: cookieAuth, oauth2 */ export const createApiApiPreviewCapsulesBatchConvertToEntries = async (args: CreateApiApiPreviewCapsulesBatchConvertToEntriesArgs, options: CreateApiApiPreviewCapsulesBatchConvertToEntriesOptions) => { 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/convert-to-entries${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(createApiApiPreviewCapsulesBatchConvertToEntriesResponse, await res.json()) };