openapi-contrib / json-schema-to-openapi-schema

Due to the OpenAPI v3.0 and JSON Schema discrepancy, you can use this JS library to convert JSON Schema objects to OpenAPI Schema.
https://www.npmjs.com/package/@openapi-contrib/json-schema-to-openapi-schema
MIT License
111 stars 16 forks source link

Version 3 isn't esm friendly anymore #50

Open OlivierCuyp opened 3 months ago

OlivierCuyp commented 3 months ago

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
yehudiduhey commented 3 months ago

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.