import {z} from 'zod' export type DealStatus = 1 | 2 | 3 | 4; export type QueryPrefix = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16; export type DataFieldFilter = {op?: QueryPrefix | undefined, val: unknown}; export type DepartmentPageSelection = Record; export type ViewType = 0 | 1; export type GetApiV1DealsArgs = {Id?: number | undefined, PipelineId?: number | undefined, StageId?: number | undefined, StageIds?: Array | undefined, Name?: string | undefined, Content?: string | undefined, Notes?: string | undefined, FromValue?: number | undefined, ToValue?: number | undefined, Source?: number | undefined, Status?: Array | undefined, ContactId?: number | undefined, ContactEmail?: string | undefined, CompanyId?: number | undefined, CreatedUserId?: number | undefined, OwnerUserId?: number | undefined, OwnerGroupId?: number | undefined, FollowerIds?: string | undefined, CategoryIds?: string | undefined, Currency?: string | undefined, LostReason?: string | undefined, IsSpam?: number | undefined, FromDate?: string | undefined, ToDate?: string | undefined, FromUpdatedDate?: string | undefined, ToUpdatedDate?: string | undefined, FromClosingDate?: string | undefined, ToClosingDate?: string | undefined, FromExpectedCloseDate?: string | undefined, ToExpectedCloseDate?: string | undefined, CustomFields?: ({[key: string]: Array | null}) | undefined, Q?: string | undefined, Fields?: string | undefined, DepartmentScope?: DepartmentPageSelection | undefined, ItemsPerPage?: number | undefined, ViewType?: ViewType | undefined, Page?: number | undefined, ReturnTotalItems?: boolean | undefined, sortCol?: string | undefined, sortDir?: string | undefined, isExport?: boolean | undefined}; export type GetApiV1DealsOptions = {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 dealResponseModel = z.object({id: z.number().int().nullable().optional(), name: z.string().nullable().optional(), contactId: z.number().int().nullable().optional(), contactName: z.string().nullable().optional(), contactEmail: z.string().nullable().optional(), contactPhone: z.string().nullable().optional(), companyId: z.number().int().nullable().optional(), companyName: z.string().nullable().optional(), categoryIds: z.string().nullable().optional(), contactJobTitle: z.string().nullable().optional(), categoryNames: z.string().nullable().optional(), pipelineId: z.number().int().nullable().optional(), stageId: z.number().int().nullable().optional(), departmentId: z.number().int().nullable().optional(), orderNo: z.number().int().nullable().optional(), createdUserId: z.number().int().nullable().optional(), createdDate: z.string().nullable().optional(), updatedUserId: z.number().int().nullable().optional(), updatedDate: z.string().nullable().optional(), ownerUserId: z.number().int().nullable().optional(), ownerGroupId: z.number().int().nullable().optional(), ownerName: z.string().nullable().optional(), ownerPhoto: z.string().nullable().optional(), ownerColor: z.string().nullable().optional(), followerIds: z.string().nullable().optional(), expectedCloseDate: z.string().nullable().optional(), closingDate: z.string().nullable().optional(), value: z.number().nullable().optional(), currency: z.string().nullable().optional(), notes: z.string().nullable().optional(), lostReason: z.string().nullable().optional(), source: z.number().int().nullable().optional(), status: dealStatus.optional(), linkedType: table.optional(), linkedId: z.number().int().nullable().optional(), customFields: z.string().nullable().optional(), isSpam: z.number().int().nullable().optional(), highlights: z.record(z.string(), z.array(z.string()).nullable()).nullable().optional()}); export const dealResponseModelQueryResult = z.object({totalItems: z.number().int().optional(), items: z.array(dealResponseModel).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 }; /** * List all existing deals * * GET /v1/deals * * Also accepts auth: API Key */ export const getApiV1Deals = async (args: GetApiV1DealsArgs, options: GetApiV1DealsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Id != null) query.append("Id", String(args.Id)) if (args.PipelineId != null) query.append("PipelineId", String(args.PipelineId)) if (args.StageId != null) query.append("StageId", String(args.StageId)) for (const item of args.StageIds ?? []) query.append("StageIds", String(item)) if (args.Name != null) query.append("Name", String(args.Name)) if (args.Content != null) query.append("Content", String(args.Content)) if (args.Notes != null) query.append("Notes", String(args.Notes)) if (args.FromValue != null) query.append("FromValue", String(args.FromValue)) if (args.ToValue != null) query.append("ToValue", String(args.ToValue)) if (args.Source != null) query.append("Source", String(args.Source)) for (const item of args.Status ?? []) query.append("Status", String(item)) if (args.ContactId != null) query.append("ContactId", String(args.ContactId)) if (args.ContactEmail != null) query.append("ContactEmail", String(args.ContactEmail)) if (args.CompanyId != null) query.append("CompanyId", String(args.CompanyId)) if (args.CreatedUserId != null) query.append("CreatedUserId", String(args.CreatedUserId)) if (args.OwnerUserId != null) query.append("OwnerUserId", String(args.OwnerUserId)) if (args.OwnerGroupId != null) query.append("OwnerGroupId", String(args.OwnerGroupId)) if (args.FollowerIds != null) query.append("FollowerIds", String(args.FollowerIds)) if (args.CategoryIds != null) query.append("CategoryIds", String(args.CategoryIds)) if (args.Currency != null) query.append("Currency", String(args.Currency)) if (args.LostReason != null) query.append("LostReason", String(args.LostReason)) if (args.IsSpam != null) query.append("IsSpam", String(args.IsSpam)) if (args.FromDate != null) query.append("FromDate", String(args.FromDate)) if (args.ToDate != null) query.append("ToDate", String(args.ToDate)) if (args.FromUpdatedDate != null) query.append("FromUpdatedDate", String(args.FromUpdatedDate)) if (args.ToUpdatedDate != null) query.append("ToUpdatedDate", String(args.ToUpdatedDate)) if (args.FromClosingDate != null) query.append("FromClosingDate", String(args.FromClosingDate)) if (args.ToClosingDate != null) query.append("ToClosingDate", String(args.ToClosingDate)) if (args.FromExpectedCloseDate != null) query.append("FromExpectedCloseDate", String(args.FromExpectedCloseDate)) if (args.ToExpectedCloseDate != null) query.append("ToExpectedCloseDate", String(args.ToExpectedCloseDate)) if (args.CustomFields != null) query.append("CustomFields", JSON.stringify(args.CustomFields)) if (args.Q != null) query.append("Q", String(args.Q)) if (args.Fields != null) query.append("Fields", String(args.Fields)) if (args.DepartmentScope != null) query.append("DepartmentScope", JSON.stringify(args.DepartmentScope)) if (args.ItemsPerPage != null) query.append("ItemsPerPage", String(args.ItemsPerPage)) if (args.ViewType != null) query.append("ViewType", String(args.ViewType)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.ReturnTotalItems != null) query.append("ReturnTotalItems", String(args.ReturnTotalItems)) if (args.sortCol != null) query.append("sortCol", String(args.sortCol)) if (args.sortDir != null) query.append("sortDir", String(args.sortDir)) if (args.isExport != null) query.append("isExport", String(args.isExport)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/deals${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(dealResponseModelQueryResult, await res.json()) };