From c8baf303a2e64752fb8fe624fc267cfb8c6675d6 Mon Sep 17 00:00:00 2001 From: 142vip Date: Tue, 17 Dec 2024 00:05:11 +0800 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20axios=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E8=B0=83=E6=95=B4=EF=BC=8C=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/axios/src/config.default.ts | 6 ++ packages/axios/src/configuration.ts | 11 ++- packages/axios/src/http-service.factory.ts | 40 +++++++++++ .../{serviceManager.ts => http-service.ts} | 67 ++++--------------- packages/axios/src/index.ts | 6 +- packages/axios/src/interface.ts | 63 ----------------- 6 files changed, 67 insertions(+), 126 deletions(-) create mode 100644 packages/axios/src/config.default.ts create mode 100644 packages/axios/src/http-service.factory.ts rename packages/axios/src/{serviceManager.ts => http-service.ts} (71%) delete mode 100644 packages/axios/src/interface.ts diff --git a/packages/axios/src/config.default.ts b/packages/axios/src/config.default.ts new file mode 100644 index 000000000000..f393bd66b2de --- /dev/null +++ b/packages/axios/src/config.default.ts @@ -0,0 +1,6 @@ +/** + * 默认配置 + */ +export default { + axios: {}, +}; diff --git a/packages/axios/src/configuration.ts b/packages/axios/src/configuration.ts index d740c4f06a72..ff5834cc17cd 100644 --- a/packages/axios/src/configuration.ts +++ b/packages/axios/src/configuration.ts @@ -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 => { @@ -31,7 +30,7 @@ import { HttpServiceFactory } from './serviceManager'; }, }) export class AxiosConfiguration { - async onReady(container) { + public async onReady(container: IMidwayContainer): Promise { await container.getAsync(HttpServiceFactory); } } diff --git a/packages/axios/src/http-service.factory.ts b/packages/axios/src/http-service.factory.ts new file mode 100644 index 000000000000..1fdc6282f273 --- /dev/null +++ b/packages/axios/src/http-service.factory.ts @@ -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 { + @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 { + return axios.create(config); + } +} diff --git a/packages/axios/src/serviceManager.ts b/packages/axios/src/http-service.ts similarity index 71% rename from packages/axios/src/serviceManager.ts rename to packages/axios/src/http-service.ts index af1daaab4313..0693455612aa 100644 --- a/packages/axios/src/serviceManager.ts +++ b/packages/axios/src/http-service.ts @@ -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 { - @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 { - 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; } @@ -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); } diff --git a/packages/axios/src/index.ts b/packages/axios/src/index.ts index 7b6e737e8b55..3e18954e8902 100644 --- a/packages/axios/src/index.ts +++ b/packages/axios/src/index.ts @@ -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 }; diff --git a/packages/axios/src/interface.ts b/packages/axios/src/interface.ts deleted file mode 100644 index f52b07962242..000000000000 --- a/packages/axios/src/interface.ts +++ /dev/null @@ -1,63 +0,0 @@ -import type { - AxiosRequestConfig, - AxiosResponse, - AxiosInterceptorManager, - AxiosDefaults, -} from 'axios'; -export interface AxiosHttpService { - defaults: AxiosDefaults; - interceptors: { - request: AxiosInterceptorManager; - response: AxiosInterceptorManager; - }; - getUri(config?: AxiosRequestConfig): string; - request, D = any>( - config: AxiosRequestConfig - ): Promise; - get, D = any>( - url: string, - config?: AxiosRequestConfig - ): Promise; - delete, D = any>( - url: string, - config?: AxiosRequestConfig - ): Promise; - head, D = any>( - url: string, - config?: AxiosRequestConfig - ): Promise; - options, D = any>( - url: string, - config?: AxiosRequestConfig - ): Promise; - post, D = any>( - url: string, - data?: D, - config?: AxiosRequestConfig - ): Promise; - put, D = any>( - url: string, - data?: D, - config?: AxiosRequestConfig - ): Promise; - patch, D = any>( - url: string, - data?: D, - config?: AxiosRequestConfig - ): Promise; - postForm, D = any>( - url: string, - data?: D, - config?: AxiosRequestConfig - ): Promise; - putForm, D = any>( - url: string, - data?: D, - config?: AxiosRequestConfig - ): Promise; - patchForm, D = any>( - url: string, - data?: D, - config?: AxiosRequestConfig - ): Promise; -} From 61dd21cc205f6afb43703c1f85e28867c95e7ae1 Mon Sep 17 00:00:00 2001 From: 142vip Date: Tue, 17 Dec 2024 10:11:17 +0800 Subject: [PATCH 2/2] chore: update --- packages/axios/test/fixtures/base-app/package.json | 3 +-- packages/axios/test/fixtures/base-app/src/configuration.ts | 4 +--- packages/axios/test/index.test.ts | 2 +- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/axios/test/fixtures/base-app/package.json b/packages/axios/test/fixtures/base-app/package.json index 08ff86d16f76..65746417f715 100644 --- a/packages/axios/test/fixtures/base-app/package.json +++ b/packages/axios/test/fixtures/base-app/package.json @@ -1,6 +1,5 @@ { "name": "base-app", "version": "1.0.0", - "dependencies": { - } + "dependencies": {} } diff --git a/packages/axios/test/fixtures/base-app/src/configuration.ts b/packages/axios/test/fixtures/base-app/src/configuration.ts index be41e56eb485..0b6f2f8a76bf 100644 --- a/packages/axios/test/fixtures/base-app/src/configuration.ts +++ b/packages/axios/test/fixtures/base-app/src/configuration.ts @@ -5,6 +5,4 @@ import { Configuration } from '@midwayjs/core'; require('../../../../src') ] }) -export class AutoConfiguration { - -} +export class AutoConfiguration {} diff --git a/packages/axios/test/index.test.ts b/packages/axios/test/index.test.ts index 76c05ec3c7ab..0345e4024791 100644 --- a/packages/axios/test/index.test.ts +++ b/packages/axios/test/index.test.ts @@ -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); });