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
package/README.md
CHANGED
|
@@ -1,34 +1,57 @@
|
|
|
1
1
|
<div align="center" markdown="1">
|
|
2
2
|
|
|
3
|
-
<img src="./counterfact.svg" alt="Counterfact" border=0>
|
|
3
|
+
<h1><img src="./counterfact.svg" alt="Counterfact" border=0></h1>
|
|
4
4
|
|
|
5
5
|
<br>
|
|
6
6
|
|
|
7
|
-
 [ [](https://coveralls.io/github/pmcelhaney/counterfact) 
|
|
8
8
|
|
|
9
9
|
</div>
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
<div align="center" markdown="1">
|
|
12
|
+
<h2>Mock servers work—until you need state, failures, or control mid-run.</h2>
|
|
13
|
+
</div
|
|
14
|
+
|
|
15
|
+
Static responses aren’t enough. There’s no shared state. You can’t inject failures. You can’t test real workflows.<br>
|
|
16
|
+
Mock servers make it easy to get started, but hard to keep going.<br>
|
|
17
|
+
Counterfact is an API simulator without those limits.
|
|
18
|
+
|
|
19
|
+
Point it at an [OpenAPI](https://www.openapis.org) document and get a live, stateful API in seconds.
|
|
20
|
+
- Type-safe TypeScript handlers for every endpoint
|
|
21
|
+
- Hot reloading as you edit
|
|
22
|
+
- Shared state across routes
|
|
23
|
+
- **A built-in REPL to control behavior at runtime**
|
|
24
|
+
- Optional proxying to real backends
|
|
25
|
+
|
|
26
|
+
Flexbile for humans. Stable for [AI agents](https://github.com/counterfact/api-simulator/blob/main/docs/patterns/ai-assisted-implementation.md).
|
|
27
|
+
|
|
28
|
+
You’re in control—without restarting.
|
|
29
|
+
|
|
30
|
+
For a *frontend developer* waiting on a backend,<br>
|
|
31
|
+
a *test engineer* who needs clean, reproducible state,<br>
|
|
32
|
+
or an *AI agent* that needs a stable API
|
|
33
|
+
|
|
34
|
+
Real enough to be useful. Fake enough to be usable.
|
|
35
|
+
|
|
12
36
|
|
|
13
|
-
|
|
37
|
+
## Try it now
|
|
14
38
|
|
|
15
39
|
```sh
|
|
16
40
|
npx counterfact@latest https://petstore3.swagger.io/api/v3/openapi.json api
|
|
17
41
|
```
|
|
18
42
|
|
|
43
|
+
> Starts a local server with a live REPL to inspect and control API behavior
|
|
19
44
|
> Requires Node ≥ 22.0.0
|
|
20
45
|
|
|
21
46
|
## Go deeper
|
|
22
47
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
| [FAQ](./docs/faq.md) | State, types, regeneration |
|
|
31
|
-
| [Petstore example](https://github.com/counterfact/example-petstore) | Full working example |
|
|
48
|
+
- [Getting started](./docs/getting-started.md) – Detailed walkthrough with state, REPL, and proxy
|
|
49
|
+
- [Patterns](./docs/patterns/index.md) – How Counterfact transforms your workflow
|
|
50
|
+
- [Example repo](https://github.com/counterfact/example-petstore) – Using Counterfact to implement the Swagger Petstore
|
|
51
|
+
- [How it compares](./docs/comparison.md) – json-server, WireMock, Prism, Microcks, MSW
|
|
52
|
+
- [Usage](./docs/usage.md) – Explore features and how to use them
|
|
53
|
+
- [Reference](./docs/reference.md) – `$` API, CLI flags, architecture
|
|
54
|
+
- [FAQ](./docs/faq.md) – State, types, regeneration
|
|
32
55
|
|
|
33
56
|
<div align="center" markdown="1">
|
|
34
57
|
|
package/bin/README.md
CHANGED
|
@@ -6,7 +6,19 @@ This directory contains the executable script that is run when a developer invok
|
|
|
6
6
|
|
|
7
7
|
| File | Description |
|
|
8
8
|
|---|---|
|
|
9
|
-
| `counterfact.js` |
|
|
9
|
+
| `counterfact.js` | Thin bootstrap: enforces minimum Node version, probes for native TypeScript execution, then delegates to `src/cli/run.ts` (or `dist/cli/run.js`) |
|
|
10
|
+
| `taglines.txt` | One-per-line list of random taglines shown in the startup banner |
|
|
11
|
+
|
|
12
|
+
## Architecture
|
|
13
|
+
|
|
14
|
+
Most of the CLI logic lives in **`src/cli/`** as TypeScript:
|
|
15
|
+
|
|
16
|
+
| Module | Description |
|
|
17
|
+
|---|---|
|
|
18
|
+
| `src/cli/run.ts` | Commander program setup, `main()` action handler, and the `runCli()` entry point |
|
|
19
|
+
| `src/cli/banner.ts` | Startup banner utilities: `padTagLine`, `createWatchMessage`, `createIntroduction` |
|
|
20
|
+
| `src/cli/check-for-updates.ts` | npm update check: `isOutdated`, `checkForUpdates` |
|
|
21
|
+
| `src/cli/telemetry.ts` | PostHog telemetry: `isTelemetryEnabled`, `sendTelemetry` |
|
|
10
22
|
|
|
11
23
|
## How It Works
|
|
12
24
|
|
|
@@ -14,19 +26,32 @@ This directory contains the executable script that is run when a developer invok
|
|
|
14
26
|
npx counterfact@latest openapi.yaml ./api [options]
|
|
15
27
|
│
|
|
16
28
|
▼
|
|
17
|
-
|
|
18
|
-
│
|
|
19
|
-
│
|
|
20
|
-
│ 1.
|
|
21
|
-
│ 2.
|
|
22
|
-
│ 3.
|
|
23
|
-
│
|
|
24
|
-
│
|
|
25
|
-
|
|
26
|
-
│
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
┌────────────────────────────────────────────────┐
|
|
30
|
+
│ bin/counterfact.js (thin bootstrap) │
|
|
31
|
+
│ │
|
|
32
|
+
│ 1. Enforce minimum Node.js version │
|
|
33
|
+
│ 2. Probe native TypeScript execution │
|
|
34
|
+
│ 3. Import runCli() from src/cli/run.ts │
|
|
35
|
+
│ (or dist/cli/run.js when compiled) │
|
|
36
|
+
│ 4. Call runCli(process.argv) │
|
|
37
|
+
└────────────────────────────────────────────────┘
|
|
38
|
+
│
|
|
39
|
+
▼
|
|
40
|
+
┌────────────────────────────────────────────────┐
|
|
41
|
+
│ src/cli/run.ts (all CLI logic) │
|
|
42
|
+
│ │
|
|
43
|
+
│ 1. Read version from package.json │
|
|
44
|
+
│ 2. Read taglines from bin/taglines.txt │
|
|
45
|
+
│ 3. Fire telemetry (if enabled) │
|
|
46
|
+
│ 4. Parse args (Commander) │
|
|
47
|
+
│ 5. Load counterfact.yaml │
|
|
48
|
+
│ 6. Merge config + args │
|
|
49
|
+
│ 7. Resolve paths │
|
|
50
|
+
│ 8. Build Config object │
|
|
51
|
+
│ 9. Run migrations if old layout detected │
|
|
52
|
+
│ 10. Print startup banner │
|
|
53
|
+
│ 11. Call start(config) from src/app.ts │
|
|
54
|
+
└────────────────────────────────────────────────┘
|
|
30
55
|
```
|
|
31
56
|
|
|
32
57
|
### Key CLI Options
|