import {z} from 'zod' export type CreateApiV1HouseholdsArgs = {body: {book_id: string, type: 'Client' | 'Prospect', household_name?: string | undefined, tier?: string | undefined, became_client_at?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, email?: string | undefined, phone_number?: string | undefined, phone_extension?: string | undefined, preferred_name?: string | undefined, ssn?: string | undefined, drivers_license_number?: string | undefined, tag_ids?: Array | undefined, custom_fields?: Record | undefined, head_of_household?: ({first_name?: string | undefined, last_name?: string | undefined, middle_name?: string | undefined, preferred_name?: string | undefined, salutation?: string | undefined, suffix?: string | undefined, gender?: ('female' | 'male' | 'nonbinary' | 'other' | 'prefer_not_to_say') | undefined, designations?: string | undefined, email?: string | undefined, phone_number?: string | undefined, phone_extension?: string | undefined, email_addresses?: (Array<{email?: string | undefined, email_type?: ('personal' | 'business' | 'school' | 'other' | 'inactive') | undefined, is_primary?: boolean | undefined}>) | undefined, phone_numbers?: (Array<{phone_number?: string | undefined, extension?: string | undefined, phone_type?: ('mobile' | 'work' | 'home' | 'fax' | 'other') | undefined, is_primary?: boolean | undefined}>) | undefined, addresses?: (Array<{line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, zip?: string | undefined, country_code?: string | undefined, address_type?: ('home' | 'work' | 'mailing' | 'other') | undefined, is_primary?: boolean | undefined}>) | undefined, employments?: (Array<{business_name?: string | undefined, role?: string | undefined, start_date?: string | undefined, end_date?: string | undefined, actively_employed?: boolean | undefined}>) | undefined, ssn?: string | undefined, drivers_license_number?: string | undefined, custom_fields?: Record | undefined}) | undefined}}; export type CreateApiV1HouseholdsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const email = z.object({email: z.string(), email_type: z.enum(["personal", "business", "school", "other", "inactive"]), is_primary: z.boolean(), id: z.string().optional()}); export const phoneNumber = z.object({phone_number: z.string(), extension: z.string().nullable().optional(), phone_type: z.enum(["mobile", "work", "home", "fax", "other"]), is_primary: z.boolean(), id: z.string().optional()}); export const address = z.object({line1: z.string().nullable().optional(), line2: z.string().nullable().optional(), city: z.string().nullable().optional(), state: z.string().nullable().optional(), zip: z.string().nullable().optional(), country_code: z.string().nullable().optional(), address_type: z.enum(["home", "work", "mailing", "other"]), is_primary: z.boolean(), id: z.string().optional()}); export const employment = z.object({id: z.string().optional(), person_id: z.string().optional(), business_id: z.string().nullable().optional(), business_name: z.string(), role: z.string().nullable().optional(), start_date: z.string().nullable().optional(), end_date: z.string().nullable().optional(), actively_employed: z.boolean().optional()}); export const person = z.object({id: z.string(), book_id: z.string(), household_id: z.string().nullable().optional(), contact_id: z.string().nullable().optional(), role_type: z.enum(["client", "prospect", "contact", "past_client"]).nullable().optional(), created_at: z.string(), updated_at: z.string(), first_name: z.string(), middle_name: z.string().nullable().optional(), last_name: z.string(), salutation: z.string().nullable().optional(), suffix: z.string().nullable().optional(), maiden_name: z.string().nullable().optional(), gender: z.enum(["female", "male", "nonbinary", "other", "prefer_not_to_say"]).nullable().optional(), designations: z.string().nullable().optional(), household_role: z.enum(["head_of_household", "partner", "spouse", "dependent_child", "other_adult", "other_dependent", "unknown", "deceased", "non_dependent_child", "grandchild"]).nullable().optional(), preferred_name: z.string().nullable().optional(), date_of_birth: z.string().nullable().optional(), date_of_death: z.string().nullable().optional(), job_title: z.string().nullable().optional(), ssn: z.string().nullable().optional(), drivers_license_number: z.string().nullable().optional(), email_addresses: z.array(email).optional(), phone_numbers: z.array(phoneNumber).optional(), addresses: z.array(address).optional(), employments: z.array(employment).optional(), custom_fields: z.object({}).optional()}); export const household = z.object({id: z.string(), type: z.enum(["Client", "Prospect", "PastClient"]), name: z.string(), tier: z.string().nullable().optional(), became_client_at: z.string().nullable().optional(), book_id: z.string(), profile_picture_url: z.string().nullable().optional(), custom_fields: z.object({}).optional(), people: z.array(person), tags: z.array(z.object({id: z.string().optional(), name: 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 }; /** * Create a household * * POST /v1/households */ export const createApiV1Households = async (args: CreateApiV1HouseholdsArgs, options?: CreateApiV1HouseholdsOptions) => { const {baseUrl = "https://api.slant.app", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/households${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(household, await res.json()) };