import {z} from 'zod' export type AnnotationPointGeometryInput = {type: 'point', x: number | string, y: number | string}; export type AnnotationRectGeometryInput = {x: number | string, y: number | string, w: number | string, h: number | string, type: 'rect'}; export type RectShapeInput = {x: number | string, y: number | string, w: number | string, h: number | string}; export type AnnotationCalloutGeometryInput = {type: 'callout', rect: RectShapeInput, leader: Array>}; export type AnnotationPolylineGeometryInput = {type: 'polyline', points: Array>}; export type AnnotationPolygonGeometryInput = {type: 'polygon', points: Array>}; export type AnnotationInkGeometryInput = {type: 'ink', strokes: Array>>}; export type AnnotationQuadsGeometryInput = {type: 'quads', quads: Array>}; export type BorderEffect = {cloudy: 1 | 2}; export type AnnotationFontInput = {family?: string | undefined, size_pts?: (number | string) | undefined, color?: (string | null) | undefined, align?: ('left' | 'center' | 'right') | undefined}; export type AnnotationStyleInput = {stroke_color?: string | undefined, stroke_opacity?: (number | string) | undefined, fill_color?: (string | null) | undefined, fill_opacity?: (number | string) | undefined, stroke_width?: (number | string) | undefined, dash?: (Array | null) | undefined, border_effect?: (BorderEffect | null) | undefined, line_endings?: (Array | null) | undefined, font?: (AnnotationFontInput | null) | undefined, blend?: ('normal' | 'multiply') | undefined, rotation?: (number | string) | undefined, stamp_attachment_id?: (string | null) | undefined}; export type CreateAnnotationRequest = {id?: (string | null) | undefined, page_id: string, kind: 'note' | 'text' | 'callout' | 'line' | 'polyline' | 'rect' | 'ellipse' | 'polygon' | 'ink' | 'highlight' | 'stamp', geometry: AnnotationPointGeometryInput | AnnotationRectGeometryInput | AnnotationCalloutGeometryInput | AnnotationPolylineGeometryInput | AnnotationPolygonGeometryInput | AnnotationInkGeometryInput | AnnotationQuadsGeometryInput, style?: AnnotationStyleInput | undefined, subject?: (string | null) | undefined, contents?: (string | null) | undefined, visibility?: ('private' | 'project') | undefined}; export type CreateApiApiV1ProjectsProjectIdAnnotationsArgs = {project_id: string, body: CreateAnnotationRequest}; export type CreateApiApiV1ProjectsProjectIdAnnotationsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const annotationPointGeometryOutput = z.object({type: z.literal("point"), x: z.string(), y: z.string()}); export const annotationRectGeometryOutput = z.object({x: z.string(), y: z.string(), w: z.string(), h: z.string(), type: z.literal("rect")}); export const rectShapeOutput = z.object({x: z.string(), y: z.string(), w: z.string(), h: z.string()}); export const annotationCalloutGeometryOutput = z.object({type: z.literal("callout"), rect: rectShapeOutput, leader: z.array(z.array(z.unknown()))}); export const annotationPolylineGeometryOutput = z.object({type: z.literal("polyline"), points: z.array(z.array(z.unknown()))}); export const annotationPolygonGeometryOutput = z.object({type: z.literal("polygon"), points: z.array(z.array(z.unknown()))}); export const annotationInkGeometryOutput = z.object({type: z.literal("ink"), strokes: z.array(z.array(z.array(z.unknown())))}); export const annotationQuadsGeometryOutput = z.object({type: z.literal("quads"), quads: z.array(z.array(z.string()))}); export const borderEffect = z.object({cloudy: z.union([z.literal(1), z.literal(2)])}); export const annotationFontOutput = z.object({family: z.string().optional(), size_pts: z.string().optional(), color: z.string().nullable().optional(), align: z.enum(["left", "center", "right"]).optional()}); export const annotationStyleOutput = z.object({stroke_color: z.string().optional(), stroke_opacity: z.string().optional(), fill_color: z.string().nullable().optional(), fill_opacity: z.string().optional(), stroke_width: z.string().optional(), dash: z.array(z.string()).nullable().optional(), border_effect: borderEffect.nullable().optional(), line_endings: z.array(z.unknown()).nullable().optional(), font: annotationFontOutput.nullable().optional(), blend: z.enum(["normal", "multiply"]).optional(), rotation: z.string().optional(), stamp_attachment_id: z.string().nullable().optional()}); export const sourceSummary = z.object({origin: z.string().nullable().optional(), nm: z.string().nullable().optional(), import_id: z.string().nullable().optional()}); export const annotation = z.object({id: z.string(), project_id: z.string(), page_id: z.string(), kind: z.enum(["note", "text", "callout", "line", "polyline", "rect", "ellipse", "polygon", "ink", "highlight", "stamp"]), geometry: z.discriminatedUnion("type", [annotationPointGeometryOutput, annotationRectGeometryOutput, annotationCalloutGeometryOutput, annotationPolylineGeometryOutput, annotationPolygonGeometryOutput, annotationInkGeometryOutput, annotationQuadsGeometryOutput]), style: annotationStyleOutput, subject: z.string().nullable(), contents: z.string().nullable(), status: z.enum(["accepted", "rejected", "completed", "cancelled"]).nullable(), author_id: z.string().nullable(), author_name: z.string(), author_kind: z.enum(["user", "agent", "imported"]), visibility: z.enum(["private", "project"]), locked: z.boolean(), bbox_x: z.string(), bbox_y: z.string(), bbox_w: z.string(), bbox_h: z.string(), display_kind: z.string(), row_version: z.number().int(), created_at: z.string(), updated_at: z.string(), deleted_at: z.string().nullable(), source: sourceSummary.nullable().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 }; /** * Create Annotation * * POST /api/v1/projects/{project_id}/annotations */ export const createApiApiV1ProjectsProjectIdAnnotations = async (args: CreateApiApiV1ProjectsProjectIdAnnotationsArgs, options: CreateApiApiV1ProjectsProjectIdAnnotationsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/projects/${encodeURIComponent(String(args.project_id))}/annotations${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(annotation, await res.json()) };