-
Notifications
You must be signed in to change notification settings - Fork 38
/
client.js
162 lines (132 loc) · 4.48 KB
/
client.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const mondayApiClient = require("./monday-api-client");
const { MONDAY_OAUTH_URL } = require("./constants.js");
const { convertToArrayIfNeeded } = require("./helpers");
const { initScrollHelperIfNeeded } = require("./helpers/ui-helpers");
const { initBackgroundTracking } = require("./services/background-tracking-service");
const EMPTY_ARRAY = [];
class MondayClientSdk {
constructor(options = {}) {
this._clientId = options.clientId;
this._apiToken = options.apiToken;
this.listeners = {};
this.setClientId = this.setClientId.bind(this);
this.setToken = this.setToken.bind(this);
this.api = this.api.bind(this);
this.listen = this.listen.bind(this);
this.get = this.get.bind(this);
this.execute = this.execute.bind(this);
this.oauth = this.oauth.bind(this);
this._receiveMessage = this._receiveMessage.bind(this);
this.storage = {
instance: {
setItem: this.setStorageInstanceItem.bind(this),
getItem: this.getStorageInstanceItem.bind(this),
deleteItem: this.deleteStorageInstanceItem.bind(this)
}
};
window.addEventListener("message", this._receiveMessage, false);
if (!options.withoutScrollHelper) initScrollHelperIfNeeded();
initBackgroundTracking(this);
}
setClientId(clientId) {
this._clientId = clientId;
}
setToken(token) {
this._apiToken = token;
}
api(query, options = {}) {
const params = { query, variables: options.variables };
const token = options.token || this._apiToken;
if (token) {
return mondayApiClient.execute(params, { token });
} else {
return new Promise((resolve, reject) => {
this._localApi("api", { params })
.then(result => {
resolve(result.data);
})
.catch(err => reject(err));
});
}
}
listen(typeOrTypes, callback, params) {
const types = convertToArrayIfNeeded(typeOrTypes);
types.forEach(type => {
this._addListener(type, callback);
this._localApi("listen", { type, params });
});
// todo uniq listeners, remove listener
}
get(type, params) {
return this._localApi("get", { type, params });
}
execute(type, params) {
return this._localApi("execute", { type, params });
}
track(name, data) {
return this.execute("track", { name, data });
}
oauth(options = {}) {
const clientId = options.clientId || this._clientId;
if (!clientId) throw new Error("clientId is required");
const mondayOauthUrl = options.mondayOauthUrl || MONDAY_OAUTH_URL;
const url = `${mondayOauthUrl}?client_id=${clientId}`;
window.location = url;
}
setStorageInstanceItem(key, value, options = {}) {
return this._localApi("storage", { method: "set", key, value, options, segment: "instance" });
}
getStorageInstanceItem(key, options = {}) {
return this._localApi("storage", { method: "get", key, options, segment: "instance" });
}
deleteStorageInstanceItem(key, options = {}) {
return this._localApi("storage", { method: "delete", key, options, segment: "instance" });
}
_localApi(method, args) {
return new Promise(resolve => {
const requestId = this._generateRequestId();
const clientId = this._clientId;
const pjson = require("../package.json");
const version = pjson.version;
window.parent.postMessage({ method, args, requestId, clientId, version }, "*");
this._addListener(requestId, data => {
resolve(data);
});
});
}
_receiveMessage(event) {
const { method, type, requestId } = event.data;
const methodListeners = this.listeners[method] || EMPTY_ARRAY;
const typeListeners = this.listeners[type] || EMPTY_ARRAY;
const requestIdListeners = this.listeners[requestId] || EMPTY_ARRAY;
let listeners = [...methodListeners, ...typeListeners, ...requestIdListeners];
if (listeners) {
listeners.forEach(listener => {
try {
listener(event.data);
} catch (err) {
console.error("Message callback error: ", err);
}
});
}
}
_addListener(key, callback) {
this.listeners[key] = this.listeners[key] || [];
this.listeners[key].push(callback);
}
_generateRequestId() {
return Math.random()
.toString(36)
.substr(2, 9);
}
_removeEventListener() {
window.removeEventListener("message", this._receiveMessage, false);
}
_clearListeners() {
this.listeners = [];
}
}
function init(options = {}) {
return new MondayClientSdk(options);
}
module.exports = init;