import {z} from 'zod' export type SecurityHuntCreateSchema = {projectId?: string | undefined, name: string, description?: string | undefined, searchQuery?: string | undefined, filters?: Record | undefined, timeRange?: ('Past 5 Mins' | 'Past 15 Mins' | 'Past 30 Mins' | 'Past 1 Hour' | 'Past 2 Hours' | 'Past 3 Hours' | 'Past 1 Day' | 'Past 2 Days' | 'Past 1 Week' | 'Past 2 Weeks' | 'Past 1 Month' | 'Past 3 Months' | 'Custom') | undefined, schedule?: ('Not Scheduled' | 'Every Hour' | 'Every 6 Hours' | 'Every Day' | 'Every Week') | undefined, notifyUsers?: (Array<{id?: string | undefined}>) | undefined, notifyTeams?: (Array<{id?: string | undefined}>) | undefined, libraryHuntId?: string | undefined}; export type CreateApiSecurityHuntArgs = {body: {data: SecurityHuntCreateSchema}}; export type CreateApiSecurityHuntOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const securityHuntReadSchema = z.object({_id: z.string().optional(), createdAt: z.object({}).optional(), updatedAt: z.object({}).optional(), deletedAt: z.object({}).optional(), projectId: z.string().optional(), name: z.string(), description: z.string().optional(), searchQuery: z.string().optional(), filters: z.object({}).optional(), timeRange: z.enum(["Past 5 Mins", "Past 15 Mins", "Past 30 Mins", "Past 1 Hour", "Past 2 Hours", "Past 3 Hours", "Past 1 Day", "Past 2 Days", "Past 1 Week", "Past 2 Weeks", "Past 1 Month", "Past 3 Months", "Custom"]).optional(), schedule: z.enum(["Not Scheduled", "Every Hour", "Every 6 Hours", "Every Day", "Every Week"]).optional(), notifyUsers: z.array(z.object({id: z.string().optional()})).optional(), notifyTeams: z.array(z.object({id: z.string().optional()})).optional(), libraryHuntId: z.string().optional(), scheduledByUserId: z.string().optional(), searchedUpToAt: z.object({}).optional(), nextRunAt: z.object({}).optional(), lastRunAt: z.object({}).optional(), lastRunEventCount: z.number().optional(), lastRunError: z.string().optional(), lastFoundAt: z.object({}).optional(), lastFoundSinceAt: z.object({}).optional(), lastFoundEventCount: z.number().optional(), createdByUserId: z.string().optional(), deletedByUserId: z.string().optional()}); export const createApiSecurityHuntResponse = z.object({data: securityHuntReadSchema.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 }; /** * Create Hunt * * POST /security-hunt */ export const createApiSecurityHunt = async (args: CreateApiSecurityHuntArgs, options?: CreateApiSecurityHuntOptions) => { const {baseUrl = "https://oneuptime.com/api", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("APIKey", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/security-hunt${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiSecurityHuntResponse, await res.json()) };