import {z} from 'zod' export type GetApiSalesLeadsArgs = {active?: boolean | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiSalesLeadsOptions = {baseUrl?: string | undefined, accountId?: string | undefined, authToken?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationStats = z.object({totalCount: z.number().int().optional(), page: z.number().int().optional(), perPage: z.number().int().optional(), totalPages: z.number().int().optional(), prevPage: z.number().int().nullable().optional(), nextPage: z.number().int().nullable().optional(), outOfRange: z.boolean().optional()}); export const namedRef = z.object({id: z.string().optional(), name: z.string().optional()}); export const party = z.object({id: z.string().optional(), name: z.string().nullable().optional(), initials: z.string().nullable().optional()}); export const salesLead = z.object({id: z.string().optional(), title: z.string().optional(), details: z.string().nullable().optional(), workflowState: z.string().nullable().optional(), nextContactDate: z.string().nullable().optional(), expectedCloseDate: z.string().nullable().optional(), qualified: z.boolean().nullable().optional(), dealValueInDollars: z.number().nullable().optional(), createdAt: z.string().optional(), createdById: z.string().nullable().optional(), updatedById: z.string().nullable().optional(), companyId: z.string().nullable().optional(), company: namedRef.optional(), leadSourceId: z.string().nullable().optional(), leadSource: namedRef.optional(), salesRepId: z.string().nullable().optional(), salesRep: party.optional(), pipelineStageId: z.string().nullable().optional(), pipelineStage: namedRef.optional(), primaryContactId: z.string().nullable().optional(), primaryContact: z.object({id: z.string().optional(), name: z.string().optional(), email: z.string().nullable().optional()}).optional(), pipelineId: z.string().nullable().optional(), pipeline: z.object({id: z.string().optional(), name: z.string().optional(), stages: z.array(z.object({id: z.string().optional(), name: z.string().optional(), position: z.number().int().optional()})).optional()}).optional(), assignedToId: z.string().nullable().optional(), assignedTo: party.optional(), divisionId: z.string().nullable().optional(), division: z.record(z.string(), z.unknown()).optional()}); export const getApiSalesLeadsResponse = z.object({stats: paginationStats.optional(), salesLeads: z.array(salesLead).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 sales leads * * GET /sales_leads */ export const getApiSalesLeads = async (args: GetApiSalesLeadsArgs, options?: GetApiSalesLeadsOptions) => { const {baseUrl = "https://api.shopvox.com/v1", fetch: fetchFn = fetch, accountId, authToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.active != null) query.append("active", String(args.active)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (accountId !== undefined) query.set("account_id", accountId) if (authToken !== undefined) query.set("authToken", authToken) const url = `${baseUrl.replace(/\/+$/, '')}/sales_leads${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(getApiSalesLeadsResponse, await res.json()) };