import {z} from 'zod' export type GetApiV1BusinessesClientIdBudgetsArgs = {offset?: number | undefined, limit?: number | undefined, client_id: number, is_deleted?: boolean | undefined, budget_type?: ('BUDGET' | 'SUBSCRIPTION') | undefined, has_member_limits?: boolean | undefined}; export type GetApiV1BusinessesClientIdBudgetsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const budgetResetPeriod = z.enum(["DAILY", "WEEKLY", "FORTNIGHTLY", "MONTHLY", "QUARTERLY", "ANNUALLY", "JULY_FINANCIAL_YEAR", "APRIL_FINANCIAL_YEAR", "NEVER"]); export const budget = z.object({id: z.string(), name: z.string(), budget_type: z.enum(["BUDGET", "SUBSCRIPTION"]), available_amount: z.number(), spent_amount: z.number(), reset_amount: z.number(), reset_period: budgetResetPeriod, current_period_start: z.string().nullable(), current_period_end: z.string().nullable(), has_member_limits: z.boolean(), rollover_funds: z.boolean(), limit_categories: z.boolean(), categories: z.array(z.object({id: z.string(), name: z.string()})), custom_fields: z.array(z.object({id: z.string(), name: z.string(), allow_options: z.array(z.string()).nullable()})), is_deleted: z.boolean(), created: z.string(), updated: z.string(), payment_types: z.array(z.enum(["bills", "cards", "reimbursements"]))}); export const budgetListResponse = z.object({count: z.number().int().optional(), next: z.string().optional(), previous: z.string().optional(), results: z.array(budget).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 }; /** * List budgets * * GET /v1/businesses/{client_id}/budgets */ export const getApiV1BusinessesClientIdBudgets = async (args: GetApiV1BusinessesClientIdBudgetsArgs, options?: GetApiV1BusinessesClientIdBudgetsOptions) => { const {baseUrl = "https://public.letsweel.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.is_deleted != null) query.append("is_deleted", String(args.is_deleted)) if (args.budget_type != null) query.append("budget_type", String(args.budget_type)) if (args.has_member_limits != null) query.append("has_member_limits", String(args.has_member_limits)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/businesses/${encodeURIComponent(String(args.client_id))}/budgets${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(budgetListResponse, await res.json()) };