Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Dec 31, 2018
1 parent a66fa03 commit 11e621b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const create = () => got.create({
'user-agent': 'https://github.com/sindresorhus/gh-got'
}
}),

methods: got.defaults.methods,

handler: (options, next) => {
if (options.token) {
options.headers.authorization = options.headers.authorization || `token ${options.token}`;
Expand All @@ -21,13 +23,13 @@ const create = () => got.create({
return next(options);
}

return next(options).catch(err => {
if (err.response && err.response.body) {
err.name = 'GitHubError';
err.message = `${err.response.body.message} (${err.statusCode})`;
return next(options).catch(error => {
if (error.response && error.response.body) {
error.name = 'GitHubError';
error.message = `${error.response.body.message} (${error.statusCode})`;
}

throw err;
throw error;
});
}
});
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
"utility"
],
"dependencies": {
"got": "^9.2.0"
"got": "^9.5.0"
},
"devDependencies": {
"ava": "^1.0.0-beta.7",
"get-stream": "^4.0.0",
"nock": "^9.1.0",
"xo": "*"
"ava": "^1.0.1",
"get-stream": "^4.1.0",
"nock": "^10.0.5",
"xo": "^0.23.0"
}
}
10 changes: 6 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,21 @@ const ghGot = require('gh-got');

## API

Same as [`got`](https://github.com/sindresorhus/got) (including the stream API and aliases), but with some additional options below.
Same API as [`got`](https://github.com/sindresorhus/got), including the stream API and aliases, but with some additional options below.

Errors are improved by using the custom GitHub error messages. Doesn't apply to the stream API.

### token
### `gh-got` specific options

#### token

Type: `string`

GitHub [access token](https://github.com/settings/tokens/new).

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

### baseUrl
#### baseUrl

Type: `string`<br>
Default: `https://api.github.com/`
Expand All @@ -82,7 +84,7 @@ To support [GitHub Enterprise](https://enterprise.github.com).

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

### body
#### body

Type: `Object`

Expand Down
33 changes: 17 additions & 16 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
import test from 'ava';
import nock from 'nock';
import getStream from 'get-stream';
import m from '.';
import ghGot from '.';

const token = process.env.GITHUB_TOKEN;

test('default', async t => {
t.is((await m('users/sindresorhus')).body.login, 'sindresorhus');
t.is((await ghGot('users/sindresorhus')).body.login, 'sindresorhus');
});

test('full path', async t => {
t.is((await m('https://api.github.com/users/sindresorhus')).body.login, 'sindresorhus');
t.is((await ghGot('https://api.github.com/users/sindresorhus')).body.login, 'sindresorhus');
});

test('accepts options', async t => {
t.is((await m('users/sindresorhus', {})).body.login, 'sindresorhus');
t.is((await ghGot('users/sindresorhus', {})).body.login, 'sindresorhus');
});

test('accepts options.endpoint without trailing slash', async t => {
t.is((await m('users/sindresorhus', {endpoint: 'https://api.github.com'})).body.login, 'sindresorhus');
t.is((await ghGot('users/sindresorhus', {endpoint: 'https://api.github.com'})).body.login, 'sindresorhus');
});

test('dedupes slashes', async t => {
t.is((await m('/users/sindresorhus', {endpoint: 'https://api.github.com/'})).body.login, 'sindresorhus');
t.is((await ghGot('/users/sindresorhus', {endpoint: 'https://api.github.com/'})).body.login, 'sindresorhus');
});

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

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

test.serial('global endpoint option', async t => {
process.env.GITHUB_ENDPOINT = 'fail';
await t.throwsAsync(m.recreate()('users/sindresorhus', {retries: 1}), 'Invalid URL: fail/');
await t.throwsAsync(ghGot.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.throwsAsync(m.recreate()('users/sindresorhus', {
await t.throwsAsync(ghGot.recreate()('users/sindresorhus', {
baseUrl: 'fail',
retries: 1
}), 'Invalid URL: fail/');
delete process.env.GITHUB_ENDPOINT;
});

test('stream interface', async t => {
t.is(JSON.parse(await getStream(m.stream('users/sindresorhus'))).login, 'sindresorhus');
t.is(JSON.parse(await getStream(m.stream.get('users/sindresorhus'))).login, 'sindresorhus');
t.is(JSON.parse(await getStream(ghGot.stream('users/sindresorhus'))).login, 'sindresorhus');
t.is(JSON.parse(await getStream(ghGot.stream.get('users/sindresorhus'))).login, 'sindresorhus');
});

test('json body', async t => {
Expand All @@ -62,12 +62,13 @@ test('json body', async t => {

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

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

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

0 comments on commit 11e621b

Please sign in to comment.