import {z} from 'zod' export type Applicant = {last_name: string, first_name: string, email: string}; export type IncorporationType = 'C_CORP' | 'S_CORP' | 'B_CORP' | 'LLC' | 'LLP' | 'SOLE_PROP' | 'ORG_501C3' | 'LP'; export type Address = {line1?: (string | null) | undefined, line2?: (string | null) | undefined, city?: (string | null) | undefined, state?: (string | null) | undefined, country?: (string | null) | undefined, postal_code?: (string | null) | undefined, phone_number?: (string | null) | undefined}; export type CompanyRelationship = 'FOUNDER' | 'EXECUTIVE' | 'SENIOR_LEADERSHIP' | 'OTHER'; export type IdentityDocumentType = 'SSN' | 'PASSPORT'; export type IdentityDocument = {country: string, type: IdentityDocumentType, number: string}; export type Prong = 'OWNERSHIP' | 'CONTROL' | 'BOTH'; export type BeneficialOwner = {legal_name: string, company_relationship?: (CompanyRelationship | null) | undefined, date_of_birth?: (string | null) | undefined, identity_document?: (IdentityDocument | null) | undefined, address?: ({line1?: (string | null) | undefined, line2?: (string | null) | undefined, city?: (string | null) | undefined, state?: (string | null) | undefined, country?: (string | null) | undefined, postal_code?: (string | null) | undefined, phone_number?: (string | null) | undefined} | null) | undefined, prong: Prong, ownership_percentage?: (number | null) | undefined}; export type Business = {legal_name?: (string | null) | undefined, incorporation_type?: (IncorporationType | null) | undefined, employer_identification_number?: (string | null) | undefined, website_url?: (string | null) | undefined, activity_description?: (string | null) | undefined, incorporation_year?: (number | null) | undefined, incorporation_state?: (string | null) | undefined, address?: (Address | null) | undefined, beneficial_owners?: (Array | null) | undefined, alternate_address?: (Address | null) | undefined}; export type ContactPreference = 'NO_OUTBOUND' | 'EMAIL_OUTBOUND'; export type CreateReferralRequest = {referral_code: string, applicant: Applicant, business?: (Business | null) | undefined, contact_preference?: (ContactPreference | null) | undefined}; export type CreateApiV1ReferralsArgs = {body: CreateReferralRequest}; export type CreateApiV1ReferralsOptions = {baseUrl?: string | undefined, oAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const referralStatus = z.enum(["UNCLAIMED", "EXPIRED", "ACTIVE", "CLOSED"]); export const domesticInstruction = z.object({account_type: z.string(), bank_account_number: z.string(), bank_routing_number: z.string(), beneficiary_name: z.string(), beneficiary_address: z.string(), bank_name: z.string(), bank_address: z.string()}); export const internationalInstruction = z.object({account_type: z.string(), swift_account_number: z.string(), swift_bank_number: z.string(), beneficiary_name: z.string(), beneficiary_address: z.string(), bank_name: z.string(), bank_address: z.string(), special_instructions: z.string()}); export const instruction = z.object({domestic: domesticInstruction, international: internationalInstruction}); export const account = z.object({id: z.string(), instructions: instruction.nullable().optional(), created_at: z.string()}); export const applicationStatus = z.enum(["NO_ACCOUNT", "ACTIVE", "NOT_SUBMITTED", "INFORMATION_PENDING", "MANUAL_REVIEW", "PROCESSING", "REJECTED", "CLOSED"]); export const application = z.object({status: applicationStatus}); export const cash = z.object({accounts: z.array(account).nullable().optional(), application: application}); export const product = z.object({cash: cash}); export const referral = z.object({id: z.string(), referral_signup_url: z.string(), expires_at: z.string(), customer_email: z.string().nullable().optional(), status: referralStatus, products: z.array(product)}); 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 }; /** * Creates a referral * * POST /v1/referrals */ export const createApiV1Referrals = async (args: CreateApiV1ReferralsArgs, options?: CreateApiV1ReferralsOptions) => { const {baseUrl = "https://api.brex.com", fetch: fetchFn = fetch, oAuth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${oAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/referrals${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(referral, await res.json()) };