import {z} from 'zod' export type UsageDirectionEnum = 'in' | 'out'; export type UsageTopTopicsReqBody = {tenant_ids?: (Array | string | null) | undefined} | {[key: string]: unknown}; export type GetApiBillingUsageTopTopicsArgs = {start_date: string, end_date: string, direction?: UsageDirectionEnum | undefined, limit?: number | undefined, tenant_ids?: (string | null) | undefined, body: UsageTopTopicsReqBody | null}; export type GetApiBillingUsageTopTopicsOptions = {baseUrl?: string | undefined, FronteggHTTPAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const usageDirectionEnum = z.enum(["in", "out"]); export const usageTopicVolume = z.object({topic: z.string(), topic_display_name: z.string(), producer_id: z.string().nullable().optional(), producer_type: z.string().nullable().optional(), producer_name: z.string().nullable().optional(), volume_bytes: z.number().optional(), volume_gb: z.number().optional(), percentage: z.number().optional()}).and(z.record(z.string(), z.unknown())); export const usageTopTopicsOthers = z.object({topic_count: z.number().int().optional(), volume_bytes: z.number().optional(), volume_gb: z.number().optional(), percentage: z.number().optional()}).and(z.record(z.string(), z.unknown())); export const usageTopTopicsRes = z.object({direction: usageDirectionEnum, start_date: z.string(), end_date: z.string(), topics: z.array(usageTopicVolume).optional(), others: usageTopTopicsOthers.optional(), total_volume_bytes: z.number().optional(), total_volume_gb: z.number().optional()}).and(z.record(z.string(), z.unknown())); 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 }; /** * Top topics by data volume * * GET /billing/usage/top-topics */ export const getApiBillingUsageTopTopics = async (args: GetApiBillingUsageTopTopicsArgs, options?: GetApiBillingUsageTopTopicsOptions) => { const {baseUrl = "https://api.streamkap.com", fetch: fetchFn = fetch, FronteggHTTPAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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.direction != null) query.append("direction", String(args.direction)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.tenant_ids != null) query.append("tenant_ids", String(args.tenant_ids)) if (FronteggHTTPAuthentication !== undefined) headers.set('authorization', `Bearer ${FronteggHTTPAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/billing/usage/top-topics${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(usageTopTopicsRes, await res.json()) };