asteasolutions/zod-to-openapi

Support ES Modules?

Closed this issue ยท 7 comments

Hi.

First, thank you very much for creating such a great product. We use Zod to OpenAPI in @hono/zod-openapi. It can only be archived if Zod to OpenAPI exists.

What I would like to know/suggest is supporting ES Modules. Currently, this only supports Common JS. However, some environments, such as Vitest, use only ES Modules and do not support Common JS. So the following problem occurred:

honojs/middleware#436

Would you happen to have any plans to support ES Modules? If so, it will be a great opportunity to work Zod to OpenAPI in more environments. Thanks!

Hi @yusukebe,

We're definitely open to supporting ESM, however time we can put in the project is limited lately so I can't promise anything in terms of time.

Will you be ok to help on this? I believe it should be mostly splitting our tsconfig to tsconfig.cjs.json and tsconfig.esm.json, updating the build scripts and adding to the package.json. Plus some testing of course, and it would help that you have a reproduction case through your library. What do you think?

I just added a PR for this: #220

I haven't tested it yet however.

@georgyangelov Thank you for your response!

@danielkarso-pon So quick handling. Great!

I encountered an issue while integrating the new Cloudflare Worker Vitest plugin. After hours of debugging, I noticed that openapi3-ts supports mjs, but @asteasolutions/zod-to-openapi (which import openapi3-ts) does not, makes vitest/cloudflare worker forces to use cjs, causing my test to fail. I hope to see this addressed in the next release.

Fyi; if you want vitest fixed with Cloudflare worker and zod-to-openapi for now and not wait for the next release you can do this in your

package.json

"dependencies": {
    "zod-to-openapi": "github:danielkarso-pon/zod-to-openapi#multi-package"
  },
}
"overrides": {
  "@asteasolutions/zod-to-openapi": "$zod-to-openapi"
}

rm -rf node_modules && npm i

And in your vitest.config.ts

import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config';
import path from 'node:path';

export default defineWorkersConfig({
	test: {
		poolOptions: {
			workers: {
				wrangler: { configPath: './wrangler.toml' },
			},
		},
		server: {
			deps: {
				inline: ['@asteasolutions/zod-to-openapi'],
			}
		},
	},
	resolve: {
		alias: {
			'@/': new URL('./src/', import.meta.url).pathname,
			'@asteasolutions/zod-to-openapi': path.resolve(__dirname, 'node_modules/zod-to-openapi/dist/mjs/index.js'),
			'openapi3-ts/oas30': path.resolve(__dirname, 'node_modules/openapi3-ts/dist/oas30.mjs'),
		},
	}
});

@yusukebe @ducan-ne @danielkarso-pon this should now be available as part of the v7.0.0 Release ๐Ÿš€ .

@danielkarso-pon @AGalabov

I've confirmed it works well! I'll release the new version of @hono/zod-openapi that includes the release. Thanks so much!