This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

semble / src / webapp / features / cards / lib / dal.ts
3.8 kB 144 lines
1import { verifySessionOnClient, logoutUser } from '@/lib/auth/dal'; 2import { createSembleClient } from '@/services/client.apiClient'; 3import { 4 CardSortField, 5 GetUrlMetadataParams, 6 SortOrder, 7 UrlType, 8} from '@semble/types'; 9import { cache } from 'react'; 10 11interface PageParams { 12 page?: number; 13 limit?: number; 14 cardSortBy?: CardSortField; 15 cardSortOrder?: SortOrder; 16 urlType?: UrlType; 17} 18 19export const getUrlMetadata = cache(async (params: GetUrlMetadataParams) => { 20 const client = createSembleClient(); 21 const response = await client.getUrlMetadata(params); 22 23 return response; 24}); 25 26export const getCardFromMyLibrary = cache(async (url: string) => { 27 const session = await verifySessionOnClient({ redirectOnFail: true }); 28 if (!session) throw new Error('No session found'); 29 const client = createSembleClient(); 30 const response = await client.getUrlStatusForMyLibrary({ url: url }); 31 32 return response; 33}); 34 35export const getMyUrlCards = cache(async (params?: PageParams) => { 36 const session = await verifySessionOnClient({ redirectOnFail: true }); 37 if (!session) throw new Error('No session found'); 38 const client = createSembleClient(); 39 const response = await client.getMyUrlCards({ 40 page: params?.page, 41 limit: params?.limit, 42 }); 43 44 return response; 45}); 46 47export const addUrlToLibrary = cache( 48 async ( 49 url: string, 50 { 51 note, 52 collectionIds, 53 viaCardId, 54 }: { note?: string; collectionIds?: string[]; viaCardId?: string }, 55 ) => { 56 const session = await verifySessionOnClient({ redirectOnFail: true }); 57 if (!session) throw new Error('No session found'); 58 const client = createSembleClient(); 59 60 try { 61 const response = await client.addUrlToLibrary({ 62 url: url, 63 note: note, 64 collectionIds: collectionIds, 65 viaCardId: viaCardId, 66 }); 67 68 return response; 69 } catch (error) { 70 await logoutUser(); 71 } 72 }, 73); 74 75export const getUrlCardView = cache(async (id: string) => { 76 const client = createSembleClient(); 77 const response = await client.getUrlCardView(id); 78 79 return response; 80}); 81 82export const getUrlCards = cache( 83 async (didOrHandle: string, params?: PageParams) => { 84 const client = createSembleClient(); 85 const response = await client.getUrlCards({ 86 identifier: didOrHandle, 87 page: params?.page, 88 limit: params?.limit, 89 sortBy: params?.cardSortBy, 90 sortOrder: params?.cardSortOrder, 91 urlType: params?.urlType, 92 }); 93 94 return response; 95 }, 96); 97 98export const removeCardFromCollection = cache( 99 async ({ 100 cardId, 101 collectionIds, 102 }: { 103 cardId: string; 104 collectionIds: string[]; 105 }) => { 106 const session = await verifySessionOnClient({ redirectOnFail: true }); 107 if (!session) throw new Error('No session found'); 108 const client = createSembleClient(); 109 110 try { 111 const response = await client.removeCardFromCollection({ 112 cardId, 113 collectionIds, 114 }); 115 116 return response; 117 } catch (error) { 118 await logoutUser(); 119 } 120 }, 121); 122 123export const removeCardFromLibrary = cache(async (cardId: string) => { 124 const session = await verifySessionOnClient({ redirectOnFail: true }); 125 if (!session) throw new Error('No session found'); 126 const client = createSembleClient(); 127 128 try { 129 const response = await client.removeCardFromLibrary({ cardId }); 130 131 return response; 132 } catch (error) { 133 await logoutUser(); 134 } 135}); 136 137export const getLibrariesForCard = cache(async (cardId: string) => { 138 const session = await verifySessionOnClient({ redirectOnFail: true }); 139 if (!session) throw new Error('No session found'); 140 const client = createSembleClient(); 141 const response = await client.getLibrariesForCard(cardId); 142 143 return response; 144});