@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/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 a file at `/routes/hello.ts` that exports a function named `GET` (1). Our implementation will depend on a type for the `GET` function which lives in `/routes/types-hello.ts` (2). That type, in turn, will depend on a type named `Message` which lives in `/components/message.ts` (3).
140
-
141
- (1) We kick off the process by iterating over the paths. (In our simple example there's only one path, at `/hello`.) In our model, each path is represented as a `Requirement`. We give each path / `Requirement` to an `OperationCoder` who will write the code. We ask the repository for a `Script` and then hand the `Script` our `OperationCoder` instance and ask it to create an export.
142
-
143
- (2a) The `OperationCoder` knows that the function has to have a type, but it doesn't know to create that type. So it recruits an `OperationTypeCoder` and hands it the `Requirement`. It then asks the `Script` to _import_ a type, passing along the `OperationTypeCoder`. The `Script` returns the name of the variable to which the imported type will be assigned. That name is all the `OperationCoder` needs to know to continue writing its portion of the code. The `OperationCoder` continues on, writing the `GET` function. Depending on what it finds in the `Requirement`, it will break off parts and delegate some of the work to other `Coder`s.
144
-
145
- (2b) The `Script` has promised that it will import the type from `/types/paths/hello.type.ts` so it needs to make sure that file exists and has a matching export. It goes to the repository to get another `Script` and asks it to _export_ the type, passing along the `OperationTypeCoder` in the process. It's a little tricky here because `OperationTypeCoder`'s `Requirement` may not have the information it needs to proceed. It might have a `$ref` pointer to some _other_ requirement that might be in a different file. So before asking for the export, it asks the `OperationTypeCoder` to give it _another_ `OperationTypeCoder` that definitely has the requirement. Because the other requirement may be in another file that the `Repository` hasn't loaded yet, this part happens asynchronously.
146
-
147
- (2c) When it's ready, the `Script` asks the `OperationTypeCoder` which definitely has an immediately usable requirement to write the export for the `Script` at `/types/paths/hello.types.ts`.
148
-
149
- (3) The `OperationTypeCoder` needs the help of a `SchemaCoder` so it asks the `Script` at `/types/paths/hello.types.ts` for an export...
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.