-
Notifications
You must be signed in to change notification settings - Fork 38
/
client.js
212 lines (171 loc) · 6.1 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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 { logWarnings } = require("./helpers/monday-api-helpers");
const EMPTY_ARRAY = [];
const STORAGE_SEGMENT_KINDS = {
GLOBAL: "v2",
INSTANCE: "instance"
};
class MondayClientSdk {
constructor(options = {}) {
this._clientId = options.clientId;
this._apiToken = options.apiToken;
this._apiVersion = options.apiVersion;
this.listeners = {};
this.setClientId = this.setClientId.bind(this);
this.setToken = this.setToken.bind(this);
this.setApiVersion = this.setApiVersion.bind(this);
this.api = this.api.bind(this);
this.listen = this.listen.bind(this);
this.get = this.get.bind(this);
this.set = this.set.bind(this);
this.execute = this.execute.bind(this);
this.oauth = this.oauth.bind(this);
this._receiveMessage = this._receiveMessage.bind(this);
this.storage = {
setItem: this.setStorageItem.bind(this),
getItem: this.getStorageItem.bind(this),
deleteItem: this.deleteStorageItem.bind(this),
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;
}
setApiVersion(apiVersion) {
this._apiVersion = apiVersion;
}
api(query, options = {}) {
const params = { query, variables: options.variables };
const token = options.token || this._apiToken;
const apiVersion = options.apiVersion || this._apiVersion;
let responsePromise;
if (token) {
responsePromise = mondayApiClient.execute(params, token, { apiVersion });
} else {
responsePromise = this._localApi("api", { params, apiVersion }).then(result => result.data);
}
return responsePromise.then(logWarnings);
}
listen(typeOrTypes, callback, params) {
const types = convertToArrayIfNeeded(typeOrTypes);
const unsubscribes = [];
types.forEach(type => {
unsubscribes.push(this._addListener(type, callback));
this._localApi("listen", { type, params });
});
return () => {
unsubscribes.forEach(unsubscribe => unsubscribe());
};
}
get(type, params) {
return this._localApi("get", { type, params });
}
set(type, params) {
return this._localApi("set", { 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;
}
setStorageItem(key, value, options = {}) {
return this._localApi("storage", { method: "set", key, value, options, segment: STORAGE_SEGMENT_KINDS.GLOBAL });
}
getStorageItem(key, options = {}) {
return this._localApi("storage", { method: "get", key, options, segment: STORAGE_SEGMENT_KINDS.GLOBAL });
}
deleteStorageItem(key, options = {}) {
return this._localApi("storage", { method: "delete", key, options, segment: STORAGE_SEGMENT_KINDS.GLOBAL });
}
setStorageInstanceItem(key, value, options = {}) {
return this._localApi("storage", { method: "set", key, value, options, segment: STORAGE_SEGMENT_KINDS.INSTANCE });
}
getStorageInstanceItem(key, options = {}) {
return this._localApi("storage", { method: "get", key, options, segment: STORAGE_SEGMENT_KINDS.INSTANCE });
}
deleteStorageInstanceItem(key, options = {}) {
return this._localApi("storage", { method: "delete", key, options, segment: STORAGE_SEGMENT_KINDS.INSTANCE });
}
_localApi(method, args) {
return new Promise((resolve, reject) => {
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 }, "*");
const removeListener = this._addListener(requestId, data => {
removeListener();
if (data.errorMessage) {
const error = new Error(data.errorMessage);
error.data = data.data;
reject(error);
} else {
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 = new Set([...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] || new Set();
this.listeners[key].add(callback);
return () => {
this.listeners[key].delete(callback);
if (this.listeners[key].size === 0) {
delete this.listeners[key];
}
};
}
_generateRequestId() {
return Math.random()
.toString(36)
.substring(2, 9);
}
_removeEventListener() {
window.removeEventListener("message", this._receiveMessage, false);
}
_clearListeners() {
this.listeners = [];
}
}
function init(options = {}) {
return new MondayClientSdk(options);
}
module.exports = init;