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

pmcelhaney / counterfact / 8624465510

10 Apr 2024 01:10AM UTC coverage: 86.555% (-0.1%) from 86.664%
8624465510

push

github

web-flow
Merge pull request #844 from pmcelhaney/refactor-path-coder

899 of 991 branches covered (90.72%)

Branch coverage included in aggregate %.

24 of 28 new or added lines in 3 files covered. (85.71%)

2835 of 3323 relevant lines covered (85.31%)

42.54 hits per line

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

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

2✔
3
import { Coder } from "./coder.js";
2✔
4
import { CONTEXT_FILE_TOKEN } from "./context-file-token.js";
2✔
5
import { ParametersTypeCoder } from "./parameters-type-coder.js";
2✔
6
import { READ_ONLY_COMMENTS } from "./read-only-comments.js";
2✔
7
import { ResponseTypeCoder } from "./response-type-coder.js";
2✔
8
import { SchemaTypeCoder } from "./schema-type-coder.js";
2✔
9

2✔
10
export class OperationTypeCoder extends Coder {
2✔
11
  constructor(requirement, requestMethod) {
2✔
12
    super(requirement);
64✔
13

64✔
14
    if (requestMethod === undefined) {
64!
NEW
15
      throw new Error("requestMethod is required");
×
NEW
16
    }
×
17

64✔
18
    this.requestMethod = requestMethod;
64✔
19
  }
64✔
20

2✔
21
  names() {
2✔
22
    return super.names(`HTTP_${this.requestMethod.toUpperCase()}`);
90✔
23
  }
90✔
24

2✔
25
  responseTypes(script) {
2✔
26
    return this.requirement
56✔
27
      .get("responses")
56✔
28
      .flatMap((response, responseCode) => {
56✔
29
        const status =
102✔
30
          responseCode === "default"
102✔
31
            ? "number | undefined"
26✔
32
            : Number.parseInt(responseCode, 10);
76✔
33

102✔
34
        if (response.has("content")) {
102✔
35
          return response.get("content").map(
40✔
36
            (content, contentType) => `{  
40✔
37
              status: ${status}, 
60✔
38
              contentType?: "${contentType}",
60✔
39
              body?: ${new SchemaTypeCoder(content.get("schema")).write(script)}
60✔
40
            }`,
40✔
41
          );
40✔
42
        }
40✔
43

62✔
44
        if (response.has("schema")) {
88✔
45
          const produces =
16✔
46
            this.requirement?.get("produces")?.data ??
16!
47
            this.requirement.specification.rootRequirement.get("produces").data;
16✔
48

16✔
49
          return produces
16✔
50
            .map(
16✔
51
              (contentType) => `{
16✔
52
            status: ${status},
16✔
53
            contentType?: "${contentType}",
16✔
54
            body?: ${new SchemaTypeCoder(response.get("schema")).write(script)}
16✔
55
          }`,
16✔
56
            )
16✔
57
            .join(" | ");
16✔
58
        }
16✔
59

46✔
60
        return `{  
46✔
61
          status: ${status} 
46✔
62
        }`;
46✔
63
      })
46✔
64

56✔
65
      .join(" | ");
56✔
66
  }
56✔
67

2✔
68
  modulePath() {
2✔
69
    const pathString = this.requirement.url
46✔
70
      .split("/")
46✔
71
      .at(-2)
46✔
72
      .replaceAll("~1", "/");
46✔
73

46✔
74
    return `${nodePath
46✔
75
      .join("path-types", pathString)
46✔
76
      .replaceAll("\\", "/")}.types.ts`;
46✔
77
  }
46✔
78

2✔
79
  // eslint-disable-next-line max-statements
2✔
80
  write(script) {
2✔
81
    // eslint-disable-next-line no-param-reassign
56✔
82
    script.comments = READ_ONLY_COMMENTS;
56✔
83

56✔
84
    const xType = script.importSharedType("WideOperationArgument");
56✔
85

56✔
86
    script.importSharedType("OmitValueWhenNever");
56✔
87

56✔
88
    const contextTypeImportName = script.importExternalType(
56✔
89
      "Context",
56✔
90
      CONTEXT_FILE_TOKEN,
56✔
91
    );
56✔
92

56✔
93
    const parameters = this.requirement.get("parameters");
56✔
94

56✔
95
    const queryType = new ParametersTypeCoder(parameters, "query").write(
56✔
96
      script,
56✔
97
    );
56✔
98

56✔
99
    const pathType = new ParametersTypeCoder(parameters, "path").write(script);
56✔
100

56✔
101
    const headerType = new ParametersTypeCoder(parameters, "header").write(
56✔
102
      script,
56✔
103
    );
56✔
104

56✔
105
    const bodyRequirement = this.requirement.get("consumes")
56✔
106
      ? parameters
4✔
107
          .find((parameter) =>
4✔
108
            ["body", "formData"].includes(parameter.get("in").data),
16✔
109
          )
4✔
110
          .get("schema")
4✔
111
      : this.requirement.select("requestBody/content/application~1json/schema");
52✔
112

56✔
113
    const bodyType =
56✔
114
      bodyRequirement === undefined
56✔
115
        ? "never"
38✔
116
        : new SchemaTypeCoder(bodyRequirement).write(script);
18✔
117

56✔
118
    const responseType = new ResponseTypeCoder(
56✔
119
      this.requirement.get("responses"),
56✔
120
      this.requirement.get("produces")?.data ??
56✔
121
        this.requirement.specification?.rootRequirement?.get("produces")?.data,
48!
122
    ).write(script);
56✔
123

56✔
124
    const proxyType = '(url: string) => "COUNTERFACT_RESPONSE"';
56✔
125

56✔
126
    return `($: OmitValueWhenNever<{ query: ${queryType}, path: ${pathType}, header: ${headerType}, body: ${bodyType}, context: ${contextTypeImportName}, response: ${responseType}, x: ${xType}, proxy: ${proxyType} }>) => ${this.responseTypes(
56✔
127
      script,
56✔
128
    )} | { status: 415, contentType: "text/plain", body: string } | "COUNTERFACT_RESPONSE" | { ALL_REMAINING_HEADERS_ARE_OPTIONAL: "COUNTERFACT_RESPONSE" }`;
56✔
129
  }
56✔
130
}
2✔
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