Skip to content

[BUG] [typescript-fetch] Correctly handle number response #3709

@deluksic

Description

@deluksic

When generating code from a path like this:

    get:
      operationId: getSlider
      description: Gets the slider value
      responses:
        200:
          description: Successfully returned slider location
          content:
            application/json:
              schema:
                type: number
                format: float

A type checking error is encountered:

Type 'TextApiResponse' is not assignable to type 'ApiResponse<number>'.
  Types of property 'value' are incompatible.
    Type '() => Promise<string>' is not assignable to type '() => Promise<number>'.
      Type 'Promise<string>' is not assignable to type 'Promise<number>'.
        Type 'string' is not assignable to type 'number'.  TS2322

    77 |         });
    78 | 
  > 79 |         return new runtime.TextApiResponse(response);
       |         ^
    80 |     }
    81 | 
    82 |    /**

Suggest adding a response handler, something like this to runtime.ts:

export class NumberApiResponse {
    constructor(public raw: Response) { }

    async value() {
        const rawText = await this.raw.text();
        const response = JSON.parse(rawText);
        if (typeof response !== 'number') {
            // this check might be unnecessary
            throw new Error(`Expected response of type 'number', got ${rawText}`)
        }
        return response;
    };
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions