import {z} from 'zod' export type GetApiV1ContactsContactArgs = {contact: number}; export type GetApiV1ContactsContactOptions = {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 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 a contact * * GET /v1/contacts/{contact} */ export const getApiV1ContactsContact = async (args: GetApiV1ContactsContactArgs, options?: GetApiV1ContactsContactOptions) => { const {baseUrl = "https://api.givebutter.com", fetch: fetchFn = fetch, http} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (http !== undefined) headers.set('authorization', `Bearer ${http}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/contacts/${encodeURIComponent(String(args.contact))}${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(contactResource, await res.json()) };