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.
Files changed (48) hide show
  1. package/README.md +36 -13
  2. package/bin/README.md +39 -14
  3. package/bin/counterfact.js +18 -547
  4. package/bin/ts-loader.mjs +1 -0
  5. package/dist/api-runner.js +202 -0
  6. package/dist/app.js +72 -138
  7. package/dist/cli/banner.js +81 -0
  8. package/dist/cli/check-for-updates.js +45 -0
  9. package/dist/cli/run.js +304 -0
  10. package/dist/cli/telemetry.js +50 -0
  11. package/dist/migrate/paths-to-routes.js +1 -0
  12. package/dist/migrate/update-route-types.js +2 -1
  13. package/dist/msw.js +78 -0
  14. package/dist/repl/raw-http-client.js +3 -1
  15. package/dist/repl/repl.js +228 -60
  16. package/dist/server/counterfact-types/generic-response-builder.ts +1 -2
  17. package/dist/server/counterfact-types/open-api-parameters.ts +3 -0
  18. package/dist/server/determine-module-kind.js +1 -0
  19. package/dist/server/dispatcher.js +45 -2
  20. package/dist/server/file-discovery.js +1 -0
  21. package/dist/server/module-loader.js +8 -0
  22. package/dist/server/request-validator.js +42 -1
  23. package/dist/server/transpiler.js +1 -0
  24. package/dist/server/{admin-api-middleware.js → web-server/admin-api-middleware.js} +19 -9
  25. package/dist/server/web-server/create-koa-app.js +68 -0
  26. package/dist/server/web-server/openapi-middleware.js +34 -0
  27. package/dist/server/{koa-middleware.js → web-server/routes-middleware.js} +11 -8
  28. package/dist/typescript-generator/code-generator.js +2 -1
  29. package/dist/typescript-generator/coder.js +4 -2
  30. package/dist/typescript-generator/operation-coder.js +4 -4
  31. package/dist/typescript-generator/operation-type-coder.js +15 -14
  32. package/dist/typescript-generator/parameter-export-type-coder.js +2 -2
  33. package/dist/typescript-generator/parameters-type-coder.js +3 -3
  34. package/dist/typescript-generator/prune.js +1 -0
  35. package/dist/typescript-generator/repository.js +1 -0
  36. package/dist/typescript-generator/response-type-coder.js +7 -6
  37. package/dist/typescript-generator/responses-type-coder.js +3 -3
  38. package/dist/typescript-generator/scenario-file-generator.js +1 -0
  39. package/dist/typescript-generator/schema-coder.js +2 -2
  40. package/dist/typescript-generator/schema-type-coder.js +7 -5
  41. package/dist/typescript-generator/script.js +58 -3
  42. package/dist/util/ensure-directory-exists.js +1 -0
  43. package/dist/util/load-config-file.js +2 -2
  44. package/dist/util/read-file.js +16 -2
  45. package/dist/util/runtime-can-execute-erasable-ts.js +1 -0
  46. package/package.json +3 -2
  47. package/dist/server/create-koa-app.js +0 -65
  48. 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
- ![MIT License](https://img.shields.io/badge/license-MIT-blue) [![TypeScript](./typescript-badge.png)](https://github.com/ellerbrock/typescript-badges/) [![Coverage Status](https://coveralls.io/repos/github/pmcelhaney/counterfact/badge.svg)](https://coveralls.io/github/pmcelhaney/counterfact)
7
+ ![MIT License](https://img.shields.io/badge/license-MIT-blue) [![Coverage Status](https://coveralls.io/repos/github/counterfact/api-simulator/badge.svg)](https://coveralls.io/github/pmcelhaney/counterfact) ![friction 0%](https://img.shields.io/badge/friction-0%25-brightgreen)
8
8
 
9
9
  </div>
10
10
 
11
- You've used mock servers. You know where they stop being useful: static responses, no shared state, no way to inject a failure mid-run, no control without restarting. Counterfact picks up where they leave off.
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
- Point it at an OpenAPI spec and it generates TypeScript handlers for every endpoint—type-safe, hot-reloading, sharing state across routes. A built-in REPL gives you a live control surface: seed data, trigger error conditions, proxy individual routes to a real backend, all on a running server. Whether you're a frontend developer waiting on a backend, a test engineer who needs clean reproducible state, or an AI agent that needs a stable API to work against, Counterfact is the simulator that doesn't plateau.
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
- | [Getting started](./docs/getting-started.md) | Detailed walkthrough with state, REPL, and proxy |
26
- | [Usage](./docs/usage.md) | Feature index: routes, context, REPL, proxy, middleware, and more |
27
- | [Patterns](./docs/patterns/index.md) | Failures, latency, AI sandboxes, integration tests |
28
- | [Reference](./docs/reference.md) | `$` API, CLI flags, architecture |
29
- | [How it compares](./docs/comparison.md) | json-server, WireMock, Prism, Microcks, MSW |
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` | Parses command-line arguments with [Commander](https://github.com/tj/commander.js), validates inputs, and calls `counterfact()` from `src/app.ts` to start the server, code generator, file watcher, and/or REPL |
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
- counterfact.js
19
-
20
- │ 1. Parse args (Commander)
21
- │ 2. Load counterfact.yaml
22
- │ 3. Merge config + args
23
- 4. Resolve paths
24
- 5. Build Config object
25
- │ 6. Run migrations if │
26
- old layout detected │
27
- │ 7. Call start(config) │
28
- │ from src/app.ts │
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