import {z} from 'zod' export type DealStatus = 1 | 2 | 3 | 4; export type Table = 0 | 1 | 2 | 5 | 6 | 7 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 19 | 20 | 21 | 22 | 23 | 25 | 26 | 27 | 31 | 32 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 50 | 51 | 52 | 53 | 55 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 72; export type DealResponseModel = {id?: (number | null) | undefined, name?: (string | null) | undefined, contactId?: (number | null) | undefined, contactName?: (string | null) | undefined, contactEmail?: (string | null) | undefined, contactPhone?: (string | null) | undefined, companyId?: (number | null) | undefined, companyName?: (string | null) | undefined, categoryIds?: (string | null) | undefined, contactJobTitle?: (string | null) | undefined, categoryNames?: (string | null) | undefined, pipelineId?: (number | null) | undefined, stageId?: (number | null) | undefined, departmentId?: (number | null) | undefined, orderNo?: (number | null) | undefined, createdUserId?: (number | null) | undefined, createdDate?: (string | null) | undefined, updatedUserId?: (number | null) | undefined, updatedDate?: (string | null) | undefined, ownerUserId?: (number | null) | undefined, ownerGroupId?: (number | null) | undefined, ownerName?: (string | null) | undefined, ownerPhoto?: (string | null) | undefined, ownerColor?: (string | null) | undefined, followerIds?: (string | null) | undefined, expectedCloseDate?: (string | null) | undefined, closingDate?: (string | null) | undefined, value?: (number | null) | undefined, currency?: (string | null) | undefined, notes?: (string | null) | undefined, lostReason?: (string | null) | undefined, source?: (number | null) | undefined, status?: DealStatus | undefined, linkedType?: Table | undefined, linkedId?: (number | null) | undefined, customFields?: (string | null) | undefined, isSpam?: (number | null) | undefined, highlights?: ({[key: string]: Array | null} | null) | undefined}; export type CreateApiV1DealsInsertbulkArgs = {body: Array}; export type CreateApiV1DealsInsertbulkOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const insertBulkError = z.object({row: z.number().int().optional(), error: z.string().nullable().optional()}); export const insertBulkResponse = z.object({inserted: z.number().int().optional(), updated: z.number().int().optional(), errors: z.array(insertBulkError).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 }; /** * POST /v1/deals/insertbulk * * Also accepts auth: API Key */ export const createApiV1DealsInsertbulk = async (args: CreateApiV1DealsInsertbulkArgs, options: CreateApiV1DealsInsertbulkOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/deals/insertbulk${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(insertBulkResponse, await res.json()) };