Skip to content

Commit

Permalink
Update to Got 9, rename endpoint option, and require Node.js 8 (#29)
Browse files Browse the repository at this point in the history
Fixes #24 
Fixes #25
  • Loading branch information
szmarczak authored and sindresorhus committed Aug 23, 2018
1 parent a73bca8 commit 402f07d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 74 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
@@ -1,5 +1,4 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
87 changes: 31 additions & 56 deletions index.js
@@ -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://github.com/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://github.com/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;
9 changes: 4 additions & 5 deletions package.json
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -33,12 +33,11 @@
"utility"
],
"dependencies": {
"got": "^8.0.0",
"is-plain-obj": "^1.1.0"
"got": "^9.1.0"
},
"devDependencies": {
"ava": "*",
"get-stream": "^3.0.0",
"ava": "^1.0.0-beta.7",
"get-stream": "^4.0.0",
"nock": "^9.1.0",
"xo": "*"
}
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -73,7 +73,7 @@ GitHub [access token](https://github.com/settings/tokens/new).

Can be set globally with the `GITHUB_TOKEN` environment variable.

### endpoint
### baseUrl

Type: `string`<br>
Default: `https://api.github.com/`
Expand Down
20 changes: 10 additions & 10 deletions test.js
Expand Up @@ -27,26 +27,26 @@ test('dedupes slashes', async t => {

test.serial('global token option', async t => {
process.env.GITHUB_TOKEN = 'fail';
await t.throws(m('users/sindresorhus'), 'Bad credentials (401)');
await t.throwsAsync(m.recreate()('users/sindresorhus'), 'Bad credentials (401)');
process.env.GITHUB_TOKEN = token;
});

test('token option', async t => {
await t.throws(m('users/sindresorhus', {token: 'fail'}), 'Bad credentials (401)');
await t.throwsAsync(m('users/sindresorhus', {token: 'fail'}), 'Bad credentials (401)');
});

test.serial('global endpoint option', async t => {
process.env.GITHUB_ENDPOINT = 'fail';
await t.throws(m('users/sindresorhus', {retries: 1}), /ENOTFOUND/);
await t.throwsAsync(m.recreate()('users/sindresorhus', {retries: 1}), 'Invalid URL: fail/');
delete process.env.GITHUB_ENDPOINT;
});

test.serial('endpoint option', async t => {
process.env.GITHUB_ENDPOINT = 'https://api.github.com/';
await t.throws(m('users/sindresorhus', {
endpoint: 'fail',
await t.throwsAsync(m.recreate()('users/sindresorhus', {
baseUrl: 'fail',
retries: 1
}), /ENOTFOUND/);
}), 'Invalid URL: fail/');
delete process.env.GITHUB_ENDPOINT;
});

Expand All @@ -56,18 +56,18 @@ test('stream interface', async t => {
});

test('json body', async t => {
const endpoint = 'http://mock-endpoint';
const baseUrl = 'http://mock-endpoint';
const body = {test: [1, 3, 3, 7]};
const reply = {ok: true};

const scope = nock(endpoint).post('/test', body).reply(200, reply);
const scope = nock(baseUrl).post('/test', body).reply(200, reply);

t.deepEqual((await m('/test', {endpoint, body})).body, reply);
t.deepEqual((await m('/test', {baseUrl, body})).body, reply);
t.truthy(scope.isDone());
});

test('custom error', async t => {
const err = await t.throws(m('users/sindresorhus', {token: 'fail'}));
const err = await t.throwsAsync(m('users/sindresorhus', {token: 'fail'}));
t.is(err.name, 'GitHubError');
t.is(err.message, 'Bad credentials (401)');
});

0 comments on commit 402f07d

Please sign in to comment.