You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was trying to see how this guide would fit together with the Serverless Toolkit and realized while it works there is some hackery around it.
Essentially the only way you can get this to work without modifying your source code for Functions is by using the LocalDevelopmentServer from @twilio/runtime-handler/dev and initializing it with a basePath that contains a node_modules/twilio/index.js file that contains code similar to this:
I think while this is certainly a power-user feature we could make it at least a bit more accessible by making it easier to swap out the Twilio client that is being used. I don't think we need to offer a full mocking capability for npm module since that's what you should use jest or similar tests for but it would be nice to make this behavior easier.
There are three options in my opinion.
Option 1: The most flexible and verbose option
Provide a way to pass your own twilio module into the LocalDevelopmentServer. So rather than doing the requireFromProject() calls everywhere, we'd allow to pass the Twilio Node library as an object into LocalDevelopmentServer.
The problem with this one could be around the process forking and how to pass around the library in this case. A workaround could be to provide a path to a file instead.
Option 2: The middle ground
Add support to the LocalDevelopmentServer config object a new property called twilioDefaultOptions that can be used to provide any of the options that you'd pass otherwise to the twilio constructor call. This includes the httpClient property but also other properties such as edge, region, logLevel or userAgentExtensions.
The main benefit of this one is that we could probably re-use this in some places ourselves as part of the Toolkit but I foresee some issues around process forking again when passing a class for exampl ein the case of httpClient. Hence this wouldn't actually solve this problem when process forking is turned on but that might be fine.
Option 3: The least flexible way
In this situation we'd allow LocalDevelopmentServer to receive a requestRewriteMap that would look something like this:
I was trying to see how this guide would fit together with the Serverless Toolkit and realized while it works there is some hackery around it.
Essentially the only way you can get this to work without modifying your source code for Functions is by using the
LocalDevelopmentServer
from@twilio/runtime-handler/dev
and initializing it with abasePath
that contains anode_modules/twilio/index.js
file that contains code similar to this:and then have a
MockHttpClient
class such as this one:I think while this is certainly a power-user feature we could make it at least a bit more accessible by making it easier to swap out the Twilio client that is being used. I don't think we need to offer a full mocking capability for npm module since that's what you should use
jest
or similar tests for but it would be nice to make this behavior easier.There are three options in my opinion.
Option 1: The most flexible and verbose option
Provide a way to pass your own
twilio
module into theLocalDevelopmentServer
. So rather than doing therequireFromProject()
calls everywhere, we'd allow to pass the Twilio Node library as an object intoLocalDevelopmentServer
.The problem with this one could be around the process forking and how to pass around the library in this case. A workaround could be to provide a path to a file instead.
Option 2: The middle ground
Add support to the
LocalDevelopmentServer
config object a new property calledtwilioDefaultOptions
that can be used to provide any of the options that you'd pass otherwise to thetwilio
constructor call. This includes thehttpClient
property but also other properties such asedge
,region
,logLevel
oruserAgentExtensions
.The main benefit of this one is that we could probably re-use this in some places ourselves as part of the Toolkit but I foresee some issues around process forking again when passing a class for exampl ein the case of
httpClient
. Hence this wouldn't actually solve this problem when process forking is turned on but that might be fine.Option 3: The least flexible way
In this situation we'd allow
LocalDevelopmentServer
to receive arequestRewriteMap
that would look something like this:And then if we detect that we inject our own
MockHTTPServer
similar to the one above.This is by far the most inflexible one but would work with process forking and would require the least amount of set up from the user.
The text was updated successfully, but these errors were encountered: