import {z} from 'zod' export type GetApiApiV1AccountsAccountIdConsumedArgs = {account_id: number, unit: string, start: string, end: string, breakdown?: ('day' | null) | undefined, split?: ('strategy' | null) | undefined, 'unstructured-api-key'?: (string | null) | undefined}; export type GetApiApiV1AccountsAccountIdConsumedOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const strategyQuantity = z.object({strategy: z.string(), quantity: z.number().int()}); export const dayQuantity = z.object({utc_day: z.string(), quantity: z.number().int(), strategies: z.array(strategyQuantity).nullable().optional()}); export const accountConsumed = z.object({account_id: z.number().int(), unit: z.string(), quantity: z.number().int(), counted_from: z.string(), counted_through: z.string(), source: z.string(), days: z.array(dayQuantity).nullable().optional(), strategies: z.array(strategyQuantity).nullable().optional(), strategies_counted_from: 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 }; /** * Licence-enforced consumption for an account * * GET /api/v1/accounts/{account_id}/consumed */ export const getApiApiV1AccountsAccountIdConsumed = async (args: GetApiApiV1AccountsAccountIdConsumedArgs, options: GetApiApiV1AccountsAccountIdConsumedOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.unit != null) query.append("unit", String(args.unit)) if (args.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) if (args.breakdown != null) query.append("breakdown", String(args.breakdown)) if (args.split != null) query.append("split", String(args.split)) if (args['unstructured-api-key'] != null) headers.set("unstructured-api-key", String(args['unstructured-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/accounts/${encodeURIComponent(String(args.account_id))}/consumed${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(accountConsumed, await res.json()) };