Skip to content

Feature request: Do not rely directly on Response for Node.js compatability #91

@jpierson-at-riis

Description

@jpierson-at-riis

For Node.js project where we still would like to make typed clients, it would be useful to have an ApiClient implementation that doesn't directly rely on the Response type normally available in the browser. Although Node.js does include fetch and Response in more recent versions, it doesn't appear that TypeScript recognizes this yet within any node specific target platform configuration from what I can tell. A workaround to this may be to create an interface such as the one below that includes a subset of the used features from the Response type and include that interface within the generated output. Thanks to structural typing of TypeScript it may be possible to generically type the Request then in a way that requires the actual Request object used to extend ApiResponse.

// Minimal interface for the Response type used in this file
export interface ApiResponse {
  headers: {
    get(name: string): string | null;
  };
  json(): Promise<any>;
  text(): Promise<string>;
}

Alternatively, some effort could be done to include a full Response definition lifted from the DOM lib or to have some guidance on including this definition in custom global type definition within Node.js project that want to make use of typed-openapi. In the future, if Request is included in node a specific lib for TypeScript then the issue may be somewhat moot but by relying on locally defined interfaces things should work either way.

References:

Here are the main TypeScript and DefinitelyTyped GitHub issues related to Node.js global fetch/Response/Request types and their availability in TypeScript:

TypeScript: Add Node.js fetch/Response/Request types to the standard library

microsoft/TypeScript#48822
https://github.com/microsoft/TypeScript/issues/48822
This is the main tracking issue for adding Node.js global fetch and related types to TypeScript’s standard library. It discusses the challenges and progress of aligning Node.js and web platform types.


DefinitelyTyped: Add Node.js fetch types to @types/node

DefinitelyTyped/DefinitelyTyped#60924
https://github.com/DefinitelyTyped/DefinitelyTyped/issues/60924
This issue tracks the addition of fetch and related types to the @types/node package, which is what most Node.js projects use for type definitions.


Node.js: Add fetch and related types to Node.js core

nodejs/node#41749
https://github.com/nodejs/node/issues/41749
This is the Node.js core issue for tracking the addition of fetch and related types to Node.js itself.

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