import {z} from 'zod' export type GetApiApiV1ProspectsArgs = {opportunityType?: ('regular' | 'speculative' | 'job' | 'generic') | undefined, opportunityId?: string | undefined, createdAfter?: (string | string) | undefined, createdBefore?: (string | string) | undefined, updatedAfter?: (string | string) | undefined, updatedBefore?: (string | string) | undefined, includeDeleted?: ('true' | 'false') | undefined, page?: number | undefined, pageSize?: number | undefined, perPage?: number | undefined}; export type GetApiApiV1ProspectsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1ProspectsResponse = z.object({status: z.literal("ok"), data: z.array(z.object({id: z.string(), personId: z.string(), firstName: z.string().nullable(), lastName: z.string().nullable(), headlineRole: z.string().nullable(), headlineCompanyName: z.string().nullable(), opportunity: z.object({id: z.string(), name: z.string(), type: z.enum(["regular", "speculative", "job", "generic"])}), addedBy: z.object({id: z.string(), name: z.string(), email: z.string()}).nullable(), stage: z.object({id: z.string(), key: z.string().nullable(), name: z.string(), position: z.number().int()}).nullable(), stageUpdatedAt: z.string().nullable(), createdAt: z.string(), updatedAt: z.string(), deletedAt: z.string().nullable()})), pagination: z.object({page: z.number().int(), pageSize: z.number().int(), total: z.number().int(), totalPages: z.number().int()})}); 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 prospects * * GET /api/v1/prospects */ export const getApiApiV1Prospects = async (args: GetApiApiV1ProspectsArgs, options: GetApiApiV1ProspectsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.opportunityType != null) query.append("opportunityType", String(args.opportunityType)) if (args.opportunityId != null) query.append("opportunityId", String(args.opportunityId)) if (args.createdAfter != null) query.append("createdAfter", String(args.createdAfter)) if (args.createdBefore != null) query.append("createdBefore", String(args.createdBefore)) if (args.updatedAfter != null) query.append("updatedAfter", String(args.updatedAfter)) if (args.updatedBefore != null) query.append("updatedBefore", String(args.updatedBefore)) if (args.includeDeleted != null) query.append("includeDeleted", String(args.includeDeleted)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/prospects${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(getApiApiV1ProspectsResponse, await res.json()) };