Skip to content

Commit 8a7c669

Browse files
gen-changelog: Use fetch instead of http.request (#3692)
1 parent c9f968b commit 8a7c669

File tree

2 files changed

+18
-46
lines changed

2 files changed

+18
-46
lines changed

resources/gen-changelog.ts

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import * as https from 'node:https';
2-
import * as util from 'node:util';
3-
41
import { execOutput, readPackageJSON } from './utils';
52

63
const packageJSON = readPackageJSON();
7-
const graphqlRequest = util.promisify(graphqlRequestImpl);
84
const labelsConfig: { [label: string]: { section: string; fold?: boolean } } = {
95
'PR: breaking change 💥': {
106
section: 'Breaking Change 💥',
@@ -151,56 +147,29 @@ function genChangeLog(
151147
return changelog;
152148
}
153149

154-
function graphqlRequestImpl(
155-
query: string,
156-
resultCB: (error?: Error, data?: any) => void,
157-
) {
158-
const req = https.request('https://api.github.com/graphql', {
150+
async function graphqlRequest(query: string) {
151+
const response = await fetch('https://api.github.com/graphql', {
159152
method: 'POST',
160153
headers: {
161154
Authorization: 'bearer ' + GH_TOKEN,
162155
'Content-Type': 'application/json',
163156
'User-Agent': 'gen-changelog',
164157
},
158+
body: JSON.stringify({ query }),
165159
});
166160

167-
req.on('response', (res) => {
168-
let responseBody = '';
169-
170-
res.setEncoding('utf8');
171-
res.on('data', (d) => (responseBody += d));
172-
res.on('error', (error) => resultCB(error));
173-
174-
res.on('end', () => {
175-
if (res.statusCode !== 200) {
176-
return resultCB(
177-
new Error(
178-
`GitHub responded with ${res.statusCode}: ${res.statusMessage}\n` +
179-
responseBody,
180-
),
181-
);
182-
}
183-
184-
let json;
185-
try {
186-
json = JSON.parse(responseBody);
187-
} catch (error) {
188-
return resultCB(error);
189-
}
190-
191-
if (json.errors) {
192-
return resultCB(
193-
new Error('Errors: ' + JSON.stringify(json.errors, null, 2)),
194-
);
195-
}
196-
197-
resultCB(undefined, json.data);
198-
});
199-
});
161+
if (!response.ok) {
162+
throw new Error(
163+
`GitHub responded with ${response.status}: ${response.statusText}\n` +
164+
(await response.text()),
165+
);
166+
}
200167

201-
req.on('error', (error) => resultCB(error));
202-
req.write(JSON.stringify({ query }));
203-
req.end();
168+
const json = await response.json();
169+
if (json.errors) {
170+
throw new Error('Errors: ' + JSON.stringify(json.errors, null, 2));
171+
}
172+
return json.data;
204173
}
205174

206175
interface CommitInfo {

tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
"benchmark/benchmark.ts"
77
],
88
"compilerOptions": {
9-
"lib": ["es2020"],
9+
"lib": [
10+
"es2020",
11+
"dom" // Workaround for missing web-compatible globals in `@types/node`
12+
],
1013
"target": "es2020",
1114
"module": "commonjs",
1215
"moduleResolution": "node",

0 commit comments

Comments
 (0)