import {z} from 'zod' export type GetApiApiExternalV1LeadsSearchArgs = {workspace_id: number, search?: (string | null) | undefined, campaign_ids?: Array | undefined, audience_ids?: Array | undefined, engagement?: Array | undefined, limit?: (number | null) | undefined}; export type GetApiApiExternalV1LeadsSearchOptions = {baseUrl?: string | undefined, defaultValue?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiExternalV1LeadsSearchResponse = z.object({total: z.number().int().optional(), returned: z.number().int().optional(), leads: z.array(z.object({lead_id: z.number().int().optional(), name: z.string().optional(), email: z.string().optional(), company_name: z.string().optional(), company_domain: z.string().optional(), genpage_url: z.string().optional(), created_at: z.string().optional()})).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 }; /** * Search the leads in a workspace. * * GET /api/external/v1/leads/search */ export const getApiApiExternalV1LeadsSearch = async (args: GetApiApiExternalV1LeadsSearchArgs, options?: GetApiApiExternalV1LeadsSearchOptions) => { const {baseUrl = "https://backend.genpage.ai", fetch: fetchFn = fetch, defaultValue} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.workspace_id != null) query.append("workspace_id", String(args.workspace_id)) if (args.search != null) query.append("search", String(args.search)) for (const item of args.campaign_ids ?? []) query.append("campaign_ids", String(item)) for (const item of args.audience_ids ?? []) query.append("audience_ids", String(item)) for (const item of args.engagement ?? []) query.append("engagement", String(item)) if (args.limit != null) query.append("limit", String(args.limit)) if (defaultValue !== undefined) headers.set('authorization', `Bearer ${defaultValue}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/external/v1/leads/search${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(getApiApiExternalV1LeadsSearchResponse, await res.json()) };