Closed bartoszherba closed 1 year ago
@bartoszherba Looking at the logic in
createServer.ts
: can't we just remove the spread operator and pass a plainreq.body
reference as the only argument to theapiFunction
?Perhaps the intention of the author here was to prevent the
apiFunction
from altering thereq.body
object. However, I think it should rather be a matter for a well-designedapiFunction
which should not cause any side effects and stay pure.If we pass
req.body
as the only argument here, we will get rid of the problematic spread operator as well as avoid an additional check preventing us from spreading a non-iterable entity.WDYT?
I did it like I did because I wanted to preserve the backward compatibility with any external integration or modules that we have. If I change the argument of the function I cannot guarantee that. It was just too risky. Also, if we can prevent mutation of the input from the caller then I think it is better and safer. We cannot assume that the input will be not modified if it mustn't be modified, though we can prevent it.
@bartoszherba Totally agree, I've made some wrong assumptions while looking at this logic, mainly due to the fact that I have a memory of this part of our docs about extending API methods.
It suggests that every API method can only receive context
and params
as arguments whereas in fact it could even be context
, param1
, param2
, paramN
- depending on how we call this API method.
This PR is approved.
Description
In revision
1558c3242bfa5aca9207ef5e41839cbee558cd8e
we changed the TS compiler target fromes5
toes2019
which completely changed the compiled output. In the previous version, the result aligned with the spread operator intentions...req.body
but in the current, non-iterable objects yielderror Found non-callable @@iterator
as spread can be used only on iterable objects like arrays or strings. To address this issue I added the check to eventually wrap non-iterable objects in the array.Please compare compiled versions below.
target ES2019 (current)
target "target": "es5"
Related Issue
Motivation and Context
How Has This Been Tested?
Screenshots:
Types of changes
Checklist:
Changelog
Tests
Code standards
Docs