@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/script.js
CHANGED
|
@@ -1,45 +1,24 @@
|
|
|
1
1
|
import createDebugger from "debug";
|
|
2
|
-
import
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
import { formatStatements, importDeclaration, objectExpression, typeAlias, typeLiteral, typeProperty, unknownType, withComment, } from "./ast.js";
|
|
3
4
|
import { escapePathForWindows } from "./windows-escape.js";
|
|
4
5
|
import { pathJoin, pathRelative, pathDirname } from "./forward-slash-path.js";
|
|
5
6
|
const debug = createDebugger("counterfact:typescript-generator:script");
|
|
6
|
-
/**
|
|
7
|
-
* Represents a single TypeScript file being assembled by the code generator.
|
|
8
|
-
*
|
|
9
|
-
* A `Script` accumulates exports, imports, and external imports contributed by
|
|
10
|
-
* {@link Coder} instances. Once all coders have resolved, {@link contents}
|
|
11
|
-
* formats the result with Prettier and returns the final source text.
|
|
12
|
-
*
|
|
13
|
-
* Scripts are created and retrieved through a {@link Repository} so that the
|
|
14
|
-
* same module path always maps to the same `Script` instance.
|
|
15
|
-
*/
|
|
7
|
+
/** One generated module: coders register nodes; printing occurs after resolution. */
|
|
16
8
|
export class Script {
|
|
17
|
-
repository;
|
|
18
|
-
comments;
|
|
19
|
-
exports;
|
|
20
|
-
versions;
|
|
21
|
-
versionFormatters;
|
|
22
|
-
imports;
|
|
23
|
-
externalImport;
|
|
24
|
-
cache;
|
|
25
|
-
typeCache;
|
|
26
9
|
path;
|
|
10
|
+
repository;
|
|
11
|
+
comments = [];
|
|
12
|
+
exports = new Map();
|
|
13
|
+
versions = new Map();
|
|
14
|
+
versionBuilders = new Map();
|
|
15
|
+
imports = new Map();
|
|
16
|
+
externalImport = new Map();
|
|
17
|
+
cache = new Map();
|
|
27
18
|
constructor(repository, path) {
|
|
28
|
-
this.repository = repository;
|
|
29
|
-
this.comments = [];
|
|
30
|
-
this.exports = new Map();
|
|
31
|
-
this.versions = new Map();
|
|
32
|
-
this.versionFormatters = new Map();
|
|
33
|
-
this.imports = new Map();
|
|
34
|
-
this.externalImport = new Map();
|
|
35
|
-
this.cache = new Map();
|
|
36
|
-
this.typeCache = new Map();
|
|
37
19
|
this.path = path;
|
|
20
|
+
this.repository = repository;
|
|
38
21
|
}
|
|
39
|
-
/**
|
|
40
|
-
* A `"../"` path fragment that points from this script's directory back to
|
|
41
|
-
* the repository root, used to resolve relative import paths.
|
|
42
|
-
*/
|
|
43
22
|
get relativePathToBase() {
|
|
44
23
|
return this.path
|
|
45
24
|
.split("/")
|
|
@@ -47,100 +26,79 @@ export class Script {
|
|
|
47
26
|
.map(() => "..")
|
|
48
27
|
.join("/");
|
|
49
28
|
}
|
|
50
|
-
|
|
51
|
-
* Picks the first name from `coder.names()` that is not already used as an
|
|
52
|
-
* import in this script, ensuring export/import name uniqueness.
|
|
53
|
-
*
|
|
54
|
-
* @param coder - The coder needing a name.
|
|
55
|
-
* @throws When all 100 candidate names are already taken.
|
|
56
|
-
*/
|
|
57
|
-
firstUniqueName(coder) {
|
|
29
|
+
firstUniqueName(coder, isType = false, isDefault = false) {
|
|
58
30
|
for (const name of coder.names()) {
|
|
59
|
-
if (!this.imports.has(name)
|
|
31
|
+
if ((!this.imports.has(name) ||
|
|
32
|
+
(this.imports.get(name).id === coder.exportId &&
|
|
33
|
+
this.imports.get(name).isType === isType &&
|
|
34
|
+
this.imports.get(name).isDefault === isDefault)) &&
|
|
35
|
+
(!this.exports.has(name) ||
|
|
36
|
+
(this.exports.get(name).id === coder.exportId &&
|
|
37
|
+
this.exports.get(name).isType === isType &&
|
|
38
|
+
this.exports.get(name).isDefault === isDefault)) &&
|
|
39
|
+
!this.externalImport.has(name))
|
|
60
40
|
return name;
|
|
61
|
-
}
|
|
62
41
|
}
|
|
63
42
|
throw new Error(`could not find a unique name for ${coder.id}`);
|
|
64
43
|
}
|
|
65
|
-
/**
|
|
66
|
-
* Registers an export for `coder` in this script and returns the export name.
|
|
67
|
-
*
|
|
68
|
-
* If the same coder has already been exported (cache hit), the previously
|
|
69
|
-
* assigned name is returned without creating a duplicate.
|
|
70
|
-
*
|
|
71
|
-
* @param coder - The coder to export.
|
|
72
|
-
* @param isType - Emit `export type` instead of `export const`.
|
|
73
|
-
* @param isDefault - Emit `export default` instead of a named export.
|
|
74
|
-
* @returns The name under which the coder is exported.
|
|
75
|
-
*/
|
|
76
44
|
export(coder, isType = false, isDefault = false) {
|
|
77
45
|
const cacheKey = isDefault ? "default" : `${coder.id}:${isType}`;
|
|
78
|
-
if (this.cache.has(cacheKey))
|
|
46
|
+
if (this.cache.has(cacheKey))
|
|
79
47
|
return this.cache.get(cacheKey);
|
|
80
|
-
|
|
81
|
-
const name = this.firstUniqueName(coder);
|
|
48
|
+
const name = this.firstUniqueName(coder, isType, isDefault);
|
|
82
49
|
this.cache.set(cacheKey, name);
|
|
83
|
-
const
|
|
84
|
-
beforeExport: coder.beforeExport(this.path),
|
|
50
|
+
const entry = {
|
|
85
51
|
done: false,
|
|
86
|
-
id: coder.
|
|
52
|
+
id: coder.exportId,
|
|
87
53
|
isDefault,
|
|
88
54
|
isType,
|
|
55
|
+
name,
|
|
89
56
|
jsdoc: "",
|
|
90
57
|
typeDeclaration: coder.typeDeclaration(this.exports, this),
|
|
91
58
|
};
|
|
92
|
-
|
|
59
|
+
this.exports.set(name, entry);
|
|
60
|
+
entry.promise = coder
|
|
93
61
|
.delegate()
|
|
94
|
-
.then((
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
62
|
+
.then((available) => {
|
|
63
|
+
entry.statements = available.exportDeclarations(this, name);
|
|
64
|
+
if (entry.statements === undefined)
|
|
65
|
+
entry.node = available.write(this);
|
|
66
|
+
entry.jsdoc = available.jsdoc();
|
|
67
|
+
return available;
|
|
99
68
|
})
|
|
100
69
|
.catch((error) => {
|
|
101
|
-
|
|
102
|
-
|
|
70
|
+
entry.node = isType ? unknownType() : objectExpression([]);
|
|
71
|
+
entry.jsdoc = `/** error creating export "${name}" for ${this.path}: ${error.stack} */`;
|
|
72
|
+
entry.error = error;
|
|
103
73
|
return undefined;
|
|
104
74
|
})
|
|
105
75
|
.finally(() => {
|
|
106
|
-
|
|
76
|
+
entry.done = true;
|
|
107
77
|
});
|
|
108
|
-
this.exports.set(name, exportStatement);
|
|
109
78
|
return name;
|
|
110
79
|
}
|
|
111
80
|
exportDefault(coder, isType = false) {
|
|
112
81
|
this.export(coder, isType, true);
|
|
113
82
|
}
|
|
114
|
-
/**
|
|
115
|
-
* Registers an import of `coder` from its owning module and returns the
|
|
116
|
-
* local alias used in this script.
|
|
117
|
-
*
|
|
118
|
-
* The coder is also exported from its home module as a side effect.
|
|
119
|
-
*
|
|
120
|
-
* @param coder - The coder to import.
|
|
121
|
-
* @param isType - Use a `import type` declaration.
|
|
122
|
-
* @param isDefault - Import the default export rather than a named export.
|
|
123
|
-
*/
|
|
124
83
|
import(coder, isType = false, isDefault = false) {
|
|
125
|
-
debug("import coder: %s", coder.id);
|
|
126
84
|
const modulePath = coder.modulePath();
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
return this.
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
const scriptFromWhichToExport = this.repository.get(modulePath);
|
|
137
|
-
const exportedName = scriptFromWhichToExport.export(coder, isType, isDefault);
|
|
85
|
+
const key = `${coder.id}@${modulePath}:${isType}:${isDefault}`;
|
|
86
|
+
if (this.cache.has(key))
|
|
87
|
+
return this.cache.get(key);
|
|
88
|
+
if (modulePath === this.path)
|
|
89
|
+
return this.export(coder, isType, isDefault);
|
|
90
|
+
const name = this.firstUniqueName(coder, isType, isDefault);
|
|
91
|
+
this.cache.set(key, name);
|
|
92
|
+
const script = this.repository.get(modulePath);
|
|
93
|
+
const exportedName = script.export(coder, isType, isDefault);
|
|
138
94
|
this.imports.set(name, {
|
|
95
|
+
id: coder.exportId,
|
|
139
96
|
isDefault,
|
|
140
97
|
isType,
|
|
141
98
|
name: exportedName,
|
|
142
|
-
script
|
|
99
|
+
script,
|
|
143
100
|
});
|
|
101
|
+
debug("import %s as %s from %s", exportedName, name, modulePath);
|
|
144
102
|
return name;
|
|
145
103
|
}
|
|
146
104
|
importType(coder) {
|
|
@@ -149,174 +107,141 @@ export class Script {
|
|
|
149
107
|
importDefault(coder, isType = false) {
|
|
150
108
|
return this.import(coder, isType, true);
|
|
151
109
|
}
|
|
152
|
-
/**
|
|
153
|
-
* Registers an import from an external npm package or absolute module path
|
|
154
|
-
* (not managed by the repository).
|
|
155
|
-
*
|
|
156
|
-
* @param name - The local binding name.
|
|
157
|
-
* @param modulePath - The module specifier (e.g. `"counterfact-types/index"`).
|
|
158
|
-
* @param isType - Use a `import type` declaration.
|
|
159
|
-
* @returns `name` (for convenience in method chaining).
|
|
160
|
-
*/
|
|
161
110
|
importExternal(name, modulePath, isType = false) {
|
|
162
111
|
this.externalImport.set(name, { isType, modulePath });
|
|
163
112
|
return name;
|
|
164
113
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
*/
|
|
168
|
-
importExternalType(name, modulePath) {
|
|
169
|
-
return this.importExternal(name, modulePath, true);
|
|
114
|
+
importExternalType(name, path) {
|
|
115
|
+
return this.importExternal(name, path, true);
|
|
170
116
|
}
|
|
171
|
-
/**
|
|
172
|
-
* Imports a type from the shared `counterfact-types/index.ts` module,
|
|
173
|
-
* resolving the path relative to this script's location in the repository.
|
|
174
|
-
*
|
|
175
|
-
* @param name - The type name to import (e.g. `"WideOperationArgument"`).
|
|
176
|
-
*/
|
|
177
117
|
importSharedType(name) {
|
|
178
|
-
return this.
|
|
118
|
+
return this.importExternalType(name, pathJoin(this.relativePathToBase, "counterfact-types/index.ts"));
|
|
179
119
|
}
|
|
180
|
-
/**
|
|
181
|
-
* Imports a type from the generated `types/versions.ts` module,
|
|
182
|
-
* resolving the path relative to this script's location in the repository.
|
|
183
|
-
*
|
|
184
|
-
* @param name - The type name to import (e.g. `"Versioned"`).
|
|
185
|
-
*/
|
|
186
120
|
importVersionsType(name) {
|
|
187
|
-
return this.
|
|
121
|
+
return this.importExternalType(name, pathJoin(this.relativePathToBase, "types/versions.ts"));
|
|
188
122
|
}
|
|
189
123
|
exportType(coder) {
|
|
190
124
|
return this.export(coder, true);
|
|
191
125
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
* `name` by {@link versionsTypeStatements}.
|
|
195
|
-
*
|
|
196
|
-
* When a formatter is present for a name, `versionsTypeStatements` delegates
|
|
197
|
-
* the entire type declaration to it instead of generating the default
|
|
198
|
-
* `Versions` object type. The formatter receives a `Map<version, importAlias>`
|
|
199
|
-
* and must return the complete TypeScript source for that operation type.
|
|
200
|
-
*/
|
|
201
|
-
setVersionFormatter(name, formatter) {
|
|
202
|
-
this.versionFormatters.set(name, formatter);
|
|
126
|
+
setVersionBuilder(name, builder) {
|
|
127
|
+
this.versionBuilders.set(name, builder);
|
|
203
128
|
}
|
|
204
129
|
declareVersion(coder, name) {
|
|
205
|
-
const version = coder.version;
|
|
206
130
|
const versions = this.versions.get(name) ?? new Map();
|
|
207
131
|
this.versions.set(name, versions);
|
|
208
|
-
if (versions.has(version))
|
|
132
|
+
if (versions.has(coder.version))
|
|
209
133
|
return;
|
|
210
|
-
|
|
211
|
-
const versionStatement = {
|
|
212
|
-
beforeExport: "",
|
|
134
|
+
const entry = {
|
|
213
135
|
done: false,
|
|
214
|
-
id: coder.
|
|
136
|
+
id: coder.exportId,
|
|
215
137
|
isDefault: false,
|
|
216
138
|
isType: true,
|
|
217
139
|
jsdoc: "",
|
|
218
|
-
typeDeclaration: "",
|
|
219
140
|
};
|
|
220
|
-
|
|
141
|
+
versions.set(coder.version, entry);
|
|
142
|
+
entry.promise = coder
|
|
221
143
|
.delegate()
|
|
222
|
-
.then((
|
|
223
|
-
|
|
224
|
-
return
|
|
144
|
+
.then((available) => {
|
|
145
|
+
entry.node = available.write(this);
|
|
146
|
+
return available;
|
|
225
147
|
})
|
|
226
148
|
.catch((error) => {
|
|
227
|
-
|
|
228
|
-
|
|
149
|
+
entry.node = unknownType();
|
|
150
|
+
entry.error = error;
|
|
229
151
|
return undefined;
|
|
230
152
|
})
|
|
231
153
|
.finally(() => {
|
|
232
|
-
|
|
154
|
+
entry.done = true;
|
|
233
155
|
});
|
|
234
|
-
versions.set(version, versionStatement);
|
|
235
156
|
}
|
|
236
|
-
/** `true` while at least one export promise is still pending. */
|
|
237
157
|
isInProgress() {
|
|
238
|
-
return
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
.
|
|
158
|
+
return [
|
|
159
|
+
...this.exports.values(),
|
|
160
|
+
...[...this.versions.values()].flatMap((versions) => [
|
|
161
|
+
...versions.values(),
|
|
162
|
+
]),
|
|
163
|
+
].some((entry) => !entry.done);
|
|
242
164
|
}
|
|
243
|
-
/** Returns a promise that resolves when all pending export promises settle. */
|
|
244
165
|
finished() {
|
|
245
166
|
return Promise.all([
|
|
246
|
-
...
|
|
247
|
-
...
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
]);
|
|
251
|
-
}
|
|
252
|
-
externalImportStatements() {
|
|
253
|
-
return Array.from(this.externalImport, ([name,
|
|
167
|
+
...this.exports.values(),
|
|
168
|
+
...[...this.versions.values()].flatMap((versions) => [
|
|
169
|
+
...versions.values(),
|
|
170
|
+
]),
|
|
171
|
+
].map((entry) => entry.promise));
|
|
172
|
+
}
|
|
173
|
+
externalImportStatements(contextPath) {
|
|
174
|
+
return Array.from(this.externalImport, ([name, entry]) => {
|
|
175
|
+
const modulePath = typeof entry.modulePath === "string"
|
|
176
|
+
? entry.modulePath
|
|
177
|
+
: (contextPath ??
|
|
178
|
+
pathRelative(pathDirname(this.path), "routes/_.context.ts"));
|
|
179
|
+
return importDeclaration(name, modulePath.startsWith(".") || typeof entry.modulePath === "string"
|
|
180
|
+
? modulePath
|
|
181
|
+
: `./${modulePath}`, entry.isType, entry.isDefault);
|
|
182
|
+
});
|
|
254
183
|
}
|
|
255
184
|
importStatements() {
|
|
256
|
-
return Array.from(this.imports, ([name,
|
|
257
|
-
const
|
|
258
|
-
return
|
|
185
|
+
return Array.from(this.imports, ([name, entry]) => {
|
|
186
|
+
const path = escapePathForWindows(pathRelative(pathDirname(this.path), entry.script.path.replace(/\.ts$/u, ".js")));
|
|
187
|
+
return importDeclaration(name, path.includes("../") ? path : `./${path}`, entry.isType, entry.isDefault, entry.name);
|
|
259
188
|
});
|
|
260
189
|
}
|
|
261
190
|
exportStatements() {
|
|
262
|
-
return
|
|
263
|
-
if (
|
|
264
|
-
return
|
|
191
|
+
return [...this.exports.values()].flatMap((entry) => {
|
|
192
|
+
if (entry.statements !== undefined)
|
|
193
|
+
return entry.statements;
|
|
194
|
+
const node = entry.node;
|
|
195
|
+
if (entry.isType) {
|
|
196
|
+
if (!ts.isTypeNode(node))
|
|
197
|
+
throw new Error(`Expected a type node for ${entry.name}`);
|
|
198
|
+
const declaration = withComment(typeAlias(entry.name, node), entry.jsdoc);
|
|
199
|
+
if (!entry.isDefault)
|
|
200
|
+
return [declaration];
|
|
201
|
+
return [
|
|
202
|
+
declaration,
|
|
203
|
+
ts.factory.createExportDeclaration(undefined, true, ts.factory.createNamedExports([
|
|
204
|
+
ts.factory.createExportSpecifier(false, entry.name, "default"),
|
|
205
|
+
])),
|
|
206
|
+
];
|
|
265
207
|
}
|
|
266
|
-
if (
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
return
|
|
208
|
+
if (ts.isTypeNode(node))
|
|
209
|
+
throw new Error(`Expected an expression for ${entry.name}`);
|
|
210
|
+
const statement = entry.isDefault
|
|
211
|
+
? ts.factory.createExportAssignment(undefined, false, node)
|
|
212
|
+
: ts.factory.createVariableStatement([ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
|
|
213
|
+
ts.factory.createVariableDeclaration(entry.name, undefined, entry.typeDeclaration, node),
|
|
214
|
+
], ts.NodeFlags.Const));
|
|
215
|
+
return [withComment(statement, entry.jsdoc)];
|
|
274
216
|
});
|
|
275
217
|
}
|
|
276
218
|
versionsTypeStatements() {
|
|
277
|
-
if (this.versions.size === 0) {
|
|
278
|
-
return [];
|
|
279
|
-
}
|
|
280
219
|
const statements = [];
|
|
281
|
-
const
|
|
220
|
+
const unbuilt = [];
|
|
282
221
|
for (const [name, versions] of this.versions) {
|
|
283
|
-
const
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
version
|
|
287
|
-
|
|
288
|
-
]));
|
|
289
|
-
statements.push(formatter(versionCodes));
|
|
222
|
+
const types = new Map();
|
|
223
|
+
for (const [version, entry] of versions) {
|
|
224
|
+
if (!entry.node || !ts.isTypeNode(entry.node))
|
|
225
|
+
throw new Error(`Expected a type for ${name} version ${version}`);
|
|
226
|
+
types.set(version, entry.node);
|
|
290
227
|
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
const names = unformatted.map(([name, versions]) => {
|
|
297
|
-
const mappedVersions = Array.from(versions, ([version, versionStatement]) => `"${version}": ${versionStatement.code}`);
|
|
298
|
-
return `"${name}": { ${mappedVersions.join(", ")} }`;
|
|
299
|
-
});
|
|
300
|
-
statements.push(`export type Versions = { ${names.join(", ")} };`);
|
|
228
|
+
const builder = this.versionBuilders.get(name);
|
|
229
|
+
if (builder)
|
|
230
|
+
statements.push(...builder(types));
|
|
231
|
+
else
|
|
232
|
+
unbuilt.push(typeProperty(name, typeLiteral([...types].map(([version, type]) => typeProperty(version, type)))));
|
|
301
233
|
}
|
|
234
|
+
if (unbuilt.length > 0)
|
|
235
|
+
statements.push(typeAlias("Versions", typeLiteral(unbuilt)));
|
|
302
236
|
return statements;
|
|
303
237
|
}
|
|
304
|
-
|
|
305
|
-
* Formats the fully assembled script source with Prettier and returns it.
|
|
306
|
-
*
|
|
307
|
-
* All pending export promises are awaited before formatting.
|
|
308
|
-
*/
|
|
309
|
-
async contents() {
|
|
238
|
+
async contents(contextPath) {
|
|
310
239
|
await this.finished();
|
|
311
|
-
return
|
|
312
|
-
this.
|
|
313
|
-
this.
|
|
314
|
-
this.
|
|
315
|
-
this.
|
|
316
|
-
|
|
317
|
-
this.versionsTypeStatements().join("\n"),
|
|
318
|
-
this.versions.size > 0 ? "\n\n" : "",
|
|
319
|
-
this.exportStatements().join("\n\n"),
|
|
320
|
-
].join(""), { parser: "typescript" });
|
|
240
|
+
return formatStatements([
|
|
241
|
+
...this.externalImportStatements(contextPath),
|
|
242
|
+
...this.importStatements(),
|
|
243
|
+
...this.versionsTypeStatements(),
|
|
244
|
+
...this.exportStatements(),
|
|
245
|
+
], this.comments);
|
|
321
246
|
}
|
|
322
247
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CookieOptions } from "./cookie-options.js";
|
|
1
2
|
import type { COUNTERFACT_RESPONSE } from "./counterfact-response.js";
|
|
2
3
|
import type { HttpStatusCode } from "./http-status-code.js";
|
|
3
4
|
import type { MaybePromise } from "./maybe-promise.js";
|
|
@@ -26,8 +27,20 @@ export interface MiddlewareTools {
|
|
|
26
27
|
|
|
27
28
|
/** A response-builder factory with every standard status code available. */
|
|
28
29
|
export type MiddlewareResponseBuilderFactory = {
|
|
30
|
+
/** @deprecated Use response.status(statusCode) instead. */
|
|
29
31
|
[StatusCode in HttpStatusCode]: ResponseBuilder;
|
|
30
|
-
} & {
|
|
32
|
+
} & {
|
|
33
|
+
/** @deprecated Use response.status(statusCode) instead. */
|
|
34
|
+
[key: number | `${number} ${string}`]: ResponseBuilder;
|
|
35
|
+
} & {
|
|
36
|
+
status: (statusCode: number | `${number} ${string}`) => ResponseBuilder;
|
|
37
|
+
header: (name: string, value: string) => MiddlewareResponseBuilderFactory;
|
|
38
|
+
cookie: (
|
|
39
|
+
name: string,
|
|
40
|
+
value: string,
|
|
41
|
+
options?: CookieOptions,
|
|
42
|
+
) => MiddlewareResponseBuilderFactory;
|
|
43
|
+
};
|
|
31
44
|
|
|
32
45
|
/**
|
|
33
46
|
* The request argument passed to a custom middleware function.
|
|
@@ -74,7 +87,7 @@ export type RespondTo<Context = unknown> = (
|
|
|
74
87
|
*
|
|
75
88
|
* export const middleware: Middleware<Context> = async ($, respondTo) => {
|
|
76
89
|
* if (!$.context.isAuthorized($.auth?.apiKey)) {
|
|
77
|
-
* return $.response
|
|
90
|
+
* return $.response.status(401).json({ error: "Unauthorized" });
|
|
78
91
|
* }
|
|
79
92
|
*
|
|
80
93
|
* return respondTo($);
|
|
@@ -1,16 +1,112 @@
|
|
|
1
|
+
import type { CookieOptions } from "./cookie-options.js";
|
|
1
2
|
import type { GenericResponseBuilder } from "./generic-response-builder.js";
|
|
2
|
-
import type { OpenApiResponses } from "./open-api-response.js";
|
|
3
|
+
import type { OpenApiResponse, OpenApiResponses } from "./open-api-response.js";
|
|
4
|
+
|
|
5
|
+
type StatusCodes<Responses> =
|
|
6
|
+
| keyof Responses
|
|
7
|
+
| {
|
|
8
|
+
[Key in keyof Responses]: Key extends `${infer Code extends number}`
|
|
9
|
+
? Code
|
|
10
|
+
: never;
|
|
11
|
+
}[keyof Responses];
|
|
12
|
+
|
|
13
|
+
type ResponseAt<Responses, Code> = Code extends keyof Responses
|
|
14
|
+
? Responses[Code]
|
|
15
|
+
: Code extends number
|
|
16
|
+
? `${Code}` extends keyof Responses
|
|
17
|
+
? Responses[`${Code}`]
|
|
18
|
+
: Responses extends { default: infer Default }
|
|
19
|
+
? Default
|
|
20
|
+
: never
|
|
21
|
+
: Responses extends { default: infer Default }
|
|
22
|
+
? Default
|
|
23
|
+
: never;
|
|
24
|
+
|
|
25
|
+
type HeaderNames<Responses extends OpenApiResponses> = {
|
|
26
|
+
[Code in keyof Responses]: [Responses[Code]["headers"]] extends [never]
|
|
27
|
+
? never
|
|
28
|
+
: keyof Responses[Code]["headers"];
|
|
29
|
+
}[keyof Responses] &
|
|
30
|
+
string;
|
|
31
|
+
|
|
32
|
+
type HeaderValues<Responses extends OpenApiResponses, Name extends string> = {
|
|
33
|
+
[Code in keyof Responses]: Name extends keyof Responses[Code]["headers"]
|
|
34
|
+
? Responses[Code]["headers"][Name]["schema"]
|
|
35
|
+
: never;
|
|
36
|
+
}[keyof Responses];
|
|
37
|
+
|
|
38
|
+
// Filter each response independently, preserving the relationship between a
|
|
39
|
+
// status code and its remaining body/header requirements.
|
|
40
|
+
type WithHeader<
|
|
41
|
+
Responses extends OpenApiResponses,
|
|
42
|
+
Name extends string,
|
|
43
|
+
Value,
|
|
44
|
+
> = {
|
|
45
|
+
[
|
|
46
|
+
Code in keyof Responses as [Responses[Code]["headers"]] extends [never]
|
|
47
|
+
? never
|
|
48
|
+
: [Name] extends [keyof Responses[Code]["headers"]]
|
|
49
|
+
? [Value] extends [Responses[Code]["headers"][Name]["schema"]]
|
|
50
|
+
? Code
|
|
51
|
+
: never
|
|
52
|
+
: never
|
|
53
|
+
]: {
|
|
54
|
+
content: Responses[Code]["content"];
|
|
55
|
+
headers: Omit<Responses[Code]["headers"], Name>;
|
|
56
|
+
requiredHeaders: Exclude<Responses[Code]["requiredHeaders"], Name>;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
3
59
|
|
|
4
60
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* argument object, allowing handlers to call e.g. `response[200].json(body)`.
|
|
61
|
+
* Select a response with `response.status(200)`.
|
|
62
|
+
* Headers supplied before selection narrow the available status codes and
|
|
63
|
+
* satisfy the same requirements as headers supplied after selection.
|
|
9
64
|
*/
|
|
10
65
|
export type ResponseBuilderFactory<
|
|
11
66
|
Responses extends OpenApiResponses = OpenApiResponses,
|
|
67
|
+
ExcludedStatuses extends PropertyKey = never,
|
|
12
68
|
> = {
|
|
69
|
+
/** @deprecated Use response.status(statusCode) instead. */
|
|
13
70
|
[StatusCode in keyof Responses]: GenericResponseBuilder<
|
|
14
71
|
Responses[StatusCode]
|
|
15
72
|
>;
|
|
16
|
-
} & {
|
|
73
|
+
} & {
|
|
74
|
+
/** @deprecated Use response.status(statusCode) instead. */
|
|
75
|
+
[key: string]: GenericResponseBuilder<Responses["default"]>;
|
|
76
|
+
} & {
|
|
77
|
+
status: <
|
|
78
|
+
Code extends
|
|
79
|
+
| StatusCodes<Responses>
|
|
80
|
+
| ("default" extends keyof Responses
|
|
81
|
+
? number | `${number} ${string}`
|
|
82
|
+
: never),
|
|
83
|
+
>(
|
|
84
|
+
statusCode: Code extends ExcludedStatuses
|
|
85
|
+
? never
|
|
86
|
+
: Code extends number
|
|
87
|
+
? `${Code}` extends ExcludedStatuses
|
|
88
|
+
? never
|
|
89
|
+
: Code
|
|
90
|
+
: Code,
|
|
91
|
+
) => Code extends unknown
|
|
92
|
+
? ResponseAt<Responses, Code> extends infer Response extends OpenApiResponse
|
|
93
|
+
? GenericResponseBuilder<Response>
|
|
94
|
+
: never
|
|
95
|
+
: never;
|
|
96
|
+
header: <
|
|
97
|
+
Name extends HeaderNames<Responses>,
|
|
98
|
+
const Value extends HeaderValues<Responses, Name>,
|
|
99
|
+
>(
|
|
100
|
+
name: Name,
|
|
101
|
+
value: Value,
|
|
102
|
+
) => ResponseBuilderFactory<
|
|
103
|
+
WithHeader<Responses, Name, Value>,
|
|
104
|
+
| ExcludedStatuses
|
|
105
|
+
| Exclude<keyof Responses, keyof WithHeader<Responses, Name, Value>>
|
|
106
|
+
>;
|
|
107
|
+
cookie: (
|
|
108
|
+
name: string,
|
|
109
|
+
value: string,
|
|
110
|
+
options?: CookieOptions,
|
|
111
|
+
) => ResponseBuilderFactory<Responses, ExcludedStatuses>;
|
|
112
|
+
};
|
|
@@ -8,6 +8,7 @@ import type { MaybePromise } from "./maybe-promise.js";
|
|
|
8
8
|
* strongly-typed `GenericResponseBuilder`.
|
|
9
9
|
*/
|
|
10
10
|
export interface ResponseBuilder {
|
|
11
|
+
/** @deprecated Select a status with response.status(statusCode) instead. */
|
|
11
12
|
[status: number | `${number} ${string}`]: ResponseBuilder;
|
|
12
13
|
binary: (body: Uint8Array | string) => ResponseBuilder;
|
|
13
14
|
content?: { body: unknown; type: string }[];
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CookieOptions } from "./cookie-options.js";
|
|
1
2
|
import type { HttpStatusCode } from "./http-status-code.js";
|
|
2
3
|
import type { WideResponseBuilder } from "./wide-response-builder.js";
|
|
3
4
|
|
|
@@ -14,7 +15,18 @@ export interface WideOperationArgument {
|
|
|
14
15
|
path: { [key: string]: string };
|
|
15
16
|
proxy: (url: string) => { proxyUrl: string };
|
|
16
17
|
query: { [key: string]: string };
|
|
17
|
-
response: {
|
|
18
|
+
response: {
|
|
19
|
+
/** @deprecated Use response.status(statusCode) instead. */
|
|
20
|
+
[StatusCode in HttpStatusCode]: WideResponseBuilder;
|
|
21
|
+
} & {
|
|
22
|
+
/** @deprecated Use response.status(statusCode) instead. */
|
|
18
23
|
[key: number]: WideResponseBuilder;
|
|
24
|
+
status: (statusCode: number | `${number} ${string}`) => WideResponseBuilder;
|
|
25
|
+
header: (name: string, value: string) => WideOperationArgument["response"];
|
|
26
|
+
cookie: (
|
|
27
|
+
name: string,
|
|
28
|
+
value: string,
|
|
29
|
+
options?: CookieOptions,
|
|
30
|
+
) => WideOperationArgument["response"];
|
|
19
31
|
};
|
|
20
32
|
}
|
package/dist/type-coder.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import type ts from "typescript";
|
|
1
2
|
import { Coder } from "./coder.js";
|
|
2
3
|
import type { Script } from "./script.js";
|
|
3
4
|
export declare class TypeCoder extends Coder {
|
|
4
|
-
write(script: Script):
|
|
5
|
+
write(script: Script): ts.TypeNode;
|
|
6
|
+
writeCode(_script: Script): ts.TypeNode;
|
|
5
7
|
}
|