mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 08:37:41 +00:00
refactor(gateway): reuse shared validators + baseHash
This commit is contained in:
@@ -31,22 +31,14 @@ import { loadOpenClawPlugins } from "../../plugins/loader.js";
|
||||
import {
|
||||
ErrorCodes,
|
||||
errorShape,
|
||||
formatValidationErrors,
|
||||
validateConfigApplyParams,
|
||||
validateConfigGetParams,
|
||||
validateConfigPatchParams,
|
||||
validateConfigSchemaParams,
|
||||
validateConfigSetParams,
|
||||
} from "../protocol/index.js";
|
||||
|
||||
function resolveBaseHash(params: unknown): string | null {
|
||||
const raw = (params as { baseHash?: unknown })?.baseHash;
|
||||
if (typeof raw !== "string") {
|
||||
return null;
|
||||
}
|
||||
const trimmed = raw.trim();
|
||||
return trimmed ? trimmed : null;
|
||||
}
|
||||
import { resolveBaseHashParam } from "./base-hash.js";
|
||||
import { assertValidParams } from "./validation.js";
|
||||
|
||||
function requireConfigBaseHash(
|
||||
params: unknown,
|
||||
@@ -68,7 +60,7 @@ function requireConfigBaseHash(
|
||||
);
|
||||
return false;
|
||||
}
|
||||
const baseHash = resolveBaseHash(params);
|
||||
const baseHash = resolveBaseHashParam(params);
|
||||
if (!baseHash) {
|
||||
respond(
|
||||
false,
|
||||
@@ -258,15 +250,7 @@ function loadSchemaWithPlugins(): ConfigSchemaResponse {
|
||||
|
||||
export const configHandlers: GatewayRequestHandlers = {
|
||||
"config.get": async ({ params, respond }) => {
|
||||
if (!validateConfigGetParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid config.get params: ${formatValidationErrors(validateConfigGetParams.errors)}`,
|
||||
),
|
||||
);
|
||||
if (!assertValidParams(params, validateConfigGetParams, "config.get", respond)) {
|
||||
return;
|
||||
}
|
||||
const snapshot = await readConfigFileSnapshot();
|
||||
@@ -274,29 +258,13 @@ export const configHandlers: GatewayRequestHandlers = {
|
||||
respond(true, redactConfigSnapshot(snapshot, schema.uiHints), undefined);
|
||||
},
|
||||
"config.schema": ({ params, respond }) => {
|
||||
if (!validateConfigSchemaParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid config.schema params: ${formatValidationErrors(validateConfigSchemaParams.errors)}`,
|
||||
),
|
||||
);
|
||||
if (!assertValidParams(params, validateConfigSchemaParams, "config.schema", respond)) {
|
||||
return;
|
||||
}
|
||||
respond(true, loadSchemaWithPlugins(), undefined);
|
||||
},
|
||||
"config.set": async ({ params, respond }) => {
|
||||
if (!validateConfigSetParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid config.set params: ${formatValidationErrors(validateConfigSetParams.errors)}`,
|
||||
),
|
||||
);
|
||||
if (!assertValidParams(params, validateConfigSetParams, "config.set", respond)) {
|
||||
return;
|
||||
}
|
||||
const { snapshot, writeOptions } = await readConfigFileSnapshotForWrite();
|
||||
@@ -319,15 +287,7 @@ export const configHandlers: GatewayRequestHandlers = {
|
||||
);
|
||||
},
|
||||
"config.patch": async ({ params, respond }) => {
|
||||
if (!validateConfigPatchParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid config.patch params: ${formatValidationErrors(validateConfigPatchParams.errors)}`,
|
||||
),
|
||||
);
|
||||
if (!assertValidParams(params, validateConfigPatchParams, "config.patch", respond)) {
|
||||
return;
|
||||
}
|
||||
const { snapshot, writeOptions } = await readConfigFileSnapshotForWrite();
|
||||
@@ -433,15 +393,7 @@ export const configHandlers: GatewayRequestHandlers = {
|
||||
);
|
||||
},
|
||||
"config.apply": async ({ params, respond }) => {
|
||||
if (!validateConfigApplyParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid config.apply params: ${formatValidationErrors(validateConfigApplyParams.errors)}`,
|
||||
),
|
||||
);
|
||||
if (!assertValidParams(params, validateConfigApplyParams, "config.apply", respond)) {
|
||||
return;
|
||||
}
|
||||
const { snapshot, writeOptions } = await readConfigFileSnapshotForWrite();
|
||||
|
||||
Reference in New Issue
Block a user