Repository navigation
Expand file tree
/
Copy pathFetcher.ts
More file actions
67 lines (66 loc) 路 2.21 KB
/
Copy pathFetcher.ts
File metadata and controls
67 lines (66 loc) 路 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { get } from 'node:https'
import type { ErrorData } from '@interfaces/index'
/**
* Fetches TypeScript error data from the official repository.
* @description Retrieves diagnostic messages from the TypeScript compiler repository and filters for error messages only.
* @returns Promise resolving to an array of ErrorData objects containing error codes, messages, and metadata
* @throws {Error} When no error data is found or when parsing individual messages fails
*/
export async function getErrorData(): Promise<ErrorData[]> {
try {
const repoUrl = 'https://raw.githubusercontent.com/microsoft/TypeScript'
const pathUrl = `${repoUrl}/refs/heads/main/src/compiler/diagnosticMessages.json`
const response = await new Promise<any>((resolve, reject) => {
get(pathUrl, res => {
let data = ''
res.on('data', chunk => (data += chunk))
res.on('end', () => {
try {
const jsonData = JSON.parse(data)
resolve({ data: jsonData })
} catch (error) {
reject(error)
}
})
}).on('error', reject)
})
if (Object.keys(response.data).length === 0) {
throw new Error('No error data found, please check the repository URL')
}
const tempData: ErrorData[] = []
const filterData = Object.entries(response.data)
.filter(([, value]) => (value as unknown as { category: string }).category === 'Error')
.reduce(
(acc, [key, value]) => {
acc[key] = value
return acc
},
{} as unknown as Record<string, unknown>
)
for (const message of Object.keys(filterData)) {
try {
const details = response.data[message]
const error: ErrorData = {
code: details.code,
message: message,
regexFind: '/N\/A/g',
regexReplace: 'N/A',
suggestion: 'N/A',
example: [
{
base: 'N/A',
fixed: 'N/A',
description: 'N/A'
}
]
}
tempData.push(error)
} catch (error) {
throw new Error(`Error parsing message: ${message}, error: ${error}`)
}
}
return tempData
} catch (error) {
throw error
}
}