import {z} from 'zod' export type GetApiV1DealsArgs = {name?: (string | null) | undefined, stage?: (string | null) | undefined, closed?: (boolean | null) | undefined, owner?: (string | null) | undefined, closeDateBefore?: (string | null) | undefined, closeDateAfter?: (string | null) | undefined, amountMin?: (number | null) | undefined, amountMax?: (number | null) | undefined, lastActivityBefore?: (string | null) | undefined, lastActivityAfter?: (string | null) | undefined, limit?: number | undefined, cursor?: (string | null) | undefined}; export type GetApiV1DealsOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dealOwner = z.object({name: z.string().nullable().optional(), email: z.string().nullable().optional()}); export const dealListItem = z.object({dealId: z.string(), name: z.string().nullable().optional(), accountName: z.string().nullable().optional(), stage: z.string().nullable().optional(), pipeline: z.string().nullable().optional(), amount: z.number().nullable().optional(), closeDate: z.string().nullable().optional(), closed: z.boolean().nullable().optional(), createdDate: z.string().nullable().optional(), lastActivityDate: z.string().nullable().optional(), owner: dealOwner.nullable().optional()}); export const paginationInfo = z.object({nextCursor: z.string().nullable().optional(), hasMore: z.boolean().optional()}); export const listDealsResponse = z.object({deals: z.array(dealListItem), pagination: paginationInfo}); 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 Deals * * GET /v1/deals */ export const getApiV1Deals = async (args: GetApiV1DealsArgs, options?: GetApiV1DealsOptions) => { const {baseUrl = "https://api.sybill.ai", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) if (args.stage != null) query.append("stage", String(args.stage)) if (args.closed != null) query.append("closed", String(args.closed)) if (args.owner != null) query.append("owner", String(args.owner)) if (args.closeDateBefore != null) query.append("closeDateBefore", String(args.closeDateBefore)) if (args.closeDateAfter != null) query.append("closeDateAfter", String(args.closeDateAfter)) if (args.amountMin != null) query.append("amountMin", String(args.amountMin)) if (args.amountMax != null) query.append("amountMax", String(args.amountMax)) if (args.lastActivityBefore != null) query.append("lastActivityBefore", String(args.lastActivityBefore)) if (args.lastActivityAfter != null) query.append("lastActivityAfter", String(args.lastActivityAfter)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) 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(listDealsResponse, await res.json()) };