Skip to content

Commit 2fba59a

Browse files
committed
chore: update docs
1 parent 4d68d5b commit 2fba59a

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,19 @@ await queryClient.fetchQuery(
207207

208208
## useMutation
209209

210-
The mutation API supports both basic usage and advanced error handling with `withResponse` and custom transformations with `selectFn`:
210+
The mutation API supports both basic usage and advanced error handling with `withResponse` and custom transformations with `selectFn`. **Note**: All mutation errors are Response-like objects with type-safe error inference based on your OpenAPI error schemas.
211211

212212
```ts
213213
// Basic mutation (returns data only)
214-
const basicMutation = useMutation(
215-
tanstackApi.mutation("post", '/authorization/organizations/:organizationId/invitations').mutationOptions
216-
);
214+
const basicMutation = useMutation({
215+
...tanstackApi.mutation("post", '/authorization/organizations/:organizationId/invitations').mutationOptions,
216+
onError: (error) => {
217+
// error is a Response-like object with typed data based on OpenAPI spec
218+
console.log(error instanceof Response); // true
219+
console.log(error.status); // 400, 401, etc. (properly typed)
220+
console.log(error.data); // Typed error response body
221+
}
222+
});
217223

218224
// With error handling using withResponse
219225
const mutationWithErrorHandling = useMutation(

packages/typed-openapi/TANSTACK_QUERY_ERROR_HANDLING.md

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ For the above spec, the TanStack Query client generates:
7575
```typescript
7676
// Error type is automatically inferred as:
7777
type CreateUserError =
78-
| { status: 400; data: ValidationError }
79-
| { status: 409; data: ConflictError }
80-
| { status: 500; data: ServerError }
78+
| Omit<Response, 'status'> & { status: 400; data: ValidationError }
79+
| Omit<Response, 'status'> & { status: 409; data: ConflictError }
80+
| Omit<Response, 'status'> & { status: 500; data: ServerError }
8181
```
8282
8383
## Usage Examples
@@ -93,6 +93,9 @@ function CreateUserForm() {
9393
...tanstackApi.mutation("post", "/users").mutationOptions,
9494
onError: (error) => {
9595
// error is fully typed based on your OpenAPI spec!
96+
// error is also a Response instance with additional data property
97+
console.log(error instanceof Response); // true
98+
9699
if (error.status === 400) {
97100
// error.data is typed as ValidationError
98101
console.error('Validation failed:', error.data.message);
@@ -138,6 +141,10 @@ function AdvancedCreateUserForm() {
138141
}).mutationOptions,
139142
onError: (error) => {
140143
// Same typed error handling as above
144+
// error is also a Response instance
145+
console.log(error.ok); // false
146+
console.log(error.headers); // Response headers
147+
141148
switch (error.status) {
142149
case 400:
143150
toast.error(`Validation: ${error.data.fields.join(', ')}`);
@@ -206,13 +213,40 @@ const handleError = (error: CreateUserError) => {
206213

207214
## Error Structure
208215

209-
All errors thrown by TanStack Query mutations follow this structure:
216+
All errors thrown by TanStack Query mutations are **Response-like objects** that extend the native Response with additional type safety:
210217

211218
```typescript
212-
interface ApiError<TData = unknown> {
213-
status: number; // HTTP status code (400-511)
219+
interface ApiError<TData = unknown> extends Omit<Response, 'status'> {
220+
status: number; // HTTP status code (400-511) - properly typed
214221
data: TData; // Typed error response body
222+
// All other Response properties are available:
223+
// ok: boolean
224+
// headers: Headers
225+
// url: string
226+
// etc.
215227
}
216228
```
217229

218-
This makes error handling predictable and type-safe across your entire application.
230+
### Key Benefits of Response-like Errors
231+
232+
- **instanceof Response**: Error objects are proper Response instances
233+
- **Consistent API**: Both success and error responses follow the same Response pattern
234+
- **Full Response Access**: Access to headers, URL, and other Response properties
235+
- **Type Safety**: The `status` property is properly typed based on configured error status codes
236+
237+
### Example Usage
238+
239+
```typescript
240+
try {
241+
await mutation.mutationFn(params);
242+
} catch (error) {
243+
console.log(error instanceof Response); // true
244+
console.log(error.ok); // false
245+
console.log(error.status); // 400, 401, etc. (properly typed)
246+
console.log(error.data); // Typed error response body
247+
console.log(error.headers.get('content-type')); // Response headers
248+
console.log(error.url); // Request URL
249+
}
250+
```
251+
252+
This makes error handling predictable and type-safe across your entire application while maintaining consistency with the Response API.

packages/typed-openapi/TANSTACK_QUERY_EXAMPLES.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ function UserForm() {
128128
}
129129
},
130130
onError: (error) => {
131-
// Type-safe error handling - error has shape { status: number, data: ErrorType }
131+
// Type-safe error handling - error is a Response-like object with data property
132+
console.log(error instanceof Response); // true
133+
console.log(error.ok); // false
134+
132135
if (error.status === 400) {
133136
toast.error(`Validation failed: ${error.data.message}`);
134137
} else if (error.status === 500) {

0 commit comments

Comments
 (0)