HonoH
Hono8mo ago
Paul

How to accept flattened objects in query params?

It seems that the hono client calls String() on the query params so that any objects passed through which should normally be something like filters[key]=value ends up becoming filters="[object Object]".

  1. I'm pretty sure this use-case should be supported since it appears out in the wild when using query params
  2. Is there a way to make this work? or is the recommendation to go with flat query objects / honojs client doesn't support objects in query params?
I think this is an error/bug that should be updated. Just want to confirm here before writing an issue on github...

// dev.spec.ts
it.only("GET /dev/query returns nested query params", async () => {
    const response = await testApiClient.api.dev.query.$get({
        query: { filters: { active: true } },
    });
    expect(response.status).toBe(HttpStatusCodes.OK);
});


// dev.routes.ts
import { createRoute, z } from "@hono/zod-openapi";

export const getQuery = createRoute({
    method: "get",
    path: "/dev/query",
    operationId: "getQuery",
    tags,
    request: {
        query: z.object({
                    filters: z.any().optional(),
        }),
    },
    responses: {
        [HttpStatusCodes.OK]: jsonContent(
            z.object({
                timestamp: z.string().datetime(),
                success: z.any(),
            }),
            "Successfully retrieved query"
        ),
    },
});


[1/2]
Was this page helpful?