chore(client-protocol): 抽取获取基 http url 的方法

This commit is contained in:
CrescentLeaf
2025-11-28 23:10:50 +08:00
parent 02b1d28a6b
commit 12861b80a1

View File

@@ -168,6 +168,18 @@ export default class LingChairClient {
return false
}
}
getBaseHttpUrl() {
const url = new URL(this.server_url)
return (({
'ws:': 'http:',
'wss:': 'https:',
'http:': 'http:',
'https:': 'https:',
})[url.protocol] || 'http:') + '//' + url.host
}
getUrlForFileByHash(file_hash?: string, defaultUrl?: string) {
return file_hash ? (this.getBaseHttpUrl() + '/uploaded_files/' + file_hash) : defaultUrl
}
async registerOrThrow({
nickname,
username,
@@ -201,13 +213,8 @@ export default class LingChairClient {
)
form.append('file_name', fileName)
chatId && form.append('chat_id', chatId)
const url = new URL(this.server_url)
const re = await fetch((({
'ws:': 'http:',
'wss:': 'https:',
'http:': 'http:',
'https:': 'https:',
})[url.protocol] || 'http:') + '//' + url.host + '/upload_file', {
const re = await fetch(this.getBaseHttpUrl() + '/upload_file', {
method: 'POST',
headers: {
"Token": this.access_token,