counterfact 0.40.1 → 0.41.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.
@@ -89,7 +89,7 @@ async function main(source, destination) {
89
89
  padTagLine(taglines[Math.floor(Math.random() * taglines.length)]),
90
90
  "",
91
91
  `| API Base URL ==> ${url}`,
92
- `| Swagger UI ==> ${swaggerUrl}`,
92
+ source === "_" ? undefined : `| Swagger UI ==> ${swaggerUrl}`,
93
93
  "",
94
94
  "| Instructions ==> https://counterfact.dev/docs/usage.html",
95
95
  "| Help/feedback ==> https://github.com/pmcelhaney/counterfact/issues",
@@ -100,7 +100,9 @@ async function main(source, destination) {
100
100
  "Starting REPL, type .help for more info",
101
101
  ];
102
102
 
103
- process.stdout.write(introduction.join("\n"));
103
+ process.stdout.write(
104
+ introduction.filter((line) => line !== undefined).join("\n"),
105
+ );
104
106
 
105
107
  process.stdout.write("\n\n");
106
108
 
@@ -120,9 +122,13 @@ async function main(source, destination) {
120
122
  program
121
123
  .name("counterfact")
122
124
  .description(
123
- "Counterfact is a tool for generating a REST API from an OpenAPI document.",
125
+ "Counterfact is a tool for mocking REST APIs in development. See https://counterfact.dev for more info.",
126
+ )
127
+ .argument(
128
+ "[openapi.yaml]",
129
+ 'path or URL to OpenAPI document or "_" to run without OpenAPI',
130
+ "_",
124
131
  )
125
- .argument("<openapi.yaml>", "path or URL to OpenAPI document")
126
132
  .argument("[destination]", "path to generated code", ".")
127
133
  .option("--port <number>", "server port number", DEFAULT_PORT)
128
134
  .option("--swagger", "include swagger-ui")
@@ -131,6 +137,7 @@ program
131
137
  .option(
132
138
  "--prefix <string>",
133
139
  "base path from which routes will be served (e.g. /api/v1)",
140
+ "",
134
141
  )
135
142
  .action(main)
136
143
  // eslint-disable-next-line sonar/process-argv
@@ -1,4 +1,6 @@
1
+ import createDebug from "debug";
1
2
  import koaProxy from "koa-proxy";
3
+ const debug = createDebug("counterfact:server:create-koa-app");
2
4
  const HTTP_STATUS_CODE_OK = 200;
3
5
  function addCors(ctx, headers) {
4
6
  // Always append CORS headers, reflecting back the headers requested if any
@@ -21,9 +23,12 @@ function getAuthObject(ctx) {
21
23
  const [username, password] = user.split(":");
22
24
  return { password, username };
23
25
  }
24
- export function koaMiddleware(dispatcher, { proxyEnabled = false, proxyUrl = "", routePrefix = "" } = {}, proxy = koaProxy) {
26
+ export function koaMiddleware(dispatcher, config, proxy = koaProxy) {
25
27
  // eslint-disable-next-line max-statements
26
28
  return async function middleware(ctx, next) {
29
+ const { proxyEnabled, proxyUrl, routePrefix } = config;
30
+ debug("middleware running for path: %s", ctx.request.path);
31
+ debug("routePrefix: %s", routePrefix);
27
32
  if (!ctx.request.path.startsWith(routePrefix)) {
28
33
  // eslint-disable-next-line @typescript-eslint/no-unsafe-return
29
34
  return await next();
@@ -12,9 +12,10 @@ export function pageMiddleware(pathname, templateName, locals) {
12
12
  const pathToHandlebarsTemplate = nodePath
13
13
  .join(__dirname, `../client/${templateName}.html.hbs`)
14
14
  .replaceAll("\\", "/");
15
- debug("pathToHandlebarsTemplate: %s", pathToHandlebarsTemplate);
16
15
  const render = Handlebars.compile(await readFile(pathToHandlebarsTemplate));
17
16
  if (ctx.URL.pathname === pathname) {
17
+ debug("rendering page: %s", pathname);
18
+ debug("locals: %o", locals);
18
19
  ctx.body = render(locals);
19
20
  return;
20
21
  }
@@ -83,7 +83,6 @@ export class Transpiler extends EventTarget {
83
83
  .replaceAll("\\", "/");
84
84
  const resultWithTransformedFileExtensions = convertFileExtensionsToCjs(result);
85
85
  try {
86
- // eslint-disable-next-line total-functions/no-unsafe-readonly-mutable-assignment
87
86
  await fs.writeFile(fullDestination, resultWithTransformedFileExtensions);
88
87
  }
89
88
  catch {
@@ -22,16 +22,14 @@ export class Script {
22
22
  }
23
23
  firstUniqueName(coder) {
24
24
  for (const name of coder.names()) {
25
- if (!this.imports.has(name) && !this.exports.has(name)) {
25
+ if (!this.imports.has(name)) {
26
26
  return name;
27
27
  }
28
28
  }
29
29
  throw new Error(`could not find a unique name for ${coder.id}`);
30
30
  }
31
31
  export(coder, isType = false, isDefault = false) {
32
- const cacheKey = isDefault
33
- ? "default"
34
- : `${coder.id}@${nodePath}:${isType}`;
32
+ const cacheKey = isDefault ? "default" : `${coder.id}:${isType}`;
35
33
  if (this.cache.has(cacheKey)) {
36
34
  return this.cache.get(cacheKey);
37
35
  }
@@ -6,6 +6,14 @@ import yaml from "js-yaml";
6
6
  import { readFile } from "../util/read-file.js";
7
7
  import { Requirement } from "./requirement.js";
8
8
  const debug = createDebug("counterfact:typescript-generator:specification");
9
+ const EMPTY_OPENAPI = `{
10
+ "openapi": "3.0.0",
11
+ "info": {
12
+ "title": "Sample API",
13
+ "version": "1.0.0"
14
+ },
15
+ "paths": {}
16
+ }`;
9
17
  export class Specification {
10
18
  constructor(rootUrl) {
11
19
  this.cache = new Map();
@@ -34,7 +42,7 @@ export class Specification {
34
42
  return this.cache.get(urlOrPath);
35
43
  }
36
44
  debug("cache miss, reading file at %s", urlOrPath);
37
- const source = await readFile(urlOrPath, "utf8");
45
+ const source = urlOrPath === "_" ? EMPTY_OPENAPI : await readFile(urlOrPath, "utf8");
38
46
  debug("read file");
39
47
  debug("parsing YAML");
40
48
  const data = await yaml.load(source);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "counterfact",
3
- "version": "0.40.1",
3
+ "version": "0.41.0",
4
4
  "description": "a library for building a fake REST API for testing",
5
5
  "type": "module",
6
6
  "main": "./src/server/counterfact.js",
@@ -50,7 +50,7 @@
50
50
  "@stryker-mutator/typescript-checker": "8.2.6",
51
51
  "@swc/core": "1.5.0",
52
52
  "@swc/jest": "0.2.36",
53
- "@testing-library/dom": "10.0.0",
53
+ "@testing-library/dom": "10.1.0",
54
54
  "@types/jest": "29.5.12",
55
55
  "@types/js-yaml": "4.0.9",
56
56
  "@types/koa": "2.15.0",
@@ -65,10 +65,10 @@
65
65
  "eslint-plugin-etc": "2.0.3",
66
66
  "eslint-plugin-file-progress": "1.3.0",
67
67
  "eslint-plugin-import": "2.29.1",
68
- "eslint-plugin-jest": "28.3.0",
68
+ "eslint-plugin-jest": "28.5.0",
69
69
  "eslint-plugin-jest-dom": "5.4.0",
70
70
  "eslint-plugin-no-explicit-type-exports": "0.12.1",
71
- "eslint-plugin-unused-imports": "3.1.0",
71
+ "eslint-plugin-unused-imports": "3.2.0",
72
72
  "husky": "9.0.11",
73
73
  "jest": "29.7.0",
74
74
  "node-mocks-http": "1.14.1",