Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] data for POST request is optional #171

Closed
QzCurious opened this issue Oct 7, 2022 · 7 comments
Closed

[Bug] data for POST request is optional #171

QzCurious opened this issue Oct 7, 2022 · 7 comments
Labels
bug Something isn't working

Comments

@QzCurious
Copy link
Contributor

I've define a body parameter for my post request. But I can ignore data without ts showing error.

codesandbox

import { makeApi, Zodios } from "@zodios/core";
import { z } from "zod";

export const Request = z.object({
  page: z.number().int().min(1),
  perpage: z.number().int().max(20)
});

const Response = z.object({});

const userApi = makeApi([
  {
    method: "post",
    path: "user/list",
    alias: "userList",
    immutable: true,
    parameters: [
      {
        name: "body",
        type: "Body",
        schema: Request
      }
    ],
    response: Response
  }
]);

const apiClient = new Zodios(userApi);
apiClient.userList(); // <-- data should be required
apiClient.post("user/list"); // <-- data should be required
@ecyrbe
Copy link
Owner

ecyrbe commented Oct 7, 2022

Thank you for opening this issue,
I'll check this bug this weekend.

@ecyrbe ecyrbe added the bug Something isn't working label Oct 11, 2022
@thelinuxlich
Copy link
Contributor

I need to investigate more but I'm using a library to extract [err, result] from async ops and specifically with zod POST endpoints it's returning a different type, forcing me to use as assertion

@thelinuxlich
Copy link
Contributor

Now, thinking about it again, wouldn't it be better if the POST endpoints instead of accepting body as the first parameter, did like the GET endpoints with a single object(so body would be a property of this object)?

@ecyrbe
Copy link
Owner

ecyrbe commented Oct 13, 2022

yes, this is maybe a disgn mistake on my part. it makes things harder to implement and also harder to autocomplete.
i'll think about it. i don't want to create a breaking change, so i need to try if overload could work here to support both.

In the mean time i'll try to first fix the reported bug.

@thelinuxlich
Copy link
Contributor

Well, major releases are expected to introduce breaking changes :)

@ecyrbe
Copy link
Owner

ecyrbe commented Oct 18, 2022

Hello @QzCurious,

Just to keep you informed that i found a way to handle this. it's not really pretty, but works.
This issue was really hard to solve because we can't declare overloads to solve this issue on all parameters types (issue is appearing for query, params and header parameters, not just body.
I need a couple more days to finalize the change on all impacted packages (core, plugins, react; solid)

@ecyrbe ecyrbe closed this as completed in 890db78 Oct 20, 2022
@ecyrbe
Copy link
Owner

ecyrbe commented Oct 20, 2022

fixed in v10.2.0 :

  • @zodios/core
  • @zodios/plugins
  • @zodios/react
  • @zodios/solid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants