-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (103 loc) · 2.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*global MicroserviceClient*/
function getWatch() {
return {
"$api.client": function (newValue, oldValue) {
if (newValue) {
if (this.$api.online === false || oldValue == false) {
this.$api.online = true;
}
return;
}
this.$api.online = false;
},
"$api.accessToken": function (newValue) {
if (newValue && newValue != "") {
return (this.$api.client = new MicroserviceClient({
URL: this.$api.url,
accessToken: newValue,
}));
}
this.$api.client = false;
},
"$api.expireAt": function (newValue, oldValue) {
if (newValue == 0) {
this.$api.client = false;
this.$api.accessToken = "";
return;
}
if (newValue < oldValue) {
return;
}
if (this.$api.timerApiClient) {
clearTimeout(this.$api.timerApiClient);
}
var self = this;
var period = newValue - Date.now();
if (period > 0 && !isNaN(period)) {
this.$api.timerApiClient = setTimeout(function () {
self.$api.expireAt = 0;
self.$api.accessToken = "";
}, period);
}
},
};
}
import { reactive } from "vue";
export default {
install: (app, apiSettings) => {
var api = {
online: false,
client: false,
timerApiClient: false,
url: apiSettings.apiURL,
variables: {},
accessToken: "",
expireAt: 0,
setAccessToken: function (setValue) {
this.accessToken = setValue.accessToken;
this.expireAt = setValue.expireAt;
},
setVariables: function(id, value){
this.variables[id] = value
},
testAccessToken: function (AccessToken, callback) {
var client = new MicroserviceClient({
URL: apiSettings.apiURL,
accessToken: AccessToken,
headers: { scope: "auth" },
});
client.get("auth/" + AccessToken, function (err, handlerResponse) {
console.log(err, handlerResponse);
if (callback) {
callback(err, handlerResponse);
}
});
},
};
if (apiSettings.methods) {
for (var i in apiSettings.methods) {
var method = apiSettings.methods[i];
if (
typeof method.name !== "undefined" &&
typeof method.function !== "undefined"
) {
api[method.name] = method.function;
}
}
}
app._APIState = false;
app.config.globalProperties["$api"] = reactive(api);
app.mixin({
created: function beforeCreate() {
if (!app._APIState) {
app._APIState = true;
app.$api = this.$api;
var watchers = getWatch();
for (var name in watchers) {
this.$watch(name, watchers[name]);
}
}
},
});
},
};