import {z} from 'zod' export type GetApiV1AccountsAccountIdBillingSummaryArgs = {startTime: string, endTime: string, granularity?: ('GRANULARITY_UNSPECIFIED' | 'DAILY') | undefined, account_id: string}; export type GetApiV1AccountsAccountIdBillingSummaryOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const typeMoney = z.object({currencyCode: z.string().optional(), units: z.string().optional(), nanos: z.number().int().optional()}); export const gatewayGetAccountUsageRequestUsageType = z.enum(["USAGE_TYPE_UNSPECIFIED", "SERVERLESS", "DEDICATED_DEPLOYMENT", "TRAINING"]); export const gatewayLineItem = z.object({category: z.string().optional(), groupingKey: z.string().optional(), groupingValue: z.string().optional(), secondaryGroupingKey: z.string().optional(), secondaryGroupingValue: z.string().optional(), quantity: z.number().optional(), unitAmount: typeMoney.optional(), totalCost: typeMoney.optional(), series: gatewayGetAccountUsageRequestUsageType.optional()}); export const gatewayUsageBucket = z.object({startTime: z.string().optional(), endTime: z.string().optional(), lineItems: z.array(gatewayLineItem).optional()}); export const gatewayGetBillingSummaryResponse = z.object({lineItems: z.array(gatewayLineItem).optional(), usageBuckets: z.array(gatewayUsageBucket).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 }; /** * Get billing summary information for an account * * GET /v1/accounts/{account_id}/billing/summary */ export const getApiV1AccountsAccountIdBillingSummary = async (args: GetApiV1AccountsAccountIdBillingSummaryArgs, options: GetApiV1AccountsAccountIdBillingSummaryOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) if (args.granularity != null) query.append("granularity", String(args.granularity)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.account_id))}/billing/summary${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(gatewayGetBillingSummaryResponse, await res.json()) };