asteasolutions / zod-to-openapi

A library that generates OpenAPI (Swagger) docs from Zod schemas
MIT License
788 stars 52 forks source link

Support ES Modules? #219

Closed yusukebe closed 3 months ago

yusukebe commented 3 months ago

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:

https://github.com/honojs/middleware/issues/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!

georgyangelov commented 3 months ago

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?

danielkarso-pon commented 3 months ago

I just added a PR for this: https://github.com/asteasolutions/zod-to-openapi/pull/220

I haven't tested it yet however.

yusukebe commented 3 months ago

@georgyangelov Thank you for your response!

@danielkarso-pon So quick handling. Great!

ducan-ne commented 3 months ago

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.

danielkarso-pon commented 3 months ago

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'),
        },
    }
});
AGalabov commented 3 months ago

@yusukebe @ducan-ne @danielkarso-pon this should now be available as part of the v7.0.0 Release :rocket: .

yusukebe commented 2 months ago

@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!