From c21f66adb3768f2ffaa48b63b47cfb822217ffb8 Mon Sep 17 00:00:00 2001 From: oeyoews Date: Thu, 24 Oct 2024 16:51:21 +0800 Subject: [PATCH] refactor(http): use fetch instead of node:http/s --- package.json | 2 +- src/featchData.ts | 97 ++++++++++++++++++++-------------------------- src/sendTiddler.ts | 43 +++++++------------- 3 files changed, 57 insertions(+), 85 deletions(-) diff --git a/package.json b/package.json index 4e3ce8d..38d8055 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "publisher": "oeyoews", "name": "usewiki2", "displayName": "usewiki2", - "version": "1.3.0", + "version": "1.4.0", "private": true, "packageManager": "pnpm@9.0.6", "description": "", diff --git a/src/featchData.ts b/src/featchData.ts index bc90287..7bb5f0a 100644 --- a/src/featchData.ts +++ b/src/featchData.ts @@ -1,67 +1,54 @@ -import http from 'node:http'; -import https from 'node:https'; import * as vscode from 'vscode'; import { enableHttps, config, getIp, getPort } from './config'; import { notify } from './notify'; -export default function fetchData(): Promise { +export default async function fetchData(): Promise { // notify(`正在获取数据${getPort()}`, 'info'); const protocal = enableHttps() ? 'https' : 'http'; const twurl = `${protocal}://${getIp()}:${getPort()}/status`; - // notify(`正在获取数据${twurl}`, 'info'); - const protocalMethos = enableHttps() ? https : http; + notify(`正在获取数据${twurl}`, 'info'); - return new Promise((resolve, reject) => { - protocalMethos - .get(twurl, (response) => { - let data: any = null; - response.on('data', (chunk) => { - data = chunk; - }); + try { + const response = await fetch(twurl); - response.on('end', () => { - try { - const dataTw = JSON.parse(data) as ITiddlyWikiStatus; - if (dataTw.tiddlywiki_version) { - resolve(dataTw); - } else { - reject(new Error('TiddlyWiki not connected')); - } - } catch (error) { - reject(error); - } - }); - }) - .on('error', (error) => { - vscode.window - .showErrorMessage(error.message, '配置端口') - .then(async (choice) => { - if (choice === '配置端口') { - // vscode.commands.executeCommand('workbench.action.openSettings', 'usewiki2.port'); - const port = await vscode.window.showInputBox({ - prompt: '输入端口', - value: getPort().toString(), - }); - if (port) { - config().update('port', Number(port), true); - vscode.window - .showInformationMessage( - '配置已更新,需要重启插件以应用更改。', - '重启插件' - ) - .then((choice) => { - if (choice === '重启插件') { - // 重启插件 - vscode.commands.executeCommand( - 'workbench.action.reloadWindow' - ); - } - }); - } - } - }); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + const dataTw = (await response.json()) as ITiddlyWikiStatus; - reject(error); + if (dataTw.tiddlywiki_version) { + return dataTw; + } else { + throw new Error('TiddlyWiki not connected'); + } + } catch (error) { + vscode.window + .showErrorMessage((error as Error).message, '配置端口') + .then(async (choice) => { + if (choice === '配置端口') { + const port = await vscode.window.showInputBox({ + prompt: '输入端口', + value: getPort().toString(), + }); + if (port) { + config().update('port', Number(port), true); + vscode.window + .showInformationMessage( + '配置已更新,需要重启插件以应用更改。', + '重启插件' + ) + .then((choice) => { + if (choice === '重启插件') { + vscode.commands.executeCommand( + 'workbench.action.reloadWindow' + ); + } + }); + } + } }); - }); + + throw error; + } } diff --git a/src/sendTiddler.ts b/src/sendTiddler.ts index 0aba443..9c676e1 100644 --- a/src/sendTiddler.ts +++ b/src/sendTiddler.ts @@ -1,43 +1,28 @@ -import http from 'node:http'; import { notify } from './notify'; import { getPort, getIp } from './config'; -export default function sendTiddler(tiddler: ITiddler) { +export default async function sendTiddler(tiddler: ITiddler) { const port = getPort(); const ip = getIp(); - const req = http.request( - { - hostname: ip, - port, - path: `/recipes/default/tiddlers/${tiddler.title}`, + const url = `http://${ip}:${port}/recipes/default/tiddlers/${tiddler.title}`; + + try { + const response = await fetch(url, { method: 'PUT', headers: { 'Content-Type': 'application/json', 'x-requested-with': 'TiddlyWiki', }, - }, - (response) => { - let responseData = ''; - - response.on('data', (chunk) => { - responseData += chunk; - }); + body: JSON.stringify(tiddler), + }); - response.on('end', () => { - if (response.statusCode === 204) { - notify('发送成功', 'info'); - } else { - notify('发送失败', 'error'); - } - }); + if (response.status === 204) { + notify('发送成功', 'info'); + } else { + notify('发送失败', 'error'); } - ); - req.on('error', (error) => { - notify(error.message, 'error'); - }); - - req.write(JSON.stringify(tiddler)); - // NOTE: 必须要手动结束 - req.end(); + } catch (error) { + notify((error as Error).message, 'error'); + } }