import {z} from 'zod' export type GetApiASAsnArgs = {asn: number}; export type GetApiASAsnOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const prefix = z.object({netblock: z.string(), id: z.string(), name: z.string(), country: z.string(), size: z.string().optional(), status: z.string().optional(), domain: z.string().nullable().optional(), rpki_status: z.enum(["valid", "invalid", "unknown"]).optional()}); export const prefix6 = z.object({netblock: z.string(), id: z.string(), name: z.string(), country: z.string(), size: z.string().optional(), status: z.string().optional(), domain: z.string().optional(), rpki_status: z.enum(["valid", "invalid", "unknown"]).optional()}); export const asnResponse = z.object({asn: z.string(), name: z.string(), country: z.string().optional(), allocated: z.string().optional(), registry: z.enum(["arin", "ripe", "apnic", "lacnic", "afrinic"]).optional(), domain: z.string(), num_ips: z.number().int().optional(), route: z.string().optional(), type: z.enum(["isp", "business", "education", "hosting", "government"]), prefixes: z.array(prefix).optional(), prefixes6: z.array(prefix6).optional(), peers: z.array(z.string()).optional(), upstreams: z.array(z.string()).optional(), downstreams: z.array(z.string()).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 }; /** * Returns information about an ASN. * * GET /AS{asn} * * Also accepts auth: BearerAuth, ApiKeyAuth */ export const getApiASAsn = async (args: GetApiASAsnArgs, options?: GetApiASAsnOptions) => { const {baseUrl = "https://ipinfo.io", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/AS${encodeURIComponent(String(args.asn))}${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(asnResponse, await res.json()) };