@rexeus/typeweaver-types 0.8.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -33,7 +33,7 @@ This plugin is included by default and doesn't need to be explicitly specified:
33
33
 
34
34
  ```bash
35
35
  # Generate with clients + types plugins
36
- npx typeweaver generate --input ./api/definition --output ./api/generated --plugins clients
36
+ npx typeweaver generate --input ./api/spec/index.ts --output ./api/generated --plugins clients
37
37
  ```
38
38
 
39
39
  More details on how to use the
@@ -49,7 +49,7 @@ For each operation (e.g., `CreateTodo`), the plugin generates four main files:
49
49
  - `<OperationId>ResponseValidator.ts`
50
50
 
51
51
  These files contain the necessary types and validators for requests and responses. All of these
52
- provided types and classes are exported.
52
+ provided types and factory functions are exported.
53
53
 
54
54
  ### 📨 Request Types
55
55
 
@@ -77,16 +77,17 @@ e.g. `CreateTodoResponse.ts`. This file contains for each response defined inlin
77
77
  `ICreateTodoSuccessResponseHeader`
78
78
  - **`I<ResponseName>ResponseBody`** - Type for success response payload structure, if defined, e.g.
79
79
  `ICreateTodoSuccessResponseBody`
80
- - **`I<ResponseName>Response`** - Complete success response interface with status code, e.g.
81
- `I<ResponseName>Response`
82
- - **`<ResponseName>Response`** - Response class extending HttpResponse with validation and type
83
- safety, e.g. `CreateTodoSuccessResponse`
80
+ - **`I<ResponseName>Response`** - Typed response object using
81
+ `ITypedHttpResponse<TType, TStatusCode, THeader, TBody>`, e.g. `ICreateTodoSuccessResponse`
82
+ - **`create<ResponseName>Response`** - Factory function that creates a typed response object, e.g.
83
+ `createCreateTodoSuccessResponse`
84
84
 
85
- Furthermore, two union types are generated, which include details about all possible responses
86
- (success + error), not only those defined inline in the operation:
85
+ Furthermore, a union type is generated, which includes all possible responses (success + error), not
86
+ only those defined inline in the operation:
87
87
 
88
- - **`I<OperationId>Response`** - Union type of all response types e.g. `ICreateTodoResponse`
89
- - **`<OperationId>Response`** - Union type of all response classes, e.g. `CreateTodoResponse`
88
+ - **`<OperationId>Response`** - Union type of all `I`-prefixed response types, e.g.
89
+ `CreateTodoResponse` which is a union of
90
+ `ICreateTodoSuccessResponse | IForbiddenErrorResponse | ...`
90
91
 
91
92
  ### 📨✓ Request Validators
92
93
 
@@ -150,7 +151,7 @@ Response validation logic for an operation is defined in one file:
150
151
  - **`safeValidate()`** - Non-throwing validation method returning `SafeResponseValidationResult`
151
152
  - **`validate()`** - Throwing validation method that returns validated response or throws
152
153
  ResponseValidationError
153
- - Valid response data is an instance of one of the generated response classes
154
+ - Valid response data is a typed response object matching one of the generated response types
154
155
  - **Header coercion logic** - Automatic conversion of headers to schema-appropriate types (single
155
156
  string value & multi string value headers)
156
157
  - **Response validation errors** - Include details about the issues with all possible responses for
@@ -165,11 +166,7 @@ Response validation logic for an operation is defined in one file:
165
166
 
166
167
  ```typescript
167
168
  import { ResponseValidationError, type IHttpResponse } from "@rexeus/typeweaver-core";
168
- import {
169
- CreateTodoResponseValidator,
170
- CreateTodoSuccessResponse,
171
- InternalServerErrorResponse,
172
- } from "path/to/generated/output";
169
+ import { CreateTodoResponseValidator } from "path/to/generated/output";
173
170
 
174
171
  const responseValidator = new CreateTodoResponseValidator();
175
172
 
@@ -183,11 +180,11 @@ const safeResult = responseValidator.safeValidate(response);
183
180
  if (safeResult.isValid) {
184
181
  console.log("Response is valid", safeResult.data);
185
182
 
186
- // Data is an instance of one of the defined response classes
187
- if (safeResult.data instanceof CreateTodoSuccessResponse) {
183
+ // Data is a typed response object use the type discriminator to narrow
184
+ if (safeResult.data.type === "CreateTodoSuccess") {
188
185
  // handle CreateTodoSuccessResponse
189
186
  }
190
- if (safeResult.data instanceof InternalServerErrorResponse) {
187
+ if (safeResult.data.type === "InternalServerError") {
191
188
  // handle InternalServerErrorResponse
192
189
  }
193
190
  // handle other response types ...
@@ -201,8 +198,8 @@ try {
201
198
  const validatedResponse = responseValidator.validate(response);
202
199
  console.log("Response is valid", validatedResponse);
203
200
 
204
- // Same here: Data is an instance of one of the defined response classes
205
- if (validatedResponse instanceof CreateTodoSuccessResponse) {
201
+ // Same here: use the type discriminator to narrow
202
+ if (validatedResponse.type === "CreateTodoSuccess") {
206
203
  // handle CreateTodoSuccessResponse
207
204
  }
208
205
  // ... handle other response types