@efebia/fastify-zod-reply 1.0.6 → 1.1.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/reply.js CHANGED
@@ -7,7 +7,8 @@ const createReply = (statusCode, defaultPayload) => {
7
7
  const finalPayload = payload !== null && payload !== void 0 ? payload : defaultPayload;
8
8
  if (typeof finalPayload === 'string')
9
9
  throw (0, exports.createError)(statusCode)(finalPayload);
10
- this.type("application/json");
10
+ if (typeof finalPayload !== 'undefined')
11
+ this.type("application/json");
11
12
  this.code(statusCode);
12
13
  return finalPayload;
13
14
  };
@@ -15,6 +15,17 @@ export type FastifyZodSchema<TZodSchema extends BaseZodSchema> = {
15
15
  Querystring: TZodSchema['Query'] extends z.ZodTypeAny ? z.output<TZodSchema['Query']> : undefined;
16
16
  Reply: TZodSchema['Reply'] extends z.ZodTypeAny ? z.input<TZodSchema['Reply']>[keyof z.infer<TZodSchema['Reply']>] : undefined;
17
17
  };
18
- export declare const route: <TSchema extends BaseZodSchema, FastifySchema extends FastifyZodSchema<TSchema> = FastifyZodSchema<TSchema>>(schema: TSchema, handler: APIHandler<FastifySchema>) => APIOptions<FastifySchema> & {
18
+ export type RouteOptions = {
19
+ strict?: boolean | {
20
+ body: boolean;
21
+ query: boolean;
22
+ params: boolean;
23
+ headers: boolean;
24
+ };
25
+ };
26
+ export declare const createRoute: ({ strict: globalStrict }?: RouteOptions) => <TSchema extends BaseZodSchema, FastifySchema extends FastifyZodSchema<TSchema> = FastifyZodSchema<TSchema>>(schema: TSchema, handler: APIHandler<FastifySchema>, options?: RouteOptions) => APIOptions<FastifySchema> & {
27
+ handler: APIHandler<FastifySchema>;
28
+ };
29
+ export declare const route: <TSchema extends BaseZodSchema, FastifySchema extends FastifyZodSchema<TSchema> = FastifyZodSchema<TSchema>>(schema: TSchema, handler: APIHandler<FastifySchema>, options?: RouteOptions) => APIOptions<FastifySchema> & {
19
30
  handler: APIHandler<FastifySchema>;
20
31
  };
package/lib/cjs/route.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.route = void 0;
3
+ exports.route = exports.createRoute = void 0;
4
4
  const zod_to_json_schema_1 = require("zod-to-json-schema");
5
5
  const mapZodError = (zodError, prefix) => zodError.errors.map(issue => `Error at ${prefix}->${issue.path.join('->')}`).join(';\n');
6
6
  const parse = async (schema, payload, tag) => {
@@ -19,8 +19,19 @@ const findStatusCode = (statusCode, availableStatusCodes) => {
19
19
  return statusCode.toString()[0] === key[0];
20
20
  });
21
21
  };
22
- const route = (schema, handler) => {
23
- const finalResult = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (schema.Body && { body: (0, zod_to_json_schema_1.zodToJsonSchema)(schema.Body, { $refStrategy: 'none' }) })), (schema.Params && { params: (0, zod_to_json_schema_1.zodToJsonSchema)(schema.Params, { $refStrategy: 'none' }) })), (schema.Query && { querystring: (0, zod_to_json_schema_1.zodToJsonSchema)(schema.Query, { $refStrategy: 'none' }) })), (schema.Headers && { headers: (0, zod_to_json_schema_1.zodToJsonSchema)(schema.Headers, { $refStrategy: 'none' }) })), { response: (0, zod_to_json_schema_1.zodToJsonSchema)(schema.Reply.partial(), {
22
+ const strictifySchema = (schema, strict) => {
23
+ if (!strict)
24
+ return schema;
25
+ return 'strict' in schema && typeof schema['strict'] === 'function' ? schema.strict() : schema;
26
+ };
27
+ const parseStrict = (tag, value) => {
28
+ if (typeof value === 'boolean')
29
+ return value;
30
+ return value[tag];
31
+ };
32
+ const createRoute = ({ strict: globalStrict = false } = {}) => (schema, handler, options) => {
33
+ const strict = typeof (options === null || options === void 0 ? void 0 : options.strict) !== 'undefined' ? options === null || options === void 0 ? void 0 : options.strict : globalStrict;
34
+ const finalResult = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (schema.Body && { body: (0, zod_to_json_schema_1.zodToJsonSchema)(strictifySchema(schema.Body, parseStrict('body', strict)), { $refStrategy: 'none', removeAdditionalStrategy: 'strict', allowedAdditionalProperties: undefined }) })), (schema.Params && { params: (0, zod_to_json_schema_1.zodToJsonSchema)(strictifySchema(schema.Params, parseStrict('params', strict)), { $refStrategy: 'none', removeAdditionalStrategy: 'strict', allowedAdditionalProperties: undefined }) })), (schema.Query && { querystring: (0, zod_to_json_schema_1.zodToJsonSchema)(strictifySchema(schema.Query, parseStrict('query', strict)), { $refStrategy: 'none', removeAdditionalStrategy: 'strict', allowedAdditionalProperties: undefined }) })), (schema.Headers && { headers: (0, zod_to_json_schema_1.zodToJsonSchema)(strictifySchema(schema.Headers, parseStrict('headers', strict)), { $refStrategy: 'none', removeAdditionalStrategy: 'strict', allowedAdditionalProperties: undefined }) })), { response: (0, zod_to_json_schema_1.zodToJsonSchema)(schema.Reply.partial(), {
24
35
  $refStrategy: 'none',
25
36
  strictUnions: true,
26
37
  })['properties'] }), (schema.Security && { security: schema.Security })), (schema.Tags && { tags: schema.Tags }));
