UNPKG

@interlinklabs/mdk

Version:

Interlink Labs MDK – universal React/TS SDK

2 lines (1 loc) 5.27 kB
import{serialize as e,parse as t}from"cookie";import{jsx as n,Fragment as i}from"react/jsx-runtime";import{useState as a,useEffect as r}from"react";import{jwtDecode as o}from"jwt-decode";const s="e97ae0aa6520499d9edf20bd5a1e13c7",l=globalThis.fetch,c=async(e,t,n)=>{const i=await l("https://interlink-mini-app.interlinklabs.ai/api/ads/intent",{method:"POST",headers:{"Content-Type":"application/json","api-public":s,Authorization:`Bearer ${e}`},body:JSON.stringify({rewardType:t,value:n})});if(!i.ok)throw new Error("createIntent failed");const a=await i.json(),r=a?.data?.intentId??a?.intentId,o=a?.data?.challenge??a?.challenge;if(!r||!o)throw new Error("INVALID_INTENT_RESPONSE");return{intentId:r,challenge:o}},d=async(e,t)=>{const n=await l("https://interlink-mini-app.interlinklabs.ai/api/ads/claim",{method:"POST",headers:{"Content-Type":"application/json","api-public":s,Authorization:`Bearer ${e}`},body:JSON.stringify({intentId:t})});if(!n.ok)throw new Error("claim failed");return n.json()};let u=null;const p=()=>u;let w,f;function h(e,t){w=e,f=t,u=E}const E={get:e=>w?.headers.cookie?t(w.headers.cookie)[e]??null:null,set(t,n,i=1){if(!f)return;const a=new Date(Date.now()+864e5*i);f.setHeader("Set-Cookie",e(t,n,{path:"/",expires:a,httpOnly:!1}))},del(t){f&&f.setHeader("Set-Cookie",e(t,"",{path:"/",expires:new Date(0)}))}};var _=Object.freeze({__proto__:null,cookieDriver:E,init:h});const k=globalThis.fetch;function y(e){try{return o(e).loginId??null}catch{return null}}var g;!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"}(g||(g={}));const m="undefined"!=typeof window;let N=!1,O=null,v=null,I=null;const T=function({appid:e,onSuccess:t,onFailure:o,children:s}){const[l,c]=a(null),[p,w]=a(null),[f,h]=a(!1),[E,y]=a(null),[g,N]=a(null),O=`jwt_${e}`;r((()=>{(async function(){return u||(u="undefined"!=typeof window?(await Promise.resolve().then((function(){return L}))).cookieDriver:(await Promise.resolve().then((function(){return _}))).cookieDriver,u)})().then(c)}),[]);const v=()=>l?.get(O)??null,I=async n=>{const i=v()||n?.token;if(!i)return l?.del(O),h(!1),void o();await async function(e,t){try{const n=await k("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}}(i,e)?(l?.set(O,i,1),h(!0),t()):(l?.del(O),h(!1),o())};r((()=>{if(!l)return;I();const e=e=>{try{const t=JSON.parse(e.data);switch(t.action){case"pass":I(t);break;case"fail":o();break;case"ad_verified":T();break;case"ad_closed":g?.("USER_CLOSED")}}catch{}};return m&&window.addEventListener("message",e),()=>{m&&window.removeEventListener("message",e)}}),[l]);const T=async()=>{const e=v();if(!e||!p)return g?.("NO_TOKEN_OR_INTENT");try{E?.(await d(e,p.intentId))}catch(e){g?.(e)}finally{w(null),y(null),N(null)}};return!l||f?null:n(i,{children:s({open:()=>{m&&window.ReactNativeWebView?.postMessage(JSON.stringify({action:"pass",appid:e}))}})})};T.getLoginId=e=>{const t=p();return y(t?.get(`jwt_${e}`)??"")},T.getUserName=async function(e){try{const t=await k(`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}},T.logOut=e=>{p()?.del(`jwt_${e}`)},T.getProfileUser=async e=>{try{const t=p(),n=t?.get(`jwt_${e}`);if(!n)return null;const i=y(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}},T.showRewardedAd=async(e,t,n,i,a)=>{const r=p(),o=r?.get(`jwt_${e}`);if(!o)return a("NO_TOKEN");try{const r=await c(o,t,n);v=i,I=a,m&&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)}},T.claimIntent=d,T.enableAdEvents=function(e={}){if(N||"undefined"==typeof window)return;const{autoClaim:t=!0,onVerified:n,onClosed:i}=e;O=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 T.claimIntent(t,e);v?.(n)}catch(e){I?.(e)}break}case"ad_closed":i?i():I?.("USER_CLOSED")}},window.addEventListener("message",O),N=!0},T.disableAdEvents=function(){N&&O&&(window.removeEventListener("message",O),O=null,N=!1)};const b={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 L=Object.freeze({__proto__:null,cookieDriver:b});export{T as Mdk,d as claimIntent,c as createIntent,T as default,h as initNodeCookies};