counterfact 1.0.2 → 1.1.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/bin/counterfact.js +12 -8
- package/dist/server/module-loader.js +6 -1
- package/dist/server/transpiler.js +4 -1
- package/dist/typescript-generator/generate.js +2 -1
- package/dist/typescript-generator/operation-coder.js +1 -0
- package/dist/typescript-generator/operation-type-coder.js +1 -1
- package/package.json +7 -7
package/bin/counterfact.js
CHANGED
|
@@ -100,6 +100,7 @@ async function main(source, destination) {
|
|
|
100
100
|
debug("executing the main function");
|
|
101
101
|
|
|
102
102
|
const options = program.opts();
|
|
103
|
+
|
|
103
104
|
const args = process.argv;
|
|
104
105
|
|
|
105
106
|
const destinationPath = nodePath
|
|
@@ -108,12 +109,17 @@ async function main(source, destination) {
|
|
|
108
109
|
|
|
109
110
|
const basePath = nodePath.resolve(destinationPath).replaceAll("\\", "/");
|
|
110
111
|
|
|
111
|
-
// If no
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
options.
|
|
116
|
-
|
|
112
|
+
// If no action-related option is provided, default to all options
|
|
113
|
+
|
|
114
|
+
const actions = ["repl", "serve", "watch", "generate"];
|
|
115
|
+
if (
|
|
116
|
+
!Object.keys(options).some((argument) =>
|
|
117
|
+
actions.some((action) => argument.startsWith(action)),
|
|
118
|
+
)
|
|
119
|
+
) {
|
|
120
|
+
for (const action of actions) {
|
|
121
|
+
options[action] = true;
|
|
122
|
+
}
|
|
117
123
|
}
|
|
118
124
|
|
|
119
125
|
debug("options: %o", options);
|
|
@@ -145,7 +151,6 @@ async function main(source, destination) {
|
|
|
145
151
|
options.watchTypes,
|
|
146
152
|
},
|
|
147
153
|
|
|
148
|
-
includeSwaggerUi: true,
|
|
149
154
|
openApiPath: source,
|
|
150
155
|
port: options.port,
|
|
151
156
|
proxyPaths: new Map([["", Boolean(options.proxyUrl)]]),
|
|
@@ -248,7 +253,6 @@ program
|
|
|
248
253
|
)
|
|
249
254
|
.argument("[destination]", "path to generated code", ".")
|
|
250
255
|
.option("-p, --port <number>", "server port number", DEFAULT_PORT)
|
|
251
|
-
.option("--swagger", "include swagger-ui")
|
|
252
256
|
.option("-o, --open", "open a browser")
|
|
253
257
|
.option("-g, --generate", "generate all code for both routes and types")
|
|
254
258
|
.option("--generate-types", "generate types")
|
|
@@ -31,7 +31,10 @@ export class ModuleLoader extends EventTarget {
|
|
|
31
31
|
this.contextRegistry = contextRegistry;
|
|
32
32
|
}
|
|
33
33
|
async watch() {
|
|
34
|
-
this.watcher = watch(
|
|
34
|
+
this.watcher = watch(this.basePath, CHOKIDAR_OPTIONS).on("all", (eventName, pathNameOriginal) => {
|
|
35
|
+
const JS_EXTENSIONS = ["js", "mjs", "cjs", "ts", "mts", "cts"];
|
|
36
|
+
if (!JS_EXTENSIONS.some((extension) => pathNameOriginal.endsWith(`.${extension}`)))
|
|
37
|
+
return;
|
|
35
38
|
const pathName = pathNameOriginal.replaceAll("\\", "/");
|
|
36
39
|
if (pathName.includes("$.context") && eventName === "add") {
|
|
37
40
|
process.stdout.write(`\n\n!!! The file at ${pathName} needs a minor update.\n See https://github.com/pmcelhaney/counterfact/blob/main/docs/context-change.md\n\n\n`);
|
|
@@ -106,6 +109,8 @@ export class ModuleLoader extends EventTarget {
|
|
|
106
109
|
}
|
|
107
110
|
}
|
|
108
111
|
else {
|
|
112
|
+
if (url === "/index")
|
|
113
|
+
this.registry.add("/", endpoint);
|
|
109
114
|
this.registry.add(url, endpoint);
|
|
110
115
|
}
|
|
111
116
|
}
|
|
@@ -25,7 +25,7 @@ export class Transpiler extends EventTarget {
|
|
|
25
25
|
}
|
|
26
26
|
async watch() {
|
|
27
27
|
debug("transpiler: watch");
|
|
28
|
-
this.watcher = chokidarWatch(
|
|
28
|
+
this.watcher = chokidarWatch(this.sourcePath, {
|
|
29
29
|
...CHOKIDAR_OPTIONS,
|
|
30
30
|
ignored: `${this.sourcePath}/js`,
|
|
31
31
|
ignoreInitial: false,
|
|
@@ -33,6 +33,9 @@ export class Transpiler extends EventTarget {
|
|
|
33
33
|
const transpiles = [];
|
|
34
34
|
this.watcher.on("all", async (eventName, sourcePathOriginal) => {
|
|
35
35
|
debug("transpiler event: %s <%s>", eventName, sourcePathOriginal);
|
|
36
|
+
const JS_EXTENSIONS = ["js", "mjs", "ts", "mts"];
|
|
37
|
+
if (!JS_EXTENSIONS.some((extension) => sourcePathOriginal.endsWith(`.${extension}`)))
|
|
38
|
+
return;
|
|
36
39
|
const sourcePath = sourcePathOriginal.replaceAll("\\", "/");
|
|
37
40
|
const destinationPath = sourcePath
|
|
38
41
|
.replace(this.sourcePath, this.destinationPath)
|
|
@@ -45,9 +45,10 @@ export async function generate(source, destination, generateOptions, repository
|
|
|
45
45
|
const securitySchemes = Object.values(securityRequirement?.data ?? {});
|
|
46
46
|
paths.forEach((pathDefinition, key) => {
|
|
47
47
|
debug("processing path %s", key);
|
|
48
|
+
const path = key === "/" ? "/index" : key;
|
|
48
49
|
pathDefinition.forEach((operation, requestMethod) => {
|
|
49
50
|
repository
|
|
50
|
-
.get(`routes${
|
|
51
|
+
.get(`routes${path}.ts`)
|
|
51
52
|
.export(new OperationCoder(operation, requestMethod, securitySchemes));
|
|
52
53
|
});
|
|
53
54
|
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import nodePath from "node:path";
|
|
2
2
|
import { Coder } from "./coder.js";
|
|
3
3
|
import { OperationTypeCoder } from "./operation-type-coder.js";
|
|
4
|
+
import path from "node:path";
|
|
4
5
|
export class OperationCoder extends Coder {
|
|
5
6
|
constructor(requirement, requestMethod, securitySchemes = {}) {
|
|
6
7
|
super(requirement);
|
|
@@ -54,7 +54,7 @@ export class OperationTypeCoder extends TypeCoder {
|
|
|
54
54
|
.at(-2)
|
|
55
55
|
.replaceAll("~1", "/");
|
|
56
56
|
return `${nodePath
|
|
57
|
-
.join("types/paths", pathString)
|
|
57
|
+
.join("types/paths", pathString === "/" ? "/index" : pathString)
|
|
58
58
|
.replaceAll("\\", "/")}.types.ts`;
|
|
59
59
|
}
|
|
60
60
|
userType() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "counterfact",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.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",
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
"prepack": "yarn build",
|
|
36
36
|
"release": "npx changeset publish",
|
|
37
37
|
"prepare": "husky install",
|
|
38
|
-
"lint": "eslint . --plugin file-progress --rule 'file-progress/activate: 1'",
|
|
39
|
-
"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\"",
|
|
38
|
+
"lint": "eslint . --plugin file-progress --rule 'file-progress/activate: 1' --ignore-pattern dist --ignore-pattern out --ignore-pattern coverage",
|
|
39
|
+
"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",
|
|
40
40
|
"go:petstore": "yarn build && yarn counterfact https://petstore3.swagger.io/api/v3/openapi.json out",
|
|
41
41
|
"go:petstore2": "yarn build && yarn counterfact https://petstore.swagger.io/v2/swagger.json out",
|
|
42
42
|
"go:example": "yarn build && node ./bin/counterfact.js ./openapi-example.yaml out",
|
|
@@ -45,9 +45,9 @@
|
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@changesets/cli": "2.27.8",
|
|
48
|
-
"@stryker-mutator/core": "8.
|
|
49
|
-
"@stryker-mutator/jest-runner": "8.
|
|
50
|
-
"@stryker-mutator/typescript-checker": "8.
|
|
48
|
+
"@stryker-mutator/core": "8.6.0",
|
|
49
|
+
"@stryker-mutator/jest-runner": "8.6.0",
|
|
50
|
+
"@stryker-mutator/typescript-checker": "8.6.0",
|
|
51
51
|
"@swc/core": "1.7.26",
|
|
52
52
|
"@swc/jest": "0.2.36",
|
|
53
53
|
"@testing-library/dom": "10.4.0",
|
|
@@ -85,7 +85,7 @@
|
|
|
85
85
|
"@hapi/accept": "6.0.3",
|
|
86
86
|
"@types/json-schema": "7.0.15",
|
|
87
87
|
"ast-types": "0.14.2",
|
|
88
|
-
"chokidar": "
|
|
88
|
+
"chokidar": "4.0.1",
|
|
89
89
|
"commander": "12.1.0",
|
|
90
90
|
"debug": "4.3.7",
|
|
91
91
|
"fetch": "1.1.0",
|