import {z} from 'zod' export type Person = {version?: number | undefined, id?: (string | null) | undefined, name?: (string | null) | undefined, email?: (string | null) | undefined}; export type DocumentRequest = {version?: number | undefined, id: string, sourceId: string, createdAt?: (string | number | number | null) | undefined, updatedAt?: (string | number | number | null) | undefined, public?: boolean | undefined, ownerEmails?: Array | undefined, displayName?: (string | null) | undefined, url?: (string | null) | undefined, content?: (string | null) | undefined, filename?: (string | null) | undefined, contentType?: string | undefined, author?: (Person | null) | undefined}; export type CreateApiV1DocumentsArgs = {body: DocumentRequest}; export type CreateApiV1DocumentsOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const recordStatus = z.enum(["created", "updated"]); export const recordResponse = z.object({status: recordStatus, recordId: z.string(), id: z.string(), sourceId: z.string()}); 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 }; /** * Import Document * * POST /v1/documents */ export const createApiV1Documents = async (args: CreateApiV1DocumentsArgs, options?: CreateApiV1DocumentsOptions) => { const {baseUrl = "https://api.sybill.ai", 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/documents${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(recordResponse, await res.json()) };