shlink-web-client/shlink-web-component/api-contract/utils.ts

26 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-02-18 12:40:37 +03:00
import type {
InvalidArgumentError,
InvalidShortUrlDeletion,
ProblemDetailsError,
2023-07-24 18:30:58 +03:00
RegularNotFound } from './errors';
2023-02-18 12:40:37 +03:00
import {
ErrorTypeV2,
ErrorTypeV3,
2023-07-24 18:30:58 +03:00
} from './errors';
export const isInvalidArgumentError = (error?: ProblemDetailsError): error is InvalidArgumentError =>
error?.type === ErrorTypeV2.INVALID_ARGUMENT || error?.type === ErrorTypeV3.INVALID_ARGUMENT;
export const isInvalidDeletionError = (error?: ProblemDetailsError): error is InvalidShortUrlDeletion =>
error?.type === 'INVALID_SHORTCODE_DELETION'
|| error?.type === ErrorTypeV2.INVALID_SHORT_URL_DELETION
|| error?.type === ErrorTypeV3.INVALID_SHORT_URL_DELETION;
export const isRegularNotFound = (error?: ProblemDetailsError): error is RegularNotFound =>
(error?.type === ErrorTypeV2.NOT_FOUND || error?.type === ErrorTypeV3.NOT_FOUND) && error?.status === 404;
2023-07-24 18:30:58 +03:00
const isProblemDetails = (e: unknown): e is ProblemDetailsError =>
!!e && typeof e === 'object' && ['type', 'detail', 'title', 'status'].every((prop) => prop in e);
export const parseApiError = (e: unknown): ProblemDetailsError | undefined => (isProblemDetails(e) ? e : undefined);