import {z} from 'zod' export type PeriodType = 'WEEKLY' | 'MONTHLY' | 'QUARTERLY' | 'YEARLY' | 'ONE_TIME'; export type Money = {amount: number, currency?: (string | null) | undefined}; export type LimitType = 'HARD' | 'SOFT'; export type SpendType = 'BUDGET_PROVISIONED_CARDS_ONLY' | 'NON_BUDGET_PROVISIONED_CARDS_ALLOWED'; export type BudgetLimitVisibilityType = 'SHARED' | 'PRIVATE'; export type CreateBudgetRequest = {name: string, description: string, parent_budget_id: string, owner_user_ids?: (Array | null) | undefined, member_user_ids?: (Array | null) | undefined, period_type: PeriodType, limit: Money, limit_type: LimitType, spend_type: SpendType, start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, limit_visibility: BudgetLimitVisibilityType, policy_id?: (string | null) | undefined, entity_id?: (string | null) | undefined}; export type CreateApiV1BudgetsArgs = {'Idempotency-Key': string, body: CreateBudgetRequest}; export type CreateApiV1BudgetsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const periodType = z.enum(["WEEKLY", "MONTHLY", "QUARTERLY", "YEARLY", "ONE_TIME"]); export const money = z.object({amount: z.number().int(), currency: z.string().nullable().optional()}); export const budgetStatus = z.enum(["APPROVED", "EXPIRED", "TERMINATED", "DELETED"]); export const limitType = z.enum(["HARD", "SOFT"]); export const spendType = z.enum(["BUDGET_PROVISIONED_CARDS_ONLY", "NON_BUDGET_PROVISIONED_CARDS_ALLOWED"]); export const budgetPeriodBalance = z.object({start_date: z.string().nullable().optional(), end_date: z.string().nullable().optional(), balance: money.nullable().optional()}); export const budget = z.object({budget_id: z.string(), account_id: z.string(), creator_user_id: z.string().nullable().optional(), name: z.string(), description: z.string().nullable().optional(), parent_budget_id: z.string().nullable().optional(), owner_user_ids: z.array(z.string()), member_user_ids: z.array(z.string()), period_type: periodType, start_date: z.string().nullable().optional(), end_date: z.string().nullable().optional(), limit: money.nullable().optional(), budget_status: budgetStatus, limit_type: limitType.nullable().optional(), spend_type: spendType, current_period_balance: budgetPeriodBalance.nullable().optional(), limit_visibility: z.enum(["SHARED", "PRIVATE"]), templateId: z.string().nullable().optional(), budget_type: z.enum(["BUDGET", "SPEND_LIMIT"])}); 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 /v1/budgets */ export const createApiV1Budgets = async (args: CreateApiV1BudgetsArgs, options?: CreateApiV1BudgetsOptions) => { 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(/\/+$/, '')}/v1/budgets${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(budget, await res.json()) };