|
1 | | -import * as https from 'node:https'; |
2 | | -import * as util from 'node:util'; |
3 | | - |
4 | 1 | import { execOutput, readPackageJSON } from './utils'; |
5 | 2 |
|
6 | 3 | const packageJSON = readPackageJSON(); |
7 | | -const graphqlRequest = util.promisify(graphqlRequestImpl); |
8 | 4 | const labelsConfig: { [label: string]: { section: string; fold?: boolean } } = { |
9 | 5 | 'PR: breaking change 💥': { |
10 | 6 | section: 'Breaking Change 💥', |
@@ -151,56 +147,29 @@ function genChangeLog( |
151 | 147 | return changelog; |
152 | 148 | } |
153 | 149 |
|
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', { |
159 | 152 | method: 'POST', |
160 | 153 | headers: { |
161 | 154 | Authorization: 'bearer ' + GH_TOKEN, |
162 | 155 | 'Content-Type': 'application/json', |
163 | 156 | 'User-Agent': 'gen-changelog', |
164 | 157 | }, |
| 158 | + body: JSON.stringify({ query }), |
165 | 159 | }); |
166 | 160 |
|
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 | + } |
200 | 167 |
|
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; |
204 | 173 | } |
205 | 174 |
|
206 | 175 | interface CommitInfo { |
|
0 commit comments