( urlOrId: string | undefined, )
| 173 | } |
| 174 | |
| 175 | export async function fetchGitHubGistImport( |
| 176 | urlOrId: string | undefined, |
| 177 | ): Promise<SnippetImportParseResult> { |
| 178 | if (!urlOrId?.trim()) { |
| 179 | throw new Error('GitHub Gist URL is required') |
| 180 | } |
| 181 | |
| 182 | const gistId = getGistId(urlOrId) |
| 183 | if (!gistId) { |
| 184 | throw new Error('Invalid GitHub Gist URL') |
| 185 | } |
| 186 | |
| 187 | let response: Response |
| 188 | try { |
| 189 | response = await fetch(`https://api.github.com/gists/${gistId}`, { |
| 190 | headers: { |
| 191 | 'Accept': 'application/vnd.github+json', |
| 192 | 'User-Agent': 'massCode', |
| 193 | }, |
| 194 | signal: AbortSignal.timeout(GIST_FETCH_TIMEOUT_MS), |
| 195 | }) |
| 196 | } |
| 197 | catch (error) { |
| 198 | if (error instanceof Error && error.name === 'TimeoutError') { |
| 199 | throw new Error('GitHub Gist request timed out') |
| 200 | } |
| 201 | |
| 202 | throw error |
| 203 | } |
| 204 | |
| 205 | if (!response.ok) { |
| 206 | throw new Error( |
| 207 | getGistRequestErrorMessage(response.status, response.headers), |
| 208 | ) |
| 209 | } |
| 210 | |
| 211 | return parseGitHubGistResponse( |
| 212 | (await response.json()) as GitHubGistResponse, |
| 213 | urlOrId, |
| 214 | ) |
| 215 | } |
no test coverage detected