@rexeus/typeweaver-types 0.7.0 → 0.9.0
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 +18 -21
- package/dist/index.cjs +331 -524
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +327 -515
- package/dist/index.mjs.map +1 -1
- package/dist/lib/ResponseValidator.d.ts +21 -7
- package/dist/lib/ResponseValidator.js +40 -2
- package/dist/lib/definitionLookup.d.ts +29 -0
- package/dist/lib/definitionLookup.js +14 -0
- package/dist/lib/index.ts +1 -1
- package/dist/templates/Request.ejs +0 -24
- package/dist/templates/RequestValidator.ejs +9 -15
- package/dist/templates/Response.ejs +20 -60
- package/dist/templates/ResponseValidator.ejs +23 -85
- package/dist/templates/SharedResponse.ejs +16 -28
- package/package.json +8 -8
- package/dist/lib/assert.d.ts +0 -1
- package/dist/lib/assert.js +0 -11
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/
|
|
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
|
|
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`** -
|
|
81
|
-
`
|
|
82
|
-
-
|
|
83
|
-
|
|
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,
|
|
86
|
-
|
|
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
|
-
-
|
|
89
|
-
|
|
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
|
|
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
|
|
187
|
-
if (safeResult.data
|
|
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
|
|
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:
|
|
205
|
-
if (validatedResponse
|
|
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
|