forked from tomassedovic/etherpad-lite-client-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
97 lines (89 loc) · 3.53 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
// Generated by CoffeeScript 1.10.0
(function() {
var _, http, https, querystring, retriever, url;
http = require('http');
https = require('https');
url = require('url');
querystring = require('querystring');
_ = require('underscore');
retriever = null;
exports.connect = function(options) {
var 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 = _.extend({}, 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.12/';
apiOptions = _.extend({
'apikey': this.options.apikey
}, functionArgs);
httpOptions = _.extend(this.options, {
path: rootPath + functionName + '?' + querystring.stringify(apiOptions)
});
chunks = [];
req = retriever.get(httpOptions, function(res) {
res.on('data', function(data) {
return chunks.push(data);
});
return res.on('end', function() {
var error, error1, response;
try {
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') {
return callback(null, response.data);
} else {
return callback({
code: response.code,
message: response.message
}, null);
}
});
});
return req.on('error', function(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'];
fn = function(functionName) {
return api[functionName] = function(args, callback) {
if (arguments.length === 1 && _.isFunction(args)) {
callback = args;
args = {};
}
if (callback == null) {
callback = (function() {});
}
api.call(functionName, args, callback);
return null;
};
};
for (i = 0, len = apiFunctions.length; i < len; i++) {
functionName = apiFunctions[i];
fn(functionName);
}
return api;
};
}).call(this);