Skip to content

Commit

Permalink
refactor: remove needle and xml2js, use node-fetch and fast-xml-parser (
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 authored Sep 14, 2024
1 parent d4e780c commit ea0dc79
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 31 deletions.
15 changes: 0 additions & 15 deletions bin/fanyi.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,3 @@ async function runFY(options = {}) {
const fanyi = require('..');
fanyi(program.args.join(' '), mergedOptions);
}

function resolveOptions(options) {
const opts = {};
const filteredKeys = Object.keys(options).filter(
(key) => isBoolean(options[key]) || typeof options[key] === 'string',
);
for (const key of filteredKeys) {
opts[key] = options[key];
}
return opts;
}

function isBoolean(val) {
return typeof val === 'boolean';
}
3 changes: 3 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"files": {
"ignore": ["coverage"]
},
"organizeImports": {
"enabled": true
},
Expand Down
Binary file modified bun.lockb
Binary file not shown.
21 changes: 8 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const needle = require('needle');
const { Groq } = require('groq-sdk');
const print = require('./lib/print');
const parseString = require('xml2js').parseString;
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
const { XMLParser } = require('fast-xml-parser');
const ora = require('ora');
const gradient = require('gradient-string');

Expand Down Expand Up @@ -32,17 +32,12 @@ module.exports = async (word, options) => {
'http://dict-co.iciba.com/api/dictionary.php?key=D191EBD014295E913574E1EAF8E06666&w=';
const spinner = ora('正在请教 iciba...').start();
try {
const response = await needle('get', `${ICIBA_URL}${endcodedWord}`, { parse: false });
if (response.statusCode === 200) {
const result = await new Promise((resolve, reject) => {
parseString(response.body, (err, res) => {
if (err) reject(err);
else resolve(res);
});
});
spinner.stop();
print.iciba(result.dict, options);
}
const response = await fetch(`${ICIBA_URL}${endcodedWord}`);
const xml = await response.text();
const parser = new XMLParser();
const result = parser.parse(xml);
spinner.stop();
print.iciba(result.dict, options);
} catch (error) {
spinner.fail('访问 iciba 失败,请检查网络');
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
"chalk": "^4.1.2",
"commander": "^12.1.0",
"dayjs": "^1.11.13",
"fast-xml-parser": "^4.5.0",
"fs-extra": "^10.1.0",
"gradient-string": "^2.0.2",
"groq-sdk": "^0.7.0",
"needle": "^3.3.1",
"node-fetch": "^3.3.2",
"ora": "^5.4.1",
"update-notifier": "^5.1.0",
"xml2js": "^0.6.2"
"update-notifier": "^5.1.0"
},
"lint-staged": {
"*.{js,ts,json,yml}": [
Expand Down

0 comments on commit ea0dc79

Please sign in to comment.