counterfact 0.10.3 → 0.12.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 (36) hide show
  1. package/.eslintrc.cjs +8 -1
  2. package/CHANGELOG.md +12 -0
  3. package/CONTRIBUTING.md +30 -0
  4. package/README.md +17 -41
  5. package/_config.yaml +12 -0
  6. package/_includes/head-custom-google-analytics.html +13 -0
  7. package/bin/counterfact.js +12 -3
  8. package/docs/usage.md +125 -441
  9. package/jest.config.json +1 -1
  10. package/package.json +5 -5
  11. package/src/client/index.html +43 -0
  12. package/src/{context-registry.js → server/context-registry.js} +0 -0
  13. package/src/{counterfact.js → server/counterfact.js} +9 -3
  14. package/src/{dispatcher.js → server/dispatcher.js} +0 -0
  15. package/src/{koa-middleware.js → server/koa-middleware.js} +0 -0
  16. package/src/{module-loader.js → server/module-loader.js} +0 -0
  17. package/src/{registry.js → server/registry.js} +0 -0
  18. package/src/{response-builder.js → server/response-builder.js} +0 -0
  19. package/src/server/start.js +96 -0
  20. package/src/{tools.js → server/tools.js} +0 -0
  21. package/src/{transpiler.js → server/transpiler.js} +0 -0
  22. package/src/typescript-generator/init.js +7 -0
  23. package/src/typescript-generator/specification.js +1 -1
  24. package/src/{read-file.js → util/read-file.js} +0 -0
  25. package/test/{context-registry.test.js → server/context-registry.test.js} +4 -1
  26. package/test/{counterfact.test.js → server/counterfact.test.js} +2 -3
  27. package/test/{dispatcher.test.js → server/dispatcher.test.js} +3 -3
  28. package/test/{koa-middleware.test.js → server/koa-middleware.test.js} +4 -4
  29. package/test/{module-loader.test.js → server/module-loader.test.js} +4 -5
  30. package/test/{registry.test.js → server/registry.test.js} +1 -1
  31. package/test/{response-builder.test.js → server/response-builder.test.js} +1 -1
  32. package/test/server/start.test.js +10 -0
  33. package/test/{tools.test.js → server/tools.test.js} +1 -1
  34. package/test/{transpiler.test.js → server/transpiler.test.js} +2 -3
  35. package/docs/koa-plugin.md +0 -35
  36. package/src/start.js +0 -61
package/.eslintrc.cjs CHANGED
@@ -36,7 +36,14 @@ const rules = {
36
36
  };
37
37
 
