-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
256 lines (240 loc) · 10.2 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Uses Node, AMD or browser globals to create a module.
// If you want something that will work in other stricter CommonJS environments,
// or if you need to create a circular dependency, see commonJsStrict.js
// Defines a module "returnExports" that depends another module called "b".
// Note that the name of the module is implied by the file name. It is best
// if the file name and the exported global have matching names.
// If the 'b' module also uses this type of boilerplate, then
// in the browser, it will create a global .b that is used below.
// If you do not want to support the browser global path, then you
// can remove the `root` use and the passing `this` as the first arg to
// the top function.
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
console.log("I DON'T KNOW WHO I AM");
// AMD. Register as an anonymous module.
define(['fetch', 'XMLHttpRequest'], factory);
} else if (typeof module === 'object' && module.exports) {
console.log("I AM IN NODEJS");
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory(require('node-fetch'),
require("xmlhttprequest").XMLHttpRequest);
} else {
console.log("I AM IN BROWSER");
// Browser globals (root is window)
root.mecmapi = factory(root.fetch, root.XMLHttpRequest);
}
}(typeof self !== 'undefined' ? self : this, function (fetch, XMLHttpRequest) {
// Use b in some fashion.
const mecmapi = function (cfg) {
const config = cfg;//hidden for redundency
const _this = this;
this.config = config;
this.config.target_domain = cfg.target_domain || "https://" + window.location.hostname;
this.config.fapi = this.config.target_domain + "/api/v3/";
this.config.url = {
api: _this.config.target_domain + "/api/v3/",
request: _this.config.fapi + `requests/`
}
this.config.httpHeaders = {
'Content-Type': "application/x-www-form-urlencoded",
'TECHNICIAN_KEY': this.config.technician_key
};
// "TECHNICIAN_KEY": this.config.technician_key
this.config.httpRequest = {
method: 'GET', // *GET, POST, PUT, DELETE, etc.
// mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
// credentials: 'same-origin', // include, *same-origin, omit
headers: this.config.httpHeaders,
credentials: "same-origin"
// redirect: 'follow', // manual, *follow, error
// referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
// body: JSON.stringify(data) // body data type must match "Content-Type" header
};
};
mecmapi.prototype.getConfigExample = function () {
return {
technician_key: "LONG-Long-key-Example",
target_domain: "domain.without_http.com",
request_id: "123456"
}
};
mecmapi.prototype.xhrRequestAsync = function (url, options) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(options.method, url, true);
for (headerName in options.headers) {
xhr.setRequestHeader(headerName, options.headers[headerName]);
}
xhr.onload = (e) => {
if (xhr.readyState === 4) {
resolve(xhr);
}
};
xhr.onerror = (e) => {
reject(xhr);
};
xhr.send(options.body);
});
};
mecmapi.prototype.httpRequestAsync = async function (url, options) {
// console.warn("HTTP OPTIONS: ", options);
return await this.xhrRequestAsync(url, options);
};
mecmapi.prototype.test = function (reqid, conf) {
console.log("MENECMAPI: TESTING");
console.log("CONFIG: ", this.config);
//make a request to get ticket contents
//check response, display OK,NOT OK accordingly
//log response
};
mecmapi.prototype.getRequest = async function (requestId) {
const _this = this;
const url = `${_this.config.url.request}${requestId}`
const response = await this.httpRequestAsync(url, _this.config.httpRequest);
return JSON.parse(response.response); // parses JSON response into native JavaScript objects
};
mecmapi.prototype.searchRequest = async function (inputData) {
const _this = this;
const body = 'input_data=' + encodeURIComponent(JSON.stringify(inputData));
const url = `${_this.config.url.api}/requests?${body}`;
const response = await this.httpRequestAsync(url, _this.config.httpRequest);
return JSON.parse(response.response); // parses JSON response into native JavaScript objects
};
mecmapi.prototype.updateRequest = async function (requestId, request) {
const _this = this;
// console.log("THIS IN SET: ", this);
const inputData = { "request": request };
const body = 'input_data=' + encodeURIComponent(JSON.stringify(inputData));
// const fbody = new FormData().set("intput_data", { request: { subject: "testing" } });//'input_data=' + encodeURIComponent(JSON.stringify(inputData));
// const url = `${_this.config.url.request}${requestId}?${body}`;
const url = `${_this.config.url.request}${requestId}`//?${body}`;
const response = await this.httpRequestAsync(url, {
..._this.config.httpRequest,
headers: {
..._this.config.httpHeaders,
'Content-Type': "application/x-www-form-urlencoded",
// 'Content-Type': "text/plain;charset=UTF-8"
},
method: "PUT",
body: body
});
return JSON.parse(response.response); // parses JSON response into native JavaScript objects
};
mecmapi.prototype.addNote = async function (requestId, note) {
const _this = this;
// console.log("THIS IN SET: ", this);
const inputData = { "note": note };
const body = 'input_data=' + JSON.stringify(inputData);
const url = `${_this.config.url.api}requests/${requestId}/notes`//?${body}`;
const response = await this.httpRequestAsync(url, {
..._this.config.httpRequest,
headers: {
..._this.config.httpHeaders,
'Content-Type': "application/x-www-form-urlencoded"
// 'Content-Type': "application/json"
},
method: "POST",
body: body
});
return JSON.parse(response.response); // parses JSON response into native JavaScript objects
};
mecmapi.prototype.worklog = {
add: () => null,
get: () => null,
update: () => null,
remove: () => null
};
mecmapi.prototype.addWorklog = async function (requestId, { tratimedif, description, name, userId }) {
const _this = this;
const etime = new Date().getTime();
const stime = etime - tratimedif;
const worklog = {
"owner": {
"id": userId/*,
"id": "1856265534885993"*/
},
"description": `"${description}"`,
"mark_first_response": true,
"start_time": {
"value": stime
},
"end_time": {
"value": etime
}
// ,
// "type": {
// "name": `"${name}"`
// }
// "include_nonoperational_hours": false,
// "other_charge": 1343434.4333,
// "recorded_time": {
// "value": "1478758440000"
// },
// "tech_charge": 1343434.4333,
};
const inputData = { "worklog": worklog };
const body = 'input_data=' + JSON.stringify(inputData);
const url = `${_this.config.url.api}requests/${requestId}/worklogs`//?${body}`;
const response = await this.httpRequestAsync(url, {
..._this.config.httpRequest,
headers: {
..._this.config.httpHeaders,
'Content-Type': "application/x-www-form-urlencoded"
// 'Content-Type': "application/json"
},
method: "POST",
body: body
});
return JSON.parse(response.response); // parses JSON response into native JavaScript objects
};
mecmapi.prototype.getWorklogs = async function (requestId) {
const _this = this;
const url = `${_this.config.url.api}requests/${requestId}/worklogs`//?${body}`;
const response = await this.httpRequestAsync(url, {
..._this.config.httpRequest,
headers: {
..._this.config.httpHeaders,
'Content-Type': "application/x-www-form-urlencoded"
// 'Content-Type': "application/json"
},
method: "GET",
body: body
});
return JSON.parse(response.response); // parses JSON response into native JavaScript objects
}
mecmapi.prototype.addWorklog.exampleInputObject = {
tratimedif: 600000,
description: "logged by mecmapi extension",
name: "autolog-mecmapi",
umail: "[email protected]"
};
tmp = {
"owner": {
"email_id": `[email protected]`/*,
"id": "1856265534885993"*/
},
"description": `description`,
"mark_first_response": true,
"start_time": {
"value": 0000001
},
"end_time": {
"value": 00000002
},
"worklog_type": {
"name": `worklog_name`
}
// "include_nonoperational_hours": false,
// "other_charge": 1343434.4333,
// "recorded_time": {
// "value": "1478758440000"
// },
// "tech_charge": 1343434.4333,
};
return mecmapi;
}));
//SOURCE: https://github.com/umdjs/umd/blob/master/templates/returnExports.js