ecyrbe / zodios

typescript http client and server with zod validation
https://www.zodios.org/
MIT License
1.71k stars 46 forks source link

make config parameter read-only #111

Closed koichik closed 2 years ago

koichik commented 2 years ago

Hi, query string as an array again! I'm useing Zodios with a GraphQL DataLoader.

example:

const userResponse = z.array(
  z.object({
    id: z.number(),
    name: z.string(),
  })
}),

type UserResponse = z.infer<typeof userResponse>

const apiClient = new Zodios(
  "https://example.com",
  [
    {
      method: "get",
      path: "/users",
      alias: "getUsers",
      parameters: [
        {
          type: 'Query',
          name: 'keys',
          schema: z.array(z.string()).min(1),
        },
      ],
      response: userResponse,
    },
  ],
);

const loader = new DataLoader<string, UserResponse>(
  async function batchFunction(keys) { // keys: readonly string[]
    const response = await apiClient.getUsers({ queries: { keys: keys as string[]} })
    ...
  }
)

It's works very well but unfortunately it needs cast because a DataLoader passes a readonly array. I don't think Zodios will change the passed config (In fact, Zodios internally casts them as read-only.), so could you concider adding readonly to the config parameter deeply? (Probably the same for the body parameter of the post method, etc.)

ecyrbe commented 2 years ago

This should be doable. I'll have to check the impact. it might be a breaking change for '@zodios/react'

ecyrbe commented 2 years ago

@koichik fixed. i had to upgrade to a major version as this is a breaking change for @zodios/react like i expected from now on, all zodios libraries will have their major versions aligned to make it easier to upgrade when their is breaking changes

koichik commented 2 years ago

Excellent! Thanks for your hard work !