import {z} from 'zod' export type Lead = {id?: string | undefined, integration?: string | undefined, srcId?: string | undefined, email?: (string | null) | undefined, firstName?: (string | null) | undefined, lastName?: (string | null) | undefined, primaryPhone?: (string | null) | undefined, addressLine1?: (string | null) | undefined, city?: (string | null) | undefined, country?: (string | null) | undefined, company?: (string | null) | undefined, postalCode?: (string | null) | undefined, website?: (string | null) | undefined, industry?: (string | null) | undefined, skypeUsername?: (string | null) | undefined, twitter?: (string | null) | undefined, annualRevenue?: (string | null) | undefined, employeeCount?: (number | null) | undefined, created?: (string | null) | undefined, modified?: (string | null) | undefined}; export type CreateApiLeadsArgs = {body: Lead}; export type CreateApiLeadsOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const lead = z.object({id: z.string().optional(), integration: z.string().optional(), srcId: z.string().optional(), email: z.string().nullable().optional(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), primaryPhone: z.string().nullable().optional(), addressLine1: z.string().nullable().optional(), city: z.string().nullable().optional(), country: z.string().nullable().optional(), company: z.string().nullable().optional(), postalCode: z.string().nullable().optional(), website: z.string().nullable().optional(), industry: z.string().nullable().optional(), skypeUsername: z.string().nullable().optional(), twitter: z.string().nullable().optional(), annualRevenue: z.string().nullable().optional(), employeeCount: z.number().int().nullable().optional(), created: z.string().nullable().optional(), modified: z.string().nullable().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 }; /** * Creates a Lead resource. * * POST /leads */ export const createApiLeads = async (args: CreateApiLeadsArgs, options?: CreateApiLeadsOptions) => { const {baseUrl = "https://example.myintranetapps.com/api", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/leads${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(lead, await res.json()) };