bluesky-embed-core
Version:
1 lines • 1.46 kB
JavaScript
import{AppBskyFeedDefs,AtpAgent}from"@atproto/api";export class BlueskyApiError extends Error{constructor({message,status,data}){super(message);this.name="BlueskyApiError";this.status=status;this.data=data}}const DEFAULT_URI="at://did:plc:zl7kgfro2rx3pavbslhhdhuy/app.bsky.feed.post/3lblfjf4evs2v";export async function fetchPostThread({params,config={depth:0,parentHeight:0}}){const agent=new AtpAgent({service:"https://public.api.bsky.app"});try{var _data_thread_post_author_labels;let atUri=DEFAULT_URI;atUri=`at://${decodeURIComponent(params.did)}/app.bsky.feed.post/${decodeURIComponent(params.rkey)}`;var _config_depth,_config_parentHeight;const{data}=await agent.getPostThread({uri:atUri,depth:(_config_depth=config.depth)!=null?_config_depth:0,parentHeight:(_config_parentHeight=config.parentHeight)!=null?_config_parentHeight:0});if(!AppBskyFeedDefs.isThreadViewPost(data.thread)){return{notFound:true}}const pwiOptOut=!!((_data_thread_post_author_labels=data.thread.post.author.labels)==null?void 0:_data_thread_post_author_labels.find(label=>label.val==="!no-unauthenticated"));if(pwiOptOut){throw new BlueskyApiError({message:"The author of this post has requested their posts not be displayed on external sites.",status:400,data:null})}return{data:JSON.parse(JSON.stringify(data.thread))}}catch(error){console.error(error);throw new BlueskyApiError({message:error instanceof Error?error.message:"Invalid Bluesky URL",status:500,data:"null"})}}