honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
19.07k stars 544 forks source link

Type instantiation is excessively deep and possibly infinite deno-ts(2589) #1216

Closed fro-profesional closed 4 months ago

fro-profesional commented 1 year ago
Screenshot 2023-07-01 at 23 41 42
yusukebe commented 1 year ago

This issue might be due to a mismatch between hono and zod-validator. As a possible solution, you could try using the npm: specifier as follows:

import { Hono } from 'npm:hono'
import { zValidator } from 'npm:@hono/zod-validator'
hectorAguero commented 1 year ago

What I found is if I call zValidator with 'npm:@hono/zod-validator' Deno retrive me the version 1.3 and the mismatch disappear, but the error message is different

But the latest version is 1.8, there is a reason Deno only support 1.3?

jtmueller commented 11 months ago

@yusukebe I don't find that getting both hono and zod-validator from npm makes any difference. Nor does getting zod-validator from esm.sh.

@hectorAguero I'm not finding that even version 0.1.3 of zod-validator avoids this error. 😢

yusukebe commented 11 months ago

This code will work correctly:

import { Hono } from 'npm:hono'
import { zValidator } from 'npm:@hono/zod-validator'
import { z } from 'npm:zod'

const app = new Hono()

const schema = z.object({
  foo: z.string()
})

app.get('/', zValidator('query', schema), (c) => {
  return c.json(c.req.valid('query'))
})

Deno.serve(app.fetch)

If errors still occur, try clearing the cache:

deno cache --reload hello.ts

Or you may need to specify the version as the latest version.

hectorAguero commented 9 months ago

Just to say, I'm having the same problem with @hono/zod-openapithe linter give me an error, but the code still run, if I switch to npm import the error is not showing

The error is No overload matches this call

Code sample is just the docs snippet with prettyJSON use, Both type of imports works in Deno Deploy.

import { OpenAPIHono, z, createRoute } from 'https://esm.sh/@hono/zod-openapi@0.9.5';
// import { OpenAPIHono, z, createRoute } from 'npm:@hono/zod-openapi';

import { prettyJSON } from 'https://deno.land/x/hono@v3.11.9/middleware.ts';
//import { prettyJSON } from 'npm:hono/pretty-json';

const app = new OpenAPIHono();

app.use('/doc/*', prettyJSON())

const UserSchema = z
  .object({
    id: z.string().openapi({
      example: '123',
    }),
    name: z.string().openapi({
      example: 'John Doe',
    }),
    age: z.number().openapi({
      example: 42,
    }),
  })
  .openapi('User')

const sampleRoute = createRoute({
  method: 'get',
  path: '',
  responses: {
    200: {
      content: {
        'application/json': {
          schema: UserSchema,
        },

      },
      description: 'Retrieve user',
    },

  },

});

app.openapi(sampleRoute, async (c) => {
  //fake await
  const user = await Promise.resolve({
    id: '123',
    name: 'John Doe',
    age: 42,
  });
  console.log(user)
  const { id, name, age } = user;
  return c.json({
    id: id,
    name: name,
    age: age,
  })
});

// The OpenAPI documentation will be available at /doc
app.doc('/doc', {
  openapi: '3.0.0',
  info: {
    version: '1.0.0',
    title: 'My API',
  },
})

Deno.serve(app.fetch)
yusukebe commented 9 months ago

@fro-profesional

Mixing esm.sh and deno.land will result in a type error because each refers to a different hono. This is unavoidable due to the limitations of TypeScript.

jyotman commented 7 months ago

@yusukebe are we open to adding support for deno for zod-validator?

yusukebe commented 7 months ago

@jyotman

Zod Validator is supporting Deno.

lisez commented 6 months ago

My workaround in Deno:

  1. copy source code from packages/zod-validator/src/index.ts in honojs/middleware repo.
  2. modify import as below:

from:

import type { Context, MiddlewareHandler, Env, ValidationTargets, TypedResponse, Input } from 'hono'
import { validator } from 'hono/validator'
import type { z, ZodSchema, ZodError } from 'zod'

to:

import type {
  Env,
  Input,
  MiddlewareHandler,
  TypedResponse,
  ValidationTargets,
} from 'hono/types.ts';
import type { Context } from 'https://deno.land/x/hono@v4.1.3/context.ts';
import type { ZodSchema } from 'https://deno.land/x/zod@v3.22.4/types.ts';
import type { z, ZodError } from 'https://deno.land/x/zod@v3.22.4/mod.ts';
import { validator } from 'https://deno.land/x/hono@v4.1.3/validator/index.ts';
KiritaniAyaka commented 4 months ago

It is also necessary to use zod by npm: specifier

fro-profesional commented 4 months ago

Yeah, making zod/hono external with esm.sh or using npm fixes the issue, thanks!

vidz1979 commented 1 month ago

Related: https://github.com/StefanTerdell/zod-to-json-schema/issues/69#issuecomment-1572954056