guardz-axios
Version:
Type-safe HTTP client built on top of Axios with runtime validation using guardz. Part of the guardz ecosystem for comprehensive TypeScript type safety.
208 lines • 6.56 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isRateLimitError = exports.isValidationError = exports.isNotFoundError = exports.isAuthorizationError = exports.isAuthenticationError = exports.isServerError = exports.isClientError = exports.isCancelError = exports.isTimeoutError = exports.isNetworkError = exports.isAxiosErrorWithResponse = exports.isAxiosError = void 0;
exports.isErrorWithStatus = isErrorWithStatus;
exports.isErrorWithCode = isErrorWithCode;
exports.categorizeAxiosError = categorizeAxiosError;
/**
* Type guard for basic AxiosError structure
*/
const isAxiosError = function (value, _config) {
if (typeof value !== "object" || value === null) {
return false;
}
const error = value;
// Check required properties
if (typeof error.message !== "string" ||
typeof error.name !== "string" ||
error.isAxiosError !== true) {
return false;
}
// Check optional properties
if (error.response !== undefined &&
(typeof error.response !== "object" || error.response === null)) {
return false;
}
if (error.config !== undefined &&
(typeof error.config !== "object" || error.config === null)) {
return false;
}
if (error.code !== undefined && typeof error.code !== "string") {
return false;
}
if (error.status !== undefined && typeof error.status !== "number") {
return false;
}
return true;
};
exports.isAxiosError = isAxiosError;
/**
* Type guard for AxiosError with response (4xx/5xx errors)
*/
const isAxiosErrorWithResponse = function (value, config) {
if (!(0, exports.isAxiosError)(value, config)) {
return false;
}
return value.response !== undefined && value.response !== null;
};
exports.isAxiosErrorWithResponse = isAxiosErrorWithResponse;
/**
* Type guard for network errors (no response received)
*/
const isNetworkError = function (value, config) {
if (!(0, exports.isAxiosError)(value, config)) {
return false;
}
const networkCodes = ["ENOTFOUND", "ECONNREFUSED", "ERR_NETWORK"];
return value.code !== undefined && networkCodes.includes(value.code);
};
exports.isNetworkError = isNetworkError;
/**
* Type guard for timeout errors
*/
const isTimeoutError = function (value, config) {
if (!(0, exports.isAxiosError)(value, config)) {
return false;
}
const timeoutCodes = ["ECONNABORTED", "ETIMEDOUT"];
return value.code !== undefined && timeoutCodes.includes(value.code);
};
exports.isTimeoutError = isTimeoutError;
/**
* Type guard for cancel errors
*/
const isCancelError = function (value, config) {
if (!(0, exports.isAxiosError)(value, config)) {
return false;
}
return value.code === "ERR_CANCELED";
};
exports.isCancelError = isCancelError;
/**
* Type guard for client errors (4xx status codes)
*/
const isClientError = function (value, config) {
if (!(0, exports.isAxiosErrorWithResponse)(value, config)) {
return false;
}
return value.response.status >= 400 && value.response.status < 500;
};
exports.isClientError = isClientError;
/**
* Type guard for server errors (5xx status codes)
*/
const isServerError = function (value, config) {
if (!(0, exports.isAxiosErrorWithResponse)(value, config)) {
return false;
}
return value.response.status >= 500 && value.response.status < 600;
};
exports.isServerError = isServerError;
/**
* Type guard for authentication errors (401 Unauthorized)
*/
const isAuthenticationError = function (value, config) {
if (!(0, exports.isAxiosErrorWithResponse)(value, config)) {
return false;
}
return value.response.status === 401;
};
exports.isAuthenticationError = isAuthenticationError;
/**
* Type guard for authorization errors (403 Forbidden)
*/
const isAuthorizationError = function (value, config) {
if (!(0, exports.isAxiosErrorWithResponse)(value, config)) {
return false;
}
return value.response.status === 403;
};
exports.isAuthorizationError = isAuthorizationError;
/**
* Type guard for not found errors (404 Not Found)
*/
const isNotFoundError = function (value, config) {
if (!(0, exports.isAxiosErrorWithResponse)(value, config)) {
return false;
}
return value.response.status === 404;
};
exports.isNotFoundError = isNotFoundError;
/**
* Type guard for validation errors (422 Unprocessable Entity)
*/
const isValidationError = function (value, config) {
if (!(0, exports.isAxiosErrorWithResponse)(value, config)) {
return false;
}
return value.response.status === 422;
};
exports.isValidationError = isValidationError;
/**
* Type guard for rate limit errors (429 Too Many Requests)
*/
const isRateLimitError = function (value, config) {
if (!(0, exports.isAxiosErrorWithResponse)(value, config)) {
return false;
}
return value.response.status === 429;
};
exports.isRateLimitError = isRateLimitError;
/**
* Type guard for specific error status code
*/
function isErrorWithStatus(statusCode) {
return function (value, config) {
if (!(0, exports.isAxiosErrorWithResponse)(value, config)) {
return false;
}
return value.response.status === statusCode;
};
}
/**
* Type guard for specific error code
*/
function isErrorWithCode(errorCode) {
return function (value, config) {
if (!(0, exports.isAxiosError)(value, config)) {
return false;
}
return value.code === errorCode;
};
}
/**
* Comprehensive error categorization function
*/
function categorizeAxiosError(error) {
if (!(0, exports.isAxiosError)(error)) {
return {
isAxiosError: false,
category: "unknown",
hasResponse: false,
};
}
const result = {
isAxiosError: true,
statusCode: error.response?.status,
errorCode: error.code,
hasResponse: !!error.response,
category: "unknown",
};
if ((0, exports.isCancelError)(error)) {
result.category = "cancel";
}
else if ((0, exports.isTimeoutError)(error)) {
result.category = "timeout";
}
else if ((0, exports.isNetworkError)(error)) {
result.category = "network";
}
else if ((0, exports.isClientError)(error)) {
result.category = "client";
}
else if ((0, exports.isServerError)(error)) {
result.category = "server";
}
return result;
}
//# sourceMappingURL=axios-error-guards.js.map