forked from tomassedovic/etherpad-lite-client-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
98 lines (93 loc) · 3.57 KB
/
main.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
// Generated by CoffeeScript 1.12.7
//let http, https, querystring, retriever, url;
const http = require('http');
const https = require('https');
let retriever = null;
exports.connect = (options) => {
let api, apiFunctions, base, base1, fn, functionName, i, len;
if (options == null) {
options = {};
}
if (!('apikey' in options)) {
throw new Error('You must specify etherpad-lite apikey');
}
api = {};
api.options = Object.assign({}, options);
(base = api.options).host || (base.host = 'localhost');
(base1 = api.options).port || (base1.port = 9001);
retriever = http;
if (api.options.port === 443 || api.options.ssl) {
retriever = https;
}
api.call = function (functionName, functionArgs, callback) {
var apiOptions, chunks, httpOptions, req, rootPath;
rootPath = api.options.rootPath || '/api/1.2.15/';
apiOptions = Object.assign({
'apikey': this.options.apikey
}, functionArgs);
httpOptions = Object.assign(this.options, {
path: rootPath + functionName + '?' + new URLSearchParams(apiOptions).toString()
});
chunks = [];
req = retriever.get(httpOptions, (res) => {
res.on('data', (data) => {
return chunks.push(data);
});
return res.on('end', () => {
var data, error, response;
try {
data = chunks.join('');
if (Buffer.isBuffer(data)) {
data = data.toString();
response = JSON.parse(data);
}
response = JSON.parse(chunks.join(''));
} catch (error1) {
error = error1;
callback({
code: -1,
message: 'cannot parse the API response'
}, null);
return;
}
if (response.code === 0 && response.message === 'ok') {
if (response.data) {
return callback(null, response.data);
} else {
return callback(null, response);
}
} else {
return callback({
code: response.code,
message: response.message
}, null);
}
});
});
return req.on('error', (error) => {
return callback({
code: -1,
message: error.message || error
}, null);
});
};
apiFunctions = ['createGroup', 'createGroupIfNotExistsFor', 'deleteGroup', 'listPads', 'listAllPads', 'createDiffHTML', 'createPad', 'createGroupPad', 'createAuthor', 'createAuthorIfNotExistsFor', 'listPadsOfAuthor', 'createSession', 'deleteSession', 'getSessionInfo', 'listSessionsOfGroup', 'listSessionsOfAuthor', 'getText', 'setText', 'getHTML', 'setHTML', 'getAttributePool', 'getRevisionsCount', 'getSavedRevisionsCount', 'listSavedRevisions', 'saveRevision', 'getRevisionChangeset', 'getLastEdited', 'deletePad', 'copyPad', 'movePad', 'getReadOnlyID', 'getPadID', 'setPublicStatus', 'getPublicStatus', 'setPassword', 'isPasswordProtected', 'listAuthorsOfPad', 'padUsersCount', 'getAuthorName', 'padUsers', 'sendClientsMessage', 'listAllGroups', 'checkToken', 'appendChatMessage', 'getChatHistory', 'getChatHistory', 'getChatHead', 'restoreRevision', 'appendText', 'getStats', 'copyPadWithoutHistory'];
fn = (functionName) => {
return api[functionName] = (args, callback) => {
if (arguments.length === 1 && (typeof args == 'function')) {
callback = args;
args = {};
}
if (callback == null) {
callback = (() => { });
}
api.call(functionName, args, callback);
return null;
};
};
for (i = 0, len = apiFunctions.length; i < len; i++) {
functionName = apiFunctions[i];
fn(functionName);
}
return api;
};