@xfe-repo/bff-micro 1.6.2 → 1.6.3

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.
@@ -5,8 +5,9 @@ import {
5
5
  } from "./chunk-F5EANDNT.mjs";
6
6
 
7
7
  // src/micro.interceptor.ts
8
- import { Injectable, Logger } from "@nestjs/common";
8
+ import { Injectable } from "@nestjs/common";
9
9
  import { CtxService } from "@xfe-repo/bff-core";
10
+ import { ApiResponseException } from "@xfe-repo/bff-validation";
10
11
  function _ts_decorate(decorators, target, key, desc) {
11
12
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
12
13
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -22,7 +23,6 @@ var _MicroInterceptor = class _MicroInterceptor {
22
23
  constructor(microService, ctx) {
23
24
  __publicField(this, "microService");
24
25
  __publicField(this, "ctx");
25
- __publicField(this, "logger", new Logger(_MicroInterceptor.name));
26
26
  this.microService = microService;
27
27
  this.ctx = ctx;
28
28
  }
@@ -33,10 +33,8 @@ var _MicroInterceptor = class _MicroInterceptor {
33
33
  this.registerInterceptors();
34
34
  }
35
35
  registerInterceptors() {
36
- const requestManager = this.axiosRef.interceptors.request;
37
- const responseManager = this.axiosRef.interceptors.response;
38
- requestManager.use(this.requestFulfilled(), this.requestRejected());
39
- responseManager.use(this.responseFulfilled(), this.responseRejected());
36
+ this.axiosRef.interceptors.request.use(this.requestFulfilled(), this.requestRejected());
37
+ this.axiosRef.interceptors.response.use(this.responseFulfilled(), this.responseRejected());
40
38
  }
41
39
  requestFulfilled() {
42
40
  return (config) => {
@@ -45,24 +43,51 @@ var _MicroInterceptor = class _MicroInterceptor {
45
43
  const dataURL = dataUrlBuilder(url)(replaceParams ? params : data);
46
44
  replaceParams ? config.params = dataURL.data : config.data = dataURL.data;
47
45
  config.url = dataURL.url;
48
- this.logger.log(`Micro Request: ${this.ctx.traceid} ${config.timeout} ${config.method?.toUpperCase()}] ${config.baseURL}${config.url} - Headers:${JSON.stringify(config.headers)}`);
46
+ const microConfig = config;
47
+ microConfig.metadata = {
48
+ startTime: Date.now()
49
+ };
49
50
  return config;
50
51
  };
51
52
  }
52
53
  requestRejected() {
53
54
  return (error) => {
54
- this.logger.error(`Micro Request Error: ${this.ctx.traceid} ${error}, ${JSON.stringify(error)}`);
55
+ const config = error.config;
56
+ this.ctx.setTrack(buildTrack({
57
+ config,
58
+ duration: getDuration(config),
59
+ error
60
+ }));
61
+ return Promise.reject(error);
55
62
  };
56
63
  }
57
64
  responseFulfilled() {
58
65
  return (response) => {
59
- this.logger.log(`Micro Response: ${this.ctx.traceid} ${response.config.url} ${JSON.stringify(response.data)}`);
60
- return response.data;
66
+ const config = response.config;
67
+ const data = response.data;
68
+ const duration = getDuration(config);
69
+ const track = buildTrack({
70
+ config,
71
+ duration,
72
+ statusCode: response.status,
73
+ responseData: response.data
74
+ });
75
+ this.ctx.setTrack(track);
76
+ if (data?.code === 200) return response.data;
77
+ throw new ApiResponseException(response.data);
61
78
  };
62
79
  }
63
80
  responseRejected() {
64
81
  return (error) => {
65
- this.logger.error(`Micro Response Error: ${this.ctx.traceid} ${error}, ${JSON.stringify(error)}`);
82
+ const config = error.config;
83
+ const duration = getDuration(config);
84
+ this.ctx.setTrack(buildTrack({
85
+ config,
86
+ duration,
87
+ statusCode: error.response?.status,
88
+ error
89
+ }));
90
+ return Promise.reject(error);
66
91
  };
67
92
  }
68
93
  };
@@ -76,6 +101,10 @@ MicroInterceptor = _ts_decorate([
76
101
  typeof CtxService === "undefined" ? Object : CtxService
77
102
  ])
78
103
  ], MicroInterceptor);
