Skip to content

Improvement suggestion for Middleware type definition #1916

Closed
@yicrotkd

Description

@yicrotkd

Description

The middleware throws the following error when neither onRequest() nor onResponse() is provided.

"Middleware must be an object with one of onRequest() or onResponse()"
https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-fetch/src/index.js#L223

Based on this, I think the type definition below could be improved by making it more explicit and better aligned with the intended behavior: https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-fetch/src/index.d.ts#L147

Proposal

It seems like the Middleware interface might be improved with the following modification:

export type Middleware {
  onRequest?: ...;
  onResponse: ...;
} | {
  onRequest: ...;
  onResponse?: ...;
}

This modification ensures that either onRequest or onResponse is required, and this is reflected in the type, allowing the issue to be detected at compile time.

Checklist

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestopenapi-fetchRelevant to the openapi-fetch library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions