import {z} from 'zod' export type Pagination = {page?: ({number?: number | undefined, size?: number | undefined}) | undefined}; export type DownloadSortOptions = 'date' | '-date' | 'user_name' | '-user_name' | 'organization' | '-organization' | 'file_name' | '-file_name' | 'folder_name' | '-folder_name' | 'ip_address' | '-ip_address'; export type GetApiStatisticsDownloadsArgs = {filter?: ({filter?: ({date?: ({gte?: string | undefined, lt?: string | undefined}) | undefined, user?: ({eq?: number | undefined}) | undefined, file?: ({eq?: number | undefined}) | undefined, folder?: ({eq?: number | undefined}) | undefined, extension?: ({eq?: string | undefined}) | undefined}) | undefined}) | undefined, page?: Pagination | undefined, sort?: DownloadSortOptions | undefined}; export type GetApiStatisticsDownloadsOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const downloadStatisticAttributes = z.object({date: z.string().optional(), action: z.string().optional(), user_name: z.string().optional(), user_id: z.string().optional(), organization: z.string().optional(), ip_address: z.string().optional(), file_id: z.number().int().optional(), file_name: z.string().optional(), file_extension: z.string().optional(), folder_id: z.number().int().optional(), folder_name: z.string().optional()}); export const downloadStatistic = z.object({type: z.string().optional(), id: z.string().optional(), attributes: downloadStatisticAttributes.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 downloadStatisticsCollection = z.object({data: z.array(downloadStatistic).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 download statistics * * GET /statistics/downloads */ export const getApiStatisticsDownloads = async (args: GetApiStatisticsDownloadsArgs, options: GetApiStatisticsDownloadsOptions) => { 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/downloads${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(downloadStatisticsCollection, await res.json()) };