import {z} from 'zod' export type GetApiV1BillingUsageTimeseriesArgs = {start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, full_history?: boolean | undefined, interval_minutes?: number | undefined, scope?: ('team' | 'personal') | undefined, workload_type?: (string | null) | undefined}; export type GetApiV1BillingUsageTimeseriesOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const usageTimeseriesPoint = z.object({bucket_date: z.string(), total_credits: z.number(), request_count: z.number().int()}); export const usageTimeseriesResponse = z.object({points: z.array(usageTimeseriesPoint)}); 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 Billing Usage Timeseries * * GET /v1/billing/usage/timeseries * * Also accepts auth: BearerAuth */ export const getApiV1BillingUsageTimeseries = async (args: GetApiV1BillingUsageTimeseriesArgs, options: GetApiV1BillingUsageTimeseriesOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.full_history != null) query.append("full_history", String(args.full_history)) if (args.interval_minutes != null) query.append("interval_minutes", String(args.interval_minutes)) if (args.scope != null) query.append("scope", String(args.scope)) if (args.workload_type != null) query.append("workload_type", String(args.workload_type)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/billing/usage/timeseries${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(usageTimeseriesResponse, await res.json()) };