import {z} from 'zod' export type Money = {amount: number, currency?: (string | null) | undefined}; export type CreateSpendBudgetRequest = {name: string, description: string, parent_budget_id: string, owner_user_ids?: (Array | null) | undefined, period_recurrence_type: 'WEEKLY' | 'MONTHLY' | 'QUARTERLY' | 'YEARLY' | 'ONE_TIME', amount: Money, limit_type?: ('HARD' | 'SOFT' | null) | undefined, start_date?: (string | null) | undefined, end_date?: (string | null) | undefined}; export type CreateApiV2BudgetsArgs = {'Idempotency-Key': string, body: CreateSpendBudgetRequest}; export type CreateApiV2BudgetsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const money = z.object({amount: z.number().int(), currency: z.string().nullable().optional()}); export const spendBudget = z.object({budget_id: z.string(), account_id: z.string(), name: z.string(), description: z.string().nullable().optional(), parent_budget_id: z.string().nullable().optional(), owner_user_ids: z.array(z.string()), period_recurrence_type: z.enum(["WEEKLY", "MONTHLY", "QUARTERLY", "YEARLY", "ONE_TIME"]), start_date: z.string().nullable().optional(), end_date: z.string().nullable().optional(), amount: money.nullable().optional(), spend_budget_status: z.enum(["ACTIVE", "ARCHIVED", "DELETED", "EXPIRED"]), limit_type: z.enum(["HARD", "SOFT"]).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 Budget * * * POST /v2/budgets */ export const createApiV2Budgets = async (args: CreateApiV2BudgetsArgs, options?: CreateApiV2BudgetsOptions) => { 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/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(spendBudget, await res.json()) };