import {z} from 'zod' export type GetApiApiAuditLogV1EntriesArgs = {limit?: number | undefined, offset?: number | undefined, filter?: ({type?: Array | undefined, date_from?: string | undefined, date_to?: string | undefined, author_id?: Array | undefined, subject_id?: Array | undefined, ip?: (Array) | undefined}) | undefined, sort?: ('type' | 'email' | 'created_at' | 'ip' | 'subject_id' | '-type' | '-email' | '-created_at' | '-ip' | '-subject_id') | undefined}; export type GetApiApiAuditLogV1EntriesOptions = {baseUrl?: string | undefined, OAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiAuditLogV1EntriesResponse = z.object({data: z.array(z.object({id: z.string(), type: z.literal("ppms/audit-log-entry"), attributes: z.object({created_at: z.string(), type: z.string(), payload: z.object({}), author_id: z.string(), author_email: z.string().nullable(), subject_id: z.string(), version: z.number().int().gte(1)})})), meta: z.object({total: z.number().int()})}); 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 }; /** * Entry list * * GET /api/audit-log/v1/entries */ export const getApiApiAuditLogV1Entries = async (args: GetApiApiAuditLogV1EntriesArgs, options?: GetApiApiAuditLogV1EntriesOptions) => { const {baseUrl = "https://example.piwik.pro", fetch: fetchFn = fetch, OAuth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.filter != null) query.append("filter", JSON.stringify(args.filter)) if (args.sort != null) query.append("sort", String(args.sort)) if (OAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/audit-log/v1/entries${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(getApiApiAuditLogV1EntriesResponse, await res.json()) };