UNPKG

therungg

Version:
2 lines (1 loc) 1.56 kB
import p from"isomorphic-ws";var s="https://therun.gg/api",i="https://d1qsrp2avfthuv.cloudfront.net",a="wss://fh76djw1t9.execute-api.eu-west-1.amazonaws.com/prod";var n=class{_websocket;onOpen=void 0;onClose=void 0;onMessage=void 0;onError=void 0;constructor(o){this._websocket=new p(`${a}${o?`?username=${o}`:""}`),this._websocket.onopen=()=>this.onOpen?this.onOpen():console.info("WebSocket connection to TheRun's Live API established."),this._websocket.onclose=()=>this.onClose?this.onClose():console.info("WebSocket connection to TheRun's Live API closed."),this._websocket.onmessage=e=>{this.onMessage&&this.onMessage(JSON.parse(e.data))},this._websocket.onerror=e=>this.onError?this.onError(e):console.error(e.message)}get connection(){return this._websocket}};var t=async(r,o)=>{try{let e=await fetch(`${o||s}/${r}`);if(!e.ok)throw new Error(e.status.toString());return await e.json()}catch(e){throw e instanceof Error?e.message==="404"?new Error("Not Found"):e.message==="429"?new Error("Rate Limited"):e.message==="500"?new Error("Bad Request"):e:new Error("Something went wrong")}};var c=async()=>await t("frontpagedata");var m=async r=>await t(r,i);var u=async()=>await t("live");var f=async r=>{let o=`live/${r}`;return await t(o)};var l=async r=>{let o=`users/${r}`;return{runs:await t(o)}};var R=async()=>await t("games");var d=async r=>{let o=`games/${r}`;return await t(o)};export{n as LiveWebSocket,R as getAllGames,u as getAllLiveRuns,c as getFrontPageData,d as getGame,m as getHistory,f as getLiveRun,l as getUserProfile,t as httpFetch};