forked from jonschlinkert/github-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
232 lines (209 loc) · 5.69 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
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
'use strict';
var use = require('use');
var utils = require('./lib/utils');
/**
* Create an instance of `GitHub` with the given options.
*
* ```js
* var GitHub = require('github-base');
* var github = new GitHub(options);
* ```
*
* @param {Object} `options`
* @api public
*/
function GitHub(options) {
if (!(this instanceof GitHub)) {
var proto = Object.create(GitHub.prototype);
GitHub.apply(proto, arguments);
return proto;
}
use(this);
this.defaults = utils.defaults(options);
}
/**
* GitHub prototype methods
*/
GitHub.prototype = {
constructor: GitHub,
/**
* Uses [simple-get][] to make a single request to the GitHub API, based on
* the provided settings. Supports any of the GitHub API VERBs:
*
* - `GET`
* - `PUT`
* - `POST`
* - `DELETE`
* - `PATCH`
*
* ```js
* //example..request
* github.request('GET', '/user/orgs', function (err, res) {
* //=> array of orgs
* });
* ```
* @name .request
* @param {String} `method` The http VERB to use
* @param {String} `url` GitHub API URL to use.
* @param {Options} `options` Request options.
* @param {Function} `cb`
* @api public
*/
request: function(method, path, options, cb) {
if (typeof options === 'function') {
return this.request.call(this, method, path, {}, options);
}
if (typeof cb !== 'function') {
throw new TypeError('expected callback to be a function');
}
utils.request(this.defaults(method, path, options), cb);
},
/**
* Makes a single `GET` request to the GitHub API based on the
* provided settings.
*
* ```js
* // get orgs for the authenticated user
* github.get('/user/orgs', function (err, res) {
* //=> array of orgs
* });
*
* // get gists for the authenticated user
* github.get('/gists', function (err, res) {
* //=> array of gists
* });
* ```
* @name .get
* @param {String} `path` path to append to the GitHub API URL.
* @param {Options} `options` Request options.
* @param {Function} `cb`
* @api public
*/
get: function(path, options, cb) {
this.request('GET', path, options, cb);
},
/**
* Performs a request using [simple-get][], and then if necessary requests
* additional paged content based on the response. Data from all pages are
* concatenated together and buffered until the last page of data has been retrieved.
*
* ```js
* // get all repos for the authenticated user
* var url = '/user/repos?type=all&per_page=1000&sort=updated';
* github.paged(url, function(err, res) {
* console.log(res);
* });
* ```
* @name .paged
* @param {String} `path` path to append to the GitHub API URL.
* @param {Function} `cb`
* @api public
*/
paged: function(path, options, cb) {
if (typeof options === 'function') {
this.paged.call(this, path, {}, options);
return;
}
utils.paged(this.defaults('GET', path, options), cb);
},
/**
* Makes a single `DELETE` request to the GitHub API based on the
* provided settings.
*
* ```js
* // un-follow someone
* github.del('/user/following/someoneelse', function(err, res) {
* console.log(res);
* });
* ```
* @name .del
* @param {String} `path` path to append to the GitHub API URL.
* @param {Options} `options` Request options.
* @param {Function} `cb`
* @api public
*/
del: function(path, options, cb) {
this.request('DELETE', path, options, cb);
},
/**
* Makes a single `PATCH` request to the GitHub API based on the
* provided settings.
*
* ```js
* // update a gist
* var fs = require('fs');
* var opts = {files: {'readme.md': { content: '# My Readme...' }}};
* github.patch('/gists/bd139161a425896f35f8', opts, function(err, res) {
* console.log(err, res);
* });
* ```
* @name .patch
* @param {String} `path` path to append to the GitHub API URL.
* @param {Options} `options` Request options.
* @param {Function} `cb`
* @api public
*/
patch: function(path, options, cb) {
this.request('PATCH', path, options, cb);
},
/**
* Makes a single `POST` request to the GitHub API based on the
* provided settings.
*
* ```js
* // create a new repo
* var opts = { name: 'new-repo-name' };
* github.post('/user/repos', opts, function(err, res) {
* console.log(res);
* });
* ```
* @name .post
* @param {String} `path` path to append to the GitHub API URL.
* @param {Options} `options` Request options.
* @param {Function} `cb`
* @api public
*/
post: function(path, options, cb) {
this.request('POST', path, options, cb);
},
/**
* Makes a single `PUT` request to the GitHub API based on the provided
* settings.
*
* ```js
* // follow someone
* github.put('/user/following/jonschlinkert', function(err, res) {
* console.log(res);
* });
* ```
* @name .put
* @param {String} `path` path to append to the GitHub API URL.
* @param {Options} `options` Request options.
* @param {Function} `cb`
* @api public
*/
put: function(path, options, cb) {
this.request('PUT', path, options, cb);
}
};
/**
* Static method for inheriting the prototype and static methods of the `Base` class.
* This method greatly simplifies the process of creating inheritance-based applications.
* See [static-extend][] for more details.
*
* ```js
* var GitHub = require('github-base');
* function MyApp() {
* GitHub.call(this);
* }
* GitHub.extend(MyApp);
* ```
* @name #extend
* @param {Function} `Ctor` constructor to extend
* @api public
*/
utils.define(GitHub, 'extend', utils.staticExtend(GitHub));
/**
* Expose `GitHub`
*/
module.exports = GitHub;