Skip to content

Commit

Permalink
feat: add custom axios config typings merge (#4278)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Feb 2, 2025
1 parent 683d79b commit 2fa19cb
Show file tree
Hide file tree
Showing 13 changed files with 12,275 additions and 23,687 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"tree-kill": "1.2.2",
"ts-jest": "29.2.5",
"ts-node": "10.9.2",
"typedoc": "^0.26.0",
"typedoc": "^0.26.11",
"typescript": "5.3.3",
"why-is-node-running": "2.3.0",
"zx": "8.2.4"
Expand Down
2 changes: 1 addition & 1 deletion packages/axios/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AxiosRequestConfig } from 'axios';
import { AxiosRequestConfig } from './dist/index';

export * from './dist/index';

Expand Down
3 changes: 2 additions & 1 deletion packages/axios/src/http-service.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
ScopeEnum,
ServiceFactory,
} from '@midwayjs/core';
import axios, { AxiosInstance, CreateAxiosDefaults } from 'axios';
import * as axios from 'axios';
import { AxiosInstance, CreateAxiosDefaults } from 'axios';

@Provide()
@Scope(ScopeEnum.Singleton)
Expand Down
7 changes: 5 additions & 2 deletions packages/axios/src/http-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
ScopeEnum,
MidwayCommonError,
} from '@midwayjs/core';
import { Axios, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { AxiosInstance } from 'axios';
import { HttpServiceFactory } from './http-service.factory';
import { AxiosRequestConfig, AxiosResponse } from './interface';

@Provide()
@Scope(ScopeEnum.Singleton)
export class HttpService implements Axios {
export class HttpService {
private instance: AxiosInstance;

@Inject()
Expand Down Expand Up @@ -120,3 +121,5 @@ export class HttpService implements Axios {
return this.instance.patchForm(url, data, config);
}
}

export interface HttpService extends AxiosInstance {}

Check failure on line 125 in packages/axios/src/http-service.ts

View workflow job for this annotation

GitHub Actions / lintAndTestLegacy (lts/*, ubuntu-latest)

An interface declaring no members is equivalent to its supertype
11 changes: 3 additions & 8 deletions packages/axios/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import * as axios from 'axios';
import * as Axios from 'axios';
const Axios = axios['default'];

export { AxiosConfiguration as Configuration } from './configuration';
export * from './http-service.factory';
export * from './http-service';
export {
/**
* @deprecated Use `Axios` directly
*/
axios,
Axios,
};
export * from './interface';
export { Axios };
15 changes: 15 additions & 0 deletions packages/axios/src/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
AxiosRequestConfig as OriginAxiosRequestConfig,
AxiosRequestHeaders,
AxiosResponse as OriginAxiosResponse
} from 'axios';

/**
* Interface for custom axios request config merging.
*/
export interface AxiosRequestConfig<D = any> extends OriginAxiosRequestConfig<D> {}
export interface AxiosResponse<T = any, D = any> extends OriginAxiosResponse<T, D> {
config: AxiosRequestConfig<D> & {
headers: AxiosRequestHeaders;
};
}
7 changes: 4 additions & 3 deletions packages/axios/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join } from 'path';
import { HttpService, axios } from '../src';
import { HttpService, Axios } from '../src';
import { createLightApp } from '@midwayjs/mock';
import * as nock from 'nock';

Expand Down Expand Up @@ -68,8 +68,9 @@ describe('/test/index.test.ts', () => {
});

it('should test defaults and interceptors', () => {
expect(httpService.defaults).toStrictEqual(axios.defaults);
expect(httpService.interceptors).toStrictEqual(axios.interceptors);
expect(Axios.get).toBeDefined();
expect(httpService.defaults).toStrictEqual(Axios.defaults);
expect(httpService.interceptors).toStrictEqual(Axios.interceptors);
});

it('should test get method', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/bullmq/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
],
"license": "MIT",
"devDependencies": {
"@midwayjs/core": "^3.19.0",
"@midwayjs/mock": "^3.19.2"
"@midwayjs/core": "workspace:^",
"@midwayjs/mock": "workspace:^"
},
"dependencies": {
"bullmq": "5.39.1"
Expand Down
Loading

0 comments on commit 2fa19cb

Please sign in to comment.