@@ -62,4 +73,5 @@ const route = (schema, handler) => {
62
73
  },
63
74
  };
64
75
  };
65
- exports.route = route;
76
+ exports.createRoute = createRoute;
77
+ exports.route = (0, exports.createRoute)({ strict: false });
@@ -15,6 +15,17 @@ export type FastifyZodV4Schema<TZodSchema extends BaseZodV4Schema> = {
15
15
  Querystring: TZodSchema['Query'] extends z.ZodTypeAny ? z.output<TZodSchema['Query']> : undefined;
16
16
  Reply: TZodSchema['Reply'] extends z.ZodTypeAny ? z.input<TZodSchema['Reply']>[keyof z.input<TZodSchema['Reply']>] : undefined;
17
17
  };
18
- export declare const routeV4: <TSchema extends BaseZodV4Schema, FastifySchema extends FastifyZodV4Schema<TSchema> = FastifyZodV4Schema<TSchema>>(schema: TSchema, handler: APIHandler<FastifySchema>) => APIOptions<FastifySchema> & {
18
+ export type RouteV4Options = {
19
+ strict?: boolean | {
20
+ body: boolean;
21
+ query: boolean;
22
+ params: boolean;
23
+ headers: boolean;
24
+ };
25
+ };
26
+ export declare const createRouteV4: ({ strict: globalStrict }?: RouteV4Options) => <TSchema extends BaseZodV4Schema, FastifySchema extends FastifyZodV4Schema<TSchema> = FastifyZodV4Schema<TSchema>>(schema: TSchema, handler: APIHandler<FastifySchema>, options?: RouteV4Options) => APIOptions<FastifySchema> & {
27
+ handler: APIHandler<FastifySchema>;
28
+ };
29
+ export declare const routeV4: <TSchema extends BaseZodV4Schema, FastifySchema extends FastifyZodV4Schema<TSchema> = FastifyZodV4Schema<TSchema>>(schema: TSchema, handler: APIHandler<FastifySchema>, options?: RouteV4Options) => APIOptions<FastifySchema> & {
19
30
  handler: APIHandler<FastifySchema>;
20
31
  };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.routeV4 = void 0;
3
+ exports.routeV4 = exports.createRouteV4 = void 0;
4
4
  const v4_1 = require("zod/v4");
5
5
  const mapZodError = (zodError, prefix) => {
6
6
  return zodError.issues.map(issue => {
@@ -24,8 +24,19 @@ const findStatusCode = (statusCode, availableStatusCodes) => {
24
24
  return statusCode.toString()[0] === key[0];
25
25
  });
26
26
  };
27
- const routeV4 = (schema, handler) => {
28
- const finalResult = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (schema.Body && { body: v4_1.z.toJSONSchema(schema.Body, { reused: 'inline', target: "draft-7", io: 'input' }) })), (schema.Params && { params: v4_1.z.toJSONSchema(schema.Params, { reused: 'inline', target: "draft-7", io: 'input' }) })), (schema.Query && { querystring: v4_1.z.toJSONSchema(schema.Query, { reused: 'inline', target: "draft-7", io: 'input' }) })), (schema.Headers && { headers: v4_1.z.toJSONSchema(schema.Headers, { reused: 'inline', target: "draft-7", io: 'input' }) })), { response: v4_1.z.toJSONSchema(schema.Reply.partial(), { reused: 'inline', target: "draft-7" })['properties'] }), (schema.Security && { security: schema.Security })), (schema.Tags && { tags: schema.Tags }));
27
+ const strictifySchema = (schema, strict) => {
28
+ if (!strict)
29
+ return schema;
30
+ return 'strict' in schema && typeof schema['strict'] === 'function' ? schema.strict() : schema;
31
+ };
32
+ const parseStrict = (tag, value) => {
33
+ if (typeof value === 'boolean')
34
+ return value;
35
+ return value[tag];
36
+ };
37
+ const createRouteV4 = ({ strict: globalStrict = false } = {}) => (schema, handler, options) => {
38
+ const strict = typeof (options === null || options === void 0 ? void 0 : options.strict) !== 'undefined' ? options === null || options === void 0 ? void 0 : options.strict : globalStrict;
39
+ const finalResult = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (schema.Body && { body: v4_1.z.toJSONSchema(strictifySchema(schema.Body, parseStrict('body', strict)), { reused: 'inline', target: "draft-7", io: 'input' }) })), (schema.Params && { params: v4_1.z.toJSONSchema(strictifySchema(schema.Params, parseStrict('params', strict)), { reused: 'inline', target: "draft-7", io: 'input' }) })), (schema.Query && { querystring: v4_1.z.toJSONSchema(strictifySchema(schema.Query, parseStrict('query', strict)), { reused: 'inline', target: "draft-7", io: 'input' }) })), (schema.Headers && { headers: v4_1.z.toJSONSchema(strictifySchema(schema.Headers, parseStrict('headers', strict)), { reused: 'inline', target: "draft-7", io: 'input' }) })), { response: v4_1.z.toJSONSchema(schema.Reply.partial(), { reused: 'inline', target: "draft-7" })['properties'] }), (schema.Security && { security: schema.Security })), (schema.Tags && { tags: schema.Tags }));
29
40
  return {
30
41
  schema: finalResult,
31
42
  handler,
@@ -64,4 +75,5 @@ const routeV4 = (schema, handler) => {
64
75
  },
65
76
  };
