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 Deal = {id?: number | undefined, name: string, contactId?: number | undefined, companyId?: (number | null) | undefined, pipelineId: number, stageId: number, departmentId?: number | undefined, orderNo: number, createdUserId?: number | undefined, createdDate?: string | undefined, updatedUserId?: number | undefined, updatedDate?: string | undefined, createdContactId?: number | undefined, createdAutomationId?: number | undefined, ownerUserId?: number | undefined, ownerGroupId?: (number | null) | undefined, followerIds?: (string | null) | undefined, expectedCloseDate?: (string | null) | undefined, closingDate?: (string | null) | undefined, value?: number | undefined, currency?: (string | null) | undefined, notes?: (string | null) | undefined, lostReason?: (string | null) | undefined, source: number, status: DealStatus, lastStatusChangeDate?: (string | null) | undefined, linkedType?: Table | undefined, linkedId?: number | undefined, customFields?: (string | null) | undefined, categoryIds?: (string | null) | undefined, isSpam?: (number | null) | undefined}; export type CreateApiV1DealsArgs = {body: Deal}; export type CreateApiV1DealsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dealStatus = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4)]); export const table = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(5), z.literal(6), z.literal(7), z.literal(10), z.literal(11), z.literal(12), z.literal(13), z.literal(14), z.literal(15), z.literal(16), z.literal(19), z.literal(20), z.literal(21), z.literal(22), z.literal(23), z.literal(25), z.literal(26), z.literal(27), z.literal(31), z.literal(32), z.literal(34), z.literal(35), z.literal(36), z.literal(37), z.literal(38), z.literal(39), z.literal(40), z.literal(41), z.literal(42), z.literal(43), z.literal(44), z.literal(45), z.literal(46), z.literal(47), z.literal(48), z.literal(50), z.literal(51), z.literal(52), z.literal(53), z.literal(55), z.literal(60), z.literal(61), z.literal(62), z.literal(63), z.literal(64), z.literal(65), z.literal(66), z.literal(67), z.literal(68), z.literal(69), z.literal(72)]); export const deal = z.object({id: z.number().int().optional(), name: z.string().min(1).max(512), contactId: z.number().int().optional(), companyId: z.number().int().nullable().optional(), pipelineId: z.number().int(), stageId: z.number().int(), departmentId: z.number().int().optional(), orderNo: z.number().int(), createdUserId: z.number().int().optional(), createdDate: z.string().optional(), updatedUserId: z.number().int().optional(), updatedDate: z.string().optional(), createdContactId: z.number().int().optional(), createdAutomationId: z.number().int().optional(), ownerUserId: z.number().int().optional(), ownerGroupId: z.number().int().nullable().optional(), followerIds: z.string().max(128).nullable().optional(), expectedCloseDate: z.string().nullable().optional(), closingDate: z.string().nullable().optional(), value: z.number().optional(), currency: z.string().max(3).nullable().optional(), notes: z.string().max(10000).nullable().optional(), lostReason: z.string().max(128).nullable().optional(), source: z.number().int(), status: dealStatus, lastStatusChangeDate: z.string().nullable().optional(), linkedType: table.optional(), linkedId: z.number().int().optional(), customFields: z.string().max(4096).nullable().optional(), categoryIds: z.string().max(128).nullable().optional(), isSpam: z.number().int().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 deal * * POST /v1/deals * * Also accepts auth: API Key */ export const createApiV1Deals = async (args: CreateApiV1DealsArgs, options: CreateApiV1DealsOptions) => { 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${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(deal, await res.json()) };