@@ -2,6 +2,53 @@ import URLSearchParams from '@ungap/url-search-params';
22import { IResponse , IResponseData } from '../../types' ;
33import logger from '../../utils/logger' ;
44
5+ const logArmsForMultiApi = ( { targetUrl, traceId, startTime, duration, success, code, msg} , response : IResponse < IResponseData > ) => {
6+ let api = targetUrl ;
7+ const { config, data } = response ;
8+ const { data : dataStr } = config ;
9+ const requestData = new URLSearchParams ( dataStr ) ;
10+ try {
11+ const actions : { action : string } [ ] = JSON . parse (
12+ requestData . get ( 'actions' ) || '[]'
13+ ) ;
14+ let apiIdentifier = '' ;
15+ const actionSet : any = { } ;
16+ for ( const item of actions ) {
17+ if ( ! actionSet [ item . action ] ) {
18+ actionSet [ item . action ] = 1 ;
19+ }
20+ }
21+ apiIdentifier = Object . keys ( actionSet ) . join ( ',' ) ;
22+ api = `${ targetUrl } ?product=${ requestData . get (
23+ 'product'
24+ ) } &action=${ apiIdentifier } `;
25+ } catch ( err ) {
26+ api = targetUrl ;
27+ }
28+
29+ // 如果是 mutiapi 一起失败了, 直接上报
30+ if ( ! success ) {
31+ logger ( { api, success, duration, code, msg, startTime, traceId } ) ;
32+ } else {
33+ const responseData = ( data ?. data || { } ) ;
34+ Object . keys ( responseData ) . forEach ( ( key ) => {
35+ const respForSingleApi = responseData [ key ] ;
36+ if ( respForSingleApi . Code && respForSingleApi . Code !== '200' ) {
37+ const apiWithIdentifier = `${ api } &identifier=${ key } &requestId=${ respForSingleApi . RequestId } ` ;
38+ console . log ( {
39+ api : apiWithIdentifier , success : false , duration, code : respForSingleApi . Code ,
40+ msg : respForSingleApi ?. Message , startTime, traceId
41+ } )
42+ logger ( {
43+ api : apiWithIdentifier , success : false , duration, code : respForSingleApi . Code ,
44+ msg : respForSingleApi ?. Message , startTime, traceId
45+ } ) ;
46+ }
47+ } )
48+ }
49+ }
50+
51+
552function armsResponseInterceptor (
653 response : IResponse < IResponseData >
754) : IResponse < IResponseData > {
@@ -40,40 +87,30 @@ function armsResponseInterceptor(
4087 const [ targetUrl , targetApiType ] = matches ;
4188 let api = targetUrl ;
4289 if ( targetApiType . includes ( 'multi' ) ) {
43- try {
44- const actions : { action : string } [ ] = JSON . parse (
45- requestData . get ( 'actions' ) || '[]'
46- ) ;
47- let apiIdentifier = '' ;
48- const actionSet : any = { } ;
49- for ( const item of actions ) {
50- if ( ! actionSet [ item . action ] ) {
51- actionSet [ item . action ] = 1 ;
52- }
53- }
54- apiIdentifier = Object . keys ( actionSet ) . join ( ',' ) ;
55- api = `${ targetUrl } ?product=${ requestData . get (
56- 'product'
57- ) } &action=${ apiIdentifier } `;
58- } catch ( err ) {
59- api = targetUrl ;
60- }
90+ logArmsForMultiApi ( {
91+ targetUrl,
92+ success : isSuccess ,
93+ duration,
94+ code : apiCode ,
95+ msg,
96+ startTime,
97+ traceId,
98+ } , response )
6199 } else {
62100 api = `${ targetUrl } ?product=${ requestData . get (
63101 'product'
64102 ) } &action=${ requestData . get ( 'action' ) } `;
103+ logger ( {
104+ api,
105+ success : isSuccess ,
106+ duration,
107+ code : apiCode ,
108+ msg,
109+ startTime,
110+ traceId,
111+ } ) ;
65112 }
66113
67- logger ( {
68- api,
69- success : isSuccess ,
70- duration,
71- code : apiCode ,
72- msg,
73- startTime,
74- traceId,
75- } ) ;
76-
77114 return response ;
78115}
79116
0 commit comments