66
77
  };
67
- exports.routeV4 = routeV4;
78
+ exports.createRouteV4 = createRouteV4;
79
+ exports.routeV4 = (0, exports.createRouteV4)({ strict: false });
package/lib/esm/reply.js CHANGED
@@ -4,7 +4,8 @@ export const createReply = (statusCode, defaultPayload) => {
4
4
  const finalPayload = payload !== null && payload !== void 0 ? payload : defaultPayload;
5
5
  if (typeof finalPayload === 'string')
6
6
  throw createError(statusCode)(finalPayload);
7
- this.type("application/json");
7
+ if (typeof finalPayload !== 'undefined')
8
+ this.type("application/json");
8
9
  this.code(statusCode);
9
10
  return finalPayload;
10
11
  };
@@ -15,6 +15,17 @@ export type FastifyZodSchema<TZodSchema extends BaseZodSchema> = {
15
15
  Querystring: TZodSchema['Query'] extends z.ZodTypeAny ? z.output<TZodSchema['Query']> : undefined;
16
16
  Reply: TZodSchema['Reply'] extends z.ZodTypeAny ? z.input<TZodSchema['Reply']>[keyof z.infer<TZodSchema['Reply']>] : undefined;
17
17
  };
18
- export declare const route: <TSchema extends BaseZodSchema, FastifySchema extends FastifyZodSchema<TSchema> = FastifyZodSchema<TSchema>>(schema: TSchema, handler: APIHandler<FastifySchema>) => APIOptions<FastifySchema> & {
18
+ export type RouteOptions = {
19
+ strict?: boolean | {
20
+ body: boolean;
21
+ query: boolean;
22
+ params: boolean;
23
+ headers: boolean;
24
+ };
25
+ };
26
+ export declare const createRoute: ({ strict: globalStrict }?: RouteOptions) => <TSchema extends BaseZodSchema, FastifySchema extends FastifyZodSchema<TSchema> = FastifyZodSchema<TSchema>>(schema: TSchema, handler: APIHandler<FastifySchema>, options?: RouteOptions) => APIOptions<FastifySchema> & {
27
+ handler: APIHandler<FastifySchema>;
28
+ };
29
+ export declare const route: <TSchema extends BaseZodSchema, FastifySchema extends FastifyZodSchema<TSchema> = FastifyZodSchema<TSchema>>(schema: TSchema, handler: APIHandler<FastifySchema>, options?: RouteOptions) => APIOptions<FastifySchema> & {
19
30
  handler: APIHandler<FastifySchema>;
20
31
  };
package/lib/esm/route.js CHANGED
@@ -16,8 +16,19 @@ const findStatusCode = (statusCode, availableStatusCodes) => {
16
16
  return statusCode.toString()[0] === key[0];
17
17
  });
