import {z} from 'zod' export type CreateSpendLimitRequest = {name: string, description?: (string | null) | undefined, parent_budget_id?: (string | null) | undefined, period_recurrence_type: 'PER_WEEK' | 'PER_MONTH' | 'PER_QUARTER' | 'PER_YEAR' | 'ONE_TIME', start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, authorization_settings: {base_limit: {amount: number, currency?: (string | null) | undefined}, limit_buffer_percentage?: (number | null) | undefined, authorization_type: 'HARD' | 'SOFT', rollover_refresh_rate: 'OFF' | 'NEVER' | 'PER_MONTH' | 'PER_QUARTER' | 'PER_YEAR'}, expense_visibility: 'SHARED' | 'PRIVATE', authorization_visibility: 'PUBLIC' | 'PRIVATE', transaction_limit?: ({amount: number, currency?: (string | null) | undefined} | null) | undefined, limit_increase_setting: 'ENABLED' | 'DISABLED', spend_type: 'BUDGET_PROVISIONED_CARDS_ONLY' | 'NON_BUDGET_PROVISIONED_CARDS_ALLOWED', auto_transfer_cards_setting: 'DISABLED' | 'ENABLED', auto_create_limit_cards_setting: 'DISABLED' | 'ALL_MEMBERS', owner_user_ids?: (Array | null) | undefined, member_user_ids?: (Array | null) | undefined, expense_policy_id: string, limit_increase_request_policy_id?: (string | null) | undefined, limit_approval_policy_id?: (string | null) | undefined, legal_entity_id?: (string | null) | undefined, department_id?: (string | null) | undefined}; export type CreateApiV2SpendLimitsArgs = {'Idempotency-Key': string, body: CreateSpendLimitRequest}; export type CreateApiV2SpendLimitsOptions = {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 }; /** * * Create Spend Limit * * * POST /v2/spend_limits */ export const createApiV2SpendLimits = async (args: CreateApiV2SpendLimitsArgs, options?: CreateApiV2SpendLimitsOptions) => { const {baseUrl = "https://api.brex.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/spend_limits${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(spendLimit, await res.json()) };