@interlinklabs/mdk
Version:
Interlink Labs MDK – universal React/TS SDK
2 lines (1 loc) • 5.38 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("cookie"),t=require("react/jsx-runtime"),n=require("react"),i=require("jwt-decode");const a="e97ae0aa6520499d9edf20bd5a1e13c7",r=globalThis.fetch,o=async(e,t,n)=>{const i=await r("https://interlink-mini-app.interlinklabs.ai/api/ads/intent",{method:"POST",headers:{"Content-Type":"application/json","api-public":a,Authorization:`Bearer ${e}`},body:JSON.stringify({rewardType:t,value:n})});if(!i.ok)throw new Error("createIntent failed");const o=await i.json(),s=o?.data?.intentId??o?.intentId,c=o?.data?.challenge??o?.challenge;if(!s||!c)throw new Error("INVALID_INTENT_RESPONSE");return{intentId:s,challenge:c}},s=async(e,t)=>{const n=await r("https://interlink-mini-app.interlinklabs.ai/api/ads/claim",{method:"POST",headers:{"Content-Type":"application/json","api-public":a,Authorization:`Bearer ${e}`},body:JSON.stringify({intentId:t})});if(!n.ok)throw new Error("claim failed");return n.json()};let c=null;const l=()=>c;let u,d;function p(e,t){u=e,d=t,c=w}const w={get:t=>u?.headers.cookie?e.parse(u.headers.cookie)[t]??null:null,set(t,n,i=1){if(!d)return;const a=new Date(Date.now()+864e5*i);d.setHeader("Set-Cookie",e.serialize(t,n,{path:"/",expires:a,httpOnly:!1}))},del(t){d&&d.setHeader("Set-Cookie",e.serialize(t,"",{path:"/",expires:new Date(0)}))}};var f=Object.freeze({__proto__:null,cookieDriver:w,init:p});const h=globalThis.fetch;function E(e){try{return i.jwtDecode(e).loginId??null}catch{return null}}var _;!function(e){e.G_PINK="G_PINK",e.G_VIOLET="G_VIOLET",e.G_STEEL="G_STEEL",e.G_BLUE="G_BLUE",e.G_PURPLE="G_PURPLE",e.G_PEACH="G_PEACH",e.PINK="PINK",e.BLUE="BLUE",e.PURPLE="PURPLE",e.GREEN="GREEN",e.YELLOW="YELLOW",e.ORANGE="ORANGE",e.WHITE="WHITE"}(_||(_={}));const k="undefined"!=typeof window;let y=!1,g=null,N=null,O=null;const m=function({appid:e,onSuccess:i,onFailure:a,children:r}){const[o,l]=n.useState(null),[u,d]=n.useState(null),[p,w]=n.useState(!1),[E,_]=n.useState(null),[y,g]=n.useState(null),N=`jwt_${e}`;n.useEffect((()=>{(async function(){return c||(c="undefined"!=typeof window?(await Promise.resolve().then((function(){return I}))).cookieDriver:(await Promise.resolve().then((function(){return f}))).cookieDriver,c)})().then(l)}),[]);const O=()=>o?.get(N)??null,m=async t=>{const n=O()||t?.token;if(!n)return o?.del(N),w(!1),void a();await async function(e,t){try{const n=await h("https://interlink-mini-app.interlinklabs.ai/api/tracking/validate-token",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({token:e,appId:t})});return!!n.ok&&!!await n.json()}catch{return!1}}(n,e)?(o?.set(N,n,1),w(!0),i()):(o?.del(N),w(!1),a())};n.useEffect((()=>{if(!o)return;m();const e=e=>{try{const t=JSON.parse(e.data);switch(t.action){case"pass":m(t);break;case"fail":a();break;case"ad_verified":v();break;case"ad_closed":y?.("USER_CLOSED")}}catch{}};return k&&window.addEventListener("message",e),()=>{k&&window.removeEventListener("message",e)}}),[o]);const v=async()=>{const e=O();if(!e||!u)return y?.("NO_TOKEN_OR_INTENT");try{E?.(await s(e,u.intentId))}catch(e){y?.(e)}finally{d(null),_(null),g(null)}};return!o||p?null:t.jsx(t.Fragment,{children:r({open:()=>{k&&window.ReactNativeWebView?.postMessage(JSON.stringify({action:"pass",appid:e}))}})})};m.getLoginId=e=>{const t=l();return E(t?.get(`jwt_${e}`)??"")},m.getUserName=async function(e){try{const t=await h(`https://interlink-mini-app.interlinklabs.ai/api/tracking/profile/${e}`);if(!t.ok)return null;const n=await t.json();return n?.data?.username??null}catch{return null}},m.logOut=e=>{l()?.del(`jwt_${e}`)},m.getProfileUser=async e=>{try{const t=l(),n=t?.get(`jwt_${e}`);if(!n)return null;const i=E(n);if(!i)return null;const a=await fetch(`https://interlink-mini-app.interlinklabs.ai/api/tracking/profile/${i}`);if(!a.ok)return null;const r=await a.json();return r?.data}catch{return null}},m.showRewardedAd=async(e,t,n,i,a)=>{const r=l(),s=r?.get(`jwt_${e}`);if(!s)return a("NO_TOKEN");try{const r=await o(s,t,n);N=i,O=a,k&&window.ReactNativeWebView&&window.ReactNativeWebView.postMessage(JSON.stringify({action:"show_ad",challenge:r.challenge,rewardType:t,value:n,intentId:r.intentId,appid:e}))}catch(e){a(e)}},m.claimIntent=s,m.enableAdEvents=function(e={}){if(y||"undefined"==typeof window)return;const{autoClaim:t=!0,onVerified:n,onClosed:i}=e;g=async e=>{if("string"!=typeof e.data)return;let a;try{a=JSON.parse(e.data)}catch{return}switch(a.action){case"ad_verified":{const{intentId:e}=a;if(!e)return;if(n)n(a);else if(t)try{const t=document.cookie.split("; ").find((e=>e.startsWith(`jwt_${a.appid}=`)))?.split("=")[1]??"";if(!t)throw new Error("NO_TOKEN");const n=await m.claimIntent(t,e);N?.(n)}catch(e){O?.(e)}break}case"ad_closed":i?i():O?.("USER_CLOSED")}},window.addEventListener("message",g),y=!0},m.disableAdEvents=function(){y&&g&&(window.removeEventListener("message",g),g=null,y=!1)};const v={get(e){const t=`; ${document.cookie}`.split(`; ${e}=`);return 2===t.length?t.pop().split(";")[0]??null:null},set(e,t,n=1){const i=new Date(Date.now()+864e5*n).toUTCString();document.cookie=`${e}=${t}; expires=${i}; path=/`},del(e){document.cookie=`${e}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`}};var I=Object.freeze({__proto__:null,cookieDriver:v});exports.Mdk=m,exports.claimIntent=s,exports.createIntent=o,exports.default=m,exports.initNodeCookies=p;