From 253d548c81063d193cb80b02a995bb7f4dbfc331 Mon Sep 17 00:00:00 2001 From: FineArchs <133759614+FineArchs@users.noreply.github.com> Date: Wed, 18 Oct 2023 20:52:54 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B5=E3=83=BC=E3=83=90=E3=83=BC=E3=83=81?= =?UTF-8?q?=E3=82=A7=E3=83=83=E3=82=AF=E3=81=AE=E6=96=B9=E6=B3=95=E3=82=92?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update api.ts * Update api.ts * lint --- packages/frontend/src/scripts/api.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/frontend/src/scripts/api.ts b/packages/frontend/src/scripts/api.ts index d1c9d79864..a8fbec627f 100644 --- a/packages/frontend/src/scripts/api.ts +++ b/packages/frontend/src/scripts/api.ts @@ -51,13 +51,20 @@ export function api(hostUrl: string, endpoint: E, data: P = {} as any, token?: string | null | undefined, signal?: AbortSignal): Promise { - if (!/^https?:\/\//.test(hostUrl)) throw new Error('invalid host name'); +export async function apiExternal(host: string, endpoint: E, data: P = {} as any, token?: string | null | undefined, signal?: AbortSignal): Promise { if (endpoint.includes('://')) throw new Error('invalid endpoint'); - const knownUrls = (await api('federation/instances', { blocked: false })).map(v => v.host); - if (!knownUrls.includes(URL(hostUrl).host)) throw new Error(hostname + 'is blocked by or not known to this server.'); - pendingApiRequestsCount.value++; + const [hostName, hostUrl] = await (new Promise(resolve => resolve(URL(host)))) + .then(url => { + if (['http:', 'https:'].includes(url.protocol)) return [url.host, host]; + else throw new Error('Only http and https are allowed as url protocol.'); + }, err => { + if (err instanceof TypeError) return [host, 'https://' + host]; + else throw err; + }); + const serverInfo = await api('federation/show-instance', { host: hostName }); + if ((!serverInfo) || serverInfo.isBlocked) throw new Error(hostName + 'is blocked by or not known to this server.'); + pendingApiRequestsCount.value++; const onFinally = () => { pendingApiRequestsCount.value--; };