@midwayjs/axios 3.3.4 → 3.4.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,7 +11,7 @@ const decorator_1 = require("@midwayjs/decorator");
11
11
  const serviceManager_1 = require("./serviceManager");
12
12
  let AxiosConfiguration = class AxiosConfiguration {
13
13
  async onReady(container) {
14
- await container.getAsync(serviceManager_1.HttpService);
14
+ await container.getAsync(serviceManager_1.HttpServiceFactory);
15
15
  }
16
16
  };
17
17
  AxiosConfiguration = __decorate([
@@ -1,11 +1,15 @@
1
- import { AxiosRequestConfig, AxiosResponse } from 'axios';
2
- import { IMidwayApplication } from '@midwayjs/core';
1
+ import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
2
+ import { ServiceFactory } from '@midwayjs/core';
3
3
  import { AxiosHttpService } from './interface';
4
+ export declare class HttpServiceFactory extends ServiceFactory<AxiosInstance> {
5
+ axiosConfig: any;
6
+ init(): Promise<void>;
7
+ protected createClient(config: any, clientName: any): Promise<AxiosInstance>;
8
+ getName(): string;
9
+ }
4
10
  export declare class HttpService implements AxiosHttpService {
5
11
  private instance;
6
- protected ctx: any;
7
- protected app: IMidwayApplication;
8
- protected httpConfig: AxiosRequestConfig;
12
+ private serviceFactory;
9
13
  get interceptors(): {
10
14
  request: import("axios").AxiosInterceptorManager<AxiosRequestConfig<any>>;
11
15
  response: import("axios").AxiosInterceptorManager<AxiosResponse<any, any>>;
@@ -9,44 +9,51 @@ var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.HttpService = void 0;
12
+ exports.HttpService = exports.HttpServiceFactory = void 0;
13
13
  const axios_1 = require("axios");
14
14
  const decorator_1 = require("@midwayjs/decorator");
15
- const interface_1 = require("./interface");
15
+ const core_1 = require("@midwayjs/core");
16
+ let HttpServiceFactory = class HttpServiceFactory extends core_1.ServiceFactory {
17
+ async init() {
18
+ let axiosConfig = this.axiosConfig;
19
+ if (!this.axiosConfig['clients']) {
20
+ axiosConfig = {
21
+ default: {},
22
+ clients: {
23
+ default: this.axiosConfig,
24
+ },
25
+ };
26
+ }
27
+ await this.initClients(axiosConfig);
28
+ }
29
+ async createClient(config, clientName) {
30
+ return axios_1.default.create(config);
31
+ }
32
+ getName() {
33
+ return 'axios';
34
+ }
35
+ };
36
+ __decorate([
37
+ (0, decorator_1.Config)('axios'),
38
+ __metadata("design:type", Object)
39
+ ], HttpServiceFactory.prototype, "axiosConfig", void 0);
40
+ __decorate([
41
+ (0, decorator_1.Init)(),
42
+ __metadata("design:type", Function),
43
+ __metadata("design:paramtypes", []),
44
+ __metadata("design:returntype", Promise)
45
+ ], HttpServiceFactory.prototype, "init", null);
46
+ HttpServiceFactory = __decorate([
47
+ (0, decorator_1.Provide)(),
48
+ (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
49
+ ], HttpServiceFactory);
50
+ exports.HttpServiceFactory = HttpServiceFactory;
16
51
  let HttpService = class HttpService {
17
52
  get interceptors() {
18
53
  return this.instance.interceptors;
19
54
  }
20
55
  async init() {
21
- var _a;
22
- if (!this.app.getApplicationContext().registry.hasObject(interface_1.AXIOS_INSTANCE_KEY)) {
23
- // 动态往全局容器中创建一个单例
24
- const instance = axios_1.default.create((_a = this.httpConfig) !== null && _a !== void 0 ? _a : {});
25
- // Add a request interceptor
26
- instance.interceptors.request.use(config => {
27
- // Do something before request is sent
28
- return config;
29
- }, error => {
30
- // Do something with request error
31
- return Promise.reject(error);
32
- });
33
- // Add a response interceptor
34
- instance.interceptors.response.use(response => {
35
- // Any status code that lie within the range of 2xx cause this function to trigger
36
- // Do something with response data
37
- return response;
38
- }, error => {
39
- // Any status codes that falls outside the range of 2xx cause this function to trigger
40
- // Do something with response error
41
- return Promise.reject(error);
42
- });
43
- this.app
44
- .getApplicationContext()
45
- .registry.registerObject(interface_1.AXIOS_INSTANCE_KEY, instance);
46
- }
47
- this.instance = this.app
48
- .getApplicationContext()
49
- .registry.getObject(interface_1.AXIOS_INSTANCE_KEY);
56
+ this.instance = this.serviceFactory.get('default');
50
57
  }
51
58
  getUri(config) {
52
59
  return this.instance.getUri(config);
@@ -78,16 +85,8 @@ let HttpService = class HttpService {
78
85
  };
79
86
  __decorate([
80
87
  (0, decorator_1.Inject)(),
81
- __metadata("design:type", Object)
82
- ], HttpService.prototype, "ctx", void 0);
83
- __decorate([
84
- (0, decorator_1.App)(),
85
- __metadata("design:type", Object)
86
- ], HttpService.prototype, "app", void 0);
87
- __decorate([
88
- (0, decorator_1.Config)('axios'),
89
- __metadata("design:type", Object)
90
- ], HttpService.prototype, "httpConfig", void 0);
88
+ __metadata("design:type", HttpServiceFactory)
89
+ ], HttpService.prototype, "serviceFactory", void 0);
91
90
  __decorate([
92
91
  (0, decorator_1.Init)(),
93
92
  __metadata("design:type", Function),
@@ -95,7 +94,8 @@ __decorate([
95
94
  __metadata("design:returntype", Promise)
96
95
  ], HttpService.prototype, "init", null);
97
96
  HttpService = __decorate([
98
- (0, decorator_1.Provide)()
97
+ (0, decorator_1.Provide)(),
98
+ (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
99
99
  ], HttpService);
100
100
  exports.HttpService = HttpService;
101
101
  //# sourceMappingURL=serviceManager.js.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@midwayjs/axios",
3
3
  "description": "midway http client with axios",
4
- "version": "3.3.4",
4
+ "version": "3.4.0-beta.10",
5
5
  "main": "dist/index",
6
6
  "typings": "index.d.ts",
7
7
  "files": [
@@ -10,12 +10,12 @@
10
10
  "index.d.ts"
11
11
  ],
12
12
  "devDependencies": {
13
- "@midwayjs/core": "^3.3.4",
14
- "@midwayjs/decorator": "^3.3.4",
15
- "@midwayjs/mock": "^3.3.4"
13
+ "@midwayjs/core": "^3.4.0-beta.10",
14
+ "@midwayjs/decorator": "^3.4.0-beta.10",
15
+ "@midwayjs/mock": "^3.4.0-beta.10"
16
16
  },
17
17
  "dependencies": {
18
- "axios": "^0.26.0"
18
+ "axios": "0.27.2"
19
19
  },
20
20
  "keywords": [
21
21
  "http client",
@@ -37,5 +37,5 @@
37
37
  "type": "git",
38
38
  "url": "https://github.com/midwayjs/midway.git"
39
39
  },
40
- "gitHead": "0c2785a87217f57a184a661c71b1d9562af02242"
40
+ "gitHead": "560a5fa311605e4008c55f31c31bc4e12eeff2a5"
41
41
  }