n8n
Version:
n8n Workflow Automation Tool
60 lines • 2.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.publicApiRouteKey = publicApiRouteKey;
exports.loadPublicControllerScopeMap = loadPublicControllerScopeMap;
exports.extractScopeFromEovHandlerChain = extractScopeFromEovHandlerChain;
exports.resolvePublicApiImplementedScope = resolvePublicApiImplementedScope;
const decorators_1 = require("@n8n/decorators");
const di_1 = require("@n8n/di");
function normalizePath(pathStr) {
return (pathStr
.replace(/\{[^}]+\}/g, '{}')
.replace(/:[^/]+/g, '{}')
.replace(/\/+/g, '/')
.replace(/\/$/, '') || '/');
}
function joinPaths(basePath, routePath) {
const base = basePath.replace(/\/$/, '') || '';
if (routePath === '/' || routePath === '') {
return base || '/';
}
return `${base}${routePath.startsWith('/') ? routePath : `/${routePath}`}`;
}
function serializeScope(requirement) {
if (requirement === undefined)
return undefined;
if (typeof requirement === 'string')
return requirement;
return ('anyOf' in requirement ? requirement.anyOf : requirement.allOf).join(',');
}
function publicApiRouteKey(method, pathStr) {
return `${method.toLowerCase()} ${normalizePath(pathStr)}`;
}
function loadPublicControllerScopeMap() {
const map = new Map();
const metadata = di_1.Container.get(decorators_1.ControllerRegistryMetadata);
for (const controllerClass of metadata.controllerClasses) {
const controller = metadata.getControllerMetadata(controllerClass);
if (!controller.isPublicApi)
continue;
for (const route of controller.routes.values()) {
if (!route.method || route.path === undefined)
continue;
map.set(publicApiRouteKey(route.method, joinPaths(controller.basePath, route.path)), serializeScope(route.apiKeyScope));
}
}
return map;
}
function extractScopeFromEovHandlerChain(handlerChain) {
for (const middleware of handlerChain) {
if (typeof middleware !== 'function' || !('__apiKeyScope' in middleware))
continue;
return middleware.__apiKeyScope;
}
return undefined;
}
function resolvePublicApiImplementedScope(controllerScopes, method, pathStr, eovHandlerScope) {
const key = publicApiRouteKey(method, pathStr);
return controllerScopes.has(key) ? controllerScopes.get(key) : eovHandlerScope;
}
//# sourceMappingURL=public-api-scope-lookup.js.map