import {z} from 'zod' export type GetApiV1ApvSimulationsArgs = {limit?: number | undefined, offset?: number | undefined, name_like?: string | undefined, status?: Array | undefined, simulation_type?: Array | undefined}; export type GetApiV1ApvSimulationsOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const pagination = z.object({limit: z.number().int().optional(), offset: z.number().int().optional(), total_count: z.number().int().optional()}); export const apvSimulationType = z.string(); export const apvSimulationListItem = z.object({created_at: z.number().int().optional(), created_by: z.string().optional(), last_run_time: z.number().int().optional(), name: z.string().optional(), run_uuid: z.string().optional(), simulation_type: apvSimulationType.optional(), simulation_uuid: z.string().optional(), status: z.string().optional()}); export const apvSimulationListOutput = z.object({pages: pagination.optional(), simulations: z.array(apvSimulationListItem).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 }; /** * # List APV Simulations * * GET /v1/apv/simulations */ export const getApiV1ApvSimulations = async (args: GetApiV1ApvSimulationsArgs, options?: GetApiV1ApvSimulationsOptions) => { const {baseUrl = "https://api.picussecurity.com", fetch: fetchFn = fetch, AccessToken} = 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 (args.name_like != null) query.append("name_like", String(args.name_like)) for (const item of args.status ?? []) query.append("status", String(item)) for (const item of args.simulation_type ?? []) query.append("simulation_type", String(item)) if (AccessToken !== undefined) headers.set("Authorization", AccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/v1/apv/simulations${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(apvSimulationListOutput, await res.json()) };