import {z} from 'zod' export type Pagination = {page?: ({number?: number | undefined, size?: number | undefined}) | undefined}; export type CdnSortOptions = 'date' | '-date' | 'referer' | '-referer' | 'file' | '-file' | 'file_name' | '-file_name' | 'folder_name' | '-folder_name'; export type GetApiStatisticsCdnUsagesArgs = {filter?: ({filter?: ({date?: ({gte?: string | undefined}) | undefined, file?: ({eq?: number | undefined}) | undefined}) | undefined}) | undefined, page?: Pagination | undefined, sort?: CdnSortOptions | undefined}; export type GetApiStatisticsCdnUsagesOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const cdnStatisticAttributes = z.object({referer: z.string().optional(), date: z.string().optional(), file_id: z.number().int().optional(), file_name: z.string().optional(), folder_name: z.string().optional(), folder_id: z.number().int().optional(), request_count: z.number().int().optional(), log_type: z.string().optional()}); export const cdnStatistic = z.object({type: z.literal("cdn_statistics").optional(), attributes: cdnStatisticAttributes.optional()}); export const paginatedCollectionLinks = z.object({self: z.string(), first: z.string(), last: z.string(), prev: z.string().nullable(), next: z.string().nullable()}); export const filterEcho = z.union([z.record(z.string(), z.unknown()), z.array(z.unknown())]); export const paginatedCollectionMeta = z.object({total: z.number().int(), count: z.number().int(), per_page: z.number().int(), current_page: z.number().int(), total_pages: z.number().int(), filter: filterEcho, sort: z.string().nullable()}); export const cdnStatisticsCollection = z.object({data: z.array(cdnStatistic).optional(), links: paginatedCollectionLinks.optional(), meta: paginatedCollectionMeta.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 Cdn usages statistics * * GET /statistics/cdn-usages */ export const getApiStatisticsCdnUsages = async (args: GetApiStatisticsCdnUsagesArgs, options: GetApiStatisticsCdnUsagesOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args.filter != null) query.append("filter", JSON.stringify(args.filter)) if (args.page != null) query.append("page", JSON.stringify(args.page)) if (args.sort != null) query.append("sort", String(args.sort)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/statistics/cdn-usages${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(cdnStatisticsCollection, await res.json()) };