import {z} from 'zod' export type GetApiV1BillingLedgerBreakdownArgs = {start: string, end: string, dimension?: (Array<'category' | 'model' | 'provider' | 'project' | 'experiment'> | null) | undefined, grain?: ('day' | 'month' | null) | undefined, project_id?: (string | null) | undefined, experiment_id?: (string | null) | undefined}; export type GetApiV1BillingLedgerBreakdownOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const ledgerBreakdownRow = z.object({category: z.string().nullable().optional(), model: z.string().nullable().optional(), provider: z.string().nullable().optional(), project_id: z.string().nullable().optional(), experiment_id: z.string().nullable().optional(), period_start: z.string().nullable().optional(), spend: z.string(), entries: z.number().int()}); export const ledgerBreakdownResponse = z.object({rows: z.array(ledgerBreakdownRow)}); 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 }; /** * Get Ledger Breakdown * * GET /v1/billing/ledger/breakdown * * Also accepts auth: BearerAuth */ export const getApiV1BillingLedgerBreakdown = async (args: GetApiV1BillingLedgerBreakdownArgs, options: GetApiV1BillingLedgerBreakdownOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) for (const item of args.dimension ?? []) query.append("dimension", String(item)) if (args.grain != null) query.append("grain", String(args.grain)) if (args.project_id != null) query.append("project_id", String(args.project_id)) if (args.experiment_id != null) query.append("experiment_id", String(args.experiment_id)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/billing/ledger/breakdown${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(ledgerBreakdownResponse, await res.json()) };