architect / functions

AWS Lambda Node runtime helpers for Architect apps
https://arc.codes
163 stars 38 forks source link

Fix events.subscribe types: returns either an async or promise function #563

Open filmaj opened 2 months ago

filmaj commented 2 months ago

Returning unknown from events.subscribe, as the types do today, sucks if you're writing tests:

Screenshot 2024-08-17 at 17 22 08

This PR attempts to address this by tweaking the return type of subscribe, which is conditional based on what kind of event handler function you pass into subscribe:

  1. If you pass an async function, subscribe returns an async function.
  2. If you pass it a regular function that accepts two parameters, the second parameter being a callback, it returns a regular function that takes three parameters, the last being a callback too.

With these changes, now my tests are tidier with less IDE complaints. With an async handler the result looks like:

Screenshot 2024-08-17 at 17 19 33

.. and using a callback interface, too:

Screenshot 2024-08-17 at 17 18 52

No matter which style of handler you pass, at least in the context of my tests, now my IDE knows that I am dealing with functions - even the right kinds to boot!