This commit is contained in:
+16
-16
@@ -27,34 +27,34 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@grammyjs/auto-chat-action": "0.1.1",
|
"@grammyjs/auto-chat-action": "0.1.1",
|
||||||
"@grammyjs/commands": "1.3.2",
|
"@grammyjs/commands": "1.0.8",
|
||||||
"@grammyjs/hydrate": "1.6.0",
|
"@grammyjs/hydrate": "1.4.1",
|
||||||
"@grammyjs/i18n": "1.1.2",
|
"@grammyjs/i18n": "1.1.2",
|
||||||
"@grammyjs/parse-mode": "1.11.1",
|
"@grammyjs/parse-mode": "1.11.1",
|
||||||
"@grammyjs/runner": "2.0.3",
|
"@grammyjs/runner": "2.0.3",
|
||||||
"@grammyjs/types": "3.25.0",
|
"@grammyjs/types": "3.20.0",
|
||||||
"@hono/node-server": "1.19.12",
|
"@hono/node-server": "1.14.2",
|
||||||
"@urql/core": "^6.0.1",
|
"@urql/core": "^6.0.1",
|
||||||
"axios": "^1.14.0",
|
"axios": "^1.14.0",
|
||||||
"callback-data": "1.1.1",
|
"callback-data": "1.1.1",
|
||||||
"grammy": "1.41.1",
|
"grammy": "1.36.1",
|
||||||
"hono": "4.12.9",
|
"hono": "4.10.3",
|
||||||
"iso-639-1": "3.1.5",
|
"iso-639-1": "3.1.5",
|
||||||
"pino": "10.3.1",
|
"pino": "9.9.0",
|
||||||
"pino-pretty": "13.1.3",
|
"pino-pretty": "13.0.0",
|
||||||
"tsx": "4.21.0",
|
"tsx": "4.20.4",
|
||||||
"valibot": "1.3.1"
|
"valibot": "0.42.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@antfu/eslint-config": "8.0.0",
|
"@antfu/eslint-config": "4.12.0",
|
||||||
"@eslint/js": "^10.0.1",
|
"@eslint/js": "^10.0.1",
|
||||||
"@types/node": "^25.5.0",
|
"@types/node": "^22.15.21",
|
||||||
"eslint": "^10.1.0",
|
"eslint": "^9.27.0",
|
||||||
"husky": "^9.1.7",
|
"husky": "^9.1.7",
|
||||||
"lint-staged": "^16.4.0",
|
"lint-staged": "^15.5.1",
|
||||||
"prettier": "3.8.1",
|
"prettier": "3.8.1",
|
||||||
"tsc-watch": "^7.2.0",
|
"tsc-watch": "^6.3.1",
|
||||||
"typescript": "^6.0.2",
|
"typescript": "^5.9.2",
|
||||||
"typescript-eslint": "^8.58.0"
|
"typescript-eslint": "^8.58.0"
|
||||||
},
|
},
|
||||||
"lint-staged": {
|
"lint-staged": {
|
||||||
|
|||||||
+13
-25
@@ -1,27 +1,15 @@
|
|||||||
import type { Config } from "#root/config.js";
|
import type { Context } from "#root/bot/context.js";
|
||||||
import type { Logger } from "#root/logger.js";
|
import path from "node:path";
|
||||||
import type { AutoChatActionFlavor } from "@grammyjs/auto-chat-action";
|
import process from "node:process";
|
||||||
import type { HydrateFlavor } from "@grammyjs/hydrate";
|
import { I18n } from "@grammyjs/i18n";
|
||||||
import type { I18nFlavor } from "@grammyjs/i18n";
|
|
||||||
import type { ParseModeFlavor } from "@grammyjs/parse-mode";
|
|
||||||
import type { Context as DefaultContext, SessionFlavor } from "grammy";
|
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
export const i18n = new I18n<Context>({
|
||||||
export interface SessionData {
|
defaultLocale: "en",
|
||||||
// field?: string;
|
directory: path.resolve(process.cwd(), "locales"),
|
||||||
}
|
useSession: true,
|
||||||
|
fluentBundleOptions: {
|
||||||
|
useIsolating: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
interface ExtendedContextFlavor {
|
export const isMultipleLocales = i18n.locales.length > 1;
|
||||||
logger: Logger;
|
|
||||||
config: Config;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Context = ParseModeFlavor<
|
|
||||||
HydrateFlavor<
|
|
||||||
DefaultContext &
|
|
||||||
ExtendedContextFlavor &
|
|
||||||
SessionFlavor<SessionData> &
|
|
||||||
I18nFlavor &
|
|
||||||
AutoChatActionFlavor
|
|
||||||
>
|
|
||||||
>;
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Composer } from "grammy";
|
|
||||||
import type { Context } from "#root/bot/context.js";
|
import type { Context } from "#root/bot/context.js";
|
||||||
import { logHandle } from "#root/bot/helpers/logging.js";
|
import { logHandle } from "#root/bot/helpers/logging.js";
|
||||||
|
import { Composer } from "grammy";
|
||||||
|
|
||||||
const composer = new Composer<Context>();
|
const composer = new Composer<Context>();
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,34 @@
|
|||||||
import type { Context, SessionData } from "#root/bot/context.js";
|
import type { Context } from "#root/bot/context.js";
|
||||||
import type { Middleware, SessionOptions } from "grammy";
|
import type { Middleware } from "grammy";
|
||||||
import { session as createSession } from "grammy";
|
import { performance } from "node:perf_hooks";
|
||||||
|
import { getUpdateInfo } from "#root/bot/helpers/logging.js";
|
||||||
|
|
||||||
type Options = Pick<
|
export function updateLogger(): Middleware<Context> {
|
||||||
SessionOptions<SessionData, Context>,
|
return async (ctx, next) => {
|
||||||
"getSessionKey" | "storage"
|
ctx.api.config.use((previous, method, payload, signal) => {
|
||||||
>;
|
ctx.logger.debug({
|
||||||
|
msg: "Bot API call",
|
||||||
export function session(options: Options): Middleware<Context> {
|
method,
|
||||||
return createSession({
|
payload
|
||||||
getSessionKey: options.getSessionKey,
|
|
||||||
storage: options.storage,
|
|
||||||
initial: () => ({})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return previous(method, payload, signal);
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.logger.debug({
|
||||||
|
msg: "Update received",
|
||||||
|
update: getUpdateInfo(ctx)
|
||||||
|
});
|
||||||
|
|
||||||
|
const startTime = performance.now();
|
||||||
|
try {
|
||||||
|
await next();
|
||||||
|
} finally {
|
||||||
|
const endTime = performance.now();
|
||||||
|
ctx.logger.debug({
|
||||||
|
msg: "Update processed",
|
||||||
|
elapsed: endTime - startTime
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,27 @@
|
|||||||
import type { MiddlewareHandler } from "hono";
|
import type { MiddlewareHandler } from "hono";
|
||||||
import { randomUUID } from "node:crypto";
|
import { getPath } from "hono/utils/url";
|
||||||
|
|
||||||
export function requestId(): MiddlewareHandler {
|
export function requestLogger(): MiddlewareHandler {
|
||||||
return async (c, next) => {
|
return async (c, next) => {
|
||||||
c.set("requestId", randomUUID());
|
const { method } = c.req;
|
||||||
|
const path = getPath(c.req.raw);
|
||||||
|
|
||||||
|
c.var.logger.debug({
|
||||||
|
msg: "Incoming request",
|
||||||
|
method,
|
||||||
|
path
|
||||||
|
});
|
||||||
|
const startTime = performance.now();
|
||||||
|
|
||||||
await next();
|
await next();
|
||||||
|
|
||||||
|
const endTime = performance.now();
|
||||||
|
c.var.logger.debug({
|
||||||
|
msg: "Request completed",
|
||||||
|
method,
|
||||||
|
path,
|
||||||
|
status: c.res.status,
|
||||||
|
elapsed: endTime - startTime
|
||||||
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user