Migrated ShlinkApiClient from axios to fetch

This commit is contained in:
Alejandro Celaya
2022-11-13 16:57:16 +01:00
parent ba48104c5c
commit 16bee43f12
5 changed files with 58 additions and 46 deletions

View File

@@ -8,11 +8,18 @@ import {
RegularNotFound,
} from '../types/errors';
const isProblemDetails = (e: unknown): e is ProblemDetailsError =>
!!e && typeof e === 'object' && Object.keys(e).every((key) => ['type', 'detail', 'title', 'status'].includes(key));
const isAxiosError = (e: unknown): e is AxiosError<ProblemDetailsError> => !!e && typeof e === 'object' && 'response' in e;
export const parseApiError = (e: unknown): ProblemDetailsError | undefined => (
isAxiosError(e) ? e.response?.data : undefined
);
export const parseApiError = (e: unknown): ProblemDetailsError | undefined => {
if (isProblemDetails(e)) {
return e;
}
return (isAxiosError(e) ? e.response?.data : undefined);
};
export const isInvalidArgumentError = (error?: ProblemDetailsError): error is InvalidArgumentError =>
error?.type === ErrorTypeV2.INVALID_ARGUMENT || error?.type === ErrorTypeV3.INVALID_ARGUMENT;