Skip to content

Commit

Permalink
✨ Add KV support (publish)
Browse files Browse the repository at this point in the history
  • Loading branch information
barbarbar338 committed Feb 28, 2023
1 parent 8bb273e commit 2d9cd3a
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 48 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ function App() {
export default App;
```

# 🔐 KV Support

You can create/delete KV pairs using this package.

```js
import { set, del } from "react-use-lanyard";

// Set KV pair
await set({
apiKey: "your_api_key", // get it using .apikey command on lanyard bot
userId: "your_user_id",
key: "test_key",
value: "test value",
// apiUrl: "lanyard.338.rocks", // if you are using self-hosted api, not required by default
});

// Delete KV pair
await del({
apiKey: "your_api_key",
userId: "your_user_id",
key: "test_key",
// apiUrl: "lanyard.338.rocks", // if you are using self-hosted api, not required by default
});
```

# 🤞 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.
Expand Down
95 changes: 47 additions & 48 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
{
"name": "react-use-lanyard",
"version": "0.2.0",
"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"
}
}

{
"name": "react-use-lanyard",
"version": "0.3.0",
"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.14.2",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^5.54.0",
"eslint": "^8.35.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.27.5",
"prettier": "^2.8.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"taze": "^0.8.5",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
},
"peerDependencies": {
"react": ">=16"
},
"dependencies": {
"swr": "^2.0.4",
"tslib": "^2.5.0"
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./constants";
export * from "./kv";
export * from "./lanyard";
export * from "./types";
19 changes: 19 additions & 0 deletions src/kv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { API_URL } from "./constants";
import { IDelete, ISet } from "./types";

export const set = async ({ apiKey, key, userId, value, apiUrl }: ISet) =>
fetch(`https://${apiUrl || API_URL}/v1/users/${userId}/kv/${key}`, {
method: "PUT",
headers: {
Authorization: apiKey,
},
body: value,
});

export const del = async ({ apiKey, key, userId, apiUrl }: IDelete) =>
fetch(`https://${apiUrl || API_URL}/v1/users/${userId}/kv/${key}`, {
method: "DELETE",
headers: {
Authorization: apiKey,
},
});
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import type { SWRResponse } from "swr";

export interface IDelete {
key: string;
userId: string;
apiKey: string;
apiUrl?: string;
}

export interface ISet extends IDelete {
value: string;
}

export type LanyardSWRSingle = SWRResponse<LanyardResponse, any>;
export type LanyardSWRMultiple = SWRResponse<LanyardResponse[], any>;

Expand Down

0 comments on commit 2d9cd3a

Please sign in to comment.