import {z} from 'zod' export type Location = {stateCode?: string | undefined, city?: string | undefined, zipCode?: string | undefined, county?: string | undefined, streetAddress?: string | undefined, description?: string | undefined}; export type LeadInquiry = {priceMin?: number | undefined, priceMax?: number | undefined, propertyType?: Array | undefined, bedroomsMin?: number | undefined, bathroomsMin?: string | undefined, locations?: Array | undefined, id?: number | undefined, leadUserId?: number | undefined, bedroomsMax?: number | undefined, bathroomsMax?: string | undefined, modifyByAgent?: boolean | undefined, createTime?: string | undefined, updateTime?: string | undefined, defaultValue?: boolean | undefined}; export type LeadProperty = {price?: number | undefined, state?: string | undefined, city?: string | undefined, streetAddress?: string | undefined, zipCode?: string | undefined, county?: string | undefined, propertyType?: string | undefined, bedrooms?: number | undefined, bathrooms?: number | undefined, squareFeet?: number | undefined, lotSize?: number | undefined, parkingSpace?: number | undefined, floors?: number | undefined, mailAddress?: boolean | undefined, id?: number | undefined, leadUserId?: number | undefined, listingId?: string | undefined, autoListingId?: number | undefined, label?: string | undefined, note?: string | undefined, listingStatus?: string | undefined, labelList?: string | undefined, pictureUrl?: string | undefined, siteListingUrl?: string | undefined, labelType?: string | undefined, priceMax?: number | undefined, priceMin?: number | undefined}; export type CustomAttribute = {attributeName: string, attributeType: string, value: string, params?: string | undefined}; export type CustomFieldRequest = {id?: number | undefined, value?: string | undefined}; export type Commission = {id?: number | undefined, splitPayType?: ('TEAM' | 'AGENT' | 'VENDOR_PARTNER' | 'LEAD' | 'OTHER' | 'BROKERAGE' | 'UPLINE') | undefined, splitPayTo?: number | undefined, splitPayToName?: string | undefined, payType?: ('TEAM' | 'AGENT' | 'VENDOR_PARTNER' | 'LEAD' | 'OTHER' | 'BROKERAGE' | 'UPLINE') | undefined, payTo?: number | undefined, payToName?: string | undefined, value?: number | undefined, calculateType?: ('PERCENT' | 'AMOUNT') | undefined, desc?: string | undefined}; export type Commissions = {preSplit?: Array | undefined, split?: Array | undefined, postSplit?: Array | undefined}; export type LeadTransaction = {created?: number | undefined, updated?: number | undefined, transactionId?: number | undefined, transactionName: string, leadName?: string | undefined, assignedAgent?: number | undefined, transactionType?: ('Purchase' | 'Listing' | 'Lease' | 'Other') | undefined, homePrice?: number | undefined, transactionStatus?: string | undefined, expectedCloseDate?: number | undefined, closeDate?: number | undefined, commissionRate?: number | undefined, gci?: number | undefined, teamRevenue?: number | undefined, agentRevenue?: number | undefined, appointmentDate?: number | undefined, agreementSignedDate?: number | undefined, offerDate?: number | undefined, contractDate?: number | undefined, appraisalDate?: number | undefined, homeInspectionDate?: number | undefined, escrowDate?: number | undefined, expiration?: number | undefined, customFields?: Array | undefined, commissions?: Commissions | undefined}; export type LeadFamilyMember = {relationship?: string | undefined, firstName: string, lastName?: string | undefined, phones?: Array | undefined, emails?: Array | undefined, birthday?: string | undefined}; export type LeadRequest = {cannotText?: boolean | undefined, cannotCall?: boolean | undefined, cannotEmail?: boolean | undefined, firstName: string, lastName?: string | undefined, emails?: Array | undefined, phones?: Array | undefined, leadTypes?: Array | undefined, assignedUserId?: number | undefined, streetAddress?: string | undefined, city?: string | undefined, state?: string | undefined, zipCode?: string | undefined, referredBy?: string | undefined, stage?: string | undefined, groups?: Array | undefined, segments?: Array | undefined, tags?: Array | undefined, tagsAdd?: Array | undefined, source?: string | undefined, inquiry?: LeadInquiry | undefined, property?: LeadProperty | undefined, unsubscription?: boolean | undefined, welcomeEmail?: boolean | undefined, leadAlert?: boolean | undefined, customAttributeList?: Array | undefined, birthday?: string | undefined, leadRouting?: boolean | undefined, assignGroupId?: number | undefined, buyingTimeFrame?: string | undefined, preQual?: string | undefined, houseToSell?: string | undefined, fthb?: string | undefined, withBuyerAgent?: string | undefined, sellingTimeFrame?: string | undefined, mortgage?: string | undefined, buyHouse?: string | undefined, withListingAgent?: string | undefined, leadTransaction?: LeadTransaction | undefined, leadFamilyMemberList?: Array | undefined, language?: string | undefined, content?: string | undefined, isPin?: boolean | undefined, ownershipId?: number | undefined, ownershipScope?: string | undefined, defaultInquiryParam?: boolean | undefined, specifiedAssignGroupId?: boolean | undefined}; export type CreateApiV10LeadsArgs = {Authorization: string, 'Content-Type': string, body: LeadRequest}; export type CreateApiV10LeadsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const leadIdResponse = z.object({leadId: z.number().int().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 }; /** * Create Lead * * POST /v1.0/leads */ export const createApiV10Leads = async (args: CreateApiV10LeadsArgs, options?: CreateApiV10LeadsOptions) => { const {baseUrl = "https://api.lofty.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1.0/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(leadIdResponse, await res.json()) };