@grammyjs/hydrate
Version:
Hydration plugin for grammY
107 lines (106 loc) • 3.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.hydrate = hydrate;
exports.hydrateContext = hydrateContext;
exports.hydrateApi = hydrateApi;
const chat_js_1 = require("./data/chat.js");
const inline_message_js_1 = require("./data/inline-message.js");
const message_js_1 = require("./data/message.js");
const update_js_1 = require("./data/update.js");
const user_js_1 = require("./data/user.js");
const deps_node_js_1 = require("./deps.node.js");
/**
* Plugin that hydrates the context object and API call results, and equips the
* objects with useful methods that are calling `bot.api` with values prefilled
* from the object they are installed on.
*
* For example, this plugin allows you to use `await ctx.message.delete()` instead of
* `await ctx.deleteMessage()`.
*
* Check out [the official plugin
* documentation](https://grammy.dev/plugins/hydrate.html) on the grammY
* webiste.
*/
function hydrate() {
const hydrator = hydrateApi();
return (ctx, next) => {
ctx.api.config.use(hydrator);
(0, update_js_1.installUpdateMethods)(ctx.api.raw, ctx.update);
return next();
};
}
function hydrateContext() {
return (ctx, next) => {
(0, update_js_1.installUpdateMethods)(ctx.api.raw, ctx.update);
return next();
};
}
function hydrateApi() {
const t = async (prev, method, payload, signal) => {
const res = await prev(method, payload, signal);
if (res.ok) {
if (isMessage(res.result)) {
(0, message_js_1.installMessageMethods)(toApi(prev), res.result);
}
else if (isInlineMessage(res.result)) {
(0, inline_message_js_1.installInlineMessageMethods)(toApi(prev), res.result);
}
else if (isChatMember(res.result) && hasChatId(payload)) {
(0, user_js_1.installUserMethods)(toApi(prev), res.result.user, payload.chat_id);
}
else if (isChat(res.result)) {
(0, chat_js_1.installChatMethods)(toApi(prev), res.result);
}
// TODO: hydrate other method call results
}
return res;
};
return t;
}
function isMessage(obj) {
return (typeof obj === "object" &&
obj !== null &&
"message_id" in obj &&
"chat" in obj);
}
function isInlineMessage(obj) {
return (typeof obj === "object" &&
obj !== null &&
"inline_message_id" in obj);
}
function isChatMember(obj) {
return (typeof obj === "object" &&
obj !== null &&
"status" in obj &&
"user" in obj);
}
function isChat(obj) {
return (typeof obj === "object" &&
obj !== null &&
"id" in obj &&
"type" in obj &&
typeof obj.type === "string" &&
["private", "group", "supergroup", "channel"].includes(obj.type));
}
function hasChatId(obj) {
return (typeof obj === "object" &&
obj !== null &&
"chat_id" in obj &&
typeof obj.chat_id === "number");
}
function toApi(connector) {
return new Proxy({}, {
get(_, method) {
const api = connector.bind(null, method);
return async (...args) => {
const data = await api(...args);
if (data.ok) {
return data.result;
}
else {
throw new deps_node_js_1.GrammyError(`Call to '${method}' failed!`, data, method, args[0]);
}
};
},
});
}