77 lines
2.0 KiB
TypeScript
Raw Normal View History

2019-08-31 20:51:14 -07:00
import { ApiError } from '@datahub/utils/api/error';
/**
* Defines available api version types
2019-08-31 20:51:14 -07:00
* @type {string}
*/
2019-08-31 20:51:14 -07:00
export enum ApiVersion {
v1 = 'v1',
v2 = 'v2'
}
/**
* Defines the root path for wherehows front-end api requests
2019-08-31 20:51:14 -07:00
* @param {ApiVersion} [version = ApiVersion.v1] the
* @return {string}
*/
2019-08-31 20:51:14 -07:00
export const getApiRoot = (version: ApiVersion = ApiVersion.v1) => `/api/${version}`;
/**
* Defines the literal possible string enum values for the an api response status
* @type {string}
*/
export enum ApiStatus {
OK = 'ok',
// Adds support for success response used in api's like /comments, will refactored to use ok
SUCCESS = 'success',
FAILED = 'failed',
ERROR = 'error'
}
/**
2019-08-31 20:51:14 -07:00
* Enumerates the currently available Api status errors
* @type {number}
*/
export enum ApiResponseStatus {
2019-08-31 20:51:14 -07:00
Ok = 200,
NotFound = 404,
UnAuthorized = 401,
InternalServerError = 500
}
/**
* Type guard discriminates an error object as an instance of ApiError
* @param {Error} e
* @return {boolean}
*/
const isApiError = (e: Error): e is ApiError => e instanceof ApiError;
2019-08-31 20:51:14 -07:00
/**
* Curried function further checks if the error instance is an ApiStatusError
* @param {Error} e he error object to check
*/
const isApiStatusError = (e: Error) => (apiResponseStatus: ApiResponseStatus) =>
isApiError(e) && e.status === apiResponseStatus;
/**
* Convenience function to ascertain if an api error is a not found code
* @param {Error} e
* @return {boolean}
*/
2019-08-31 20:51:14 -07:00
export const isNotFoundApiError = (e: Error) => isApiStatusError(e)(ApiResponseStatus.NotFound);
/**
* Checks that a server response status is a server exception
* @param {Error} e
* @return {boolean}
*/
export const isServerExceptionApiError = (e: Error) =>
isApiError(e) && e.status >= ApiResponseStatus.InternalServerError;
/**
* Checks that a server response status is an unauthorized error
* @param {Error} e
* @return {boolean}
*/
2019-08-31 20:51:14 -07:00
export const isUnAuthorizedApiError = (e: Error) => isApiStatusError(e)(ApiResponseStatus.UnAuthorized);