You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* wip vibecode
* wip: ApiResponse infer withResponse
* wip: fix response.error/status inference
* fix: inference based on error status code
* feat: TAllResponses for happy-case narrowing
* feat: configurable status codes
* chore: clean
* feat: includeClient option
* feat: add CLI options
Adds CLI options for client inclusion and success codes
Introduces flags to control API client generation and customize success status codes via the command line, enhancing flexibility for different use cases.
* chore: add examples
* docs: usage examples
* docs
* feat: return Response object directly when using withResponse
Improves type-safe API error handling and response access
Refactors API client response typing to unify success and error data under a consistent interface.
Replaces separate error property with direct data access and ensures the Response object retains its methods.
Updates documentation with clearer examples for type-safe error handling and data access patterns.
Facilitates more ergonomic and predictable client usage, especially for error cases.
* chore: update snapshots
* fix: tanstack type-error due to withResponse
* wip: allow passing withResponse to tanstack api
Adds type-safe error handling to TanStack Query generator
Introduces discriminated unions and configurable success status codes for more robust error handling in generated TanStack Query clients.
Supports advanced mutation options with `withResponse` and `selectFn` to enable granular control over success and error transformations in API responses.
Improves documentation to highlight new error handling features and integration patterns.
* feat: type mutationOptions errors (mutate callback onError)
* feat: configurable error status codes
* chore: changeset
* refactor: throw a Response
* chore: update docs
* chore: add pkg.pr.new badge
* chore: add pkg.pr.new workflow
https://github.com/stackblitz-labs/pkg.pr.new?tab=readme-ov-file#examples
* ci
* ci
* chore: mv examples
* feat: SuccessResponse/ErrorResponse
* feat: TypedResponseError + expose successStatusCodes/errorStatusCodes
* docs: link to example fetcher + inline example
* refactor: ai slop + allow withResponse/throwOnStatusError on tanstack mutations
test: add integration tests for the tanstack runtime
feat: expose runtime status codes + new InferResponseByStatus type
ci: fix
* chore: update tests
* docs
Add comprehensive type-safe error handling and configurable status codes
6
+
7
+
-**Type-safe error handling**: Added discriminated unions for API responses with `SafeApiResponse` and `InferResponseByStatus` types that distinguish between success and error responses based on HTTP status codes
8
+
-**TypedResponseError class**: Introduced `TypedResponseError` that extends the native Error class to include typed response data for easier error handling
9
+
- Expose `successStatusCodes` and `errorStatusCodes` arrays on the generated API client instance for runtime access
10
+
-**withResponse parameter**: Enhanced API clients to optionally return both the parsed data and the original Response object for advanced use cases
11
+
-**throwOnStatusError option**: Added `throwOnStatusError` option to automatically throw `TypedResponseError` for error status codes, simplifying error handling in async/await patterns, defaulting to `true` (unless `withResponse` is set to true)
12
+
-**TanStack Query integration**: The above features are fully integrated into the TanStack Query client generator:
13
+
- Advanced mutation options supporting `withResponse` and `selectFn` parameters
14
+
- Automatic error type inference based on OpenAPI error schemas instead of generic Error type
15
+
- Type-safe error handling with discriminated unions for mutations
16
+
- Response-like error objects that extend Response with additional `data` property for consistency
17
+
-**Configurable status codes**: Made success and error status codes fully configurable:
18
+
- New `--success-status-codes` and `--error-status-codes` CLI options
19
+
-`GeneratorOptions` now accepts `successStatusCodes` and `errorStatusCodes` arrays
20
+
- Default error status codes cover comprehensive 4xx and 5xx ranges
21
+
-**Enhanced CLI options**: Added new command-line options for better control:
22
+
-`--include-client` to control whether to generate API client types and implementation
23
+
-`--include-client=false` to only generate the schemas and endpoints
24
+
-**Enhanced types**: expose `SuccessStatusCode` / `ErrorStatusCode` type and their matching runtime typed arrays
25
+
-**Comprehensive documentation**: Added detailed examples and guides for error handling patterns
26
+
27
+
This release significantly improves the type safety and flexibility of generated API clients, especially for error handling scenarios.
- Headless API client, bring your own fetcher ! (fetch, axios, ky, etc...)
13
+
- Headless API client, [bring your own fetcher](packages/typed-openapi/API_CLIENT_EXAMPLES.md#basic-api-client-api-client-examplets)(fetch, axios, ky, etc...) !
12
14
- Generates a fully typesafe API client with just types by default (instant suggestions)
15
+
-**Type-safe error handling**: with discriminated unions and configurable success/error status codes
16
+
-**withResponse & throwOnStatusError**: Get a union-style response object or throw on configured error status codes, with full type inference
17
+
-**TanStack Query integration**: with `withResponse` and `selectFn` options for advanced success/error handling
13
18
- Or you can also generate a client with runtime validation using one of the following runtimes:
For more info, run any command with the `--help` flag: $ typed-openapi --help
53
+
For more info, run any command with the `--help` flag:
54
+
$ typed-openapi --help
47
55
48
-
Options: -o, --output <path> Output path for the api client ts file (defaults to `<input>.<runtime>.ts`) -r, --runtime
49
-
<name> Runtime to use for validation; defaults to `none`; available: 'none'|'arktype'|'io-ts'|'typebox'|
50
-
'valibot'|'yup'|'zod' (default: none) -h, --help Display this message -v, --version Display version number
56
+
Options:
57
+
-o, --output <path> Output path for the api client ts file (defaults to `<input>.<runtime>.ts`)
58
+
-r, --runtime <n> Runtime to use for validation; defaults to `none`; available: Type<"arktype"|"io-ts"|"none"|"typebox"|"valibot"|"yup"|"zod"> (default: none)
59
+
--schemas-only Only generate schemas, skipping client generation (defaults to false) (default: false)
60
+
--include-client Include API client types and implementation (defaults to true) (default: true)
61
+
--success-status-codes <codes> Comma-separated list of success status codes for type-safe error handling (defaults to 2xx and 3xx ranges)
62
+
--tanstack [name] Generate tanstack client with withResponse support for error handling, defaults to false, can optionally specify a name for the generated file
63
+
-h, --help Display this message
64
+
-v, --version Display version number
51
65
```
52
66
53
67
## Non-goals
@@ -65,6 +79,223 @@ Options: -o, --output <path> Output path for the api client ts file (defaults to
65
79
66
80
Basically, let's focus on having a fast and typesafe API client generation instead.
67
81
82
+
## Usage Examples
83
+
84
+
### API Client Setup
85
+
86
+
The generated client is headless - you need to provide your own fetcher. Here are ready-to-use examples:
87
+
88
+
-**[Basic API Client](packages/typed-openapi/API_CLIENT_EXAMPLES.md#basic-api-client-api-client-examplets)** - Simple, dependency-free wrapper
89
+
-**[Validating API Client](packages/typed-openapi/API_CLIENT_EXAMPLES.md#validating-api-client-api-client-with-validationts)** - With request/response validation
90
+
91
+
92
+
### Type-Safe Error Handling & Response Modes
93
+
94
+
You can choose between two response styles:
95
+
96
+
-**Direct data return** (default):
97
+
```ts
98
+
const user =awaitapi.get("/users/{id}", { path: { id: "123" } });
99
+
// Throws TypedResponseError on error status (default)
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.
0 commit comments