import {z} from 'zod' export type GetUsageMetricsReqBody = {tenant_ids?: (Array | string | null) | undefined} | {[key: string]: unknown}; export type CreateApiBillingUsageMetricsArgs = {page_size?: (number | null) | undefined, page?: (number | null) | undefined, sort?: ('asc' | 'desc') | undefined, id?: (string | null) | undefined, connector?: (string | null) | undefined, partial_name?: (string | null) | undefined, tenant_ids?: (string | null) | undefined, entity_type?: (string | null) | undefined, body: GetUsageMetricsReqBody | null}; export type CreateApiBillingUsageMetricsOptions = {baseUrl?: string | undefined, FronteggHTTPAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiBillingUsageMetricsResponse = 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 }; /** * Get usage metrics (POST) * * POST /billing/usage/metrics */ export const createApiBillingUsageMetrics = async (args: CreateApiBillingUsageMetricsArgs, options?: CreateApiBillingUsageMetricsOptions) => { 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.page_size != null) query.append("page_size", String(args.page_size)) if (args.page != null) query.append("page", String(args.page)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.id != null) query.append("id", String(args.id)) if (args.connector != null) query.append("connector", String(args.connector)) if (args.partial_name != null) query.append("partial_name", String(args.partial_name)) if (args.tenant_ids != null) query.append("tenant_ids", String(args.tenant_ids)) if (args.entity_type != null) query.append("entity_type", String(args.entity_type)) if (FronteggHTTPAuthentication !== undefined) headers.set('authorization', `Bearer ${FronteggHTTPAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/billing/usage/metrics${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiBillingUsageMetricsResponse, await res.json()) };