import {z} from 'zod' export type GetApiAccountsIdContactArgs = {id: string}; export type GetApiAccountsIdContactOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const personName = z.object({first: z.string().optional(), middle: z.string().optional(), last: z.string().optional(), suffix: z.string().optional(), prefix: z.string().optional(), company: z.string().optional()}); export const customerRelationship = z.enum(["AUTHORIZED_USER", "BUSINESS", "PRIMARY"]); export const accountHolderType = z.enum(["BUSINESS", "CONSUMER"]); export const businessCustomer = z.object({name: z.string(), registeredId: z.string().optional()}); export const accountAddress = z.object({line1: z.string(), line2: z.string().optional(), line3: z.string().optional(), city: z.string(), region: z.string(), postalCode: z.string(), country: z.string()}); export const phoneNumberType = z.enum(["HOME", "BUSINESS", "CELL", "FAX"]); export const accountPhone = z.object({type: phoneNumberType, country: z.string().optional(), number: z.string()}); export const accountHolder = z.object({name: personName, relationship: customerRelationship, type: accountHolderType, businessCustomer: businessCustomer.optional(), emails: z.array(z.string()).optional(), addresses: z.array(accountAddress).optional(), telephones: z.array(accountPhone).optional()}); export const getAccountContactResponse = z.object({holders: z.array(accountHolder).optional(), emails: z.array(z.string()).optional(), addresses: z.array(accountAddress).optional(), telephones: z.array(accountPhone).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 }; /** * Get account contact * * GET /accounts/{id}/contact */ export const getApiAccountsIdContact = async (args: GetApiAccountsIdContactArgs, options?: GetApiAccountsIdContactOptions) => { const {baseUrl = "https://api.givechariot.com/fdx/v6", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.id))}/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(getAccountContactResponse, await res.json()) };