Add parameter injection support
Closed this issue · 0 comments
In some cases it's desired to set arguments from middleware that procedures can optionally make use of. For example, we've created authentication middelware that adds requester data as an argument for the procedure call. Currently, every procedure function has to have the parameter for the argument to avoid the Unknown parameter
error. This leads to a lot of overhead, because most functions won't need the requester data.
Therefore we want to add the option to add optional arguments. Let's look at an example.
Current situation
export async function someProtectedFunction(data: Data, requester?: Requester): Promise<void>
{
// Do something with data and ignore the requester
}
export async function somePublicFunction(requester: Requester): Promise<void>
{
const data = getRequesterData(requester);
return someProtectedFunction(data); // Not providing requester, because it's not needed
}
In this situation we need to add the requester
parameter to the someProtectedFunction
to assure this function can be placed in a separate segment and placed on another server. But, the parameter is just sitting there doing nothing... This can also confuse new devs, so we want to avoid this situation.
Desired situation
export async function someProtectedFunction(data: Data): Promise<void>
{
// Look ma, no requester!
}
export async function somePublicFunction(requester: Requester): Promise<void>
{
const data = getRequesterData(requester);
return someProtectedFunction(data); // Not even possible to provide the requester!
}
For this, we need to allow middleware to define arguments that only need to injected if the function has it as parameter. As possible solution we could use a *
as prefix.
request.setArgument('*requester', requesterData);
When running the procedure, the runtime needs to check if the argument is optional or not.