import {z} from 'zod' export type GetApiV1LeadsApiKeyArgs = {apiKey: string}; export type GetApiV1LeadsApiKeyOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const adaptekApiApplicationLeadsQueriesGetLeadsColorOfMoneyQAndAModel = z.object({question: z.string().nullable().optional(), answer: z.string().nullable().optional()}); export const adaptekApiApplicationLeadsGetLeadResponse = z.object({agencyId: z.number().int().optional(), agencyName: z.string().nullable().optional(), agentId: z.number().int().optional(), agentName: z.string().nullable().optional(), externalAgentId: z.string().nullable().optional(), leadId: z.string().nullable().optional(), firstName: z.string().nullable().optional(), preferredName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), email: z.string().nullable().optional(), emailType: z.string().nullable().optional(), phone: z.string().nullable().optional(), phoneType: z.string().nullable().optional(), zipCode: z.string().nullable().optional(), referrer: z.string().nullable().optional(), browser: z.string().nullable().optional(), dateReceived: z.string().optional(), ipAddress: z.string().nullable().optional(), comraInfo: z.array(adaptekApiApplicationLeadsQueriesGetLeadsColorOfMoneyQAndAModel).nullable().optional(), comraScore: z.number().int().nullable().optional()}); export const getApiV1LeadsApiKeyResponse = z.array(adaptekApiApplicationLeadsGetLeadResponse); 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 }; /** * Get list of leads from FMO or Holding Company * * GET /v1/Leads/{apiKey} */ export const getApiV1LeadsApiKey = async (args: GetApiV1LeadsApiKeyArgs, options: GetApiV1LeadsApiKeyOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Leads/${encodeURIComponent(String(args.apiKey))}${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(getApiV1LeadsApiKeyResponse, await res.json()) };