Skip to content

Commit ea0dc79

Browse files
authored
refactor: remove needle and xml2js, use node-fetch and fast-xml-parser (#119)
1 parent d4e780c commit ea0dc79

File tree

5 files changed

+14
-31
lines changed

5 files changed

+14
-31
lines changed

bin/fanyi.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,3 @@ async function runFY(options = {}) {
8585
const fanyi = require('..');
8686
fanyi(program.args.join(' '), mergedOptions);
8787
}
88-
89-
function resolveOptions(options) {
90-
const opts = {};
91-
const filteredKeys = Object.keys(options).filter(
92-
(key) => isBoolean(options[key]) || typeof options[key] === 'string',
93-
);
94-
for (const key of filteredKeys) {
95-
opts[key] = options[key];
96-
}
97-
return opts;
98-
}
99-
100-
function isBoolean(val) {
101-
return typeof val === 'boolean';
102-
}

biome.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
3+
"files": {
4+
"ignore": ["coverage"]
5+
},
36
"organizeImports": {
47
"enabled": true
58
},

bun.lockb

-562 Bytes
Binary file not shown.

index.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const needle = require('needle');
21
const { Groq } = require('groq-sdk');
32
const print = require('./lib/print');
4-
const parseString = require('xml2js').parseString;
3+
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
4+
const { XMLParser } = require('fast-xml-parser');
55
const ora = require('ora');
66
const gradient = require('gradient-string');
77

@@ -32,17 +32,12 @@ module.exports = async (word, options) => {
3232
'http://dict-co.iciba.com/api/dictionary.php?key=D191EBD014295E913574E1EAF8E06666&w=';
3333
const spinner = ora('正在请教 iciba...').start();
3434
try {
35-
const response = await needle('get', `${ICIBA_URL}${endcodedWord}`, { parse: false });
36-
if (response.statusCode === 200) {
37-
const result = await new Promise((resolve, reject) => {
38-
parseString(response.body, (err, res) => {
39-
if (err) reject(err);
40-
else resolve(res);
41-
});
42-
});
43-
spinner.stop();
44-
print.iciba(result.dict, options);
45-
}
35+
const response = await fetch(`${ICIBA_URL}${endcodedWord}`);
36+
const xml = await response.text();
37+
const parser = new XMLParser();
38+
const result = parser.parse(xml);
39+
spinner.stop();
40+
print.iciba(result.dict, options);
4641
} catch (error) {
4742
spinner.fail('访问 iciba 失败,请检查网络');
4843
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
"chalk": "^4.1.2",
2424
"commander": "^12.1.0",
2525
"dayjs": "^1.11.13",
26+
"fast-xml-parser": "^4.5.0",
2627
"fs-extra": "^10.1.0",
2728
"gradient-string": "^2.0.2",
2829
"groq-sdk": "^0.7.0",
29-
"needle": "^3.3.1",
30+
"node-fetch": "^3.3.2",
3031
"ora": "^5.4.1",
31-
"update-notifier": "^5.1.0",
32-
"xml2js": "^0.6.2"
32+
"update-notifier": "^5.1.0"
3333
},
3434
"lint-staged": {
3535
"*.{js,ts,json,yml}": [

0 commit comments

Comments
 (0)