|
1 | 1 | 'use strict';
|
2 | 2 | const got = require('got');
|
3 |
| -const isPlainObj = require('is-plain-obj'); |
4 | 3 |
|
5 |
| -function ghGot(path, opts) { |
6 |
| - if (typeof path !== 'string') { |
7 |
| - return Promise.reject(new TypeError(`Expected \`path\` to be a string, got ${typeof path}`)); |
8 |
| - } |
9 |
| - |
10 |
| - const env = process.env; |
11 |
| - |
12 |
| - opts = Object.assign({ |
| 4 | +const create = () => got.create({ |
| 5 | + options: got.mergeOptions(got.defaults.options, { |
13 | 6 | json: true,
|
14 |
| - token: env.GITHUB_TOKEN, |
15 |
| - endpoint: env.GITHUB_ENDPOINT ? env.GITHUB_ENDPOINT : 'https://api.github.com/' |
16 |
| - }, opts); |
17 |
| - |
18 |
| - opts.headers = Object.assign({ |
19 |
| - accept: 'application/vnd.github.v3+json', |
20 |
| - 'user-agent': 'https://github.com/sindresorhus/gh-got' |
21 |
| - }, opts.headers); |
22 |
| - |
23 |
| - if (opts.token) { |
24 |
| - opts.headers.authorization = `token ${opts.token}`; |
25 |
| - } |
26 |
| - |
27 |
| - // https://developer.github.com/v3/#http-verbs |
28 |
| - if (opts.method && opts.method.toLowerCase() === 'put' && !opts.body) { |
29 |
| - opts.headers['content-length'] = 0; |
30 |
| - } |
31 |
| - |
32 |
| - const url = /^https?/.test(path) ? path : opts.endpoint.replace(/\/$/, '') + '/' + path.replace(/^\//, ''); |
| 7 | + token: process.env.GITHUB_TOKEN, |
| 8 | + baseUrl: process.env.GITHUB_ENDPOINT || 'https://api.github.com', |
| 9 | + headers: { |
| 10 | + accept: 'application/vnd.github.v3+json', |
| 11 | + 'user-agent': 'https://github.com/sindresorhus/gh-got' |
| 12 | + } |
| 13 | + }), |
| 14 | + methods: got.defaults.methods, |
| 15 | + handler: (options, next) => { |
| 16 | + if (options.token) { |
| 17 | + options.headers.authorization = `token ${options.token}`; |
| 18 | + } |
33 | 19 |
|
34 |
| - if (opts.stream) { |
35 |
| - return got.stream(url, opts); |
36 |
| - } |
| 20 | + if (options.method && options.method === 'PUT' && !options.body) { |
| 21 | + options.headers['content-length'] = 0; |
| 22 | + } |
37 | 23 |
|
38 |
| - return got(url, opts).catch(err => { |
39 |
| - if (err.response && isPlainObj(err.response.body)) { |
40 |
| - err.name = 'GitHubError'; |
41 |
| - err.message = `${err.response.body.message} (${err.statusCode})`; |
| 24 | + if (options.stream) { |
| 25 | + return next(options); |
42 | 26 | }
|
43 | 27 |
|
44 |
| - throw err; |
45 |
| - }); |
46 |
| -} |
| 28 | + return next(options).catch(err => { |
| 29 | + if (err.response && err.response.body) { |
| 30 | + err.name = 'GitHubError'; |
| 31 | + err.message = `${err.response.body.message} (${err.statusCode})`; |
| 32 | + } |
47 | 33 |
|
48 |
| -const helpers = [ |
49 |
| - 'get', |
50 |
| - 'post', |
51 |
| - 'put', |
52 |
| - 'patch', |
53 |
| - 'head', |
54 |
| - 'delete' |
55 |
| -]; |
| 34 | + throw err; |
| 35 | + }); |
| 36 | + } |
| 37 | +}); |
56 | 38 |
|
57 |
| -ghGot.stream = (url, opts) => ghGot(url, Object.assign({}, opts, { |
58 |
| - json: false, |
59 |
| - stream: true |
60 |
| -})); |
| 39 | +module.exports = create(); |
61 | 40 |
|
62 |
| -for (const x of helpers) { |
63 |
| - const method = x.toUpperCase(); |
64 |
| - ghGot[x] = (url, opts) => ghGot(url, Object.assign({}, opts, {method})); |
65 |
| - ghGot.stream[x] = (url, opts) => ghGot.stream(url, Object.assign({}, opts, {method})); |
| 41 | +if (process.env.NODE_ENV === 'test') { |
| 42 | + module.exports.recreate = create; |
66 | 43 | }
|
67 |
| - |
68 |
| -module.exports = ghGot; |
0 commit comments