@efebia/fastify-zod-reply 1.4.3 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/routeHelpers.d.ts +11 -0
- package/lib/cjs/routeHelpers.js +17 -4
- package/lib/cjs/routeV4.d.ts +1 -1
- package/lib/cjs/routeV4.js +11 -21
- package/lib/esm/routeHelpers.d.ts +11 -0
- package/lib/esm/routeHelpers.js +15 -3
- package/lib/esm/routeV4.d.ts +1 -1
- package/lib/esm/routeV4.js +12 -22
- package/package.json +3 -2
|
@@ -15,3 +15,14 @@ export declare const strictifySchema: (schema: z.ZodType, strict: boolean) => an
|
|
|
15
15
|
export declare const parseStrict: (tag: keyof Exclude<NonNullable<RouteV4Options["strict"]>, boolean>, value: NonNullable<RouteV4Options["strict"]>) => boolean;
|
|
16
16
|
export declare const findStatusCode: (statusCode: number, availableStatusCodes: [string | number, any][]) => [string | number, any] | undefined;
|
|
17
17
|
export declare const mapZodError: (zodError: z.ZodError, prefix: string) => string;
|
|
18
|
+
export declare const buildZodJsonSchemaParams: (io: "input" | "output") => {
|
|
19
|
+
io: "input" | "output";
|
|
20
|
+
reused: "inline";
|
|
21
|
+
target: "draft-7";
|
|
22
|
+
unrepresentable: "any";
|
|
23
|
+
override(ctx: {
|
|
24
|
+
zodSchema: z.core.$ZodTypes;
|
|
25
|
+
jsonSchema: z.core.JSONSchema.BaseSchema;
|
|
26
|
+
path: (string | number)[];
|
|
27
|
+
}): void;
|
|
28
|
+
};
|
package/lib/cjs/routeHelpers.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.mapZodError = exports.findStatusCode = exports.parseStrict = exports.strictifySchema = exports.parse = void 0;
|
|
3
|
+
exports.buildZodJsonSchemaParams = exports.mapZodError = exports.findStatusCode = exports.parseStrict = exports.strictifySchema = exports.parse = void 0;
|
|
4
4
|
const parse = async (schema, payload, tag) => {
|
|
5
5
|
const result = await schema.safeParseAsync(payload);
|
|
6
6
|
return Object.assign(Object.assign({}, result), { tag });
|
|
@@ -9,9 +9,7 @@ exports.parse = parse;
|
|
|
9
9
|
const strictifySchema = (schema, strict) => {
|
|
10
10
|
if (!strict)
|
|
11
11
|
return schema;
|
|
12
|
-
return "strict" in schema && typeof schema["strict"] === "function"
|
|
13
|
-
? schema.strict()
|
|
14
|
-
: schema;
|
|
12
|
+
return "strict" in schema && typeof schema["strict"] === "function" ? schema.strict() : schema;
|
|
15
13
|
};
|
|
16
14
|
exports.strictifySchema = strictifySchema;
|
|
17
15
|
const parseStrict = (tag, value) => {
|
|
@@ -42,3 +40,18 @@ const mapZodError = (zodError, prefix) => {
|
|
|
42
40
|
.join("\n");
|
|
43
41
|
};
|
|
44
42
|
exports.mapZodError = mapZodError;
|
|
43
|
+
const buildZodJsonSchemaParams = (io) => {
|
|
44
|
+
return {
|
|
45
|
+
io,
|
|
46
|
+
reused: "inline",
|
|
47
|
+
target: "draft-7",
|
|
48
|
+
unrepresentable: "any",
|
|
49
|
+
override(ctx) {
|
|
50
|
+
const def = ctx.zodSchema._zod.def;
|
|
51
|
+
if (def.type === "date") {
|
|
52
|
+
ctx.jsonSchema.type = "string";
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
exports.buildZodJsonSchemaParams = buildZodJsonSchemaParams;
|
package/lib/cjs/routeV4.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ export type RouteV4Options = {
|
|
|
33
33
|
headers: boolean;
|
|
34
34
|
};
|
|
35
35
|
};
|
|
36
|
-
export declare const createRouteV4: <RequestAugmentation extends object = {}, ReplyAugmentation extends object = {}>({ strict: globalStrict }?: RouteV4Options) => <TSchema extends BaseZodV4Schema, FastifySchema extends FastifyZodV4Schema<TSchema> = FastifyZodV4Schema<TSchema>>(schema: TSchema, handler: NoInfer<APIHandler<FastifySchema, RequestAugmentation, ReplyAugmentation>>, options?: RouteV4Options) => APIOptions<FastifySchema> & {
|
|
36
|
+
export declare const createRouteV4: <RequestAugmentation extends object = {}, ReplyAugmentation extends object = {}>({ strict: globalStrict, }?: RouteV4Options) => <TSchema extends BaseZodV4Schema, FastifySchema extends FastifyZodV4Schema<TSchema> = FastifyZodV4Schema<TSchema>>(schema: TSchema, handler: NoInfer<APIHandler<FastifySchema, RequestAugmentation, ReplyAugmentation>>, options?: RouteV4Options) => APIOptions<FastifySchema> & {
|
|
37
37
|
handler: APIHandler<FastifySchema>;
|
|
38
38
|
};
|
|
39
39
|
export declare const routeV4: <TSchema extends BaseZodV4Schema, FastifySchema extends FastifyZodV4Schema<TSchema> = FastifyZodV4Schema<TSchema>>(schema: TSchema, handler: NoInfer<APIHandler<FastifySchema, {}, {}>>, options?: RouteV4Options) => APIOptions<FastifySchema> & {
|
package/lib/cjs/routeV4.js
CHANGED
|
@@ -4,20 +4,17 @@ exports.routeV4 = exports.createRouteV4 = void 0;
|
|
|
4
4
|
const v4_1 = require("zod/v4");
|
|
5
5
|
const error_js_1 = require("./error.js");
|
|
6
6
|
const routeHelpers_js_1 = require("./routeHelpers.js");
|
|
7
|
-
const createRouteV4 = ({ strict: globalStrict = false } = {}) => (schema, handler, options) => {
|
|
7
|
+
const createRouteV4 = ({ strict: globalStrict = false, } = {}) => (schema, handler, options) => {
|
|
8
8
|
const strict = typeof (options === null || options === void 0 ? void 0 : options.strict) !== "undefined" ? options === null || options === void 0 ? void 0 : options.strict : globalStrict;
|
|
9
9
|
const finalResult = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (schema.Body && {
|
|
10
|
-
body: v4_1.z.toJSONSchema((0, routeHelpers_js_1.strictifySchema)(schema.Body, (0, routeHelpers_js_1.parseStrict)("body", strict)),
|
|
10
|
+
body: v4_1.z.toJSONSchema((0, routeHelpers_js_1.strictifySchema)(schema.Body, (0, routeHelpers_js_1.parseStrict)("body", strict)), (0, routeHelpers_js_1.buildZodJsonSchemaParams)('input')),
|
|
11
11
|
})), (schema.Params && {
|
|
12
|
-
params: v4_1.z.toJSONSchema((0, routeHelpers_js_1.strictifySchema)(schema.Params, (0, routeHelpers_js_1.parseStrict)("params", strict)),
|
|
12
|
+
params: v4_1.z.toJSONSchema((0, routeHelpers_js_1.strictifySchema)(schema.Params, (0, routeHelpers_js_1.parseStrict)("params", strict)), (0, routeHelpers_js_1.buildZodJsonSchemaParams)('input')),
|
|
13
13
|
})), (schema.Query && {
|
|
14
|
-
querystring: v4_1.z.toJSONSchema((0, routeHelpers_js_1.strictifySchema)(schema.Query, (0, routeHelpers_js_1.parseStrict)("query", strict)),
|
|
14
|
+
querystring: v4_1.z.toJSONSchema((0, routeHelpers_js_1.strictifySchema)(schema.Query, (0, routeHelpers_js_1.parseStrict)("query", strict)), (0, routeHelpers_js_1.buildZodJsonSchemaParams)('input')),
|
|
15
15
|
})), (schema.Headers && {
|
|
16
|
-
headers: v4_1.z.toJSONSchema((0, routeHelpers_js_1.strictifySchema)(schema.Headers, (0, routeHelpers_js_1.parseStrict)("headers", strict)),
|
|
17
|
-
})), { response: v4_1.z.toJSONSchema(schema.Reply.partial(), {
|
|
18
|
-
reused: "inline",
|
|
19
|
-
target: "draft-7",
|
|
20
|
-
})["properties"] }), (schema.Security && { security: schema.Security })), (schema.Tags && { tags: schema.Tags })), (schema.Description && { description: schema.Description })), (schema.Summary && { summary: schema.Summary })), (schema.Notes && { notes: schema.Notes }));
|
|
16
|
+
headers: v4_1.z.toJSONSchema((0, routeHelpers_js_1.strictifySchema)(schema.Headers, (0, routeHelpers_js_1.parseStrict)("headers", strict)), (0, routeHelpers_js_1.buildZodJsonSchemaParams)('input')),
|
|
17
|
+
})), { response: v4_1.z.toJSONSchema(schema.Reply.partial(), (0, routeHelpers_js_1.buildZodJsonSchemaParams)('output'))["properties"] }), (schema.Security && { security: schema.Security })), (schema.Tags && { tags: schema.Tags })), (schema.Description && { description: schema.Description })), (schema.Summary && { summary: schema.Summary })), (schema.Notes && { notes: schema.Notes }));
|
|
21
18
|
return {
|
|
22
19
|
schema: finalResult,
|
|
23
20
|
handler,
|
|
@@ -25,12 +22,8 @@ const createRouteV4 = ({ strict: globalStrict = false } = {}) => (schema, handle
|
|
|
25
22
|
var _a, _b, _c;
|
|
26
23
|
const results = await Promise.all([
|
|
27
24
|
...(schema.Body ? [(0, routeHelpers_js_1.parse)(schema.Body, request.body, "body")] : []),
|
|
28
|
-
...(schema.Params
|
|
29
|
-
|
|
30
|
-
: []),
|
|
31
|
-
...(schema.Query
|
|
32
|
-
? [(0, routeHelpers_js_1.parse)(schema.Query, request.query, "query")]
|
|
33
|
-
: []),
|
|
25
|
+
...(schema.Params ? [(0, routeHelpers_js_1.parse)(schema.Params, request.params, "params")] : []),
|
|
26
|
+
...(schema.Query ? [(0, routeHelpers_js_1.parse)(schema.Query, request.query, "query")] : []),
|
|
34
27
|
]);
|
|
35
28
|
for (const result of results) {
|
|
36
29
|
if (!result.success) {
|
|
@@ -42,12 +35,9 @@ const createRouteV4 = ({ strict: globalStrict = false } = {}) => (schema, handle
|
|
|
42
35
|
});
|
|
43
36
|
}
|
|
44
37
|
}
|
|
45
|
-
request.body =
|
|
46
|
-
|
|
47
|
-
request.
|
|
48
|
-
((_b = results.find((r) => r.tag === "params")) === null || _b === void 0 ? void 0 : _b.data) || {};
|
|
49
|
-
request.query =
|
|
50
|
-
((_c = results.find((r) => r.tag === "query")) === null || _c === void 0 ? void 0 : _c.data) || {};
|
|
38
|
+
request.body = ((_a = results.find((r) => r.tag === "body")) === null || _a === void 0 ? void 0 : _a.data) || {};
|
|
39
|
+
request.params = ((_b = results.find((r) => r.tag === "params")) === null || _b === void 0 ? void 0 : _b.data) || {};
|
|
40
|
+
request.query = ((_c = results.find((r) => r.tag === "query")) === null || _c === void 0 ? void 0 : _c.data) || {};
|
|
51
41
|
},
|
|
52
42
|
preSerialization: (request, reply, payload, done) => {
|
|
53
43
|
const foundSchema = (0, routeHelpers_js_1.findStatusCode)(reply.statusCode, Object.entries(schema.Reply.shape));
|
|
@@ -15,3 +15,14 @@ export declare const strictifySchema: (schema: z.ZodType, strict: boolean) => an
|
|
|
15
15
|
export declare const parseStrict: (tag: keyof Exclude<NonNullable<RouteV4Options["strict"]>, boolean>, value: NonNullable<RouteV4Options["strict"]>) => boolean;
|
|
16
16
|
export declare const findStatusCode: (statusCode: number, availableStatusCodes: [string | number, any][]) => [string | number, any] | undefined;
|
|
17
17
|
export declare const mapZodError: (zodError: z.ZodError, prefix: string) => string;
|
|
18
|
+
export declare const buildZodJsonSchemaParams: (io: "input" | "output") => {
|
|
19
|
+
io: "input" | "output";
|
|
20
|
+
reused: "inline";
|
|
21
|
+
target: "draft-7";
|
|
22
|
+
unrepresentable: "any";
|
|
23
|
+
override(ctx: {
|
|
24
|
+
zodSchema: z.core.$ZodTypes;
|
|
25
|
+
jsonSchema: z.core.JSONSchema.BaseSchema;
|
|
26
|
+
path: (string | number)[];
|
|
27
|
+
}): void;
|
|
28
|
+
};
|
package/lib/esm/routeHelpers.js
CHANGED
|
@@ -5,9 +5,7 @@ export const parse = async (schema, payload, tag) => {
|
|
|
5
5
|
export const strictifySchema = (schema, strict) => {
|
|
6
6
|
if (!strict)
|
|
7
7
|
return schema;
|
|
8
|
-
return "strict" in schema && typeof schema["strict"] === "function"
|
|
9
|
-
? schema.strict()
|
|
10
|
-
: schema;
|
|
8
|
+
return "strict" in schema && typeof schema["strict"] === "function" ? schema.strict() : schema;
|
|
11
9
|
};
|
|
12
10
|
export const parseStrict = (tag, value) => {
|
|
13
11
|
if (typeof value === "boolean")
|
|
@@ -34,3 +32,17 @@ export const mapZodError = (zodError, prefix) => {
|
|
|
34
32
|
})
|
|
35
33
|
.join("\n");
|
|
36
34
|
};
|
|
35
|
+
export const buildZodJsonSchemaParams = (io) => {
|
|
36
|
+
return {
|
|
37
|
+
io,
|
|
38
|
+
reused: "inline",
|
|
39
|
+
target: "draft-7",
|
|
40
|
+
unrepresentable: "any",
|
|
41
|
+
override(ctx) {
|
|
42
|
+
const def = ctx.zodSchema._zod.def;
|
|
43
|
+
if (def.type === "date") {
|
|
44
|
+
ctx.jsonSchema.type = "string";
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
};
|
package/lib/esm/routeV4.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ export type RouteV4Options = {
|
|
|
33
33
|
headers: boolean;
|
|
34
34
|
};
|
|
35
35
|
};
|
|
36
|
-
export declare const createRouteV4: <RequestAugmentation extends object = {}, ReplyAugmentation extends object = {}>({ strict: globalStrict }?: RouteV4Options) => <TSchema extends BaseZodV4Schema, FastifySchema extends FastifyZodV4Schema<TSchema> = FastifyZodV4Schema<TSchema>>(schema: TSchema, handler: NoInfer<APIHandler<FastifySchema, RequestAugmentation, ReplyAugmentation>>, options?: RouteV4Options) => APIOptions<FastifySchema> & {
|
|
36
|
+
export declare const createRouteV4: <RequestAugmentation extends object = {}, ReplyAugmentation extends object = {}>({ strict: globalStrict, }?: RouteV4Options) => <TSchema extends BaseZodV4Schema, FastifySchema extends FastifyZodV4Schema<TSchema> = FastifyZodV4Schema<TSchema>>(schema: TSchema, handler: NoInfer<APIHandler<FastifySchema, RequestAugmentation, ReplyAugmentation>>, options?: RouteV4Options) => APIOptions<FastifySchema> & {
|
|
37
37
|
handler: APIHandler<FastifySchema>;
|
|
38
38
|
};
|
|
39
39
|
export declare const routeV4: <TSchema extends BaseZodV4Schema, FastifySchema extends FastifyZodV4Schema<TSchema> = FastifyZodV4Schema<TSchema>>(schema: TSchema, handler: NoInfer<APIHandler<FastifySchema, {}, {}>>, options?: RouteV4Options) => APIOptions<FastifySchema> & {
|
package/lib/esm/routeV4.js
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
1
1
|
import { z } from "zod/v4";
|
|
2
2
|
import { FastifyZodReplyError } from "./error.js";
|
|
3
|
-
import { findStatusCode, mapZodError, parse, parseStrict, strictifySchema
|
|
4
|
-
export const createRouteV4 = ({ strict: globalStrict = false } = {}) => (schema, handler, options) => {
|
|
3
|
+
import { buildZodJsonSchemaParams, findStatusCode, mapZodError, parse, parseStrict, strictifySchema } from "./routeHelpers.js";
|
|
4
|
+
export const createRouteV4 = ({ strict: globalStrict = false, } = {}) => (schema, handler, options) => {
|
|
5
5
|
const strict = typeof (options === null || options === void 0 ? void 0 : options.strict) !== "undefined" ? options === null || options === void 0 ? void 0 : options.strict : globalStrict;
|
|
6
6
|
const finalResult = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (schema.Body && {
|
|
7
|
-
body: z.toJSONSchema(strictifySchema(schema.Body, parseStrict("body", strict)),
|
|
7
|
+
body: z.toJSONSchema(strictifySchema(schema.Body, parseStrict("body", strict)), buildZodJsonSchemaParams('input')),
|
|
8
8
|
})), (schema.Params && {
|
|
9
|
-
params: z.toJSONSchema(strictifySchema(schema.Params, parseStrict("params", strict)),
|
|
9
|
+
params: z.toJSONSchema(strictifySchema(schema.Params, parseStrict("params", strict)), buildZodJsonSchemaParams('input')),
|
|
10
10
|
})), (schema.Query && {
|
|
11
|
-
querystring: z.toJSONSchema(strictifySchema(schema.Query, parseStrict("query", strict)),
|
|
11
|
+
querystring: z.toJSONSchema(strictifySchema(schema.Query, parseStrict("query", strict)), buildZodJsonSchemaParams('input')),
|
|
12
12
|
})), (schema.Headers && {
|
|
13
|
-
headers: z.toJSONSchema(strictifySchema(schema.Headers, parseStrict("headers", strict)),
|
|
14
|
-
})), { response: z.toJSONSchema(schema.Reply.partial(), {
|
|
15
|
-
reused: "inline",
|
|
16
|
-
target: "draft-7",
|
|
17
|
-
})["properties"] }), (schema.Security && { security: schema.Security })), (schema.Tags && { tags: schema.Tags })), (schema.Description && { description: schema.Description })), (schema.Summary && { summary: schema.Summary })), (schema.Notes && { notes: schema.Notes }));
|
|
13
|
+
headers: z.toJSONSchema(strictifySchema(schema.Headers, parseStrict("headers", strict)), buildZodJsonSchemaParams('input')),
|
|
14
|
+
})), { response: z.toJSONSchema(schema.Reply.partial(), buildZodJsonSchemaParams('output'))["properties"] }), (schema.Security && { security: schema.Security })), (schema.Tags && { tags: schema.Tags })), (schema.Description && { description: schema.Description })), (schema.Summary && { summary: schema.Summary })), (schema.Notes && { notes: schema.Notes }));
|
|
18
15
|
return {
|
|
19
16
|
schema: finalResult,
|
|
20
17
|
handler,
|
|
@@ -22,12 +19,8 @@ export const createRouteV4 = ({ strict: globalStrict = false } = {}) => (schema,
|
|
|
22
19
|
var _a, _b, _c;
|
|
23
20
|
const results = await Promise.all([
|
|
24
21
|
...(schema.Body ? [parse(schema.Body, request.body, "body")] : []),
|
|
25
|
-
...(schema.Params
|
|
26
|
-
|
|
27
|
-
: []),
|
|
28
|
-
...(schema.Query
|
|
29
|
-
? [parse(schema.Query, request.query, "query")]
|
|
30
|
-
: []),
|
|
22
|
+
...(schema.Params ? [parse(schema.Params, request.params, "params")] : []),
|
|
23
|
+
...(schema.Query ? [parse(schema.Query, request.query, "query")] : []),
|
|
31
24
|
]);
|
|
32
25
|
for (const result of results) {
|
|
33
26
|
if (!result.success) {
|
|
@@ -39,12 +32,9 @@ export const createRouteV4 = ({ strict: globalStrict = false } = {}) => (schema,
|
|
|
39
32
|
});
|
|
40
33
|
}
|
|
41
34
|
}
|
|
42
|
-
request.body =
|
|
43
|
-
|
|
44
|
-
request.
|
|
45
|
-
((_b = results.find((r) => r.tag === "params")) === null || _b === void 0 ? void 0 : _b.data) || {};
|
|
46
|
-
request.query =
|
|
47
|
-
((_c = results.find((r) => r.tag === "query")) === null || _c === void 0 ? void 0 : _c.data) || {};
|
|
35
|
+
request.body = ((_a = results.find((r) => r.tag === "body")) === null || _a === void 0 ? void 0 : _a.data) || {};
|
|
36
|
+
request.params = ((_b = results.find((r) => r.tag === "params")) === null || _b === void 0 ? void 0 : _b.data) || {};
|
|
37
|
+
request.query = ((_c = results.find((r) => r.tag === "query")) === null || _c === void 0 ? void 0 : _c.data) || {};
|
|
48
38
|
},
|
|
49
39
|
preSerialization: (request, reply, payload, done) => {
|
|
50
40
|
const foundSchema = findStatusCode(reply.statusCode, Object.entries(schema.Reply.shape));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@efebia/fastify-zod-reply",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"fastify": "^5.3.0",
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
"build:esm": "tsc -p ./tsconfig.build.json --module nodenext --moduleResolution nodenext --outDir lib/esm",
|
|
15
15
|
"build:cjs": "tsc -p ./tsconfig.build.json --module commonjs --moduleResolution node --outDir lib/cjs",
|
|
16
16
|
"build": "yarn build:esm && yarn build:cjs",
|
|
17
|
-
"test": "node --import tsx --test ./**/*.test.ts"
|
|
17
|
+
"test": "node --import tsx --test ./**/*.test.ts",
|
|
18
|
+
"test:only": "node --import tsx --test-only ./**/*.test.ts"
|
|
18
19
|
},
|
|
19
20
|
"main": "./lib/esm/index.js",
|
|
20
21
|
"types": "./lib/esm/index.d.ts",
|