104
+ function getDuration(config) {
105
+ return config?.metadata ? Date.now() - config.metadata.startTime : void 0;
106
+ }
107
+ __name(getDuration, "getDuration");
79
108
  var dataUrlBuilder = /* @__PURE__ */ __name((originURL) => {
80
109
  return (params) => {
81
110
  if (!originURL.includes(":")) {
@@ -96,6 +125,26 @@ var dataUrlBuilder = /* @__PURE__ */ __name((originURL) => {
96
125
  };
97
126
  };
98
127
  }, "dataUrlBuilder");
128
+ function buildTrack(options) {
129
+ const { config, duration, statusCode, responseData, error } = options;
130
+ const fullURL = `${config?.baseURL || ""}${config?.url || ""}`;
131
+ return {
132
+ type: "UpstreamAccessLog",
133
+ datetime: (/* @__PURE__ */ new Date()).toISOString(),
134
+ method: config?.method?.toUpperCase(),
135
+ reqURL: fullURL,
136
+ query: config?.params,
137
+ body: config?.data,
138
+ statusCode,
139
+ duration,
140
+ response: responseData,
141
+ error: error && {
142
+ message: error.message,
143
+ code: error.code
144
+ }
145
+ };
146
+ }
147
+ __name(buildTrack, "buildTrack");
99
148
 
100
149
  export {
101
150
  MicroInterceptor
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  MicroInterceptor
3
- } from "./chunk-R2NQNPXU.mjs";
3
+ } from "./chunk-ESZSUOJ5.mjs";
4
4
  import {
5
5
  MicroService,
6
6
  __name
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { MicroInterceptor } from './micro.interceptor.mjs';
2
2
  export { MicroModule } from './micro.module.mjs';
3
- export { MicrAxiosInstance, MicroRequest, MicroRequestConfig, MicroService } from './micro.service.mjs';
3
+ export { MicrAxiosInstance, MicroRequest, MicroRequestConfig, MicroResponseType, MicroService } from './micro.service.mjs';
4
4
  export * from 'axios';
5
5
  import '@nestjs/common';
6
6
  import '@xfe-repo/bff-core';
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { MicroInterceptor } from './micro.interceptor.js';
2
2
  export { MicroModule } from './micro.module.js';
3
- export { MicrAxiosInstance, MicroRequest, MicroRequestConfig, MicroService } from './micro.service.js';
3
+ export { MicrAxiosInstance, MicroRequest, MicroRequestConfig, MicroResponseType, MicroService } from './micro.service.js';
4
4
  export * from 'axios';
5
5
  import '@nestjs/common';
6
6
  import '@xfe-repo/bff-core';
package/dist/index.js CHANGED
@@ -33,6 +33,7 @@ module.exports = __toCommonJS(index_exports);
33
33
  // src/micro.interceptor.ts
34
34
  var import_common2 = require("@nestjs/common");
35
35
  var import_bff_core = require("@xfe-repo/bff-core");
36
+ var import_bff_validation = require("@xfe-repo/bff-validation");
36
37
 
37
38
  // src/micro.service.ts
38
39
  var import_axios = require("@nestjs/axios");
@@ -86,7 +87,6 @@ var _MicroInterceptor = class _MicroInterceptor {
86
87
  constructor(microService, ctx) {
87
88
  __publicField(this, "microService");
88
89
  __publicField(this, "ctx");
89
- __publicField(this, "logger", new import_common2.Logger(_MicroInterceptor.name));
90
90
  this.microService = microService;
91
91
  this.ctx = ctx;
92
92
  }
@@ -97,10 +97,8 @@ var _MicroInterceptor = class _MicroInterceptor {
97
97
  this.registerInterceptors();
98
98
  }
99
99
  registerInterceptors() {
100
- const requestManager = this.axiosRef.interceptors.request;
101
- const responseManager = this.axiosRef.interceptors.response;
102
- requestManager.use(this.requestFulfilled(), this.requestRejected());
103
- responseManager.use(this.responseFulfilled(), this.responseRejected());
100
+ this.axiosRef.interceptors.request.use(this.requestFulfilled(), this.requestRejected());
101
+ this.axiosRef.interceptors.response.use(this.responseFulfilled(), this.responseRejected());
104
102
  }
105
103
  requestFulfilled() {
106
104
  return (config) => {
@@ -109,24 +107,51 @@ var _MicroInterceptor = class _MicroInterceptor {
109
107
  const dataURL = dataUrlBuilder(url)(replaceParams ? params : data);
110
108
  replaceParams ? config.params = dataURL.data : config.data = dataURL.data;
111
109
  config.url = dataURL.url;
112
- this.logger.log(`Micro Request: ${this.ctx.traceid} ${config.timeout} ${config.method?.toUpperCase()}] ${config.baseURL}${config.url} - Headers:${JSON.stringify(config.headers)}`);
110
+ const microConfig = config;
111
+ microConfig.metadata = {
112
+ startTime: Date.now()
113
+ };
113
114
  return config;
114
115
  };
115
116
  }
116
117
  requestRejected() {
117
118
  return (error) => {
118
- this.logger.error(`Micro Request Error: ${this.ctx.traceid} ${error}, ${JSON.stringify(error)}`);
119
+ const config = error.config;
120
+ this.ctx.setTrack(buildTrack({
121
+ config,
122
+ duration: getDuration(config),
123
+ error
124
+ }));
125
+ return Promise.reject(error);
119
126
  };
120
127
  }
121
128
  responseFulfilled() {
122
129
  return (response) => {
123
- this.logger.log(`Micro Response: ${this.ctx.traceid} ${response.config.url} ${JSON.stringify(response.data)}`);
124
- return response.data;
130
+ const config = response.config;
131
+ const data = response.data;
132
+ const duration = getDuration(config);
133
+ const track = buildTrack({
134
+ config,
135
+ duration,
136
+ statusCode: response.status,
137
+ responseData: response.data
138
+ });
139
+ this.ctx.setTrack(track);
140
+ if (data?.code === 200) return response.data;
141
+ throw new import_bff_validation.ApiResponseException(response.data);
125
142
  };
126
143
  }
127
144
  responseRejected() {
128
145
  return (error) => {
129
- this.logger.error(`Micro Response Error: ${this.ctx.traceid} ${error}, ${JSON.stringify(error)}`);
146
+ const config = error.config;
147
+ const duration = getDuration(config);
148
+ this.ctx.setTrack(buildTrack({
149
+ config,
150
+ duration,
151
+ statusCode: error.response?.status,
152
+ error
153
+ }));
154
+ return Promise.reject(error);
130
155
  };
131
156
  }
132
157
  };
@@ -140,6 +165,10 @@ MicroInterceptor = _ts_decorate2([
140
165
  typeof import_bff_core.CtxService === "undefined" ? Object : import_bff_core.CtxService
141
166
  ])
142
167
  ], MicroInterceptor);
168
+ function getDuration(config) {
169
+ return config?.metadata ? Date.now() - config.metadata.startTime : void 0;
170
+ }
171
+ __name(getDuration, "getDuration");
143
172
  var dataUrlBuilder = /* @__PURE__ */ __name((originURL) => {
144
173
  return (params) => {
145
174
  if (!originURL.includes(":")) {
@@ -160,6 +189,26 @@ var dataUrlBuilder = /* @__PURE__ */ __name((originURL) => {
160
189
  };
161
190
  };
162
191
  }, "dataUrlBuilder");
192
+ function buildTrack(options) {
193
+ const { config, duration, statusCode, responseData, error } = options;
194
+ const fullURL = `${config?.baseURL || ""}${config?.url || ""}`;
195
+ return {
196
+ type: "UpstreamAccessLog",
197
+ datetime: (/* @__PURE__ */ new Date()).toISOString(),
198
+ method: config?.method?.toUpperCase(),
199
+ reqURL: fullURL,
200
+ query: config?.params,
201
+ body: config?.data,
202
+ statusCode,
203
+ duration,
204
+ response: responseData,
205
+ error: error && {
206
+ message: error.message,
207
+ code: error.code
208
+ }
209
+ };
210
+ }
211
+ __name(buildTrack, "buildTrack");
163
212
 
164
213
  // src/micro.module.ts
165
214
  var import_axios2 = require("@nestjs/axios");
package/dist/index.mjs CHANGED
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  MicroModule
3
- } from "./chunk-ZAMMXFEP.mjs";
3
+ } from "./chunk-FJLXKRSL.mjs";
4
4
  import {
5
5
  MicroInterceptor
6
- } from "./chunk-R2NQNPXU.mjs";
6
+ } from "./chunk-ESZSUOJ5.mjs";
7
7
  import {
8
8
  MicroService
9
9
  } from "./chunk-F5EANDNT.mjs";
