import {z} from 'zod' export type GetApiSourcesArgs = {offset?: number | undefined, limit: number, sortby?: ('abc' | 'count' | 'count_asc' | 'txt_count' | 'txt_count_asc' | 'recency') | undefined, search?: string | undefined}; export type GetApiSourcesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiSourcesResponse = z.object({searches: z.array(z.object({id: z.number().int(), keyword: z.string(), search_type: z.enum(["keyword", "domain", "ip", "iprange", "moved", "literal", "cloudflare", "aws", "gcp", "azure", "asn"]), searchparams: z.string().optional(), dbdate: z.number().int().optional(), results_count: z.number().int(), results_fetched: z.number().int(), company_id: z.number().int(), created_at: z.string(), updated_at: z.string().optional(), deleted_at: z.string().optional(), screenshot: z.string().optional(), limited_result: z.union([z.literal(0), z.literal(1)]), txt_records_fetched: z.number().int(), is_elastic: z.boolean()})).optional(), total: z.number().int().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 of Sources * * GET /sources */ export const getApiSources = async (args: GetApiSourcesArgs, options?: GetApiSourcesOptions) => { const {baseUrl = "https://asm.cloud.tenable.com/api/1.0", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sortby != null) query.append("sortby", String(args.sortby)) if (args.search != null) query.append("search", String(args.search)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/sources${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(getApiSourcesResponse, await res.json()) };