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

feat: Export ./oas30 and ./oas31 #111

Merged
merged 1 commit into from
Apr 14, 2023
Merged

Conversation

koooge
Copy link
Contributor

@koooge koooge commented Apr 14, 2023

Hi there, this is a proposal for import syntax for compatibility with v3 code. It is helpful since we can upgrade openapi3-ts without code changes like oas31.OpenAPIObject.

// Both are ok
import { oas31 } from 'openapi3-ts';
import { OpenAPIObject, OpenApiBuilder } from 'openapi3-ts/oas31';

Also, it would fix #107

Signed-off-by: koooge <koooooge@gmail.com>
@koooge koooge changed the title feat: Export oas30 and oas31 feat: Export ./oas30 and ./oas31 Apr 14, 2023
@pjmolina pjmolina merged commit 1c4d339 into metadevpro:master Apr 14, 2023
@koooge koooge deleted the export_pkg branch April 14, 2023 19:04
@RobinTail
Copy link
Contributor

it does not work with ESM now

SyntaxError: Named export 'oas30' not found. The requested module 'openapi3-ts' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

Comment on lines +13 to +20
"./oas30": {
"import": "./dist/mjs/oas30.js",
"require": "./dist/cjs/oas30.js"
},
"./oas31": {
"import": "./dist/mjs/oas31.js",
"require": "./dist/cjs/oas31.js"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why, but such imports are not appreciated by Typescript, however they are operational if ignored. ¯_(ツ)_/¯

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, actually my build fails to make DTS with such imports

src/open-api.ts(8,8): error TS2307: Cannot find module 'openapi3-ts/oas30' or its corresponding type declarations.
Error: error occured in dts build
DTS Build error
RollupError: Failed to compile. Check the logs above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something is not right with it.

Copy link
Contributor Author

@koooge koooge Apr 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I guess your tsconfig.json has moduleResolution: "node" which cannot resolve the exports field. As one option, node16 would work tho.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is.
Could you tell me please, @koooge , does it mean, that the minimum Node version of my package (that depends on openapi3-ts) should be upgraded from 14 to 16 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say node version does not matter. It is due to the config of typescript. If you use tsc with "moduleResolution": "node" , this approach would not work.

Copy link
Contributor

@RobinTail RobinTail Apr 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Thank you.
But there is another problem.
Still, my ESM setup does not work with the following error regarding ./oas30 import:

SyntaxError: Named export 'isReferenceObject' not found. The requested module 'openapi3-ts/oas30' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'openapi3-ts/oas30';

I use ts-node-esm command (of ts-node) as a runner.

Copy link
Contributor

@RobinTail RobinTail Apr 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It thinks that /oas30 is CommonJS, @koooge .

It can be fixed by placing package.json file inside ./dist/mjs having {"type": "module"}.

However, it does not end on it. It can not find files being imported by oas30.js withing having .jsextensions in the import statements. I believe the ESM build should actually have the explicitly specified extensions. Probably the ESM compiler need to be configured. CC @pjmolina

However, this is a well-known issue, which I'm not sure what is the right solution to,
microsoft/TypeScript#16577 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's because moduleResolution node(=node10) checks "main" in pckage.json. Use "moduleResolution": "node16" or bundler as a workaround. For support tsc, we need to export only "." and create dist/oas30 & dist/oas31 I suppose.

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

Successfully merging this pull request may close these issues.

import { OpenAPIObject } from "openapi3-ts/src/model/openapi31" doesn't work with mocha and ESM
3 participants