@specify-sh/sdk
Version:
JS SDK for Specify Publishers
9 lines (4 loc) • 11.8 kB
JavaScript
var a=Object.create;var{getPrototypeOf:s,defineProperty:g,getOwnPropertyNames:r}=Object;var o=Object.prototype.hasOwnProperty;var e=(J,D,O)=>{O=J!=null?a(s(J)):{};let Y=D||!J||!J.__esModule?g(O,"default",{value:J,enumerable:!0}):O;for(let $ of r(J))if(!o.call(Y,$))g(Y,$,{get:()=>J[$],enumerable:!0});return Y};var KK=(J,D)=>()=>(D||J((D={exports:{}}).exports,D),D.exports);var c=KK((w,_)=>{var P=typeof globalThis!=="undefined"&&globalThis||typeof self!=="undefined"&&self||typeof global!=="undefined"&&global,V=function(){function J(){this.fetch=!1,this.DOMException=P.DOMException}return J.prototype=P,new J}();(function(J){var D=function(O){var Y=typeof J!=="undefined"&&J||typeof self!=="undefined"&&self||typeof Y!=="undefined"&&Y,$={searchParams:"URLSearchParams"in Y,iterable:"Symbol"in Y&&"iterator"in Symbol,blob:"FileReader"in Y&&"Blob"in Y&&function(){try{return new Blob,!0}catch(K){return!1}}(),formData:"FormData"in Y,arrayBuffer:"ArrayBuffer"in Y};function x(K){return K&&DataView.prototype.isPrototypeOf(K)}if($.arrayBuffer)var y=["[object Int8Array]","[object Uint8Array]","[object Uint8ClampedArray]","[object Int16Array]","[object Uint16Array]","[object Int32Array]","[object Uint32Array]","[object Float32Array]","[object Float64Array]"],h=ArrayBuffer.isView||function(K){return K&&y.indexOf(Object.prototype.toString.call(K))>-1};function N(K){if(typeof K!=="string")K=String(K);if(/[^a-z0-9\-#$%&'*+.^_`|~!]/i.test(K)||K==="")throw new TypeError('Invalid character in header field name: "'+K+'"');return K.toLowerCase()}function z(K){if(typeof K!=="string")K=String(K);return K}function B(K){var E={next:function(){var G=K.shift();return{done:G===void 0,value:G}}};if($.iterable)E[Symbol.iterator]=function(){return E};return E}function Z(K){if(this.map={},K instanceof Z)K.forEach(function(E,G){this.append(G,E)},this);else if(Array.isArray(K))K.forEach(function(E){this.append(E[0],E[1])},this);else if(K)Object.getOwnPropertyNames(K).forEach(function(E){this.append(E,K[E])},this)}if(Z.prototype.append=function(K,E){K=N(K),E=z(E);var G=this.map[K];this.map[K]=G?G+", "+E:E},Z.prototype.delete=function(K){delete this.map[N(K)]},Z.prototype.get=function(K){return K=N(K),this.has(K)?this.map[K]:null},Z.prototype.has=function(K){return this.map.hasOwnProperty(N(K))},Z.prototype.set=function(K,E){this.map[N(K)]=z(E)},Z.prototype.forEach=function(K,E){for(var G in this.map)if(this.map.hasOwnProperty(G))K.call(E,this.map[G],G,this)},Z.prototype.keys=function(){var K=[];return this.forEach(function(E,G){K.push(G)}),B(K)},Z.prototype.values=function(){var K=[];return this.forEach(function(E){K.push(E)}),B(K)},Z.prototype.entries=function(){var K=[];return this.forEach(function(E,G){K.push([G,E])}),B(K)},$.iterable)Z.prototype[Symbol.iterator]=Z.prototype.entries;function A(K){if(K.bodyUsed)return Promise.reject(new TypeError("Already read"));K.bodyUsed=!0}function q(K){return new Promise(function(E,G){K.onload=function(){E(K.result)},K.onerror=function(){G(K.error)}})}function m(K){var E=new FileReader,G=q(E);return E.readAsArrayBuffer(K),G}function b(K){var E=new FileReader,G=q(E);return E.readAsText(K),G}function l(K){var E=new Uint8Array(K),G=new Array(E.length);for(var U=0;U<E.length;U++)G[U]=String.fromCharCode(E[U]);return G.join("")}function H(K){if(K.slice)return K.slice(0);else{var E=new Uint8Array(K.byteLength);return E.set(new Uint8Array(K)),E.buffer}}function R(){if(this.bodyUsed=!1,this._initBody=function(K){if(this.bodyUsed=this.bodyUsed,this._bodyInit=K,!K)this._bodyText="";else if(typeof K==="string")this._bodyText=K;else if($.blob&&Blob.prototype.isPrototypeOf(K))this._bodyBlob=K;else if($.formData&&FormData.prototype.isPrototypeOf(K))this._bodyFormData=K;else if($.searchParams&&URLSearchParams.prototype.isPrototypeOf(K))this._bodyText=K.toString();else if($.arrayBuffer&&$.blob&&x(K))this._bodyArrayBuffer=H(K.buffer),this._bodyInit=new Blob([this._bodyArrayBuffer]);else if($.arrayBuffer&&(ArrayBuffer.prototype.isPrototypeOf(K)||h(K)))this._bodyArrayBuffer=H(K);else this._bodyText=K=Object.prototype.toString.call(K);if(!this.headers.get("content-type")){if(typeof K==="string")this.headers.set("content-type","text/plain;charset=UTF-8");else if(this._bodyBlob&&this._bodyBlob.type)this.headers.set("content-type",this._bodyBlob.type);else if($.searchParams&&URLSearchParams.prototype.isPrototypeOf(K))this.headers.set("content-type","application/x-www-form-urlencoded;charset=UTF-8")}},$.blob)this.blob=function(){var K=A(this);if(K)return K;if(this._bodyBlob)return Promise.resolve(this._bodyBlob);else if(this._bodyArrayBuffer)return Promise.resolve(new Blob([this._bodyArrayBuffer]));else if(this._bodyFormData)throw new Error("could not read FormData body as blob");else return Promise.resolve(new Blob([this._bodyText]))},this.arrayBuffer=function(){if(this._bodyArrayBuffer){var K=A(this);if(K)return K;if(ArrayBuffer.isView(this._bodyArrayBuffer))return Promise.resolve(this._bodyArrayBuffer.buffer.slice(this._bodyArrayBuffer.byteOffset,this._bodyArrayBuffer.byteOffset+this._bodyArrayBuffer.byteLength));else return Promise.resolve(this._bodyArrayBuffer)}else return this.blob().then(m)};if(this.text=function(){var K=A(this);if(K)return K;if(this._bodyBlob)return b(this._bodyBlob);else if(this._bodyArrayBuffer)return Promise.resolve(l(this._bodyArrayBuffer));else if(this._bodyFormData)throw new Error("could not read FormData body as text");else return Promise.resolve(this._bodyText)},$.formData)this.formData=function(){return this.text().then(i)};return this.json=function(){return this.text().then(JSON.parse)},this}var u=["DELETE","GET","HEAD","OPTIONS","POST","PUT"];function p(K){var E=K.toUpperCase();return u.indexOf(E)>-1?E:K}function Q(K,E){if(!(this instanceof Q))throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');E=E||{};var G=E.body;if(K instanceof Q){if(K.bodyUsed)throw new TypeError("Already read");if(this.url=K.url,this.credentials=K.credentials,!E.headers)this.headers=new Z(K.headers);if(this.method=K.method,this.mode=K.mode,this.signal=K.signal,!G&&K._bodyInit!=null)G=K._bodyInit,K.bodyUsed=!0}else this.url=String(K);if(this.credentials=E.credentials||this.credentials||"same-origin",E.headers||!this.headers)this.headers=new Z(E.headers);if(this.method=p(E.method||this.method||"GET"),this.mode=E.mode||this.mode||null,this.signal=E.signal||this.signal,this.referrer=null,(this.method==="GET"||this.method==="HEAD")&&G)throw new TypeError("Body not allowed for GET or HEAD requests");if(this._initBody(G),this.method==="GET"||this.method==="HEAD"){if(E.cache==="no-store"||E.cache==="no-cache"){var U=/([?&])_=[^&]*/;if(U.test(this.url))this.url=this.url.replace(U,"$1_="+new Date().getTime());else{var W=/\?/;this.url+=(W.test(this.url)?"&":"?")+"_="+new Date().getTime()}}}}Q.prototype.clone=function(){return new Q(this,{body:this._bodyInit})};function i(K){var E=new FormData;return K.trim().split("&").forEach(function(G){if(G){var U=G.split("="),W=U.shift().replace(/\+/g," "),L=U.join("=").replace(/\+/g," ");E.append(decodeURIComponent(W),decodeURIComponent(L))}}),E}function d(K){var E=new Z,G=K.replace(/\r?\n[\t ]+/g," ");return G.split("\r").map(function(U){return U.indexOf(`
`)===0?U.substr(1,U.length):U}).forEach(function(U){var W=U.split(":"),L=W.shift().trim();if(L){var S=W.join(":").trim();E.append(L,S)}}),E}R.call(Q.prototype);function M(K,E){if(!(this instanceof M))throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');if(!E)E={};this.type="default",this.status=E.status===void 0?200:E.status,this.ok=this.status>=200&&this.status<300,this.statusText=E.statusText===void 0?"":""+E.statusText,this.headers=new Z(E.headers),this.url=E.url||"",this._initBody(K)}R.call(M.prototype),M.prototype.clone=function(){return new M(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new Z(this.headers),url:this.url})},M.error=function(){var K=new M(null,{status:0,statusText:""});return K.type="error",K};var n=[301,302,303,307,308];M.redirect=function(K,E){if(n.indexOf(E)===-1)throw new RangeError("Invalid status code");return new M(null,{status:E,headers:{location:K}})},O.DOMException=Y.DOMException;try{new O.DOMException}catch(K){O.DOMException=function(E,G){this.message=E,this.name=G;var U=Error(E);this.stack=U.stack},O.DOMException.prototype=Object.create(Error.prototype),O.DOMException.prototype.constructor=O.DOMException}function T(K,E){return new Promise(function(G,U){var W=new Q(K,E);if(W.signal&&W.signal.aborted)return U(new O.DOMException("Aborted","AbortError"));var L=new XMLHttpRequest;function S(){L.abort()}L.onload=function(){var I={status:L.status,statusText:L.statusText,headers:d(L.getAllResponseHeaders()||"")};I.url="responseURL"in L?L.responseURL:I.headers.get("X-Request-URL");var C="response"in L?L.response:L.responseText;setTimeout(function(){G(new M(C,I))},0)},L.onerror=function(){setTimeout(function(){U(new TypeError("Network request failed"))},0)},L.ontimeout=function(){setTimeout(function(){U(new TypeError("Network request failed"))},0)},L.onabort=function(){setTimeout(function(){U(new O.DOMException("Aborted","AbortError"))},0)};function t(I){try{return I===""&&Y.location.href?Y.location.href:I}catch(C){return I}}if(L.open(W.method,t(W.url),!0),W.credentials==="include")L.withCredentials=!0;else if(W.credentials==="omit")L.withCredentials=!1;if("responseType"in L){if($.blob)L.responseType="blob";else if($.arrayBuffer&&W.headers.get("Content-Type")&&W.headers.get("Content-Type").indexOf("application/octet-stream")!==-1)L.responseType="arraybuffer"}if(E&&typeof E.headers==="object"&&!(E.headers instanceof Z))Object.getOwnPropertyNames(E.headers).forEach(function(I){L.setRequestHeader(I,z(E.headers[I]))});else W.headers.forEach(function(I,C){L.setRequestHeader(C,I)});if(W.signal)W.signal.addEventListener("abort",S),L.onreadystatechange=function(){if(L.readyState===4)W.signal.removeEventListener("abort",S)};L.send(typeof W._bodyInit==="undefined"?null:W._bodyInit)})}if(T.polyfill=!0,!Y.fetch)Y.fetch=T,Y.Headers=Z,Y.Request=Q,Y.Response=M;return O.Headers=Z,O.Request=Q,O.Response=M,O.fetch=T,O}({})})(V);V.fetch.ponyfill=!0;delete V.fetch.polyfill;var X=P.fetch?P:V;w=X.fetch;w.default=X.fetch;w.fetch=X.fetch;w.Headers=X.Headers;w.Request=X.Request;w.Response=X.Response;_.exports=w});var f=e(c(),1);class k extends Error{constructor(J){super(J);this.name="AuthenticationError"}}class j extends Error{constructor(J){super(J);this.name="ValidationError"}}class F extends Error{status;constructor(J,D){super(J);this.status=D;this.name="APIError"}}var UK="http://app.specify.sh/api";class v{publisherKey;constructor(J){if(!this.validatePublisherKey(J.publisherKey))throw new k("Invalid API key format");this.publisherKey=J.publisherKey}validatePublisherKey(J){return J.startsWith("spk_")&&J.length===34}validateAddress(J){return/^0x[a-fA-F0-9]{40}$/.test(J)}async serve(J){if(!this.validateAddress(J))throw new j("Invalid wallet address");let D;try{if(D=await f.default(`${UK}/ads`,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":this.publisherKey},body:JSON.stringify({walletAddress:J})}),!D.ok){if(D.status===404)return null;if(D.status===401)throw new k("Invalid API key");throw new F(`HTTP error! status: ${D.status}`,D.status)}return await D.json()}catch(O){if(O instanceof F)throw O;throw new F(`Failed to fetch ad content: ${O instanceof Error?O.message:"Unknown error"}`,O instanceof Error&&"status"in O?O.status:0)}}}export{v as default,j as ValidationError,k as AuthenticationError,F as APIError};
/* Built with ❤️ by Specify team */
//# debugId=012D0FBCFD3140FA64756E2164756E21