Skip to content

Commit

Permalink
[breaking] now requires Node.js >=4
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 6, 2016
1 parent b265f49 commit 1fc6d02
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 48 deletions.
3 changes: 0 additions & 3 deletions .editorconfig
Expand Up @@ -10,6 +10,3 @@ insert_final_newline = true
[{package.json,*.yml}]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
42 changes: 16 additions & 26 deletions index.js
@@ -1,34 +1,32 @@
'use strict';
var got = require('got');
var objectAssign = require('object-assign');
var Promise = require('pinkie-promise');
const got = require('got');

function ghGot(path, opts) {
if (typeof path !== 'string') {
return Promise.reject(new TypeError('Path should be a string'));
return Promise.reject(new TypeError(`Expected 'path' to be a string, got ${typeof path}`));
}

opts = objectAssign({json: true, endpoint: 'https://api.github.com/'}, opts);
opts = Object.assign({json: true, endpoint: 'https://api.github.com/'}, opts);

opts.headers = objectAssign({
opts.headers = Object.assign({
'accept': 'application/vnd.github.v3+json',
'user-agent': 'https://github.com/sindresorhus/gh-got'
}, opts.headers);

var env = process.env;
var token = env.GITHUB_TOKEN || opts.token;
const env = process.env;
const token = env.GITHUB_TOKEN || opts.token;

This comment has been minimized.

Copy link
@millette

millette Apr 9, 2016

I was writing a tiny cli to fetch current github rate limits for a user (given a token as an argument) but with the environment variable defined, I can't override it unless I clear it first. In other words, wouldn't this be preferable?

const token = opts.token || env.GITHUB_TOKEN;

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Apr 9, 2016

Author Owner

I guess. Can't really remember why I did it like this.


if (token) {
opts.headers.authorization = 'token ' + token;
opts.headers.authorization = `token ${token}`;
}

// https://developer.github.com/v3/#http-verbs
if (opts.method && opts.method.toLowerCase() === 'put' && !opts.body) {
opts.headers['content-length'] = 0;
}

var endpoint = env.GITHUB_ENDPOINT ? env.GITHUB_ENDPOINT.replace(/[^/]$/, '$&/') : opts.endpoint;
var url = /https?/.test(path) ? path : endpoint + path;
const endpoint = env.GITHUB_ENDPOINT ? env.GITHUB_ENDPOINT.replace(/[^/]$/, '$&/') : opts.endpoint;
const url = /https?/.test(path) ? path : endpoint + path;

if (opts.stream) {
return got.stream(url, opts);
Expand All @@ -37,7 +35,7 @@ function ghGot(path, opts) {
return got(url, opts);
}

var helpers = [
const helpers = [
'get',
'post',
'put',
Expand All @@ -46,20 +44,12 @@ var helpers = [
'delete'
];

helpers.forEach(function (el) {
ghGot[el] = function (url, opts) {
return ghGot(url, objectAssign({}, opts, {method: el.toUpperCase()}));
};
});
ghGot.stream = (url, opts) => ghGot(url, Object.assign({}, opts, {json: false, stream: true}));

ghGot.stream = function (url, opts) {
return ghGot(url, objectAssign({}, opts, {json: false, stream: true}));
};

helpers.forEach(function (el) {
ghGot.stream[el] = function (url, opts) {
return ghGot.stream(url, objectAssign({}, opts, {method: el.toUpperCase()}));
};
});
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}));
}

module.exports = ghGot;
11 changes: 6 additions & 5 deletions package.json
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -33,13 +33,14 @@
"utility"
],
"dependencies": {
"got": "^5.2.0",
"object-assign": "^4.0.1",
"pinkie-promise": "^2.0.0"
"got": "^6.2.0"
},
"devDependencies": {
"ava": "*",
"get-stream": "^1.0.0",
"get-stream": "^2.0.0",
"xo": "*"
},
"xo": {
"esnext": true
}
}
6 changes: 3 additions & 3 deletions readme.md
Expand Up @@ -22,7 +22,7 @@ got('https://api.github.com/users/sindresorhus', {
json: true,
headers: {
'accept': 'application/vnd.github.v3+json',
'authorization': 'token ' + token
'authorization': `token ${token}`
}
}).then(res => {
console.log(res.body.login);
Expand Down Expand Up @@ -67,7 +67,7 @@ Can be overridden globally with the `GITHUB_TOKEN` environment variable.

### endpoint

Type: `string`
Type: `string`<br>
Default: `https://api.github.com/`

To support [GitHub Enterprise](https://enterprise.github.com).
Expand All @@ -77,4 +77,4 @@ Can be overridden globally with the `GITHUB_ENDPOINT` environment variable.

## License

MIT © [Sindre Sorhus](http://sindresorhus.com)
MIT © [Sindre Sorhus](https://sindresorhus.com)
20 changes: 9 additions & 11 deletions test.js
@@ -1,34 +1,32 @@
import test from 'ava';
import getStream from 'get-stream';
import fn from './';

global.Promise = Promise;
import m from './';

const token = process.env.GITHUB_TOKEN;

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

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

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

test.serial('token option', async t => {
process.env.GITHUB_TOKEN = 'fail';
await t.throws(fn('users/sindresorhus'), 'Response code 401 (Unauthorized)');
await t.throws(m('users/sindresorhus'), 'Response code 401 (Unauthorized)');
process.env.GITHUB_TOKEN = token;
});

test('endpoint option', async t => {
await t.throws(fn('users/sindresorhus', {endpoint: 'fail', retries: 1}), /ENOTFOUND/);
await t.throws(m('users/sindresorhus', {endpoint: 'fail', retries: 1}), /ENOTFOUND/);
});

test('stream interface', async t => {
t.is(JSON.parse(await getStream(fn.stream('users/sindresorhus'))).login, 'sindresorhus');
t.is(JSON.parse(await getStream(fn.stream.get('users/sindresorhus'))).login, 'sindresorhus');
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');
});

0 comments on commit 1fc6d02

Please sign in to comment.