import {z} from 'zod' export type CreateApiV1InsightsImportFileArgs = {body: {fields?: (Array<{label: string, value: string | boolean | number | Array | null, type?: ('BOOLEAN' | 'DATETIME' | 'EMAIL' | 'NPS' | 'NUMBER' | 'PERSON' | 'PHONE' | 'SELECT' | 'TEXT' | 'URL') | undefined}>) | undefined, mime_type?: string | undefined, project_id?: (string | null) | undefined, folder_id?: (string | null) | undefined, author_id?: string | undefined, title: string, url: string, cover_image_file_id?: (string | null) | undefined}}; export type CreateApiV1InsightsImportFileOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1InsightsImportFileResponse = 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 }; /** * Import file to insight * * POST /v1/insights/import/file */ export const createApiV1InsightsImportFile = async (args: CreateApiV1InsightsImportFileArgs, options?: CreateApiV1InsightsImportFileOptions) => { 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/import/file${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(createApiV1InsightsImportFileResponse, await res.json()) };