import { IDatasetsGetResponse, IDatasetView, IDatasetViewGetResponse, IReadDatasetsOptionBag } from 'wherehows-web/typings/api/datasets/dataset'; import { getHeaders, getJSON } from 'wherehows-web/utils/api/fetcher'; import { datasetsCountUrl, datasetsUrl, datasetsUrlRoot, datasetUrlByUrn } from 'wherehows-web/utils/api/datasets/shared'; import { encodeUrn } from 'wherehows-web/utils/validators/urn'; /** * Reads a dataset by urn, in the li format * @param {string} urn * @returns {Promise} */ const readDatasetByUrn = async (urn: string = ''): Promise => { const { dataset } = await getJSON>({ url: datasetUrlByUrn(encodeUrn(urn)) }); return dataset!; }; /** * Constructs a url to get a dataset urn given a dataset id * @param {number} id * @return {string} */ const datasetUrnTranslationUrlByUrn = (id: number): string => `${datasetsUrlRoot('v2')}/idtourn/${id}`; /** * Translates a dataset id to a dataset urn, using the endpoint at datasetIdTranslationUrlByUrn() * @param {number} id * @return {Promise} */ const datasetIdToUrn = async (id: number) => { const headers = await getHeaders({ url: datasetUrnTranslationUrlByUrn(id) }); return headers.get('whUrn'); }; /** * Fetches the datasets for a platform, and prefix and returns the list of datasets in the * response * @param {IReadDatasetsOptionBag} { * platform, * prefix * } * @returns {Promise} */ const readDatasets = async ({ platform, prefix }: IReadDatasetsOptionBag): Promise => { const url = datasetsUrl({ platform, prefix }); const response = await getJSON({ url }); return response ? [...response.elements] : []; }; /** * Gets the number of datasets, if provided, using the platform and prefix also * @param {Partial} { platform, prefix } * @returns {Promise} */ const readDatasetsCount = async ({ platform, prefix }: Partial): Promise => { const url = datasetsCountUrl({ platform, prefix }); return await getJSON({ url }); }; export { readDatasets, readDatasetsCount, readDatasetByUrn, datasetIdToUrn };