Skip to content

Commit

Permalink
Require Node.js 14 and move to ESM
Browse files Browse the repository at this point in the history
Fixes #42
  • Loading branch information
sindresorhus committed Oct 18, 2022
1 parent 2902a01 commit 24abafe
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 111 deletions.
1 change: 0 additions & 1 deletion .github/funding.yml

This file was deleted.

8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 18
- 16
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
68 changes: 40 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,51 @@
'use strict';
const got = require('got');
import process from 'node:process';
import got from 'got';

const getRateLimit = headers => ({
limit: parseInt(headers['x-ratelimit-limit'], 10),
remaining: parseInt(headers['x-ratelimit-remaining'], 10),
reset: new Date(parseInt(headers['x-ratelimit-reset'], 10) * 1000)
limit: Number.parseInt(headers['x-ratelimit-limit'], 10),
remaining: Number.parseInt(headers['x-ratelimit-remaining'], 10),
reset: new Date(Number.parseInt(headers['x-ratelimit-reset'], 10) * 1000),
});

const create = () => got.extend({
prefixUrl: process.env.GITHUB_ENDPOINT || 'https://api.github.com',
headers: {
accept: 'application/vnd.github.v3+json',
'user-agent': 'https://github.com/sindresorhus/gh-got'
'user-agent': 'https://github.com/sindresorhus/gh-got',
},
responseType: 'json',
token: process.env.GITHUB_TOKEN,
context: {
token: process.env.GITHUB_TOKEN,
},
hooks: {
init: [
(raw, options) => {
// TODO: This should be fixed in Got.
// TODO: This doesn't seem to have any effect.
if (typeof options.url === 'string' && options.url.startsWith('/')) {
options.url = options.url.slice(1);
}

if ('token' in raw) {
options.context.token = raw.token;
delete raw.token;
}
},
],
},
handlers: [
(options, next) => {
// Authorization
if (options.token && !options.headers.authorization) {
options.headers.authorization = `token ${options.token}`;
// TODO: This should be fixed in Got
// TODO: This doesn't seem to have any effect.
if (typeof options.url === 'string' && options.url.startsWith('/')) {
options.url = options.url.slice(1);
}

// `options.body` -> `options.json`
options.json = options.body;
delete options.body;
// Authorization
const {token} = options.context;
if (token && !options.headers.authorization) {
options.headers.authorization = `token ${token}`;
}

// Don't touch streams
if (options.isStream) {
Expand All @@ -46,7 +67,7 @@ const create = () => got.extend({
// Nicer errors
if (response && response.body) {
error.name = 'GitHubError';
error.message = `${response.body.message} (${error.response.statusCode})`;
error.message = `${response.body.message} (${response.statusCode})`;
}

// Rate limit for errors
Expand All @@ -57,23 +78,14 @@ const create = () => got.extend({
throw error;
}
})();
}
},
],
hooks: {
init: [
options => {
// TODO: This should be fixed in Got
// Remove leading slashes
if (typeof options.url === 'string' && options.url.startsWith('/')) {
options.url = options.url.slice(1);
}
}
]
}
});

module.exports = create();
const ghGot = create();

export default ghGot;

if (process.env.NODE_ENV === 'test') {
module.exports.recreate = create;
ghGot.recreate = create;
}
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": ">=14.16"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -32,12 +34,12 @@
"utility"
],
"dependencies": {
"got": "^10.5.7"
"got": "^12.5.2"
},
"devDependencies": {
"ava": "^3.0.2",
"get-stream": "^5.1.0",
"nock": "^12.0.0",
"xo": "^0.26.1"
"ava": "^4.3.3",
"get-stream": "^6.0.1",
"nock": "^13.2.9",
"xo": "^0.52.4"
}
}
93 changes: 46 additions & 47 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,59 @@ Unless you're already using Got, you should probably use GitHub's own [@octokit/

## Install

```
$ npm install gh-got
```sh
npm install gh-got
```

## Usage

Instead of:

```js
const got = require('got');
import got from 'got';

const token = 'foo';

(async () => {
const {body} = await got('https://api.github.com/users/sindresorhus', {
json: true,
headers: {
'accept': 'application/vnd.github.v3+json',
'authorization': `token ${token}`
}
});

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

console.log(body.login);
//=> 'sindresorhus'
```

You can do:

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

(async () => {
const {body} = await ghGot('users/sindresorhus', {token: 'foo'});
const {body} = await ghGot('users/sindresorhus', {
context: {
token: 'foo'
}
});

console.log(body.login);
//=> 'sindresorhus'
})();
console.log(body.login);
//=> 'sindresorhus'
```

Or:

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

(async () => {
const {body} = await ghGot('https://api.github.com/users/sindresorhus', {token: 'foo'});
const {body} = await ghGot('https://api.github.com/users/sindresorhus', {
context: {
token: 'foo'
}
});

console.log(body.login);
//=> 'sindresorhus'
})();
console.log(body.login);
//=> 'sindresorhus'
```

## API
Expand Down Expand Up @@ -94,14 +97,12 @@ Can be specified as a plain object and will be serialized as JSON with the appro
Responses and errors have a `.rateLimit` property with info about the current [rate limit](https://developer.github.com/v3/#rate-limiting). *(This is not yet implemented for the stream API)*

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

(async () => {
const {rateLimit} = await ghGot('users/sindresorhus');
const {rateLimit} = await ghGot('users/sindresorhus');

console.log(rateLimit);
//=> {limit: 5000, remaining: 4899, reset: [Date 2018-12-31T20:45:20.000Z]}
})();
console.log(rateLimit);
//=> {limit: 5000, remaining: 4899, reset: [Date 2018-12-31T20:45:20.000Z]}
```

## Authorization
Expand All @@ -115,21 +116,19 @@ Authorization for GitHub uses the following logic:
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'
})();
import ghGot from 'gh-got';

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

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

## Pagination

See the [Got docs](https://github.com/sindresorhus/got#pagination).
See the [Got docs](https://github.com/sindresorhus/got/blob/main/documentation/4-pagination.md).
Loading

0 comments on commit 24abafe

Please sign in to comment.