Skip to content

Commit b51e409

Browse files
committed
chore: improve elysia example
1 parent 0595d2a commit b51e409

File tree

2 files changed

+68
-16
lines changed

2 files changed

+68
-16
lines changed

docs/ru/tma/init-data.md

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,48 @@ console.log(initData.hash); // Хэш для проверки
9191

9292
## Пример интеграции с Elysia
9393

94+
Этот пример демонстрирует, как интегрировать `@gramio/init-data` с Elysia с помощью удобного плагина с типо-безопасностью.
95+
96+
`examples` в `x-init-data` заголовке являются валидными сгенерированными `init-data`, которые позволяют легче тестировать ваш API в OpenAPI клиентах.
97+
9498
```ts twoslash
9599
import { validateAndParseInitData } from "@gramio/init-data";
96100
import { Elysia } from "elysia";
97101

98-
const authElysia = new Elysia()
99-
.derive(({ headers, error }) => {
100-
const initData = headers["x-init-data"];
101-
if (!initData) return error("Unauthorized");
102-
102+
export const authElysia = new Elysia({
103+
name: "auth",
104+
})
105+
.guard({
106+
headers: t.Object({
107+
"x-init-data": t.String({
108+
examples: [
109+
signInitData(
110+
{
111+
user: {
112+
id: 1,
113+
first_name: "durov",
114+
username: "durov",
115+
},
116+
},
117+
secretKey
118+
),
119+
],
120+
}),
121+
}),
122+
response: {
123+
401: t.Literal("UNAUTHORIZED"),
124+
},
125+
})
126+
.resolve(({ headers, error }) => {
103127
const result = validateAndParseInitData(
104-
initData,
105-
process.env.TOKEN as string
128+
headers["x-init-data"],
129+
config.BOT_TOKEN
106130
);
107-
if (!result || !result.user) return error("Unauthorized");
131+
if (!result || !result.user)
132+
return error("Unauthorized", "UNAUTHORIZED");
108133

109134
return {
135+
tgId: result.user.id,
110136
user: result.user,
111137
};
112138
})

docs/tma/init-data.md

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,48 @@ bun install @gramio/init-data
2626

2727
## Elysia integration example
2828

29+
This example shows how to integrate `@gramio/init-data` with Elysia via convenient plugin with type safety.
30+
31+
`examples` at `x-init-data` header is valid generated init-data which allows you to more easily test your API in OpenAPI Client.
32+
2933
```ts twoslash
3034
import { validateAndParseInitData } from "@gramio/init-data";
3135
import { Elysia } from "elysia";
3236

33-
const authElysia = new Elysia()
34-
.derive(({ headers, error }) => {
35-
const initData = headers["x-init-data"];
36-
if (!initData) return error("Unauthorized");
37-
37+
export const authElysia = new Elysia({
38+
name: "auth",
39+
})
40+
.guard({
41+
headers: t.Object({
42+
"x-init-data": t.String({
43+
examples: [
44+
signInitData(
45+
{
46+
user: {
47+
id: 1,
48+
first_name: "durov",
49+
username: "durov",
50+
},
51+
},
52+
secretKey
53+
),
54+
],
55+
}),
56+
}),
57+
response: {
58+
401: t.Literal("UNAUTHORIZED"),
59+
},
60+
})
61+
.resolve(({ headers, error }) => {
3862
const result = validateAndParseInitData(
39-
initData,
40-
process.env.TOKEN as string
63+
headers["x-init-data"],
64+
config.BOT_TOKEN
4165
);
42-
if (!result || !result.user) return error("Unauthorized");
66+
if (!result || !result.user)
67+
return error("Unauthorized", "UNAUTHORIZED");
4368

4469
return {
70+
tgId: result.user.id,
4571
user: result.user,
4672
};
4773
})

0 commit comments

Comments
 (0)