18
18
  };
19
- export const route = (schema, handler) => {
20
- const finalResult = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (schema.Body && { body: zodToJsonSchema(schema.Body, { $refStrategy: 'none' }) })), (schema.Params && { params: zodToJsonSchema(schema.Params, { $refStrategy: 'none' }) })), (schema.Query && { querystring: zodToJsonSchema(schema.Query, { $refStrategy: 'none' }) })), (schema.Headers && { headers: zodToJsonSchema(schema.Headers, { $refStrategy: 'none' }) })), { response: zodToJsonSchema(schema.Reply.partial(), {
19
+ const strictifySchema = (schema, strict) => {
20
+ if (!strict)
21
+ return schema;
22
+ return 'strict' in schema && typeof schema['strict'] === 'function' ? schema.strict() : schema;
23
+ };
24
+ const parseStrict = (tag, value) => {
25
+ if (typeof value === 'boolean')
26
+ return value;
27
+ return value[tag];
28
+ };
29
+ export const createRoute = ({ strict: globalStrict = false } = {}) => (schema, handler, options) => {
30
+ const strict = typeof (options === null || options === void 0 ? void 0 : options.strict) !== 'undefined' ? options === null || options === void 0 ? void 0 : options.strict : globalStrict;
31
+ const finalResult = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (schema.Body && { body: zodToJsonSchema(strictifySchema(schema.Body, parseStrict('body', strict)), { $refStrategy: 'none', removeAdditionalStrategy: 'strict', allowedAdditionalProperties: undefined }) })), (schema.Params && { params: zodToJsonSchema(strictifySchema(schema.Params, parseStrict('params', strict)), { $refStrategy: 'none', removeAdditionalStrategy: 'strict', allowedAdditionalProperties: undefined }) })), (schema.Query && { querystring: zodToJsonSchema(strictifySchema(schema.Query, parseStrict('query', strict)), { $refStrategy: 'none', removeAdditionalStrategy: 'strict', allowedAdditionalProperties: undefined }) })), (schema.Headers && { headers: zodToJsonSchema(strictifySchema(schema.Headers, parseStrict('headers', strict)), { $refStrategy: 'none', removeAdditionalStrategy: 'strict', allowedAdditionalProperties: undefined }) })), { response: zodToJsonSchema(schema.Reply.partial(), {
21
32
  $refStrategy: 'none',
22
33
  strictUnions: true,
23
34
  })['properties'] }), (schema.Security && { security: schema.Security })), (schema.Tags && { tags: schema.Tags }));
@@ -59,3 +70,4 @@ export const route = (schema, handler) => {
59
70
  },
60
71
  };
61
72
  };
73
+ export const route = createRoute({ strict: false });
@@ -15,6 +15,17 @@ export type FastifyZodV4Schema<TZodSchema extends BaseZodV4Schema> = {
15
15
  Querystring: TZodSchema['Query'] extends z.ZodTypeAny ? z.output<TZodSchema['Query']> : undefined;
16
16
  Reply: TZodSchema['Reply'] extends z.ZodTypeAny ? z.input<TZodSchema['Reply']>[keyof z.input<TZodSchema['Reply']>] : undefined;
17
17
  };
18
- export declare const routeV4: <TSchema extends BaseZodV4Schema, FastifySchema extends FastifyZodV4Schema<TSchema> = FastifyZodV4Schema<TSchema>>(schema: TSchema, handler: APIHandler<FastifySchema>) => APIOptions<FastifySchema> & {
18
+ export type RouteV4Options = {
19
+ strict?: boolean | {
20
+ body: boolean;
21
+ query: boolean;
22
+ params: boolean;
23
+ headers: boolean;
24
+ };
25
+ };
26
+ export declare const createRouteV4: ({ strict: globalStrict }?: RouteV4Options) => <TSchema extends BaseZodV4Schema, FastifySchema extends FastifyZodV4Schema<TSchema> = FastifyZodV4Schema<TSchema>>(schema: TSchema, handler: APIHandler<FastifySchema>, options?: RouteV4Options) => APIOptions<FastifySchema> & {
27
+ handler: APIHandler<FastifySchema>;
28
+ };
29
+ export declare const routeV4: <TSchema extends BaseZodV4Schema, FastifySchema extends FastifyZodV4Schema<TSchema> = FastifyZodV4Schema<TSchema>>(schema: TSchema, handler: APIHandler<FastifySchema>, options?: RouteV4Options) => APIOptions<FastifySchema> & {
19
30
  handler: APIHandler<FastifySchema>;
20
31
  };
@@ -21,8 +21,19 @@ const findStatusCode = (statusCode, availableStatusCodes) => {
21
21
  return statusCode.toString()[0] === key[0];
22
22
  });
