-
-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support ESM as operation handlers #660
Comments
Hi @cdimascio There is one of pretty simple implementation: https://github.com/CoinSpace/CoinSpace/blob/fd5c229ce60b67bcf88a342209931efb1cf87b93/server/lib/esmresolver.js |
@mahnunchik thanks for the example. it looks reasonable. to help move this along, would you be up for preparing this as a PR. |
I've tested @mahnunchik but it does not work for a function array. In order for this to work, the resolver usage should preview the possibility of a Promise. I've tried by modifying openapivalidator.js from:
to: Promise.resolve(resolver(basePath, route, context.apiDoc)).then(r => {
router[method.toLowerCase()](path, r);
}); Then changing @mahnunchik code to: handlersCache[cacheKey] = import(modulePath).then(module => {
if (!module[oId]) {
// eslint-disable-next-line max-len
throw Error(`Could not find 'x-eov-operation-handler' with id ${oId} in module '${baseName}'. Make sure operation '${oId}' defined in your API spec exists as a handler function in '${baseName}'.`);
}
return module[oId];
}
).catch(next); And finally just returning handlersCache[cacheKey] in the resolver function. Although it take a little extra time for the routes to work in the server, it allowed me to use an array with middlewares, just like I was already used to. |
Just came back to say that I discovered that @mahnunchik implementation fails on Windows. In order to fix it, just add a "file://" in the import at line 24: const modulePath = "file://" + path.join(handlersPath, `${baseName}.mjs`);
cache[cacheKey] = import(modulePath); But the library should really support this properly, considering async imports properly. |
Is your feature request related to a problem? Please describe.
It would be helpfull to support ECMAScript modules as operation handlers.
Describe the solution you'd like
Describe alternatives you've considered
#575
The text was updated successfully, but these errors were encountered: