Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add parse modes type hints, minor refactoring #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 15 additions & 31 deletions src/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
import type { Transformer } from "./deps.deno.ts";

const wellKnownParseModesMap = new Map([
["html", "HTML"],
["markdown", "Markdown"],
["markdownv2", "MarkdownV2"],
]);
import type { ParseMode, Transformer } from "./deps.deno.ts";

/**
* Creates a new transformer for the given parse mode.
* @param parseMode {string} The parse mode to use. If the parse mode is not in the well known parse modes map, it will be used as is.
* @see https://core.telegram.org/bots/api#formatting-options for well known parse modes.
* @param parseMode {ParseMode} The parse mode to use.
* @see https://core.telegram.org/bots/api#formatting-options for more information about formatting.
* @returns {Transformer} The transformer.
*/
const buildTransformer = (parseMode: string) => {
const normalisedParseMode =
wellKnownParseModesMap.get(parseMode.toLowerCase()) ?? parseMode;
if (!wellKnownParseModesMap.has(parseMode.toLowerCase())) {
console.warn(
`Could not find parse_mode: ${parseMode}. If this is a valid parse_mode, you should ignore this message.`,
);
}

const transformer: Transformer = (prev, method, payload, signal) => {
const buildTransformer = (parseMode: ParseMode): Transformer => (
(prev, method, payload, signal) => {
if (!payload || "parse_mode" in payload) {
return prev(method, payload, signal);
}
Expand All @@ -32,37 +18,35 @@ const buildTransformer = (parseMode: string) => {
"media" in payload &&
!("parse_mode" in payload.media)
) {
payload.media.parse_mode = normalisedParseMode;
payload.media.parse_mode = parseMode;
}
break;
break;

case "answerInlineQuery":
if ("results" in payload) {
for (let result of payload.results) {
for (const result of payload.results) {
if (
"input_message_content" in result &&
!("parse_mode" in result.input_message_content)
) {
result.input_message_content.parse_mode = normalisedParseMode;
}
else if (!("parse_mode" in result)) {
result.parse_mode = normalisedParseMode;
result.input_message_content.parse_mode = parseMode;
} else if (!("parse_mode" in result)) {
result.parse_mode = parseMode;
}
}
}
break;
break;

default:
payload = { ...payload, ...{ parse_mode: normalisedParseMode } };
payload = { ...payload, ...{ parse_mode: parseMode } };
}

return prev(
method,
payload,
signal,
);
};
return transformer;
};
}
);

export { buildTransformer as parseMode };