import {z} from 'zod' export type GetApiV1BillingMetersIdEventSummariesArgs = {customer: string, end_time: number, ending_before?: string | undefined, expand?: Array | undefined, id: string, limit?: number | undefined, start_time: number, starting_after?: string | undefined, value_grouping_window?: ('day' | 'hour') | undefined, body: Record}; export type GetApiV1BillingMetersIdEventSummariesOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const billingMeterEventSummary = z.object({aggregated_value: z.number(), end_time: z.number().int(), id: z.string().max(5000), livemode: z.boolean(), meter: z.string().max(5000), object: z.literal("billing.meter_event_summary"), start_time: z.number().int()}); export const getApiV1BillingMetersIdEventSummariesResponse = z.object({data: z.array(billingMeterEventSummary), has_more: z.boolean(), object: z.literal("list"), url: z.string().max(5000)}); 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 }; /** * List billing meter event summaries * * GET /v1/billing/meters/{id}/event_summaries * * Also accepts auth: bearerAuth */ export const getApiV1BillingMetersIdEventSummaries = async (args: GetApiV1BillingMetersIdEventSummariesArgs, options?: GetApiV1BillingMetersIdEventSummariesOptions) => { const {baseUrl = "https://api.stripe.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (args.customer != null) query.append("customer", String(args.customer)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.ending_before != null) query.append("ending_before", String(args.ending_before)) for (const item of args.expand ?? []) query.append("expand", String(item)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (args.value_grouping_window != null) query.append("value_grouping_window", String(args.value_grouping_window)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/billing/meters/${encodeURIComponent(String(args.id))}/event_summaries${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1BillingMetersIdEventSummariesResponse, await res.json()) };