2020-01-29 16:48:24 -08:00
|
|
|
import { ApiVersion, getApiRoot } from '@datahub/utils/api/shared';
|
|
|
|
import { encodeUrn } from '@datahub/utils/validators/urn';
|
|
|
|
import { getJSON } from '@datahub/utils/api/fetcher';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic entity root path
|
2020-08-26 15:44:50 -07:00
|
|
|
* @param entityEndpoint the URL path segment or endpoint for the specific entity, this may be different from the entity name
|
2020-01-29 16:48:24 -08:00
|
|
|
* @param version version of the API, defaulted to V2
|
|
|
|
*/
|
2020-08-26 15:44:50 -07:00
|
|
|
export const entityApiRoot = (entityEndpoint: string, version: ApiVersion = ApiVersion.v2): string =>
|
|
|
|
`${getApiRoot(version)}/${entityEndpoint}`;
|
2020-01-29 16:48:24 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic entity read api url
|
|
|
|
* @param urn urn for the entity
|
2020-08-26 15:44:50 -07:00
|
|
|
* @param entityEndpoint the URL path segment or endpoint for the specific entity, this may be different from the entity name
|
2020-01-29 16:48:24 -08:00
|
|
|
*/
|
2020-08-26 15:44:50 -07:00
|
|
|
export const entityApiByUrn = (urn: string, entityEndpoint: string): string =>
|
|
|
|
`${entityApiRoot(entityEndpoint)}/${encodeUrn(urn)}`;
|
2020-01-29 16:48:24 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic entity read api call
|
|
|
|
* @param urn urn for the entity
|
2020-08-26 15:44:50 -07:00
|
|
|
* @param entityEndpoint the URL path segment or endpoint for the specific entity, this may be different from the entity name
|
2020-01-29 16:48:24 -08:00
|
|
|
*/
|
2020-08-26 15:44:50 -07:00
|
|
|
export const readEntity = <E>(urn: string, entityEndpoint: string): Promise<E> =>
|
|
|
|
getJSON<E>({ url: entityApiByUrn(urn, entityEndpoint) });
|