generated from antfu/starter-vscode
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(http): use fetch instead of node:http/s
- Loading branch information
Showing
3 changed files
with
57 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
"publisher": "oeyoews", | ||
"name": "usewiki2", | ||
"displayName": "usewiki2", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"private": true, | ||
"packageManager": "[email protected]", | ||
"description": "", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |