Write and edit Skmtc generators: packages that turn an OpenAPI document into application code as a tree of producer classes. Use before you write or change any generator source: a new generator, a cloned one, a schema type or an operation kind it does not render yet, names and export paths, enrichments, another generator's output, or an edge case it renders wrongly.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Skmtc Generator?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/skmtc-skmtc-generator-skmtc)
---
name: skmtc-generator
description: "Write and edit Skmtc generators: packages that turn an OpenAPI document into application code as a tree of producer classes. Use before you write or change any generator source: a new generator, a cloned one, a schema type or an operation kind it does not render yet, names and export paths, enrichments, another generator's output, or an edge case it renders wrongly."
---
# Write a Skmtc generator
Skmtc derives application code from an OpenAPI document. A **generator**
is a small package that the engine runs over every **model** (a schema,
by its `ref`) and every **operation** (`path` + `method`) of the
document. The engine writes every generated file again on each run, so
you change the generator and never its output.
## Entry, base, projection
- The **entry** is the object that represents your generator to core. It
holds what core must know about the generator as a whole: its id, the
subjects it takes (`isSupported`), its enrichment schema, and
`transform`. A generator has one.
- A **projection base** is the foundation of projections in your
generator. It holds what they share: how to name a definition, its
kind, its file, and the types of their enrichments and options. A
generator can have several, and several projections can extend one.
- A **projection** builds one definition for a subject.
**One generator, one kind of subject.** The entry, the bases and the
projections of a generator are all for operations, or all for models, or
all for webhooks. Core refuses a projection of another kind. When an
operation generator needs models, insert them from a model generator,
such as `@skmtc/gen-zod`.
To place something, ask what it describes. The whole generator: the
entry. The name, kind or file of definitions: the base. The code of one
definition: the projection. The class name does not tell you which you
have: `TopEntry` in `@skmtc/gen-md-docs` is a projection.
## A projection is a tree of producers
A **producer** is a class with a constructor and a `toString()`.
- The constructor takes one part of the document and stores what that
part is made of as fields: other producers, and plain facts such as a
name, a method or a flag.
- `toString()` outputs those fields as target code. It reads its own
fields and nothing else.
A **projection** is a producer that owns one named definition in a file.
A **snippet** is a producer for a part inside a definition. Your code
builds producers while the engine generates. The engine calls
`toString()` once for each file, at the end.
`@skmtc/gen-effect-http` writes this for `POST /search`:
```ts
export const createApiSearch = (body: typeof SearchRequest.Type) =>
Effect.gen(function* () {
const client = yield* HttpClient.HttpClient;
const request = yield* HttpClientRequest.post("/search").pipe(
HttpClientRequest.schemaBodyJson(SearchRequest)(body),
);
const response = yield* client.execute(request);
return yield* HttpClientResponse.schemaBodyJson(CreateApiSearchResponse)(response);
});
```
These producers build it:
```
EffectEndpoint the function projection
├─ parameters List<TypedParameter> (body: typeof … .Type)
├─ request EffectRequest const request = … ;
│ └─ steps List<UrlParamsStep | HeadersStep | BodyStep>
│ one line inside .pipe( … )
└─ response ResponseRead | ResponseVoid
the last two lines
```
```ts
// EffectEndpoint
override toString(): string {
return `${this.parameters} =>
Effect.gen(function* () {
const client = yield* HttpClient.HttpClient;
${this.request}
${this.response}
})`
}
```
Each part that a reader of the output can name is a class. Each field
is a thing in the output: `parameters`, `request`, `response`. A new
kind of step is a new class and one line in `EffectRequest`. No function
in the package returns code, and the mean complexity of its functions is
1.5 ([`scripts/complexity.ts`](https://github.com/skmtc/skmtc/tree/main/deno/docs/skills/skmtc-generator/scripts/complexity.ts) in this skill's source measures it).
## Why producers
Code that builds output from functions and joined strings is correct for
the common cases and wrong for the rest. Text has no imports, the engine
cannot reuse it, and another generator cannot refer to it. A function
that inspects or rewrites a schema does the parser's work a second time,
and takes one more branch for each case it meets.
A producer handles its case once. Each place that has the part gets it,
with its imports. A new case is a new class and one case in the router.
The classes that exist do not change.
Each person who meets the generator gets something from the tree:
| Who | What the tree gives |
|---|---|
| You, the author | Each case has one place. The template in `toString()` has the shape of the output, so a wrong line in a file leads to one class |
| A person who clones your generator | Generator source is the customization surface. A clone changes one class, or one static in `base.ts`, and the other classes stay as they are |
| The author of another generator | A definition to insert by its name, with its import |
| The consumer of the output | Files with the imports that they use, and one definition for each model |
## What Skmtc already does
Most of a generator's hard problems are solved before your code runs.
**Before you write a function that reads the document, find its job in
this table.** If the job is here, use what does it.
| Job | Use | What it gives you |
|---|---|---|
| Merge an `allOf`, read `oneOf` and `anyOf` as a union, resolve each `$ref`, remove what a broken `$ref` holds | The parser. A `settings.parse` key (`allOf`, `anyOf`, `intersections`, `danglingRefs`) changes how it reads | A schema you render as it is, with no code that inspects, repairs, merges or compares schemas |
| Choose the code for a schema type | Your **router**: one `switch (schema.type)` with a `never` default, which returns a producer | Each class reads typed fields from a narrowed schema. A new type is a compile error until it has a case and a class |
| Build a model once, wherever it is used | `insertModel`, `toModelRef` | One definition and one import for each model. You hold its name |
| Give an inline schema (a body, a parameters object, a response) a definition | `insertNormalizedModel` with a `fallbackName` | The model generator renders it. You hold its name |
| Find a cycle, and the reference that must be lazy | `toModelRef` (`suspended`, `inCycle`) and `isReferencedBack` | No walk of the document and no set of visited schemas |
| Write the imports of a file | `this.register({ imports })` for a library. Nothing for an insert | Imports that match what the file uses |
| Know a name or a path before anything is built | The base's statics, `toIdentifierName` and `toExportPath`, in `base.ts`. Other files read `settings.identifier` and `settings.exportPath` | One place to change a name. Other generators find your definition |
| Read an operation's parameters, body and response | `toParams`, `toParametersObject`, `toRequestBody`, `toSuccessResponse` on the operation | The path item's parameters merged in, the JSON media type chosen, a `$ref` resolved |
| Join a list and leave out absent items | `List`, built in the constructor | Separators and brackets in one place |
| Put a string from the document into code | `handleKey`, `sanitizeIdentifier`, `toStringLiteral`, `toPathTemplate` (`references/typescript.md`) | Correct quotes and escapes for any character |
| Make more than one definition from one subject | Variants. Pass `variant` on | Names and files that stay apart |
| Take a setting from the consumer | Enrichments, declared as a schema on the entry | Core parses the values. A projection reads the typed result from `this.settings.enrichments`. A wrong value fails the run and names its place in `skmtc.config.json` |
| Resolve two definitions that claim one name | The engine. The second model or operation of a name gets a counter (`AuditLog2`) and a `DUPLICATE_NAME` warning (`settings.generate.deduplicate`). An inline definition that asks for that name gets a counter too | Nothing to write. Ask `context.claimInlineName` for the name of an inline definition that you name yourself |
| Give a definition with a counter the file of its name | The engine: `toExportPath` gets the argument `name` | Build the path from `name` where a file must have the name of its declaration |
| Resolve two export paths that differ only in case | The engine: both definitions go into the file of the first path (`settings.generate.fileCase`) | Nothing to write |
| Report a fault in the document | The parser: issues in the manifest | Nothing to write |
Four jobs are yours, and each has one place:
| Your decision | Its one place |
|---|---|
| Which producer renders a part that has kinds (a response with a body, a response with none) | The constructor of the producer that holds the part |
| A name you derive from the identifier (an alias, a suffix) | A function in `base.ts`, beside the statics |
| Optional and nullable | The modifiers, applied once where the value renders |
| A fact that several producers read | One plain class, named after what it reads from the document |
## Read first
1. **Your twin.** A stock generator already does the nearest job. Open
it before you write a line, and copy its shape, names and order.
| You write | Start from |
|---|---|
| A model generator: each schema becomes a validator, a schema value or a type | [`skeleton/`](https://github.com/skmtc/skmtc/tree/main/deno/docs/skills/skmtc-generator/skeleton) in this skill's source. Copy it and fill its slots |
| An operation generator that builds a function: a client call, a handler | `@skmtc/gen-effect-http`: `EffectEndpoint.ts` and the nine classes beside it |
| An operation generator for a hook over a model generator | `@skmtc/gen-tanstack-query-fetch-zod` |
| A form or other UI for each operation | `@skmtc/gen-shadcn-form` |
| A member for each operation in one class or interface (an Index) | `@skmtc/gen-md-docs`: `TopContents.ts`, `TagGroup.ts`, `TagContents.ts` and `ContentsEntry.ts` (`references/engine.md`, "Shapes of generator") |
| Many operations in one file (an accumulator) | `@skmtc/gen-msw`, `@skmtc/gen-express` |
| A class for one schema type | The class for that type in [`skeleton/src/`](https://github.com/skmtc/skmtc/tree/main/deno/docs/skills/skmtc-generator/skeleton/src) or `@skmtc/gen-effect-schema` |
Get the source with `skmtc clone <project> -g @skmtc/<name>`, or read
it at `https://jsr.io/@skmtc/<name>`.
2. **The reference for your path** (next section).
3. **Signatures.** Skmtc has almost no presence in training data, so
read a signature and never recall one:
`deno doc jsr:@skmtc/core@<pinned version> <Symbol>`. Read it in
place of a cast around a type error too.
## Pick your path
1. **A new model generator.** Read
[references/model.md](references/model.md).
2. **A new operation generator.** Read
[references/operation.md](references/operation.md).
3. **A change to a generator that exists**, yours or a clone. Find the
job in the tables above, open the file that holds it, and read the
reference for that generator's shape.
Every path uses these when the work reaches them:
- [references/producers.md](references/producers.md): how to split
output into producers, with the wrong form and the right form of each
rule. Read it before you write the first class.
- [references/engine.md](references/engine.md): read it before you use
another generator's output, add an enrichment or an option, pass a
variant on, or write many subjects into one file.
- [references/typescript.md](references/typescript.md): read it before
you register an import, choose an identifier kind, or put a string
from the document into code.
Every path ends with [references/finish.md](references/finish.md).
## Steps
1. **Write the output by hand** for two subjects that differ: an
operation with a body and one without, or an object and a union.
Done when each file would compile in the target project.
2. **Name the parts.** Mark each part of that output that a reader can
name. List one producer for each, with its fields. Done when every
line of the hand-written output belongs to one producer in the list,
and every field names a thing in the output.
3. **Give each need its owner.** List each thing in that output that is
not fixed text: a name, an import, another schema's code, a list, a
lazy reference, a value from the consumer. Beside each, write the row
of "What Skmtc already does" that supplies it. Done when every item
has a row, or is one of your producers.
4. **Write identity first**: `base.ts`, with the name and the export
path of each definition. Done when no other file builds a name or a
path.
5. **Build the producers from the leaves up.** Run the generator after
each one. Read the import header of an output file first, then the
body. Done when the output matches what you wrote in step 1.
6. **Finish** with [references/finish.md](references/finish.md).
## When a case does not fit
An edge case shows that a decision has no place yet. Give it its place:
1. **A part renders wrongly for some subjects.** The part needs its own
producer, or a second producer for the other kind. Add the class, and
choose between the two in one place.
2. **A schema type or a keyword has no rendering.** Add a case to the
router and a class for it.
3. **A name is wrong or clashes.** Change the static in `base.ts`.
4. **You need the code or the name of another schema.** Insert it.
5. **The document is ambiguous or broken.** Render what the parser
gives. If your target has no form for it, throw from the router case
and name the schema's pointer and the `settings.parse` key that
changes the parse. Code that detects, repairs, merges or compares
schemas is the parser's.
6. **None of these fits.** The engine lacks something. Stop, and report
the case and what you need from the engine.
Three signs that you have left this path: a function that takes a schema
and returns a schema or a yes or no about it; the same condition in two
places; a second function written to correct the first.
## Jobs that are not the generator's
A generator turns the document into code. Some checks and repairs look
useful, but another tool already does them. Before you add one, ask:
**what happens without it?**
- If another tool reports the fault clearly, leave it to that tool.
- If the output is wrong and no tool reports it, the job is yours.
| It looks useful to | Whose job it is | What you do |
|---|---|---|
| Check the output for a fault that the compiler of the target language reports: a name that two files of a Go package declare, a type that does not exist | The compiler | Nothing. Add a row to the troubleshooting table of the language skill |
| Repair, merge or compare schemas | The parser | Render what the parser gives. A `settings.parse` key changes the parse |
| Report a fault in the document | The parser, as issues in the manifest | Nothing |
| Format the output | The formatter of the project, after generation | Write correct code. Do not align or wrap it |
| Generate code that is the same for every document | A hand-written file in the project | Write the file once, by hand |
A check is yours when the target language is silent. Python keeps the
last of two bindings of one name and reports nothing, so the Python
layer stops the run for a name that a file binds twice. Go reports a
name that is declared twice in a package, so the Go layer does not look
for it.
If two names clash often, the fault is in the name. Change the name in
`base.ts`. Do not add a check.
## Rules for every path
- **Two methods.** A producer has a constructor and `toString()`. The
one exception is the method that adds to an accumulator
(`references/engine.md`).
- **A field is a part or a fact.** `summary`, `options` and `action`
are parts. `chain`, `parts` and `lines` hold everything and name
nothing.
- **Target code is written in `toString()`**, as one template over the
fields, in the shape of the output.
- **`List` is for a real list**: items of one kind, in a number the
document sets. A fixed set of different parts is a template over named
fields.
- **Only `undefined` and `null` are absent.** `0`, `false`, `''` and
`[]` are values, and they render.
- **Name an operation from its method and path**, with core's
`toEndpointName`. `operationId` is the document author's and changes
without notice.
- **Read the document through its methods**: `isRef()`, `resolve()`,
`toParams()`, `toRequestBody()`. Do the same thing the same way as
the code around you.
- **A function outside a class needs a reason a class cannot give**:
the router, the config of the entry and of the base, an identity
function in `base.ts`, a type guard on raw input.
- **Complexity of 10 or less for each function.** The router is exempt.
A constructor above 10 holds two concepts.
- **Generate only what the document changes.** Code that is the same
for every document is a hand-written file in the project.
- **Render does not run Prettier or Biome.** No formatter runs in the
engine. Read the raw output, and format after generation.
## When you're done
1. **Generate from a real document**, not only from fixtures. Read the
manifest: the definitions you expect, at the paths you expect, with
no errors.
2. **Lint.** `deno lint`, with `jsr:@skmtc/lint-plugin` in the
generator's `deno.json`. No findings. From plugin 0.1.3 the rule
`skmtc/complexity` reports a function over the limit.
3. **Measure.** The `skmtc/complexity` rule of step 2 reports no
function over the limit.
4. **Test.** Run the generator's tests and type-check the generated
output in the target project.
5. **Do the finish pass** in
[references/finish.md](references/finish.md) on your diff.
6. **Report** each command with its exit code, what you could not
check, each function you wrote for a job that the table gives to
Skmtc, each thing the engine lacked, and each place where this skill
or the docs were wrong or silent. `skmtc feedback` sends the last two
to the Skmtc team as a public issue, so put no tokens or personal
data in it.