1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00
Files
checktheroads-hyperglass/hyperglass/ui/hooks/useASNDetail.ts

31 lines
985 B
TypeScript
Raw Normal View History

import { useQuery } from 'react-query';
2021-01-03 23:51:09 -07:00
import type { QueryObserverResult } from 'react-query';
import type { TASNQuery } from '~/types';
import type { TUseASNDetailFn } from './types';
async function query(ctx: TUseASNDetailFn): Promise<TASNQuery> {
const [asn] = ctx.queryKey;
const res = await fetch('https://api.asrank.caida.org/v2/graphql', {
mode: 'cors',
method: 'POST',
headers: { 'content-type': 'application/json' },
2021-01-03 23:51:09 -07:00
/* eslint no-useless-escape: 0 */
body: JSON.stringify({ query: `{ asn(asn:\"${asn}\"){ organization { orgName } } }` }),
});
return await res.json();
}
2020-12-31 23:09:54 -07:00
/**
* Query the Caida AS Rank API to get an ASN's organization name for the AS Path component.
* @see https://api.asrank.caida.org/v2/docs
2020-12-31 23:09:54 -07:00
*/
2021-01-03 23:51:09 -07:00
export function useASNDetail(asn: string): QueryObserverResult<TASNQuery> {
return useQuery(asn, query, {
refetchOnWindowFocus: false,
refetchInterval: false,
refetchOnMount: false,
cacheTime: Infinity,
});
}