json-schema-to-openapi-schema
json-schema-to-openapi-schema copied to clipboard
Version 3 isn't esm friendly anymore
With previous the version 2, I had no issue using this package with tsx or ts-node targeting esm. Now with version 3, convert is not a function but an object with a default key containing the function. Here is what I mean:
Convert2: [AsyncFunction: convert]
Convert3: { default: [AsyncFunction: convert] }
Below the steps to reproduce in Node v20.11.1.
Install:
npm init -y
npm i -D tsx ts-node
npm i json-schema-to-openapi-schema2@npm:@openapi-contrib/json-schema-to-openapi-schema@2 \
json-schema-to-openapi-schema3@npm:@openapi-contrib/json-schema-to-openapi-schema@3 \
package.json:
{
// ...
"type": "module",
// ...
}
test.ts:
import convert2 from 'json-schema-to-openapi-schema2';
import convert3 from 'json-schema-to-openapi-schema3';
console.log('Convert2:', convert2);
console.log('Convert3:', convert3);
tscong.json:
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"alwaysStrict": true
}
}
Run with tsx:
npx tsx ts-node/esm index.ts
Run with ts-node:
node --loader ts-node/esm index.ts
Facing the same issue, I don't understand why the transpilation to esm and commonjs was replaced by a transpilation that targets ESM but that uses module commonjs. In my opinion the code should be using ESM modules as it's the new standard. And optionally could still have a commonjs transpiled version as it was setup previously.
I cleaned up shortly project transpilation and created this PR to restore ESM. I had to downgrade in my local project to build v2.2.6 in the meanwhile.
Not sure who is owning this project actually. @jonluca ?
Thanks for the PR! Merged in
Hi all -- I think this might still be an issue? (Either that, or I'm missing something, which is equally possible 😄)
I added "@openapi-contrib/json-schema-to-openapi-schema": "^3.0.3" to my ESM project, but I ended up having to do this in order to use the conversion function:
import jstoas from '@openapi-contrib/json-schema-to-openapi-schema'
const convert = jstoas.default
await convert(...)
EDIT: This actually doesn't work either:
TypeError: convert is not a function
Can someone provide working example code using the latest version?
OK, it turns out that it actually works as expected if I import in the usual way, like this:
import convert from '@openapi-contrib/json-schema-to-openapi-schema'
But, TypeScript seems to think that the function is not callable for some reason:
If I add a @ts-expect-error annotation to get it to ignore the issue, it does work and I am able to use the conversion function.
The good news is that the function still works if I tell TypeScript to ignore the error, but it would be ideal if it recognized that the default export is, in fact, a callable function.
I added a named export - could try import { convert } from '@openapi-contrib/json-schema-to-openapi-schema' in v4.0.0?
That fixed it -- nice! Thanks for the quick fix! 🙌