decocms
Version:
Deco CMS — Self-hostable MCP Gateway for managing AI connections and tools
70 lines (68 loc) • 2.4 kB
JavaScript
import { APIError, BetterCallError, ValidationError } from "./error.mjs";
import { isAPIError, tryCatch } from "./utils.mjs";
import { toResponse } from "./to-response.mjs";
import { createInternalContext } from "./context.mjs";
//#region src/endpoint.ts
function createEndpoint(pathOrOptions, handlerOrOptions, handlerOrNever) {
const path = typeof pathOrOptions === "string" ? pathOrOptions : void 0;
const options = typeof handlerOrOptions === "object" ? handlerOrOptions : pathOrOptions;
const handler = typeof handlerOrOptions === "function" ? handlerOrOptions : handlerOrNever;
if ((options.method === "GET" || options.method === "HEAD") && options.body) throw new BetterCallError("Body is not allowed with GET or HEAD methods");
if (path && /\/{2,}/.test(path)) throw new BetterCallError("Path cannot contain consecutive slashes");
const internalHandler = async (...inputCtx) => {
const context = inputCtx[0] || {};
const { data: internalContext, error: validationError } = await tryCatch(createInternalContext(context, {
options,
path
}));
if (validationError) {
if (!(validationError instanceof ValidationError)) throw validationError;
if (options.onValidationError) await options.onValidationError({
message: validationError.message,
issues: validationError.issues
});
throw new APIError(400, {
message: validationError.message,
code: "VALIDATION_ERROR"
});
}
const response = await handler(internalContext).catch(async (e) => {
if (isAPIError(e)) {
const onAPIError = options.onAPIError;
if (onAPIError) await onAPIError(e);
if (context.asResponse) return e;
}
throw e;
});
const headers = internalContext.responseHeaders;
const status = internalContext.responseStatus;
return context.asResponse ? toResponse(response, {
headers,
status
}) : context.returnHeaders ? context.returnStatus ? {
headers,
response,
status
} : {
headers,
response
} : context.returnStatus ? {
response,
status
} : response;
};
internalHandler.options = options;
internalHandler.path = path;
return internalHandler;
}
createEndpoint.create = (opts) => {
return (path, options, handler) => {
return createEndpoint(path, {
...options,
use: [...options?.use || [], ...opts?.use || []]
}, handler);
};
};
//#endregion
export { createEndpoint };
//# sourceMappingURL=endpoint.mjs.map