|
|
@@ -0,0 +1,78 @@
|
|
|
+import type { ShareLink } from '../types';
|
|
|
+
|
|
|
+const API_BASE = '/api';
|
|
|
+
|
|
|
+export async function createShareLink(
|
|
|
+ shortCode: string,
|
|
|
+ expiresIn: number,
|
|
|
+ token?: string
|
|
|
+): Promise<{ shareUrl: string; linkCode: string; expiresAt: string }> {
|
|
|
+ const headers: Record<string, string> = {
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ };
|
|
|
+ if (token) {
|
|
|
+ headers['Authorization'] = `Bearer ${token}`;
|
|
|
+ }
|
|
|
+
|
|
|
+ const res = await fetch(`${API_BASE}/images/${shortCode}/links`, {
|
|
|
+ method: 'POST',
|
|
|
+ headers,
|
|
|
+ body: JSON.stringify({ expires_in: expiresIn }),
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!res.ok) {
|
|
|
+ const error = await res.json().catch(() => ({}));
|
|
|
+ throw new Error(error.error || 'Failed to create share link');
|
|
|
+ }
|
|
|
+
|
|
|
+ return res.json();
|
|
|
+}
|
|
|
+
|
|
|
+export async function listShareLinks(
|
|
|
+ shortCode: string,
|
|
|
+ token?: string
|
|
|
+): Promise<ShareLink[]> {
|
|
|
+ const headers: Record<string, string> = {};
|
|
|
+ if (token) {
|
|
|
+ headers['Authorization'] = `Bearer ${token}`;
|
|
|
+ }
|
|
|
+
|
|
|
+ const res = await fetch(`${API_BASE}/images/${shortCode}/links`, { headers });
|
|
|
+
|
|
|
+ if (!res.ok) {
|
|
|
+ throw new Error('Failed to load share links');
|
|
|
+ }
|
|
|
+
|
|
|
+ const data = await res.json();
|
|
|
+ return (data.links || []).map((link: Record<string, unknown>) => ({
|
|
|
+ id: link.id as string,
|
|
|
+ linkCode: link.link_code as string,
|
|
|
+ shareUrl: link.share_url as string,
|
|
|
+ expiresAt: new Date(link.expires_at as string),
|
|
|
+ viewCount: link.view_count as number,
|
|
|
+ lastViewedAt: link.last_viewed_at ? new Date(link.last_viewed_at as string) : undefined,
|
|
|
+ isRevoked: link.is_revoked as boolean,
|
|
|
+ isValid: link.is_valid as boolean,
|
|
|
+ createdAt: new Date(link.created_at as string),
|
|
|
+ }));
|
|
|
+}
|
|
|
+
|
|
|
+export async function revokeShareLink(
|
|
|
+ shortCode: string,
|
|
|
+ linkCode: string,
|
|
|
+ token?: string
|
|
|
+): Promise<void> {
|
|
|
+ const headers: Record<string, string> = {};
|
|
|
+ if (token) {
|
|
|
+ headers['Authorization'] = `Bearer ${token}`;
|
|
|
+ }
|
|
|
+
|
|
|
+ const res = await fetch(`${API_BASE}/images/${shortCode}/links/${linkCode}`, {
|
|
|
+ method: 'DELETE',
|
|
|
+ headers,
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!res.ok) {
|
|
|
+ throw new Error('Failed to revoke share link');
|
|
|
+ }
|
|
|
+}
|