import {z} from 'zod' export type CreateApiApiPreviewCapsulesReadCostArgs = {body: {capsule_ids: Array, threshold?: number | undefined}}; export type CreateApiApiPreviewCapsulesReadCostOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiPreviewCapsulesReadCostResponse = z.object({capsules: z.array(z.union([z.object({available: z.literal(false), id: z.string().max(64)}).and(z.record(z.string(), z.unknown())), z.object({available: z.literal(true), id: z.string().max(64), measured: z.literal(false), reason: z.enum(["encrypted", "archived"]), ref: z.string().max(512), references: z.array(z.object({expansion: z.string().max(200), ref: z.string().max(512), title: z.string().max(2000).nullable()}).and(z.record(z.string(), z.unknown()))), title: z.string().max(2000).nullable()}).and(z.record(z.string(), z.unknown())), z.object({available: z.literal(true), id: z.string().max(64), measured: z.literal(true), oversize: z.array(z.object({admits: z.array(z.object({ref: z.string().max(512), title: z.string().max(2000).nullable()}).and(z.record(z.string(), z.unknown()))), chars: z.number().int().gte(-9007199254740991).lte(9007199254740991), edit_sites: z.array(z.union([z.object({text: z.string().max(512), where: z.literal("body")}).and(z.record(z.string(), z.unknown())), z.object({field: z.string().max(200), text: z.string().max(512), where: z.literal("field")}).and(z.record(z.string(), z.unknown()))])), id: z.string().max(64).nullable(), ref: z.string().max(512).nullable(), saving_chars: z.number().int().gte(-9007199254740991).lte(9007199254740991).nullable(), source_ref: z.string().max(512).nullable(), title: z.string().max(2000).nullable(), withholds_linked: z.boolean()}).and(z.record(z.string(), z.unknown()))), parts: z.array(z.object({chars: z.number().int().gte(-9007199254740991).lte(9007199254740991), edit_sites: z.array(z.union([z.object({text: z.string().max(512), where: z.literal("body")}).and(z.record(z.string(), z.unknown())), z.object({field: z.string().max(200), text: z.string().max(512), where: z.literal("field")}).and(z.record(z.string(), z.unknown()))])).optional(), files: z.number().int().gte(-9007199254740991).lte(9007199254740991).optional(), id: z.string().max(64).nullable(), kind: z.enum(["own", "attachments", "fields", "inlined", "unavailable", "pointer", "capped", "withheld", "unexpanded", "unresolved"]), pointer: z.object({admits: z.array(z.object({ref: z.string().max(512), title: z.string().max(2000).nullable()}).and(z.record(z.string(), z.unknown()))), saving_chars: z.number().int().gte(-9007199254740991).lte(9007199254740991), withholds_linked: z.boolean()}).and(z.record(z.string(), z.unknown())).optional(), potential_chars: z.number().int().gte(-9007199254740991).lte(9007199254740991).nullable().optional(), ref: z.string().max(512).nullable(), source_ref: z.string().max(512).optional(), title: z.string().max(2000).nullable()}).and(z.record(z.string(), z.unknown()))), ref: z.string().max(512), repeated_text: z.array(z.object({chars: z.number().int().gte(-9007199254740991).lte(9007199254740991), cost_chars: z.number().int().gte(-9007199254740991).lte(9007199254740991), extra_copies: z.number().int().gte(-9007199254740991).lte(9007199254740991), paths: z.array(z.string().max(1000)), preview: z.string().max(400)}).and(z.record(z.string(), z.unknown()))), title: z.string().max(2000).nullable(), total_chars: z.number().int().gte(-9007199254740991).lte(9007199254740991)}).and(z.record(z.string(), z.unknown()))])), set: z.object({repeats: z.array(z.object({chars: z.number().int().gte(-9007199254740991).lte(9007199254740991), id: z.string().max(64), ref: z.string().max(512).nullable(), repeat_cost_chars: z.number().int().gte(-9007199254740991).lte(9007199254740991), times: z.number().int().gte(-9007199254740991).lte(9007199254740991), title: z.string().max(2000).nullable()}).and(z.record(z.string(), z.unknown()))), total_chars: z.number().int().gte(-9007199254740991).lte(9007199254740991)}).and(z.record(z.string(), z.unknown())), threshold: 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 }; /** * Measure what reading capsules costs in characters, alone and read together; capsules you cannot read come back not available * * POST /api/preview/capsules/read-cost * * Also accepts auth: cookieAuth, oauth2 */ export const createApiApiPreviewCapsulesReadCost = async (args: CreateApiApiPreviewCapsulesReadCostArgs, options: CreateApiApiPreviewCapsulesReadCostOptions) => { 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/read-cost${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(createApiApiPreviewCapsulesReadCostResponse, await res.json()) };