• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmcelhaney / counterfact / 5942933897

22 Aug 2023 06:59PM UTC coverage: 83.567% (-3.8%) from 87.388%
5942933897

push

github

web-flow
Merge pull request #517 from pmcelhaney/windows-support

disable the end-to-end test because it won't work on Windows

389 of 423 branches covered (91.96%)

Branch coverage included in aggregate %.

1818 of 2218 relevant lines covered (81.97%)

9.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.39
/src/typescript-generator/response-type-coder.js
1
import nodePath from "node:path";
1✔
2

1✔
3
import { Coder } from "./coder.js";
1✔
4
import { SchemaTypeCoder } from "./schema-type-coder.js";
1✔
5
import { printObject, printObjectWithoutQuotes } from "./printers.js";
1✔
6

1✔
7
export class ResponseTypeCoder extends Coder {
1✔
8
  constructor(requirement, openApi2MediaTypes = []) {
1✔
9
    super(requirement);
9✔
10

9✔
11
    this.openApi2MediaTypes = openApi2MediaTypes;
9✔
12
  }
9✔
13

1✔
14
  typeForDefaultStatusCode(listedStatusCodes) {
1✔
15
    this.needsHttpStatusCodeImport = true;
8✔
16

8✔
17
    const definedStatusCodes = listedStatusCodes.filter(
8✔
18
      (key) => key !== "default"
8✔
19
    );
8✔
20

8✔
21
    if (definedStatusCodes.length === 0) {
8✔
22
      return "[statusCode in HttpStatusCode]";
4✔
23
    }
4✔
24

4✔
25
    return `[statusCode in Exclude<HttpStatusCode, ${definedStatusCodes.join(
4✔
26
      " | "
4✔
27
    )}>]`;
4✔
28
  }
4✔
29

1✔
30
  normalizeStatusCode(statusCode) {
1✔
31
    if (statusCode === "default") {
15✔
32
      return this.typeForDefaultStatusCode(Object.keys(this.requirement.data));
8✔
33
    }
8✔
34

7✔
35
    return statusCode;
7✔
36
  }
7✔
37

1✔
38
  buildContentObjectType(script, response) {
1✔
39
    if (response.has("content")) {
12✔
40
      return response.get("content").map((content, mediaType) => [
4✔
41
        mediaType,
4✔
42
        `{ 
4✔
43
            schema:  ${new SchemaTypeCoder(content.get("schema")).write(script)}
4✔
44
         }`,
4✔
45
      ]);
4✔
46
    }
4✔
47

8✔
48
    return this.openApi2MediaTypes.map((mediaType) => [
8✔
49
      mediaType,
8✔
50
      `{
8✔
51
            schema: ${new SchemaTypeCoder(response.get("schema")).write(script)}
8✔
52
         }`,
8✔
53
    ]);
8✔
54
  }
8✔
55

1✔
56
  printContentObjectType(script, response) {
1✔
57
    if (response.has("content") || response.has("schema")) {
12✔
58
      return printObject(this.buildContentObjectType(script, response));
12✔
59
    }
12✔
60

×
61
    return "{}";
×
62
  }
×
63

1✔
64
  buildHeaders(script, response) {
1✔
65
    return response
×
66
      .get("headers")
×
67
      .map((value, name) => [
×
68
        name,
×
69
        `{ schema: ${new SchemaTypeCoder(value.get("schema") ?? value).write(
×
70
          script
×
71
        )}}`,
×
72
      ]);
×
73
  }
×
74

1✔
75
  printHeaders(script, response) {
1✔
76
    if (!response.has("headers")) {
12✔
77
      return "{}";
12✔
78
    }
12✔
79

×
80
    return printObject(this.buildHeaders(script, response));
×
81
  }
×
82

1✔
83
  buildResponseObjectType(script) {
1✔
84
    return printObjectWithoutQuotes(
6✔
85
      this.requirement.map((response, responseCode) => [
6✔
86
        this.normalizeStatusCode(responseCode),
12✔
87
        `{
12✔
88
          headers: ${this.printHeaders(script, response)};
12✔
89
          content: ${this.printContentObjectType(script, response)};
12✔
90
        }`,
12✔
91
      ])
6✔
92
    );
6✔
93
  }
6✔
94

1✔
95
  write(script) {
1✔
96
    const basePath = script.path
6✔
97
      .split("/")
6✔
98
      .slice(0, -1)
6✔
99
      .map(() => "..")
6✔
100
      .join("/");
6✔
101

6✔
102
    script.importExternalType(
6✔
103
      "ResponseBuilderFactory",
6✔
104
      nodePath
6✔
105
        .join(basePath, "response-builder-factory.js")
6✔
106
        .replaceAll("\\", "/")
6✔
107
    );
6✔
108

6✔
109
    if (this.needsHttpStatusCodeImport) {
6!
110
      script.importExternalType(
×
111
        "HttpStatusCode",
×
112
        nodePath
×
113
          .join(basePath, "response-builder-factory.js")
×
114
          .replaceAll("\\", "/")
×
115
      );
×
116
    }
×
117

6✔
118
    return `ResponseBuilderFactory<${this.buildResponseObjectType(script)}>`;
6✔
119
  }
6✔
120
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc