@counterfact/generator 1.0.0 → 1.2.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/CHANGELOG.md +42 -0
- package/dist/central-types.d.ts +14 -0
- package/dist/central-types.js +128 -0
- package/dist/code-generator.js +28 -15
- package/dist/multipart-schema-type-coder.d.ts +10 -0
- package/dist/multipart-schema-type-coder.js +26 -0
- package/dist/operation-coder.js +10 -2
- package/dist/operation-type-coder.js +32 -6
- package/dist/prune.js +1 -0
- package/dist/repository.d.ts +2 -2
- package/dist/repository.js +99 -23
- package/dist/route-type-tree.d.ts +6 -0
- package/dist/route-type-tree.js +37 -0
- package/dist/scenario-file-generator.d.ts +5 -0
- package/dist/scenario-file-generator.js +112 -15
- package/dist/schema-type-coder.d.ts +2 -1
- package/dist/schema-type-coder.js +11 -12
- package/dist/script.d.ts +7 -2
- package/dist/script.js +44 -11
- package/dist/templates/counterfact-types/index.ts +1 -0
- package/dist/templates/counterfact-types/multipart-file.ts +7 -0
- package/dist/versions-ts-generator.js +27 -9
- package/package.json +5 -5
- package/src/README.md +38 -12
package/src/README.md
CHANGED
|
@@ -10,6 +10,25 @@ Fortunately, we _do_ have pretty much all the information we need to generate _t
|
|
|
10
10
|
|
|
11
11
|
We can also use those types on the _client_ side, assuming the client is written in TypeScript.
|
|
12
12
|
|
|
13
|
+
## Public generated types
|
|
14
|
+
|
|
15
|
+
Generated projects expose `Routes`, `Components`, and `Schemas` through
|
|
16
|
+
`types/index.ts`. `Routes` is a tree of slash-prefixed segments: an operation on
|
|
17
|
+
`/hello/world` uses `Routes["/hello"]["/world"]["GET"]`. Parent operations and
|
|
18
|
+
child paths can share one branch. Routes import `Routes` from that entry point and use
|
|
19
|
+
`Routes["/hello-world"]["GET"]`; model consumers use `Schemas["Message"]`
|
|
20
|
+
or `Components["schemas"]["Message"]`. Direct imports of operation and
|
|
21
|
+
component types are deprecated for consumers.
|
|
22
|
+
|
|
23
|
+
The individual operation and component modules remain generator implementation
|
|
24
|
+
and compatibility surfaces. Internal coders continue importing dependencies
|
|
25
|
+
directly. `central-types.ts` registers operations and all declared components,
|
|
26
|
+
then emits the central maps after the repository has resolved their types.
|
|
27
|
+
The central entry point also re-exports `Context$`, `Scenario`, `Scenario$`, and
|
|
28
|
+
the directory-indexed `Middleware` map from `types/helpers.ts`. Helper generation
|
|
29
|
+
and context watching own that module; OpenAPI generation owns the API maps.
|
|
30
|
+
Both writers serialize helper updates without rewriting each other's exports.
|
|
31
|
+
|
|
13
32
|
## Files
|
|
14
33
|
|
|
15
34
|
| File | Description |
|
|
@@ -21,6 +40,7 @@ We can also use those types on the _client_ side, assuming the client is written
|
|
|
21
40
|
| `script.ts` | `Script` class: manages code generation for a single output file — imports, exports, deduplication, and Prettier formatting |
|
|
22
41
|
| `coder.ts` | Abstract `Coder` base class: defines the Template Method pattern used by all code-generating components |
|
|
23
42
|
| `type-coder.ts` | Abstract `TypeCoder` base class (extends `Coder`): specialises `Coder` for type-generating components |
|
|
43
|
+
| `central-types.ts` | Assembles the public `Routes`, `Components`, and `Schemas` maps, preserving logical paths and combining component versions |
|
|
24
44
|
| `operation-coder.ts` | `OperationCoder`: generates the route handler function for an OpenAPI operation |
|
|
25
45
|
| `operation-type-coder.ts` | `OperationTypeCoder`: generates the TypeScript type for a route handler, including parameters and response builder |
|
|
26
46
|
| `parameters-type-coder.ts` | `ParametersTypeCoder`: generates the typed object for path/query/header parameters of an operation |
|
|
@@ -29,7 +49,7 @@ We can also use those types on the _client_ side, assuming the client is written
|
|
|
29
49
|
| `response-type-coder.ts` | `ResponseTypeCoder`: generates the type for a single HTTP response, including headers, content, and named examples |
|
|
30
50
|
| `schema-type-coder.ts` | `SchemaTypeCoder`: converts an OpenAPI schema definition to a TypeScript type (objects, arrays, unions, enums) |
|
|
31
51
|
| `schema-coder.ts` | `SchemaCoder`: generates a JSON Schema representation of an OpenAPI schema for use in runtime validation |
|
|
32
|
-
| `ast.ts` | TypeScript AST construction and printing helpers
|
|
52
|
+
| `ast.ts` | TypeScript AST construction and printing helpers |
|
|
33
53
|
| `read-only-comments.ts` | Standard warning comments inserted into generated type files to discourage manual edits |
|
|
34
54
|
|
|
35
55
|
## Architecture
|
|
@@ -136,14 +156,20 @@ paths:
|
|
|
136
156
|
type: string
|
|
137
157
|
```
|
|
138
158
|
|
|
139
|
-
Our goal is to produce
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
159
|
+
Our goal is to produce `routes/hello-world.ts`, exporting a `GET` handler typed
|
|
160
|
+
as `Routes["/hello-world"]["GET"]`, and a reusable model available as
|
|
161
|
+
`Schemas["Message"]`.
|
|
162
|
+
|
|
163
|
+
1. `CodeGenerator` registers every declared component with the central index,
|
|
164
|
+
including unused schemas. It traverses path operations and exports each
|
|
165
|
+
`OperationCoder` to its route script.
|
|
166
|
+
2. `OperationCoder.typeDeclaration()` registers its `OperationTypeCoder` with
|
|
167
|
+
the central index and imports `Routes` into the route. The index imports the
|
|
168
|
+
handler contract from its existing operation module. Versioned operations
|
|
169
|
+
still register each version's argument type on the shared operation script.
|
|
170
|
+
3. The operation contract uses other coders for parameters, responses, and
|
|
171
|
+
schemas. `Script.importType()` resolves their exports through `Repository`,
|
|
172
|
+
keeping those internal imports direct and collision-safe.
|
|
173
|
+
4. After all types resolve, the central script emits `Routes`, `Components`,
|
|
174
|
+
and `Schemas = Components["schemas"]` through a deferred statement builder.
|
|
175
|
+
`Repository` writes regenerated types and preserves user-edited route text.
|