@midwayjs/axios 4.0.0-beta.11 → 4.0.0-beta.13
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.
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
import { ServiceFactory } from '@midwayjs/core';
|
|
1
|
+
import { MidwayTraceService, ServiceFactory } from '@midwayjs/core';
|
|
2
2
|
import { AxiosInstance, CreateAxiosDefaults } from 'axios';
|
|
3
3
|
export declare class HttpServiceFactory extends ServiceFactory<AxiosInstance> {
|
|
4
4
|
axiosConfig: any;
|
|
5
|
+
traceEnabled: boolean;
|
|
6
|
+
traceInjector: (args: {
|
|
7
|
+
request?: unknown;
|
|
8
|
+
custom?: Record<string, unknown>;
|
|
9
|
+
}) => any;
|
|
10
|
+
traceService: MidwayTraceService;
|
|
5
11
|
init(): Promise<void>;
|
|
6
12
|
getName(): string;
|
|
7
13
|
protected createClient(config: CreateAxiosDefaults, clientName: string): Promise<AxiosInstance>;
|
|
@@ -14,6 +14,9 @@ const core_1 = require("@midwayjs/core");
|
|
|
14
14
|
const axios = require("axios");
|
|
15
15
|
let HttpServiceFactory = class HttpServiceFactory extends core_1.ServiceFactory {
|
|
16
16
|
axiosConfig;
|
|
17
|
+
traceEnabled;
|
|
18
|
+
traceInjector;
|
|
19
|
+
traceService;
|
|
17
20
|
async init() {
|
|
18
21
|
let axiosConfig = this.axiosConfig;
|
|
19
22
|
if (!this.axiosConfig['clients']) {
|
|
@@ -30,7 +33,26 @@ let HttpServiceFactory = class HttpServiceFactory extends core_1.ServiceFactory
|
|
|
30
33
|
return 'axios';
|
|
31
34
|
}
|
|
32
35
|
async createClient(config, clientName) {
|
|
33
|
-
|
|
36
|
+
const client = axios.create(config);
|
|
37
|
+
client.interceptors.request.use(requestConfig => {
|
|
38
|
+
if (this.traceService && this.traceEnabled !== false) {
|
|
39
|
+
const configuredCarrier = typeof this.traceInjector === 'function'
|
|
40
|
+
? this.traceInjector({
|
|
41
|
+
request: requestConfig,
|
|
42
|
+
custom: {
|
|
43
|
+
clientName,
|
|
44
|
+
},
|
|
45
|
+
})
|
|
46
|
+
: undefined;
|
|
47
|
+
const carrier = configuredCarrier ??
|
|
48
|
+
requestConfig.headers ??
|
|
49
|
+
new axios.AxiosHeaders();
|
|
50
|
+
requestConfig.headers = carrier;
|
|
51
|
+
this.traceService.injectContext(carrier);
|
|
52
|
+
}
|
|
53
|
+
return requestConfig;
|
|
54
|
+
});
|
|
55
|
+
return client;
|
|
34
56
|
}
|
|
35
57
|
};
|
|
36
58
|
exports.HttpServiceFactory = HttpServiceFactory;
|
|
@@ -38,6 +60,18 @@ __decorate([
|
|
|
38
60
|
(0, core_1.Config)('axios'),
|
|
39
61
|
__metadata("design:type", Object)
|
|
40
62
|
], HttpServiceFactory.prototype, "axiosConfig", void 0);
|
|
63
|
+
__decorate([
|
|
64
|
+
(0, core_1.Config)('axios.tracing.enable'),
|
|
65
|
+
__metadata("design:type", Boolean)
|
|
66
|
+
], HttpServiceFactory.prototype, "traceEnabled", void 0);
|
|
67
|
+
__decorate([
|
|
68
|
+
(0, core_1.Config)('axios.tracing.injector'),
|
|
69
|
+
__metadata("design:type", Function)
|
|
70
|
+
], HttpServiceFactory.prototype, "traceInjector", void 0);
|
|
71
|
+
__decorate([
|
|
72
|
+
(0, core_1.Inject)(),
|
|
73
|
+
__metadata("design:type", core_1.MidwayTraceService)
|
|
74
|
+
], HttpServiceFactory.prototype, "traceService", void 0);
|
|
41
75
|
__decorate([
|
|
42
76
|
(0, core_1.Init)(),
|
|
43
77
|
__metadata("design:type", Function),
|
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": "4.0.0-beta.
|
|
4
|
+
"version": "4.0.0-beta.13",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
"index.d.ts"
|
|
11
11
|
],
|
|
12
12
|
"devDependencies": {
|
|
13
|
-
"@midwayjs/core": "^4.0.0-beta.
|
|
14
|
-
"@midwayjs/mock": "^4.0.0-beta.
|
|
13
|
+
"@midwayjs/core": "^4.0.0-beta.13",
|
|
14
|
+
"@midwayjs/mock": "^4.0.0-beta.13",
|
|
15
15
|
"nock": "13.5.6"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
@@ -37,5 +37,5 @@
|
|
|
37
37
|
"type": "git",
|
|
38
38
|
"url": "https://github.com/midwayjs/midway.git"
|
|
39
39
|
},
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "9a38b66a84a6880370cac90d737f94f9c5f2f256"
|
|
41
41
|
}
|