Skip to content

[typescript-fetch] Invalid response signature #439

@paztis

Description

@paztis

I use typescript-fetch to generate me fetch code for this openapi operation

        "/pet/{petId}": {
            "get": {
                "tags": [
                    "pet"
                ],
                "summary": "Find pet by ID.",
                "description": "Returns a single pet.",
                "operationId": "getPetById",
                "parameters": [
                    {
                        "name": "petId",
                        "in": "path",
                        "description": "ID of pet to return",
                        "required": true,
                        "schema": {
                            "type": "integer",
                            "format": "int64"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "successful operation",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Pet"
                                }
                            },
                            "application/xml": {
                                "schema": {
                                    "$ref": "#/components/schemas/Pet"
                                }
                            }
                        }
                    },
                    "400": {
                        "description": "Invalid ID supplied"
                    },
                    "404": {
                        "description": "Pet not found"
                    },
                    "default": {
                        "description": "Unexpected error"
                    }
                },
            }
  }

It generates me this code

  async getPetById(
    p: {
      petId: number
    },
    timeout?: number,
    opts: RequestInit = {},
  ): Promise<
    Res<200, t_Pet> | Res<400, void> | Res<404, void> | Res<StatusCode, void>
  > {
    const url = this.basePath + `/pet/${p["petId"]}`
    const headers = this._headers({ Accept: "application/json" }, opts.headers)

    return this._fetch(url, { method: "GET", ...opts, headers }, timeout)
  }

But this is not usable at runtime because

    const client = new SwaggerPetstoreOpenApi30({
        basePath: 'https://petstore.swagger.io/v2'
    });
    const response = await client.getPetById({
        petId: 1
    });

    if (response.status === 200) {
        Const data = await response.json();  => here data is t_Pet or void 
    }

What you must generate is

  async getPetById(
    p: {
      petId: number
    },
    timeout?: number,
    opts: RequestInit = {},
  ): Promise<
    Res<200, t_Pet> | Res<400, void> | Res<404, void> | Res<Exclude<StatusCode, 200 | 400 | 404>, void>
  > {
    const url = this.basePath + `/pet/${p["petId"]}`
    const headers = this._headers({ Accept: "application/json" }, opts.headers)

    return this._fetch(url, { method: "GET", ...opts, headers }, timeout)
  }

You need to add Exclude the processed StatusCodes from the list

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions