Support ESM import of handler module in default resolver#922
Closed
mdmower-csnw wants to merge 2 commits intocdimascio:masterfrom
Closed
Support ESM import of handler module in default resolver#922mdmower-csnw wants to merge 2 commits intocdimascio:masterfrom
mdmower-csnw wants to merge 2 commits intocdimascio:masterfrom
Conversation
Owner
|
#921 is now merged. please resolve conflicts and will get this in as well |
Make the default request handler resolver work in ESM projects by
allowing a URL value for the handlersPath. If a URL is passed, then
a dynamic import will be used to load the handler module. If a string is
passed, then require() will be used.
Because this project uses moduleResolution:node in tsconfig, it's not
possible to write async import(...) directly in code. It has to be
obscured so that the compiler does not replace it, hence the HACK!
function that is essentially eval('import(...)').
ESM users should be aware that a file extension must be specified when
defining x-eov-operation-handler in schemas. For example:
```
{
"get": {
"summary": "Get list",
"x-eov-operation-id": "getList",
"x-eov-operation-handler": "list.js",
"responses": {
...
```
fb7d4d8 to
ee2841e
Compare
- Restore original "baseName" variable name - Make sure string path handling is exactly the same as prior to URL handling was added
Contributor
Author
|
Usage instructions could be incorporated into https://github.com/cdimascio/express-openapi-validator/tree/master/examples/5-custom-operation-resolver, but maybe the name should change to |
zeiglerd
added a commit
to zeiglerd/pidwin-fs-take-home-project
that referenced
this pull request
Jul 20, 2024
- add esm-resolver until cdimascio/express-openapi-validator#922 is resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
URLvalue tooptions.operationHandlerswould 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:nodeintsconfig.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:truewould no longer work. Fix one issue, introduce another...Usage example
When defining
x-eov-operation-handlerin schemas, include the file extension on the module name. For example:{ "get": { "summary": "Get thing", "x-eov-operation-id": "getThing", "x-eov-operation-handler": "things.js", // <-- include file extension "responses": {