@counterfact/generator 1.2.0 → 2.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 +55 -0
- package/dist/adopt-spellings.d.ts +15 -0
- package/dist/adopt-spellings.js +48 -0
- package/dist/case-safe-paths.d.ts +21 -0
- package/dist/case-safe-paths.js +57 -0
- package/dist/central-types.d.ts +2 -1
- package/dist/central-types.js +2 -2
- package/dist/client-multipart-schema-type-coder.d.ts +16 -0
- package/dist/client-multipart-schema-type-coder.js +78 -0
- package/dist/client-response-schemas.d.ts +29 -0
- package/dist/client-response-schemas.js +120 -0
- package/dist/client-security.d.ts +10 -0
- package/dist/client-security.js +49 -0
- package/dist/code-generator.d.ts +22 -4
- package/dist/code-generator.js +51 -57
- package/dist/generate-client.d.ts +3 -0
- package/dist/generate-client.js +418 -0
- package/dist/generate-versioned-client.d.ts +12 -0
- package/dist/generate-versioned-client.js +103 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/migrate-route-layout.d.ts +8 -0
- package/dist/migrate-route-layout.js +448 -0
- package/dist/named-client-operations.d.ts +19 -0
- package/dist/named-client-operations.js +96 -0
- package/dist/operation-coder.js +3 -3
- package/dist/operation-type-coder.d.ts +9 -3
- package/dist/operation-type-coder.js +43 -33
- package/dist/path-operations.d.ts +2 -0
- package/dist/path-operations.js +31 -0
- package/dist/prune.d.ts +1 -1
- package/dist/prune.js +10 -3
- package/dist/repository.d.ts +30 -1
- package/dist/repository.js +224 -34
- package/dist/requirement.js +3 -1
- package/dist/route-file-layout.d.ts +32 -0
- package/dist/route-file-layout.js +152 -0
- package/dist/scenario-file-generator.js +27 -7
- package/dist/script.d.ts +2 -0
- package/dist/script.js +16 -4
- package/dist/standalone-client-validators.d.ts +2 -0
- package/dist/standalone-client-validators.js +85 -0
- package/dist/swagger2-client.d.ts +31 -0
- package/dist/swagger2-client.js +92 -0
- package/examples/generate-routes.mjs +2 -0
- package/package.json +4 -3
- package/src/README.md +21 -20
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { RESERVED_WORDS } from "./reserved-words.js";
|
|
2
|
+
function words(value) {
|
|
3
|
+
return value
|
|
4
|
+
.replaceAll(/(?<lower>[a-z\d])(?<upper>[A-Z])/gu, "$<lower> $<upper>")
|
|
5
|
+
.split(/[^A-Za-z\d]+/u)
|
|
6
|
+
.filter(Boolean);
|
|
7
|
+
}
|
|
8
|
+
function capitalize(word) {
|
|
9
|
+
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
10
|
+
}
|
|
11
|
+
function safeIdentifier(name, fallback) {
|
|
12
|
+
if (name === "")
|
|
13
|
+
return fallback;
|
|
14
|
+
if (/^\d/u.test(name))
|
|
15
|
+
return `_${name}`;
|
|
16
|
+
return RESERVED_WORDS.has(name) ? `${name}_` : name;
|
|
17
|
+
}
|
|
18
|
+
/** Converts arbitrary text such as `list_users` to `listUsers`. */
|
|
19
|
+
function camelCase(value, fallback = "_") {
|
|
20
|
+
const [first = "", ...rest] = words(value);
|
|
21
|
+
return safeIdentifier(first.charAt(0).toLowerCase() +
|
|
22
|
+
first.slice(1) +
|
|
23
|
+
rest.map(capitalize).join(""), fallback);
|
|
24
|
+
}
|
|
25
|
+
/** Converts arbitrary text such as `explodable-parameters` to `ExplodableParameters`. */
|
|
26
|
+
function pascalCase(value, fallback = "_") {
|
|
27
|
+
return safeIdentifier(words(value).map(capitalize).join(""), fallback);
|
|
28
|
+
}
|
|
29
|
+
/** Reserves `name`, appending a counter when it is already taken. */
|
|
30
|
+
function uniqueName(name, taken) {
|
|
31
|
+
let candidate = name;
|
|
32
|
+
for (let index = 2; taken.has(candidate.toLowerCase()); index += 1) {
|
|
33
|
+
candidate = `${name}${index}`;
|
|
34
|
+
}
|
|
35
|
+
taken.add(candidate.toLowerCase());
|
|
36
|
+
return candidate;
|
|
37
|
+
}
|
|
38
|
+
function isPathParameterSegment(segment) {
|
|
39
|
+
return /^\{[^}]+\}$/u.test(segment);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Names an operation that has no `operationId` from its path and method,
|
|
43
|
+
* for example `GET /pets` → `petsList` and `GET /pets/{id}` → `petsDetail`.
|
|
44
|
+
*/
|
|
45
|
+
function fallbackOperationName(path, method) {
|
|
46
|
+
const segments = path.split("/").filter(Boolean);
|
|
47
|
+
const base = camelCase(segments.filter((segment) => !isPathParameterSegment(segment)).join(" "), "root");
|
|
48
|
+
const endsWithParameter = isPathParameterSegment(segments.at(-1) ?? "");
|
|
49
|
+
const suffixes = {
|
|
50
|
+
delete: "Delete",
|
|
51
|
+
get: endsWithParameter ? "Detail" : "List",
|
|
52
|
+
patch: "PartialUpdate",
|
|
53
|
+
post: "Create",
|
|
54
|
+
put: "Update",
|
|
55
|
+
};
|
|
56
|
+
// eslint-disable-next-line security/detect-object-injection -- method is an OpenAPI operation key; unknown methods fall back below.
|
|
57
|
+
return `${base}${suffixes[method] ?? pascalCase(method)}`;
|
|
58
|
+
}
|
|
59
|
+
function moduleNameFor(path, tags, options) {
|
|
60
|
+
const tag = tags[0];
|
|
61
|
+
if (options.moduleNameFirstTag && tag !== undefined) {
|
|
62
|
+
const name = pascalCase(tag, "");
|
|
63
|
+
if (name !== "")
|
|
64
|
+
return name;
|
|
65
|
+
}
|
|
66
|
+
const segment = path.split("/").filter(Boolean)[options.moduleNameIndex ?? 0];
|
|
67
|
+
return segment === undefined
|
|
68
|
+
? "Root"
|
|
69
|
+
: pascalCase(segment.replaceAll(/[{}]/gu, ""), "Root");
|
|
70
|
+
}
|
|
71
|
+
export function namedOperations(paths, options) {
|
|
72
|
+
const groups = new Map();
|
|
73
|
+
for (const { path, operations } of paths) {
|
|
74
|
+
const pathParameters = Array.from(path.matchAll(/\{(?<name>[^}]+)\}/gu), (match) => match.groups["name"]);
|
|
75
|
+
for (const { method, operationId, tags } of operations) {
|
|
76
|
+
const name = camelCase(moduleNameFor(path, tags, options), "root");
|
|
77
|
+
const key = name.toLowerCase();
|
|
78
|
+
let group = groups.get(key);
|
|
79
|
+
if (!group) {
|
|
80
|
+
group = {
|
|
81
|
+
name,
|
|
82
|
+
used: new Set(["constructor", "http"]),
|
|
83
|
+
operations: new Map(),
|
|
84
|
+
};
|
|
85
|
+
groups.set(key, group);
|
|
86
|
+
}
|
|
87
|
+
const methodName = uniqueName((operationId && camelCase(operationId, "")) ||
|
|
88
|
+
fallbackOperationName(path, method), group.used);
|
|
89
|
+
group.operations.set(methodName, { path, method, pathParameters });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return Object.fromEntries(Array.from(groups.values(), ({ name, operations }) => [
|
|
93
|
+
name,
|
|
94
|
+
Object.fromEntries(operations),
|
|
95
|
+
]));
|
|
96
|
+
}
|
package/dist/operation-coder.js
CHANGED
|
@@ -42,8 +42,8 @@ export class OperationCoder extends Coder {
|
|
|
42
42
|
const [response] = responses.map((item) => item.data);
|
|
43
43
|
const content = response?.content;
|
|
44
44
|
const streaming = content !== undefined &&
|
|
45
|
-
Object.
|
|
46
|
-
|
|
45
|
+
Object.entries(content).some(([media, definition]) => STREAMING_CONTENT_TYPES.has(media) &&
|
|
46
|
+
definition?.itemSchema !== undefined);
|
|
47
47
|
const method = streaming
|
|
48
48
|
? "stream"
|
|
49
49
|
: response !== undefined &&
|
|
@@ -79,7 +79,7 @@ export class OperationCoder extends Coder {
|
|
|
79
79
|
// merged handler type after all versions have been declared.
|
|
80
80
|
const versionedArgCoder = new VersionedArgTypeCoder(this.requirement, this.version, this.requestMethod, this.securitySchemes);
|
|
81
81
|
const sharedScript = script.repository.get(operationTypeCoder.modulePath());
|
|
82
|
-
sharedScript.declareVersion(versionedArgCoder, operationTypeCoder.getOperationBaseName());
|
|
82
|
+
sharedScript.declareVersion(versionedArgCoder, sharedScript.operationVersionKey(operationTypeCoder.getOperationBaseName(), operationTypeCoder.logicalRoute));
|
|
83
83
|
}
|
|
84
84
|
const path = this.requirement.parent?.url.split("/").at(-1) ??
|
|
85
85
|
this.requirement.url.split("/").at(-2);
|
|
@@ -28,6 +28,12 @@ export declare class OperationTypeCoder extends TypeCoder {
|
|
|
28
28
|
/** Returns the stable HTTP-method identifier, independent of operationId. */
|
|
29
29
|
getOperationBaseName(): string;
|
|
30
30
|
jsdoc(): string;
|
|
31
|
+
/**
|
|
32
|
+
* The OpenAPI path (e.g. `/Pet`). It tells apart case-variant paths that
|
|
33
|
+
* share a module, and stays the same across API versions, unlike the
|
|
34
|
+
* requirement URL, which includes the specification's location.
|
|
35
|
+
*/
|
|
36
|
+
get logicalRoute(): string;
|
|
31
37
|
get exportId(): string;
|
|
32
38
|
names(): Generator<string>;
|
|
33
39
|
/**
|
|
@@ -73,7 +79,7 @@ export declare class OperationTypeCoder extends TypeCoder {
|
|
|
73
79
|
*/
|
|
74
80
|
protected getEffectiveParameters(): Requirement | undefined;
|
|
75
81
|
/**
|
|
76
|
-
* Builds the
|
|
82
|
+
* Builds the dollar-argument type body and sets
|
|
77
83
|
* up all required shared-type imports on `script`.
|
|
78
84
|
*
|
|
79
85
|
* This helper is reused by both {@link writeCode} (non-versioned) and
|
|
@@ -96,7 +102,7 @@ export declare class OperationTypeCoder extends TypeCoder {
|
|
|
96
102
|
* `script.importType(this)` so that the type is written to the per-version
|
|
97
103
|
* file and an import is added to the calling script.
|
|
98
104
|
*
|
|
99
|
-
* Only the
|
|
105
|
+
* Only the dollar-argument type body is emitted — the
|
|
100
106
|
* function-wrapper `($: Versioned<…>) => MaybePromise<COUNTERFACT_RESPONSE>`
|
|
101
107
|
* is assembled by the shared script's `versionsTypeStatements()`.
|
|
102
108
|
*/
|
|
@@ -125,7 +131,7 @@ export declare class VersionedArgTypeCoder extends OperationTypeCoder {
|
|
|
125
131
|
exportDeclarations(): undefined;
|
|
126
132
|
write(script: Script): ts.TypeNode;
|
|
127
133
|
/**
|
|
128
|
-
* Generates the
|
|
134
|
+
* Generates the dollar-argument type and writes
|
|
129
135
|
* it to the per-version script.
|
|
130
136
|
*/
|
|
131
137
|
writeCode(script: Script): ts.TypeNode;
|
|
@@ -44,10 +44,20 @@ export class OperationTypeCoder extends TypeCoder {
|
|
|
44
44
|
jsdoc() {
|
|
45
45
|
return buildJsDoc(this.requirement.data);
|
|
46
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* The OpenAPI path (e.g. `/Pet`). It tells apart case-variant paths that
|
|
49
|
+
* share a module, and stays the same across API versions, unlike the
|
|
50
|
+
* requirement URL, which includes the specification's location.
|
|
51
|
+
*/
|
|
52
|
+
get logicalRoute() {
|
|
53
|
+
const pointer = this.requirement.parent?.url.split("/").at(-1) ??
|
|
54
|
+
this.requirement.url.split("/").at(-2);
|
|
55
|
+
return pointer.replaceAll("~1", "/").replaceAll("~0", "~");
|
|
56
|
+
}
|
|
47
57
|
get exportId() {
|
|
48
58
|
return this.version === ""
|
|
49
59
|
? this.id
|
|
50
|
-
: `${this.constructor.name}@${this.
|
|
60
|
+
: `${this.constructor.name}@${this.logicalRoute}:${this.requestMethod.toLowerCase()}`;
|
|
51
61
|
}
|
|
52
62
|
names() {
|
|
53
63
|
return super.names(this.getOperationBaseName());
|
|
@@ -199,7 +209,7 @@ export class OperationTypeCoder extends TypeCoder {
|
|
|
199
209
|
return new Requirement([...map.values()], this.requirement.url, this.requirement.specification);
|
|
200
210
|
}
|
|
201
211
|
/**
|
|
202
|
-
* Builds the
|
|
212
|
+
* Builds the dollar-argument type body and sets
|
|
203
213
|
* up all required shared-type imports on `script`.
|
|
204
214
|
*
|
|
205
215
|
* This helper is reused by both {@link writeCode} (non-versioned) and
|
|
@@ -211,7 +221,6 @@ export class OperationTypeCoder extends TypeCoder {
|
|
|
211
221
|
*/
|
|
212
222
|
buildDollarArgType(script, baseName, modulePath) {
|
|
213
223
|
const xType = script.importSharedType("WideOperationArgument");
|
|
214
|
-
script.importSharedType("OmitValueWhenNever");
|
|
215
224
|
script.importSharedType("COUNTERFACT_RESPONSE");
|
|
216
225
|
script.contextRoutePath = normalizeOpenApiPath(this.requirement.url.split("/").at(-2).replaceAll("~1", "/"));
|
|
217
226
|
const contextTypeImportName = script.importExternalType("Context", {
|
|
@@ -232,17 +241,14 @@ export class OperationTypeCoder extends TypeCoder {
|
|
|
232
241
|
const bodyTypes = legacyBody
|
|
233
242
|
? [new SchemaTypeCoder(legacyBody, this.version).write(script)]
|
|
234
243
|
: [];
|
|
244
|
+
const requestBody = this.requirement.get("requestBody");
|
|
235
245
|
const seenBodySchemas = new Set();
|
|
236
246
|
for (const mediaType of [
|
|
237
247
|
"application/json",
|
|
238
248
|
"application/x-www-form-urlencoded",
|
|
239
249
|
"multipart/form-data",
|
|
240
250
|
]) {
|
|
241
|
-
const schema =
|
|
242
|
-
.get("requestBody")
|
|
243
|
-
?.get("content")
|
|
244
|
-
?.get(mediaType)
|
|
245
|
-
?.get("schema");
|
|
251
|
+
const schema = requestBody?.get("content")?.get(mediaType)?.get("schema");
|
|
246
252
|
if (schema) {
|
|
247
253
|
const Coder = mediaType === "multipart/form-data"
|
|
248
254
|
? MultipartSchemaTypeCoder
|
|
@@ -254,6 +260,11 @@ export class OperationTypeCoder extends TypeCoder {
|
|
|
254
260
|
}
|
|
255
261
|
}
|
|
256
262
|
}
|
|
263
|
+
if (requestBody &&
|
|
264
|
+
bodyTypes.length > 0 &&
|
|
265
|
+
requestBody.get("required")?.data !== true) {
|
|
266
|
+
bodyTypes.push(ts.factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword));
|
|
267
|
+
}
|
|
257
268
|
const bodyType = bodyTypes.length === 0 ? neverType() : unionType(bodyTypes);
|
|
258
269
|
const openApi2MediaTypes = (this.requirement.get("produces")?.data ??
|
|
259
270
|
this.requirement.specification?.rootRequirement?.get("produces")
|
|
@@ -273,29 +284,29 @@ export class OperationTypeCoder extends TypeCoder {
|
|
|
273
284
|
// OpenAPI 3.2 querystring parameter: the entire query string treated as a
|
|
274
285
|
// single typed object (similar to requestBody for query strings).
|
|
275
286
|
const querystringParam = parameters?.find((parameter) => parameter.get("in")?.data === "querystring");
|
|
276
|
-
const
|
|
277
|
-
|
|
287
|
+
const [querystringMedia] = querystringParam?.get("content")?.map((media) => media) ?? [];
|
|
288
|
+
const querystringSchema = querystringMedia?.get("schema") ?? querystringParam?.get("schema");
|
|
289
|
+
const querystringType = querystringSchema
|
|
290
|
+
? new SchemaTypeCoder(querystringSchema, this.version).write(script)
|
|
278
291
|
: neverType();
|
|
279
292
|
const querystringTypeName = this.exportParameterType(script, "querystring", querystringType, baseName, modulePath);
|
|
280
293
|
const versionLiteralType = this.version !== "" ? literalType(this.version) : neverType();
|
|
281
|
-
return
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
]),
|
|
298
|
-
]);
|
|
294
|
+
return typeLiteral([
|
|
295
|
+
typeProperty("query", queryTypeName),
|
|
296
|
+
typeProperty("querystring", querystringTypeName),
|
|
297
|
+
typeProperty("path", pathTypeName),
|
|
298
|
+
typeProperty("headers", headersTypeName),
|
|
299
|
+
typeProperty("cookie", cookieTypeName),
|
|
300
|
+
typeProperty("body", bodyType),
|
|
301
|
+
typeProperty("context", typeReference(contextTypeImportName)),
|
|
302
|
+
typeProperty("response", responseType),
|
|
303
|
+
typeProperty("x", typeReference(xType)),
|
|
304
|
+
typeProperty("proxy", proxyType),
|
|
305
|
+
typeProperty("auth", this.authType()),
|
|
306
|
+
typeProperty("user", this.userType()),
|
|
307
|
+
typeProperty("delay", delayType),
|
|
308
|
+
typeProperty("version", versionLiteralType),
|
|
309
|
+
].filter((property) => property.type?.kind !== ts.SyntaxKind.NeverKeyword));
|
|
299
310
|
}
|
|
300
311
|
exportDeclarations(script, name) {
|
|
301
312
|
if (this.version === "")
|
|
@@ -304,7 +315,7 @@ export class OperationTypeCoder extends TypeCoder {
|
|
|
304
315
|
const versioned = script.importVersionsType("Versioned");
|
|
305
316
|
const promise = script.importSharedType("MaybePromise");
|
|
306
317
|
const response = script.importSharedType("COUNTERFACT_RESPONSE");
|
|
307
|
-
script.setVersionBuilder(this.getOperationBaseName(), (versions) => {
|
|
318
|
+
script.setVersionBuilder(script.operationVersionKey(this.getOperationBaseName(), this.logicalRoute), (versions) => {
|
|
308
319
|
const mapName = `${name}_$_Versions`;
|
|
309
320
|
return [
|
|
310
321
|
typeAlias(mapName, typeLiteral([...versions].map(([version, type]) => typeProperty(version, type))), false),
|
|
@@ -318,7 +329,6 @@ export class OperationTypeCoder extends TypeCoder {
|
|
|
318
329
|
writeCode(script) {
|
|
319
330
|
script.comments = READ_ONLY_COMMENTS;
|
|
320
331
|
script.importSharedType("WideOperationArgument");
|
|
321
|
-
script.importSharedType("OmitValueWhenNever");
|
|
322
332
|
script.importSharedType("MaybePromise");
|
|
323
333
|
script.importSharedType("COUNTERFACT_RESPONSE");
|
|
324
334
|
const arg = this.buildDollarArgType(script, this.getOperationBaseName(), this.modulePath());
|
|
@@ -334,7 +344,7 @@ export class OperationTypeCoder extends TypeCoder {
|
|
|
334
344
|
* `script.importType(this)` so that the type is written to the per-version
|
|
335
345
|
* file and an import is added to the calling script.
|
|
336
346
|
*
|
|
337
|
-
* Only the
|
|
347
|
+
* Only the dollar-argument type body is emitted — the
|
|
338
348
|
* function-wrapper `($: Versioned<…>) => MaybePromise<COUNTERFACT_RESPONSE>`
|
|
339
349
|
* is assembled by the shared script's `versionsTypeStatements()`.
|
|
340
350
|
*/
|
|
@@ -383,13 +393,13 @@ export class VersionedArgTypeCoder extends OperationTypeCoder {
|
|
|
383
393
|
return undefined;
|
|
384
394
|
}
|
|
385
395
|
write(script) {
|
|
386
|
-
if (script
|
|
396
|
+
if (script === script.repository.get(this.modulePath())) {
|
|
387
397
|
return this.writeCode(script);
|
|
388
398
|
}
|
|
389
399
|
return typeReference(script.importType(this));
|
|
390
400
|
}
|
|
391
401
|
/**
|
|
392
|
-
* Generates the
|
|
402
|
+
* Generates the dollar-argument type and writes
|
|
393
403
|
* it to the per-version script.
|
|
394
404
|
*/
|
|
395
405
|
writeCode(script) {
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Requirement } from "./requirement.js";
|
|
2
|
+
const methods = new Set([
|
|
3
|
+
"get",
|
|
4
|
+
"put",
|
|
5
|
+
"post",
|
|
6
|
+
"delete",
|
|
7
|
+
"options",
|
|
8
|
+
"head",
|
|
9
|
+
"patch",
|
|
10
|
+
"trace",
|
|
11
|
+
]);
|
|
12
|
+
export function pathOperations(pathItem) {
|
|
13
|
+
let resolved = pathItem;
|
|
14
|
+
while (resolved.isReference)
|
|
15
|
+
resolved = resolved.reference();
|
|
16
|
+
const operations = resolved.flatMap((operation, name) => {
|
|
17
|
+
if (name === "additionalOperations") {
|
|
18
|
+
return operation.map((additional, method) => [
|
|
19
|
+
additional,
|
|
20
|
+
method.toLowerCase(),
|
|
21
|
+
]);
|
|
22
|
+
}
|
|
23
|
+
return methods.has(name) ? [[operation, name]] : [];
|
|
24
|
+
});
|
|
25
|
+
return operations.map(([operation, method]) => {
|
|
26
|
+
// A referenced operation still belongs to the endpoint and its path-item parameters.
|
|
27
|
+
const logical = new Requirement(operation.data, `${pathItem.url}/${method}`, pathItem.specification);
|
|
28
|
+
logical.parent = pathItem;
|
|
29
|
+
return [logical, method];
|
|
30
|
+
});
|
|
31
|
+
}
|
package/dist/prune.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @param openApiPaths - Iterable of OpenAPI path strings (e.g. "/pet/{id}")
|
|
6
6
|
* @returns Number of files removed
|
|
7
7
|
*/
|
|
8
|
-
export declare function pruneRoutes(destination: string, openApiPaths: Iterable<string>): Promise<number>;
|
|
8
|
+
export declare function pruneRoutes(destination: string, openApiPaths: Iterable<string>, outputPaths?: Iterable<string>): Promise<number>;
|
|
9
9
|
/**
|
|
10
10
|
* Prunes obsolete files from the OpenAPI generator-owned namespaces below
|
|
11
11
|
* `types/`. Hidden files are included by the recursive directory walk.
|
package/dist/prune.js
CHANGED
|
@@ -81,11 +81,18 @@ function openApiPathToRouteFile(openApiPath) {
|
|
|
81
81
|
* @param openApiPaths - Iterable of OpenAPI path strings (e.g. "/pet/{id}")
|
|
82
82
|
* @returns Number of files removed
|
|
83
83
|
*/
|
|
84
|
-
export async function pruneRoutes(destination, openApiPaths) {
|
|
84
|
+
export async function pruneRoutes(destination, openApiPaths, outputPaths) {
|
|
85
85
|
const routesDir = nodePath.join(destination, "routes");
|
|
86
|
-
const expectedFiles = new Set(
|
|
86
|
+
const expectedFiles = new Set(outputPaths
|
|
87
|
+
? Array.from(outputPaths, (path) => path.slice("routes/".length))
|
|
88
|
+
: Array.from(openApiPaths).map(openApiPathToRouteFile));
|
|
87
89
|
debug("expected route files: %o", Array.from(expectedFiles));
|
|
88
|
-
const actualFiles = (await collectTypeScriptFiles(routesDir)).filter((file) =>
|
|
90
|
+
const actualFiles = (await collectTypeScriptFiles(routesDir)).filter((file) => {
|
|
91
|
+
const name = nodePath.basename(file);
|
|
92
|
+
return (name !== "_.context.ts" &&
|
|
93
|
+
name !== "_.middleware.ts" &&
|
|
94
|
+
!name.startsWith("_.counterfact-legacy-"));
|
|
95
|
+
});
|
|
89
96
|
debug("actual route files: %o", actualFiles);
|
|
90
97
|
let prunedCount = 0;
|
|
91
98
|
for (const file of actualFiles) {
|
package/dist/repository.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { Script } from "./script.js";
|
|
|
2
2
|
interface WriteFilesOptions {
|
|
3
3
|
routes?: boolean;
|
|
4
4
|
types?: boolean;
|
|
5
|
+
prune?: boolean;
|
|
6
|
+
keepVersionsFile?: boolean;
|
|
5
7
|
}
|
|
6
8
|
/**
|
|
7
9
|
* Collection of {@link Script} objects keyed by their repository-relative
|
|
@@ -14,6 +16,14 @@ interface WriteFilesOptions {
|
|
|
14
16
|
*/
|
|
15
17
|
export declare class Repository {
|
|
16
18
|
scripts: Map<string, Script>;
|
|
19
|
+
private readonly routesByPath;
|
|
20
|
+
private readonly specRouteFiles;
|
|
21
|
+
private readonly typeLayout;
|
|
22
|
+
private readonly typeScripts;
|
|
23
|
+
private routeLayout;
|
|
24
|
+
recordRouteFile(logicalFile: string): void;
|
|
25
|
+
route(logicalFile: string, path: string): Script;
|
|
26
|
+
outputPath(logical: string): string;
|
|
17
27
|
constructor();
|
|
18
28
|
/**
|
|
19
29
|
* Returns the {@link Script} for `path`, creating it if it does not yet
|
|
@@ -46,7 +56,26 @@ export declare class Repository {
|
|
|
46
56
|
* @param destination - Absolute path to the output root directory.
|
|
47
57
|
* @param options - Controls which artefacts are written.
|
|
48
58
|
*/
|
|
49
|
-
writeFiles(destination: string, { routes, types }: WriteFilesOptions): Promise<void>;
|
|
59
|
+
writeFiles(destination: string, { routes, types, prune, keepVersionsFile }: WriteFilesOptions): Promise<void>;
|
|
60
|
+
private assertSafeRouteLayout;
|
|
61
|
+
/**
|
|
62
|
+
* Waits for all scripts to finish, then writes only the schemas for
|
|
63
|
+
* `version` directly under `destination`, without the
|
|
64
|
+
* `types/<version>/components/schemas/` prefix. Versioned schemas are written
|
|
65
|
+
* to `<destination>/<version>/`.
|
|
66
|
+
*
|
|
67
|
+
* Every other script in the repository is left unwritten. Fails, naming the
|
|
68
|
+
* schema, when a schema imports anything that would not be written, because
|
|
69
|
+
* the flattened file would then contain a broken import.
|
|
70
|
+
*
|
|
71
|
+
* @param destination - Absolute path to the output directory.
|
|
72
|
+
* @param version - Version label of the schemas to write (`""` when unversioned).
|
|
73
|
+
* @returns The output paths written, relative to `destination`.
|
|
74
|
+
*/
|
|
75
|
+
writeSchemas(destination: string, version?: string): Promise<string[]>;
|
|
76
|
+
/** Write an isolated tree of generated scripts at their original paths. */
|
|
77
|
+
writeGeneratedScripts(destination: string, version: string, extra?: ReadonlyMap<string, string>): Promise<string[]>;
|
|
78
|
+
private writeOutputs;
|
|
50
79
|
/**
|
|
51
80
|
* Creates the default `routes/_.context.ts` file if it does not already
|
|
52
81
|
* exist.
|