Read-it-later social network
at main 56 lines 1.7 kB view raw
1import { SLICES_BEARER_TOKEN, SLICES_NETWORK_ACCESS_TOKEN } from "$env/static/private"; 2import type { LexiconCommunityBookmark, SliceItem, SliceList } from "$lib/utils"; 3 4const SLICES_NETWORK_SLICE_URI = "at://did:plc:gotnvwkr56ibs33l4hwgfoet/network.slices.slice/3m26tswgbi42i" 5 6const baseUrl = "https://slices-api.fly.dev/xrpc/"; 7 8type GetListProps = { 9 limit?: number; // default: 50, max: 100 10 cursor?: string | null; 11 where?: { 12 [key: string]: { eq?: string, contains?: string, in?: string[] } 13 }; 14 sortBy?: { field: string, direction: "desc" | "asc" }[] 15}; 16 17export class SlicesAPI<T> { 18 19 collection: string; 20 sliceUri: string; 21 22 constructor({ collection, sliceUri }: { collection: string, sliceUri : string }) { 23 this.collection = collection; 24 this.sliceUri = sliceUri; 25 } 26 27 /** 28 async getRecord({ uri }: { uri: string }) { 29 const response = await fetch(`${baseUrl}${this.collection}.getRecord?${searchParams.toString()}`); 30 return await response.json() as SliceItem<T>; 31 } 32 **/ 33 34 async getList(body: GetListProps) { 35 const response = await fetch(`${baseUrl}${this.collection}.getRecords`, { 36 method: "POST", 37 headers: { 38 // "Accept": "*/*", 39 "Content-Type": "application/json", 40 // "Authorization": `Bearer ${SLICES_BEARER_TOKEN}` 41 }, 42 body: JSON.stringify({ ...body, slice: SLICES_NETWORK_SLICE_URI }) 43 }); 44 const data = await response.json() as SliceList<T>; 45 for (const d of data.records) { 46 console.log(d); 47 } 48 console.log(data.cursor); 49 return data; 50 } 51} 52 53export const LexiconBookmarkSlicesAPI = new SlicesAPI<LexiconCommunityBookmark>({ 54 collection: "community.lexicon.bookmarks.bookmark", 55 sliceUri: SLICES_NETWORK_SLICE_URI 56});