Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: axios component #4231

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/axios/src/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* 默认配置
*/
export default {
axios: {},
};
11 changes: 5 additions & 6 deletions packages/axios/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Configuration } from '@midwayjs/core';
import { HttpServiceFactory } from './serviceManager';
import { Configuration, IMidwayContainer } from '@midwayjs/core';
import ConfigDefault from './config.default';
import { HttpServiceFactory } from './http-service.factory';

@Configuration({
namespace: 'axios',
importConfigs: [
{
default: {
axios: {},
},
default: ConfigDefault,
},
],
importConfigFilter: config => {
Expand All @@ -31,7 +30,7 @@ import { HttpServiceFactory } from './serviceManager';
},
})
export class AxiosConfiguration {
async onReady(container) {
public async onReady(container: IMidwayContainer): Promise<void> {
await container.getAsync(HttpServiceFactory);
}
}
40 changes: 40 additions & 0 deletions packages/axios/src/http-service.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
Config,
Init,
Provide,
Scope,
ScopeEnum,
ServiceFactory,
} from '@midwayjs/core';
import axios, { AxiosInstance, CreateAxiosDefaults } from 'axios';

@Provide()
@Scope(ScopeEnum.Singleton)
export class HttpServiceFactory extends ServiceFactory<AxiosInstance> {
@Config('axios')
axiosConfig: any;

@Init()
async init() {
let axiosConfig = this.axiosConfig;
if (!this.axiosConfig['clients']) {
axiosConfig = {
default: {},
clients: {
default: this.axiosConfig,
},
};
}
await this.initClients(axiosConfig);
}
public getName(): string {
return 'axios';
}

protected async createClient(
config: CreateAxiosDefaults,
clientName: string
): Promise<AxiosInstance> {
return axios.create(config);
}
}
Original file line number Diff line number Diff line change
@@ -1,62 +1,31 @@
import type {
AxiosInstance,
AxiosRequestConfig,
AxiosResponse,
CreateAxiosDefaults,
} from 'axios';
import axios from 'axios';
import {
Config,
Init,
Inject,
MidwayCommonError,
Provide,
Scope,
ScopeEnum,
MidwayCommonError,
ServiceFactory,
} from '@midwayjs/core';
import { AxiosHttpService } from './interface';

@Provide()
@Scope(ScopeEnum.Singleton)
export class HttpServiceFactory extends ServiceFactory<AxiosInstance> {
@Config('axios')
axiosConfig;

@Init()
async init() {
let axiosConfig = this.axiosConfig;
if (!this.axiosConfig['clients']) {
axiosConfig = {
default: {},
clients: {
default: this.axiosConfig,
},
};
}
await this.initClients(axiosConfig);
}

protected async createClient(
config: CreateAxiosDefaults,
clientName: any
): Promise<AxiosInstance> {
return axios.create(config);
}

getName(): string {
return 'axios';
}
}
import { Axios, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { HttpServiceFactory } from './http-service.factory';

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

@Inject()
private serviceFactory: HttpServiceFactory;

@Init()
protected async init() {
const clientName = this.serviceFactory.getDefaultClientName() || 'default';

this.instance = this.serviceFactory.get(clientName);
if (!this.instance) {
throw new MidwayCommonError('axios default instance not found.');
}
}
get defaults() {
return this.instance.defaults;
}
Expand All @@ -65,16 +34,6 @@ export class HttpService implements AxiosHttpService {
return this.instance.interceptors;
}

@Init()
protected async init() {
this.instance = this.serviceFactory.get(
this.serviceFactory.getDefaultClientName?.() || 'default'
);
if (!this.instance) {
throw new MidwayCommonError('axios default instance not found.');
}
}

getUri(config?: AxiosRequestConfig): string {
return this.instance.getUri(config);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/axios/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import axios from 'axios';
import * as Axios from 'axios';

export { AxiosConfiguration as Configuration } from './configuration';
export * from './interface';
export * from './serviceManager';
export * from './http-service.factory';
export * from './http-service';
export {
/**
* @deprecated Use `Axios` directly
*/
axios,
Axios,
};
export { Axios };
63 changes: 0 additions & 63 deletions packages/axios/src/interface.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/axios/test/fixtures/base-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "base-app",
"version": "1.0.0",
"dependencies": {
}
"dependencies": {}
}
4 changes: 1 addition & 3 deletions packages/axios/test/fixtures/base-app/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ import { Configuration } from '@midwayjs/core';
require('../../../../src')
]
})
export class AutoConfiguration {

}
export class AutoConfiguration {}
2 changes: 1 addition & 1 deletion packages/axios/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('/test/index.test.ts', () => {
}
});

it('should test defaults and intercepters', () => {
it('should test defaults and interceptors', () => {
expect(httpService.defaults).toStrictEqual(axios.defaults);
expect(httpService.interceptors).toStrictEqual(axios.interceptors);
});
Expand Down
Loading