1
- import axios , { AxiosRequestConfig , Method } from 'axios' ;
2
1
import { DataSourceType } from '~/model' ;
3
2
import { AnyObject , IDashboardConfig } from '..' ;
4
- import { cryptSign } from './utils' ;
3
+ import { DefaultApiClient , IAPIClient } from '../shared' ;
4
+ import { Method } from 'axios' ;
5
5
6
+ export { FacadeApiClient , DefaultApiClient } from '../shared' ;
7
+ export type { IAPIClient , IAPIClientRequestOptions } from '../shared' ;
6
8
export type TQueryPayload = {
7
9
type : DataSourceType ;
8
10
key : string ;
9
11
query : string ;
10
12
env ?: AnyObject ;
11
13
} ;
12
14
13
- export interface IAPIClientRequestOptions {
14
- string ?: boolean ;
15
- params ?: AnyObject ;
16
- headers ?: AnyObject ;
17
- }
18
-
19
- export interface IAPIClient {
20
- getRequest : < T = $TSFixMe > (
21
- method : Method ,
22
- signal ?: AbortSignal ,
23
- ) => ( url : string , data : AnyObject , options ?: IAPIClientRequestOptions ) => Promise < T > ;
15
+ export interface IDashboardAPIClient extends IAPIClient {
24
16
query : < T = $TSFixMe > ( signal ?: AbortSignal ) => ( data : TQueryPayload , options ?: AnyObject ) => Promise < T > ;
25
17
}
26
18
27
- export class DefaultApiClient implements IAPIClient {
28
- baseURL : string ;
29
- app_id : string ;
30
- app_secret : string ;
31
- makeQueryENV : ( ( ) => AnyObject ) | null ;
32
-
33
- constructor ( ) {
34
- this . baseURL = 'http://localhost:31200' ;
35
- this . app_id = '' ;
36
- this . app_secret = '' ;
37
- this . makeQueryENV = null ;
38
- }
39
-
40
- getAuthentication ( params : Record < string , unknown > ) {
41
- if ( ! this . app_id || ! this . app_secret ) {
42
- return undefined ;
43
- }
44
- const nonce_str = new Date ( ) . getTime ( ) . toString ( ) ;
45
- return {
46
- app_id : this . app_id ,
47
- nonce_str,
48
- sign : cryptSign (
49
- {
50
- app_id : this . app_id ,
51
- nonce_str,
52
- ...params ,
53
- } ,
54
- this . app_secret ,
55
- ) ,
56
- } ;
57
- }
58
-
59
- getRequest ( method : Method , signal ?: AbortSignal ) {
60
- return ( url : string , data : AnyObject , options : IAPIClientRequestOptions = { } ) => {
61
- const headers = this . buildHeader ( options ) ;
62
- const conf = this . buildAxiosConfig ( method , url , data , options , headers , signal ) ;
19
+ export class DashboardApiClient extends DefaultApiClient implements IDashboardAPIClient {
20
+ makeQueryENV ?: ( ( ) => AnyObject ) | null = null ;
63
21
64
- return axios ( conf )
65
- . then ( ( res ) => {
66
- return res . data ;
67
- } )
68
- . catch ( ( err : Error ) => {
69
- return Promise . reject ( err ) ;
70
- } ) ;
22
+ query < T > ( signal : AbortSignal | undefined ) : ( data : TQueryPayload , options ?: AnyObject ) => Promise < T > {
23
+ return async ( data : TQueryPayload , options : AnyObject = { } ) => {
24
+ if ( ! data . env ) {
25
+ data . env = this . makeQueryENV ?.( ) ?? { error : 'failed to run makeQueryENV' } ;
26
+ }
27
+ return this . getRequest < T > ( 'POST' , signal ) ( '/query' , data , options ) ;
71
28
} ;
72
29
}
30
+ }
73
31
74
- buildAxiosConfig (
75
- method : Method ,
76
- url : string ,
77
- data : AnyObject ,
78
- options : IAPIClientRequestOptions ,
79
- headers : AnyObject ,
80
- signal : AbortSignal | undefined ,
81
- ) {
82
- const conf : AxiosRequestConfig = {
83
- baseURL : this . baseURL ,
84
- method,
85
- url,
86
- params : method === 'GET' ? data : options . params ,
87
- headers,
88
- signal,
89
- } ;
32
+ export class DashboardApiFacadeClient implements IDashboardAPIClient {
33
+ constructor ( public implementation : IDashboardAPIClient ) { }
90
34
91
- if ( [ 'POST' , 'PUT' ] . includes ( method ) ) {
92
- conf . data = options . string ? JSON . stringify ( data ) : data ;
93
- conf . data . authentication = this . getAuthentication ( conf . data ) ;
94
- }
95
- return conf ;
35
+ query < T > ( signal ?: AbortSignal ) {
36
+ return this . implementation . query < T > ( signal ) ;
96
37
}
97
38
98
- buildHeader ( options : IAPIClientRequestOptions ) : AnyObject {
99
- const token = window . localStorage . getItem ( 'token' ) ;
100
- return {
101
- 'X-Requested-With' : 'XMLHttpRequest' ,
102
- 'Content-Type' : options . string ? 'application/x-www-form-urlencoded' : 'application/json' ,
103
- authorization : token ? `bearer ${ token } ` : '' ,
104
- ...options . headers ,
105
- } ;
106
- }
107
-
108
- query ( signal ?: AbortSignal ) {
109
- return async ( data : TQueryPayload , options : AnyObject = { } ) => {
110
- if ( ! data . env ) {
111
- data . env = this . makeQueryENV ?.( ) ?? { error : 'failed to run makeQueryENV' } ;
112
- }
113
- return this . getRequest ( 'POST' , signal ) ( '/query' , data , options ) ;
114
- } ;
39
+ getRequest < T > ( method : Method , signal ?: AbortSignal ) {
40
+ return this . implementation . getRequest < T > ( method , signal ) ;
115
41
}
116
42
}
117
43
118
- const Default = new DefaultApiClient ( ) ;
44
+ const Default = new DashboardApiClient ( ) ;
119
45
120
46
export function configureAPIClient ( config : IDashboardConfig ) {
121
47
if ( Default . baseURL !== config . apiBaseURL ) {
@@ -127,29 +53,15 @@ export function configureAPIClient(config: IDashboardConfig) {
127
53
if ( config . app_secret ) {
128
54
Default . app_secret = config . app_secret ;
129
55
}
56
+
130
57
if ( config . makeQueryENV ) {
131
58
Default . makeQueryENV = config . makeQueryENV ;
132
59
}
133
60
}
134
61
135
- export class FacadeApiClient implements IAPIClient {
136
- implementation : IAPIClient = Default ;
137
-
138
- getRequest < T > (
139
- method : Method ,
140
- signal ?: AbortSignal ,
141
- ) : ( url : string , data : AnyObject , options ?: IAPIClientRequestOptions ) => Promise < T > {
142
- return this . implementation . getRequest ( method , signal ) ;
143
- }
144
-
145
- query < T > ( signal ?: AbortSignal ) : ( data : TQueryPayload , options ?: AnyObject ) => Promise < T > {
146
- return this . implementation . query ( signal ) ;
147
- }
148
- }
149
-
150
62
/**
151
63
* @example facadeApiClient.implementation = new MyAPIClient();
152
64
*/
153
- export const facadeApiClient = new FacadeApiClient ( ) ;
65
+ export const facadeApiClient = new DashboardApiFacadeClient ( Default ) ;
154
66
155
- export const APIClient : IAPIClient = facadeApiClient ;
67
+ export const APIClient : IDashboardAPIClient = facadeApiClient ;
0 commit comments