import {z} from 'zod' export type GetApiV2SpendLimitsIdArgs = {id: string}; export type GetApiV2SpendLimitsIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const merchantCategory = z.enum(["ADVERTISING_MARKETING", "GROCERY", "TELEPHONY", "OFFICE_SUPPLIES", "PRIVATE_AIR_TRAVEL", "CLOTHING", "CAR_RENTAL", "VEHICLE_EXPENSES", "RESTAURANTS", "GAMBLING", "FLOWERS", "ELECTRONICS", "LEGAL_SERVICES", "UTILITIES", "FURNITURE", "BARS_NIGHTLIFE", "LAUNDRY", "EVENT_EXPENSES", "SHIPPING", "OTHER_TRAVEL_EXPENSES", "CHARITY", "SOFTWARE", "LODGING", "FACILITIES_EXPENSES", "SERVERS", "CONFERENCES", "FOOD_DELIVERY", "RENT", "AIRLINE_EXPENSES", "OTHER_BUSINESS_EXPENSES", "BANK_FINANCIAL_FEES", "BOOKS_NEWSPAPERS", "CONSULTANT_CONTRACTOR", "CORPORATE_INSURANCE", "DIGITAL_GOODS", "LICENSES_TAXES", "GAS_FUEL", "GENERAL_MERCHANDISE", "MEDICAL", "MEMBERSHIPS_CLUBS", "PARKING_EXPENSES", "POLITICAL_DONATIONS", "PUBLIC_TRANSPORTATION", "RECURRING_SOFTWARE_SAAS", "RIDESHARE_TAXI", "TOLL_BRIDGE_FEES", "TRAINING_EDUCATION", "TRAVEL"]); export const money = z.object({amount: z.number().int(), currency: z.string().nullable().optional()}); export const spendLimitPeriodBalance = z.object({start_date: z.string().nullable().optional(), end_date: z.string().nullable().optional(), start_time: z.string().nullable().optional(), end_time: z.string().nullable().optional(), amount_spent: money.nullable().optional(), rollover_amount: money.nullable().optional()}); export const spendLimit = z.object({id: z.string(), account_id: z.string(), name: z.string(), description: z.string().nullable().optional(), parent_budget_id: z.string().nullable().optional(), status: z.enum(["ACTIVE", "EXPIRED", "ARCHIVED", "DELETED"]), period_recurrence_type: z.enum(["PER_WEEK", "PER_MONTH", "PER_QUARTER", "PER_YEAR", "ONE_TIME"]), start_date: z.string().nullable().optional(), end_date: z.string().nullable().optional(), start_time_utc: z.string().nullable().optional(), end_time_utc: z.string().nullable().optional(), authorization_settings: z.object({base_limit: z.object({amount: z.number().int(), currency: z.string().nullable().optional()}), limit_buffer_percentage: z.number().int().nullable().optional(), authorization_type: z.enum(["HARD", "SOFT"]), rollover_refresh_rate: z.enum(["OFF", "NEVER", "PER_MONTH", "PER_QUARTER", "PER_YEAR"]), limit_with_increases: z.object({amount: z.number().int(), currency: z.string().nullable().optional()})}).nullable().optional(), expense_visibility: z.enum(["SHARED", "PRIVATE"]), authorization_visibility: z.enum(["PUBLIC", "PRIVATE"]), merchant_category_controls: z.object({allowed_merchant_categories: z.array(merchantCategory).nullable().optional(), blocked_merchant_categories: z.array(merchantCategory).nullable().optional()}).nullable().optional(), transaction_limit: z.object({amount: z.number().int(), currency: z.string().nullable().optional()}).nullable().optional(), limit_increase_setting: z.enum(["ENABLED", "DISABLED"]), spend_type: z.enum(["BUDGET_PROVISIONED_CARDS_ONLY", "NON_BUDGET_PROVISIONED_CARDS_ALLOWED"]), auto_transfer_cards_setting: z.enum(["DISABLED", "ENABLED"]), auto_create_limit_cards_setting: z.enum(["DISABLED", "ALL_MEMBERS"]), owner_user_ids: z.array(z.string()), member_user_ids: z.array(z.string()), expense_policy_id: z.string(), limit_increase_request_policy_id: z.string().nullable().optional(), limit_approval_policy_id: z.string().nullable().optional(), current_period_balance: spendLimitPeriodBalance.nullable().optional(), legal_entity_id: z.string().nullable().optional(), department_id: z.string().nullable().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 Spend Limit * * * GET /v2/spend_limits/{id} */ export const getApiV2SpendLimitsId = async (args: GetApiV2SpendLimitsIdArgs, options?: GetApiV2SpendLimitsIdOptions) => { const {baseUrl = "https://api.brex.com", 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(/\/+$/, '')}/v2/spend_limits/${encodeURIComponent(String(args.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(spendLimit, await res.json()) };