hono: hono/zod-openapi + RPC not working

What version of Hono are you using?

Hono@4.0.10, @hono/zod-openapi@0.9.9

What runtime/platform is your app running on?

Deno(1.41.3)

What steps can reproduce the bug?

I have prepared a minimally configured project in this repository.

https://github.com/Octo8080X/inspect-fresh-hono-openapi-rpc

Due to some issues, it appears that hono(RPC) + hono/zod-openapi is not working.

As one of the causes, there seems to be no openapi() in the object provided by the z method provided by @hono/zod-openapi.

import { Handler } from "$fresh/server.ts";
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";

const ParamsSchema = z.object({});

console.log(z.string());
// => ZodString {
//      spa: [Function: bound safeParseAsync] AsyncFunction,
//      _def: { checks: [], typeName: "ZodString", coerce: false },
//      parse: [Function: bound parse],
//      safeParse: [Function: bound safeParse],
//      parseAsync: [Function: bound parseAsync] AsyncFunction,
//      safeParseAsync: [Function: bound safeParseAsync] AsyncFunction,
//      refine: [Function: bound ],
//      refinement: [Function: bound refinement],
//      superRefine: [Function: bound superRefine],
//      optional: [Function: bound ],
//      nullable: [Function: bound ],
//      nullish: [Function: bound nullish],
//      array: [Function: bound array],
//      promise: [Function: bound promise],
//      or: [Function: bound or],
//      and: [Function: bound and],
//      transform: [Function: bound ],
//      brand: [Function: bound brand],
//      default: [Function: bound ],
//      catch: [Function: bound catch],
//      describe: [Function: bound describe],
//      pipe: [Function: bound pipe],
//      readonly: [Function: bound readonly],
//      isNullable: [Function: bound isNullable],
//      isOptional: [Function: bound isOptional]
//    }

const HeloWorldSchema = z
  .object({
    message: z.string().openapi({  // <= Property 'openapi' does not exist on type 'ZodString'.
      example: "Hello World!",
    }),
  })
  .openapi("HeloWorldResponse");  // <= Property 'openapi' does not exist on type 'ZodObject<{ message: any; }, "strip", ZodTypeAny, { [x: string]: any; message?: any; }, { [x: string]: any; message?: any; }>'

const route = createRoute({
  method: "get",
  path: "/api/hello_world",
  request: {
    params: ParamsSchema,
  },
  responses: {
    200: {
      content: {
        "application/json": {
          schema: HeloWorldSchema,
        },
      },
      description: "Retrieve the user",
    },
  },
});

const app = new OpenAPIHono();

const appRoutes = app.openapi(route, (c) => {
  return c.json({
    message: "Hello World!",
  });
});

export type AppRoutesType = typeof appRoutes;

Again, an error is made here by creating a client using a type created from a definition that is probably faulty in some way.

import { useEffect, useState } from "preact/hooks";
import { hc } from "$hono/mod.ts";
import type { AppRoutesType } from "../routes/api/[...path].ts";
const client = hc<AppRoutesType>("http://localhost:8000/");   //  <= Type mismatch

export default function HelloWorld() {
  // omission
}

RPC with openapi removed works correctly.

What is the expected behavior?

RPC can be used without causing type errors.

What do you see instead?

No response

Additional information

Perhaps this issue illustrates the same problem. https://github.com/honojs/hono/issues/1784

About this issue

  • Original URL
  • State: closed
  • Created 3 months ago
  • Comments: 19 (11 by maintainers)

Most upvoted comments

@yusukebe san @nakasyou san

Thank you very much. I will try the method of using npm:hono/client. I will report the results again within 12 hours.

Problem solved. Thank you!

@nakasyou san CC: @yusukebe

Thank you very much.

I have referred to the source code for the minimum configuration you provided. I wrote npm: directly once since it seemed that importmap was not used as a comparison.

I also removed the option to use node_modules and re-cached it again, and it now works.

Once normalized, using importmap again also works.

Perhaps the cache and the option to use node_modules in the default settings of fresh were incompatible.

hono I don’t think that in itself is a problem. Thanks for confirming.

updated: https://github.com/Octo8080X/inspect-fresh-hono-openapi-rpc

Hmm, I can’t understand for it. I tried https://github.com/Octo8080X/inspect-fresh-hono-openapi-rpc, it doesn’t work well.

But, I created minimal sample https://github.com/nakasyou/hono-openapi-deno, it’s working well.

Not sure, but it may be because @asteasolutions/zod-to-openapi only supports CommonJS and not ESM.

@nakasyou Can you take a look?

@nakasyou

But RPC-mode working well when I replaced https://deno.land/x/hono/mod.ts to npm:hono/client. I think the reason is that the hono referenced by @hono/zod-openapi is not https://deno.land/x/hono/mod.ts, but on npm.

Yeah, exactly!

@Octo8080X

So if you want to use RPC-mode, you can use the modules with npm specifier like npm:hono/client.

@yusukebe

Is RPC-mode working well?

I tested, the answer is no. But RPC-mode working well when I replaced https://deno.land/x/hono/mod.ts to npm:hono/client. I think the reason is that the hono referenced by @hono/zod-openapi is not https://deno.land/x/hono/mod.ts, but on npm.