lanyard-wrapper
v2.0.1
Published
Fully-typed Lanyard API wrapper for JavaScript/TypeScript (in browser environment)
Downloads
13
Readme
Lanyard Wrapper
Fully-typed Lanyard API wrapper for JavaScript/TypeScript (in browser environment)
Looking for Node.js version? Check out node-lanyard-wrapper
Features
- Fully-typed
- Supports WebSocket connection
- Supports REST API
- Supports managing Lanyard KV
Installation
NPM
npm i lanyard-wrapper
Yarn
yarn add lanyard-wrapper
CDN
<script src="https://cdn.jsdelivr.net/npm/lanyard-wrapper/dist/index.browser.js"></script>
Usage
Check out Lanyard Wrapper Documentation for more information about the data object.
Browser
Connecting with WebSocket
<script src="https://cdn.jsdelivr.net/npm/lanyard-wrapper/dist/index.browser.js"></script>
<script type="text/javascript">
function onUpdate(data) {
// data is a Lanyard data object
console.log(data);
}
LanyardWrapper.connectWebSocket("USER_ID", onUpdate)
.then(ws => {
// ws is a WebSocket object
console.log(ws);
})
.catch(err => {
console.error(err);
});
</script>
OR Using the REST API
<script src="https://cdn.jsdelivr.net/npm/lanyard-wrapper/dist/index.browser.js"></script>
<script type="text/javascript">
LanyardWrapper.fetchUserData("USER_ID")
.then(data => {
console.log(data);
})
.catch(err => {
console.error(err);
});
LanyardWrapper.fetchUserDataForMultipleUsers(["USER_ID_1", "USER_ID_2"])
.then(data => {
console.log(data);
})
.catch(err => {
console.error(err);
});
</script>
CommonJS / ES Modules / TypeScript
Connecting with WebSocket
// Use require() if you are using CommonJS
const LanyardWrapper = require("lanyard-wrapper");
// Use import if you are using ES Modules
import { connectWebSocket } from "lanyard-wrapper";
function onUpdate(data) {
// data is a Lanyard data object
console.log(data);
}
connectWebSocket("USER_ID", onUpdate)
.then(ws => {
// ws is a WebSocket object
console.log(ws);
})
.catch(err => {
console.error(err);
});
OR Using the REST API
// Use require() if you are using CommonJS
const LanyardWrapper = require("lanyard-wrapper");
// Use import if you are using ES Modules
import { fetchUserData, fetchUserDataForMultipleUsers } from "lanyard-wrapper";
fetchUserData("USER_ID")
.then(data => {
console.log(data);
})
.catch(err => {
console.error(err);
});
fetchUserDataForMultipleUsers(["USER_ID_1", "USER_ID_2"])
.then(data => {
console.log(data);
})
.catch(err => {
console.error(err);
});
Contributing
Pull requests are welcome.