import {z} from 'zod' export type CreateApiV1InsightsInsightIdCommentsArgs = {insight_id: string, body: {body: string, body_type?: ('text' | 'html') | undefined, author_id?: string | undefined, created_at?: string | undefined, published_at?: string | undefined, thread_id?: string | undefined}}; export type CreateApiV1InsightsInsightIdCommentsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1InsightsInsightIdCommentsResponse = z.object({data: z.object({id: z.string(), url: z.string().optional(), body: z.string(), author: z.object({id: z.string(), name: z.string()}), created_at: z.string(), updated_at: z.string(), comment_type: z.enum(["page", "inline"]), thread_id: z.string(), text_range: z.object({text: z.string(), text_before: z.string().nullable(), text_after: z.string().nullable()}).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 insight comment * * POST /v1/insights/{insight_id}/comments */ export const createApiV1InsightsInsightIdComments = async (args: CreateApiV1InsightsInsightIdCommentsArgs, options?: CreateApiV1InsightsInsightIdCommentsOptions) => { const {baseUrl = "https://dovetail.com/api", 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(/\/+$/, '')}/v1/insights/${encodeURIComponent(String(args.insight_id))}/comments${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(createApiV1InsightsInsightIdCommentsResponse, await res.json()) };