import {z} from 'zod' export type Pagination = {page?: ({number?: number | undefined, size?: number | undefined}) | undefined}; export type LoginSortOptions = 'date' | '-date' | 'user_name' | '-user_name' | 'organization' | '-organization' | 'ip_address' | '-ip_address' | 'activity_source' | '-activity_source'; export type GetApiStatisticsLoginsArgs = {filter?: ({filter?: ({date?: ({gte?: string | undefined, lt?: string | undefined}) | undefined, user?: ({eq?: number | undefined}) | undefined}) | undefined}) | undefined, page?: Pagination | undefined, sort?: LoginSortOptions | undefined}; export type GetApiStatisticsLoginsOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const loginStatisticAttributes = 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(), activity_source: z.string().optional()}); export const loginStatistic = z.object({type: z.string().optional(), id: z.string().optional(), attributes: loginStatisticAttributes.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 loginStatisticsCollection = z.object({data: z.array(loginStatistic).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 login statistics * * GET /statistics/logins */ export const getApiStatisticsLogins = async (args: GetApiStatisticsLoginsArgs, options: GetApiStatisticsLoginsOptions) => { 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/logins${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(loginStatisticsCollection, await res.json()) };