import {z} from 'zod' export type CreateNodeOperation = {type: 'create_node', nodeType: string, params: Array, referenceId?: (string | null) | undefined, position?: ('before' | 'after' | null) | undefined}; export type BlockAttrs = {id?: (string | null) | undefined, level?: (number | null) | undefined} | {[key: string]: unknown}; export type Mark = {type: string, attrs?: (BlockAttrs | null) | undefined} | {[key: string]: unknown}; export type BlockNodeInput = {type: string, attrs?: (BlockAttrs | null) | undefined, content?: (Array | null) | undefined, marks?: (Array | null) | undefined, text?: (string | null) | undefined} | {[key: string]: unknown}; export type UpdateNodeOperation = {type: 'update_node', nodeId: string, node: BlockNodeInput}; export type DeleteNodeOperation = {type: 'delete_node', nodeId: string}; export type EditJournalRequest = {journal_id: string, operations: Array, sync_realtime_state?: boolean | undefined} | {[key: string]: unknown}; export type PatchApiV1JournalsEditorEditArgs = {body: EditJournalRequest}; export type PatchApiV1JournalsEditorEditOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const operationResultData = z.object({created: z.boolean().nullable().optional(), nodeId: z.string().nullable().optional(), updated: z.string().nullable().optional(), deleted: z.string().nullable().optional()}); export const editResults = z.object({operation: z.enum(["create_node", "update_node", "delete_node"]), success: z.boolean(), error: z.string().nullable().optional(), data: operationResultData.nullable().optional()}); export const editJournalResponse = z.object({success: z.boolean(), message: z.string().nullable().optional(), journal_id: z.string(), results: z.array(editResults), timestamp: z.string().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 }; /** * Edit Journal * * PATCH /v1/journals/editor/edit */ export const patchApiV1JournalsEditorEdit = async (args: PatchApiV1JournalsEditorEditArgs, options: PatchApiV1JournalsEditorEditOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/journals/editor/edit${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(editJournalResponse, await res.json()) };