-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathindex.js
70 lines (63 loc) · 2.06 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
import parseConfigFile from './zuliprc';
const api = require('./api');
const accounts = require('./resources/accounts');
const streams = require('./resources/streams');
const messages = require('./resources/messages');
const queues = require('./resources/queues');
const events = require('./resources/events');
const users = require('./resources/users');
const emojis = require('./resources/emojis');
const typing = require('./resources/typing');
const reactions = require('./resources/reactions');
const server = require('./resources/server');
const filters = require('./resources/filters');
const eventsWapper = require('./events_wrapper');
function getCallEndpoint(config) {
return function callEndpoint(endpoint, method = 'GET', params) {
const myConfig = { ...config };
let finalendpoint = endpoint;
if (!endpoint.startsWith('/')) {
finalendpoint = `/${endpoint}`; // eslint-disable-line
}
const url = myConfig.apiURL + finalendpoint;
return api(url, myConfig, method, params);
};
}
function resources(config) {
return {
config,
callEndpoint: getCallEndpoint(config),
accounts: accounts(config),
streams: streams(config),
messages: messages(config),
queues: queues(config),
events: events(config),
users: users(config),
emojis: emojis(config),
typing: typing(config),
reactions: reactions(config),
server: server(config),
filters: filters(config),
callOnEachEvent: eventsWapper(config),
};
}
function zulip(initialConfig) {
if (initialConfig.zuliprc) {
return parseConfigFile(initialConfig.zuliprc).then((config) => resources(config));
}
const config = initialConfig;
if (config.realm.endsWith('/api')) {
config.apiURL = `${config.realm}/v1`;
} else {
config.apiURL = `${config.realm}/api/v1`;
}
if (!config.apiKey) {
return accounts(config).retrieve().then((res) => {
if (res.error) return Promise.reject(res.msg)
config.apiKey = res.api_key;
return resources(config);
});
}
return Promise.resolve(resources(config));
}
module.exports = zulip;