Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't override authorization header if it already exists #33

Merged
merged 4 commits into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const create = () => got.create({
methods: got.defaults.methods,
handler: (options, next) => {
if (options.token) {
options.headers.authorization = `token ${options.token}`;
options.headers.authorization = options.headers.authorization || `token ${options.token}`;
}

if (options.method && options.method === 'PUT' && !options.body) {
Expand Down
27 changes: 27 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,33 @@ Type: `Object`
Can be specified as a plain object and will be serialized as JSON with the appropriate headers set.


## Authorization

Authorization for GitHub uses the following logic:

1. If `options.headers.authorization` is passed to `gh-got`, then this will be used as first preference.
2. If `options.token` is provided, then the `authorization` header will be set to `token <options.token>`.
3. If `options.headers.authorization` and `options.token` are not provided, then the `authorization` header will be set to `token <process.env.GITHUB_TOKEN>`

In most cases, this means you can simply set `GITHUB_TOKEN`, but it also allows it to be overridden by setting `options.token` or `options.headers.authorization` explicitly. For example, if [authenticating as a GitHub App](https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#authenticating-as-a-github-app), you could do the following:

```js
const ghGot = require(`gh-got`);

(async () => {
const options = {
headers: {
authorization: `Bearer ${jwt}`
}
};
const {body} = await ghGot('app', options);

console.log(body.name);
//=> 'MyApp'
})();
```


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)