mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 17:18:25 +00:00
refactor(src): split oversized modules
This commit is contained in:
60
src/gateway/server-methods/nodes.helpers.ts
Normal file
60
src/gateway/server-methods/nodes.helpers.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import type { ErrorObject } from "ajv";
|
||||
import {
|
||||
ErrorCodes,
|
||||
errorShape,
|
||||
formatValidationErrors,
|
||||
} from "../protocol/index.js";
|
||||
import { formatForLog } from "../ws-log.js";
|
||||
import type { RespondFn } from "./types.js";
|
||||
|
||||
type ValidatorFn = ((value: unknown) => boolean) & {
|
||||
errors?: ErrorObject[] | null;
|
||||
};
|
||||
|
||||
export function respondInvalidParams(params: {
|
||||
respond: RespondFn;
|
||||
method: string;
|
||||
validator: ValidatorFn;
|
||||
}) {
|
||||
params.respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid ${params.method} params: ${formatValidationErrors(params.validator.errors)}`,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
export async function respondUnavailableOnThrow(
|
||||
respond: RespondFn,
|
||||
fn: () => Promise<void>,
|
||||
) {
|
||||
try {
|
||||
await fn();
|
||||
} catch (err) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(ErrorCodes.UNAVAILABLE, formatForLog(err)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function uniqueSortedStrings(values: unknown[]) {
|
||||
return [...new Set(values.filter((v) => typeof v === "string"))]
|
||||
.map((v) => v.trim())
|
||||
.filter(Boolean)
|
||||
.sort();
|
||||
}
|
||||
|
||||
export function safeParseJson(value: string | null | undefined): unknown {
|
||||
if (typeof value !== "string") return undefined;
|
||||
const trimmed = value.trim();
|
||||
if (!trimmed) return undefined;
|
||||
try {
|
||||
return JSON.parse(trimmed) as unknown;
|
||||
} catch {
|
||||
return { payloadJSON: value };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user