Skip to content

Commit 6f057d2

Browse files
committed
no auto process of actions
1 parent 5670fea commit 6f057d2

File tree

9 files changed

+2836
-2207
lines changed

9 files changed

+2836
-2207
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,12 @@ daptinClient.worldManager.loadModels().then(function () {
4242

4343

4444
```
45+
46+
47+
Publish
48+
==
49+
50+
```bash
51+
npm adduser
52+
npm publish
53+
```

lib/clients/actionmanager.ts

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import axios from "axios"
1+
import {AxiosInstance} from "axios"
22
import * as jwt_decode from 'jwt-decode';
33
import {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

139105
export default ActionManager

lib/clients/configmanager.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
import axios from "axios"
1+
import {AxiosInstance} from "axios"
22
import {TokenGetter} from "./interface";
33
import AppConfig from "./appconfig";
44

55
export class ConfigManager {
66
appConfig: AppConfig;
77
getToken: TokenGetter;
8+
private axios: AxiosInstance;
89

9-
constructor(appConfig: AppConfig, getToken: TokenGetter) {
10+
constructor(appConfig: AppConfig, getToken: TokenGetter, axiosInstance: AxiosInstance) {
1011
this.appConfig = appConfig;
1112
this.getToken = getToken;
13+
this.axios = axiosInstance;
1214
}
1315

1416
getConfig(configName: string, configType: string) {
1517
const that = this;
1618
return new Promise(function (resolve, reject) {
17-
axios({
19+
that.axios({
1820
url: that.appConfig.endpoint + "/_config/" + configType + "/" + configName,
1921
headers: {
2022
"Authorization": "Bearer " + that.getToken.getToken()
@@ -33,7 +35,7 @@ export class ConfigManager {
3335
getAllConfig() {
3436
const that = this;
3537
return new Promise(function (resolve, reject) {
36-
axios({
38+
that.axios({
3739
url: that.appConfig.endpoint + "/_config",
3840
headers: {
3941
"Authorization": "Bearer " + that.getToken.getToken()
@@ -52,7 +54,7 @@ export class ConfigManager {
5254
setConfig(configName: string, configType: string, configValue: any) {
5355
const that = this;
5456
return new Promise(function (resolve, reject) {
55-
axios({
57+
that.axios({
5658
url: that.appConfig.endpoint + "/_config/" + configType + "/" + configName,
5759
headers: {
5860
"Authorization": "Bearer " + that.getToken.getToken()
@@ -67,4 +69,4 @@ export class ConfigManager {
6769
})
6870
});
6971
}
70-
}
72+
}

lib/clients/statsmanager.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import axios, {AxiosResponse} from "axios"
1+
import {AxiosInstance, AxiosResponse} from "axios"
22
import {TokenGetter} from './interface'
33
import AppConfig from "./appconfig";
44

55
export class StatsManager {
66
tokenGetter: TokenGetter;
77
appConfig: AppConfig;
8+
private axios: AxiosInstance;
89

9-
constructor(appConfig, tokenGetter: TokenGetter) {
10+
constructor(appConfig, tokenGetter: TokenGetter, axiosInstance: AxiosInstance) {
1011
this.appConfig = appConfig;
1112
this.tokenGetter = tokenGetter;
13+
this.axios = axiosInstance;
1214
}
1315

1416
private static queryToParams(statsRequest) {
@@ -39,7 +41,7 @@ export class StatsManager {
3941
getStats(tableName, statsRequest) {
4042
const that = this;
4143
return new Promise(function (resolve, reject) {
42-
return axios({
44+
return that.axios({
4345
url: that.appConfig.getEndpoint() + "/aggregate/" + tableName + StatsManager.queryToParams(statsRequest),
4446
headers: {
4547
"Authorization": "Bearer " + that.tokenGetter.getToken()
@@ -54,4 +56,4 @@ export class StatsManager {
5456
}
5557

5658

57-
export default StatsManager;
59+
export default StatsManager;

lib/clients/worldmanager.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Created by artpar on 6/7/17.
33
*/
4-
import axios, {AxiosResponse} from "axios"
4+
import {AxiosInstance, AxiosResponse} from "axios"
55
import {AppConfigProvider, TokenGetter} from "./interface";
66
import ActionManager from "./actionmanager";
77

@@ -33,19 +33,13 @@ export class WorldManager {
3333
columnTypes: any;
3434
worlds: Object;
3535
systemActions: any;
36-
37-
modelLoader(typeName: string, force: boolean): Promise<any> {
38-
const that = this;
39-
return new Promise(function (resolve, reject) {
40-
return that.getColumnKeys(typeName, force).then(resolve).catch(reject)
41-
});
42-
};
43-
4436
tokenGetter: TokenGetter;
37+
private axios: AxiosInstance;
4538

46-
constructor(appConfig: AppConfigProvider, tokenGetter: TokenGetter, jsonApi: any, actionManager: ActionManager) {
39+
constructor(appConfig: AppConfigProvider, tokenGetter: TokenGetter, jsonApi: any, actionManager: ActionManager, axios: AxiosInstance) {
4740
this.appConfig = appConfig;
4841
this.jsonApi = jsonApi;
42+
this.axios = axios;
4943
this.actionManager = actionManager;
5044
this.tokenGetter = tokenGetter;
5145
this.columnKeysCache = {};
@@ -56,12 +50,19 @@ export class WorldManager {
5650
this.columnTypes = {};
5751
}
5852

53+
modelLoader(typeName: string, force: boolean): Promise<any> {
54+
const that = this;
55+
return new Promise(function (resolve, reject) {
56+
return that.getColumnKeys(typeName, force).then(resolve).catch(reject)
57+
});
58+
};
59+
5960
init() {
6061
const that = this;
6162
return new Promise(function (resolve, reject) {
6263
that.columnTypes = [];
6364

64-
axios(that.appConfig.getEndpoint() + "/meta?query=column_types", {
65+
that.axios(that.appConfig.getEndpoint() + "/meta?query=column_types", {
6566
headers: {
6667
"Authorization": "Bearer " + that.tokenGetter.getToken()
6768
}
@@ -97,7 +98,7 @@ export class WorldManager {
9798
const that = this;
9899

99100
return new Promise(function (resolve, reject) {
100-
axios({
101+
that.axios({
101102
url: that.appConfig.getEndpoint() + "/track/start/" + stateMachineRefId,
102103
method: "POST",
103104
data: {
@@ -118,7 +119,7 @@ export class WorldManager {
118119
trackObjectEvent(typeName, stateMachineRefId, eventName) {
119120
const that = this;
120121
return new Promise(function (resolve, reject) {
121-
axios({
122+
that.axios({
122123
url: that.appConfig.getEndpoint() + "/track/event/" + typeName + "/" + stateMachineRefId + "/" + eventName,
123124
method: "POST",
124125
headers: {
@@ -141,7 +142,7 @@ export class WorldManager {
141142
return
142143
}
143144

144-
axios(that.appConfig.getEndpoint() + '/jsmodel/' + typeName + ".js", {
145+
that.axios(that.appConfig.getEndpoint() + '/jsmodel/' + typeName + ".js", {
145146
headers: {
146147
"Authorization": "Bearer " + that.tokenGetter.getToken()
147148
},
@@ -267,7 +268,7 @@ export class WorldManager {
267268
return new Promise(function (resolve, reject) {
268269

269270
if (worldName.indexOf("_has_") > -1) {
270-
resolve();
271+
resolve(null);
271272
return
272273
}
273274

@@ -342,4 +343,4 @@ export class WorldManager {
342343
}
343344
}
344345

345-
export default WorldManager;
346+
export default WorldManager;

lib/main.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import axios from "axios"
12
import {ActionManager} from "./clients/actionmanager"
23
import {AppConfig} from './clients/appconfig'
34
import {StatsManager} from './clients/statsmanager'
@@ -23,22 +24,26 @@ export class DaptinClient {
2324
public statsManager: StatsManager;
2425
public configManager: ConfigManager;
2526

26-
constructor(endpoint, debug, tokenGetter) {
27+
constructor(endpoint, debug, tokenGetter, axiosConfig : any) {
2728
const that = this;
2829
debug = debug || false;
30+
axiosConfig = axiosConfig || {}
31+
let axiosInstance = axios.create(axiosConfig)
2932
that.appConfig = new AppConfig(endpoint);
3033

3134
that.jsonApi = new JsonApi({
3235
apiUrl: that.appConfig.getEndpoint() + '/api',
3336
pluralize: false,
34-
logger: debug
37+
logger: debug,
38+
...axiosConfig
3539
});
40+
that.jsonApi.axios = axiosInstance;
3641

3742
that.tokenGetter = tokenGetter || new LocalStorageTokenGetter();
38-
that.actionManager = new ActionManager(that.appConfig, that.tokenGetter);
39-
that.worldManager = new WorldManager(that.appConfig, that.tokenGetter, that.jsonApi, that.actionManager);
40-
that.statsManager = new StatsManager(that.appConfig, that.tokenGetter);
41-
that.configManager = new ConfigManager(that.appConfig, that.tokenGetter);
43+
that.actionManager = new ActionManager(that.appConfig, that.tokenGetter, axiosInstance);
44+
that.worldManager = new WorldManager(that.appConfig, that.tokenGetter, that.jsonApi, that.actionManager, axiosInstance);
45+
that.statsManager = new StatsManager(that.appConfig, that.tokenGetter, axiosInstance);
46+
that.configManager = new ConfigManager(that.appConfig, that.tokenGetter, axiosInstance);
4247

4348

4449
that.jsonApi.insertMiddlewareBefore("HEADER", {
@@ -55,4 +60,4 @@ export class DaptinClient {
5560

5661
}
5762

58-
}
63+
}

0 commit comments

Comments
 (0)