This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
/
index.js
71 lines (60 loc) · 1.58 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
const request = require('request-promise')
class Nautic {
constructor({ apiUrl, username, password }) {
this.apiUrl = apiUrl
this.authorization = `${Buffer.from(`${username}:${password}`).toString(
'base64'
)}`
}
static isRequired(param) {
throw new Error(`${param} is required`)
}
async createContact({ body = this.constructor.isRequired('body') }) {
const { apiUrl, authorization } = this
return await request.post({
uri: `${apiUrl}/contacts/new`,
headers: {
Authorization: `Basic ${authorization}`
},
body,
json: true
})
}
async findContact({
param = this.constructor.isRequired('param'),
value = this.constructor.isRequired('value')
}) {
const { apiUrl, authorization } = this
return await request({
uri: `${apiUrl}/contacts?search=${param}:${value}`,
headers: {
Authorization: `Basic ${authorization}`
},
json: true
})
}
async getContact({ id = this.constructor.isRequired('id') }) {
const { apiUrl, authorization } = this
return await request({
uri: `${apiUrl}/contacts/${id}`,
headers: {
Authorization: `Basic ${authorization}`
},
json: true
})
}
async updateContact({ id = this.constructor.isRequired('id'), ...rest }) {
const { apiUrl, authorization } = this
return await request.patch({
uri: `${apiUrl}/contacts/${id}/edit`,
headers: {
Authorization: `Basic ${authorization}`
},
body: {
...rest
},
json: true
})
}
}
module.exports = Nautic