import {z} from 'zod' export type CreateApiFortinetArgs = {page?: number | undefined, limit?: number | undefined}; export type CreateApiFortinetOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fortinetCredential = z.object({url: z.string().nullable().optional(), login: z.string().nullable().optional(), password: z.string().nullable().optional(), FortiGuardID: z.string().nullable().optional(), country: z.string().nullable().optional()}); export const fortinetData = z.object({domain: z.string().nullable().optional(), industry: z.string().nullable().optional(), size: z.string().nullable().optional(), revenue: z.string().nullable().optional(), credentials: z.array(fortinetCredential).optional()}); export const createApiFortinetResponse = z.object({data: z.array(fortinetData).optional(), total: z.number().int().optional(), page: z.number().int().optional(), limit: z.number().int().optional(), totalPages: 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 }; /** * FortiBleed Feed * * POST /fortinet */ export const createApiFortinet = async (args: CreateApiFortinetArgs, options?: CreateApiFortinetOptions) => { const {baseUrl = "https://api.hudsonrock.com/json/v3", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKeyAuth !== undefined) headers.set("api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/fortinet${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiFortinetResponse, await res.json()) };