Skip to content

Commit 24abafe

Browse files
committedOct 18, 2022
Require Node.js 14 and move to ESM
Fixes #42
1 parent 2902a01 commit 24abafe

File tree

6 files changed

+139
-111
lines changed

6 files changed

+139
-111
lines changed
 

‎.github/funding.yml

-1
This file was deleted.

‎.github/workflows/main.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13+
- 18
14+
- 16
1315
- 14
14-
- 12
15-
- 10
1616
steps:
17-
- uses: actions/checkout@v2
18-
- uses: actions/setup-node@v1
17+
- uses: actions/checkout@v3
18+
- uses: actions/setup-node@v3
1919
with:
2020
node-version: ${{ matrix.node-version }}
2121
- run: npm install

‎index.js

+40-28
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,51 @@
1-
'use strict';
2-
const got = require('got');
1+
import process from 'node:process';
2+
import got from 'got';
33

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

1010
const create = () => got.extend({
1111
prefixUrl: process.env.GITHUB_ENDPOINT || 'https://api.github.com',
1212
headers: {
1313
accept: 'application/vnd.github.v3+json',
14-
'user-agent': 'https://github.com/sindresorhus/gh-got'
14+
'user-agent': 'https://github.com/sindresorhus/gh-got',
1515
},
1616
responseType: 'json',
17-
token: process.env.GITHUB_TOKEN,
17+
context: {
18+
token: process.env.GITHUB_TOKEN,
19+
},
20+
hooks: {
21+
init: [
22+
(raw, options) => {
23+
// TODO: This should be fixed in Got.
24+
// TODO: This doesn't seem to have any effect.
25+
if (typeof options.url === 'string' && options.url.startsWith('/')) {
26+
options.url = options.url.slice(1);
27+
}
28+
29+
if ('token' in raw) {
30+
options.context.token = raw.token;
31+
delete raw.token;
32+
}
33+
},
34+
],
35+
},
1836
handlers: [
1937
(options, next) => {
20-
// Authorization
21-
if (options.token && !options.headers.authorization) {
22-
options.headers.authorization = `token ${options.token}`;
38+
// TODO: This should be fixed in Got
39+
// TODO: This doesn't seem to have any effect.
40+
if (typeof options.url === 'string' && options.url.startsWith('/')) {
41+
options.url = options.url.slice(1);
2342
}
2443

25-
// `options.body` -> `options.json`
26-
options.json = options.body;
27-
delete options.body;
44+
// Authorization
45+
const {token} = options.context;
46+
if (token && !options.headers.authorization) {
47+
options.headers.authorization = `token ${token}`;
48+
}
2849

2950
// Don't touch streams
3051
if (options.isStream) {
@@ -46,7 +67,7 @@ const create = () => got.extend({
4667
// Nicer errors
4768
if (response && response.body) {
4869
error.name = 'GitHubError';
49-
error.message = `${response.body.message} (${error.response.statusCode})`;
70+
error.message = `${response.body.message} (${response.statusCode})`;
5071
}
5172

5273
// Rate limit for errors
@@ -57,23 +78,14 @@ const create = () => got.extend({
5778
throw error;
5879
}
5980
})();
60-
}
81+
},
6182
],
62-
hooks: {
63-
init: [
64-
options => {
65-
// TODO: This should be fixed in Got
66-
// Remove leading slashes
67-
if (typeof options.url === 'string' && options.url.startsWith('/')) {
68-
options.url = options.url.slice(1);
69-
}
70-
}
71-
]
72-
}
7383
});
7484

75-
module.exports = create();
85+
const ghGot = create();
86+
87+
export default ghGot;
7688

7789
if (process.env.NODE_ENV === 'test') {
78-
module.exports.recreate = create;
90+
ghGot.recreate = create;
7991
}

‎package.json

+8-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
"email": "sindresorhus@gmail.com",
1111
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": "./index.js",
1315
"engines": {
14-
"node": ">=10"
16+
"node": ">=14.16"
1517
},
1618
"scripts": {
1719
"test": "xo && ava"
@@ -32,12 +34,12 @@
3234
"utility"
3335
],
3436
"dependencies": {
35-
"got": "^10.5.7"
37+
"got": "^12.5.2"
3638
},
3739
"devDependencies": {
38-
"ava": "^3.0.2",
39-
"get-stream": "^5.1.0",
40-
"nock": "^12.0.0",
41-
"xo": "^0.26.1"
40+
"ava": "^4.3.3",
41+
"get-stream": "^6.0.1",
42+
"nock": "^13.2.9",
43+
"xo": "^0.52.4"
4244
}
4345
}

‎readme.md

+46-47
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,59 @@ Unless you're already using Got, you should probably use GitHub's own [@octokit/
66

77
## Install
88

9-
```
10-
$ npm install gh-got
9+
```sh
10+
npm install gh-got
1111
```
1212

1313
## Usage
1414

1515
Instead of:
1616

1717
```js
18-
const got = require('got');
18+
import got from 'got';
19+
1920
const token = 'foo';
2021

21-
(async () => {
22-
const {body} = await got('https://api.github.com/users/sindresorhus', {
23-
json: true,
24-
headers: {
25-
'accept': 'application/vnd.github.v3+json',
26-
'authorization': `token ${token}`
27-
}
28-
});
29-
30-
console.log(body.login);
31-
//=> 'sindresorhus'
32-
})();
22+
const {body} = await got('https://api.github.com/users/sindresorhus', {
23+
json: true,
24+
headers: {
25+
'accept': 'application/vnd.github.v3+json',
26+
'authorization': `token ${token}`
27+
}
28+
});
29+
30+
console.log(body.login);
31+
//=> 'sindresorhus'
3332
```
3433

3534
You can do:
3635

3736
```js
38-
const ghGot = require('gh-got');
37+
import ghGot from 'gh-got';
3938

40-
(async () => {
41-
const {body} = await ghGot('users/sindresorhus', {token: 'foo'});
39+
const {body} = await ghGot('users/sindresorhus', {
40+
context: {
41+
token: 'foo'
42+
}
43+
});
4244

43-
console.log(body.login);
44-
//=> 'sindresorhus'
45-
})();
45+
console.log(body.login);
46+
//=> 'sindresorhus'
4647
```
4748

4849
Or:
4950

5051
```js
51-
const ghGot = require('gh-got');
52+
import ghGot from 'gh-got';
5253

53-
(async () => {
54-
const {body} = await ghGot('https://api.github.com/users/sindresorhus', {token: 'foo'});
54+
const {body} = await ghGot('https://api.github.com/users/sindresorhus', {
55+
context: {
56+
token: 'foo'
57+
}
58+
});
5559

56-
console.log(body.login);
57-
//=> 'sindresorhus'
58-
})();
60+
console.log(body.login);
61+
//=> 'sindresorhus'
5962
```
6063

6164
## API
@@ -94,14 +97,12 @@ Can be specified as a plain object and will be serialized as JSON with the appro
9497
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)*
9598

9699
```js
97-
const ghGot = require('gh-got');
100+
import ghGot from 'gh-got';
98101

99-
(async () => {
100-
const {rateLimit} = await ghGot('users/sindresorhus');
102+
const {rateLimit} = await ghGot('users/sindresorhus');
101103

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

107108
## Authorization
@@ -115,21 +116,19 @@ Authorization for GitHub uses the following logic:
115116
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:
116117

117118
```js
118-
const ghGot = require(`gh-got`);
119-
120-
(async () => {
121-
const options = {
122-
headers: {
123-
authorization: `Bearer ${jwt}`
124-
}
125-
};
126-
const {body} = await ghGot('app', options);
127-
128-
console.log(body.name);
129-
//=> 'MyApp'
130-
})();
119+
import ghGot from 'gh-got';
120+
121+
const options = {
122+
headers: {
123+
authorization: `Bearer ${jwt}`
124+
}
125+
};
126+
const {body} = await ghGot('app', options);
127+
128+
console.log(body.name);
129+
//=> 'MyApp'
131130
```
132131

133132
## Pagination
134133

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

‎test.js

+41-25
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,102 @@
1-
const test = require('ava');
2-
const nock = require('nock');
3-
const getStream = require('get-stream');
4-
const ghGot = require('.');
1+
import process from 'node:process';
2+
import test from 'ava';
3+
import nock from 'nock';
4+
import getStream from 'get-stream';
5+
import ghGot from './index.js';
56

67
const token = process.env.GITHUB_TOKEN;
78

89
test('default', async t => {
9-
t.is((await ghGot('users/sindresorhus')).body.login, 'sindresorhus');
10+
const {body} = await ghGot('users/sindresorhus');
11+
t.is(body.login, 'sindresorhus');
1012
});
1113

1214
test('full path', async t => {
13-
t.is((await ghGot('https://api.github.com/users/sindresorhus', {prefixUrl: ''})).body.login, 'sindresorhus');
15+
const {body} = await ghGot('https://api.github.com/users/sindresorhus', {prefixUrl: ''});
16+
t.is(body.login, 'sindresorhus');
1417
});
1518

1619
test('accepts options', async t => {
17-
t.is((await ghGot('users/sindresorhus', {})).body.login, 'sindresorhus');
20+
const {body} = await ghGot('users/sindresorhus', {});
21+
t.is(body.login, 'sindresorhus');
1822
});
1923

2024
test('accepts options.prefixUrl without trailing slash', async t => {
21-
t.is((await ghGot('users/sindresorhus', {prefixUrl: 'https://api.github.com'})).body.login, 'sindresorhus');
25+
const {body} = await ghGot('users/sindresorhus', {prefixUrl: 'https://api.github.com'});
26+
t.is(body.login, 'sindresorhus');
2227
});
2328

24-
test('dedupes slashes', async t => {
25-
t.is((await ghGot('/users/sindresorhus', {prefixUrl: 'https://api.github.com/'})).body.login, 'sindresorhus');
29+
test.failing('dedupes slashes', async t => {
30+
const {body} = await ghGot('/users/sindresorhus', {prefixUrl: 'https://api.github.com/'});
31+
t.is(body.login, 'sindresorhus');
2632
});
2733

2834
test.serial('global token option', async t => {
2935
process.env.GITHUB_TOKEN = 'fail';
36+
3037
await t.throwsAsync(
3138
ghGot.recreate()('users/sindresorhus'),
3239
{
33-
message: 'Bad credentials (401)'
34-
}
40+
message: 'Bad credentials (401)',
41+
},
3542
);
43+
3644
process.env.GITHUB_TOKEN = token;
3745
});
3846

3947
test('token option', async t => {
40-
await t.throwsAsync(ghGot('users/sindresorhus', {token: 'fail'}), {
41-
message: 'Bad credentials (401)'
48+
await t.throwsAsync(ghGot('users/sindresorhus', {context: {token: 'fail'}}), {
49+
message: 'Bad credentials (401)',
4250
});
4351
});
4452

4553
test.serial('global endpoint option', async t => {
4654
process.env.GITHUB_ENDPOINT = 'fail';
55+
4756
await t.throwsAsync(ghGot.recreate()('users/sindresorhus', {retries: 1}), {
48-
message: 'Invalid URL: fail/users/sindresorhus'
57+
message: /Invalid URL/,
4958
});
59+
5060
delete process.env.GITHUB_ENDPOINT;
5161
});
5262

5363
test.serial('endpoint option', async t => {
5464
process.env.GITHUB_ENDPOINT = 'https://api.github.com/';
65+
5566
await t.throwsAsync(ghGot.recreate()('users/sindresorhus', {
5667
prefixUrl: 'fail',
57-
retries: 1
68+
retries: 1,
5869
}), {
59-
message: 'Invalid URL: fail/users/sindresorhus'
70+
message: /Invalid URL/,
6071
});
72+
6173
delete process.env.GITHUB_ENDPOINT;
6274
});
6375

6476
test('stream interface', async t => {
65-
t.is(JSON.parse(await getStream(ghGot.stream('users/sindresorhus'))).login, 'sindresorhus');
66-
t.is(JSON.parse(await getStream(ghGot.stream.get('users/sindresorhus'))).login, 'sindresorhus');
77+
const string1 = await getStream(ghGot.stream('users/sindresorhus'));
78+
t.is(JSON.parse(string1).login, 'sindresorhus');
79+
80+
const string2 = await getStream(ghGot.stream.get('users/sindresorhus'));
81+
t.is(JSON.parse(string2).login, 'sindresorhus');
6782
});
6883

6984
test('json body', async t => {
7085
const prefixUrl = 'http://mock-endpoint';
71-
const body = {test: [1, 3, 3, 7]};
86+
const postBody = {test: [1, 3, 3, 7]};
7287
const reply = {ok: true};
7388

74-
const scope = nock(prefixUrl).post('/test', body).reply(200, reply);
89+
const scope = nock(prefixUrl).post('/test', postBody).reply(200, reply);
7590

76-
t.deepEqual((await ghGot.post('test', {prefixUrl, body})).body, reply);
91+
const {body} = await ghGot.post('test', {prefixUrl, json: postBody});
92+
t.deepEqual(body, reply);
7793
t.truthy(scope.isDone());
7894
});
7995

8096
test('custom error', async t => {
81-
await t.throwsAsync(ghGot('users/sindresorhus', {token: 'fail'}), {
97+
await t.throwsAsync(ghGot('users/sindresorhus', {context: {token: 'fail'}}), {
8298
name: 'GitHubError',
83-
message: 'Bad credentials (401)'
99+
message: 'Bad credentials (401)',
84100
});
85101
});
86102

@@ -92,7 +108,7 @@ test('.rateLimit response property', async t => {
92108
});
93109

94110
test('.rateLimit error property', async t => {
95-
const {rateLimit} = await t.throwsAsync(ghGot('users/sindresorhus', {token: 'fail'}));
111+
const {rateLimit} = await t.throwsAsync(ghGot('users/sindresorhus', {context: {token: 'fail'}}));
96112
t.is(typeof rateLimit.limit, 'number');
97113
t.is(typeof rateLimit.remaining, 'number');
98114
t.true(rateLimit.reset instanceof Date);

0 commit comments

Comments
 (0)
Please sign in to comment.