-
Notifications
You must be signed in to change notification settings - Fork 9
/
octo.js
276 lines (225 loc) · 6.63 KB
/
octo.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*!
* octo.js
* Copyright (c) 2012 Justin Palmer <[email protected]>
* MIT Licensed
*/
(function() {
if(typeof superagent === 'undefined' && require) {
superagent = require('superagent')
btoa = require('btoa')
}
var octo = {}
// The main entry point for interacting with the GitHub API v3.
//
// var gh = octo.api()
// gh.get('/events').on('success', function(events) {
// console.log(events);
// })
//
octo.api = function() {
var host = 'https://api.github.com',
agent = superagent,
limit,
remaining,
username,
password,
token
function api() {}
function pager(method, path, params) {
var page = 1,
perpage = 30,
hasnext = false,
hasprev = false,
headers = {},
callbacks = {}
var request = function() {
var req = superagent[method](api.host() + path)
var complete = function(res) {
limit = ~~res.header['x-ratelimit-limit']
remaining = ~~res.header['x-ratelimit-remaining']
var link = res.header['link']
hasnext = (/rel=\"next\"/i).test(link)
hasprev = (/rel=\"next\"/).test(link)
pager.trigger('end', res)
if(res.ok) pager.trigger('success', res)
if(res.error) pager.trigger('error', res)
}
if(token) req.set('Authorization', 'token ' + token)
if(!token && username && password)
req.set('Authorization', 'Basic ' + btoa(username + ':' + password))
req
.set(headers)
.query({page: page, per_page: perpage})
.send(params)
.end(complete)
}
// ### Paging
// Each subsequent request for additional pages can easily share the same callbacks and properties.
//
// var events = api.get('/events').on('end', function(response) {
// console.log(response.body);
// events.next()
// console.log(events.page());
// })
//
// events()
//
function pager() { request() }
// Sets or gets the current page
//
// Returns the pager
pager.page = function(v) {
if(!arguments.length) return page
page = v
return pager
}
// Sets or gets the items returned per page
//
// Returns the pager
pager.perpage = function(v) {
if(!arguments.length) return perpage
perpage = v
return pager
}
// Increments the page number by one and fires a requests for the next page
//
// Returns the pager
pager.next = function() {
page += 1
request()
return pager
}
// Decrements the page number by one and fires a request for the previous page
//
// Returns the pager
pager.prev = function() {
page -= 1
request()
return pager
}
// Determines if the server is reporting a next page of results
pager.hasnext = function() {
return hasnext;
}
// Determines if the server is reporting a previous page of results
pager.hasprev = function() {
return hasprev;
}
// Registers a callback for an event
//
// Supported events:
//
// * `success` - Request was successful
// * `error` - Request returned an error
// * `end` - Request is complete
//
// Returns a pager
pager.on = function(event, callback) {
if (typeof callbacks[event] == 'undefined')
callbacks[event] = []
callbacks[event].push(callback)
return pager
}
// Unregisters a previously registered callback
pager.off = function(event, callback) {
if (callbacks[event] instanceof Array) {
var cbacks = callbacks[event], i = 0
for (i; i < cbacks.length; i++) {
if (cbacks[i] === callback) {
cbacks.splice(i, 1)
break
}
}
}
return pager
}
// Triggers a custom event
pager.trigger = function(event, data) {
if (callbacks[event] instanceof Array) {
callbacks[event].forEach(function(callback) {
callback.call(pager, data)
})
}
return pager
}
// Sets a request header
pager.set = function(key, val) {
headers[key] = val
return pager
}
return pager
}
// Sets or gets the GitHub API host
// Uses https://api.github.com by default
//
// var gh = octo.api().host('https://api.github.com')
//
// Returns the api
api.host = function(val) {
if(!arguments.length) return host
host = val
return api
}
// Initializes a GET request to GitHub API v3
// Returns a pager
api.get = function(path, params) {
return new pager('get', path)
}
// Initializes a POST request to GitHub API v3
// Returns a pager
api.post = function(path, params) {
return new pager('post', path, params)
}
// Initializes a PATCH request to GitHub API v3
// Returns a pager
api.patch = function(path, params) {
return new pager('patch', path, params)
}
// Initializes a PUT request to GitHub API v3
// Returns a pager
api.put = function(path, params) {
return new pager('put', path, params)
}
// Initializes a DELETE request to GitHub API v3
// Returns a pager
api.delete = function(path, params) {
return new pager('delete', path, params)
}
// Returns the API rate limit as reported by GitHub
api.limit = function() {
return limit
}
// Returns the number of requests that can be made before the `limit` is reached
api.remaining = function() {
return remaining;
}
// Sets or gets the Basic Auth username
// Returns the api
api.username = function(v) {
if(!arguments.length) return username;
username = v
return api
}
// Sets or gets the Basic Auth password
// Returns the api
api.password = function(v) {
if(!arguments.length) return password;
password = v
return api
}
// Sets or gets an OAuth two token. You can temporarily use Basic Auth to create a
// GitHub Authorization which will grant you an OAuth token. You can use this token in
// your scripts
// Returns the api
api.token = function(v) {
if(!arguments.length) return token;
token = v
return api
}
return api
}
if("undefined" != typeof exports)
module.exports = octo
else
window.octo = octo
})()