Skip to content

Commit 3cc3b05

Browse files
authored
Merge branch 'master' into root-tsconfig
2 parents 89481a0 + acfe2ab commit 3cc3b05

9 files changed

Lines changed: 433 additions & 398 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Version 25
44

5+
### v25.3.1
6+
7+
- Small optimization for running diagnostics (non-production mode);
8+
- Fixed the type of the `examples` property for `ez.dateIn()` and `ez.dateOut()` arguments.
9+
510
### v25.3.0
611

712
- Changed bundler from `tsup` to `tsdown`.

express-zod-api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "express-zod-api",
3-
"version": "25.3.0",
3+
"version": "25.3.1",
44
"description": "A Typescript framework to help you get an API server up and running with I/O schema validation and custom middlewares in minutes.",
55
"license": "MIT",
66
"repository": {

express-zod-api/src/date-in-schema.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { z } from "zod";
22

33
export const ezDateInBrand = Symbol("DateIn");
44

5-
export const dateIn = ({
6-
examples,
7-
...rest
8-
}: Parameters<z.ZodString["meta"]>[0] = {}) => {
5+
export interface DateInParams
6+
extends Omit<Parameters<z.ZodString["meta"]>[0], "examples"> {
7+
examples?: string[];
8+
}
9+
10+
export const dateIn = ({ examples, ...rest }: DateInParams = {}) => {
911
const schema = z.union([
1012
z.iso.date(),
1113
z.iso.datetime(),

express-zod-api/src/date-out-schema.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { z } from "zod";
22

33
export const ezDateOutBrand = Symbol("DateOut");
44

5-
export const dateOut = (meta: Parameters<z.ZodString["meta"]>[0] = {}) =>
5+
export interface DateOutParams
6+
extends Omit<Parameters<z.ZodString["meta"]>[0], "examples"> {
7+
examples?: string[];
8+
}
9+
10+
export const dateOut = (meta: DateOutParams = {}) =>
611
z
712
.date()
813
.transform((date) => date.toISOString())

express-zod-api/src/routing.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ export interface Routing {
2424

2525
export type Parsers = Partial<Record<ContentType, RequestHandler[]>>;
2626

27+
interface InitProps {
28+
app: IRouter;
29+
getLogger: GetLogger;
30+
config: CommonConfig;
31+
routing: Routing;
32+
parsers?: Parsers;
33+
}
34+
2735
const lineUp = (methods: CORSMethod[]) =>
2836
methods // auxiliary methods go last
2937
.sort((a, b) => +isMethod(b) - +isMethod(a) || a.localeCompare(b))
@@ -50,34 +58,31 @@ const makeCorsHeaders = (accessMethods: CORSMethod[]) => ({
5058

5159
type Siblings = Map<CORSMethod, [RequestHandler[], AbstractEndpoint]>;
5260

53-
export const initRouting = ({
61+
/** This fn exists to reduce the complexity of initRouting and to ensure the disposal of Diagnostics ASAP */
62+
const collectSiblings = ({
5463
app,
5564
getLogger,
5665
config,
5766
routing,
5867
parsers,
59-
}: {
60-
app: IRouter;
61-
getLogger: GetLogger;
62-
config: CommonConfig;
63-
routing: Routing;
64-
parsers?: Parsers;
65-
}) => {
66-
let doc = isProduction() ? undefined : new Diagnostics(getLogger()); // disposable
68+
}: InitProps) => {
69+
const doc = isProduction() ? undefined : new Diagnostics(getLogger());
6770
const familiar = new Map<string, Siblings>();
6871
const onEndpoint: OnEndpoint = (method, path, endpoint) => {
69-
if (!isProduction()) {
70-
doc?.checkSchema(endpoint, { path, method });
71-
doc?.checkPathParams(path, endpoint, { method });
72-
}
72+
doc?.checkSchema(endpoint, { path, method });
73+
doc?.checkPathParams(path, endpoint, { method });
7374
const matchingParsers = parsers?.[endpoint.requestType] || [];
7475
const value = R.pair(matchingParsers, endpoint);
7576
if (!familiar.has(path))
7677
familiar.set(path, new Map(config.cors ? [["options", value]] : []));
7778
familiar.get(path)?.set(method, value);
7879
};
7980
walkRouting({ routing, onEndpoint, onStatic: app.use.bind(app) });
80-
doc = undefined; // hint for garbage collector
81+
return familiar;
82+
};
83+
84+
export const initRouting = ({ app, config, getLogger, ...rest }: InitProps) => {
85+
const familiar = collectSiblings({ app, getLogger, config, ...rest });
8186
const deprioritized = new Map<string, RequestHandler>();
8287
for (const [path, methods] of familiar) {
8388
const accessMethods = Array.from(methods.keys());

express-zod-api/tests/__snapshots__/documentation.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,7 @@ paths:
18001800
uuid:
18011801
type: string
18021802
format: uuid
1803-
pattern: ^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000)$
1803+
pattern: ^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$
18041804
cuid:
18051805
type: string
18061806
format: cuid

express-zod-api/tests/env.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ describe("Environment checks", () => {
8181
});
8282

8383
describe("Zod new features", () => {
84+
test("Codecs can be reversed", () => {
85+
const schema = z.codec(z.iso.datetime(), z.date(), {
86+
decode: (str) => new Date(str),
87+
encode: (date) => date.toISOString(),
88+
});
89+
const {
90+
in: to,
91+
out: from,
92+
transform: encode,
93+
reverseTransform: decode,
94+
} = schema._zod.def;
95+
const reversed = z.codec(from, to, { decode, encode });
96+
expect(reversed.parse(new Date("2022-01-01T00:00:00.000Z"))).toBe(
97+
"2022-01-01T00:00:00.000Z",
98+
);
99+
});
100+
84101
test("ZodError equality", () => {
85102
try {
86103
z.number().parse("test");

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@
2929
"typescript-eslint": "catalog:dev",
3030
"vitest": "^3.2.3"
3131
},
32-
"packageManager": "pnpm@10.14.0+sha512.ad27a79641b49c3e481a16a805baa71817a04bbe06a38d17e60e2eaee83f6a146c6a688125f5792e48dd5ba30e7da52a5cda4c3992b9ccf333f9ce223af84748"
32+
"packageManager": "pnpm@10.15.0+sha512.486ebc259d3e999a4e8691ce03b5cac4a71cbeca39372a9b762cb500cfdf0873e2cb16abe3d951b1ee2cf012503f027b98b6584e4df22524e0c7450d9ec7aa7b"
3333
}

0 commit comments

Comments
 (0)