Skip to content
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

[enhancement] openapi-response-validator: native ESM node modules default export not working as intended #874

Open
webketje opened this issue May 25, 2023 · 3 comments

Comments

@webketje
Copy link

Context

  • openapi-response-validator version: 12.1.3
  • Node version: 18.12.1
  • Package type: module (native node ESM ending in .js)

Bug

import OpenAPIResponseValidator from 'openapi-response-validator'
const validator = new OpenAPIResponseValidator({ responses: { }, components: {}})
// throws "OpenAPIResponseValidator is not a constructor"
// because OpenAPIResponseValidator === { __esModule: true, default: function OpenAPIResponseValidator () {}}

Temp fixes

(clunky, TS types intellisense no longer working in VS Code)

import OpenAPIResponseValidator from 'openapi-response-validator'
const validator = new OpenAPIResponseValidator.default({ responses: { }, components: {}})

or (in generated code, not clean but working)
Replace following statements

exports.__esModule = true
exports["default"] = OpenAPIResponseValidator

with

OpenAPIResponseValidator.default = OpenAPIResponseValidator
OpenAPIResponseValidator.__esModule = true
module.exports = OpenAPIResponseValidator

Solution

Provide different entrypoints in dist and expose them to the relevant environments using package.json main, module and exports fields. Alternatively, manually wrap output in a UMD wrapper

@jsdevel
Copy link
Contributor

jsdevel commented May 25, 2023

plz submit a PR

@renkei
Copy link

renkei commented Jun 4, 2023

The same issue also exists for OpenAPIRequestValidator and OpenAPIRequestCoercer. As mentioned by the reporter, using

new OpenAPIRequestValidator.default(...)
new OpenAPIResponseValidator.default(...)
new OpenAPIRequestCoercer.default(...)

instead of

new OpenAPIRequestValidator(...)
new OpenAPIResponseValidator(...)
new OpenAPIRequestCoercer(...)

is a workaround for ESM projects. For CommonJS projects, the workaround is not necessary.

@jtlindsey
Copy link

same issue with import OpenAPISchemaValidator from 'openapi-schema-validator', workarounds not working. Property 'default' does not exist on type 'typeof OpenAPISchemaValidator'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants