Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 19, 2015
0 parents commit a0e406f
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
12 changes: 12 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"node": true,
"esnext": true,
"bitwise": true,
"curly": true,
"immed": true,
"newcap": true,
"noarg": true,
"undef": true,
"unused": "vars",
"strict": true
}
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sudo: false
language: node_js
node_js:
- 'iojs'
- '0.12'
- '0.10'
50 changes: 50 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';
var got = require('got');
var objectAssign = require('object-assign');

function ghGot(path, opts, cb) {
if (!path) {
throw new Error('path required');
}

if (typeof opts !== 'object') {
cb = opts;
opts = {};
}

cb = cb || function () {};
opts = objectAssign({}, opts, {json: true});

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

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

if (token) {
opts.headers['authorization'] = 'token ' + token;
}

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

return got(url, opts, cb);
};

[
'get',
'post',
'put',
'patch',
'head',
'delete'
].forEach(function (el) {
ghGot[el] = function (url, opts, cb) {
return ghGot(url, objectAssign({}, opts, {method: el.toUpperCase()}), cb);
};
});

module.exports = ghGot;
21 changes: 21 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
42 changes: 42 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "gh-got",
"version": "0.0.0",
"description": "Convenience wrapper for `got` to interact with the GitHub API",
"license": "MIT",
"repository": "sindresorhus/gh-got",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "node test.js"
},
"files": [
"index.js"
],
"keywords": [
"got",
"gh",
"github",
"api",
"request",
"http",
"https",
"get",
"url",
"uri",
"util",
"utility"
],
"dependencies": {
"got": "^2.7.2",
"object-assign": "^2.0.0"
},
"devDependencies": {
"ava": "0.0.4"
}
}
69 changes: 69 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# gh-got [![Build Status](https://travis-ci.org/sindresorhus/gh-got.svg?branch=master)](https://travis-ci.org/sindresorhus/gh-got)

> Convenience wrapper for [`got`](https://github.com/sindresorhus/got) to interact with the [GitHub API](https://developer.github.com/v3/)

## Install

```
$ npm install --save gh-got
```


## Usage

Instead of:

```js
var got = require('got');
var token = 'foo';

got('https://api.github.com/users/sindresorhus', {
json: true
headers: {
'accept': 'application/vnd.github.v3+json',
'authorization': 'token ' + token;
}
}, function (err, data) {
console.log(data.login);
//=> 'sindresorhus'
});
```

You can do:

```js
var ghGot = require('gh-got');

ghGot('users/sindresorhus', {token: 'foo'}, function (err, data) {
console.log(data.login);
//=> 'sindresorhus'
});
```


## API

Same as [`got`](https://github.com/sindresorhus/got), but with some additional options:

### token

Type: `string`

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

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

### endpoint

Type: `string`
Default: `https://api.github.com/`

To support [GitHub Enterprise](https://enterprise.github.com).

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


## License

MIT © [Sindre Sorhus](http://sindresorhus.com)
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
var test = require('ava');
var ghGot = require('./');

test(function (t) {
t.plan(1);

ghGot('users/sindresorhus', function (err, data) {
t.assert(data.login === 'sindresorhus');
});
});

0 comments on commit a0e406f

Please sign in to comment.