-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
110 lines (97 loc) Β· 2.52 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
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
const fetch = require('isomorphic-fetch')
class Telegraph {
constructor (token, opts) {
this.options = Object.assign({
token: token,
apiRoot: 'https://api.telegra.ph'
}, opts)
}
set token (token) {
this.options.token = token
}
callService (method, payload) {
return fetch(`${this.options.apiRoot}/${method}`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(payload)
})
.then(unwrap)
}
createAccount (shortName, name, url) {
return this.callService('createAccount', {
short_name: shortName,
author_name: name,
author_url: url
})
}
createPage (title, content, authorName, authorUrl, returnContent) {
return this.callService('createPage', {
access_token: this.options.token,
title: title,
author_name: authorName,
author_url: authorUrl,
content: content,
return_content: returnContent
})
}
editAccountInfo (shortName, name, url) {
return this.callService('editAccountInfo', {
access_token: this.options.token,
short_name: shortName,
author_name: name,
author_url: url
})
}
editPage (path, title, content, authorName, authorUrl, returnContent) {
return this.callService(`editPage/${path}`, {
access_token: this.options.token,
title: title,
author_name: authorName,
author_url: authorUrl,
content: content,
return_content: returnContent
})
}
getPage (path, returnContent) {
return this.callService(`getPage/${path}`, {
access_token: this.options.token,
return_content: returnContent
})
}
getViews (path, year, month, day, hour) {
return this.callService(`getViews/${path}`, {
access_token: this.options.token,
year: year,
month: month,
day: day,
hour: hour
})
}
getPageList (offset, limit) {
return this.callService('getPageList', {
access_token: this.options.token,
offset: offset,
limit: limit
})
}
revokeAccessToken () {
return this.callService('revokeAccessToken', {
access_token: this.options.token
})
}
}
function unwrap (res) {
if (!res.ok) {
const err = new Error(res.statusText || 'Error calling telegra.ph')
err.statusCode = res.status
throw err
}
return res.json()
.then((json) => {
if (('ok' in json && !json.ok)) {
throw new Error(json.error || 'Error calling telegra.ph')
}
return json.result
})
}
module.exports = Telegraph