refactor: dedupe record guards

This commit is contained in:
Peter Steinberger
2026-04-07 04:21:14 +01:00
parent 59eb291c6e
commit b7be963501
6 changed files with 16 additions and 26 deletions

View File

@@ -1,7 +1,9 @@
import {
asOptionalRecord,
hasNonEmptyString as sharedHasNonEmptyString,
isRecord as sharedIsRecord,
normalizeOptionalString,
readStringValue,
} from "openclaw/plugin-sdk/text-runtime";
export function encodeQuery(params: Record<string, string | undefined>): string {
@@ -16,15 +18,11 @@ export function encodeQuery(params: Record<string, string | undefined>): string
return queryString ? `?${queryString}` : "";
}
export function readString(value: unknown): string | undefined {
return typeof value === "string" ? value : undefined;
}
export const readString = readStringValue;
export const normalizeString = normalizeOptionalString;
export function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null;
}
export const isRecord = sharedIsRecord;
export const asRecord = asOptionalRecord;

View File

@@ -1,3 +1,5 @@
import { isRecord } from "../../../src/utils.js";
export type JsonObject = Record<string, unknown>;
export type ExternalPluginCompatibility = {
@@ -22,10 +24,6 @@ export const EXTERNAL_CODE_PLUGIN_REQUIRED_FIELD_PATHS = [
"openclaw.build.openclawVersion",
] as const;
function isRecord(value: unknown): value is JsonObject {
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
}
function getTrimmedString(value: unknown): string | undefined {
return typeof value === "string" && value.trim() ? value.trim() : undefined;
}

View File

@@ -2,6 +2,7 @@ import { execFileSync } from "node:child_process";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, join } from "node:path";
import { isRecord } from "../src/utils.js";
function writeStdoutLine(message = ""): void {
process.stdout.write(`${message}\n`);
@@ -546,10 +547,6 @@ function extractResponseText(payload: OpenAIResponse): string {
return chunks.join("\n").trim();
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null;
}
function fallbackCategory(issueText: string): "bug" | "enhancement" {
const lower = issueText.toLowerCase();
const bugSignals = [

View File

@@ -1,8 +1,5 @@
import { validateMinHostVersion } from "../../src/plugins/min-host-version.ts";
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null;
}
import { isRecord } from "../../src/utils.js";
export type ExtensionPackageJson = {
name?: string;

View File

@@ -0,0 +1,7 @@
export function isRecord(value) {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
export function trimString(value) {
return typeof value === "string" ? value.trim() : "";
}

View File

@@ -1,18 +1,11 @@
import fs from "node:fs";
import path from "node:path";
import { pathToFileURL } from "node:url";
import { isRecord, trimString } from "./lib/record-shared.mjs";
import { writeTextFileIfChanged } from "./runtime-postbuild-shared.mjs";
export const OFFICIAL_CHANNEL_CATALOG_RELATIVE_PATH = "dist/channel-catalog.json";
function isRecord(value) {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
function trimString(value) {
return typeof value === "string" ? value.trim() : "";
}
function toCatalogInstall(value, packageName) {
const install = isRecord(value) ? value : {};
const npmSpec = trimString(install.npmSpec) || packageName;