import {z} from 'zod' export type GetApiEntityGroupsEntityTokenOrExternalIdOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getEntityGroupResponse = z.object({entity_group_token: z.string().optional(), entity_group_name: z.string().optional(), entity_group_type: z.enum(["business", "joint_account", "membership", "merchant"]).optional(), created_at: z.string().optional(), archived_at: z.string().optional(), entity_group_external_identifiers: z.array(z.string()).optional(), entity_group_relationships: z.array(z.object({entity_token: z.string().optional(), name: z.string().optional(), type: z.enum(["person", "business"]).optional(), ownership_percentage: z.string().optional(), title: z.string().optional(), relationship_created_at: z.string().optional(), roles: z.array(z.enum(["primary", "shareholder", "secondary", "beneficiary", "beneficial_owner", "control_prong", "director", "authorized_signer", "principal_owner", "coborrower", "joint", "guarantor"])).optional(), bank_accounts: z.array(z.object({bank_account_token: z.string().optional(), external_account_id: z.string().optional(), account_holder_type: z.enum(["primary", "secondary", "power_of_attorney", "authorized_signer", "authorized_user", "beneficiary", "joint_owner", "custodian", "guardian", "trustee"]).optional()})).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 }; /** * Get information about an Entity Group * * GET /entity-groups/{entity_token_or_external_id} * * Also accepts auth: basic */ export const getApiEntityGroupsEntityTokenOrExternalId = async (options?: GetApiEntityGroupsEntityTokenOrExternalIdOptions) => { const {baseUrl = "https://sandbox.alloy.co/v1", 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(/\/+$/, '')}/entity-groups/${encodeURIComponent(String(args.entity_token_or_external_id))}${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(getEntityGroupResponse, await res.json()) };