mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 11:51:23 +00:00
refactor(gateway): dedupe json endpoint prelude
This commit is contained in:
45
src/gateway/http-endpoint-helpers.ts
Normal file
45
src/gateway/http-endpoint-helpers.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import type { IncomingMessage, ServerResponse } from "node:http";
|
||||
import type { AuthRateLimiter } from "./auth-rate-limit.js";
|
||||
import type { ResolvedGatewayAuth } from "./auth.js";
|
||||
import { authorizeGatewayBearerRequestOrReply } from "./http-auth-helpers.js";
|
||||
import { readJsonBodyOrError, sendMethodNotAllowed } from "./http-common.js";
|
||||
|
||||
export async function handleGatewayPostJsonEndpoint(
|
||||
req: IncomingMessage,
|
||||
res: ServerResponse,
|
||||
opts: {
|
||||
pathname: string;
|
||||
auth: ResolvedGatewayAuth;
|
||||
maxBodyBytes: number;
|
||||
trustedProxies?: string[];
|
||||
rateLimiter?: AuthRateLimiter;
|
||||
},
|
||||
): Promise<false | { body: unknown } | undefined> {
|
||||
const url = new URL(req.url ?? "/", `http://${req.headers.host || "localhost"}`);
|
||||
if (url.pathname !== opts.pathname) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (req.method !== "POST") {
|
||||
sendMethodNotAllowed(res);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const authorized = await authorizeGatewayBearerRequestOrReply({
|
||||
req,
|
||||
res,
|
||||
auth: opts.auth,
|
||||
trustedProxies: opts.trustedProxies,
|
||||
rateLimiter: opts.rateLimiter,
|
||||
});
|
||||
if (!authorized) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const body = await readJsonBodyOrError(req, res, opts.maxBodyBytes);
|
||||
if (body === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return { body };
|
||||
}
|
||||
Reference in New Issue
Block a user