Skip to content

Commit

Permalink
Specify ErrorResponse as interface to provide obvious contract (#10876
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sgrishchenko authored Sep 27, 2023
1 parent da57748 commit b98e82d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/error-response-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Fix `ErrorResponse` type to avoid leaking internal field
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,4 @@
- yionr
- yuleicul
- zheng-chuang
- sgrishchenko
17 changes: 11 additions & 6 deletions packages/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1533,11 +1533,21 @@ export const redirectDocument: RedirectFunction = (url, init) => {
return response;
};

export type ErrorResponse = {
status: number;
statusText: string;
data: any;
};

/**
* @private
* Utility class we use to hold auto-unwrapped 4xx/5xx Response bodies
*
* We don't export the class for public use since it's an implementation
* detail, but we export the interface above so folks can build their own
* abstractions around instances via isRouteErrorResponse()
*/
export class ErrorResponseImpl {
export class ErrorResponseImpl implements ErrorResponse {
status: number;
statusText: string;
data: any;
Expand All @@ -1562,11 +1572,6 @@ export class ErrorResponseImpl {
}
}

// We don't want the class exported since usage of it at runtime is an
// implementation detail, but we do want to export the shape so folks can
// build their own abstractions around instances via isRouteErrorResponse()
export type ErrorResponse = InstanceType<typeof ErrorResponseImpl>;

/**
* Check if the given error is an ErrorResponse generated from a 4xx/5xx
* Response thrown from an action/loader
Expand Down

0 comments on commit b98e82d

Please sign in to comment.