Files
trade-message-center/packages/onetalk-contract/src/guards.ts
T

27 lines
959 B
TypeScript

// 定义 OneTalk canonical 边界的严格 plain-record 守卫
/** 仅接受当前 realm 的普通对象或 null-prototype record。 */
export const isPlainRecord = (value: unknown): value is Record<string, unknown> => {
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
const prototype = Object.getPrototypeOf(value);
return prototype === Object.prototype || prototype === null;
};
/** 判断 OneTalk 白名单资料头像是否为不含空白的绝对 HTTP(S) URL。 */
export const isOneTalkAvatarUrl = (value: unknown): value is string => {
if (
typeof value !== "string" ||
value.length === 0 ||
value.trim() !== value ||
/\s/u.test(value)
) {
return false;
}
try {
const url = new URL(value);
return (url.protocol === "http:" || url.protocol === "https:") && url.hostname.length > 0;
} catch {
return false;
}
};