23
23
  };
24
- export const routeV4 = (schema, handler) => {
25
- const finalResult = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (schema.Body && { body: z.toJSONSchema(schema.Body, { reused: 'inline', target: "draft-7", io: 'input' }) })), (schema.Params && { params: z.toJSONSchema(schema.Params, { reused: 'inline', target: "draft-7", io: 'input' }) })), (schema.Query && { querystring: z.toJSONSchema(schema.Query, { reused: 'inline', target: "draft-7", io: 'input' }) })), (schema.Headers && { headers: z.toJSONSchema(schema.Headers, { reused: 'inline', target: "draft-7", io: 'input' }) })), { response: z.toJSONSchema(schema.Reply.partial(), { reused: 'inline', target: "draft-7" })['properties'] }), (schema.Security && { security: schema.Security })), (schema.Tags && { tags: schema.Tags }));
24
+ const strictifySchema = (schema, strict) => {
25
+ if (!strict)
26
+ return schema;
27
+ return 'strict' in schema && typeof schema['strict'] === 'function' ? schema.strict() : schema;
28
+ };
29
+ const parseStrict = (tag, value) => {
30
+ if (typeof value === 'boolean')
31
+ return value;
32
+ return value[tag];
33
+ };
34
+ export const createRouteV4 = ({ strict: globalStrict = false } = {}) => (schema, handler, options) => {
35
+ const strict = typeof (options === null || options === void 0 ? void 0 : options.strict) !== 'undefined' ? options === null || options === void 0 ? void 0 : options.strict : globalStrict;
36
+ const finalResult = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (schema.Body && { body: z.toJSONSchema(strictifySchema(schema.Body, parseStrict('body', strict)), { reused: 'inline', target: "draft-7", io: 'input' }) })), (schema.Params && { params: z.toJSONSchema(strictifySchema(schema.Params, parseStrict('params', strict)), { reused: 'inline', target: "draft-7", io: 'input' }) })), (schema.Query && { querystring: z.toJSONSchema(strictifySchema(schema.Query, parseStrict('query', strict)), { reused: 'inline', target: "draft-7", io: 'input' }) })), (schema.Headers && { headers: z.toJSONSchema(strictifySchema(schema.Headers, parseStrict('headers', strict)), { reused: 'inline', target: "draft-7", io: 'input' }) })), { response: z.toJSONSchema(schema.Reply.partial(), { reused: 'inline', target: "draft-7" })['properties'] }), (schema.Security && { security: schema.Security })), (schema.Tags && { tags: schema.Tags }));
26
37
  return {
27
38
  schema: finalResult,
28
39
  handler,
@@ -61,3 +72,4 @@ export const routeV4 = (schema, handler) => {
61
72
  },
62
73
  };
63
74
  };
75
+ export const routeV4 = createRouteV4({ strict: false });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@efebia/fastify-zod-reply",
3
- "version": "1.0.6",
3
+ "version": "1.1.0",
4
4
  "license": "MIT",
5
5
  "dependencies": {
6
6
  "fastify": "^5.3.0",
@@ -12,14 +12,17 @@
12
12
  "/lib"
13
13
  ],
14
14
  "scripts": {
15
- "build:esm": "tsc --module nodenext --moduleResolution nodenext --outDir lib/esm",
16
- "build:cjs": "tsc --module commonjs --moduleResolution node --outDir lib/cjs",
17
- "build": "yarn build:esm && yarn build:cjs"
15
+ "build:esm": "tsc -p ./tsconfig.build.json --module nodenext --moduleResolution nodenext --outDir lib/esm",
16
+ "build:cjs": "tsc -p ./tsconfig.build.json --module commonjs --moduleResolution node --outDir lib/cjs",
17
+ "build": "yarn build:esm && yarn build:cjs",
18
+ "test": "node --import tsx --test ./**/*.test.ts"
18
19
  },
19
20
  "main": "./lib/esm/index.js",
20
21
  "types": "./lib/esm/index.d.ts",
21
22
  "type": "module",
22
23
  "devDependencies": {
24
+ "@types/node": "22",
25
+ "tsx": "^4.20.3",
23
26
  "typescript": "~5.8.3"
24
27
  },
25
28
  "exports": {