counterfact 0.37.1 → 0.37.2
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/dist/server/dispatcher.js +7 -1
- package/dist/typescript-generator/operation-type-coder.js +4 -0
- package/dist/typescript-generator/read-only-comments.js +5 -0
- package/dist/typescript-generator/repository.js +3 -1
- package/dist/typescript-generator/schema-type-coder.js +1 -0
- package/dist/typescript-generator/script.js +3 -0
- package/package.json +4 -4
|
@@ -110,9 +110,15 @@ export class Dispatcher {
|
|
|
110
110
|
}
|
|
111
111
|
return false;
|
|
112
112
|
}
|
|
113
|
-
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
113
|
+
// eslint-disable-next-line sonarjs/cognitive-complexity, max-statements
|
|
114
114
|
async request({ body, headers = {}, method, path, query, req, }) {
|
|
115
115
|
debug(`request: ${method} ${path}`);
|
|
116
|
+
// If the incoming path includes the base path, remove it
|
|
117
|
+
if (this.openApiDocument?.basePath !== undefined &&
|
|
118
|
+
path.toLowerCase().startsWith(this.openApiDocument.basePath.toLowerCase())) {
|
|
119
|
+
// eslint-disable-next-line security/detect-non-literal-regexp
|
|
120
|
+
path = path.replace(new RegExp(this.openApiDocument.basePath, "iu"), "");
|
|
121
|
+
}
|
|
116
122
|
const { matchedPath } = this.registry.handler(path);
|
|
117
123
|
const operation = this.operationForPathAndMethod(matchedPath, method);
|
|
118
124
|
const response = await this.registry.endpoint(method, path, this.parameterTypes(operation?.parameters))({
|
|
@@ -2,6 +2,7 @@ import nodePath from "node:path";
|
|
|
2
2
|
import { Coder } from "./coder.js";
|
|
3
3
|
import { CONTEXT_FILE_TOKEN } from "./context-file-token.js";
|
|
4
4
|
import { ParametersTypeCoder } from "./parameters-type-coder.js";
|
|
5
|
+
import { READ_ONLY_COMMENTS } from "./read-only-comments.js";
|
|
5
6
|
import { ResponseTypeCoder } from "./response-type-coder.js";
|
|
6
7
|
import { SchemaTypeCoder } from "./schema-type-coder.js";
|
|
7
8
|
export class OperationTypeCoder extends Coder {
|
|
@@ -48,7 +49,10 @@ export class OperationTypeCoder extends Coder {
|
|
|
48
49
|
.join("path-types", pathString)
|
|
49
50
|
.replaceAll("\\", "/")}.types.ts`;
|
|
50
51
|
}
|
|
52
|
+
// eslint-disable-next-line max-statements
|
|
51
53
|
write(script) {
|
|
54
|
+
// eslint-disable-next-line no-param-reassign
|
|
55
|
+
script.comments = READ_ONLY_COMMENTS;
|
|
52
56
|
const contextTypeImportName = script.importExternalType("Context", CONTEXT_FILE_TOKEN);
|
|
53
57
|
const parameters = this.requirement.get("parameters");
|
|
54
58
|
const queryType = parameters === undefined
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export const READ_ONLY_COMMENTS = [
|
|
2
|
+
"This code was automatically generated from an OpenAPI description.",
|
|
3
|
+
"Do not edit this file. Edit the OpenAPI file instead.",
|
|
4
|
+
"For more information, see https://github.com/pmcelhaney/counterfact/blob/main/docs/faq-generated-code.md",
|
|
5
|
+
];
|
|
@@ -93,7 +93,9 @@ export class Context {
|
|
|
93
93
|
`);
|
|
94
94
|
}
|
|
95
95
|
findContextPath(destination, path) {
|
|
96
|
-
return nodePath
|
|
96
|
+
return nodePath
|
|
97
|
+
.relative(nodePath.join(destination, nodePath.dirname(path)), this.nearestContextFile(destination, path))
|
|
98
|
+
.replaceAll("\\", "/");
|
|
97
99
|
}
|
|
98
100
|
nearestContextFile(destination, path) {
|
|
99
101
|
const directory = nodePath.dirname(path).replace("path-types", "paths");
|
|
@@ -73,6 +73,7 @@ export class SchemaTypeCoder extends Coder {
|
|
|
73
73
|
return `components/${this.requirement.data.$ref.split("/").at(-1)}.ts`;
|
|
74
74
|
}
|
|
75
75
|
write(script) {
|
|
76
|
+
// script.comments = READ_ONLY_COMMENTS;
|
|
76
77
|
if (this.requirement.isReference) {
|
|
77
78
|
return script.importType(this);
|
|
78
79
|
}
|
|
@@ -5,6 +5,7 @@ const debug = createDebugger("counterfact:typescript-generator:script");
|
|
|
5
5
|
export class Script {
|
|
6
6
|
constructor(repository, path) {
|
|
7
7
|
this.repository = repository;
|
|
8
|
+
this.comments = [];
|
|
8
9
|
this.exports = new Map();
|
|
9
10
|
this.imports = new Map();
|
|
10
11
|
this.externalImport = new Map();
|
|
@@ -131,6 +132,8 @@ export class Script {
|
|
|
131
132
|
}
|
|
132
133
|
contents() {
|
|
133
134
|
return prettier.format([
|
|
135
|
+
this.comments.map((comment) => `// ${comment}`).join("\n"),
|
|
136
|
+
this.comments.length > 0 ? "\n\n" : "",
|
|
134
137
|
this.externalImportStatements().join("\n"),
|
|
135
138
|
this.importStatements().join("\n"),
|
|
136
139
|
"\n\n",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "counterfact",
|
|
3
|
-
"version": "0.37.
|
|
3
|
+
"version": "0.37.2",
|
|
4
4
|
"description": "a library for building a fake REST API for testing",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/server/counterfact.js",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"@stryker-mutator/core": "8.2.6",
|
|
48
48
|
"@stryker-mutator/jest-runner": "8.2.6",
|
|
49
49
|
"@stryker-mutator/typescript-checker": "8.2.6",
|
|
50
|
-
"@swc/core": "1.4.
|
|
50
|
+
"@swc/core": "1.4.6",
|
|
51
51
|
"@swc/jest": "0.2.36",
|
|
52
52
|
"@testing-library/dom": "9.3.4",
|
|
53
53
|
"@types/jest": "29.5.12",
|
|
@@ -97,8 +97,8 @@
|
|
|
97
97
|
"koa-proxy": "1.0.0-alpha.3",
|
|
98
98
|
"koa2-swagger-ui": "5.10.0",
|
|
99
99
|
"node-fetch": "3.3.2",
|
|
100
|
-
"open": "10.0
|
|
100
|
+
"open": "10.1.0",
|
|
101
101
|
"prettier": "3.2.5",
|
|
102
|
-
"typescript": "5.
|
|
102
|
+
"typescript": "5.4.2"
|
|
103
103
|
}
|
|
104
104
|
}
|