From 2d9cd3ad609cb7a6943d69916cb5d8eec9d0b567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20DEM=C4=B0RC=C4=B0?= Date: Tue, 28 Feb 2023 07:23:08 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20KV=20support=20(publish)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 25 ++++++++++++++ package.json | 95 ++++++++++++++++++++++++++-------------------------- src/index.ts | 1 + src/kv.ts | 19 +++++++++++ src/types.ts | 11 ++++++ 5 files changed, 103 insertions(+), 48 deletions(-) create mode 100644 src/kv.ts diff --git a/README.md b/README.md index 13eb5d7..f255d28 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/package.json b/package.json index e840c9a..a2003a4 100644 --- a/package.json +++ b/package.json @@ -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": "hi@338.rocks", - "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": "hi@338.rocks", + "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" + } +} diff --git a/src/index.ts b/src/index.ts index 826a017..2d40a89 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ export * from "./constants"; +export * from "./kv"; export * from "./lanyard"; export * from "./types"; diff --git a/src/kv.ts b/src/kv.ts new file mode 100644 index 0000000..f1e54b6 --- /dev/null +++ b/src/kv.ts @@ -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, + }, + }); diff --git a/src/types.ts b/src/types.ts index 2cbd5ea..e5f084a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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; export type LanyardSWRMultiple = SWRResponse;