import {z} from 'zod' export type GetApiV1UsageArgs = {start_time: string, end_time: string, bucket_width?: ('hour' | 'day') | undefined, breakdown_type?: (Array<'voice_id' | 'voice_name' | 'api_key' | 'model'>) | undefined, page_size?: number | undefined, next_page_token?: string | undefined}; export type GetApiV1UsageOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const usageResult = z.object({voice_id: z.string().optional(), voice_name: z.string().optional(), api_key: z.string().optional(), model: z.string().optional(), minutes_used: z.number()}); export const usageBucket = z.object({starting_at: z.string(), ending_at: z.string(), results: z.array(usageResult)}); export const usageAnalyticsResponse = z.object({data: z.array(usageBucket), next_page_token: z.string().optional(), total: z.number()}); 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 }; /** * Retrieve advanced API usage analytics * * GET /v1/usage */ export const getApiV1Usage = async (args: GetApiV1UsageArgs, options?: GetApiV1UsageOptions) => { const {baseUrl = "https://supertoneapi.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.bucket_width != null) query.append("bucket_width", String(args.bucket_width)) for (const item of args.breakdown_type ?? []) query.append("breakdown_type", String(item)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.next_page_token != null) query.append("next_page_token", String(args.next_page_token)) if (apiKey !== undefined) headers.set("x-sup-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/usage${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(usageAnalyticsResponse, await res.json()) };