-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
178 lines (159 loc) · 4.51 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
'use strict';
const use = require('use');
const utils = require('./lib/utils');
/**
* Create an instance of `GitHub` with the given options.
*
* ```js
* const GitHub = require('github-base');
* const github = new GitHub([options]);
* ```
* @name GitHub
* @param {Object} `options`
* @api public
*/
class GitHub {
constructor(options = {}) {
this.options = options;
use(this);
}
/**
* Uses [needle][] to make a request to the GitHub API. Supports the following verbs:
* `GET`, `PUT`, `POST`, `PATCH`, and `DELETE`. Takes a callback or returns a promise.
*
* ```js
* // list all orgs for the authenticated user
* const auth = require('./local-private-auth-info');
* const github = new GitHub(auth);
* github.request('GET', '/user/orgs')
* .then(res => console.log(res.body));
* ```
* @name .request
* @param {String} `method` The http VERB to use
* @param {String} `path` The path to append to the base GitHub API URL.
* @param {Options} `options` Request [options](#options).
* @api public
*/
request(method, path, options) {
return utils.request(method, path, { ...this.options, ...options });
}
/**
* Make a `GET` request to the GitHub API.
*
* ```js
* // get a list of orgs for the authenticated user
* github.get('/user/orgs')
* .then(res => console.log(res.body));
*
* // get gists for the authenticated user
* github.get('/gists')
* .then(res => console.log(res.body));
* ```
* @name .get
* @param {String} `path` The path to append to the base GitHub API URL.
* @param {Options} `options` Request [options](#options).
* @api public
*/
get(path, options) {
return this.request('GET', path, options);
}
/**
* Make a `DELETE` request to the GitHub API.
*
* ```js
* // un-follow someone
* github.delete('/user/following/:some_username', { some_username: 'whatever' })
* .then(res => {
* console.log('RESPONSE:', res);
* });
* ```
* @name .delete
* @param {String} `path` The path to append to the base GitHub API URL.
* @param {Options} `options` Request [options](#options).
* @api public
*/
delete(path, options) {
return this.request('DELETE', path, options);
}
/**
* Make a `PATCH` request to the GitHub API.
*
* ```js
* // update a gist
* const fs = require('fs');
* const options = { files: { 'readme.md': { content: fs.readFileSync('README.md', 'utf8') } } };
* github.patch('/gists/bd139161a425896f35f8', options)
* .then(res => {
* console.log('RESPONSE:', res);
* });
* ```
* @name .patch
* @param {String} `path` The path to append to the base GitHub API URL.
* @param {Options} `options` Request [options](#options).
* @api public
*/
patch(path, options) {
return this.request('PATCH', path, options);
}
/**
* Make a `POST` request to the GitHub API.
*
* ```js
* // create a new repository
* github.post('/user/repos', { name: 'new-repo-name' })
* .then(res => {
* console.log('RESPONSE:', res);
* });
* ```
* @name .post
* @param {String} `path` The path to append to the base GitHub API URL.
* @param {Options} `options` Request [options](#options).
* @api public
*/
post(path, options) {
return this.request('POST', path, options);
}
/**
* Make a `PUT` request to the GitHub API.
*
* ```js
* // follow someone
* github.put('/user/following/jonschlinkert')
* .then(res => {
* console.log('RESPONSE:', res);
* });
* ```
* @name .put
* @param {String} `path` The path to append to the base GitHub API URL.
* @param {Options} `options` Request [options](#options).
* @api public
*/
put(path, options) {
return this.request('PUT', path, options);
}
/**
* Recursively make GET requests until all pages of records are returned.
*
* ```js
* // get all repos for the authenticated user
* github.paged('/user/repos?type=all&per_page=1000&sort=updated')
* .then(res => console.log(res.pages))
* .catch(console.error)
* ```
* @name .paged
* @param {String} `path` The path to append to the base GitHub API URL.
* @param {Options} `options` Request [options](#options).
* @api public
*/
paged(path, options, next) {
if (typeof options === 'function') {
next = options;
options = null;
}
return utils.paged('GET', path, { ...this.options, ...options, next });
}
}
/**
* Expose `GitHub`
*/
module.exports = GitHub;