Skip to content

Commit c5491da

Browse files
rarkinssindresorhus
authored andcommittedAug 27, 2018
Don't override authorization header if it already exists (#33)
1 parent d20f61c commit c5491da

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed
 

‎index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const create = () => got.create({
1414
methods: got.defaults.methods,
1515
handler: (options, next) => {
1616
if (options.token) {
17-
options.headers.authorization = `token ${options.token}`;
17+
options.headers.authorization = options.headers.authorization || `token ${options.token}`;
1818
}
1919

2020
if (options.method && options.method === 'PUT' && !options.body) {

‎readme.md

+27
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,33 @@ Type: `Object`
8989
Can be specified as a plain object and will be serialized as JSON with the appropriate headers set.
9090

9191

92+
## Authorization
93+
94+
Authorization for GitHub uses the following logic:
95+
96+
1. If `options.headers.authorization` is passed to `gh-got`, then this will be used as first preference.
97+
2. If `options.token` is provided, then the `authorization` header will be set to `token <options.token>`.
98+
3. If `options.headers.authorization` and `options.token` are not provided, then the `authorization` header will be set to `token <process.env.GITHUB_TOKEN>`
99+
100+
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:
101+
102+
```js
103+
const ghGot = require(`gh-got`);
104+
105+
(async () => {
106+
const options = {
107+
headers: {
108+
authorization: `Bearer ${jwt}`
109+
}
110+
};
111+
const {body} = await ghGot('app', options);
112+
113+
console.log(body.name);
114+
//=> 'MyApp'
115+
})();
116+
```
117+
118+
92119
## License
93120

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

0 commit comments

Comments
 (0)
Please sign in to comment.