Skip to content

Commit

Permalink
refactor(http): use fetch instead of node:http/s
Browse files Browse the repository at this point in the history
  • Loading branch information
oeyoews committed Oct 24, 2024
1 parent 174d413 commit c21f66a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 85 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"publisher": "oeyoews",
"name": "usewiki2",
"displayName": "usewiki2",
"version": "1.3.0",
"version": "1.4.0",
"private": true,
"packageManager": "[email protected]",
"description": "",
Expand Down
97 changes: 42 additions & 55 deletions src/featchData.ts
Original file line number Diff line number Diff line change
@@ -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<ITiddlyWikiStatus> {
export default async function fetchData(): Promise<ITiddlyWikiStatus> {
// 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;
}
}
43 changes: 14 additions & 29 deletions src/sendTiddler.ts
Original file line number Diff line number Diff line change
@@ -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');
}
}

0 comments on commit c21f66a

Please sign in to comment.