counterfact 0.20.6 → 0.21.1
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/CHANGELOG.md +12 -0
- package/bin/counterfact.js +1 -1
- package/package.json +2 -2
- package/src/typescript-generator/operation-type-coder.js +1 -1
- package/test/typescript-generator/__snapshots__/operation-type-coder.test.js.snap +50 -0
- package/test/typescript-generator/operation-type-coder.test.js +48 -0
package/CHANGELOG.md
CHANGED
package/bin/counterfact.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "counterfact",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.1",
|
|
4
4
|
"description": "a library for building a fake REST API for testing",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/server/counterfact.js",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"@testing-library/dom": "^9.3.0",
|
|
43
43
|
"@types/koa": "2.13.8",
|
|
44
44
|
"@types/koa-static": "^4.0.2",
|
|
45
|
-
"eslint": "8.
|
|
45
|
+
"eslint": "8.47.0",
|
|
46
46
|
"eslint-config-hardcore": "36.4.0",
|
|
47
47
|
"eslint-formatter-github-annotations": "0.1.0",
|
|
48
48
|
"eslint-import-resolver-typescript": "^3.2.5",
|
|
@@ -35,7 +35,7 @@ export class OperationTypeCoder extends Coder {
|
|
|
35
35
|
if (response.has("schema")) {
|
|
36
36
|
const produces =
|
|
37
37
|
this.requirement?.get("produces")?.data ??
|
|
38
|
-
this.requirement.rootRequirement.get("produces").data;
|
|
38
|
+
this.requirement.specification.rootRequirement.get("produces").data;
|
|
39
39
|
|
|
40
40
|
return produces
|
|
41
41
|
.map(
|
|
@@ -36,6 +36,56 @@ exports[`an OperationTypeCoder falls back to root level produces (OpenAPI 2) 1`]
|
|
|
36
36
|
"
|
|
37
37
|
`;
|
|
38
38
|
|
|
39
|
+
exports[`an OperationTypeCoder generates a complex post operation (OpenAPI 2 with produces at the root) 1`] = `
|
|
40
|
+
"type TestType = ({
|
|
41
|
+
query,
|
|
42
|
+
path,
|
|
43
|
+
header,
|
|
44
|
+
body,
|
|
45
|
+
context,
|
|
46
|
+
proxy,
|
|
47
|
+
}: {
|
|
48
|
+
query: { \\"name?\\": string };
|
|
49
|
+
path: { \\"id?\\": string };
|
|
50
|
+
header: { \\"name?\\": string };
|
|
51
|
+
body: Type;
|
|
52
|
+
context: typeof default;
|
|
53
|
+
response: ResponseBuilderFactory<{
|
|
54
|
+
200: {
|
|
55
|
+
headers: {};
|
|
56
|
+
content: {};
|
|
57
|
+
};
|
|
58
|
+
400: {
|
|
59
|
+
headers: {};
|
|
60
|
+
content: {};
|
|
61
|
+
};
|
|
62
|
+
[statusCode in Exclude<HttpStatusCode, 200 | 400>]: {
|
|
63
|
+
headers: {};
|
|
64
|
+
content: {};
|
|
65
|
+
};
|
|
66
|
+
}>;
|
|
67
|
+
proxy: (url: string) => { proxyUrl: string };
|
|
68
|
+
}) =>
|
|
69
|
+
| {
|
|
70
|
+
status: 200;
|
|
71
|
+
contentType?: \\"application/json\\";
|
|
72
|
+
body?: Type;
|
|
73
|
+
}
|
|
74
|
+
| {
|
|
75
|
+
status: 400;
|
|
76
|
+
contentType?: \\"application/json\\";
|
|
77
|
+
body?: Type;
|
|
78
|
+
}
|
|
79
|
+
| {
|
|
80
|
+
status: number | undefined;
|
|
81
|
+
contentType?: \\"application/json\\";
|
|
82
|
+
body?: Type;
|
|
83
|
+
}
|
|
84
|
+
| { status: 415; contentType: \\"text/plain\\"; body: string }
|
|
85
|
+
| void;
|
|
86
|
+
"
|
|
87
|
+
`;
|
|
88
|
+
|
|
39
89
|
exports[`an OperationTypeCoder generates a complex post operation (OpenAPI 2) 1`] = `
|
|
40
90
|
"type TestType = ({
|
|
41
91
|
query,
|
|
@@ -3,6 +3,7 @@ import prettier from "prettier";
|
|
|
3
3
|
|
|
4
4
|
import { OperationTypeCoder } from "../../src/typescript-generator/operation-type-coder.js";
|
|
5
5
|
import { Requirement } from "../../src/typescript-generator/requirement.js";
|
|
6
|
+
import { Specification } from "../../src/typescript-generator/specification.js";
|
|
6
7
|
|
|
7
8
|
function format(code) {
|
|
8
9
|
return prettier.format(code, { parser: "typescript" });
|
|
@@ -163,6 +164,53 @@ describe("an OperationTypeCoder", () => {
|
|
|
163
164
|
).toMatchSnapshot();
|
|
164
165
|
});
|
|
165
166
|
|
|
167
|
+
it("generates a complex post operation (OpenAPI 2 with produces at the root)", () => {
|
|
168
|
+
const specification = new Specification();
|
|
169
|
+
|
|
170
|
+
specification.rootRequirement = new Requirement({
|
|
171
|
+
produces: ["application/json"],
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
const requirement = new Requirement(
|
|
175
|
+
{
|
|
176
|
+
consumes: ["application/json"],
|
|
177
|
+
|
|
178
|
+
parameters: [
|
|
179
|
+
{ name: "id", in: "path", type: "string" },
|
|
180
|
+
{ name: "name", in: "query", type: "string" },
|
|
181
|
+
{ name: "name", in: "header", type: "string" },
|
|
182
|
+
{
|
|
183
|
+
name: "body",
|
|
184
|
+
in: "body",
|
|
185
|
+
schema: { $ref: "#/components/schemas/Example" },
|
|
186
|
+
},
|
|
187
|
+
],
|
|
188
|
+
|
|
189
|
+
responses: {
|
|
190
|
+
200: {
|
|
191
|
+
schema: { $ref: "#/components/schemas/Example" },
|
|
192
|
+
},
|
|
193
|
+
|
|
194
|
+
400: {
|
|
195
|
+
schema: { $ref: "#/components/schemas/Error" },
|
|
196
|
+
},
|
|
197
|
+
|
|
198
|
+
default: {
|
|
199
|
+
schema: { $ref: "#/components/schemas/Error" },
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
"#/paths/hello/post",
|
|
204
|
+
specification
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
const coder = new OperationTypeCoder(requirement);
|
|
208
|
+
|
|
209
|
+
expect(
|
|
210
|
+
format(`type TestType = ${coder.write(dummyScript)}`)
|
|
211
|
+
).toMatchSnapshot();
|
|
212
|
+
});
|
|
213
|
+
|
|
166
214
|
it("generates a simple get operation", () => {
|
|
167
215
|
const requirement = new Requirement(
|
|
168
216
|
{
|