@efebia/fastify-zod-reply 1.4.0 → 1.4.1
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/error.d.ts +69 -0
- package/lib/cjs/error.js +37 -1
- package/lib/cjs/index.d.ts +1 -1
- package/lib/cjs/index.js +4 -1
- package/lib/cjs/reply.d.ts +0 -4
- package/lib/cjs/reply.js +2 -11
- package/lib/esm/error.d.ts +69 -0
- package/lib/esm/error.js +34 -0
- package/lib/esm/index.d.ts +1 -1
- package/lib/esm/index.js +1 -1
- package/lib/esm/reply.d.ts +0 -4
- package/lib/esm/reply.js +1 -9
- package/package.json +1 -1
package/lib/cjs/error.d.ts
CHANGED
|
@@ -2,3 +2,72 @@ export declare class FastifyZodReplyError extends Error {
|
|
|
2
2
|
statusCode: number;
|
|
3
3
|
constructor(message?: string, statusCode?: number);
|
|
4
4
|
}
|
|
5
|
+
export declare const createError: (statusCode: number) => (message: string | {
|
|
6
|
+
message: string;
|
|
7
|
+
}) => FastifyZodReplyError;
|
|
8
|
+
type FastifyZodReplyErrorObjectItem = {
|
|
9
|
+
statusCode: number;
|
|
10
|
+
payload: string | {
|
|
11
|
+
message: string;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
declare const DEFAULT_OBJECT: {
|
|
15
|
+
readonly badRequest: {
|
|
16
|
+
readonly statusCode: 400;
|
|
17
|
+
readonly payload: {
|
|
18
|
+
readonly message: "badRequest";
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
readonly unauthorized: {
|
|
22
|
+
readonly statusCode: 401;
|
|
23
|
+
readonly payload: {
|
|
24
|
+
readonly message: "unauthorized";
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
readonly forbidden: {
|
|
28
|
+
readonly statusCode: 403;
|
|
29
|
+
readonly payload: {
|
|
30
|
+
readonly message: "forbidden";
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
readonly notFound: {
|
|
34
|
+
readonly statusCode: 404;
|
|
35
|
+
readonly payload: {
|
|
36
|
+
readonly message: "notFound";
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
readonly notAcceptable: {
|
|
40
|
+
readonly statusCode: 406;
|
|
41
|
+
readonly payload: {
|
|
42
|
+
readonly message: "notAcceptable";
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
readonly conflict: {
|
|
46
|
+
readonly statusCode: 409;
|
|
47
|
+
readonly payload: {
|
|
48
|
+
readonly message: "conflict";
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
readonly internalServerError: {
|
|
52
|
+
readonly statusCode: 500;
|
|
53
|
+
readonly payload: {
|
|
54
|
+
readonly message: "internalServerError";
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
type ReverseFastifyZodReplyErrorObjectItem<TOverrides extends Record<string, FastifyZodReplyErrorObjectItem>> = {
|
|
59
|
+
[key in keyof TOverrides as TOverrides[key]['statusCode']]: {
|
|
60
|
+
label: key;
|
|
61
|
+
payload: TOverrides[key]['payload'];
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
type Deduplicate<TOverrides extends Record<string, FastifyZodReplyErrorObjectItem>> = {
|
|
65
|
+
[key in keyof typeof DEFAULT_OBJECT as typeof DEFAULT_OBJECT[key]['statusCode'] extends keyof ReverseFastifyZodReplyErrorObjectItem<TOverrides> ? never : key]: typeof DEFAULT_OBJECT[key];
|
|
66
|
+
} & TOverrides;
|
|
67
|
+
type FastifyZodReplyErrorObject<TOverrides extends Record<string, FastifyZodReplyErrorObjectItem>> = {
|
|
68
|
+
[key in keyof Deduplicate<TOverrides>]: (arg?: string | {
|
|
69
|
+
message: string;
|
|
70
|
+
}) => FastifyZodReplyError;
|
|
71
|
+
};
|
|
72
|
+
export declare const buildHTTPErrorObject: <const TRecord extends Record<string, FastifyZodReplyErrorObjectItem>>(overrides?: TRecord) => FastifyZodReplyErrorObject<TRecord>;
|
|
73
|
+
export {};
|
package/lib/cjs/error.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FastifyZodReplyError = void 0;
|
|
3
|
+
exports.buildHTTPErrorObject = exports.createError = exports.FastifyZodReplyError = void 0;
|
|
4
4
|
class FastifyZodReplyError extends Error {
|
|
5
5
|
constructor(message, statusCode) {
|
|
6
6
|
super(message);
|
|
@@ -8,3 +8,39 @@ class FastifyZodReplyError extends Error {
|
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
10
|
exports.FastifyZodReplyError = FastifyZodReplyError;
|
|
11
|
+
const createError = (statusCode) => (message) => {
|
|
12
|
+
const customError = new FastifyZodReplyError("", statusCode);
|
|
13
|
+
if (typeof message === "string")
|
|
14
|
+
customError.message = message;
|
|
15
|
+
else
|
|
16
|
+
customError.message = message.message;
|
|
17
|
+
return customError;
|
|
18
|
+
};
|
|
19
|
+
exports.createError = createError;
|
|
20
|
+
const DEFAULT_OBJECT = {
|
|
21
|
+
badRequest: { statusCode: 400, payload: { message: "badRequest" } },
|
|
22
|
+
unauthorized: { statusCode: 401, payload: { message: "unauthorized" } },
|
|
23
|
+
forbidden: { statusCode: 403, payload: { message: "forbidden" } },
|
|
24
|
+
notFound: { statusCode: 404, payload: { message: "notFound" } },
|
|
25
|
+
notAcceptable: { statusCode: 406, payload: { message: "notAcceptable" } },
|
|
26
|
+
conflict: { statusCode: 409, payload: { message: "conflict" } },
|
|
27
|
+
internalServerError: {
|
|
28
|
+
statusCode: 500,
|
|
29
|
+
payload: { message: "internalServerError" },
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
const buildHTTPErrorObject = (overrides) => {
|
|
33
|
+
return Object.fromEntries([...Object.entries(overrides !== null && overrides !== void 0 ? overrides : {})
|
|
34
|
+
.concat(Object.entries(DEFAULT_OBJECT))
|
|
35
|
+
.reduce((acc, [label, value]) => {
|
|
36
|
+
if (acc.get(value.statusCode))
|
|
37
|
+
return acc;
|
|
38
|
+
acc.set(value.statusCode, { label, obj: value });
|
|
39
|
+
return acc;
|
|
40
|
+
}, new Map())
|
|
41
|
+
.entries()].map(([statusCode, value]) => {
|
|
42
|
+
const baseErrorFn = (0, exports.createError)(statusCode);
|
|
43
|
+
return [value.label, (arg) => baseErrorFn(arg !== null && arg !== void 0 ? arg : value.obj.payload)];
|
|
44
|
+
}));
|
|
45
|
+
};
|
|
46
|
+
exports.buildHTTPErrorObject = buildHTTPErrorObject;
|
package/lib/cjs/index.d.ts
CHANGED
|
@@ -29,7 +29,7 @@ declare module "fastify" {
|
|
|
29
29
|
}
|
|
30
30
|
declare const _default: import("fastify").FastifyPluginCallback<FastifyReplyPluginOptions, import("fastify").RawServerDefault, import("fastify").FastifyTypeProviderDefault, import("fastify").FastifyBaseLogger>;
|
|
31
31
|
export default _default;
|
|
32
|
-
export
|
|
32
|
+
export { buildHTTPErrorObject, FastifyZodReplyError } from "./error.js";
|
|
33
33
|
export * from "./routeV4.js";
|
|
34
34
|
export * from "./sseRouteV4.js";
|
|
35
35
|
export * from "./types.js";
|
package/lib/cjs/index.js
CHANGED
|
@@ -17,6 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
17
17
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
18
|
};
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.FastifyZodReplyError = exports.buildHTTPErrorObject = void 0;
|
|
20
21
|
const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
|
|
21
22
|
const reply_js_1 = require("./reply.js");
|
|
22
23
|
const utils_js_1 = require("./utils.js");
|
|
@@ -49,7 +50,9 @@ exports.default = (0, fastify_plugin_1.default)(async (fastify, opts) => {
|
|
|
49
50
|
fastify: "5.x",
|
|
50
51
|
name: "@efebia/fastify-zod-reply",
|
|
51
52
|
});
|
|
52
|
-
|
|
53
|
+
var error_js_1 = require("./error.js");
|
|
54
|
+
Object.defineProperty(exports, "buildHTTPErrorObject", { enumerable: true, get: function () { return error_js_1.buildHTTPErrorObject; } });
|
|
55
|
+
Object.defineProperty(exports, "FastifyZodReplyError", { enumerable: true, get: function () { return error_js_1.FastifyZodReplyError; } });
|
|
53
56
|
__exportStar(require("./routeV4.js"), exports);
|
|
54
57
|
__exportStar(require("./sseRouteV4.js"), exports);
|
|
55
58
|
__exportStar(require("./types.js"), exports);
|
package/lib/cjs/reply.d.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
import { FastifyReply } from "fastify";
|
|
2
|
-
import { FastifyZodReplyError } from "./error.js";
|
|
3
2
|
type ReplyFunction<T> = T extends (...args: any[]) => any ? (this: FastifyReply, ...args: Parameters<T>) => ReturnType<T> : never;
|
|
4
3
|
export declare const createReply: (statusCode: number, defaultPayload: object) => ReplyFunction<any>;
|
|
5
|
-
export declare const createError: (statusCode: number) => (message: string | {
|
|
6
|
-
message: string;
|
|
7
|
-
}) => FastifyZodReplyError;
|
|
8
4
|
export {};
|
package/lib/cjs/reply.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.createReply = void 0;
|
|
4
4
|
const error_js_1 = require("./error.js");
|
|
5
5
|
const createReply = (statusCode, defaultPayload) => {
|
|
6
6
|
return function (payload) {
|
|
7
7
|
const finalPayload = payload !== null && payload !== void 0 ? payload : defaultPayload;
|
|
8
8
|
if (typeof finalPayload === 'string')
|
|
9
|
-
throw (0,
|
|
9
|
+
throw (0, error_js_1.createError)(statusCode)(finalPayload);
|
|
10
10
|
if (typeof finalPayload !== 'undefined')
|
|
11
11
|
this.type("application/json");
|
|
12
12
|
this.code(statusCode);
|
|
@@ -14,12 +14,3 @@ const createReply = (statusCode, defaultPayload) => {
|
|
|
14
14
|
};
|
|
15
15
|
};
|
|
16
16
|
exports.createReply = createReply;
|
|
17
|
-
const createError = (statusCode) => (message) => {
|
|
18
|
-
const customError = new error_js_1.FastifyZodReplyError("", statusCode);
|
|
19
|
-
if (typeof message === "string")
|
|
20
|
-
customError.message = message;
|
|
21
|
-
else
|
|
22
|
-
customError.message = message.message;
|
|
23
|
-
return customError;
|
|
24
|
-
};
|
|
25
|
-
exports.createError = createError;
|
package/lib/esm/error.d.ts
CHANGED
|
@@ -2,3 +2,72 @@ export declare class FastifyZodReplyError extends Error {
|
|
|
2
2
|
statusCode: number;
|
|
3
3
|
constructor(message?: string, statusCode?: number);
|
|
4
4
|
}
|
|
5
|
+
export declare const createError: (statusCode: number) => (message: string | {
|
|
6
|
+
message: string;
|
|
7
|
+
}) => FastifyZodReplyError;
|
|
8
|
+
type FastifyZodReplyErrorObjectItem = {
|
|
9
|
+
statusCode: number;
|
|
10
|
+
payload: string | {
|
|
11
|
+
message: string;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
declare const DEFAULT_OBJECT: {
|
|
15
|
+
readonly badRequest: {
|
|
16
|
+
readonly statusCode: 400;
|
|
17
|
+
readonly payload: {
|
|
18
|
+
readonly message: "badRequest";
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
readonly unauthorized: {
|
|
22
|
+
readonly statusCode: 401;
|
|
23
|
+
readonly payload: {
|
|
24
|
+
readonly message: "unauthorized";
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
readonly forbidden: {
|
|
28
|
+
readonly statusCode: 403;
|
|
29
|
+
readonly payload: {
|
|
30
|
+
readonly message: "forbidden";
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
readonly notFound: {
|
|
34
|
+
readonly statusCode: 404;
|
|
35
|
+
readonly payload: {
|
|
36
|
+
readonly message: "notFound";
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
readonly notAcceptable: {
|
|
40
|
+
readonly statusCode: 406;
|
|
41
|
+
readonly payload: {
|
|
42
|
+
readonly message: "notAcceptable";
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
readonly conflict: {
|
|
46
|
+
readonly statusCode: 409;
|
|
47
|
+
readonly payload: {
|
|
48
|
+
readonly message: "conflict";
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
readonly internalServerError: {
|
|
52
|
+
readonly statusCode: 500;
|
|
53
|
+
readonly payload: {
|
|
54
|
+
readonly message: "internalServerError";
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
type ReverseFastifyZodReplyErrorObjectItem<TOverrides extends Record<string, FastifyZodReplyErrorObjectItem>> = {
|
|
59
|
+
[key in keyof TOverrides as TOverrides[key]['statusCode']]: {
|
|
60
|
+
label: key;
|
|
61
|
+
payload: TOverrides[key]['payload'];
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
type Deduplicate<TOverrides extends Record<string, FastifyZodReplyErrorObjectItem>> = {
|
|
65
|
+
[key in keyof typeof DEFAULT_OBJECT as typeof DEFAULT_OBJECT[key]['statusCode'] extends keyof ReverseFastifyZodReplyErrorObjectItem<TOverrides> ? never : key]: typeof DEFAULT_OBJECT[key];
|
|
66
|
+
} & TOverrides;
|
|
67
|
+
type FastifyZodReplyErrorObject<TOverrides extends Record<string, FastifyZodReplyErrorObjectItem>> = {
|
|
68
|
+
[key in keyof Deduplicate<TOverrides>]: (arg?: string | {
|
|
69
|
+
message: string;
|
|
70
|
+
}) => FastifyZodReplyError;
|
|
71
|
+
};
|
|
72
|
+
export declare const buildHTTPErrorObject: <const TRecord extends Record<string, FastifyZodReplyErrorObjectItem>>(overrides?: TRecord) => FastifyZodReplyErrorObject<TRecord>;
|
|
73
|
+
export {};
|
package/lib/esm/error.js
CHANGED
|
@@ -4,3 +4,37 @@ export class FastifyZodReplyError extends Error {
|
|
|
4
4
|
this.statusCode = statusCode !== null && statusCode !== void 0 ? statusCode : -1;
|
|
5
5
|
}
|
|
6
6
|
}
|
|
7
|
+
export const createError = (statusCode) => (message) => {
|
|
8
|
+
const customError = new FastifyZodReplyError("", statusCode);
|
|
9
|
+
if (typeof message === "string")
|
|
10
|
+
customError.message = message;
|
|
11
|
+
else
|
|
12
|
+
customError.message = message.message;
|
|
13
|
+
return customError;
|
|
14
|
+
};
|
|
15
|
+
const DEFAULT_OBJECT = {
|
|
16
|
+
badRequest: { statusCode: 400, payload: { message: "badRequest" } },
|
|
17
|
+
unauthorized: { statusCode: 401, payload: { message: "unauthorized" } },
|
|
18
|
+
forbidden: { statusCode: 403, payload: { message: "forbidden" } },
|
|
19
|
+
notFound: { statusCode: 404, payload: { message: "notFound" } },
|
|
20
|
+
notAcceptable: { statusCode: 406, payload: { message: "notAcceptable" } },
|
|
21
|
+
conflict: { statusCode: 409, payload: { message: "conflict" } },
|
|
22
|
+
internalServerError: {
|
|
23
|
+
statusCode: 500,
|
|
24
|
+
payload: { message: "internalServerError" },
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
export const buildHTTPErrorObject = (overrides) => {
|
|
28
|
+
return Object.fromEntries([...Object.entries(overrides !== null && overrides !== void 0 ? overrides : {})
|
|
29
|
+
.concat(Object.entries(DEFAULT_OBJECT))
|
|
30
|
+
.reduce((acc, [label, value]) => {
|
|
31
|
+
if (acc.get(value.statusCode))
|
|
32
|
+
return acc;
|
|
33
|
+
acc.set(value.statusCode, { label, obj: value });
|
|
34
|
+
return acc;
|
|
35
|
+
}, new Map())
|
|
36
|
+
.entries()].map(([statusCode, value]) => {
|
|
37
|
+
const baseErrorFn = createError(statusCode);
|
|
38
|
+
return [value.label, (arg) => baseErrorFn(arg !== null && arg !== void 0 ? arg : value.obj.payload)];
|
|
39
|
+
}));
|
|
40
|
+
};
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -29,7 +29,7 @@ declare module "fastify" {
|
|
|
29
29
|
}
|
|
30
30
|
declare const _default: import("fastify").FastifyPluginCallback<FastifyReplyPluginOptions, import("fastify").RawServerDefault, import("fastify").FastifyTypeProviderDefault, import("fastify").FastifyBaseLogger>;
|
|
31
31
|
export default _default;
|
|
32
|
-
export
|
|
32
|
+
export { buildHTTPErrorObject, FastifyZodReplyError } from "./error.js";
|
|
33
33
|
export * from "./routeV4.js";
|
|
34
34
|
export * from "./sseRouteV4.js";
|
|
35
35
|
export * from "./types.js";
|
package/lib/esm/index.js
CHANGED
|
@@ -30,7 +30,7 @@ export default fp(async (fastify, opts) => {
|
|
|
30
30
|
fastify: "5.x",
|
|
31
31
|
name: "@efebia/fastify-zod-reply",
|
|
32
32
|
});
|
|
33
|
-
export
|
|
33
|
+
export { buildHTTPErrorObject, FastifyZodReplyError } from "./error.js";
|
|
34
34
|
export * from "./routeV4.js";
|
|
35
35
|
export * from "./sseRouteV4.js";
|
|
36
36
|
export * from "./types.js";
|
package/lib/esm/reply.d.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
import { FastifyReply } from "fastify";
|
|
2
|
-
import { FastifyZodReplyError } from "./error.js";
|
|
3
2
|
type ReplyFunction<T> = T extends (...args: any[]) => any ? (this: FastifyReply, ...args: Parameters<T>) => ReturnType<T> : never;
|
|
4
3
|
export declare const createReply: (statusCode: number, defaultPayload: object) => ReplyFunction<any>;
|
|
5
|
-
export declare const createError: (statusCode: number) => (message: string | {
|
|
6
|
-
message: string;
|
|
7
|
-
}) => FastifyZodReplyError;
|
|
8
4
|
export {};
|
package/lib/esm/reply.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createError } from "./error.js";
|
|
2
2
|
export const createReply = (statusCode, defaultPayload) => {
|
|
3
3
|
return function (payload) {
|
|
4
4
|
const finalPayload = payload !== null && payload !== void 0 ? payload : defaultPayload;
|
|
@@ -10,11 +10,3 @@ export const createReply = (statusCode, defaultPayload) => {
|
|
|
10
10
|
return finalPayload;
|
|
11
11
|
};
|
|
12
12
|
};
|
|
13
|
-
export const createError = (statusCode) => (message) => {
|
|
14
|
-
const customError = new FastifyZodReplyError("", statusCode);
|
|
15
|
-
if (typeof message === "string")
|
|
16
|
-
customError.message = message;
|
|
17
|
-
else
|
|
18
|
-
customError.message = message.message;
|
|
19
|
-
return customError;
|
|
20
|
-
};
|