Skip to content

Commit

Permalink
fix: export AxiosError class at top-level
Browse files Browse the repository at this point in the history
Currently, there is no way to use AxiosError correctly. This was caused only by exporting the type at the top-level but the implementation deep inside the axios instance.
Exported the AxiosError class at the top-level to fix inconsistencies.

Refs: axios#5062

BREAKING CHANGE: AxiosError should be used from the top-level import rather than using it from the axios instance.
  • Loading branch information
haneenmahd committed Oct 9, 2022
1 parent facb32c commit 477e0aa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ export interface AxiosResponse<T = any, D = any> {
request?: any;
}

export class AxiosErrorType<T = unknown, D = any> extends Error {
export class AxiosError<T = unknown, D = any> extends Error {
constructor(
message?: string,
code?: string,
Expand Down Expand Up @@ -429,7 +429,7 @@ export class AxiosErrorType<T = unknown, D = any> extends Error {
static readonly ETIMEDOUT = "ETIMEDOUT";
}

export class CanceledError<T> extends AxiosErrorType<T> {}
export class CanceledError<T> extends AxiosError<T> {}

export type AxiosPromise<T = any> = Promise<AxiosResponse<T>>;

Expand Down Expand Up @@ -566,12 +566,12 @@ export interface AxiosStatic extends AxiosInstance {
Cancel: CancelStatic;
CancelToken: CancelTokenStatic;
Axios: typeof Axios;
AxiosError: typeof AxiosErrorType;
AxiosError: typeof AxiosError;
readonly VERSION: string;
isCancel(value: any): value is Cancel;
all<T>(values: Array<T | Promise<T>>): Promise<T[]>;
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
isAxiosError<T = any, D = any>(payload: any): payload is AxiosErrorType<T, D>;
isAxiosError<T = any, D = any>(payload: any): payload is AxiosError<T, D>;
toFormData(
sourceObj: object,
targetFormData?: GenericFormData,
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import axios from './lib/axios.js';
export default axios;
import axios from "./lib/axios.js";
import AxiosError from "./lib/core/AxiosError.js";
export { AxiosError };
export default axios;
42 changes: 19 additions & 23 deletions lib/axios.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
'use strict';

import utils from './utils.js';
import bind from './helpers/bind.js';
import Axios from './core/Axios.js';
import mergeConfig from './core/mergeConfig.js';
import defaults from './defaults/index.js';
import formDataToJSON from './helpers/formDataToJSON.js';
import CanceledError from './cancel/CanceledError.js';
import CancelToken from './cancel/CancelToken.js';
import isCancel from './cancel/isCancel.js';
import {VERSION} from './env/data.js';
import toFormData from './helpers/toFormData.js';
import AxiosError from './core/AxiosError.js';
import spread from './helpers/spread.js';
import isAxiosError from './helpers/isAxiosError.js';
"use strict";

import utils from "./utils.js";
import bind from "./helpers/bind.js";
import Axios from "./core/Axios.js";
import mergeConfig from "./core/mergeConfig.js";
import defaults from "./defaults/index.js";
import formDataToJSON from "./helpers/formDataToJSON.js";
import CanceledError from "./cancel/CanceledError.js";
import CancelToken from "./cancel/CancelToken.js";
import isCancel from "./cancel/isCancel.js";
import { VERSION } from "./env/data.js";
import toFormData from "./helpers/toFormData.js";
import spread from "./helpers/spread.js";
import isAxiosError from "./helpers/isAxiosError.js";

/**
* Create an instance of Axios
Expand All @@ -27,10 +26,10 @@ function createInstance(defaultConfig) {
const instance = bind(Axios.prototype.request, context);

// Copy axios.prototype to instance
utils.extend(instance, Axios.prototype, context, {allOwnKeys: true});
utils.extend(instance, Axios.prototype, context, { allOwnKeys: true });

// Copy context to instance
utils.extend(instance, context, null, {allOwnKeys: true});
utils.extend(instance, context, null, { allOwnKeys: true });

// Factory for creating new instances
instance.create = function create(instanceConfig) {
Expand All @@ -53,9 +52,6 @@ axios.isCancel = isCancel;
axios.VERSION = VERSION;
axios.toFormData = toFormData;

// Expose AxiosError class
axios.AxiosError = AxiosError;

// alias for CanceledError for backward compatibility
axios.Cancel = axios.CanceledError;

Expand All @@ -69,8 +65,8 @@ axios.spread = spread;
// Expose isAxiosError
axios.isAxiosError = isAxiosError;

axios.formToJSON = thing => {
axios.formToJSON = (thing) => {
return formDataToJSON(utils.isHTMLForm(thing) ? new FormData(thing) : thing);
};

export default axios
export default axios;

0 comments on commit 477e0aa

Please sign in to comment.