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

pmcelhaney / counterfact / 5942365482

22 Aug 2023 05:58PM CUT coverage: 87.693% (+0.4%) from 87.318%
5942365482

Pull #377

github

pmcelhaney
replaceAll not replace
Pull Request #377: run CI on Windows as well as Linux

431 of 458 branches covered (94.1%)

Branch coverage included in aggregate %.

219 of 219 new or added lines in 13 files covered. (100.0%)

2013 of 2329 relevant lines covered (86.43%)

21.13 hits per line

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

94.0
/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);
28✔
10

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

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

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

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

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

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

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

1✔
38
  buildContentObjectType(script, response) {
1✔
39
    if (response.has("content")) {
25✔
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

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")) {
48✔
58
      return printObject(this.buildContentObjectType(script, response));
25✔
59
    }
25✔
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")) {
48✔
77
      return "{}";
47✔
78
    }
47✔
79

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

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

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

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

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

25✔
118
    return `ResponseBuilderFactory<${this.buildResponseObjectType(script)}>`;
25✔
119
  }
25✔
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

© 2025 Coveralls, Inc