counterfact 2.8.1 → 2.10.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 +36 -13
- package/bin/README.md +39 -14
- package/bin/counterfact.js +18 -547
- package/bin/ts-loader.mjs +1 -0
- package/dist/api-runner.js +202 -0
- package/dist/app.js +72 -138
- package/dist/cli/banner.js +81 -0
- package/dist/cli/check-for-updates.js +45 -0
- package/dist/cli/run.js +304 -0
- package/dist/cli/telemetry.js +50 -0
- package/dist/migrate/paths-to-routes.js +1 -0
- package/dist/migrate/update-route-types.js +2 -1
- package/dist/msw.js +78 -0
- package/dist/repl/raw-http-client.js +3 -1
- package/dist/repl/repl.js +228 -60
- package/dist/server/counterfact-types/generic-response-builder.ts +1 -2
- package/dist/server/counterfact-types/open-api-parameters.ts +3 -0
- package/dist/server/determine-module-kind.js +1 -0
- package/dist/server/dispatcher.js +45 -2
- package/dist/server/file-discovery.js +1 -0
- package/dist/server/module-loader.js +8 -0
- package/dist/server/request-validator.js +42 -1
- package/dist/server/transpiler.js +1 -0
- package/dist/server/{admin-api-middleware.js → web-server/admin-api-middleware.js} +19 -9
- package/dist/server/web-server/create-koa-app.js +68 -0
- package/dist/server/web-server/openapi-middleware.js +34 -0
- package/dist/server/{koa-middleware.js → web-server/routes-middleware.js} +11 -8
- package/dist/typescript-generator/code-generator.js +2 -1
- package/dist/typescript-generator/coder.js +4 -2
- package/dist/typescript-generator/operation-coder.js +4 -4
- package/dist/typescript-generator/operation-type-coder.js +15 -14
- package/dist/typescript-generator/parameter-export-type-coder.js +2 -2
- package/dist/typescript-generator/parameters-type-coder.js +3 -3
- package/dist/typescript-generator/prune.js +1 -0
- package/dist/typescript-generator/repository.js +1 -0
- package/dist/typescript-generator/response-type-coder.js +7 -6
- package/dist/typescript-generator/responses-type-coder.js +3 -3
- package/dist/typescript-generator/scenario-file-generator.js +1 -0
- package/dist/typescript-generator/schema-coder.js +2 -2
- package/dist/typescript-generator/schema-type-coder.js +7 -5
- package/dist/typescript-generator/script.js +58 -3
- package/dist/util/ensure-directory-exists.js +1 -0
- package/dist/util/load-config-file.js +2 -2
- package/dist/util/read-file.js +16 -2
- package/dist/util/runtime-can-execute-erasable-ts.js +1 -0
- package/package.json +3 -2
- package/dist/server/create-koa-app.js +0 -65
- package/dist/server/openapi-middleware.js +0 -48
|
@@ -2,6 +2,7 @@ import { mkdtempSync, writeFileSync, rmSync } from "node:fs";
|
|
|
2
2
|
import { tmpdir } from "node:os";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { pathToFileURL } from "node:url";
|
|
5
|
+
/* eslint-disable security/detect-non-literal-fs-filename -- runtime probe only writes fixed filenames in a fresh temporary directory. */
|
|
5
6
|
/**
|
|
6
7
|
* Probes the current Node.js runtime to determine whether it can execute
|
|
7
8
|
* TypeScript source files directly (via `--experimental-strip-types` or
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "counterfact",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.10.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",
|
|
@@ -76,7 +76,8 @@
|
|
|
76
76
|
"lint:quickfix": "eslint --fix . eslint --fix demo-ts --rule=\"import/namespace: 0,etc/no-deprecated:0,import/no-cycle:0,no-explicit-type-exports/no-explicit-type-exports:0,import/no-deprecated:0,import/no-self-import:0,import/default:0,import/no-named-as-default:0\" --ignore-pattern dist --ignore-pattern out",
|
|
77
77
|
"go:petstore": "yarn build && yarn counterfact https://petstore3.swagger.io/api/v3/openapi.json out",
|
|
78
78
|
"go:petstore2": "yarn build && yarn counterfact https://petstore.swagger.io/v2/swagger.json out",
|
|
79
|
-
"go:example": "yarn build && node ./bin/counterfact.js ./test/fixtures/openapi
|
|
79
|
+
"go:example": "yarn build && node ./bin/counterfact.js ./test/fixtures/openapi/example.yaml out",
|
|
80
|
+
"go:multiple-apis": "yarn build && node ./bin/counterfact.js --config ./test/fixtures/config/multiple-apis.yaml",
|
|
80
81
|
"counterfact": "./bin/counterfact.js",
|
|
81
82
|
"postinstall": "patch-package"
|
|
82
83
|
},
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import createDebug from "debug";
|
|
2
|
-
import Koa from "koa";
|
|
3
|
-
import bodyParser from "koa-bodyparser";
|
|
4
|
-
import { koaSwagger } from "koa2-swagger-ui";
|
|
5
|
-
import { adminApiMiddleware } from "./admin-api-middleware.js";
|
|
6
|
-
import { routesMiddleware } from "./koa-middleware.js";
|
|
7
|
-
import { openapiMiddleware } from "./openapi-middleware.js";
|
|
8
|
-
const debug = createDebug("counterfact:server:create-koa-app");
|
|
9
|
-
/**
|
|
10
|
-
* Builds and configures the Koa application with all built-in middleware.
|
|
11
|
-
*
|
|
12
|
-
* The middleware stack (in order) is:
|
|
13
|
-
* 1. OpenAPI document serving at `/counterfact/openapi`
|
|
14
|
-
* 2. Swagger UI at `/counterfact/swagger`
|
|
15
|
-
* 3. Admin API (when `config.startAdminApi` is `true`) at `/_counterfact/api/`
|
|
16
|
-
* 4. Redirect `/counterfact` → `/counterfact/swagger`
|
|
17
|
-
* 5. Body parser
|
|
18
|
-
* 6. JSON serialisation of object bodies
|
|
19
|
-
* 7. Route-dispatching middleware
|
|
20
|
-
*
|
|
21
|
-
* @param config - Server configuration.
|
|
22
|
-
* @param dispatcher - The Dispatcher used to build the route-dispatching middleware.
|
|
23
|
-
* @param registry - The route Registry used to build the admin API middleware.
|
|
24
|
-
* @param contextRegistry - The ContextRegistry used to build the admin API middleware.
|
|
25
|
-
* @returns A configured Koa application (not yet listening).
|
|
26
|
-
*/
|
|
27
|
-
export function createKoaApp({ config, contextRegistry, dispatcher, registry, }) {
|
|
28
|
-
const app = new Koa();
|
|
29
|
-
app.use(openapiMiddleware([
|
|
30
|
-
{
|
|
31
|
-
path: config.openApiPath,
|
|
32
|
-
baseUrl: `//localhost:${config.port}${config.routePrefix}`,
|
|
33
|
-
},
|
|
34
|
-
]));
|
|
35
|
-
app.use(koaSwagger({
|
|
36
|
-
routePrefix: "/counterfact/swagger",
|
|
37
|
-
swaggerOptions: {
|
|
38
|
-
url: "/counterfact/openapi",
|
|
39
|
-
},
|
|
40
|
-
}));
|
|
41
|
-
if (config.startAdminApi) {
|
|
42
|
-
app.use(adminApiMiddleware(registry, contextRegistry, config));
|
|
43
|
-
}
|
|
44
|
-
debug("basePath: %s", config.basePath);
|
|
45
|
-
app.use(async (ctx, next) => {
|
|
46
|
-
if (ctx.URL.pathname === "/counterfact") {
|
|
47
|
-
ctx.redirect("/counterfact/swagger");
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
await next();
|
|
51
|
-
});
|
|
52
|
-
app.use(bodyParser());
|
|
53
|
-
app.use(async (ctx, next) => {
|
|
54
|
-
await next();
|
|
55
|
-
if (ctx.body !== null &&
|
|
56
|
-
ctx.body !== undefined &&
|
|
57
|
-
typeof ctx.body === "object" &&
|
|
58
|
-
!Buffer.isBuffer(ctx.body)) {
|
|
59
|
-
ctx.body = JSON.stringify(ctx.body, null, 2);
|
|
60
|
-
ctx.type = "application/json";
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
app.use(routesMiddleware(dispatcher, config));
|
|
64
|
-
return app;
|
|
65
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { bundle } from "@apidevtools/json-schema-ref-parser";
|
|
2
|
-
import { dump } from "js-yaml";
|
|
3
|
-
/**
|
|
4
|
-
* Returns a Koa middleware that serves bundled OpenAPI documents as YAML.
|
|
5
|
-
*
|
|
6
|
-
* When `documents` has exactly one entry the document is served at
|
|
7
|
-
* `/counterfact/openapi` (backward-compatible behaviour).
|
|
8
|
-
*
|
|
9
|
-
* When `documents` has more than one entry each document is served at
|
|
10
|
-
* `/counterfact/openapi/{id}` where `id` comes from the corresponding entry.
|
|
11
|
-
*
|
|
12
|
-
* Every served document is augmented with a `servers` entry (OpenAPI 3.x) and
|
|
13
|
-
* a `host` field (OpenAPI 2.x / Swagger) so that the Swagger UI can send
|
|
14
|
-
* requests to the running Counterfact instance.
|
|
15
|
-
*
|
|
16
|
-
* @param documents - Array of document descriptors. Each entry must provide
|
|
17
|
-
* `path` (file path or URL to the source OpenAPI document) and `baseUrl`
|
|
18
|
-
* (the base URL to inject, e.g. `"//localhost:3100/api"`). An optional `id`
|
|
19
|
-
* string is used to build the per-document URL when more than one document
|
|
20
|
-
* is present.
|
|
21
|
-
* @returns A Koa middleware function.
|
|
22
|
-
*/
|
|
23
|
-
export function openapiMiddleware(documents) {
|
|
24
|
-
return async (ctx, next) => {
|
|
25
|
-
let matched;
|
|
26
|
-
if (documents.length === 1) {
|
|
27
|
-
if (ctx.URL.pathname === "/counterfact/openapi") {
|
|
28
|
-
matched = documents[0];
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
matched = documents.find((doc) => ctx.URL.pathname === `/counterfact/openapi/${doc.id}`);
|
|
33
|
-
}
|
|
34
|
-
if (matched) {
|
|
35
|
-
const openApiDocument = (await bundle(matched.path));
|
|
36
|
-
openApiDocument.servers ??= [];
|
|
37
|
-
openApiDocument.servers.unshift({
|
|
38
|
-
description: "Counterfact",
|
|
39
|
-
url: matched.baseUrl,
|
|
40
|
-
});
|
|
41
|
-
// OpenApi 2 support:
|
|
42
|
-
openApiDocument.host = matched.baseUrl;
|
|
43
|
-
ctx.body = dump(openApiDocument);
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
await next();
|
|
47
|
-
};
|
|
48
|
-
}
|