File size: 793 Bytes
fc69895
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { base } from "$app/paths";
import { page } from "$app/state";

// Returns a public share URL for a conversation id.
// If `id` is already a 7-char share id, no network call is made.
export async function createShareLink(id: string): Promise<string> {
	const prefix =
		page.data.publicConfig.PUBLIC_SHARE_PREFIX ||
		`${page.data.publicConfig.PUBLIC_ORIGIN || page.url.origin}${base}`;

	if (id.length === 7) {
		return `${prefix}/r/${id}`;
	}

	const res = await fetch(`${base}/conversation/${id}/share`, {
		method: "POST",
		headers: { "Content-Type": "application/json" },
	});

	if (!res.ok) {
		const text = await res.text().catch(() => "");
		throw new Error(text || "Failed to create share link");
	}

	const { shareId } = await res.json();
	return `${prefix}/r/${shareId}`;
}