counterfact 2.11.0 → 2.14.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/README.md +2 -1
- package/dist/api-runner.js +8 -2
- package/dist/app.js +8 -1
- package/dist/cli/run.js +44 -6
- package/dist/cli/telemetry.js +10 -4
- package/dist/migrate/update-route-types.js +1 -0
- package/dist/msw.js +1 -0
- package/dist/repl/repl.js +4 -0
- package/dist/server/counterfact-types/example.ts +5 -1
- package/dist/server/counterfact-types/generic-response-builder.ts +4 -0
- package/dist/server/counterfact-types/open-api-parameters.ts +8 -1
- package/dist/server/counterfact-types/response-builder.ts +5 -0
- package/dist/server/counterfact-types/wide-response-builder.ts +1 -0
- package/dist/server/dispatcher.js +60 -6
- package/dist/server/json-to-xml.js +32 -7
- package/dist/server/load-openapi-document.js +2 -2
- package/dist/server/module-loader.js +5 -0
- package/dist/server/openapi-document.js +18 -1
- package/dist/server/registry.js +22 -5
- package/dist/server/request-validator.js +1 -0
- package/dist/server/response-builder.js +28 -5
- package/dist/server/web-server/create-koa-app.js +4 -1
- package/dist/server/web-server/openapi-middleware.js +5 -0
- package/dist/server/web-server/routes-middleware.js +43 -1
- package/dist/typescript-generator/code-generator.js +25 -10
- package/dist/typescript-generator/coder.js +1 -1
- package/dist/typescript-generator/jsdoc.js +11 -7
- package/dist/typescript-generator/operation-coder.js +14 -0
- package/dist/typescript-generator/operation-type-coder.js +65 -9
- package/dist/typescript-generator/repository.js +97 -8
- package/dist/typescript-generator/requirement.js +25 -3
- package/dist/typescript-generator/response-type-coder.js +20 -7
- package/dist/typescript-generator/schema-coder.js +2 -2
- package/dist/typescript-generator/schema-type-coder.js +16 -3
- package/dist/typescript-generator/specification.js +17 -6
- package/dist/typescript-generator/streaming-content-types.js +16 -0
- package/dist/typescript-generator/versions-ts-generator.js +25 -0
- package/dist/util/apply-overlay.js +119 -0
- package/package.json +29 -29
|
@@ -26,7 +26,19 @@ export class Requirement {
|
|
|
26
26
|
}
|
|
27
27
|
/** `true` when this node is a JSON Reference (`$ref`) rather than inline data. */
|
|
28
28
|
get isReference() {
|
|
29
|
-
return this.data
|
|
29
|
+
return (typeof this.data === "object" &&
|
|
30
|
+
this.data !== null &&
|
|
31
|
+
this.data["$ref"] !== undefined);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* When this node is a JSON Reference, returns the raw `$ref` URL string.
|
|
35
|
+
* Returns `undefined` for non-reference (inline) nodes.
|
|
36
|
+
*/
|
|
37
|
+
get refUrl() {
|
|
38
|
+
if (typeof this.data !== "object" || this.data === null) {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
return this.data["$ref"];
|
|
30
42
|
}
|
|
31
43
|
/**
|
|
32
44
|
* Resolves the `$ref` and returns the target {@link Requirement}.
|
|
@@ -34,7 +46,7 @@ export class Requirement {
|
|
|
34
46
|
* @throws When `isReference` is `false` or the specification is not set.
|
|
35
47
|
*/
|
|
36
48
|
reference() {
|
|
37
|
-
return this.specification.getRequirement(this.
|
|
49
|
+
return this.specification.getRequirement(this.refUrl);
|
|
38
50
|
}
|
|
39
51
|
/**
|
|
40
52
|
* Returns `true` when this node has a child property named `item`.
|
|
@@ -47,6 +59,9 @@ export class Requirement {
|
|
|
47
59
|
if (this.isReference) {
|
|
48
60
|
return this.reference().has(item);
|
|
49
61
|
}
|
|
62
|
+
if (typeof this.data !== "object" || this.data === null) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
50
65
|
return item in this.data;
|
|
51
66
|
}
|
|
52
67
|
/**
|
|
@@ -62,7 +77,11 @@ export class Requirement {
|
|
|
62
77
|
if (!this.has(key)) {
|
|
63
78
|
return undefined;
|
|
64
79
|
}
|
|
65
|
-
|
|
80
|
+
if (typeof this.data !== "object" || this.data === null) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
const objectData = this.data;
|
|
84
|
+
const child = new Requirement(objectData[key], `${this.url}/${this.escapeJsonPointer(key)}`, this.specification);
|
|
66
85
|
child.parent = this;
|
|
67
86
|
return child;
|
|
68
87
|
}
|
|
@@ -108,6 +127,9 @@ export class Requirement {
|
|
|
108
127
|
* @param callback - Called for each child with `(child, key)`.
|
|
109
128
|
*/
|
|
110
129
|
forEach(callback) {
|
|
130
|
+
if (typeof this.data !== "object" || this.data === null) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
111
133
|
Object.keys(this.data).forEach((key) => {
|
|
112
134
|
callback(this.select(this.escapeJsonPointer(key)), key);
|
|
113
135
|
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { printObject } from "./printers.js";
|
|
2
2
|
import { SchemaTypeCoder } from "./schema-type-coder.js";
|
|
3
|
+
import { STREAMING_CONTENT_TYPES } from "./streaming-content-types.js";
|
|
3
4
|
import { TypeCoder } from "./type-coder.js";
|
|
4
5
|
import { pathJoin } from "../util/forward-slash-path.js";
|
|
5
6
|
export class ResponseTypeCoder extends TypeCoder {
|
|
@@ -9,18 +10,30 @@ export class ResponseTypeCoder extends TypeCoder {
|
|
|
9
10
|
this.openApi2MediaTypes = openApi2MediaTypes;
|
|
10
11
|
}
|
|
11
12
|
names() {
|
|
12
|
-
return super.names(this.requirement.
|
|
13
|
+
return super.names(this.requirement.refUrl.split("/").at(-1));
|
|
13
14
|
}
|
|
14
15
|
buildContentObjectType(script, response) {
|
|
15
16
|
if (response.has("content")) {
|
|
16
17
|
return response
|
|
17
18
|
.get("content")
|
|
18
|
-
.map((content, mediaType) =>
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
.map((content, mediaType) => {
|
|
20
|
+
let schemaType;
|
|
21
|
+
if (content.has("itemSchema") &&
|
|
22
|
+
STREAMING_CONTENT_TYPES.has(mediaType)) {
|
|
23
|
+
schemaType = `AsyncIterable<${new SchemaTypeCoder(content.get("itemSchema"), this.version).write(script)}>`;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
schemaType = content.has("schema")
|
|
27
|
+
? new SchemaTypeCoder(content.get("schema"), this.version).write(script)
|
|
28
|
+
: "unknown";
|
|
29
|
+
}
|
|
30
|
+
return [
|
|
31
|
+
mediaType,
|
|
32
|
+
`{
|
|
33
|
+
schema: ${schemaType}
|
|
22
34
|
}`,
|
|
23
|
-
|
|
35
|
+
];
|
|
36
|
+
});
|
|
24
37
|
}
|
|
25
38
|
return this.openApi2MediaTypes.map((mediaType) => [
|
|
26
39
|
mediaType,
|
|
@@ -78,7 +91,7 @@ export class ResponseTypeCoder extends TypeCoder {
|
|
|
78
91
|
return printObject(exampleNames.map((name) => [name, "unknown"]));
|
|
79
92
|
}
|
|
80
93
|
modulePath() {
|
|
81
|
-
return pathJoin("types", this.version, this.requirement.
|
|
94
|
+
return pathJoin("types", this.version, this.requirement.refUrl + ".ts");
|
|
82
95
|
}
|
|
83
96
|
writeCode(script) {
|
|
84
97
|
return `{
|
|
@@ -8,7 +8,7 @@ function scrubSchema(schema) {
|
|
|
8
8
|
}
|
|
9
9
|
export class SchemaCoder extends Coder {
|
|
10
10
|
names() {
|
|
11
|
-
return super.names(`${this.requirement.
|
|
11
|
+
return super.names(`${this.requirement.refUrl.split("/").at(-1)}Schema`);
|
|
12
12
|
}
|
|
13
13
|
objectSchema(script) {
|
|
14
14
|
const { properties, required } = this.requirement.data;
|
|
@@ -34,7 +34,7 @@ export class SchemaCoder extends Coder {
|
|
|
34
34
|
return script.importExternalType("JSONSchema6", "json-schema");
|
|
35
35
|
}
|
|
36
36
|
modulePath() {
|
|
37
|
-
return `types/${this.requirement.
|
|
37
|
+
return `types/${this.requirement.refUrl}.ts`;
|
|
38
38
|
}
|
|
39
39
|
writeCode(script) {
|
|
40
40
|
const { type } = this.requirement.data;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { buildJsDoc } from "./jsdoc.js";
|
|
2
2
|
import { TypeCoder } from "./type-coder.js";
|
|
3
|
+
import { Requirement } from "./requirement.js";
|
|
3
4
|
import { pathJoin } from "../util/forward-slash-path.js";
|
|
4
5
|
export class SchemaTypeCoder extends TypeCoder {
|
|
5
6
|
names() {
|
|
6
|
-
return super.names(this.requirement.
|
|
7
|
+
return super.names(this.requirement.refUrl?.split("/").at(-1));
|
|
7
8
|
}
|
|
8
9
|
jsdoc() {
|
|
9
10
|
return buildJsDoc(this.requirement.data);
|
|
@@ -82,6 +83,14 @@ export class SchemaTypeCoder extends TypeCoder {
|
|
|
82
83
|
const key = matchingKey();
|
|
83
84
|
const items = (allOf ?? anyOf ?? oneOf);
|
|
84
85
|
const types = items.map((_item, index) => new SchemaTypeCoder(this.requirement.get(key).get(index), this.version).write(script));
|
|
86
|
+
// Include the default schema from discriminator.defaultMapping (OpenAPI 3.2)
|
|
87
|
+
if (!allOf) {
|
|
88
|
+
const { discriminator } = this.requirement.data;
|
|
89
|
+
if (discriminator?.defaultMapping) {
|
|
90
|
+
const defaultRequirement = new Requirement({ $ref: discriminator.defaultMapping }, "", this.requirement.specification);
|
|
91
|
+
types.push(new SchemaTypeCoder(defaultRequirement, this.version).write(script));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
85
94
|
return types.join(allOf ? " & " : " | ");
|
|
86
95
|
}
|
|
87
96
|
writeEnum(_script, requirement) {
|
|
@@ -90,10 +99,14 @@ export class SchemaTypeCoder extends TypeCoder {
|
|
|
90
99
|
.join(" | ");
|
|
91
100
|
}
|
|
92
101
|
modulePath() {
|
|
93
|
-
return pathJoin("types", this.version, this.requirement.
|
|
102
|
+
return pathJoin("types", this.version, this.requirement.refUrl.replace(/^#\//u, "") + ".ts");
|
|
94
103
|
}
|
|
95
104
|
writeCode(script) {
|
|
96
|
-
const { allOf, anyOf, oneOf, type, format } = this.requirement
|
|
105
|
+
const { allOf, anyOf, oneOf, type, format, itemSchema } = this.requirement
|
|
106
|
+
.data;
|
|
107
|
+
if (itemSchema) {
|
|
108
|
+
return `AsyncIterable<${new SchemaTypeCoder(this.requirement.get("itemSchema"), this.version).write(script)}>`;
|
|
109
|
+
}
|
|
97
110
|
if (allOf ?? anyOf ?? oneOf) {
|
|
98
111
|
return this.writeGroup(script, { allOf, anyOf, oneOf });
|
|
99
112
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { bundle } from "@apidevtools/json-schema-ref-parser";
|
|
2
2
|
import createDebug from "debug";
|
|
3
|
+
import { applyOverlays } from "../util/apply-overlay.js";
|
|
3
4
|
import { Requirement } from "./requirement.js";
|
|
4
5
|
const debug = createDebug("counterfact:typescript-generator:specification");
|
|
5
6
|
/**
|
|
@@ -23,12 +24,14 @@ export class Specification {
|
|
|
23
24
|
* Loads the OpenAPI document at `urlOrPath`, bundles all external `$ref`
|
|
24
25
|
* references, and returns a fully initialised {@link Specification}.
|
|
25
26
|
*
|
|
26
|
-
* @param urlOrPath
|
|
27
|
+
* @param urlOrPath - A local file path or HTTP(S) URL.
|
|
28
|
+
* @param overlays - Optional ordered list of overlay file paths/URLs to
|
|
29
|
+
* apply after loading the document.
|
|
27
30
|
* @throws When the document cannot be found or parsed.
|
|
28
31
|
*/
|
|
29
|
-
static async fromFile(urlOrPath) {
|
|
32
|
+
static async fromFile(urlOrPath, overlays = []) {
|
|
30
33
|
const specification = new Specification();
|
|
31
|
-
await specification.load(urlOrPath);
|
|
34
|
+
await specification.load(urlOrPath, overlays);
|
|
32
35
|
return specification;
|
|
33
36
|
}
|
|
34
37
|
/**
|
|
@@ -42,14 +45,22 @@ export class Specification {
|
|
|
42
45
|
return this.rootRequirement.select(url.slice(2));
|
|
43
46
|
}
|
|
44
47
|
/**
|
|
45
|
-
* Loads (or reloads) the specification from `urlOrPath
|
|
48
|
+
* Loads (or reloads) the specification from `urlOrPath`, then applies any
|
|
49
|
+
* overlay files listed in `overlays` in order.
|
|
46
50
|
*
|
|
47
51
|
* @param urlOrPath - A local file path or HTTP(S) URL.
|
|
52
|
+
* @param overlays - Optional ordered list of overlay file paths/URLs.
|
|
48
53
|
* @throws When the document cannot be found or parsed.
|
|
49
54
|
*/
|
|
50
|
-
async load(urlOrPath) {
|
|
55
|
+
async load(urlOrPath, overlays = []) {
|
|
51
56
|
try {
|
|
52
|
-
|
|
57
|
+
const document = (await bundle(urlOrPath, {
|
|
58
|
+
resolve: { http: { safeUrlResolver: false } },
|
|
59
|
+
}));
|
|
60
|
+
if (overlays.length > 0) {
|
|
61
|
+
await applyOverlays(document, overlays);
|
|
62
|
+
}
|
|
63
|
+
this.rootRequirement = new Requirement(document, urlOrPath, this);
|
|
53
64
|
}
|
|
54
65
|
catch (error) {
|
|
55
66
|
const details = error instanceof Error ? error.message : String(error);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content types that represent sequential/streaming media in OpenAPI 3.2.
|
|
3
|
+
*
|
|
4
|
+
* When a Media Type Object uses `itemSchema` together with one of these content
|
|
5
|
+
* types, the generated TypeScript body type is `AsyncIterable<T>` rather than
|
|
6
|
+
* a plain schema type. On the server side, returning an `AsyncIterable` for
|
|
7
|
+
* one of these content types causes Counterfact to stream each item in the
|
|
8
|
+
* appropriate wire format.
|
|
9
|
+
*/
|
|
10
|
+
export const STREAMING_CONTENT_TYPES = new Set([
|
|
11
|
+
"text/event-stream",
|
|
12
|
+
"application/jsonl",
|
|
13
|
+
"application/x-ndjson",
|
|
14
|
+
"application/ndjson",
|
|
15
|
+
"application/json-seq",
|
|
16
|
+
]);
|
|
@@ -30,6 +30,7 @@ export async function generateVersionsTsContent(versions) {
|
|
|
30
30
|
const source = [
|
|
31
31
|
"// This file is auto-generated by Counterfact. Do not edit.",
|
|
32
32
|
"",
|
|
33
|
+
'/** Union of all version strings declared for this API group (e.g. `"v1" | "v2" | "v3"`). */',
|
|
33
34
|
`export type Versions = ${versionsUnion};`,
|
|
34
35
|
"",
|
|
35
36
|
"/**",
|
|
@@ -42,6 +43,30 @@ export async function generateVersionsTsContent(versions) {
|
|
|
42
43
|
"",
|
|
43
44
|
"type VersionMap = Partial<Record<Versions, object>>;",
|
|
44
45
|
"",
|
|
46
|
+
"/**",
|
|
47
|
+
" * The type of the `$` argument in a versioned route handler.",
|
|
48
|
+
" *",
|
|
49
|
+
" * @typeParam T - A map from version string to the `$`-arg type for that version",
|
|
50
|
+
" * (e.g. `{ v1: $v1Type; v2: $v2Type }`). Generated by Counterfact from the spec.",
|
|
51
|
+
" * @typeParam V - The union of currently active version keys; defaults to all keys of `T`.",
|
|
52
|
+
" *",
|
|
53
|
+
" * An instance of `Versioned<T, V>` exposes:",
|
|
54
|
+
" * - All properties of the intersection `T[V]` (request data, response builders, etc.)",
|
|
55
|
+
' * - `version` — the version string for the current request (e.g. `"v2"`).',
|
|
56
|
+
" * - `minVersion(min)` — type predicate that returns `true` when the current version",
|
|
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
|
+
" */",
|
|
45
70
|
"export type Versioned<",
|
|
46
71
|
" T extends VersionMap,",
|
|
47
72
|
" V extends keyof T & Versions = keyof T & Versions,",
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { load as loadYaml } from "js-yaml";
|
|
2
|
+
import { JSONPath } from "jsonpath-plus";
|
|
3
|
+
import { readFile } from "./read-file.js";
|
|
4
|
+
/**
|
|
5
|
+
* Deeply merges `source` into `target`, overwriting scalar values and
|
|
6
|
+
* recursively merging plain objects. Arrays and non-plain-object values in
|
|
7
|
+
* `source` always overwrite the corresponding entry in `target`.
|
|
8
|
+
*/
|
|
9
|
+
function deepMerge(target, source) {
|
|
10
|
+
for (const [key, value] of Object.entries(source)) {
|
|
11
|
+
// Guard against prototype pollution attacks.
|
|
12
|
+
if (key === "__proto__" || key === "constructor" || key === "prototype") {
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
if (typeof value === "object" &&
|
|
16
|
+
value !== null &&
|
|
17
|
+
!Array.isArray(value) &&
|
|
18
|
+
typeof target[key] === "object" &&
|
|
19
|
+
target[key] !== null &&
|
|
20
|
+
!Array.isArray(target[key])) {
|
|
21
|
+
deepMerge(target[key], value);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
target[key] = value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Applies a list of overlay actions to `document` in place.
|
|
30
|
+
*
|
|
31
|
+
* Each action may either:
|
|
32
|
+
* - **update**: deep-merge the `action.update` object into every node matched
|
|
33
|
+
* by the JSONPath `action.target`.
|
|
34
|
+
* - **remove**: delete every node matched by `action.target` from its parent.
|
|
35
|
+
*
|
|
36
|
+
* @param document - The OpenAPI document object to mutate.
|
|
37
|
+
* @param actions - The ordered list of overlay actions to apply.
|
|
38
|
+
*/
|
|
39
|
+
export function applyOverlayActions(document, actions) {
|
|
40
|
+
for (const action of actions) {
|
|
41
|
+
const results = JSONPath({
|
|
42
|
+
path: action.target,
|
|
43
|
+
json: document,
|
|
44
|
+
resultType: "all",
|
|
45
|
+
});
|
|
46
|
+
if (action.remove === true) {
|
|
47
|
+
// Iterate in reverse so that removing by numeric index doesn't shift
|
|
48
|
+
// subsequent items in the same parent array.
|
|
49
|
+
for (const result of [...results].reverse()) {
|
|
50
|
+
const { parent, parentProperty } = result;
|
|
51
|
+
if (Array.isArray(parent)) {
|
|
52
|
+
parent.splice(Number(parentProperty), 1);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
delete parent[String(parentProperty)];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else if (action.update !== undefined) {
|
|
60
|
+
for (const result of results) {
|
|
61
|
+
if (typeof result.value === "object" &&
|
|
62
|
+
result.value !== null &&
|
|
63
|
+
!Array.isArray(result.value)) {
|
|
64
|
+
deepMerge(result.value, action.update);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Loads and parses an overlay file (YAML or JSON), validates that it looks
|
|
72
|
+
* like a valid OpenAPI overlay document, and returns the parsed object.
|
|
73
|
+
*
|
|
74
|
+
* @param overlayPath - Path or URL to the overlay file.
|
|
75
|
+
* @throws When the file cannot be read, parsed, or does not contain an
|
|
76
|
+
* `overlay` version field and an `actions` array.
|
|
77
|
+
*/
|
|
78
|
+
export async function loadOverlay(overlayPath) {
|
|
79
|
+
let content;
|
|
80
|
+
try {
|
|
81
|
+
content = await readFile(overlayPath);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
const details = error instanceof Error ? error.message : String(error);
|
|
85
|
+
throw new Error(`Could not read overlay file "${overlayPath}".\n${details}`, { cause: error });
|
|
86
|
+
}
|
|
87
|
+
let parsed;
|
|
88
|
+
try {
|
|
89
|
+
parsed = loadYaml(content);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
const details = error instanceof Error ? error.message : String(error);
|
|
93
|
+
throw new Error(`Could not parse overlay file "${overlayPath}".\n${details}`, { cause: error });
|
|
94
|
+
}
|
|
95
|
+
if (typeof parsed !== "object" ||
|
|
96
|
+
parsed === null ||
|
|
97
|
+
!("overlay" in parsed) ||
|
|
98
|
+
!("actions" in parsed) ||
|
|
99
|
+
!Array.isArray(parsed.actions)) {
|
|
100
|
+
throw new Error(`"${overlayPath}" does not appear to be a valid OpenAPI overlay file. ` +
|
|
101
|
+
`Expected an object with "overlay" and "actions" fields.`);
|
|
102
|
+
}
|
|
103
|
+
return parsed;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Applies all overlays listed in `overlayPaths` to `document` in order.
|
|
107
|
+
*
|
|
108
|
+
* Each overlay is loaded from disk (or a URL), parsed, and its actions are
|
|
109
|
+
* applied sequentially. The document is mutated in place.
|
|
110
|
+
*
|
|
111
|
+
* @param document - The OpenAPI document object to mutate.
|
|
112
|
+
* @param overlayPaths - Ordered list of paths/URLs to overlay files.
|
|
113
|
+
*/
|
|
114
|
+
export async function applyOverlays(document, overlayPaths) {
|
|
115
|
+
for (const overlayPath of overlayPaths) {
|
|
116
|
+
const overlay = await loadOverlay(overlayPath);
|
|
117
|
+
applyOverlayActions(document, overlay.actions);
|
|
118
|
+
}
|
|
119
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "counterfact",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.14.0",
|
|
4
4
|
"description": "Generate a TypeScript-based mock server from an OpenAPI spec in seconds — with stateful routes, hot reload, and REPL support.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/app.js",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": {
|
|
9
|
-
"import": "./dist/app.js"
|
|
9
|
+
"import": "./dist/app.js",
|
|
10
|
+
"types": "./dist/server/types.d.ts"
|
|
10
11
|
}
|
|
11
12
|
},
|
|
12
13
|
"types": "./dist/server/types.d.ts",
|
|
@@ -78,44 +79,43 @@
|
|
|
78
79
|
"go:petstore2": "yarn build && yarn counterfact https://petstore.swagger.io/v2/swagger.json out",
|
|
79
80
|
"go:example": "yarn build && node ./bin/counterfact.js ./test/fixtures/openapi/example.yaml out",
|
|
80
81
|
"go:multiple-apis": "yarn build && node ./bin/counterfact.js --config ./test/fixtures/config/multiple-apis.yaml",
|
|
81
|
-
"counterfact": "./bin/counterfact.js"
|
|
82
|
-
"postinstall": "patch-package"
|
|
82
|
+
"counterfact": "./bin/counterfact.js"
|
|
83
83
|
},
|
|
84
84
|
"devDependencies": {
|
|
85
|
-
"@changesets/cli": "2.
|
|
85
|
+
"@changesets/cli": "2.31.0",
|
|
86
86
|
"@eslint/js": "10.0.1",
|
|
87
|
-
"@jest/globals": "
|
|
88
|
-
"@swc/core": "1.15.
|
|
87
|
+
"@jest/globals": "30.4.1",
|
|
88
|
+
"@swc/core": "1.15.40",
|
|
89
89
|
"@swc/jest": "0.2.39",
|
|
90
90
|
"@testing-library/dom": "10.4.1",
|
|
91
|
-
"@types/debug": "
|
|
91
|
+
"@types/debug": "4.1.13",
|
|
92
92
|
"@types/jest": "30.0.0",
|
|
93
93
|
"@types/js-yaml": "4.0.9",
|
|
94
|
-
"@types/koa": "3.0.
|
|
94
|
+
"@types/koa": "3.0.3",
|
|
95
95
|
"@types/koa-bodyparser": "4.3.13",
|
|
96
96
|
"@types/koa-proxy": "1.0.8",
|
|
97
97
|
"@types/koa-static": "4.0.4",
|
|
98
98
|
"@types/node": "22",
|
|
99
|
-
"@typescript-eslint/eslint-plugin": "
|
|
100
|
-
"@typescript-eslint/parser": "
|
|
99
|
+
"@typescript-eslint/eslint-plugin": "8.60.0",
|
|
100
|
+
"@typescript-eslint/parser": "8.60.0",
|
|
101
101
|
"copyfiles": "2.4.1",
|
|
102
|
-
"eslint": "10.
|
|
102
|
+
"eslint": "10.4.0",
|
|
103
103
|
"eslint-formatter-github-annotations": "0.1.0",
|
|
104
104
|
"eslint-import-resolver-typescript": "4.4.4",
|
|
105
105
|
"eslint-plugin-etc": "2.0.3",
|
|
106
106
|
"eslint-plugin-file-progress": "4.0.0",
|
|
107
107
|
"eslint-plugin-import": "2.32.0",
|
|
108
|
-
"eslint-plugin-jest": "29.15.
|
|
108
|
+
"eslint-plugin-jest": "29.15.2",
|
|
109
109
|
"eslint-plugin-jest-dom": "5.5.0",
|
|
110
|
-
"eslint-plugin-n": "
|
|
110
|
+
"eslint-plugin-n": "18.0.1",
|
|
111
111
|
"eslint-plugin-no-explicit-type-exports": "0.12.1",
|
|
112
112
|
"eslint-plugin-prettier": "5.5.5",
|
|
113
|
-
"eslint-plugin-promise": "
|
|
114
|
-
"eslint-plugin-regexp": "
|
|
115
|
-
"eslint-plugin-security": "
|
|
113
|
+
"eslint-plugin-promise": "7.3.0",
|
|
114
|
+
"eslint-plugin-regexp": "3.1.0",
|
|
115
|
+
"eslint-plugin-security": "4.0.0",
|
|
116
116
|
"eslint-plugin-unused-imports": "4.4.1",
|
|
117
117
|
"husky": "9.1.7",
|
|
118
|
-
"jest": "30.
|
|
118
|
+
"jest": "30.4.2",
|
|
119
119
|
"jest-retries": "1.0.1",
|
|
120
120
|
"node-mocks-http": "1.17.2",
|
|
121
121
|
"rimraf": "6.1.3",
|
|
@@ -124,31 +124,31 @@
|
|
|
124
124
|
"using-temporary-files": "2.2.1"
|
|
125
125
|
},
|
|
126
126
|
"dependencies": {
|
|
127
|
-
"@apidevtools/json-schema-ref-parser": "
|
|
127
|
+
"@apidevtools/json-schema-ref-parser": "15.3.5",
|
|
128
128
|
"@hapi/accept": "6.0.3",
|
|
129
129
|
"@types/json-schema": "7.0.15",
|
|
130
|
-
"ajv": "8.
|
|
130
|
+
"ajv": "8.20.0",
|
|
131
131
|
"chokidar": "5.0.0",
|
|
132
132
|
"commander": "14.0.3",
|
|
133
133
|
"debug": "4.4.3",
|
|
134
|
-
"fs-extra": "11.3.
|
|
134
|
+
"fs-extra": "11.3.5",
|
|
135
135
|
"http-terminator": "3.2.0",
|
|
136
136
|
"js-yaml": "4.1.1",
|
|
137
|
-
"json-schema-faker": "0.6.
|
|
137
|
+
"json-schema-faker": "0.6.2",
|
|
138
|
+
"jsonpath-plus": "10.4.0",
|
|
138
139
|
"jsonwebtoken": "9.0.3",
|
|
139
|
-
"koa": "3.2.
|
|
140
|
+
"koa": "3.2.1",
|
|
140
141
|
"koa-bodyparser": "4.4.1",
|
|
141
142
|
"koa-proxies": "0.12.4",
|
|
142
143
|
"koa2-swagger-ui": "5.12.0",
|
|
143
144
|
"node-fetch": "3.3.2",
|
|
144
145
|
"open": "11.0.0",
|
|
145
|
-
"
|
|
146
|
-
"
|
|
147
|
-
"
|
|
148
|
-
"prettier": "3.8.1",
|
|
146
|
+
"posthog-node": "5.35.5",
|
|
147
|
+
"precinct": "13.0.0",
|
|
148
|
+
"prettier": "3.8.3",
|
|
149
149
|
"recast": "0.23.11",
|
|
150
|
-
"tsx": "
|
|
151
|
-
"typescript": "6.0.
|
|
150
|
+
"tsx": "4.22.3",
|
|
151
|
+
"typescript": "6.0.3"
|
|
152
152
|
},
|
|
153
153
|
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e",
|
|
154
154
|
"resolutions": {
|