Skip to content

Commit

Permalink
fix(types): fixed AxiosRequestConfig header interface by refactoring …
Browse files Browse the repository at this point in the history
…it to RawAxiosRequestConfig; (#5420)
  • Loading branch information
DigitalBrainJS committed Jan 6, 2023
1 parent 8651bf1 commit 0811963
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 226 deletions.
42 changes: 23 additions & 19 deletions index.d.cts
Expand Up @@ -103,24 +103,24 @@ declare class CanceledError<T> extends AxiosError<T> {
}

declare class Axios {
constructor(config?: axios.AxiosRequestConfig);
constructor(config?: axios.RawAxiosRequestConfig);
defaults: axios.AxiosDefaults;
interceptors: {
request: axios.AxiosInterceptorManager<axios.AxiosRequestConfig>;
response: axios.AxiosInterceptorManager<axios.AxiosResponse>;
};
getUri(config?: axios.AxiosRequestConfig): string;
request<T = any, R = axios.AxiosResponse<T>, D = any>(config: axios.AxiosRequestConfig<D>): Promise<R>;
get<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.AxiosRequestConfig<D>): Promise<R>;
delete<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.AxiosRequestConfig<D>): Promise<R>;
head<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.AxiosRequestConfig<D>): Promise<R>;
options<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.AxiosRequestConfig<D>): Promise<R>;
post<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
put<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
patch<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
postForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
putForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
patchForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.AxiosRequestConfig<D>): Promise<R>;
getUri(config?: axios.RawAxiosRequestConfig): string;
request<T = any, R = axios.AxiosResponse<T>, D = any>(config: axios.RawAxiosRequestConfig<D>): Promise<R>;
get<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
delete<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
head<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
options<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
post<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
put<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
patch<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
postForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
putForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
patchForm<T = any, R = axios.AxiosResponse<T>, D = any>(url: string, data?: D, config?: axios.RawAxiosRequestConfig<D>): Promise<R>;
}

