2018-02-14 16:00:32 -08:00
|
|
|
import { IReadDatasetsOptionBag } from 'wherehows-web/typings/api/datasets/dataset';
|
|
|
|
import { getJSON } from 'wherehows-web/utils/api/fetcher';
|
|
|
|
import { ApiVersion, getApiRoot } from 'wherehows-web/utils/api/shared';
|
2018-02-21 11:06:21 -08:00
|
|
|
import { encodeForwardSlash } from 'wherehows-web/utils/validators/urn';
|
2018-02-14 16:00:32 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the base url for a platform given a specified ApiVersion
|
|
|
|
* @param {ApiVersion} version
|
|
|
|
*/
|
|
|
|
export const platformsUrlRoot = (version: ApiVersion) => `${getApiRoot(version)}/platforms`;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Composes a url for platforms, uses platform and prefix if provided
|
|
|
|
* @param {IReadDatasetsOptionBag} {platform}
|
|
|
|
* @param {IReadDatasetsOptionBag} {prefix}
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
const platformsUrl = ({ platform, prefix }: IReadDatasetsOptionBag): string => {
|
|
|
|
const urlRoot = platformsUrlRoot('v2');
|
|
|
|
|
|
|
|
if (platform && prefix) {
|
2018-02-21 11:06:21 -08:00
|
|
|
return `${urlRoot}/${platform}/prefix/${encodeForwardSlash(prefix)}`;
|
2018-02-14 16:00:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (platform) {
|
|
|
|
return `${urlRoot}/${platform}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return urlRoot;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads the platforms endpoint and returns a list of platforms or prefixes found
|
|
|
|
* @param {IReadDatasetsOptionBag} {platform}
|
|
|
|
* @param {IReadDatasetsOptionBag} {prefix}
|
|
|
|
* @returns {Promise<Array<string>>}
|
|
|
|
*/
|
|
|
|
const readPlatforms = async ({ platform, prefix }: IReadDatasetsOptionBag): Promise<Array<string>> => {
|
|
|
|
const url = platformsUrl({ platform, prefix });
|
|
|
|
const response = await getJSON<Array<string>>({ url });
|
|
|
|
|
|
|
|
return response || [];
|
|
|
|
};
|
|
|
|
|
|
|
|
export { readPlatforms };
|