@@ -1,21 +1,20 @@
1
1
  import { OnModuleInit } from '@nestjs/common';
2
2
  import { CtxService } from '@xfe-repo/bff-core';
3
- import { InternalAxiosRequestConfig, AxiosResponse } from 'axios';
3
+ import { InternalAxiosRequestConfig, AxiosError, AxiosResponse } from 'axios';
4
4
  import { MicroService } from './micro.service.mjs';
5
5
  import '@nestjs/axios';
6
6
 
7
7
  declare class MicroInterceptor implements OnModuleInit {
8
8
  private readonly microService;
9
9
  private readonly ctx;
10
- private readonly logger;
11
10
  constructor(microService: MicroService, ctx: CtxService);
12
11
  private get axiosRef();
13
12
  onModuleInit(): void;
14
13
  private registerInterceptors;
15
14
  requestFulfilled(): (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig<any>;
16
- requestRejected(): (error: any) => void;
15
+ requestRejected(): (error: AxiosError) => Promise<never>;
17
16
  responseFulfilled(): (response: AxiosResponse) => any;
18
- responseRejected(): (error: any) => void;
17
+ responseRejected(): (error: AxiosError) => Promise<never>;
19
18
  }
20
19
 
21
20
  export { MicroInterceptor };
@@ -1,21 +1,20 @@
1
1
  import { OnModuleInit } from '@nestjs/common';
2
2
  import { CtxService } from '@xfe-repo/bff-core';
3
- import { InternalAxiosRequestConfig, AxiosResponse } from 'axios';
3
+ import { InternalAxiosRequestConfig, AxiosError, AxiosResponse } from 'axios';
4
4
  import { MicroService } from './micro.service.js';
5
5
  import '@nestjs/axios';
6
6
 
7
7
  declare class MicroInterceptor implements OnModuleInit {
8
8
  private readonly microService;
9
9
  private readonly ctx;
10
- private readonly logger;
11
10
  constructor(microService: MicroService, ctx: CtxService);
12
11
  private get axiosRef();
13
12
  onModuleInit(): void;
14
13
  private registerInterceptors;
15
14
  requestFulfilled(): (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig<any>;
16
- requestRejected(): (error: any) => void;
15
+ requestRejected(): (error: AxiosError) => Promise<never>;
17
16
  responseFulfilled(): (response: AxiosResponse) => any;
18
- responseRejected(): (error: any) => void;
17
+ responseRejected(): (error: AxiosError) => Promise<never>;
19
18
  }
20
19
 
21
20
  export { MicroInterceptor };
@@ -28,6 +28,7 @@ __export(micro_interceptor_exports, {
28
28
  module.exports = __toCommonJS(micro_interceptor_exports);
29
29
  var import_common2 = require("@nestjs/common");
30
30
  var import_bff_core = require("@xfe-repo/bff-core");
31
+ var import_bff_validation = require("@xfe-repo/bff-validation");
31
32
 
32
33
  // src/micro.service.ts
33
34
  var import_axios = require("@nestjs/axios");
@@ -81,7 +82,6 @@ var _MicroInterceptor = class _MicroInterceptor {
81
82
  constructor(microService, ctx) {
82
83
  __publicField(this, "microService");
83
84
  __publicField(this, "ctx");
84
- __publicField(this, "logger", new import_common2.Logger(_MicroInterceptor.name));
85
85
  this.microService = microService;
86
86
  this.ctx = ctx;
87
87
  }
@@ -92,10 +92,8 @@ var _MicroInterceptor = class _MicroInterceptor {
92
92
  this.registerInterceptors();
93
93
  }
94
94
  registerInterceptors() {
95
- const requestManager = this.axiosRef.interceptors.request;
96
- const responseManager = this.axiosRef.interceptors.response;
97
- requestManager.use(this.requestFulfilled(), this.requestRejected());
98
- responseManager.use(this.responseFulfilled(), this.responseRejected());
95
+ this.axiosRef.interceptors.request.use(this.requestFulfilled(), this.requestRejected());
96
+ this.axiosRef.interceptors.response.use(this.responseFulfilled(), this.responseRejected());
99
97
  }
100
98
  requestFulfilled() {
101
99
  return (config) => {
@@ -104,24 +102,51 @@ var _MicroInterceptor = class _MicroInterceptor {
104
102
  const dataURL = dataUrlBuilder(url)(replaceParams ? params : data);
105
103
  replaceParams ? config.params = dataURL.data : config.data = dataURL.data;
106
104
  config.url = dataURL.url;
107
- this.logger.log(`Micro Request: ${this.ctx.traceid} ${config.timeout} ${config.method?.toUpperCase()}] ${config.baseURL}${config.url} - Headers:${JSON.stringify(config.headers)}`);
105
+ const microConfig = config;
106
+ microConfig.metadata = {
107
+ startTime: Date.now()
108
+ };
108
109
  return config;
109
110
  };
110
111
  }
111
112
  requestRejected() {
112
113
  return (error) => {
113
- this.logger.error(`Micro Request Error: ${this.ctx.traceid} ${error}, ${JSON.stringify(error)}`);
114
+ const config = error.config;
115
+ this.ctx.setTrack(buildTrack({
116
+ config,
117
+ duration: getDuration(config),
118
+ error
119
+ }));
120
+ return Promise.reject(error);
114
121
  };
115
122
  }
116
123
  responseFulfilled() {
117
124
  return (response) => {
118
- this.logger.log(`Micro Response: ${this.ctx.traceid} ${response.config.url} ${JSON.stringify(response.data)}`);
119
- return response.data;
125
+ const config = response.config;
126
+ const data = response.data;
127
+ const duration = getDuration(config);
128
+ const track = buildTrack({
129
+ config,
130
+ duration,
131
+ statusCode: response.status,
132
+ responseData: response.data
133
+ });
134
+ this.ctx.setTrack(track);
135
+ if (data?.code === 200) return response.data;
136
+ throw new import_bff_validation.ApiResponseException(response.data);
120
137
  };
121
138
  }
122
139
  responseRejected() {
123
140
  return (error) => {
124
- this.logger.error(`Micro Response Error: ${this.ctx.traceid} ${error}, ${JSON.stringify(error)}`);
141
+ const config = error.config;
142
+ const duration = getDuration(config);
143
+ this.ctx.setTrack(buildTrack({
144
+ config,
145
+ duration,
146
+ statusCode: error.response?.status,
147
+ error
148
+ }));
149
+ return Promise.reject(error);
125
150
  };
126
151
  }
127
152
  };
@@ -135,6 +160,10 @@ MicroInterceptor = _ts_decorate2([
135
160
  typeof import_bff_core.CtxService === "undefined" ? Object : import_bff_core.CtxService
136
161
  ])
137
162
  ], MicroInterceptor);
163
+ function getDuration(config) {
164
+ return config?.metadata ? Date.now() - config.metadata.startTime : void 0;
165
+ }
166
+ __name(getDuration, "getDuration");
138
167
  var dataUrlBuilder = /* @__PURE__ */ __name((originURL) => {
139
168
  return (params) => {
140
169
  if (!originURL.includes(":")) {
@@ -155,6 +184,26 @@ var dataUrlBuilder = /* @__PURE__ */ __name((originURL) => {
155
184
  };
156
185
  };
157
186
  }, "dataUrlBuilder");
187
+ function buildTrack(options) {
188
+ const { config, duration, statusCode, responseData, error } = options;
189
+ const fullURL = `${config?.baseURL || ""}${config?.url || ""}`;
190
+ return {
191
+ type: "UpstreamAccessLog",
192
+ datetime: (/* @__PURE__ */ new Date()).toISOString(),
193
+ method: config?.method?.toUpperCase(),
194
+ reqURL: fullURL,
195
+ query: config?.params,
196
+ body: config?.data,
197
+ statusCode,
198
+ duration,
199
+ response: responseData,
200
+ error: error && {
201
+ message: error.message,
202
+ code: error.code
203
+ }
204
+ };
205
+ }
206
+ __name(buildTrack, "buildTrack");
158
207
  // Annotate the CommonJS export names for ESM import in node:
159
208
  0 && (module.exports = {
160
209
  MicroInterceptor
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  MicroInterceptor
3
- } from "./chunk-R2NQNPXU.mjs";
3
+ } from "./chunk-ESZSUOJ5.mjs";
4
4
  import "./chunk-F5EANDNT.mjs";
5
5
  export {
6
6
  MicroInterceptor
@@ -32,6 +32,7 @@ var import_common3 = require("@nestjs/common");
32
32
  // src/micro.interceptor.ts
33
33
  var import_common2 = require("@nestjs/common");
34
34
  var import_bff_core = require("@xfe-repo/bff-core");
35
+ var import_bff_validation = require("@xfe-repo/bff-validation");
35
36
 
36
37
  // src/micro.service.ts
37
38
  var import_axios = require("@nestjs/axios");
@@ -85,7 +86,6 @@ var _MicroInterceptor = class _MicroInterceptor {
85
86
  constructor(microService, ctx) {
86
87
  __publicField(this, "microService");
87
88
  __publicField(this, "ctx");
88
- __publicField(this, "logger", new import_common2.Logger(_MicroInterceptor.name));
89
89
  this.microService = microService;
90
90
  this.ctx = ctx;
91
91
  }
@@ -96,10 +96,8 @@ var _MicroInterceptor = class _MicroInterceptor {
96
96
  this.registerInterceptors();
97
97
  }
98
98
  registerInterceptors() {
99
- const requestManager = this.axiosRef.interceptors.request;
100
- const responseManager = this.axiosRef.interceptors.response;
101
- requestManager.use(this.requestFulfilled(), this.requestRejected());
102
- responseManager.use(this.responseFulfilled(), this.responseRejected());
99
+ this.axiosRef.interceptors.request.use(this.requestFulfilled(), this.requestRejected());
100
+ this.axiosRef.interceptors.response.use(this.responseFulfilled(), this.responseRejected());
103
101
  }
104
102
  requestFulfilled() {
105
103
  return (config) => {
@@ -108,24 +106,51 @@ var _MicroInterceptor = class _MicroInterceptor {
108
106
  const dataURL = dataUrlBuilder(url)(replaceParams ? params : data);
109
107
  replaceParams ? config.params = dataURL.data : config.data = dataURL.data;
110
108
  config.url = dataURL.url;
111
- this.logger.log(`Micro Request: ${this.ctx.traceid} ${config.timeout} ${config.method?.toUpperCase()}] ${config.baseURL}${config.url} - Headers:${JSON.stringify(config.headers)}`);
109
+ const microConfig = config;
110
+ microConfig.metadata = {
111
+ startTime: Date.now()
112
+ };
112
113
  return config;
113
114
  };
114
115
  }
115
116
  requestRejected() {
116
117
  return (error) => {
117
- this.logger.error(`Micro Request Error: ${this.ctx.traceid} ${error}, ${JSON.stringify(error)}`);
118
+ const config = error.config;
119
+ this.ctx.setTrack(buildTrack({
120
+ config,
121
+ duration: getDuration(config),
122
+ error
123
+ }));
124
+ return Promise.reject(error);
118
125
  };
119
126
  }
120
127
  responseFulfilled() {
121
128
  return (response) => {
122
- this.logger.log(`Micro Response: ${this.ctx.traceid} ${response.config.url} ${JSON.stringify(response.data)}`);
123
- return response.data;
129
+ const config = response.config;
130
+ const data = response.data;
131
+ const duration = getDuration(config);
132
+ const track = buildTrack({
133
+ config,
134
+ duration,
135
+ statusCode: response.status,
136
+ responseData: response.data
137
+ });
138
+ this.ctx.setTrack(track);
139
+ if (data?.code === 200) return response.data;
140
+ throw new import_bff_validation.ApiResponseException(response.data);
124
141
  };
125
142
  }
126
143
  responseRejected() {
127
144
  return (error) => {
128
- this.logger.error(`Micro Response Error: ${this.ctx.traceid} ${error}, ${JSON.stringify(error)}`);
145
+ const config = error.config;
146
+ const duration = getDuration(config);
147
+ this.ctx.setTrack(buildTrack({
148
+ config,
149
+ duration,
150
+ statusCode: error.response?.status,
151
+ error
152
+ }));
153
+ return Promise.reject(error);
129
154
  };
130
155
  }
131
156
  };
@@ -139,6 +164,10 @@ MicroInterceptor = _ts_decorate2([
139
164
  typeof import_bff_core.CtxService === "undefined" ? Object : import_bff_core.CtxService
140
165
  ])
141
166
  ], MicroInterceptor);
167
+ function getDuration(config) {
168
+ return config?.metadata ? Date.now() - config.metadata.startTime : void 0;
169
+ }
170
+ __name(getDuration, "getDuration");
142
171
  var dataUrlBuilder = /* @__PURE__ */ __name((originURL) => {
143
172
  return (params) => {
144
173
  if (!originURL.includes(":")) {
@@ -159,6 +188,26 @@ var dataUrlBuilder = /* @__PURE__ */ __name((originURL) => {
159
188
  };
160
189
  };
161
190
  }, "dataUrlBuilder");
191
+ function buildTrack(options) {
192
+ const { config, duration, statusCode, responseData, error } = options;
193
+ const fullURL = `${config?.baseURL || ""}${config?.url || ""}`;
194
+ return {
195
+ type: "UpstreamAccessLog",
196
+ datetime: (/* @__PURE__ */ new Date()).toISOString(),
197
+ method: config?.method?.toUpperCase(),
198
+ reqURL: fullURL,
199
+ query: config?.params,
200
+ body: config?.data,
201
+ statusCode,
202
+ duration,
203
+ response: responseData,
204
+ error: error && {
205
+ message: error.message,
206
+ code: error.code
207
+ }
208
+ };
209
+ }
210
+ __name(buildTrack, "buildTrack");
162
211
 
163
212
  // src/micro.module.ts
164
213
  function _ts_decorate3(decorators, target, key, desc) {
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  MicroModule
3
- } from "./chunk-ZAMMXFEP.mjs";
4
- import "./chunk-R2NQNPXU.mjs";
3
+ } from "./chunk-FJLXKRSL.mjs";
4
+ import "./chunk-ESZSUOJ5.mjs";
5
5
  import "./chunk-F5EANDNT.mjs";
6
6
  export {
7
7
  MicroModule
@@ -2,7 +2,12 @@ import { HttpService } from '@nestjs/axios';
2
2
  import { AxiosInstance, AxiosRequestConfig } from 'axios';
3
3
 
4
4
  type MicroRequestConfig = AxiosRequestConfig;
5
- type MicroRequest<P = any, R = any> = P extends void ? (params: void, config?: MicroRequestConfig) => Promise<R> : (params: P, config?: MicroRequestConfig) => Promise<R>;
5
+ type MicroResponseType<T = any> = {
6
+ code: number;
7
+ msg: string;
8
+ data: T;
9
+ };
10
+ type MicroRequest<P = any, R = MicroResponseType> = P extends void ? (params: void, config?: MicroRequestConfig) => Promise<R> : (params: P, config?: MicroRequestConfig) => Promise<R>;
6
11
  interface MicrAxiosInstance extends AxiosInstance {
7
12
  request<P = any, R = any>(config: AxiosRequestConfig<P>): Promise<R>;
8
13
  }
@@ -13,4 +18,4 @@ declare class MicroService {
13
18
  request<P = any, R = any>(config: MicroRequestConfig): Promise<R>;
14
19
  }
15
20
 
16
- export { type MicrAxiosInstance, type MicroRequest, type MicroRequestConfig, MicroService };
21
+ export { type MicrAxiosInstance, type MicroRequest, type MicroRequestConfig, type MicroResponseType, MicroService };
@@ -2,7 +2,12 @@ import { HttpService } from '@nestjs/axios';
2
2
  import { AxiosInstance, AxiosRequestConfig } from 'axios';
3
3
 
4
4
  type MicroRequestConfig = AxiosRequestConfig;
5
- type MicroRequest<P = any, R = any> = P extends void ? (params: void, config?: MicroRequestConfig) => Promise<R> : (params: P, config?: MicroRequestConfig) => Promise<R>;
5
+ type MicroResponseType<T = any> = {
6
+ code: number;
7
+ msg: string;
8
+ data: T;
9
+ };
10
+ type MicroRequest<P = any, R = MicroResponseType> = P extends void ? (params: void, config?: MicroRequestConfig) => Promise<R> : (params: P, config?: MicroRequestConfig) => Promise<R>;
6
11
  interface MicrAxiosInstance extends AxiosInstance {
7
12
  request<P = any, R = any>(config: AxiosRequestConfig<P>): Promise<R>;
8
13
  }
@@ -13,4 +18,4 @@ declare class MicroService {
13
18
  request<P = any, R = any>(config: MicroRequestConfig): Promise<R>;
14
19
  }
15
20
 
16
- export { type MicrAxiosInstance, type MicroRequest, type MicroRequestConfig, MicroService };
21
+ export { type MicrAxiosInstance, type MicroRequest, type MicroRequestConfig, type MicroResponseType, MicroService };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xfe-repo/bff-micro",
3
- "version": "1.6.2",
3
+ "version": "1.6.3",
4
4
  "sideEffects": false,
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -23,6 +23,7 @@
23
23
  "@nestjs/axios": "^4.0.1",
24
24
  "@nestjs/common": "11.1.13",
25
25
  "axios": "^1.13.4",
26
+ "@xfe-repo/bff-validation": "1.6.2",
26
27
  "@xfe-repo/bff-core": "1.6.2"
27
28
  },
28
29
  "devDependencies": {
@@ -30,9 +31,9 @@
30
31
  "@types/node": "^20.17.0",
31
32
  "eslint": "8.57.1",
32
33
  "jest": "^30.2.0",
33
- "@xfe-repo/eslint-config": "1.6.0",
34
+ "@xfe-repo/typescript-config": "1.6.1",
34
35
  "@xfe-repo/jest-config": "1.6.0",
35
- "@xfe-repo/typescript-config": "1.6.0"
36
+ "@xfe-repo/eslint-config": "1.6.0"
36
37
  },
37
38
  "peerDependencies": {},
38
39
  "publishConfig": {