|
|
@@ -33,6 +33,27 @@ interface ShareLinkRecord {
|
|
|
created_at: string;
|
|
|
}
|
|
|
|
|
|
+// Backend returns paginated response with { data: [...], meta: {...} }
|
|
|
+interface PaginatedResponse<T> {
|
|
|
+ data: T[];
|
|
|
+ meta?: {
|
|
|
+ total: number;
|
|
|
+ limit: number;
|
|
|
+ offset: number;
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
+// Helper to extract array from paginated response
|
|
|
+function extractData<T>(response: { data: T[] | PaginatedResponse<T> | null }): T[] {
|
|
|
+ if (!response.data) return [];
|
|
|
+ // Check if response.data is the paginated wrapper or direct array
|
|
|
+ if (Array.isArray(response.data)) {
|
|
|
+ return response.data;
|
|
|
+ }
|
|
|
+ // It's a paginated response { data: [...], meta: {...} }
|
|
|
+ return (response.data as PaginatedResponse<T>).data || [];
|
|
|
+}
|
|
|
+
|
|
|
function generateLinkCode(): string {
|
|
|
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
|
let result = '';
|
|
|
@@ -116,7 +137,8 @@ export async function listShareLinks(
|
|
|
throw new Error(response.error.message || 'Failed to load share links');
|
|
|
}
|
|
|
|
|
|
- return (response.data || []).map(recordToShareLink);
|
|
|
+ const records = extractData<ShareLinkRecord>(response);
|
|
|
+ return records.map(recordToShareLink);
|
|
|
}
|
|
|
|
|
|
export async function revokeShareLink(
|
|
|
@@ -131,11 +153,12 @@ export async function revokeShareLink(
|
|
|
.limit(1)
|
|
|
.get();
|
|
|
|
|
|
- if (findResponse.error || !findResponse.data || findResponse.data.length === 0) {
|
|
|
+ const records = extractData<ShareLinkRecord>(findResponse);
|
|
|
+ if (findResponse.error || records.length === 0) {
|
|
|
throw new Error('Share link not found');
|
|
|
}
|
|
|
|
|
|
- const linkId = findResponse.data[0].id;
|
|
|
+ const linkId = records[0].id;
|
|
|
|
|
|
// Update the link to mark it as revoked
|
|
|
const updateResponse = await client
|
|
|
@@ -159,11 +182,12 @@ export async function getShareLinkByCode(
|
|
|
.limit(1)
|
|
|
.get();
|
|
|
|
|
|
- if (response.error || !response.data || response.data.length === 0) {
|
|
|
+ const records = extractData<ShareLinkRecord>(response);
|
|
|
+ if (response.error || records.length === 0) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- return recordToShareLink(response.data[0]);
|
|
|
+ return recordToShareLink(records[0]);
|
|
|
}
|
|
|
|
|
|
export async function incrementViewCount(
|
|
|
@@ -178,11 +202,12 @@ export async function incrementViewCount(
|
|
|
.limit(1)
|
|
|
.get();
|
|
|
|
|
|
- if (findResponse.error || !findResponse.data || findResponse.data.length === 0) {
|
|
|
+ const records = extractData<ShareLinkRecord>(findResponse);
|
|
|
+ if (findResponse.error || records.length === 0) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- const link = findResponse.data[0];
|
|
|
+ const link = records[0];
|
|
|
const currentCount = link.view_count || 0;
|
|
|
|
|
|
// Update view count
|