Skip to content

Commit

Permalink
no auto process of actions
Browse files Browse the repository at this point in the history
  • Loading branch information
artpar committed Sep 28, 2024
1 parent 5670fea commit 6f057d2
Show file tree
Hide file tree
Showing 9 changed files with 2,836 additions and 2,207 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,12 @@ daptinClient.worldManager.loadModels().then(function () {


```


Publish
==

```bash
npm adduser
npm publish
```
56 changes: 11 additions & 45 deletions lib/clients/actionmanager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from "axios"
import {AxiosInstance} from "axios"
import * as jwt_decode from 'jwt-decode';
import {AppConfigProvider, TokenGetter} from "./interface";

Expand All @@ -7,20 +7,17 @@ export class ActionManager {
appConfig: AppConfigProvider;
tokenGetter: TokenGetter;
actionMap: any;
private axios: AxiosInstance;

constructor(appConfig, getToken) {
constructor(appConfig, getToken, axiosInstance) {
this.appConfig = appConfig;
this.tokenGetter = getToken;
this.actionMap = {};
this.axios = axiosInstance;
}

setActions(typeName, actions) {
this.actionMap[typeName] = actions;
};


private static base64ToArrayBuffer(base64) {
const binaryString = window.atob(base64);
const binaryString = atob(base64);
const binaryLen = binaryString.length;
const bytes = new Uint8Array(binaryLen);
for (let i = 0; i < binaryLen; i++) {
Expand All @@ -39,11 +36,14 @@ export class ActionManager {
window.URL.revokeObjectURL(url);
};

setActions(typeName, actions) {
this.actionMap[typeName] = actions;
};

getGuestActions() {
const that = this;
return new Promise(function (resolve, reject) {
axios({
that.axios({
url: that.appConfig.endpoint + "/actions",
method: "GET"
}).then(function (respo) {
Expand All @@ -59,7 +59,7 @@ export class ActionManager {
// console.log("invoke action", type, actionName, data);
const that = this;
return new Promise(function (resolve, reject) {
axios({
that.axios({
url: that.appConfig.endpoint + "/action/" + type + "/" + actionName,
method: "POST",
headers: {
Expand All @@ -70,41 +70,7 @@ export class ActionManager {
}
}).then(function (res) {
resolve(res.data);
console.log("action response", res);
const responses = res.data;
for (let i = 0; i < responses.length; i++) {
const responseType = responses[i].ResponseType;

const data = responses[i].Attributes;
switch (responseType) {
case "client.notify":
console.log("notify client", data);
break;
case "client.store.set":
console.log("notify client", data);
if (window && window.localStorage) {
window.localStorage.setItem(data.key, data.value);
if (data.key === "token") {
window.localStorage.setItem('user', JSON.stringify(jwt_decode(data.value)));
}
}
break;
case "client.file.download":
ActionManager.saveByteArray(data);
break;
case "client.redirect":
break;

case "client.cookie.set":
if (document) {
document.cookie = data.key + "=" + data.value + ";"
}
break;

}
}
}, function (res) {
console.log("action failed", res);
reject(res);
})

Expand Down Expand Up @@ -134,6 +100,6 @@ export class ActionManager {
return this.actionMap[typeName][actionName];
};

};
}

export default ActionManager
14 changes: 8 additions & 6 deletions lib/clients/configmanager.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import axios from "axios"
import {AxiosInstance} from "axios"
import {TokenGetter} from "./interface";
import AppConfig from "./appconfig";

export class ConfigManager {
appConfig: AppConfig;
getToken: TokenGetter;
private axios: AxiosInstance;

constructor(appConfig: AppConfig, getToken: TokenGetter) {
constructor(appConfig: AppConfig, getToken: TokenGetter, axiosInstance: AxiosInstance) {
this.appConfig = appConfig;
this.getToken = getToken;
this.axios = axiosInstance;
}

getConfig(configName: string, configType: string) {
const that = this;
return new Promise(function (resolve, reject) {
axios({
that.axios({
url: that.appConfig.endpoint + "/_config/" + configType + "/" + configName,
headers: {
"Authorization": "Bearer " + that.getToken.getToken()
Expand All @@ -33,7 +35,7 @@ export class ConfigManager {
getAllConfig() {
const that = this;
return new Promise(function (resolve, reject) {
axios({
that.axios({
url: that.appConfig.endpoint + "/_config",
headers: {
"Authorization": "Bearer " + that.getToken.getToken()
Expand All @@ -52,7 +54,7 @@ export class ConfigManager {
setConfig(configName: string, configType: string, configValue: any) {
const that = this;
return new Promise(function (resolve, reject) {
axios({
that.axios({
url: that.appConfig.endpoint + "/_config/" + configType + "/" + configName,
headers: {
"Authorization": "Bearer " + that.getToken.getToken()
Expand All @@ -67,4 +69,4 @@ export class ConfigManager {
})
});
}
}
}
10 changes: 6 additions & 4 deletions lib/clients/statsmanager.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import axios, {AxiosResponse} from "axios"
import {AxiosInstance, AxiosResponse} from "axios"
import {TokenGetter} from './interface'
import AppConfig from "./appconfig";

export class StatsManager {
tokenGetter: TokenGetter;
appConfig: AppConfig;
private axios: AxiosInstance;

constructor(appConfig, tokenGetter: TokenGetter) {
constructor(appConfig, tokenGetter: TokenGetter, axiosInstance: AxiosInstance) {
this.appConfig = appConfig;
this.tokenGetter = tokenGetter;
this.axios = axiosInstance;
}

private static queryToParams(statsRequest) {
Expand Down Expand Up @@ -39,7 +41,7 @@ export class StatsManager {
getStats(tableName, statsRequest) {
const that = this;
return new Promise(function (resolve, reject) {
return axios({
return that.axios({
url: that.appConfig.getEndpoint() + "/aggregate/" + tableName + StatsManager.queryToParams(statsRequest),
headers: {
"Authorization": "Bearer " + that.tokenGetter.getToken()
Expand All @@ -54,4 +56,4 @@ export class StatsManager {
}


export default StatsManager;
export default StatsManager;
33 changes: 17 additions & 16 deletions lib/clients/worldmanager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Created by artpar on 6/7/17.
*/
import axios, {AxiosResponse} from "axios"
import {AxiosInstance, AxiosResponse} from "axios"
import {AppConfigProvider, TokenGetter} from "./interface";
import ActionManager from "./actionmanager";

Expand Down Expand Up @@ -33,19 +33,13 @@ export class WorldManager {
columnTypes: any;
worlds: Object;
systemActions: any;

modelLoader(typeName: string, force: boolean): Promise<any> {
const that = this;
return new Promise(function (resolve, reject) {
return that.getColumnKeys(typeName, force).then(resolve).catch(reject)
});
};

tokenGetter: TokenGetter;
private axios: AxiosInstance;

constructor(appConfig: AppConfigProvider, tokenGetter: TokenGetter, jsonApi: any, actionManager: ActionManager) {
constructor(appConfig: AppConfigProvider, tokenGetter: TokenGetter, jsonApi: any, actionManager: ActionManager, axios: AxiosInstance) {
this.appConfig = appConfig;
this.jsonApi = jsonApi;
this.axios = axios;
this.actionManager = actionManager;
this.tokenGetter = tokenGetter;
this.columnKeysCache = {};
Expand All @@ -56,12 +50,19 @@ export class WorldManager {
this.columnTypes = {};
}

modelLoader(typeName: string, force: boolean): Promise<any> {
const that = this;
return new Promise(function (resolve, reject) {
return that.getColumnKeys(typeName, force).then(resolve).catch(reject)
});
};

init() {
const that = this;
return new Promise(function (resolve, reject) {
that.columnTypes = [];

axios(that.appConfig.getEndpoint() + "/meta?query=column_types", {
that.axios(that.appConfig.getEndpoint() + "/meta?query=column_types", {
headers: {
"Authorization": "Bearer " + that.tokenGetter.getToken()
}
Expand Down Expand Up @@ -97,7 +98,7 @@ export class WorldManager {
const that = this;

return new Promise(function (resolve, reject) {
axios({
that.axios({
url: that.appConfig.getEndpoint() + "/track/start/" + stateMachineRefId,
method: "POST",
data: {
Expand All @@ -118,7 +119,7 @@ export class WorldManager {
trackObjectEvent(typeName, stateMachineRefId, eventName) {
const that = this;
return new Promise(function (resolve, reject) {
axios({
that.axios({
url: that.appConfig.getEndpoint() + "/track/event/" + typeName + "/" + stateMachineRefId + "/" + eventName,
method: "POST",
headers: {
Expand All @@ -141,7 +142,7 @@ export class WorldManager {
return
}

axios(that.appConfig.getEndpoint() + '/jsmodel/' + typeName + ".js", {
that.axios(that.appConfig.getEndpoint() + '/jsmodel/' + typeName + ".js", {
headers: {
"Authorization": "Bearer " + that.tokenGetter.getToken()
},
Expand Down Expand Up @@ -267,7 +268,7 @@ export class WorldManager {
return new Promise(function (resolve, reject) {

if (worldName.indexOf("_has_") > -1) {
resolve();
resolve(null);
return
}

Expand Down Expand Up @@ -342,4 +343,4 @@ export class WorldManager {
}
}

export default WorldManager;
export default WorldManager;
19 changes: 12 additions & 7 deletions lib/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import axios from "axios"
import {ActionManager} from "./clients/actionmanager"
import {AppConfig} from './clients/appconfig'
import {StatsManager} from './clients/statsmanager'
Expand All @@ -23,22 +24,26 @@ export class DaptinClient {
public statsManager: StatsManager;
public configManager: ConfigManager;

constructor(endpoint, debug, tokenGetter) {
constructor(endpoint, debug, tokenGetter, axiosConfig : any) {
const that = this;
debug = debug || false;
axiosConfig = axiosConfig || {}
let axiosInstance = axios.create(axiosConfig)
that.appConfig = new AppConfig(endpoint);

that.jsonApi = new JsonApi({
apiUrl: that.appConfig.getEndpoint() + '/api',
pluralize: false,
logger: debug
logger: debug,
...axiosConfig
});
that.jsonApi.axios = axiosInstance;

that.tokenGetter = tokenGetter || new LocalStorageTokenGetter();
that.actionManager = new ActionManager(that.appConfig, that.tokenGetter);
that.worldManager = new WorldManager(that.appConfig, that.tokenGetter, that.jsonApi, that.actionManager);
that.statsManager = new StatsManager(that.appConfig, that.tokenGetter);
that.configManager = new ConfigManager(that.appConfig, that.tokenGetter);
that.actionManager = new ActionManager(that.appConfig, that.tokenGetter, axiosInstance);
that.worldManager = new WorldManager(that.appConfig, that.tokenGetter, that.jsonApi, that.actionManager, axiosInstance);
that.statsManager = new StatsManager(that.appConfig, that.tokenGetter, axiosInstance);
that.configManager = new ConfigManager(that.appConfig, that.tokenGetter, axiosInstance);


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

}

}
}
Loading

0 comments on commit 6f057d2

Please sign in to comment.