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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # counterfact
2
2
 
3
+ ## 0.21.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 65f081b: fix error when the produces content type is defined at the root
8
+
9
+ ## 0.21.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 9e0e4d3: it's Friday ¯\_(ツ)\_/¯
14
+
3
15
  ## 0.20.6
4
16
 
5
17
  ### Patch Changes
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env ts-node
1
+ #!/usr/bin/env ts-node --esm --transpileOnly
2
2
 
3
3
  import nodePath from "node:path";
4
4
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "counterfact",
3
- "version": "0.20.6",
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.46.0",
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
  {