import {z} from 'zod' export type GetApiCardsCardIdSpendingLimitsArgs = {cardId: string, accountId: string, budgetId: string, personId?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiCardsCardIdSpendingLimitsOptions = {baseUrl?: string | undefined, CommonAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiCardsCardIdSpendingLimitsResponse = z.object({limit: z.number().int().gte(1).lte(1000).optional(), offset: z.number().int().optional(), count: z.number().int(), rows: z.array(z.object({id: z.string().max(36).optional(), amount: z.number().gt(0), currencyCode: z.string().optional(), active: z.boolean().optional(), limitTurnedOff: z.boolean().optional(), limitWindow: z.enum(["DAY", "WEEK", "MONTH", "LIFETIME"]), available: z.object({uses: z.number().optional(), amount: z.number().optional(), daysRemaining: z.number().optional()}).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 }; /** * Retrieve spending limit for a card * * GET /cards/{cardId}/spending-limits */ export const getApiCardsCardIdSpendingLimits = async (args: GetApiCardsCardIdSpendingLimitsArgs, options?: GetApiCardsCardIdSpendingLimitsOptions) => { const {baseUrl = "https://api.equalsmoney.com/v2", fetch: fetchFn = fetch, CommonAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.accountId != null) query.append("accountId", String(args.accountId)) if (args.budgetId != null) query.append("budgetId", String(args.budgetId)) if (args.personId != null) query.append("personId", String(args.personId)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (CommonAuth !== undefined) headers.set("Authorization", CommonAuth) const url = `${baseUrl.replace(/\/+$/, '')}/cards/${encodeURIComponent(String(args.cardId))}/spending-limits${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(getApiCardsCardIdSpendingLimitsResponse, await res.json()) };