Skip to content

Commit 2d50d4a

Browse files
committed
refactor: clean up schema field assertions
1 parent 6d7d975 commit 2d50d4a

2 files changed

Lines changed: 33 additions & 35 deletions

File tree

‎src/helpers.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
1-
import type { Version } from "@gramio/schema-parser";
1+
import type { Method, Version } from "@gramio/schema-parser";
2+
3+
export function assertMethodFields(
4+
methods: Method[],
5+
requirements: Record<string, readonly string[]>,
6+
) {
7+
const missing = Object.entries(requirements).flatMap(
8+
([methodName, fields]) => {
9+
const method = methods.find(({ name }) => name === methodName);
10+
11+
return method
12+
? fields
13+
.filter(
14+
(field) =>
15+
!method.parameters.some((parameter) => parameter.key === field),
16+
)
17+
.map((field) => `${methodName}.${field}`)
18+
: [methodName];
19+
},
20+
);
21+
22+
if (missing.length)
23+
throw new Error(
24+
`Required Bot API schema fields are missing: ${missing.join(", ")}`,
25+
);
26+
}
227

328
export class CodeGenerator {
429
static generateComment(value: string | string[]) {

‎src/index.ts‎

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { type Field, getCustomSchema } from "@gramio/schema-parser";
44
import prettier from "prettier";
55
import { OUTPUT_PATH, PRETTIER_OPTIONS } from "./config";
66
import { APIMethods, Objects, Params } from "./entities";
7-
import { CodeGenerator, generateHeader } from "./helpers";
7+
import { CodeGenerator, assertMethodFields, generateHeader } from "./helpers";
88

99
export interface IGeneratedFile {
1010
name: string;
@@ -31,37 +31,12 @@ const schema = await getCustomSchema();
3131
}
3232

3333
const { methods } = schema;
34-
const objects = schema.objects
34+
const objects = schema.objects;
3535

36-
// Telegram added the same stop controls to both draft methods in Bot API 10.3.
37-
// The upstream HTML briefly exposed them on sendMessageDraft only, which made a
38-
// successful regeneration silently remove the rich-draft fields. Keep this
39-
// assertion next to the schema boundary so no release can regress either
40-
// declaration even if the source page or parser changes again.
41-
{
42-
const requiredDraftFields = ["can_stop", "keep_on_stop"];
43-
const draftMethods = ["sendMessageDraft", "sendRichMessageDraft"];
44-
const problems: string[] = [];
45-
46-
for (const methodName of draftMethods) {
47-
const method = methods.find((candidate) => candidate.name === methodName);
48-
if (!method) {
49-
problems.push(`${methodName} is missing`);
50-
continue;
51-
}
52-
53-
for (const field of requiredDraftFields)
54-
if (!method.parameters.some((parameter) => parameter.key === field))
55-
problems.push(`${methodName}.${field} is missing`);
56-
}
57-
58-
if (problems.length)
59-
throw new Error(
60-
`Bot API draft controls are incomplete in the parsed schema:\n ${problems.join(
61-
"\n ",
62-
)}`,
63-
);
64-
}
36+
assertMethodFields(methods, {
37+
sendMessageDraft: ["can_stop", "keep_on_stop"],
38+
sendRichMessageDraft: ["can_stop", "keep_on_stop"],
39+
});
6540

6641
// Guard against silently losing FormattableString support. Telegram documents a
6742
// `parse_mode` sibling for exactly those text fields that accept formatting
@@ -135,9 +110,7 @@ const objects = schema.objects
135110
// We derive the set once so generators can emit `| { toJSON(): T }` unions.
136111

137112
const markupTypes = new Set(
138-
objects
139-
.filter((o) => o.semanticType === "markup")
140-
.map((o) => o.name),
113+
objects.filter((o) => o.semanticType === "markup").map((o) => o.name),
141114
);
142115

143116
// ─── File generation ──────────────────────────────────────────────────────────

0 commit comments

Comments
 (0)