Skip to content

Commit 122705e

Browse files
committed
REF: replacing all has* functions with a single findRequestTypeDefiningSchema().
1 parent 1bff037 commit 122705e

2 files changed

Lines changed: 25 additions & 35 deletions

File tree

express-zod-api/src/deep-checks.ts

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,17 @@ export const findNestedSchema = (
3434
(err: DeepCheckError) => err.cause,
3535
)();
3636

37-
export const hasUpload = (subject: IOSchema) =>
38-
Boolean(
39-
findNestedSchema(subject, {
40-
condition: (schema) =>
41-
globalRegistry.get(schema)?.[metaSymbol]?.brand === ezUploadBrand,
42-
io: "input",
43-
}),
44-
);
45-
46-
export const hasRaw = (subject: IOSchema) =>
47-
Boolean(
48-
findNestedSchema(subject, {
49-
condition: (schema) =>
50-
globalRegistry.get(schema)?.[metaSymbol]?.brand === ezRawBrand,
51-
io: "input",
52-
}),
53-
);
54-
55-
export const hasForm = (subject: IOSchema) =>
56-
Boolean(
57-
findNestedSchema(subject, {
58-
condition: (schema) =>
59-
globalRegistry.get(schema)?.[metaSymbol]?.brand === ezFormBrand,
60-
io: "input",
61-
}),
62-
);
37+
export const findRequestTypeDefiningSchema = (subject: IOSchema) =>
38+
findNestedSchema(subject, {
39+
condition: (schema) => {
40+
const { brand } = globalRegistry.get(schema)?.[metaSymbol] || {};
41+
return (
42+
typeof brand === "symbol" &&
43+
[ezUploadBrand, ezRawBrand, ezFormBrand].includes(brand)
44+
);
45+
},
46+
io: "input",
47+
});
6348

6449
const unsupported: FirstPartyKind[] = [
6550
"nan",

express-zod-api/src/endpoint.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Request, Response } from "express";
22
import * as R from "ramda";
3-
import { z } from "zod";
3+
import { globalRegistry, z } from "zod";
44
import { NormalizedResponse, ResponseVariant } from "./api-response";
5-
import { hasForm, hasRaw, hasUpload } from "./deep-checks";
5+
import { findRequestTypeDefiningSchema } from "./deep-checks";
66
import {
77
FlatObject,
88
getActualMethod,
@@ -15,16 +15,20 @@ import {
1515
OutputValidationError,
1616
ResultHandlerError,
1717
} from "./errors";
18+
import { ezFormBrand } from "./form-schema";
1819
import { IOSchema } from "./io-schema";
1920
import { lastResortHandler } from "./last-resort";
2021
import { ActualLogger } from "./logger-helpers";
2122
import { LogicalContainer } from "./logical-container";
23+
import { metaSymbol } from "./metadata";
2224
import { AuxMethod, Method } from "./method";
2325
import { AbstractMiddleware, ExpressMiddleware } from "./middleware";
2426
import { ContentType } from "./content-type";
27+
import { ezRawBrand } from "./raw-schema";
2528
import { Routable } from "./routable";
2629
import { AbstractResultHandler } from "./result-handler";
2730
import { Security } from "./security";
31+
import { ezUploadBrand } from "./upload-schema";
2832

2933
export type Handler<IN, OUT, OPT> = (params: {
3034
/** @desc The inputs from the enabled input sources validated against the final input schema (incl. Middlewares) */
@@ -137,13 +141,14 @@ export class Endpoint<
137141

138142
/** @internal */
139143
public override get requestType() {
140-
return hasUpload(this.#def.inputSchema)
141-
? "upload"
142-
: hasRaw(this.#def.inputSchema)
143-
? "raw"
144-
: hasForm(this.#def.inputSchema)
145-
? "form"
146-
: "json";
144+
const found = findRequestTypeDefiningSchema(this.#def.inputSchema);
145+
if (found) {
146+
const { brand } = globalRegistry.get(found)?.[metaSymbol] || {};
147+
if (brand === ezUploadBrand) return "upload";
148+
if (brand === ezRawBrand) return "raw";
149+
if (brand === ezFormBrand) return "form";
150+
}
151+
return "json";
147152
}
148153

149154
/** @internal */

0 commit comments

Comments
 (0)