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

RobinTail / express-zod-api / 13444086320

20 Feb 2025 08:28PM UTC coverage: 100.0%. First build
13444086320

Pull #2425

github

web-flow
Merge 60a537c42 into 23732b2fe
Pull Request #2425: Fix: async `errorHandler` for not found case

1253 of 1279 branches covered (97.97%)

1 of 1 new or added line in 1 file covered. (100.0%)

4223 of 4223 relevant lines covered (100.0%)

210.05 hits per line

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

100.0
/express-zod-api/src/diagnostics.ts
1
import * as R from "ramda";
4✔
2
import type { ZodRawShape } from "zod";
3
import { responseVariants } from "./api-response";
4✔
4
import { FlatObject, getRoutePathParams } from "./common-helpers";
4✔
5
import { contentTypes } from "./content-type";
4✔
6
import { assertJsonCompatible } from "./deep-checks";
4✔
7
import { AbstractEndpoint } from "./endpoint";
8
import { extractObjectSchema } from "./io-schema";
4✔
9
import { ActualLogger } from "./logger-helpers";
10

11
export class Diagnostics {
4✔
12
  /** @desc (catcher)(...args) => bool | ReturnValue<typeof catcher> */
13
  readonly #trier = R.tryCatch(assertJsonCompatible);
112✔
14
  #verifiedEndpoints = new WeakSet<AbstractEndpoint>();
112✔
15
  #verifiedPaths = new WeakMap<
112✔
16
    AbstractEndpoint,
17
    { shape: ZodRawShape; paths: string[] }
18
  >();
112✔
19

20
  constructor(protected logger: ActualLogger) {}
112✔
21

22
  public checkJsonCompat(endpoint: AbstractEndpoint, ctx: FlatObject): void {
112✔
23
    if (this.#verifiedEndpoints.has(endpoint)) return;
168✔
24
    if (endpoint.getRequestType() === "json") {
144✔
25
      this.#trier((reason) =>
120✔
26
        this.logger.warn(
32✔
27
          "The final input schema of the endpoint contains an unsupported JSON payload type.",
32✔
28
          Object.assign(ctx, { reason }),
32✔
29
        ),
32✔
30
      )(endpoint.getSchema("input"), "in");
120✔
31
    }
120✔
32
    for (const variant of responseVariants) {
168✔
33
      const catcher = this.#trier((reason) =>
264✔
34
        this.logger.warn(
32✔
35
          `The final ${variant} response schema of the endpoint contains an unsupported JSON payload type.`,
32✔
36
          Object.assign(ctx, { reason }),
32✔
37
        ),
32✔
38
      );
264✔
39
      for (const { mimeTypes, schema } of endpoint.getResponses(variant)) {
264✔
40
        if (!mimeTypes?.includes(contentTypes.json)) continue;
264!
41
        catcher(schema, "out");
264✔
42
      }
264✔
43
    }
264✔
44
    this.#verifiedEndpoints.add(endpoint);
132✔
45
  }
168✔
46

47
  public checkPathParams(
112✔
48
    path: string,
168✔
49
    endpoint: AbstractEndpoint,
168✔
50
    ctx: FlatObject,
168✔
51
  ): void {
168✔
52
    const ref = this.#verifiedPaths.get(endpoint);
168✔
53
    if (ref?.paths.includes(path)) return;
168!
54
    const params = getRoutePathParams(path);
168✔
55
    if (params.length === 0) return; // next statement can be expensive
168✔
56
    const shape =
16✔
57
      ref?.shape || extractObjectSchema(endpoint.getSchema("input")).shape;
168✔
58
    for (const param of params) {
168✔
59
      if (param in shape) continue;
16!
60
      this.logger.warn(
16✔
61
        "The input schema of the endpoint is most likely missing the parameter of the path it's assigned to.",
16✔
62
        Object.assign(ctx, { path, param }),
16✔
63
      );
16✔
64
    }
16✔
65
    if (ref) ref.paths.push(path);
124✔
66
    else this.#verifiedPaths.set(endpoint, { shape, paths: [path] });
12✔
67
  }
168✔
68
}
112✔
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