declare enum HttpStatusCode {
Expand Down Expand Up @@ -342,7 +342,7 @@ declare namespace axios {

type AxiosAdapterConfig = AxiosAdapter | AxiosAdapterName;

interface AxiosRequestConfig<D = any> {
interface RawAxiosRequestConfig<D = any> {
url?: string;
method?: Method | string;
baseURL?: string;
Expand Down Expand Up @@ -384,6 +384,10 @@ declare namespace axios {
formSerializer?: FormSerializerOptions;
}

interface AxiosRequestConfig<D = any> extends RawAxiosRequestConfig {

This comment has been minimized.

Copy link
@hilleer

hilleer Jan 18, 2023

Contributor

Looks like you forgot to pass generic D here to RawAxiosRequestConfig. I'm seeing compilation errors after bumping to latest version due to this.

headers: AxiosRequestHeaders;
}

interface HeadersDefaults {
common: RawAxiosRequestHeaders;
delete: RawAxiosRequestHeaders;
Expand All @@ -398,11 +402,11 @@ declare namespace axios {
unlink?: RawAxiosRequestHeaders;
}

interface AxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
interface AxiosDefaults<D = any> extends Omit<RawAxiosRequestConfig<D>, 'headers'> {
headers: HeadersDefaults;
}

interface CreateAxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
interface CreateAxiosDefaults<D = any> extends Omit<RawAxiosRequestConfig<D>, 'headers'> {
headers?: RawAxiosRequestHeaders | AxiosHeaders | Partial<HeadersDefaults>;
}

Expand All @@ -426,7 +430,7 @@ declare namespace axios {
}

interface Canceler {
(message?: string, config?: AxiosRequestConfig, request?: any): void;
(message?: string, config?: RawAxiosRequestConfig, request?: any): void;
}

interface CancelTokenStatic {
Expand Down Expand Up @@ -457,8 +461,8 @@ declare namespace axios {
}

interface AxiosInstance extends Axios {
<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
<T = any, R = AxiosResponse<T>, D = any>(config: RawAxiosRequestConfig<D>): Promise<R>;
<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: RawAxiosRequestConfig<D>): Promise<R>;

defaults: Omit<AxiosDefaults, 'headers'> & {
headers: HeadersDefaults & {
Expand Down
42 changes: 23 additions & 19 deletions index.d.ts
Expand Up @@ -283,7 +283,7 @@ type AxiosAdapterName = 'xhr' | 'http' | string;

type AxiosAdapterConfig = AxiosAdapter | AxiosAdapterName;

export interface AxiosRequestConfig<D = any> {
export interface RawAxiosRequestConfig<D = any> {
url?: string;
method?: Method | string;
baseURL?: string;
Expand Down Expand Up @@ -325,6 +325,10 @@ export interface AxiosRequestConfig<D = any> {
formSerializer?: FormSerializerOptions;
}

export interface AxiosRequestConfig<D = any> extends RawAxiosRequestConfig {
headers: AxiosRequestHeaders;
}

export interface HeadersDefaults {
common: RawAxiosRequestHeaders;
delete: RawAxiosRequestHeaders;
Expand All @@ -339,11 +343,11 @@ export interface HeadersDefaults {
unlink?: RawAxiosRequestHeaders;
}

export interface AxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
export interface AxiosDefaults<D = any> extends Omit<RawAxiosRequestConfig<D>, 'headers'> {
headers: HeadersDefaults;
}

export interface CreateAxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
export interface CreateAxiosDefaults<D = any> extends Omit<RawAxiosRequestConfig<D>, 'headers'> {
headers?: RawAxiosRequestHeaders | AxiosHeaders | Partial<HeadersDefaults>;
}

Expand Down Expand Up @@ -409,7 +413,7 @@ export interface Cancel {
}

export interface Canceler {
(message?: string, config?: AxiosRequestConfig, request?: any): void;
(message?: string, config?: RawAxiosRequestConfig, request?: any): void;
}

export interface CancelTokenStatic {
Expand Down Expand Up @@ -440,29 +444,29 @@ export interface AxiosInterceptorManager<V> {
}

export class Axios {
constructor(config?: AxiosRequestConfig);
constructor(config?: RawAxiosRequestConfig);
defaults: AxiosDefaults;
interceptors: {
request: AxiosInterceptorManager<AxiosRequestConfig>;
response: AxiosInterceptorManager<AxiosResponse>;
};
getUri(config?: AxiosRequestConfig): string;
request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
get<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
delete<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
head<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
options<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
post<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
put<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
patch<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
postForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
putForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
patchForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
getUri(config?: RawAxiosRequestConfig): string;
request<T = any, R = AxiosResponse<T>, D = any>(config: RawAxiosRequestConfig<D>): Promise<R>;
get<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: RawAxiosRequestConfig<D>): Promise<R>;
delete<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: RawAxiosRequestConfig<D>): Promise<R>;
head<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: RawAxiosRequestConfig<D>): Promise<R>;
options<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: RawAxiosRequestConfig<D>): Promise<R>;
post<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig<D>): Promise<R>;
put<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig<D>): Promise<R>;
patch<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig<D>): Promise<R>;
postForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig<D>): Promise<R>;
putForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig<D>): Promise<R>;
patchForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: RawAxiosRequestConfig<D>): Promise<R>;
}

export interface AxiosInstance extends Axios {
<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
<T = any, R = AxiosResponse<T>, D = any>(config: RawAxiosRequestConfig<D>): Promise<R>;
<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: RawAxiosRequestConfig<D>): Promise<R>;

defaults: Omit<AxiosDefaults, 'headers'> & {
headers: HeadersDefaults & {
Expand Down
117 changes: 60 additions & 57 deletions test/module/test.js
Expand Up @@ -58,90 +58,93 @@ describe('module', function () {
await remove(BACKUP_PATH);
});

it('should have consistent ESM export', function () {
const namedExport = {};
const factoryExport = {};

Object.entries(axiosFactory).forEach(([key, value]) => {
if(!utils.hasOwnProp(Axios, key) && !(key in instance) && ignoreList.indexOf(key) === -1) {
factoryExport[key] = value;
}
});
describe('export', function () {

Object.entries(axios).forEach(([key, value]) => {
key!=='default' && ignoreList.indexOf(key) === -1 && (namedExport[key] = value);
});
it('should have consistent ESM export', function () {
const namedExport = {};
const factoryExport = {};

assert.deepStrictEqual(namedExport, factoryExport);
});
Object.entries(axiosFactory).forEach(([key, value]) => {
if (!utils.hasOwnProp(Axios, key) && !(key in instance) && ignoreList.indexOf(key) === -1) {
factoryExport[key] = value;
}
});

describe('CommonJS', ()=> {
const pkgPath = path.join(__dirname, './cjs');
Object.entries(axios).forEach(([key, value]) => {
key !== 'default' && ignoreList.indexOf(key) === -1 && (namedExport[key] = value);
});

after(async ()=> {
await remove(path.join(pkgPath, './node_modules'));
assert.deepStrictEqual(namedExport, factoryExport);
});

it('should be able to be loaded with require', async function () {
this.timeout(30000);
describe('CommonJS', () => {
const pkgPath = path.join(__dirname, './cjs');

await exec(`npm test --prefix ${pkgPath}`);
});
});
after(async () => {
await remove(path.join(pkgPath, './node_modules'));
});

describe('ESM', ()=> {
const pkgPath = path.join(__dirname, './esm');
it('should be able to be loaded with require', async function () {
this.timeout(30000);

after(async ()=> {
await remove(path.join(pkgPath, './node_modules'));
await exec(`npm test --prefix ${pkgPath}`);
});
});

it('should be able to be loaded with import', async function () {
this.timeout(30000);
describe('ESM', () => {
const pkgPath = path.join(__dirname, './esm');

await exec(`npm test --prefix ${pkgPath}`);
});
});
after(async () => {
await remove(path.join(pkgPath, './node_modules'));
});

describe('TS', ()=> {
const pkgPath = path.join(__dirname, './ts');
it('should be able to be loaded with import', async function () {
this.timeout(30000);

after(async ()=> {
await remove(path.join(pkgPath, './node_modules'));
await exec(`npm test --prefix ${pkgPath}`);
});
});

it('should be able to be loaded with import', async function () {
this.timeout(30000);
describe('TS', () => {
const pkgPath = path.join(__dirname, './ts');

await exec(`npm test --prefix ${pkgPath}`, {});
});
});
after(async () => {
await remove(path.join(pkgPath, './node_modules'));
});

describe('TS require(\'axios\')', ()=> {
const pkgPath = path.join(__dirname, './ts-require');
it('should be able to be loaded with import', async function () {
this.timeout(30000);

after(async ()=> {
await remove(path.join(pkgPath, './node_modules'));
await exec(`npm test --prefix ${pkgPath}`, {});
});
});

it('should be able to be loaded with require', async function () {
this.timeout(30000);
describe('TS require(\'axios\')', () => {
const pkgPath = path.join(__dirname, './ts-require');

await exec(`npm test --prefix ${pkgPath}`, {});
});
});
after(async () => {
await remove(path.join(pkgPath, './node_modules'));
});

describe('TS require(\'axios\').default', ()=> {
const pkgPath = path.join(__dirname, './ts-require-default');
it('should be able to be loaded with require', async function () {
this.timeout(30000);

after(async ()=> {
await remove(path.join(pkgPath, './node_modules'));
await exec(`npm test --prefix ${pkgPath}`, {});
});
});

it('should be able to be loaded with require', async function () {
this.timeout(30000);
describe('TS require(\'axios\').default', () => {
const pkgPath = path.join(__dirname, './ts-require-default');

after(async () => {
await remove(path.join(pkgPath, './node_modules'));
});

it('should be able to be loaded with require', async function () {
this.timeout(30000);

await exec(`npm test --prefix ${pkgPath}`, {});
await exec(`npm test --prefix ${pkgPath}`, {});
});
});
});

Expand Down
6 changes: 3 additions & 3 deletions test/module/typings/cjs/index.ts
@@ -1,6 +1,6 @@
import axios = require('axios');

const config: axios.AxiosRequestConfig = {
const config: axios.RawAxiosRequestConfig = {
url: '/user',
method: 'get',
baseURL: 'https://api.example.com/',
Expand Down Expand Up @@ -38,11 +38,11 @@ const config: axios.AxiosRequestConfig = {
cancelToken: new axios.CancelToken((cancel: axios.Canceler) => {})
};

const nullValidateStatusConfig: axios.AxiosRequestConfig = {
const nullValidateStatusConfig: axios.RawAxiosRequestConfig = {
validateStatus: null
};

const undefinedValidateStatusConfig: axios.AxiosRequestConfig = {
const undefinedValidateStatusConfig: axios.RawAxiosRequestConfig = {
validateStatus: undefined
};

Expand Down

0 comments on commit 0811963

Please sign in to comment.