-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to Got 9, rename
endpoint
option, and require Node.js 8 (#29)
- Loading branch information
1 parent
a73bca8
commit 402f07d
Showing
5 changed files
with
47 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
language: node_js | ||
node_js: | ||
- '10' | ||
- '8' | ||
- '6' | ||
- '4' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,43 @@ | ||
'use strict'; | ||
const got = require('got'); | ||
const isPlainObj = require('is-plain-obj'); | ||
|
||
function ghGot(path, opts) { | ||
if (typeof path !== 'string') { | ||
return Promise.reject(new TypeError(`Expected \`path\` to be a string, got ${typeof path}`)); | ||
} | ||
|
||
const env = process.env; | ||
|
||
opts = Object.assign({ | ||
const create = () => got.create({ | ||
options: got.mergeOptions(got.defaults.options, { | ||
json: true, | ||
token: env.GITHUB_TOKEN, | ||
endpoint: env.GITHUB_ENDPOINT ? env.GITHUB_ENDPOINT : 'https://api.github.com/' | ||
}, opts); | ||
|
||
opts.headers = Object.assign({ | ||
accept: 'application/vnd.github.v3+json', | ||
'user-agent': 'https:/sindresorhus/gh-got' | ||
}, opts.headers); | ||
|
||
if (opts.token) { | ||
opts.headers.authorization = `token ${opts.token}`; | ||
} | ||
|
||
// https://developer.github.com/v3/#http-verbs | ||
if (opts.method && opts.method.toLowerCase() === 'put' && !opts.body) { | ||
opts.headers['content-length'] = 0; | ||
} | ||
|
||
const url = /^https?/.test(path) ? path : opts.endpoint.replace(/\/$/, '') + '/' + path.replace(/^\//, ''); | ||
token: process.env.GITHUB_TOKEN, | ||
baseUrl: process.env.GITHUB_ENDPOINT || 'https://api.github.com', | ||
headers: { | ||
accept: 'application/vnd.github.v3+json', | ||
'user-agent': 'https:/sindresorhus/gh-got' | ||
} | ||
}), | ||
methods: got.defaults.methods, | ||
handler: (options, next) => { | ||
if (options.token) { | ||
options.headers.authorization = `token ${options.token}`; | ||
} | ||
|
||
if (opts.stream) { | ||
return got.stream(url, opts); | ||
} | ||
if (options.method && options.method === 'PUT' && !options.body) { | ||
options.headers['content-length'] = 0; | ||
} | ||
|
||
return got(url, opts).catch(err => { | ||
if (err.response && isPlainObj(err.response.body)) { | ||
err.name = 'GitHubError'; | ||
err.message = `${err.response.body.message} (${err.statusCode})`; | ||
if (options.stream) { | ||
return next(options); | ||
} | ||
|
||
throw err; | ||
}); | ||
} | ||
return next(options).catch(err => { | ||
if (err.response && err.response.body) { | ||
err.name = 'GitHubError'; | ||
err.message = `${err.response.body.message} (${err.statusCode})`; | ||
} | ||
|
||
const helpers = [ | ||
'get', | ||
'post', | ||
'put', | ||
'patch', | ||
'head', | ||
'delete' | ||
]; | ||
throw err; | ||
}); | ||
} | ||
}); | ||
|
||
ghGot.stream = (url, opts) => ghGot(url, Object.assign({}, opts, { | ||
json: false, | ||
stream: true | ||
})); | ||
module.exports = create(); | ||
|
||
for (const x of helpers) { | ||
const method = x.toUpperCase(); | ||
ghGot[x] = (url, opts) => ghGot(url, Object.assign({}, opts, {method})); | ||
ghGot.stream[x] = (url, opts) => ghGot.stream(url, Object.assign({}, opts, {method})); | ||
if (process.env.NODE_ENV === 'test') { | ||
module.exports.recreate = create; | ||
} | ||
|
||
module.exports = ghGot; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters