























@@ -46,17 +46,9 @@ export type MessagePresentationTone = "info" | "success" | "warning" | "danger"
46464747export type MessagePresentationButtonStyle = InteractiveButtonStyle;
484849-export type MessagePresentationButton = {
50-label: string;
51-value?: string;
52-url?: string;
53-style?: MessagePresentationButtonStyle;
54-};
49+export type MessagePresentationButton = InteractiveReplyButton;
555056-export type MessagePresentationOption = {
57-label: string;
58-value: string;
59-};
51+export type MessagePresentationOption = InteractiveReplyOption;
60526153export type MessagePresentationTextBlock = {
6254type: "text";
@@ -124,11 +116,18 @@ function normalizePresentationTone(value: unknown): MessagePresentationTone | un
124116 : undefined;
125117}
126118127-function normalizeInteractiveButton(raw: unknown): InteractiveReplyButton | undefined {
119+function toRecord(raw: unknown): Record<string, unknown> | undefined {
128120if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
129121return undefined;
130122}
131-const record = raw as Record<string, unknown>;
123+return raw as Record<string, unknown>;
124+}
125+126+function normalizeButton(raw: unknown): InteractiveReplyButton | undefined {
127+const record = toRecord(raw);
128+if (!record) {
129+return undefined;
130+}
132131const label = normalizeOptionalString(record.label) ?? normalizeOptionalString(record.text);
133132const value =
134133normalizeOptionalString(record.value) ??
@@ -146,11 +145,11 @@ function normalizeInteractiveButton(raw: unknown): InteractiveReplyButton | unde
146145};
147146}
148147149-function normalizeInteractiveOption(raw: unknown): InteractiveReplyOption | undefined {
150-if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
148+function normalizeOption(raw: unknown): InteractiveReplyOption | undefined {
149+const record = toRecord(raw);
150+if (!record) {
151151return undefined;
152152}
153-const record = raw as Record<string, unknown>;
154153const label = normalizeOptionalString(record.label) ?? normalizeOptionalString(record.text);
155154const value = normalizeOptionalString(record.value);
156155if (!label || !value) {
@@ -159,30 +158,28 @@ function normalizeInteractiveOption(raw: unknown): InteractiveReplyOption | unde
159158return { label, value };
160159}
161160161+function normalizeList<T>(value: unknown, normalizeEntry: (entry: unknown) => T | undefined): T[] {
162+return Array.isArray(value)
163+ ? value.map((entry) => normalizeEntry(entry)).filter((entry): entry is T => Boolean(entry))
164+ : [];
165+}
166+162167function normalizeInteractiveBlock(raw: unknown): InteractiveReplyBlock | undefined {
163-if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
168+const record = toRecord(raw);
169+if (!record) {
164170return undefined;
165171}
166-const record = raw as Record<string, unknown>;
167172const type = normalizeOptionalLowercaseString(record.type);
168173if (type === "text") {
169174const text = normalizeOptionalString(record.text);
170175return text ? { type: "text", text } : undefined;
171176}
172177if (type === "buttons") {
173-const buttons = Array.isArray(record.buttons)
174- ? record.buttons
175-.map((entry) => normalizeInteractiveButton(entry))
176-.filter((entry): entry is InteractiveReplyButton => Boolean(entry))
177- : [];
178+const buttons = normalizeList(record.buttons, normalizeButton);
178179return buttons.length > 0 ? { type: "buttons", buttons } : undefined;
179180}
180181if (type === "select") {
181-const options = Array.isArray(record.options)
182- ? record.options
183-.map((entry) => normalizeInteractiveOption(entry))
184-.filter((entry): entry is InteractiveReplyOption => Boolean(entry))
185- : [];
182+const options = normalizeList(record.options, normalizeOption);
186183return options.length > 0
187184 ? {
188185type: "select",
@@ -195,50 +192,19 @@ function normalizeInteractiveBlock(raw: unknown): InteractiveReplyBlock | undefi
195192}
196193197194export function normalizeInteractiveReply(raw: unknown): InteractiveReply | undefined {
198-if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
195+const record = toRecord(raw);
196+if (!record) {
199197return undefined;
200198}
201-const record = raw as Record<string, unknown>;
202-const blocks = Array.isArray(record.blocks)
203- ? record.blocks
204-.map((entry) => normalizeInteractiveBlock(entry))
205-.filter((entry): entry is InteractiveReplyBlock => Boolean(entry))
206- : [];
199+const blocks = normalizeList(record.blocks, normalizeInteractiveBlock);
207200return blocks.length > 0 ? { blocks } : undefined;
208201}
209202210-function normalizePresentationButton(raw: unknown): MessagePresentationButton | undefined {
211-if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
212-return undefined;
213-}
214-const record = raw as Record<string, unknown>;
215-const label = normalizeOptionalString(record.label) ?? normalizeOptionalString(record.text);
216-const value =
217-normalizeOptionalString(record.value) ??
218-normalizeOptionalString(record.callbackData) ??
219-normalizeOptionalString(record.callback_data);
220-const url = normalizeOptionalString(record.url);
221-if (!label || (!value && !url)) {
222-return undefined;
223-}
224-return {
225- label,
226- ...(value ? { value } : {}),
227- ...(url ? { url } : {}),
228-style: normalizeButtonStyle(record.style),
229-};
230-}
231-232-function normalizePresentationOption(raw: unknown): MessagePresentationOption | undefined {
233-const option = normalizeInteractiveOption(raw);
234-return option ? { label: option.label, value: option.value } : undefined;
235-}
236-237203function normalizePresentationBlock(raw: unknown): MessagePresentationBlock | undefined {
238-if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
204+const record = toRecord(raw);
205+if (!record) {
239206return undefined;
240207}
241-const record = raw as Record<string, unknown>;
242208const type = normalizeOptionalLowercaseString(record.type);
243209if (type === "text" || type === "context") {
244210const text = normalizeOptionalString(record.text);
@@ -248,19 +214,11 @@ function normalizePresentationBlock(raw: unknown): MessagePresentationBlock | un
248214return { type: "divider" };
249215}
250216if (type === "buttons") {
251-const buttons = Array.isArray(record.buttons)
252- ? record.buttons
253-.map((entry) => normalizePresentationButton(entry))
254-.filter((entry): entry is MessagePresentationButton => Boolean(entry))
255- : [];
217+const buttons = normalizeList(record.buttons, normalizeButton);
256218return buttons.length > 0 ? { type: "buttons", buttons } : undefined;
257219}
258220if (type === "select") {
259-const options = Array.isArray(record.options)
260- ? record.options
261-.map((entry) => normalizePresentationOption(entry))
262-.filter((entry): entry is MessagePresentationOption => Boolean(entry))
263- : [];
221+const options = normalizeList(record.options, normalizeOption);
264222return options.length > 0
265223 ? {
266224type: "select",
@@ -273,15 +231,11 @@ function normalizePresentationBlock(raw: unknown): MessagePresentationBlock | un
273231}
274232275233export function normalizeMessagePresentation(raw: unknown): MessagePresentation | undefined {
276-if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
234+const record = toRecord(raw);
235+if (!record) {
277236return undefined;
278237}
279-const record = raw as Record<string, unknown>;
280-const blocks = Array.isArray(record.blocks)
281- ? record.blocks
282-.map((entry) => normalizePresentationBlock(entry))
283-.filter((entry): entry is MessagePresentationBlock => Boolean(entry))
284- : [];
238+const blocks = normalizeList(record.blocks, normalizePresentationBlock);
285239const title = normalizeOptionalString(record.title);
286240if (!title && blocks.length === 0) {
287241return undefined;
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。