@rexeus/typeweaver-gen 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 +58 -29
- package/dist/index.cjs +409 -153
- package/dist/index.d.cts +196 -149
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +196 -149
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +386 -139
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -4
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
|
|
41
|
+
for (const resource of context.normalizedSpec.resources) {
|
|
42
42
|
const content = context.renderTemplate("Entity.ejs", {
|
|
43
|
-
|
|
44
|
-
operations,
|
|
43
|
+
resource,
|
|
44
|
+
operations: resource.operations,
|
|
45
45
|
coreDir: context.coreDir,
|
|
46
46
|
});
|
|
47
|
-
context.writeFile(`${
|
|
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: `
|
|
64
|
-
|
|
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 |
|
|
75
|
-
collectResources?(
|
|
76
|
-
|
|
77
|
-
):
|
|
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`
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
|
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.
|