"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { useLanyard: () => useLanyard }); module.exports = __toCommonJS(src_exports); var import_store = require("svelte/store"); var DEFAULT_REST_URL = "https://api.lanyard.rest/v1"; var DEFAULT_WS_URL = "wss://api.lanyard.rest/socket"; function useLanyard(config) { if (config.method === "rest") { return (0, import_store.readable)(void 0, (set) => lanyardRest(config, set)); } if (config.method === "ws") { if ("id" in config) { return (0, import_store.readable)(void 0, (set) => lanyardWS(config, set)); } return (0, import_store.readable)(void 0, (set) => lanyardWS(config, set)); } } function lanyardRest(config, set) { if (typeof window === "undefined") { return; } const restUrl = config.restUrl ?? DEFAULT_REST_URL; const lanyardFetch = async () => await fetch(`${restUrl}/users/${config.id}`).then( (res) => res.json() ); const updateStore = async () => { const res = await lanyardFetch(); if (res.success) { set(res.data); } else { throw new Error(res.error.message); } }; updateStore(); const timer = setInterval(updateStore, config.pollInterval ?? 5e3); return () => clearInterval(timer); } function lanyardWS(config, set) { if (typeof window === "undefined") { return; } const wsUrl = config.wsUrl ?? DEFAULT_WS_URL; const ws = new WebSocket(wsUrl); const send = (message) => ws.send(JSON.stringify(message)); const recv = (callback) => { ws.addEventListener("message", callback); }; const once = () => new Promise((res) => { const fn = (event) => { ws.removeEventListener("message", fn); res(JSON.parse(event.data)); }; ws.addEventListener("message", fn); }); const waitInit = () => new Promise((res, rej) => { const open = () => { ws.removeEventListener("open", open); res(); }; ws.addEventListener("open", open); const err = () => { ws.removeEventListener("error", err); rej(); }; ws.addEventListener("error", err); }); waitInit().then(async () => { if ("all" in config) { send({ op: 2 /* INITIALIZE */, d: { subscribe_to_all: true } }); } if ("ids" in config) { send({ op: 2 /* INITIALIZE */, d: { subscribe_to_ids: config.ids } }); } if ("id" in config) { send({ op: 2 /* INITIALIZE */, d: { subscribe_to_id: config.id } }); } const hello = await once(); const heartbeatInterval = hello.d.heartbeat_interval; const heartbeat = () => { send({ op: 3 /* HEARTBEAT */, d: void 0 }); }; setInterval(heartbeat, heartbeatInterval); const init = await once(); const state = init.d; set(state); recv((event) => { const update = JSON.parse(event.data); if ("user_id" in update.d) { const { user_id, ...data } = update.d; state[update.d.user_id] = data; set(state); } else { set({ ...update.d }); } }); }); return () => ws.close(); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useLanyard });