import {z} from 'zod' export type HouseholdStoreRequest = {name: string, head_contact_id?: (number | null) | undefined, note?: (string | null) | undefined, envelope_name?: (string | null) | undefined}; export type CreateApiV1HouseholdsArgs = {body: HouseholdStoreRequest}; export type CreateApiV1HouseholdsOptions = {baseUrl?: string | undefined, http?: string | undefined, fetch?: typeof fetch | undefined}; export const pointOfContactResource = z.object({id: z.string(), type: z.string(), prefix: z.string(), first_name: z.string(), middle_name: z.string(), last_name: z.string(), suffix: z.string(), gender: z.string(), pronouns: z.string(), dob: z.string(), employer: z.string(), title: z.string(), twitter_url: z.string(), linkedin_url: z.string(), facebook_url: z.string(), tiktok_url: z.string(), instagram_url: z.string(), website_url: z.string(), emails: z.record(z.string(), z.unknown()), phones: z.record(z.string(), z.unknown()), primary_email: z.string(), primary_phone: z.string(), is_email_subscribed: z.string(), is_phone_subscribed: z.string(), is_address_subscribed: z.string(), address_unsubscribed_at: z.string(), archived_at: z.string(), created_at: z.string(), updated_at: z.string(), first_time_supporter_at: z.string()}); export const addressResource = z.object({id: z.number().int(), account_id: z.number().int().nullable(), name: z.string().nullable(), address_1: z.string(), address_2: z.string().nullable(), city: z.string(), state: z.string().nullable(), zipcode: z.string().nullable(), country: z.string(), type: z.string(), is_primary: z.boolean(), created_at: z.string().nullable(), updated_at: z.string().nullable()}); export const associatedCompanyResource = z.object({id: z.string(), type: z.string(), company_name: z.string(), name_display: z.string(), title: z.string(), twitter_url: z.string(), linkedin_url: z.string(), facebook_url: z.string(), tiktok_url: z.string(), instagram_url: z.string(), website_url: z.string(), image_url: z.string().nullable(), emails: z.record(z.string(), z.unknown()), phones: z.record(z.string(), z.unknown()), primary_email: z.string(), primary_phone: z.string(), is_email_subscribed: z.string(), is_phone_subscribed: z.string(), is_address_subscribed: z.string(), address_unsubscribed_at: z.string(), note: z.string(), addresses: z.array(addressResource), primary_address: addressResource, archived_at: z.string(), created_at: z.string(), updated_at: z.string(), first_time_supporter_at: z.string()}); export const emailResource = z.object({type: z.string().nullable(), value: z.string().nullable()}); export const phoneResource = z.object({type: z.string().nullable(), value: z.string().nullable()}); export const appHttpResourcesAPIAddressResource = z.object({address_1: z.string(), address_2: z.string().nullable(), city: z.string(), state: z.string().nullable(), zipcode: z.string().nullable(), country: z.string(), type: z.string(), is_primary: z.boolean(), created_at: z.string().nullable(), updated_at: z.string().nullable()}); export const externalIdResource = z.object({id: z.number().int(), label: z.string(), external_id: z.string(), created_at: z.string(), updated_at: z.string()}); export const contactResource = z.object({id: z.string(), external_id: z.string(), contact_since: z.string(), type: z.string(), prefix: z.string(), first_name: z.string(), preferred_name: z.string(), middle_name: z.string(), last_name: z.string(), suffix: z.string(), gender: z.string(), pronouns: z.string(), dob: z.string(), company: z.string(), employer: z.string(), company_name: z.string(), point_of_contact: pointOfContactResource.nullable().optional(), associated_companies: z.array(associatedCompanyResource).nullable().optional(), title: z.string(), website_url: z.string(), twitter_url: z.string(), linkedin_url: z.string(), facebook_url: z.string(), tiktok_url: z.string(), instagram_url: z.string(), emails: z.array(emailResource), phones: z.array(phoneResource), primary_email: z.string(), primary_phone: z.string(), note: z.string(), addresses: z.array(appHttpResourcesAPIAddressResource), primary_address: appHttpResourcesAPIAddressResource.nullable(), last_donation_amount: z.string(), stats: z.object({total_contributions: z.string(), recurring_contributions: z.string()}), tags: z.string(), custom_fields: z.array(z.unknown()), external_ids: z.array(externalIdResource), is_email_subscribed: z.string(), is_phone_subscribed: z.string(), is_address_subscribed: z.string(), email_opt_in: z.string(), sms_opt_in: z.string(), address_unsubscribed_at: z.string(), archived_at: z.string(), created_at: z.string(), updated_at: z.string(), salutation_name: z.string()}); export const appHttpResourcesAPIHouseholdResource = z.object({id: z.number().int(), name: z.string(), head_contact_id: z.number().int().nullable(), account_id: z.number().int(), envelope_name: z.string().nullable(), note: z.string().nullable(), created_at: z.string(), updated_at: z.string(), contacts: z.array(contactResource).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.givebutter.com", fetch: fetchFn = fetch, http} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (http !== undefined) headers.set('authorization', `Bearer ${http}`) 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(appHttpResourcesAPIHouseholdResource, await res.json()) };