import {z} from 'zod' export type SavedSearchRequest = {search: {datasource: 'hostnames' | 'scans', createdAt?: string | undefined, description?: string | undefined, longDescription?: string | undefined, name: string, ownerDescription?: string | undefined, permissions?: (Array<'public:read' | 'team:read' | 'team:write'>) | undefined, query: string, tlp?: ('red' | 'amber+strict' | 'amber' | 'green' | 'clear') | undefined, usertags?: Array | undefined}}; export type UpdateApiApiV1UserSearchesSearchIdArgs = {searchId: string, body: SavedSearchRequest}; export type UpdateApiApiV1UserSearchesSearchIdOptions = {baseUrl?: string | undefined, apikeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const savedSearchResponse = z.object({datasource: z.enum(["hostnames", "scans"]).optional(), createdAt: z.string().optional(), description: z.string().optional(), longDescription: z.string().optional(), name: z.string().optional(), ownerDescription: z.string().optional(), permissions: z.array(z.enum(["public:read", "team:read", "team:write"])).optional(), query: z.string().optional(), tlp: z.enum(["red", "amber+strict", "amber", "green", "clear"]).optional(), usertags: z.array(z.string()).optional(), _id: z.string().optional()}); export const updateApiApiV1UserSearchesSearchIdResponse = z.object({search: savedSearchResponse.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 }; /** * Update Saved Search * * PUT /api/v1/user/searches/{searchId}/ */ export const updateApiApiV1UserSearchesSearchId = async (args: UpdateApiApiV1UserSearchesSearchIdArgs, options?: UpdateApiApiV1UserSearchesSearchIdOptions) => { const {baseUrl = "https://urlscan.io", fetch: fetchFn = fetch, apikeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apikeyAuth !== undefined) headers.set("api-key", apikeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/user/searches/${encodeURIComponent(String(args.searchId))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiApiV1UserSearchesSearchIdResponse, await res.json()) };