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

RobinTail / express-zod-api / 9289592109

29 May 2024 04:16PM UTC coverage: 100.0%. Remained the same
9289592109

Pull #1798

github

web-flow
Merge 9d1b232d5 into b680f486b
Pull Request #1798: Bump typescript-eslint from 8.0.0-alpha.16 to 8.0.0-alpha.20 in the typescript-eslint group

687 of 721 branches covered (95.28%)

1130 of 1130 relevant lines covered (100.0%)

474.27 hits per line

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

100.0
/src/api-response.ts
1
import { z } from "zod";
2

3
export const defaultStatusCodes = {
156✔
4
  positive: 200,
5
  negative: 400,
6
};
7

8
export interface ApiResponse<S extends z.ZodTypeAny> {
9
  schema: S;
10
  /**
11
   * @default 200 for a positive response
12
   * @default 400 for a negative response
13
   * @override statusCodes
14
   * */
15
  statusCode?: number;
16
  /**
17
   * @default [200] for positive response
18
   * @default [400] for negative response
19
   * */
20
  statusCodes?: [number, ...number[]];
21
  /**
22
   * @default "application/json"
23
   * @override mimeTypes
24
   * */
25
  mimeType?: string;
26
  /** @default [ "application/json" ] */
27
  mimeTypes?: [string, ...string[]];
28
}
29

30
export type NormalizedResponse = Required<
31
  Pick<ApiResponse<z.ZodTypeAny>, "schema" | "statusCodes" | "mimeTypes">
32
>;
33

34
export type AnyResponseDefinition =
35
  | z.ZodTypeAny // plain schema, default status codes applied
36
  | ApiResponse<z.ZodTypeAny> // single response definition, status code(s) customizable
37
  | ApiResponse<z.ZodTypeAny>[]; // Feature #1431: different responses for different status codes
38

39
export const normalizeApiResponse = (
156✔
40
  subject: AnyResponseDefinition,
41
  fallback: Omit<NormalizedResponse, "schema">,
42
): NormalizedResponse[] => {
43
  if (subject instanceof z.ZodType) {
1,644✔
44
    return [{ ...fallback, schema: subject }];
1,524✔
45
  }
46
  return (Array.isArray(subject) ? subject : [subject]).map(
120✔
47
    ({ schema, statusCodes, statusCode, mimeTypes, mimeType }) => ({
144✔
48
      schema,
49
      statusCodes: statusCode
144✔
50
        ? [statusCode]
51
        : statusCodes || fallback.statusCodes,
120✔
52
      mimeTypes: mimeType ? [mimeType] : mimeTypes || fallback.mimeTypes,
318✔
53
    }),
54
  );
55
};
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