Skip to content

Commit 3ee8140

Browse files
committed
first commit
0 parents  commit 3ee8140

File tree

6 files changed

+133
-0
lines changed

6 files changed

+133
-0
lines changed

.eslintrc.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"env": {
3+
"es2021": true,
4+
"node": true
5+
},
6+
"extends": [
7+
"eslint:recommended",
8+
"plugin:@typescript-eslint/recommended"
9+
],
10+
"parser": "@typescript-eslint/parser",
11+
"parserOptions": {
12+
"ecmaVersion": "latest",
13+
"sourceType": "module"
14+
},
15+
"plugins": [
16+
"@typescript-eslint"
17+
],
18+
"rules": {
19+
}
20+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
bun.lockb

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
```
2+
bun install
3+
bun run dev
4+
```
5+
6+
```
7+
open http://localhost:3000
8+
```
9+
# stub

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"scripts": {
3+
"dev": "bun run --hot src/index.ts"
4+
},
5+
"dependencies": {
6+
"hono": "^3.12.7",
7+
"redis": "^4.6.12"
8+
},
9+
"devDependencies": {
10+
"@types/bun": "^1.0.0",
11+
"@typescript-eslint/eslint-plugin": "^6.19.1",
12+
"@typescript-eslint/parser": "^6.19.1",
13+
"eslint": "^8.56.0"
14+
}
15+
}

src/index.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { Hono } from "hono";
2+
import { getCookie } from "hono/cookie";
3+
import { createClient } from "redis";
4+
5+
const client = createClient();
6+
client.on("error", (err) => console.log("Redis Client Error", err));
7+
await client.connect();
8+
9+
type TModel = {
10+
path: string;
11+
method: string;
12+
data: object;
13+
status: number;
14+
};
15+
16+
const app = new Hono();
17+
18+
const create = (userId: string, value: TModel) => {
19+
client.set(
20+
`${userId}:${value.method}:${value.path}`,
21+
JSON.stringify({ status: value.status, data: value.data }),
22+
);
23+
};
24+
25+
const clear = async (userId: string) => {
26+
for await (const key of client.scanIterator({
27+
TYPE: "string",
28+
MATCH: `${userId}:*`,
29+
})) {
30+
client.del(key);
31+
}
32+
};
33+
34+
const get = (
35+
userId: string,
36+
method: string,
37+
path: string,
38+
): ReturnType<typeof client.get> => {
39+
return client.get(`${userId}:${method}:${path}`);
40+
};
41+
42+
app.post("/create", async (c) => {
43+
const userIdCookie = getCookie(c, "userId");
44+
if (!userIdCookie) return c.json({});
45+
const data = await c.req.json<TModel>();
46+
create(userIdCookie, data);
47+
return c.json({
48+
user: userIdCookie,
49+
...data,
50+
});
51+
});
52+
53+
app.post("/clear", (c) => {
54+
const userIdCookie = getCookie(c, "userId");
55+
if (!userIdCookie) return c.json({});
56+
clear(userIdCookie);
57+
return c.json({ cookie: userIdCookie });
58+
});
59+
60+
app.all("/api/*", async (c) => {
61+
const userIdCookie = getCookie(c, "userId");
62+
if (!userIdCookie) return c.json({});
63+
const path = c.req.path.replace("/api", "");
64+
const redisData = await get(userIdCookie, c.req.method, path);
65+
if (!redisData) return c.json({});
66+
const { status, data } = JSON.parse(redisData);
67+
c.status = status;
68+
return c.json(data);
69+
});
70+
71+
export default app;

tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2020"],
4+
"module": "es2022",
5+
"preserveConstEnums": true,
6+
"moduleResolution": "node",
7+
"sourceMap": true,
8+
"target": "es2022",
9+
"types": ["node"],
10+
"outDir": "dist",
11+
"esModuleInterop": true,
12+
"strict": true,
13+
"jsx": "react-jsx",
14+
"jsxImportSource": "hono/jsx"
15+
}
16+
}

0 commit comments

Comments
 (0)