forked from github-tools/github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGist.js
188 lines (170 loc) · 6.58 KB
/
Gist.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* @file
* @copyright 2013 Michael Aufreiter (Development Seed) and 2016 Yahoo Inc.
* @license Licensed under {@link https://spdx.org/licenses/BSD-3-Clause-Clear.html BSD-3-Clause-Clear}.
* Github.js is freely distributable.
*/
import Requestable from './Requestable';
/**
* A Gist can retrieve and modify gists.
*/
class Gist extends Requestable {
/**
* Create a Gist.
* @param {string} id - the id of the gist (not required when creating a gist)
* @param {Requestable.auth} [auth] - information required to authenticate to Github
* @param {string} [apiBase=https://api.github.com] - the base Github API URL
*/
constructor(id, auth, apiBase) {
super(auth, apiBase);
this.__id = id;
}
/**
* Fetch a gist.
* @see https://developer.github.com/v3/gists/#get-a-single-gist
* @param {Requestable.callback} [cb] - will receive the gist
* @return {Promise} - the Promise for the http request
*/
read(cb) {
return this._request('GET', `/gists/${this.__id}`, null, cb);
}
/**
* Create a new gist.
* @see https://developer.github.com/v3/gists/#create-a-gist
* @param {Object} gist - the data for the new gist
* @param {Requestable.callback} [cb] - will receive the new gist upon creation
* @return {Promise} - the Promise for the http request
*/
create(gist, cb) {
return this._request('POST', '/gists', gist, cb)
.then((response) => {
this.__id = response.data.id;
return response;
});
}
/**
* Delete a gist.
* @see https://developer.github.com/v3/gists/#delete-a-gist
* @param {Requestable.callback} [cb] - will receive true if the request succeeds
* @return {Promise} - the Promise for the http request
*/
delete(cb) {
return this._request('DELETE', `/gists/${this.__id}`, null, cb);
}
/**
* Fork a gist.
* @see https://developer.github.com/v3/gists/#fork-a-gist
* @param {Requestable.callback} [cb] - the function that will receive the gist
* @return {Promise} - the Promise for the http request
*/
fork(cb) {
return this._request('POST', `/gists/${this.__id}/forks`, null, cb);
}
/**
* Update a gist.
* @see https://developer.github.com/v3/gists/#edit-a-gist
* @param {Object} gist - the new data for the gist
* @param {Requestable.callback} [cb] - the function that receives the API result
* @return {Promise} - the Promise for the http request
*/
update(gist, cb) {
return this._request('PATCH', `/gists/${this.__id}`, gist, cb);
}
/**
* Star a gist.
* @see https://developer.github.com/v3/gists/#star-a-gist
* @param {Requestable.callback} [cb] - will receive true if the request is successful
* @return {Promise} - the Promise for the http request
*/
star(cb) {
return this._request('PUT', `/gists/${this.__id}/star`, null, cb);
}
/**
* Unstar a gist.
* @see https://developer.github.com/v3/gists/#unstar-a-gist
* @param {Requestable.callback} [cb] - will receive true if the request is successful
* @return {Promise} - the Promise for the http request
*/
unstar(cb) {
return this._request('DELETE', `/gists/${this.__id}/star`, null, cb);
}
/**
* Check if a gist is starred by the user.
* @see https://developer.github.com/v3/gists/#check-if-a-gist-is-starred
* @param {Requestable.callback} [cb] - will receive true if the gist is starred and false if the gist is not starred
* @return {Promise} - the Promise for the http request
*/
isStarred(cb) {
return this._request204or404(`/gists/${this.__id}/star`, null, cb);
}
/**
* List the gist's commits
* @see https://developer.github.com/v3/gists/#list-gist-commits
* @param {Requestable.callback} [cb] - will receive the array of commits
* @return {Promise} - the Promise for the http request
*/
listCommits(cb) {
return this._requestAllPages(`/gists/${this.__id}/commits`, null, cb);
}
/**
* Fetch one of the gist's revision.
* @see https://developer.github.com/v3/gists/#get-a-specific-revision-of-a-gist
* @param {string} revision - the id of the revision
* @param {Requestable.callback} [cb] - will receive the revision
* @return {Promise} - the Promise for the http request
*/
getRevision(revision, cb) {
return this._request('GET', `/gists/${this.__id}/${revision}`, null, cb);
}
/**
* List the gist's comments
* @see https://developer.github.com/v3/gists/comments/#list-comments-on-a-gist
* @param {Requestable.callback} [cb] - will receive the array of comments
* @return {Promise} - the promise for the http request
*/
listComments(cb) {
return this._requestAllPages(`/gists/${this.__id}/comments`, null, cb);
}
/**
* Fetch one of the gist's comments
* @see https://developer.github.com/v3/gists/comments/#get-a-single-comment
* @param {number} comment - the id of the comment
* @param {Requestable.callback} [cb] - will receive the comment
* @return {Promise} - the Promise for the http request
*/
getComment(comment, cb) {
return this._request('GET', `/gists/${this.__id}/comments/${comment}`, null, cb);
}
/**
* Comment on a gist
* @see https://developer.github.com/v3/gists/comments/#create-a-comment
* @param {string} comment - the comment to add
* @param {Requestable.callback} [cb] - the function that receives the API result
* @return {Promise} - the Promise for the http request
*/
createComment(comment, cb) {
return this._request('POST', `/gists/${this.__id}/comments`, {body: comment}, cb);
}
/**
* Edit a comment on the gist
* @see https://developer.github.com/v3/gists/comments/#edit-a-comment
* @param {number} comment - the id of the comment
* @param {string} body - the new comment
* @param {Requestable.callback} [cb] - will receive the modified comment
* @return {Promise} - the promise for the http request
*/
editComment(comment, body, cb) {
return this._request('PATCH', `/gists/${this.__id}/comments/${comment}`, {body: body}, cb);
}
/**
* Delete a comment on the gist.
* @see https://developer.github.com/v3/gists/comments/#delete-a-comment
* @param {number} comment - the id of the comment
* @param {Requestable.callback} [cb] - will receive true if the request succeeds
* @return {Promise} - the Promise for the http request
*/
deleteComment(comment, cb) {
return this._request('DELETE', `/gists/${this.__id}/comments/${comment}`, null, cb);
}
}
module.exports = Gist;