import {z} from 'zod' export type GetApiV1ReferralsArgs = {cursor?: (string | null) | undefined}; export type GetApiV1ReferralsOptions = {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 referralPage = z.object({next_cursor: z.string().nullable().optional(), items: z.array(referral)}); 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 }; /** * List referrals * * GET /v1/referrals */ export const getApiV1Referrals = async (args: GetApiV1ReferralsArgs, options?: GetApiV1ReferralsOptions) => { const {baseUrl = "https://api.brex.com", fetch: fetchFn = fetch, oAuth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (oAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${oAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/referrals${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(referralPage, await res.json()) };