import {z} from 'zod' export type GetApiInvestigationsArgs = {q?: ({statuses?: Array | undefined, types?: Array | undefined, assignedAgents?: Array | undefined, entityTokens?: Array | undefined, investigationTokens?: Array | undefined, externalEntityIds?: Array | undefined, deviceIds?: Array | undefined, programNames?: Array | undefined, includeClosed?: boolean | undefined, archived?: boolean | undefined, createdAtFrom?: (number | string) | undefined, createdAtTo?: (number | string) | undefined, updatedAtFrom?: (number | string) | undefined, updatedAtTo?: (number | string) | undefined}) | undefined, pageNum?: number | undefined, numRowsPerPage?: number | undefined, sortColumn?: ('updated_at' | 'created_at' | 'total_alerts' | 'risk_score') | undefined, sortOrder?: ('DESC' | 'ASC') | undefined}; export type GetApiInvestigationsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getInvestigationQueue = z.object({_embedded: z.object({pagination: z.object({total_investigations: z.number().optional(), total_pages: z.number().optional(), page: z.number().optional(), per_page: z.number().optional()}).optional(), sorting: z.object({column: z.string().optional(), order: z.string().optional()}).optional()}).optional(), error: z.object({}).optional(), investigations: z.array(z.object({investigation_token: z.string().optional(), name: z.string().optional(), total_alerts: z.number().optional(), type: z.string().optional(), state: z.string().optional(), source: z.string().optional(), outcome: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), closed_at: z.string().nullable().optional(), archived_at: z.string().nullable().optional(), _embedded: z.object({entities: z.object({"P-KaKZxdSGnbxXfKXnXdyp": z.object({entity_name: z.string().optional(), entity_type: z.string().optional(), entity_token: z.string().optional(), risk_score: z.number().optional()}).optional()}).optional(), assigned_agent: z.object({first_name: z.string().optional(), last_name: z.string().optional(), email: z.string().optional(), token: z.string().optional()}).optional()}).optional(), device_ids: z.array(z.string()).optional(), program_names: z.array(z.string()).optional(), alert_assignees: z.array(z.string()).optional()})).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 list of Investigations * * GET /investigations * * Also accepts auth: basic */ export const getApiInvestigations = async (args: GetApiInvestigationsArgs, options?: GetApiInvestigationsOptions) => { const {baseUrl = "https://sandbox.alloy.co/v1", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", JSON.stringify(args.q)) if (args.pageNum != null) query.append("pageNum", String(args.pageNum)) if (args.numRowsPerPage != null) query.append("numRowsPerPage", String(args.numRowsPerPage)) if (args.sortColumn != null) query.append("sortColumn", String(args.sortColumn)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/investigations${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(getInvestigationQueue, await res.json()) };