38
38
  module.exports = {
39
- ignorePatterns: ["/node_modules/", "/coverage/", "/reports/", "/out/"],
39
+ ignorePatterns: [
40
+ "/node_modules/",
41
+ "/coverage/",
42
+ "/reports/",
43
+ "/out/",
44
+ "_includes",
45
+ ".stryker-tmp",
46
+ ],
40
47
 
41
48
  extends: ["hardcore", "hardcore/ts", "hardcore/node"],
42
49
 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # counterfact
2
2
 
3
+ ## 0.12.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 1a622d2: a REPL allows you to interact with the server's context programatically
8
+
9
+ ## 0.11.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 5a7e12b: --open opens the browser to a landing page with pointers to the code, Swagger, and the docs
14
+
3
15
  ## 0.10.3
4
16
 
5
17
  ### Patch Changes
@@ -0,0 +1,30 @@
1
+ # Contributing
2
+
3
+ ## Where I need help
4
+
5
+ - **Feedback:** First and foremost, send me feedback. What makes sense? What's confusing? What's missing? What's broken? [Send me an email](pmcelhaney@gmail.com), [open an issue](https://github.com/pmcelhaney/counterfact/issues/new), or [start a discussion](https://github.com/pmcelhaney/counterfact/discussions).
6
+ - **Documentation:** There are probably typos in this very document. Please send PRs, large and small. You can do it right from your browser. If you're viewing this document in Github, click the pencil button in the top right corner.
7
+ - **Graphic design:** I'm terrible at graphic design. I know good design when I see it, and I can tell you why it's good, but struggle with the creative process. Whether it's building a web site, designing a logo, brainstorming on a GUI, or fixing up some ugly Markdown code, I'll take whatever help I can get!
8
+ - **Tests:** Test coverage is pretty good, but there are gaps. Filling those gaps is an easy way to gain some familiarity with the codebase.
9
+ - **Code Generation:** As of this writing, the code that writes the code works okay, but it's kind of sloppy. Instead of printing strings of source code directly, I'd like to refactor everything to build ASTs. I'd also like to bring in [json-schema-to-typescript](https://github.com/bcherny/json-schema-to-typescript), which is more accurate than my hand-rolled MVP.
10
+ - **Convert to TypeScript**: While Counterfact generates and runs TypeScript, ironically the code itself is in JavaScript. That's partly because running the unit tests in Jest requires [--experimental-vm-modules](https://jestjs.io/docs/ecmascript-modules). Getting Jest working with that and TypeScript at the same time was too much trouble when the project was getting off the ground.
11
+ - **New features and bug fixes:** See the [issues list](https://github.com/pmcelhaney/counterfact/issues). If you plan on working on something, please add a comment and/or assign yourself.
12
+ - **Spread the word!** If you find this project useful, please let others know about it. Share it in your team Slack, on social media, etc.
13
+
14
+ ## Development
15
+
16
+ This is a pretty straightforward NodeJS project.
17
+
18
+ ```sh
19
+ git clone git@github.com:pmcelhaney/counterfact.git
20
+ cd counterfact
21
+ npm install
22
+ npm lint
23
+ npm test
24
+ ```
25
+
26
+ The [code generator](./src/typescript-generator/README.md) is under `src/typescript-generator`. The server is directly under `src`. I'm planning to move it to `src/server`.
27
+
28
+ Testing and linting changes is important, but at this point I'm more concerned about changing the word "I" in this page to "we", so don't hesitate to create a PR, even it's not "finished".
29
+
30
+ Thanks in advance!
package/README.md CHANGED
@@ -1,35 +1,21 @@
1
1
 
2
- <div align="right">
3
2
 
4
- [![Coverage Status](https://coveralls.io/repos/github/pmcelhaney/counterfact/badge.svg)](https://coveralls.io/github/pmcelhaney/counterfact) [![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fpmcelhaney%2Fcounterfact%2Fmain)](https://dashboard.stryker-mutator.io/reports/github.com/pmcelhaney/counterfact/main) ![MIT License](https://img.shields.io/badge/license-MIT-blue)
5
-
6
-
7
- </div>
8
-
9
- <div align="center">
3
+ <div align="center" markdown="1">
10
4
 
11
5
  # Counterfact
12
6
 
13
7
  _Front end development without back end headaches_
14
8
 
9
+ [Quick Start](#quick-start) | [Documentation](./docs/usage.md) | [Contributing](CONTRIBUTING.md)
15
10
 
11
+ Counterfact is a stand-in REST server powered by Node, TypeScript, and OpenAPI.<br>
12
+ It enables you to write front end code and test UX flows without a complete back end.
16
13
 
17
-
18
- [Watch Demo](#watch-demo) | [Quick Start](#quick-start) | [Documentation](#documentation) | [Support](#support)
19
-
20
-
21
-
22
- </div>
23
-
24
-
25
-
26
- <div align="center">
27
- Counterfact is a stand-in REST server powered by Node, TypeScript, and OpenAPI.<br>It simulates complex, stateful back end behavior without running the whole stack.
28
14
  </div>
29
15
 
30
16
  <br>
31
17
 
32
- <table align="center" cols="2">
18
+ <table align="center" cols="2" markdown="1">
33
19
 
34
20
  <tr>
35
21
  <td>
@@ -52,33 +38,23 @@ Counterfact is a stand-in REST server powered by Node, TypeScript, and OpenAPI.<
52
38
 
53
39
  </table>
54
40
 
41
+ <div align="center" markdown="1">
55
42
 
56
- <div id="watch-demo" align="center">
43
+ [![Coverage Status](https://coveralls.io/repos/github/pmcelhaney/counterfact/badge.svg)](https://coveralls.io/github/pmcelhaney/counterfact) [![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fpmcelhaney%2Fcounterfact%2Fmain)](https://dashboard.stryker-mutator.io/reports/github.com/pmcelhaney/counterfact/main) ![MIT License](https://img.shields.io/badge/license-MIT-blue)
57
44
 
58
- ```
59
45
 
60
-
61
-
62
-
63
-
64
- video will go here
65
-
66
-
67
-
68
-
69
- ```
70
46
  </div>
71
47
 
48
+ <h2 id="quick-start">Got 60 Seconds? Try me!</h2>
72
49
 
73
- ## Quick Start
74
-
75
- Try it now with one command. The only prequisite is Node 16+.
50
+ Copy the following command into your terminal. The only prerequisite is Node 16+.
76
51
 
77
52
  ```sh copy
78
53
  npx counterfact@latest https://petstore3.swagger.io/api/v3/openapi.json api --open
79
54
  ```
80
55
 
81
- ### What does it do?
56
+ <details>
57
+ <summary>What does that command do?</summary>
82
58
 
83
59
  1. installs the `@latest` version of `counterfact`
84
60
  2. reads an [OpenAPI 3](https://oai.github.io/Documentation/) document (`https://petstore3.swagger.io/api/v3/openapi.json`)
@@ -86,14 +62,14 @@ npx counterfact@latest https://petstore3.swagger.io/api/v3/openapi.json api --op
86
62
  4. starts a server which implements the API
87
63
  5. opens your browser to [Swagger UI](https://swagger.io/tools/swagger-ui/) (`--open`)
88
64
 
89
- You can use Swagger to try out the auto-generated API. Out of the box, it returns random responses using metadata from the OpenAPI document. You can edit the files under `./api/paths` to add more realistic behavior. There's no need to restart the server.
90
-
65
+ You can use Swagger to try out the auto-generated API. Out of the box, it returns random responses using metadata from the OpenAPI document. Edit the files under `./api/paths` to add more realistic behavior. There's no need to restart the server.
91
66
 
92
- ## Documentation
67
+ </details>
93
68
 
94
- Coming soon!
69
+ <br>
95
70
 
71
+ 📗 See [Usage](./docs/usage.md) for detailed documentation.
96
72
 
97
- ## Support
73
+ ---
98
74
 
99
- Counterfact is brand new as of October 3, 2022. Please send feedback / questions to pmcelhaney@gmail.com or [create a new issue](https://github.com/pmcelhaney/counterfact/issues/new). If you like what you see, please give this project a star!
75
+ Counterfact is brand new as of October 5, 2022. Please send feedback / questions to pmcelhaney@gmail.com or [create a new issue](https://github.com/pmcelhaney/counterfact/issues/new). If you like what you see, please give this project a star!
package/_config.yaml ADDED
@@ -0,0 +1,12 @@
1
+ remote_theme: pages-themes/dinky@v0.2.0
2
+ plugins:
3
+ - jekyll-remote-theme
4
+ - jekyll-seo-tag
5
+ markdown: GFM
6
+ theme: jekyll-theme-cayman
7
+ title: Counterfact
8
+ description: OpenAPI to TypeScript generator and mock server
9
+ url: https://counterfact.dev
10
+ author: Partrick McElhaney
11
+ google_analytics: G-2QHMETF62T
12
+
@@ -0,0 +1,13 @@
1
+ <script
2
+ async
3
+ src="https://www.googletagmanager.com/gtag/js?id={{ site.google_analytics }}"
4
+ ></script>
5
+ <script>
6
+ window.dataLayer = window.dataLayer || [];
7
+ function gtag() {
8
+ dataLayer.push(arguments);
9
+ }
10
+ gtag("js", new Date());
11
+
12
+ gtag("config", "{{ site.google_analytics }}");
13
+ </script>
@@ -1,12 +1,13 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import nodePath from "node:path";
4
+ import repl from "node:repl";
4
5
 
5
6
  import { program } from "commander";
6
7
  import open from "open";
7
8
 
8
9
  import { generate } from "../src/typescript-generator/generate.js";
9
- import { start } from "../src/start.js";
10
+ import { start } from "../src/server/start.js";
10
11
 
11
12
  const DEFAULT_PORT = 3100;
12
13
 
@@ -23,7 +24,7 @@ async function main(source, destination) {
23
24
  const startServer = options.server || includeSwagger;
24
25
 
25
26
  if (startServer) {
26
- await start({
27
+ const { contextRegistry } = await start({
27
28
  basePath,
28
29
  port: options.port,
29
30
  openApiPath: source,
@@ -33,10 +34,18 @@ async function main(source, destination) {
33
34
  process.stdout.write(
34
35
  `API is running at http://localhost:${options.port}.\n`
35
36
  );
37
+
38
+ process.stdout.write(
39
+ '\n\nStarting REPL... (start with `const context = loadContext("/")`)\n'
40
+ );
41
+
42
+ const replServer = repl.start("> ");
43
+
44
+ replServer.context.loadContext = (path) => contextRegistry.find(path);
36
45
  }
37
46
 
38
47
  if (openBrowser) {
39
- await open(`http://localhost:${options.port}/counterfact/swagger`);
48
+ await open(`http://localhost:${options.port}/counterfact`);
40
49
  }
41
50
  }
42
51