@counterfact/generator 0.1.5 → 1.0.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 +40 -0
- package/dist/ast.d.ts +23 -0
- package/dist/ast.js +142 -0
- package/dist/coder.d.ts +16 -22
- package/dist/coder.js +15 -17
- package/dist/operation-coder.d.ts +5 -3
- package/dist/operation-coder.js +40 -25
- package/dist/operation-type-coder.d.ts +17 -25
- package/dist/operation-type-coder.js +115 -129
- package/dist/operation-type-name-mapping.d.ts +3 -2
- package/dist/operation-type-name-mapping.js +22 -10
- package/dist/parameter-export-type-coder.d.ts +4 -3
- package/dist/parameters-type-coder.d.ts +2 -1
- package/dist/parameters-type-coder.js +7 -18
- package/dist/repository.d.ts +3 -10
- package/dist/repository.js +244 -95
- package/dist/response-type-coder.d.ts +7 -7
- package/dist/response-type-coder.js +37 -53
- package/dist/responses-type-coder.d.ts +4 -4
- package/dist/responses-type-coder.js +39 -26
- package/dist/sanitize-identifier.d.ts +1 -0
- package/dist/sanitize-identifier.js +17 -0
- package/dist/scenario-file-generator.js +137 -134
- package/dist/schema-coder.d.ts +6 -4
- package/dist/schema-coder.js +31 -22
- package/dist/schema-required-properties.d.ts +3 -0
- package/dist/schema-required-properties.js +47 -0
- package/dist/schema-type-coder.d.ts +10 -8
- package/dist/schema-type-coder.js +157 -64
- package/dist/script.d.ts +18 -95
- package/dist/script.js +137 -212
- package/dist/templates/counterfact-types/middleware.ts +15 -2
- package/dist/templates/counterfact-types/response-builder-factory.ts +102 -6
- package/dist/templates/counterfact-types/response-builder.ts +1 -0
- package/dist/templates/counterfact-types/wide-operation-argument.ts +13 -1
- package/dist/type-coder.d.ts +3 -1
- package/dist/type-coder.js +5 -1
- package/dist/versions-ts-generator.d.ts +0 -9
- package/dist/versions-ts-generator.js +46 -77
- package/package.json +5 -4
- package/src/README.md +23 -2
- package/dist/context-file-token.d.ts +0 -1
- package/dist/context-file-token.js +0 -1
- package/dist/printers.d.ts +0 -2
- package/dist/printers.js +0 -10
package/dist/type-coder.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
+
import { typeReference } from "./ast.js";
|
|
1
2
|
import { Coder } from "./coder.js";
|
|
2
3
|
export class TypeCoder extends Coder {
|
|
3
4
|
write(script) {
|
|
4
5
|
if (this.requirement?.isReference) {
|
|
5
|
-
return script.importType(this);
|
|
6
|
+
return typeReference(script.importType(this));
|
|
6
7
|
}
|
|
7
8
|
return this.writeCode(script);
|
|
8
9
|
}
|
|
10
|
+
writeCode(_script) {
|
|
11
|
+
throw new Error("TypeCoder.writeCode() must be implemented by a subclass");
|
|
12
|
+
}
|
|
9
13
|
}
|
|
@@ -1,10 +1 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generates the TypeScript source text for `types/versions.ts`.
|
|
3
|
-
*
|
|
4
|
-
* Returns an empty string when `versions` is empty.
|
|
5
|
-
* The returned string is formatted with Prettier.
|
|
6
|
-
*
|
|
7
|
-
* @param versions - Ordered list of unique, non-empty version strings.
|
|
8
|
-
* The first entry is the oldest version.
|
|
9
|
-
*/
|
|
10
1
|
export declare function generateVersionsTsContent(versions: readonly string[]): Promise<string>;
|
|
@@ -1,82 +1,51 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
* Builds the `VersionsGTE` map: for each version V at index i,
|
|
4
|
-
* maps V to all versions at indices >= i (i.e. V and all later-declared
|
|
5
|
-
* versions, where "later" means "newer").
|
|
6
|
-
*/
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { formatStatements, literalType, parameter, typeAlias, typeLiteral, typeProperty, typeReference, unionType, withJsDoc, } from "./ast.js";
|
|
7
3
|
function buildVersionsGTE(versions) {
|
|
8
|
-
|
|
9
|
-
versions.forEach((version, index) => {
|
|
10
|
-
result.set(version, versions.slice(index));
|
|
11
|
-
});
|
|
12
|
-
return result;
|
|
4
|
+
return new Map(versions.map((version, index) => [version, versions.slice(index)]));
|
|
13
5
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
6
|
+
const VERSIONED_DESCRIPTION = `The type of the \u0060$\u0060 argument in a versioned route handler.
|
|
7
|
+
|
|
8
|
+
@typeParam T - A map from version string to the \u0060$\u0060-arg type for that version
|
|
9
|
+
(e.g. \u0060{ v1: $v1Type; v2: $v2Type }\u0060). Generated by Counterfact from the spec.
|
|
10
|
+
@typeParam V - The union of currently active version keys; defaults to all keys of \u0060T\u0060.
|
|
11
|
+
|
|
12
|
+
An instance of \u0060Versioned<T, V>\u0060 exposes:
|
|
13
|
+
- All properties of the intersection \u0060T[V]\u0060 (request data, response builders, etc.)
|
|
14
|
+
- \u0060version\u0060 — the version string for the current request (e.g. \u0060"v2"\u0060).
|
|
15
|
+
- \u0060minVersion(min)\u0060 — type predicate that returns \u0060true\u0060 when the current version
|
|
16
|
+
is at or after \u0060min\u0060 in the declared version order and narrows \u0060$\u0060 accordingly.`;
|
|
23
17
|
export async function generateVersionsTsContent(versions) {
|
|
24
|
-
if (versions.length === 0)
|
|
18
|
+
if (versions.length === 0)
|
|
25
19
|
return "";
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
"",
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
"
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
"",
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
"
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
" * is at or after `min` in the declared version order and narrows `$` accordingly.",
|
|
58
|
-
" *",
|
|
59
|
-
" * @example",
|
|
60
|
-
" * ```ts",
|
|
61
|
-
" * export const GET: HTTP_GET = ($) => {",
|
|
62
|
-
' * if ($.minVersion("v2")) {',
|
|
63
|
-
" * // $ is now typed as the v2+ arg — v2-only fields are available",
|
|
64
|
-
" * return $.response[200].json({ id: $.path.id, extra: $.body.extra });",
|
|
65
|
-
" * }",
|
|
66
|
-
" * return $.response[200].json({ id: $.path.id });",
|
|
67
|
-
" * };",
|
|
68
|
-
" * ```",
|
|
69
|
-
" */",
|
|
70
|
-
"export type Versioned<",
|
|
71
|
-
" T extends VersionMap,",
|
|
72
|
-
" V extends keyof T & Versions = keyof T & Versions,",
|
|
73
|
-
"> = T[V] & {",
|
|
74
|
-
" version: V;",
|
|
75
|
-
" minVersion<M extends keyof T & Versions>(",
|
|
76
|
-
" min: M,",
|
|
77
|
-
" ): this is Versioned<T, Extract<V, VersionsGTE[M]>>;",
|
|
78
|
-
"};",
|
|
79
|
-
"",
|
|
80
|
-
].join("\n");
|
|
81
|
-
return format(source, { parser: "typescript" });
|
|
20
|
+
const activeKeys = () => ts.factory.createIntersectionTypeNode([
|
|
21
|
+
ts.factory.createTypeOperatorNode(ts.SyntaxKind.KeyOfKeyword, typeReference("T")),
|
|
22
|
+
typeReference("Versions"),
|
|
23
|
+
]);
|
|
24
|
+
const activeVersionMap = ts.factory.createIndexedAccessTypeNode(typeReference("VersionsGTE"), typeReference("M"));
|
|
25
|
+
const narrowing = ts.factory.createTypePredicateNode(undefined, ts.factory.createThisTypeNode(), typeReference("Versioned", [
|
|
26
|
+
typeReference("T"),
|
|
27
|
+
typeReference("Extract", [typeReference("V"), activeVersionMap]),
|
|
28
|
+
]));
|
|
29
|
+
const minVersion = ts.factory.createMethodSignature(undefined, "minVersion", undefined, [ts.factory.createTypeParameterDeclaration(undefined, "M", activeKeys())], [parameter("min", typeReference("M"))], narrowing);
|
|
30
|
+
return formatStatements([
|
|
31
|
+
withJsDoc(typeAlias("Versions", unionType(versions.map(literalType))), {
|
|
32
|
+
description: 'Union of all version strings declared for this API group (e.g. \u0060"v1" | "v2" | "v3"\u0060).',
|
|
33
|
+
}),
|
|
34
|
+
withJsDoc(typeAlias("VersionsGTE", typeLiteral([...buildVersionsGTE(versions)].map(([version, newer]) => typeProperty(version, unionType(newer.map(literalType)))), true)), {
|
|
35
|
+
description: "Maps each version to the set of versions that are greater than or equal to it.\nUsed by \u0060Versioned.minVersion()\u0060 to narrow which versions a handler must support.",
|
|
36
|
+
}),
|
|
37
|
+
typeAlias("VersionMap", typeReference("Partial", [
|
|
38
|
+
typeReference("Record", [
|
|
39
|
+
typeReference("Versions"),
|
|
40
|
+
ts.factory.createKeywordTypeNode(ts.SyntaxKind.ObjectKeyword),
|
|
41
|
+
]),
|
|
42
|
+
]), false),
|
|
43
|
+
withJsDoc(typeAlias("Versioned", ts.factory.createIntersectionTypeNode([
|
|
44
|
+
ts.factory.createIndexedAccessTypeNode(typeReference("T"), typeReference("V")),
|
|
45
|
+
typeLiteral([typeProperty("version", typeReference("V")), minVersion], true),
|
|
46
|
+
]), true, [
|
|
47
|
+
ts.factory.createTypeParameterDeclaration(undefined, "T", typeReference("VersionMap")),
|
|
48
|
+
ts.factory.createTypeParameterDeclaration(undefined, "V", activeKeys(), activeKeys()),
|
|
49
|
+
]), { description: VERSIONED_DESCRIPTION }),
|
|
50
|
+
], ["This file is auto-generated by Counterfact. Do not edit."]);
|
|
82
51
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@counterfact/generator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Generate Counterfact route scaffolds and OpenAPI-derived TypeScript contracts.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -52,10 +52,11 @@
|
|
|
52
52
|
"test:packed-consumer": "node test/package/generator-consumer-smoke.mjs"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@counterfact/openapi": "0.1.
|
|
56
|
-
"@counterfact/types": "0.
|
|
55
|
+
"@counterfact/openapi": "0.1.6",
|
|
56
|
+
"@counterfact/types": "0.2.0",
|
|
57
57
|
"chokidar": "5.0.0",
|
|
58
58
|
"debug": "4.4.3",
|
|
59
|
-
"prettier": "3.9.10"
|
|
59
|
+
"prettier": "3.9.10",
|
|
60
|
+
"typescript": "6.0.3"
|
|
60
61
|
}
|
|
61
62
|
}
|
package/src/README.md
CHANGED
|
@@ -29,12 +29,33 @@ We can also use those types on the _client_ side, assuming the client is written
|
|
|
29
29
|
| `response-type-coder.ts` | `ResponseTypeCoder`: generates the type for a single HTTP response, including headers, content, and named examples |
|
|
30
30
|
| `schema-type-coder.ts` | `SchemaTypeCoder`: converts an OpenAPI schema definition to a TypeScript type (objects, arrays, unions, enums) |
|
|
31
31
|
| `schema-coder.ts` | `SchemaCoder`: generates a JSON Schema representation of an OpenAPI schema for use in runtime validation |
|
|
32
|
-
| `
|
|
33
|
-
| `printers.ts` | Utility functions for formatting TypeScript object literals in generated code |
|
|
32
|
+
| `ast.ts` | TypeScript AST construction and printing helpers |
|
|
34
33
|
| `read-only-comments.ts` | Standard warning comments inserted into generated type files to discourage manual edits |
|
|
35
34
|
|
|
36
35
|
## Architecture
|
|
37
36
|
|
|
37
|
+
### AST assembly
|
|
38
|
+
|
|
39
|
+
Type coders compose `TypeNode`s, and runtime coders compose `Expression`s through
|
|
40
|
+
`write()`. `Script` assembles import, export, and version declarations into a
|
|
41
|
+
`SourceFile`; the TypeScript printer emits it before Prettier formats the result.
|
|
42
|
+
Versioned operations register declaration builders rather than raw source text.
|
|
43
|
+
Context imports resolve their module paths before printing.
|
|
44
|
+
|
|
45
|
+
The standalone versions, scenario, middleware, and starter-file generators use
|
|
46
|
+
the same AST helpers. Repository regeneration reads existing route ASTs to find
|
|
47
|
+
exported handlers and imports, then inserts printed declarations without
|
|
48
|
+
reprinting user-authored text.
|
|
49
|
+
|
|
50
|
+
`ast.ts` centralizes printing, literal property names, type references, and
|
|
51
|
+
synthetic JSDoc. TypeScript is an explicit, pinned generator dependency because
|
|
52
|
+
its compiler API is used when the installed package generates files.
|
|
53
|
+
|
|
54
|
+
Keep OpenAPI traversal and file-writing policy separate from AST construction.
|
|
55
|
+
Existing route edits must survive regeneration, including when new handlers are
|
|
56
|
+
added. Check generated assignment behavior as well as snapshots when changing
|
|
57
|
+
type composition.
|
|
58
|
+
|
|
38
59
|
```
|
|
39
60
|
openapi.yaml
|
|
40
61
|
│
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const CONTEXT_FILE_TOKEN = "@@CONTEXT_FILE_TOKEN@@";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const CONTEXT_FILE_TOKEN = "@@CONTEXT_FILE_TOKEN@@";
|
package/dist/printers.d.ts
DELETED
package/dist/printers.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export function printObjectWithoutQuotes(entries) {
|
|
2
|
-
return `{\n${entries
|
|
3
|
-
.map(([key, value]) => `${key}: ${value}`)
|
|
4
|
-
.join(",\n")}\n}`;
|
|
5
|
-
}
|
|
6
|
-
export function printObject(entries) {
|
|
7
|
-
return `{\n${entries
|
|
8
|
-
.map(([key, value]) => `"${key}": ${value}`)
|
|
9
|
-
.join(",\n")}\n}`;
|
|
10
|
-
}
|