import {z} from 'zod' export type GetApiEventsEventIdOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const eventResourceType = z.enum(["site", "cluster", "mission", "cluster_visit", "site_visit", "mission_media", "asset", "defect", "site_inspection", "asset_inspection", "organization", "user", "role", "api_key", "webhook"]); export const eventActionType = z.enum(["create", "update", "delete"]); export const event = z.object({id: z.string(), organizationId: z.string(), transactionId: z.string(), resourceType: eventResourceType, resourceId: z.string().nullable(), resourceIds: z.array(z.string()).nullable(), resourceName: z.string(), action: eventActionType, actorId: z.string(), actorType: z.enum(["user", "api_key", "system"]).nullable(), actorName: z.string().nullable(), actorRoleName: z.string().nullable(), actorIsSuperAdmin: z.boolean().nullable(), timestamp: z.number(), before: z.record(z.string(), z.unknown()).nullable(), after: z.record(z.string(), z.unknown()).nullable()}); export const getApiEventsEventIdResponse = z.object({data: event}); 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 event * * GET /events/{eventId} * * Also accepts auth: ApiKeyAuth */ export const getApiEventsEventId = async (options: GetApiEventsEventIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/events/${encodeURIComponent(String(args.eventId))}${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(getApiEventsEventIdResponse, await res.json()) };