Skip to content

Commit

Permalink
✨ Support for self-hosted api
Browse files Browse the repository at this point in the history
  • Loading branch information
barbarbar338 committed Dec 6, 2022
1 parent a9dc6d3 commit a352046
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 227 deletions.
334 changes: 167 additions & 167 deletions LICENSE

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,47 @@ function App() {
export default App;
```

# 🤞 Using Self-Hosted API

You can use this package to connect to your own self-hosted Lanyard API. To do this, you need to pass the `apiUrl` option to the `useLanyard` hook. See [Lanyard self-hosting guide](https://github.com/Phineas/lanyard#self-host-with-docker) for more information.

Using without websocket:

```js
import { useLanyard } from "react-use-lanyard";

function App() {
const lanyard = useLanyard({
userId: "952574663916154960",
apiUrl: "lanyard.338.rocks",
});

return (
<pre>{!lanyard.isValidating && JSON.stringify(lanyard, null, 4)}</pre>
);
}

export default App;
```

Using with websocket:

```js
import { useLanyard } from "react-use-lanyard";

function App() {
const { loading, status /*, websocket */ } = useLanyard({
userId: "952574663916154960",
socket: true,
apiUrl: "lanyard.338.rocks",
});

return <pre>{!loading && JSON.stringify(status, null, 4)}</pre>;
}

export default App;
```

# 📄 License

Copyright © 2021 [Barış DEMİRCİ](https://github.com/barbarbar338).
Expand Down
98 changes: 48 additions & 50 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
{
"name": "react-use-lanyard",
"version": "0.1.4",
"main": "dist",
"typings": "dist",
"repository": {
"type": "git",
"url": "https://github.com/barbarbar338/react-use-lanyard"
},
"author": {
"email": "[email protected]",
"name": "Barış DEMİRCİ",
"url": "https://338.rocks/"
},
"license": "GPL-3.0",
"description": "🚀 Use Lanyard API easily in you React app!",
"scripts": {
"prebuild": "rimraf dist",
"build": "tsc",
"format": "prettier --write .",
"format:watch": "onchange . -- prettier --write {{changed}}",
"lint": "eslint --fix .",
"lint:watch": "onchange . -- eslint --fix {{changed}}",
"update": "taze major -w && yarn"
},
"devDependencies": {
"@types/node": "^18.7.23",
"@types/react": "^18.0.21",
"@types/react-dom": "^18.0.6",
"@typescript-eslint/eslint-plugin": "^5.38.1",
"@typescript-eslint/parser": "^5.38.1",
"eslint": "^8.24.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"onchange": "^7.1.0",
"prettier": "^2.7.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"taze": "^0.8.2",
"ts-node": "^10.9.1",
"typescript": "^4.8.4"
},
"peerDependencies": {
"react": ">=16"
},
"dependencies": {
"swr": "^1.3.0",
"tslib": "^2.4.0"
}
}
{
"name": "react-use-lanyard",
"version": "0.1.5",
"main": "dist",
"typings": "dist",
"repository": {
"type": "git",
"url": "https://github.com/barbarbar338/react-use-lanyard"
},
"author": {
"email": "[email protected]",
"name": "Barış DEMİRCİ",
"url": "https://338.rocks/"
},
"license": "GPL-3.0",
"description": "🚀 Use Lanyard API easily in you React app!",
"scripts": {
"prebuild": "rimraf dist",
"build": "tsc",
"format": "prettier --write .",
"lint": "eslint --fix .",
"update": "taze latest -w"
},
"devDependencies": {
"@types/node": "^18.11.11",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.9",
"@typescript-eslint/eslint-plugin": "^5.45.1",
"@typescript-eslint/parser": "^5.45.1",
"eslint": "^8.29.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"prettier": "^2.8.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"taze": "^0.8.4",
"ts-node": "^10.9.1",
"typescript": "^4.9.3"
},
"peerDependencies": {
"react": ">=16"
},
"dependencies": {
"swr": "^1.3.0",
"tslib": "^2.4.1"
}
}

3 changes: 1 addition & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export const API_URL = "https://lanyard.rest/v1";
export const WEBSOCKET_URL = "wss://lanyard.rest/socket";
export const API_URL = "lanyard.rest";
export const HEARTBEAT_INTERVAL = 30_000;
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./types";
export * from "./constants";
export * from "./lanyard";
export * from "./types";
18 changes: 11 additions & 7 deletions src/lanyard.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { useEffect, useState } from "react";
import useSWR from "swr";
import { API_URL, HEARTBEAT_INTERVAL } from "./constants";
import type {
LanyardData,
LanyardGeneric,
LanyardOptions,
LanyardResponse,
LanyardGeneric,
} from "./types";
import { API_URL, HEARTBEAT_INTERVAL, WEBSOCKET_URL } from "./constants";
import { useEffect, useState } from "react";
import useSWR from "swr";

export const useLanyard = <T extends LanyardOptions>(
options: T,
): LanyardGeneric<T> => {
const apiUrl = options.apiUrl || API_URL;

if (options.socket) {
const [status, setStatus] = useState<LanyardData>();
const [websocket, setWebsocket] = useState<WebSocket>();
Expand All @@ -35,7 +37,7 @@ export const useLanyard = <T extends LanyardOptions>(
const connectWebsocket = () => {
if (heartbeat) clearInterval(heartbeat);

socket = new WebSocket(WEBSOCKET_URL);
socket = new WebSocket(`wss://${apiUrl}/socket`);
setWebsocket(socket);
setLoading(true);

Expand Down Expand Up @@ -88,7 +90,7 @@ export const useLanyard = <T extends LanyardOptions>(
`lanyard_${options.userId}`,
async () => {
const req = await fetch(
`${API_URL}/users/${options.userId}`,
`https://${apiUrl}/v1/users/${options.userId}`,
);

const body = (await req.json()) as LanyardResponse;
Expand All @@ -104,7 +106,9 @@ export const useLanyard = <T extends LanyardOptions>(
const responseArray: LanyardResponse[] = [];

for (const id of options.userId) {
const req = await fetch(`${API_URL}/users/${id}`);
const req = await fetch(
`https://${apiUrl}/v1/users/${id}`,
);

const body = (await req.json()) as LanyardResponse;
if (body.error) throw new Error(body.error.message);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type LanyardGeneric<T> = T extends { socket: true }
export interface LanyardOptions {
userId: string | string[];
socket?: boolean;
apiUrl?: string;
}

export interface LanyardResponse {
Expand Down

0 comments on commit a352046

Please sign in to comment.