Skip to content

Commit b941391

Browse files
quesurifnvi-ssc
andauthored
Port method and path fixes - can overwrite autogenerated routes - sticks to its http methods (#10)
* added method & headers to apply/improve port spec * limit service ports to their portSpec http methods * make a port path validation * replace the port path only if methods match * execute port only if path is provided and matches * fix priorities, custom over autogenerated * execution speed fix - includes over new Regexp * specPortMethods is null - "".includes error Co-authored-by: vi-ssc <[email protected]>
1 parent 1f1a7ce commit b941391

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

src/adapters/controllers/post-invoke-port.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export default function anyInvokePortFactory (invokePort) {
1313
const result = await invokePort({
1414
port: httpRequest.params.port,
1515
args: httpRequest.body,
16+
method: httpRequest.method,
17+
headers: httpRequest.headers,
18+
path: httpRequest.path,
1619
id: httpRequest.params.id || null
1720
})
1821

src/aegis.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,13 @@ const router = {
9696
if (ctrl.ports) {
9797
for (const portName in ctrl.ports) {
9898
const port = ctrl.ports[portName]
99+
const specPortMethods = port?.methods?.join('|') || ""
100+
99101
if (port.path) {
100102
routeOverrides.set(port.path, portName)
101103
}
102104

103-
if (checkAllowedMethods(ctrl, method)) {
105+
if (checkAllowedMethods(ctrl, method) && (!port.methods || (specPortMethods.includes(method.toLowerCase()))) ) {
104106
routes.set(port.path || path(ctrl.endpoint), {
105107
[method]: adapter(ctrl.fn)
106108
})
@@ -131,6 +133,8 @@ const router = {
131133
}
132134

133135
function makeRoutes () {
136+
router.adminRoute(getConfig, http)
137+
router.userRoutes(getRoutes)
134138
router.autoRoutes(endpoint, 'get', liveUpdate, http)
135139
router.autoRoutes(endpoint, 'get', getModels, http)
136140
router.autoRoutes(endpoint, 'post', postModels, http)
@@ -146,8 +150,6 @@ function makeRoutes () {
146150
router.autoRoutes(endpointPortId, 'patch', anyInvokePorts, http, true)
147151
router.autoRoutes(endpointPortId, 'delete', anyInvokePorts, http, true)
148152
router.autoRoutes(endpointPortId, 'get', anyInvokePorts, http, true)
149-
router.adminRoute(getConfig, http)
150-
router.userRoutes(getRoutes)
151153
console.log(routes)
152154
}
153155

src/domain/use-cases/invoke-port.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,22 @@ export default function makeInvokePort ({
3333
}
3434
/**
3535
*
36-
* @param {{id:string,model:import('..').Model,args:string[],port:string}} input
36+
* @param {{id:String,model:import('..').Model,args:String[],port:String,method:String,headers:Array}} input
3737
* @returns
3838
*/
3939
return async function invokePort (input) {
4040
if (isMainThread) {
4141
return threadpool.runJob(invokePort.name, input, modelName)
4242
} else {
4343
try {
44-
let { id = null, port = null} = input;
44+
let { id = null, port = null, method, path } = input;
4545
const service = await findModelService(id)
4646
if (!service) {
4747
throw new Error('could not find a service associated with given id')
4848
}
4949

50+
const specPorts = service.getPorts();
5051
if(!port) {
51-
const specPorts = service.getPorts();
52-
const path = context['requestContext'].getStore().get('path');
5352
for(const p of Object.entries(specPorts)) {
5453
if(!p[1].path) {
5554
continue;
@@ -68,6 +67,14 @@ export default function makeInvokePort ({
6867
throw new Error('the port or record ID is invalid')
6968
}
7069

70+
const specPortMethods = specPorts[port]?.methods.join('|').toLowerCase();
71+
if (specPortMethods && !(specPortMethods.includes(method.toLowerCase()))) {
72+
throw new Error('invalid method for given port');
73+
}
74+
if (specPorts[port]?.path && !pathsMatch(specPorts[port].path, path)) {
75+
throw new Error('invalid path for given port');
76+
}
77+
7178
return await service[port](input)
7279
} catch (error) {
7380
return AppError(error)

0 commit comments

Comments
 (0)