import {z} from 'zod' export type GetApiV1AnalyticsBucketsBucketIdStorageArgs = {bucket_id: string, start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, group_by?: string | undefined}; export type GetApiV1AnalyticsBucketsBucketIdStorageOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, NamespaceHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const apiAnalyticsBucketsModelsTimeRange = z.object({start: z.string(), end: z.string()}); export const storageMetric = z.object({time_bucket: z.string(), total_size_bytes: z.number().int(), object_count: z.number().int(), avg_size_bytes: z.number().int(), growth_rate_percent: z.number().nullable().optional()}); export const bucketStorageResponse = z.object({bucket_id: z.string(), time_range: apiAnalyticsBucketsModelsTimeRange, metrics: z.array(storageMetric), summary: z.record(z.string(), z.unknown()).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 Bucket Storage * * GET /v1/analytics/buckets/{bucket_id}/storage */ export const getApiV1AnalyticsBucketsBucketIdStorage = async (args: GetApiV1AnalyticsBucketsBucketIdStorageArgs, options?: GetApiV1AnalyticsBucketsBucketIdStorageOptions) => { const {baseUrl = "https://api.mixpeek.com", fetch: fetchFn = fetch, BearerAuth, NamespaceHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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.group_by != null) query.append("group_by", String(args.group_by)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) if (NamespaceHeader !== undefined) headers.set("X-Namespace", NamespaceHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/analytics/buckets/${encodeURIComponent(String(args.bucket_id))}/storage${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(bucketStorageResponse, await res.json()) };