astahmer / typed-openapi

Generate a headless Typescript API client from an OpenAPI spec
https://typed-openapi-web.vercel.app/
200 stars 23 forks source link

parameter body on post is not generated #8

Closed MartinN3 closed 11 months ago

MartinN3 commented 1 year ago

In your example you have

// <ApiClientTypes>
export type EndpointParameters = {
  body?: unknown;
  query?: Record<string, unknown>;
  header?: Record<string, unknown>;
  path?: Record<string, unknown>;
};

which means body can be specified, therefore

 export type post_AddPet = {
    method: "POST";
    path: "/pet";
    parameters: never;
    response: Schemas.Pet;
  };

should be

  export type post_AddPet = {
    method: "POST";
    path: "/pet";
    parameters: {
      body: Schemas.Pet
    };
    response: Schemas.Pet;
  };

is that a case? Thanks.

veej commented 1 year ago

I just ran into the same issue. Is there any info?

astahmer commented 1 year ago

sorry for the late response, I've been focusing on maintaining another open source library (Panda CSS), so I haven't been using this lib for a while, feel free to send a PR 🙏

MartinN3 commented 1 year ago

Thanks for acknowledge

brentrager commented 1 year ago

@astahmer I believe the patchfile below fixes this issue. I have a whole branch with this if you give me a way to make a PR.

diff --git a/packages/typed-openapi/src/generator.ts b/packages/typed-openapi/src/generator.ts
index 79ce224..e51b149 100644
--- a/packages/typed-openapi/src/generator.ts
+++ b/packages/typed-openapi/src/generator.ts
@@ -143,6 +143,7 @@ const generateEndpointSchemaList = (ctx: GeneratorContext) => {
             ${parameters.query ? `query:  ${parameterObjectToString(parameters.query)},` : ""}
         ${parameters.path ? `path:  ${parameterObjectToString(parameters.path)},` : ""}
         ${parameters.header ? `header:  ${parameterObjectToString(parameters.header)},` : ""}
+        ${parameters.body ? `body:  ${parameterObjectToString(parameters.body)},` : ""}
           }`
           : "parameters: never,"
       }
diff --git a/packages/typed-openapi/src/map-openapi-endpoints.ts b/packages/typed-openapi/src/map-openapi-endpoints.ts
index dc852a5..a7107fb 100644
--- a/packages/typed-openapi/src/map-openapi-endpoints.ts
+++ b/packages/typed-openapi/src/map-openapi-endpoints.ts
@@ -72,6 +72,7 @@ export const mapOpenApiEndpoints = (doc: OpenAPIObject) => {

       // Body
       if (operation.requestBody) {
+        endpoint.meta.hasParameters = true;
         const requestBody = refs.unwrap(operation.requestBody ?? {});
         const content = requestBody.content;
         const matchingMediaType = Object.keys(content).find(isAllowedParamMediaTypes);
@@ -154,7 +155,7 @@ type MutationMethod = "post" | "put" | "patch" | "delete";
 type Method = "get" | "head" | "options" | MutationMethod;

 export type EndpointParameters = {
-  body?: Box;
+  body?: Box<BoxRef>;
   query?: Box<BoxRef> | Record<string, AnyBox>;
   header?: Box<BoxRef> | Record<string, AnyBox>;
   path?: Box<BoxRef> | Record<string, AnyBox>;
astahmer commented 1 year ago

awesome !

if you give me a way to make a PR.

what do you mean ? to contribute you can fork the repo, then :

pnpm i
// make your changes then
pnpm build
// add your test then
pnpm test
brentrager commented 1 year ago

@astahmer cool, didn't know that forking was necessary, so wasn't clear, but will do

till-work commented 1 year ago

I think this fix introduced a problem. Body type names are not prefixed by namespace Schemas. which leads to typescript errors.

astahmer commented 1 year ago

would you like to work on the fix ?

till-work commented 1 year ago

I'll have a Look :-)

astahmer commented 11 months ago

this was fixed recently ! https://github.com/astahmer/typed-openapi/pull/16