2020-12-22 02:00:36 -07:00
|
|
|
import { useQuery } from 'react-query';
|
2020-12-29 16:50:26 -07:00
|
|
|
|
2021-01-03 23:51:09 -07:00
|
|
|
import type { QueryObserverResult } from 'react-query';
|
2021-01-01 01:34:42 -07:00
|
|
|
import type { TASNQuery } from '~/types';
|
2020-12-29 16:50:26 -07:00
|
|
|
import type { TUseASNDetailFn } from './types';
|
2020-12-22 02:00:36 -07:00
|
|
|
|
2021-01-01 01:34:42 -07:00
|
|
|
async function query(ctx: TUseASNDetailFn): Promise<TASNQuery> {
|
2020-12-29 16:50:26 -07:00
|
|
|
const [asn] = ctx.queryKey;
|
2021-01-01 01:34:42 -07:00
|
|
|
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 */
|
2021-01-01 01:34:42 -07:00
|
|
|
body: JSON.stringify({ query: `{ asn(asn:\"${asn}\"){ organization { orgName } } }` }),
|
|
|
|
});
|
2020-12-22 02:00:36 -07:00
|
|
|
return await res.json();
|
|
|
|
}
|
|
|
|
|
2020-12-31 23:09:54 -07:00
|
|
|
/**
|
2021-01-01 01:34:42 -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> {
|
2020-12-29 16:50:26 -07:00
|
|
|
return useQuery(asn, query, {
|
|
|
|
refetchOnWindowFocus: false,
|
|
|
|
refetchInterval: false,
|
|
|
|
refetchOnMount: false,
|
|
|
|
cacheTime: Infinity,
|
|
|
|
});
|
2020-12-22 02:00:36 -07:00
|
|
|
}
|