UNPKG

@specify-sh/sdk

Version:
8 lines (3 loc) 2.08 kB
class N extends Error{constructor(t){super(t);this.name="AuthenticationError"}}class g extends Error{details;constructor(t,c){super(t);this.details=c;this.name="ValidationError"}}class l extends Error{status;details;constructor(t,c,p){super(t);this.status=c;this.details=p;this.name="APIError"}}class x extends Error{constructor(t="No ad found for this address"){super(t);this.name="NotFoundError"}}var S;((i)=>{i.LANDSCAPE="LANDSCAPE";i.SQUARE="SQUARE";i.LONG_BANNER="LONG_BANNER";i.SHORT_BANNER="SHORT_BANNER";i.NO_IMAGE="NO_IMAGE"})(S||={});var b="https://app.specify.sh/api";class E{publisherKey;constructor(t){if(!this.validatePublisherKey(t.publisherKey))throw new N("Invalid API key format");this.publisherKey=t.publisherKey}validatePublisherKey(t){return t.startsWith("spk_")&&t.length===34}validateAddress(t){return/^0x[a-fA-F0-9]{40}$/.test(t)}validateAddresses(t){return t.every((c)=>this.validateAddress(c))}async serve(t,c){let p=Array.isArray(t)?t:[t];if(!this.validateAddresses(p))throw new g("Invalid wallet address format");let u=[...new Set(p)];if(u.length===0)throw new g("At least one wallet address is required");if(u.length>50)throw new g("Maximum 50 wallet addresses allowed");let n;try{if(n=await fetch(`${b}/ads`,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":this.publisherKey},body:JSON.stringify({walletAddresses:u,imageFormat:c})}),!n.ok){if(n.status===404)throw new x;if(n.status===401)throw new N("Invalid API key");if(n.status===400){let R=await n.json();throw new g(R.error||"Invalid request",R.details)}throw new l(`HTTP error! status: ${n.status}`,n.status)}return await n.json()}catch(i){if(i instanceof l||i instanceof N||i instanceof g||i instanceof x)throw i;throw new l(`Failed to fetch ad content: ${i instanceof Error?i.message:"Unknown error"}`,i instanceof Error&&"status"in i?i.status:0)}}}export{E as default,g as ValidationError,x as NotFoundError,S as ImageFormat,N as AuthenticationError,l as APIError}; /* Built with ❤️ by Specify team */ //# debugId=820CC54793EAF6B364756E2164756E21