import {z} from 'zod' export type GetApiV1AssetPolicyPolicyIdEventsArgs = {limit?: number | undefined, offset?: number | undefined}; export type GetApiV1AssetPolicyPolicyIdEventsOptions = {baseUrl?: string | undefined, XAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const activityEvent = z.object({id: z.string().optional(), event_type: z.string(), subject_type: z.string(), subject_id: z.string(), subject_name: z.string().optional(), status: z.enum(["succeeded", "failed", "pending"]), error_message: z.string().optional(), channel: z.string().optional(), destination: z.string().optional(), alert_config_id: z.string().optional(), asset_policy_id: z.string().optional(), created_at: z.string(), updated_at: z.string().optional(), seen: z.boolean().optional(), message_payload: z.object({}).optional()}); export const getApiV1AssetPolicyPolicyIdEventsResponse = z.object({data: z.object({events: z.array(activityEvent).optional()}).optional(), total: z.number().int().optional(), limit: z.number().int().optional(), offset: z.number().int().optional(), message: z.string().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 asset policy events * * GET /v1/asset/policy/{policy_id}/events */ export const getApiV1AssetPolicyPolicyIdEvents = async (args: GetApiV1AssetPolicyPolicyIdEventsArgs, options?: GetApiV1AssetPolicyPolicyIdEventsOptions) => { const {baseUrl = "https://api.projectdiscovery.io", fetch: fetchFn = fetch, XAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (XAPIKey !== undefined) headers.set("X-API-Key", XAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/asset/policy/${encodeURIComponent(String(args.policy_id))}/events${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(getApiV1AssetPolicyPolicyIdEventsResponse, await res.json()) };