import {z} from 'zod' export type DeleteApiV1InsightsInsightIdArgs = {insight_id: string}; export type DeleteApiV1InsightsInsightIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const deleteApiV1InsightsInsightIdResponse = z.object({data: z.object({id: z.string(), url: z.string().optional(), title: z.string(), type: z.literal("insight"), fields: z.array(z.object({label: z.string(), value: z.union([z.string(), z.boolean(), z.number(), z.array(z.string())]).nullable(), type: z.enum(["BOOLEAN", "DATETIME", "EMAIL", "NPS", "NUMBER", "PERSON", "PHONE", "SELECT", "TEXT", "URL"]).optional()})), created_at: z.string(), deleted: z.boolean(), project: z.object({id: z.string().nullable()}), folder: z.object({id: z.string()}).nullable(), files: z.array(z.object({id: z.string(), name: z.string(), type: z.string().nullable(), size: z.number().nullable(), status: z.enum(["completed", "pending", "failed"]), author: z.union([z.object({id: z.string(), name: z.string().optional()}), z.string().nullable()]).nullable(), created_at: z.union([z.string(), z.string().nullable()]).nullable(), url: z.string().nullable()})), cover_image: z.object({id: z.string(), name: z.string(), type: z.string().nullable(), size: z.number().nullable(), status: z.enum(["completed", "pending", "failed"]), author: z.union([z.object({id: z.string(), name: z.string().optional()}), z.string().nullable()]).nullable(), created_at: z.union([z.string(), z.string().nullable()]).nullable(), url: z.string().nullable()}).nullable()})}); 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 }; /** * Delete insight * * DELETE /v1/insights/{insight_id} */ export const deleteApiV1InsightsInsightId = async (args: DeleteApiV1InsightsInsightIdArgs, options?: DeleteApiV1InsightsInsightIdOptions) => { const {baseUrl = "https://dovetail.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/insights/${encodeURIComponent(String(args.insight_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "DELETE", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(deleteApiV1InsightsInsightIdResponse, await res.json()) };