Skip to content

Commit

Permalink
chore: move storage to @gramio/storage
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Apr 4, 2024
1 parent 1416338 commit 368b178
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 40 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Session plugin for GramIO.

Currently not optimized and support only in-memory storage.
**!!!Currently not optimized!!!**

## Usage

Expand All @@ -24,3 +24,28 @@ const bot = new Bot(process.env.token!)

bot.start();
```

### Redis example

[More info](https://github.com/gramiojs/storages/tree/master/packages/redis)

```ts
import { Bot } from "gramio";
import { session } from "@gramio/session";
import { redisStorage } from "@gramio/storage-redis";

const bot = new Bot(process.env.token!)
.extend(
session({
key: "sessionKey",
initial: () => ({ apple: 1 }),
storage: redisStorage(),
})
)
.on("message", (context) => {
context.send(`🍏 apple count is ${++context.sessionKey.apple}`);
})
.onStart(console.log);

bot.start();
```
Binary file modified bun.lockb
Binary file not shown.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gramio/session",
"version": "0.0.1",
"version": "0.1.0",
"description": "Session plugin for GramIO",
"main": "dist/index.js",
"type": "commonjs",
Expand All @@ -19,15 +19,15 @@
"in-memory"
],
"devDependencies": {
"@types/bun": "^1.0.7",
"gramio": "^0.0.20",
"typescript": "^5.3.3"
"@types/bun": "^1.0.12",
"gramio": "^0.0.26",
"typescript": "^5.4.4"
},
"peerDependencies": {
"gramio": "^0.0.20"
},
"dependencies": {
"@gramio/types": "^7.1.0"
"@gramio/storage": "^0.0.1"
},
"files": [
"dist"
Expand Down
20 changes: 0 additions & 20 deletions src/in-memory-storage.ts

This file was deleted.

8 changes: 2 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { type Storage, inMemoryStorage } from "@gramio/storage";
import {
type BotLike,
type ContextType,
type MaybePromise,
Plugin,
} from "gramio";
import { inMemoryStorage } from "in-memory-storage";
import type { Storage } from "types";

interface SessionOptions<
Data = unknown,
Expand All @@ -19,14 +18,13 @@ interface SessionOptions<
initial?: (context: ContextType<BotLike, "message">) => MaybePromise<Data>;
}

// biome-ignore lint/suspicious/noExplicitAny: <explanation>
function createProxy(value: any, storage: Storage, sessionKey: string) {
return new Proxy(value, {
get(target, key) {
return target[key];
},
set(target, key, newValue) {
console.log("test", target, key, newValue);

target[key] = newValue;

storage.set(sessionKey, target);
Expand Down Expand Up @@ -59,11 +57,9 @@ export function session<Data = unknown, Key extends string = "session">(
Object.defineProperty(obj, key, {
enumerable: true,
get() {
console.log("get");
return createProxy(session, storage, sessionKey);
},
set(value) {
console.log("set", value);
// TODO: optimize it
storage.set(sessionKey, value);
},
Expand Down
8 changes: 0 additions & 8 deletions src/types.ts

This file was deleted.

0 comments on commit 368b178

Please sign in to comment.