@rexeus/typeweaver-gen 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 CHANGED
@@ -38,13 +38,13 @@ export default class MyPlugin extends BasePlugin {
38
38
 
39
39
  // Use the generate phase to render templates and write files
40
40
  async generate(context: GeneratorContext) {
41
- for (const [entity, { operations }] of Object.entries(context.resources.entityResources)) {
41
+ for (const resource of context.normalizedSpec.resources) {
42
42
  const content = context.renderTemplate("Entity.ejs", {
43
- entity,
44
- operations,
43
+ resource,
44
+ operations: resource.operations,
45
45
  coreDir: context.coreDir,
46
46
  });
47
- context.writeFile(`${entity}/${entity}Stuff.ts`, content);
47
+ context.writeFile(`${resource.name}/${resource.name}Stuff.ts`, content);
48
48
  }
49
49
  }
50
50
  }
@@ -60,8 +60,9 @@ Templates live under your plugin’s `src/templates`. They receive your data obj
60
60
  `renderTemplate`, and file tracking.
61
61
  - Registry: `PluginRegistry` to register and query plugins; the CLI orchestrates lifecycle
62
62
  execution.
63
- - Resource model: `GetResourcesResult`, `EntityResources`, representing the normalized API data
64
- derived from your definition.
63
+ - Resource model: `NormalizedSpec`, `NormalizedResource`, `NormalizedOperation`,
64
+ `NormalizedResponse`, and `NormalizedResponseUsage`, representing the normalized API data derived
65
+ from your spec entrypoint.
65
66
 
66
67
  ## πŸ”Œ Plugin lifecycle
67
68
 
@@ -71,12 +72,10 @@ what you need.
71
72
  ```ts
72
73
  type TypeweaverPlugin = {
73
74
  name: string;
74
- initialize?(context: PluginContext): void | Promise<void>;
75
- collectResources?(
76
- resources: GetResourcesResult
77
- ): GetResourcesResult | Promise<GetResourcesResult>;
78
- generate?(context: GeneratorContext): void | Promise<void>;
79
- finalize?(context: PluginContext): void | Promise<void>;
75
+ initialize?(context: PluginContext): Promise<void> | void;
76
+ collectResources?(normalizedSpec: NormalizedSpec): Promise<NormalizedSpec> | NormalizedSpec;
77
+ generate?(context: GeneratorContext): Promise<void> | void;
78
+ finalize?(context: PluginContext): Promise<void> | void;
80
79
  };
81
80
  ```
82
81
 
@@ -91,24 +90,54 @@ type TypeweaverPlugin = {
91
90
 
92
91
  ## 🧰 Generator context
93
92
 
94
- The `GeneratorContext` describes the generation phase: it provides access to resolved paths,
95
- configuration, the normalized resources, and helper methods for safe file emission and templating.
96
- You receive it only inside the `generate` lifecycle method.
93
+ The `GeneratorContext` is passed to your plugin's `generate` method. It gives you everything you
94
+ need to emit files: the normalized spec, resolved paths, and helpers for imports, templates, and
95
+ file tracking.
96
+
97
+ ### Properties
98
+
99
+ | Property | Description |
100
+ | -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
101
+ | `normalizedSpec` | The fully validated and normalized API spec β€” resources, operations, and responses ready for generation. |
102
+ | `templateDir` | Absolute path to your plugin's compiled EJS templates (`dist/templates/`). Pass template names relative to this directory to `renderTemplate`. |
103
+ | `coreDir` | The import specifier for `@rexeus/typeweaver-core` β€” use in templates so generated code imports core types correctly. |
104
+ | `responsesOutputDir` | Absolute path to the shared `responses/` output directory where canonical response files are written. |
105
+ | `specOutputDir` | Absolute path to the `spec/` output directory containing the bundled spec and shim files. |
106
+
107
+ ### Methods
108
+
109
+ | Method | Description |
110
+ | --------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |
111
+ | `getCanonicalResponse(name)` | Look up a canonical (shared) response by name. Throws if the response does not exist. |
112
+ | `getCanonicalResponseOutputFile(name)` | Get the absolute output file path for a canonical response. |
113
+ | `getCanonicalResponseImportPath({ importerDir, responseName })` | Compute the relative import path from a generated file to a canonical response file. |
114
+ | `getSpecImportPath({ importerDir })` | Compute the relative import path from a generated `spec.ts` consumer to the bundled spec shim. |
115
+ | `getOperationOutputPaths({ resourceName, operationId })` | Get all output file paths for a given operation (request, response, validators, etc.). |
116
+ | `getResourceOutputDir(name)` | Get the absolute output directory for a resource (e.g. `<output>/todo/`). |
117
+ | `writeFile(rel, content)` | Write a file relative to the output root. Creates directories as needed and tracks the file for cleanup. |
118
+ | `renderTemplate(tplPath, data)` | Render an EJS template from `templateDir` with the given data object. Returns the rendered string. |
119
+ | `addGeneratedFile(rel)` | Mark a file as generated (for tracking) without writing it. Useful when another tool produces the file. |
120
+ | `getGeneratedFiles()` | List all file paths tracked during this generation run. |
121
+
122
+ ### Example: using the context in a plugin
97
123
 
98
124
  ```ts
99
- type GeneratorContext = {
100
- inputDir: string;
101
- outputDir: string;
102
- templateDir: string;
103
- coreDir: string;
104
- config: PluginConfig;
105
- resources: GetResourcesResult;
106
- writeFile(rel: string, content: string): void; // mkdir -p + write + track
107
- // Tracked files are automatically exported via a generated barrel index.ts
108
- renderTemplate(tplPath: string, data: unknown): string; // EJS render
109
- addGeneratedFile(rel: string): void; // track only
110
- getGeneratedFiles(): string[]; // list tracked files
111
- };
125
+ async generate(context: GeneratorContext) {
126
+ for (const resource of context.normalizedSpec.resources) {
127
+ const outputDir = context.getResourceOutputDir(resource.name);
128
+
129
+ for (const operation of resource.operations) {
130
+ // Render a template with operation data
131
+ const content = context.renderTemplate("MyTemplate.ejs", {
132
+ coreDir: context.coreDir,
133
+ operation,
134
+ });
135
+
136
+ // Write the result β€” directories are created automatically
137
+ context.writeFile(`${resource.name}/${operation.operationId}Custom.ts`, content);
138
+ }
139
+ }
140
+ }
112
141
  ```
113
142
 
114
143
  ### πŸ“¦ Shipping runtime helpers
@@ -141,7 +170,7 @@ output.
141
170
 
142
171
  ## πŸ“Œ Notes
143
172
 
144
- - Plugins are configured/executed by the CLI (`@rexeus/typeweaver`). See the CLI options
173
+ - Plugins are configured and executed by the CLI (`@rexeus/typeweaver`). See the CLI options
145
174
  [here](https://github.com/rexeus/typeweaver/tree/main/packages/cli/README.md#️-options).
146
175
  - Keep plugins focused: one concern per plugin (clients, routers, infra).
147
176
  - Prefer `GeneratorContext.writeFile` over manual fs writes for tracking and directory setup.