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

pmcelhaney / counterfact / 5837079355

11 Aug 2023 09:10PM CUT coverage: 87.031%. Remained the same
5837079355

push

github

web-flow
Merge pull request #494 from pmcelhaney/shebangs-shebangs

fix an issue where ts-node was not being used

407 of 434 branches covered (93.78%)

Branch coverage included in aggregate %.

1821 of 2126 relevant lines covered (85.65%)

21.22 hits per line

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

95.17
/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);
27✔
10

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

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

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

11✔
21
    if (definedStatusCodes.length === 0) {
11✔
22
      return "[statusCode in HttpStatusCode]";
7✔
23
    }
7✔
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") {
48✔
32
      return this.typeForDefaultStatusCode(Object.keys(this.requirement.data));
11✔
33
    }
11✔
34

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

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

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

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

23✔
61
    return "{}";
23✔
62
  }
23✔
63

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

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

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

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

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

24✔
102
    script.importExternalType(
24✔
103
      "ResponseBuilderFactory",
24✔
104
      nodePath.join(basePath, "response-builder-factory.js")
24✔
105
    );
24✔
106

24✔
107
    if (this.needsHttpStatusCodeImport) {
24!
108
      script.importExternalType(
×
109
        "HttpStatusCode",
×
110
        nodePath.join(basePath, "response-builder-factory.js")
×
111
      );
×
112
    }
×
113

24✔
114
    return `ResponseBuilderFactory<${this.buildResponseObjectType(script)}>`;
24✔
115
  }
24✔
116
}
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

© 2025 Coveralls, Inc