I'm trying to simulate the behavior of firebases rules with node, where I create an object with the routes ad the handlers per each route, but I'm getting problems with the typescript interface, here is what I have:
interface Rule {
allowReadIf?: () => boolean,
allowWriteIf?: () => boolean,
allowAllIf?: () => boolean,
routes?: {
[key: string]: Rule
}
}
But I don't like the routes
key, because any child route would have another routes and that's useless... I'd like something more like:
interface Rule {
allowReadIf?: () => boolean,
allowWriteIf?: () => boolean,
allowAllIf?: () => boolean,
[key: string]: Rule // This creates me errors, because allowReadIf, allowWriteIf... can't be assigned to type Rule
}
So I could use in this way:
'clients': {
allowAllIf: () => isLogged(),
'details': {
...
},
'add': {
allowWriteIf: () => isAdmin()
}
}
Instead of creating a routes key per each child, this would be uggly with many nested routes
'clients': {
allowAllIf: () => isLogged(),
routes: {
'details': {
...
},
'add': {
allowWriteIf: () => isAdmin()
}
}
}
In this case I would have a route /clients
that will only handle requests from logged users, all users can access the details... but only admins can call /clients/add
In simple words, instead of having:
myVar['clients']['routes']['details']
I want to have:
myVar['clients']['details']