2023-07-27 14:31:52 +09:00
|
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 14:31:52 +09:00
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2022-12-30 12:00:50 +09:00
|
|
|
|
/* objを検査して
|
|
|
|
|
* 1. 配列に何も入っていない時はクエリを付けない
|
|
|
|
|
* 2. プロパティがundefinedの時はクエリを付けない
|
|
|
|
|
* (new URLSearchParams(obj)ではそこまで丁寧なことをしてくれない)
|
2023-07-08 07:08:16 +09:00
|
|
|
|
*/
|
2024-09-09 20:57:36 +09:00
|
|
|
|
export function query(obj: Record<string, string | number | boolean>): string {
|
2021-10-01 00:31:43 +09:00
|
|
|
|
const params = Object.entries(obj)
|
2024-09-09 20:57:36 +09:00
|
|
|
|
.filter(([, v]) => Array.isArray(v) ? v.length : v !== undefined) // eslint-disable-line @typescript-eslint/no-unnecessary-condition
|
|
|
|
|
.reduce<Record<string, string | number | boolean>>((a, [k, v]) => (a[k] = v, a), {});
|
2021-10-01 00:31:43 +09:00
|
|
|
|
|
|
|
|
|
return Object.entries(params)
|
2022-05-07 07:19:15 +02:00
|
|
|
|
.map((p) => `${p[0]}=${encodeURIComponent(p[1])}`)
|
2021-10-01 00:31:43 +09:00
|
|
|
|
.join('&');
|
2019-02-13 23:45:35 +09:00
|
|
|
|
}
|
2019-03-18 13:20:49 +09:00
|
|
|
|
|
2024-09-09 20:57:36 +09:00
|
|
|
|
export function appendQuery(url: string, queryString: string): string {
|
|
|
|
|
return `${url}${/\?/.test(url) ? url.endsWith('?') ? '' : '&' : '?'}${queryString}`;
|
2019-03-18 13:20:49 +09:00
|
|
|
|
}
|
2024-07-14 15:27:52 +09:00
|
|
|
|
|
|
|
|
|
export function extractDomain(url: string) {
|
2024-07-14 17:28:34 +09:00
|
|
|
|
const match = url.match(/^(?:https?:)?(?:\/\/)?(?:[^@\n]+@)?([^:\/\n]+)/im);
|
|
|
|
|
return match ? match[1] : null;
|
2024-07-14 15:27:52 +09:00
|
|
|
|
}
|
2025-04-27 13:05:09 -04:00
|
|
|
|
|
|
|
|
|
export function maybeMakeRelative(urlStr: string, baseStr: string): string {
|
|
|
|
|
try {
|
|
|
|
|
const baseObj = new URL(baseStr);
|
|
|
|
|
const urlObj = new URL(urlStr);
|
|
|
|
|
/* in all places where maybeMakeRelative is used, baseStr is the
|
|
|
|
|
* instance's public URL, which can't have path components, so the
|
|
|
|
|
* relative URL will always have the whole path from the urlStr
|
|
|
|
|
*/
|
|
|
|
|
if (urlObj.origin === baseObj.origin) {
|
|
|
|
|
return urlObj.pathname + urlObj.search + urlObj.hash;
|
|
|
|
|
}
|
|
|
|
|
return urlStr;
|
2025-04-27 15:42:19 -04:00
|
|
|
|
} catch (error) {
|
2025-04-27 13:05:09 -04:00
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
}
|