Support ESM import of handler module in default resolver #922
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Make the default request handler resolver work in ESM projects by allowing a URL value for
options.operationHandlers
. If a URL is passed, then a dynamic import will be used to load handler modules. If a string is passed, thenrequire()
will be used.Existing projects should not be adversely affected by this change. The assignment of a
URL
value tooptions.operationHandlers
would be the indicator that a user wants to opt-in to this new import handling.Fixes #660
Fixes #838
Notice
Because this project uses
moduleResolution:node
intsconfig.json
, it's not possible to writeasync import(...)
directly in code. It has to be obscured so that the compiler does not replace it withrequire()
. Hence, there is a very obvious HACK! in this PR that obscuresimport()
viaFunction('x', 'return import(x)')
(it's very similar toeval('import(...)')
).This hack will probably sink this PR, but I figured I'd post it at least as a proof of concept. Ideally, the module resolution for tsconfig would be updated to
Node16
, but that brings its own issues sinceresolveJsonModule:true
would no longer work. Fix one issue, introduce another...Usage example
When defining
x-eov-operation-handler
in schemas, include the file extension on the module name. For example: