diff --git a/src/adapters/controllers/post-invoke-port.js b/src/adapters/controllers/post-invoke-port.js index 7897a61b..5d5f2242 100644 --- a/src/adapters/controllers/post-invoke-port.js +++ b/src/adapters/controllers/post-invoke-port.js @@ -13,6 +13,9 @@ export default function anyInvokePortFactory (invokePort) { const result = await invokePort({ port: httpRequest.params.port, args: httpRequest.body, + method: httpRequest.method, + headers: httpRequest.headers, + path: httpRequest.path, id: httpRequest.params.id || null }) diff --git a/src/aegis.js b/src/aegis.js index 324d56f3..270c14e5 100644 --- a/src/aegis.js +++ b/src/aegis.js @@ -96,11 +96,13 @@ const router = { if (ctrl.ports) { for (const portName in ctrl.ports) { const port = ctrl.ports[portName] + const specPortMethods = (port?.methods || "").join('|') + if (port.path) { routeOverrides.set(port.path, portName) } - if (checkAllowedMethods(ctrl, method)) { + if (checkAllowedMethods(ctrl, method) && (!port.methods || (specPortMethods.includes(method.toLowerCase()))) ) { routes.set(port.path || path(ctrl.endpoint), { [method]: adapter(ctrl.fn) }) @@ -131,6 +133,8 @@ const router = { } function makeRoutes () { + router.adminRoute(getConfig, http) + router.userRoutes(getRoutes) router.autoRoutes(endpoint, 'get', liveUpdate, http) router.autoRoutes(endpoint, 'get', getModels, http) router.autoRoutes(endpoint, 'post', postModels, http) @@ -146,8 +150,6 @@ function makeRoutes () { router.autoRoutes(endpointPortId, 'patch', anyInvokePorts, http, true) router.autoRoutes(endpointPortId, 'delete', anyInvokePorts, http, true) router.autoRoutes(endpointPortId, 'get', anyInvokePorts, http, true) - router.adminRoute(getConfig, http) - router.userRoutes(getRoutes) console.log(routes) } diff --git a/src/domain/use-cases/invoke-port.js b/src/domain/use-cases/invoke-port.js index 84fd817f..97fdd395 100644 --- a/src/domain/use-cases/invoke-port.js +++ b/src/domain/use-cases/invoke-port.js @@ -33,7 +33,7 @@ export default function makeInvokePort ({ } /** * - * @param {{id:string,model:import('..').Model,args:string[],port:string}} input + * @param {{id:String,model:import('..').Model,args:String[],port:String,method:String,headers:Array}} input * @returns */ return async function invokePort (input) { @@ -41,15 +41,14 @@ export default function makeInvokePort ({ return threadpool.runJob(invokePort.name, input, modelName) } else { try { - let { id = null, port = null} = input; + let { id = null, port = null, method, path } = input; const service = await findModelService(id) if (!service) { throw new Error('could not find a service associated with given id') } + const specPorts = service.getPorts(); if(!port) { - const specPorts = service.getPorts(); - const path = context['requestContext'].getStore().get('path'); for(const p of Object.entries(specPorts)) { if(!p[1].path) { continue; @@ -68,6 +67,14 @@ export default function makeInvokePort ({ throw new Error('the port or record ID is invalid') } + const specPortMethods = specPorts[port]?.methods.join('|').toLowerCase(); + if (specPortMethods && !(specPortMethods.includes(method.toLowerCase()))) { + throw new Error('invalid method for given port'); + } + if (specPorts[port]?.path && !pathsMatch(specPorts[port].path, path)) { + throw new Error('invalid path for given port'); + } + return await service[port](input) } catch (error) { return AppError(error)