1- import axios from "axios"
1+ import { AxiosInstance } from "axios"
22import * as jwt_decode from 'jwt-decode' ;
33import { AppConfigProvider , TokenGetter } from "./interface" ;
44
@@ -7,20 +7,17 @@ export class ActionManager {
77 appConfig : AppConfigProvider ;
88 tokenGetter : TokenGetter ;
99 actionMap : any ;
10+ private axios : AxiosInstance ;
1011
11- constructor ( appConfig , getToken ) {
12+ constructor ( appConfig , getToken , axiosInstance ) {
1213 this . appConfig = appConfig ;
1314 this . tokenGetter = getToken ;
1415 this . actionMap = { } ;
16+ this . axios = axiosInstance ;
1517 }
1618
17- setActions ( typeName , actions ) {
18- this . actionMap [ typeName ] = actions ;
19- } ;
20-
21-
2219 private static base64ToArrayBuffer ( base64 ) {
23- const binaryString = window . atob ( base64 ) ;
20+ const binaryString = atob ( base64 ) ;
2421 const binaryLen = binaryString . length ;
2522 const bytes = new Uint8Array ( binaryLen ) ;
2623 for ( let i = 0 ; i < binaryLen ; i ++ ) {
@@ -39,11 +36,14 @@ export class ActionManager {
3936 window . URL . revokeObjectURL ( url ) ;
4037 } ;
4138
39+ setActions ( typeName , actions ) {
40+ this . actionMap [ typeName ] = actions ;
41+ } ;
4242
4343 getGuestActions ( ) {
4444 const that = this ;
4545 return new Promise ( function ( resolve , reject ) {
46- axios ( {
46+ that . axios ( {
4747 url : that . appConfig . endpoint + "/actions" ,
4848 method : "GET"
4949 } ) . then ( function ( respo ) {
@@ -59,7 +59,7 @@ export class ActionManager {
5959 // console.log("invoke action", type, actionName, data);
6060 const that = this ;
6161 return new Promise ( function ( resolve , reject ) {
62- axios ( {
62+ that . axios ( {
6363 url : that . appConfig . endpoint + "/action/" + type + "/" + actionName ,
6464 method : "POST" ,
6565 headers : {
@@ -70,41 +70,7 @@ export class ActionManager {
7070 }
7171 } ) . then ( function ( res ) {
7272 resolve ( res . data ) ;
73- console . log ( "action response" , res ) ;
74- const responses = res . data ;
75- for ( let i = 0 ; i < responses . length ; i ++ ) {
76- const responseType = responses [ i ] . ResponseType ;
77-
78- const data = responses [ i ] . Attributes ;
79- switch ( responseType ) {
80- case "client.notify" :
81- console . log ( "notify client" , data ) ;
82- break ;
83- case "client.store.set" :
84- console . log ( "notify client" , data ) ;
85- if ( window && window . localStorage ) {
86- window . localStorage . setItem ( data . key , data . value ) ;
87- if ( data . key === "token" ) {
88- window . localStorage . setItem ( 'user' , JSON . stringify ( jwt_decode ( data . value ) ) ) ;
89- }
90- }
91- break ;
92- case "client.file.download" :
93- ActionManager . saveByteArray ( data ) ;
94- break ;
95- case "client.redirect" :
96- break ;
97-
98- case "client.cookie.set" :
99- if ( document ) {
100- document . cookie = data . key + "=" + data . value + ";"
101- }
102- break ;
103-
104- }
105- }
10673 } , function ( res ) {
107- console . log ( "action failed" , res ) ;
10874 reject ( res ) ;
10975 } )
11076
@@ -134,6 +100,6 @@ export class ActionManager {
134100 return this . actionMap [ typeName ] [ actionName ] ;
135101 } ;
136102
137- } ;
103+ }
138104
139105export default ActionManager
0 commit comments