hono icon indicating copy to clipboard operation
hono copied to clipboard

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

Open fro-profesional opened this issue 1 year ago • 9 comments

Screenshot 2023-07-01 at 23 41 42

fro-profesional avatar Jul 02 '23 06:07 fro-profesional

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'

yusukebe avatar Jul 15 '23 12:07 yusukebe

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?

hectorAguero avatar Aug 30 '23 13:08 hectorAguero

@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. 😢

jtmueller avatar Nov 10 '23 10:11 jtmueller

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.

yusukebe avatar Nov 11 '23 01:11 yusukebe

Just to say, I'm having the same problem with @hono/zod-openapi the 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/[email protected]';
// import { OpenAPIHono, z, createRoute } from 'npm:@hono/zod-openapi';

import { prettyJSON } from 'https://deno.land/x/[email protected]/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)

hectorAguero avatar Jan 04 '24 19:01 hectorAguero

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

yusukebe avatar Jan 09 '24 12:01 yusukebe

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

jyotman avatar Mar 03 '24 14:03 jyotman

@jyotman

Zod Validator is supporting Deno.

yusukebe avatar Mar 04 '24 03:03 yusukebe

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/[email protected]/context.ts';
import type { ZodSchema } from 'https://deno.land/x/[email protected]/types.ts';
import type { z, ZodError } from 'https://deno.land/x/[email protected]/mod.ts';
import { validator } from 'https://deno.land/x/[email protected]/validator/index.ts';

lisez avatar Mar 22 '24 18:03 lisez

It is also necessary to use zod by npm: specifier

KiritaniAyaka avatar Jun 01 '24 05:06 KiritaniAyaka

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

fro-profesional avatar Jun 02 '24 00:06 fro-profesional

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

vidz1979 avatar Aug 10 '24 19:08 vidz1979