forked from intercom/intercom-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
134 lines (133 loc) · 3.76 KB
/
Copy pathclient.js
File metadata and controls
134 lines (133 loc) · 3.76 KB
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
import unirest from 'unirest';
import Bluebird from 'bluebird';
import User from './user';
import Event from './event';
import Company from './company';
import Contact from './contact';
import Counts from './counts';
import Admin from './admin';
import Tag from './tag';
import Segment from './segment';
import Message from './message';
import Conversation from './conversation';
import Note from './note';
export default class Client {
constructor(...args) {
if (args.length === 2) {
this.appId = args[0];
this.appApiKey = args[1];
} else if (args.length === 1) {
this.appId = args[0].appId;
this.appApiKey = args[0].appApiKey;
}
if (!this.appId || !this.appApiKey) {
throw new Error('Could not construct a client with those parameters');
}
this.users = new User(this);
this.events = new Event(this);
this.companies = new Company(this);
this.contacts = new Contact(this);
this.leads = new Contact(this);
this.counts = new Counts(this);
this.admins = new Admin(this);
this.tags = new Tag(this);
this.segments = new Segment(this);
this.messages = new Message(this);
this.conversations = new Conversation(this);
this.notes = new Note(this);
this.promises = false;
}
usePromises() {
this.promises = true;
return this;
}
promiseProxy(f, req) {
if (this.promises) {
const callbackHandler = this.callback;
return new Bluebird((resolve, reject) => {
const resolver = (err, data) => {
if (err) {
reject(new Error(JSON.stringify(err)));
} else {
resolve(data);
}
};
req.end(r => callbackHandler(resolver, r));
});
} else {
req.end(r => this.callback(f, r));
}
}
ping(f) {
unirest.get('https://api.intercom.io/admins')
.auth(this.appId, this.appApiKey)
.type('json')
.header('Accept', 'application/json')
.header('User-Agent', 'intercom-node-client/2.0.0')
.end(r => f(r.status));
}
put(endpoint, data, f) {
return this.promiseProxy(f,
unirest.put(`https://api.intercom.io${endpoint}`)
.auth(this.appId, this.appApiKey)
.type('json')
.send(data)
.header('Accept', 'application/json')
.header('User-Agent', 'intercom-node-client/2.0.0')
);
}
post(endpoint, data, f) {
return this.promiseProxy(f,
unirest.post(`https://api.intercom.io${endpoint}`)
.auth(this.appId, this.appApiKey)
.type('json')
.send(data)
.header('Accept', 'application/json')
.header('User-Agent', 'intercom-node-client/2.0.0')
);
}
get(endpoint, data, f) {
return this.promiseProxy(f,
unirest.get(`https://api.intercom.io${endpoint}`)
.auth(this.appId, this.appApiKey)
.type('json')
.query(data)
.header('Accept', 'application/json')
.header('User-Agent', 'intercom-node-client/2.0.0')
);
}
nextPage(paginationObject, f) {
return this.promiseProxy(f,
unirest.get(paginationObject.next)
.auth(this.appId, this.appApiKey)
.type('json')
.header('Accept', 'application/json')
.header('User-Agent', 'intercom-node-client/2.0.0')
);
}
delete(endpoint, data, f) {
return this.promiseProxy(f,
unirest.delete(`https://api.intercom.io${endpoint}`)
.auth(this.appId, this.appApiKey)
.type('json')
.query(data)
.header('Accept', 'application/json')
.header('User-Agent', 'intercom-node-client/2.0.0')
);
}
callback(f, data) {
if (!f) {
return;
}
if (f.length >= 2) {
const hasErrors = data.error || (data.body && data.body.type === 'error.list');
if (hasErrors) {
f(data, null);
} else {
f(null, data);
}
} else {
f(data);
}
}
}