@efebia/fastify-zod-reply 1.4.2 → 1.4.3
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/index.d.ts +59 -9
- package/lib/cjs/index.js +1 -4
- package/lib/cjs/routeV4.d.ts +7 -1
- package/lib/esm/index.d.ts +59 -9
- package/lib/esm/index.js +1 -4
- package/lib/esm/routeV4.d.ts +7 -1
- package/package.json +1 -1
package/lib/cjs/index.d.ts
CHANGED
|
@@ -1,4 +1,61 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type CodePayload<TReply, TCode extends number, TFallback = unknown> = TReply extends {
|
|
2
|
+
send(payload?: infer TMap): any;
|
|
3
|
+
} ? TMap extends Record<TCode, infer T> ? T : TFallback : TFallback;
|
|
4
|
+
export type ResponseParam<TReply, TCode extends number> = CodePayload<TReply, TCode> extends Record<PropertyKey, unknown> ? CodePayload<TReply, TCode> & Record<string, unknown> : CodePayload<TReply, TCode>;
|
|
5
|
+
export type ValidateResponseParam<TThis, TCode extends number> = CodePayload<TThis, TCode, never> extends never ? string : CodePayload<TThis, TCode, never> extends {
|
|
6
|
+
message: infer M;
|
|
7
|
+
} ? M & string : never;
|
|
8
|
+
export type ErrorMethodParam<TThis, TCode extends number> = ValidateResponseParam<TThis, TCode> | ResponseParam<TThis, TCode>;
|
|
9
|
+
export type ErrorCodePayload<TVal> = [TVal] extends [string] ? {
|
|
10
|
+
message: TVal;
|
|
11
|
+
} : TVal;
|
|
12
|
+
type DefaultArgs<TReply, TCode extends number, TParam> = CodePayload<TReply, TCode, never> extends never ? [val?: undefined] : [val: TParam];
|
|
13
|
+
declare module "fastify" {
|
|
14
|
+
interface FastifyReply {
|
|
15
|
+
ok(...args: DefaultArgs<this, 200, ResponseParam<this, 200>>): CodePayload<this, 200, {
|
|
16
|
+
message: "ok";
|
|
17
|
+
}>;
|
|
18
|
+
ok(val: ResponseParam<this, 200>): CodePayload<this, 200>;
|
|
19
|
+
created(...args: DefaultArgs<this, 201, ResponseParam<this, 201>>): CodePayload<this, 201, {
|
|
20
|
+
message: "created";
|
|
21
|
+
}>;
|
|
22
|
+
created(val: ResponseParam<this, 201>): CodePayload<this, 201>;
|
|
23
|
+
accepted(...args: DefaultArgs<this, 202, ResponseParam<this, 202>>): CodePayload<this, 202, {
|
|
24
|
+
message: "accepted";
|
|
25
|
+
}>;
|
|
26
|
+
accepted(val: ResponseParam<this, 202>): CodePayload<this, 202>;
|
|
27
|
+
noContent(): CodePayload<this, 204, void>;
|
|
28
|
+
badRequest(...args: DefaultArgs<this, 400, ErrorMethodParam<this, 400>>): CodePayload<this, 400, {
|
|
29
|
+
message: "badRequest";
|
|
30
|
+
}>;
|
|
31
|
+
badRequest<TVal extends ErrorMethodParam<this, 400>>(val: TVal): ErrorCodePayload<TVal>;
|
|
32
|
+
unauthorized(...args: DefaultArgs<this, 401, ErrorMethodParam<this, 401>>): CodePayload<this, 401, {
|
|
33
|
+
message: "unauthorized";
|
|
34
|
+
}>;
|
|
35
|
+
unauthorized<TVal extends ErrorMethodParam<this, 401>>(val: TVal): ErrorCodePayload<TVal>;
|
|
36
|
+
forbidden(...args: DefaultArgs<this, 403, ErrorMethodParam<this, 403>>): CodePayload<this, 403, {
|
|
37
|
+
message: "forbidden";
|
|
38
|
+
}>;
|
|
39
|
+
forbidden<TVal extends ErrorMethodParam<this, 403>>(val: TVal): ErrorCodePayload<TVal>;
|
|
40
|
+
notFound(...args: DefaultArgs<this, 404, ErrorMethodParam<this, 404>>): CodePayload<this, 404, {
|
|
41
|
+
message: "notFound";
|
|
42
|
+
}>;
|
|
43
|
+
notFound<TVal extends ErrorMethodParam<this, 404>>(val: TVal): ErrorCodePayload<TVal>;
|
|
44
|
+
notAcceptable(...args: DefaultArgs<this, 406, ErrorMethodParam<this, 406>>): CodePayload<this, 406, {
|
|
45
|
+
message: "notAcceptable";
|
|
46
|
+
}>;
|
|
47
|
+
notAcceptable<TVal extends ErrorMethodParam<this, 406>>(val: TVal): ErrorCodePayload<TVal>;
|
|
48
|
+
conflict(...args: DefaultArgs<this, 409, ErrorMethodParam<this, 409>>): CodePayload<this, 409, {
|
|
49
|
+
message: "conflict";
|
|
50
|
+
}>;
|
|
51
|
+
conflict<TVal extends ErrorMethodParam<this, 409>>(val: TVal): ErrorCodePayload<TVal>;
|
|
52
|
+
internalServerError(...args: DefaultArgs<this, 500, ErrorMethodParam<this, 500>>): CodePayload<this, 500, {
|
|
53
|
+
message: "internalServerError";
|
|
54
|
+
}>;
|
|
55
|
+
internalServerError<TVal extends ErrorMethodParam<this, 500>>(val: TVal): ErrorCodePayload<TVal>;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export type StatusCode<TCode extends number> = {
|
|
2
59
|
statusCode: TCode;
|
|
3
60
|
payload: any;
|
|
4
61
|
};
|
|
@@ -18,15 +75,8 @@ export interface FastifyStatusCode {
|
|
|
18
75
|
export type FastifyReplyPluginOptions = {
|
|
19
76
|
statusCodes?: {
|
|
20
77
|
[key in keyof FastifyStatusCode]?: FastifyStatusCode[key];
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
export type DecoratedReply = {
|
|
24
|
-
[key in keyof FastifyStatusCode]: <T>(val?: T) => T;
|
|
78
|
+
} & Record<string, StatusCode<any>>;
|
|
25
79
|
};
|
|
26
|
-
declare module "fastify" {
|
|
27
|
-
interface FastifyReply extends DecoratedReply {
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
80
|
declare const _default: import("fastify").FastifyPluginCallback<FastifyReplyPluginOptions, import("fastify").RawServerDefault, import("fastify").FastifyTypeProviderDefault, import("fastify").FastifyBaseLogger>;
|
|
31
81
|
export default _default;
|
|
32
82
|
export { buildHTTPErrorObject, FastifyZodReplyError } from "./error.js";
|
package/lib/cjs/index.js
CHANGED
|
@@ -33,10 +33,7 @@ const defaultOptions = {
|
|
|
33
33
|
notFound: { statusCode: 404, payload: { message: "notFound" } },
|
|
34
34
|
notAcceptable: { statusCode: 406, payload: { message: "notAcceptable" } },
|
|
35
35
|
conflict: { statusCode: 409, payload: { message: "conflict" } },
|
|
36
|
-
internalServerError: {
|
|
37
|
-
statusCode: 500,
|
|
38
|
-
payload: { message: "internalServerError" },
|
|
39
|
-
},
|
|
36
|
+
internalServerError: { statusCode: 500, payload: { message: "internalServerError" } },
|
|
40
37
|
},
|
|
41
38
|
};
|
|
42
39
|
exports.default = (0, fastify_plugin_1.default)(async (fastify, opts) => {
|
package/lib/cjs/routeV4.d.ts
CHANGED
|
@@ -14,11 +14,16 @@ export type BaseZodV4Schema = {
|
|
|
14
14
|
Summary?: string;
|
|
15
15
|
Notes?: string;
|
|
16
16
|
};
|
|
17
|
+
type ErrorReplyFallback<TReply> = {
|
|
18
|
+
[K in Exclude<400 | 401 | 403 | 404 | 406 | 409 | 500, keyof TReply>]?: {
|
|
19
|
+
message: string;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
17
22
|
export type FastifyZodV4Schema<TZodSchema extends BaseZodV4Schema> = {
|
|
18
23
|
Body: TZodSchema["Body"] extends z.ZodTypeAny ? z.output<TZodSchema["Body"]> : undefined;
|
|
19
24
|
Params: TZodSchema["Params"] extends z.ZodTypeAny ? z.output<TZodSchema["Params"]> : undefined;
|
|
20
25
|
Querystring: TZodSchema["Query"] extends z.ZodTypeAny ? z.output<TZodSchema["Query"]> : undefined;
|
|
21
|
-
Reply: TZodSchema["Reply"] extends z.ZodTypeAny ? z.input<TZodSchema["Reply"]>
|
|
26
|
+
Reply: TZodSchema["Reply"] extends z.ZodTypeAny ? z.input<TZodSchema["Reply"]> & ErrorReplyFallback<z.input<TZodSchema["Reply"]>> : undefined;
|
|
22
27
|
};
|
|
23
28
|
export type RouteV4Options = {
|
|
24
29
|
strict?: boolean | {
|
|
@@ -34,3 +39,4 @@ export declare const createRouteV4: <RequestAugmentation extends object = {}, Re
|
|
|
34
39
|
export declare const routeV4: <TSchema extends BaseZodV4Schema, FastifySchema extends FastifyZodV4Schema<TSchema> = FastifyZodV4Schema<TSchema>>(schema: TSchema, handler: NoInfer<APIHandler<FastifySchema, {}, {}>>, options?: RouteV4Options) => APIOptions<FastifySchema> & {
|
|
35
40
|
handler: APIHandler<FastifySchema>;
|
|
36
41
|
};
|
|
42
|
+
export {};
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -1,4 +1,61 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type CodePayload<TReply, TCode extends number, TFallback = unknown> = TReply extends {
|
|
2
|
+
send(payload?: infer TMap): any;
|
|
3
|
+
} ? TMap extends Record<TCode, infer T> ? T : TFallback : TFallback;
|
|
4
|
+
export type ResponseParam<TReply, TCode extends number> = CodePayload<TReply, TCode> extends Record<PropertyKey, unknown> ? CodePayload<TReply, TCode> & Record<string, unknown> : CodePayload<TReply, TCode>;
|
|
5
|
+
export type ValidateResponseParam<TThis, TCode extends number> = CodePayload<TThis, TCode, never> extends never ? string : CodePayload<TThis, TCode, never> extends {
|
|
6
|
+
message: infer M;
|
|
7
|
+
} ? M & string : never;
|
|
8
|
+
export type ErrorMethodParam<TThis, TCode extends number> = ValidateResponseParam<TThis, TCode> | ResponseParam<TThis, TCode>;
|
|
9
|
+
export type ErrorCodePayload<TVal> = [TVal] extends [string] ? {
|
|
10
|
+
message: TVal;
|
|
11
|
+
} : TVal;
|
|
12
|
+
type DefaultArgs<TReply, TCode extends number, TParam> = CodePayload<TReply, TCode, never> extends never ? [val?: undefined] : [val: TParam];
|
|
13
|
+
declare module "fastify" {
|
|
14
|
+
interface FastifyReply {
|
|
15
|
+
ok(...args: DefaultArgs<this, 200, ResponseParam<this, 200>>): CodePayload<this, 200, {
|
|
16
|
+
message: "ok";
|
|
17
|
+
}>;
|
|
18
|
+
ok(val: ResponseParam<this, 200>): CodePayload<this, 200>;
|
|
19
|
+
created(...args: DefaultArgs<this, 201, ResponseParam<this, 201>>): CodePayload<this, 201, {
|
|
20
|
+
message: "created";
|
|
21
|
+
}>;
|
|
22
|
+
created(val: ResponseParam<this, 201>): CodePayload<this, 201>;
|
|
23
|
+
accepted(...args: DefaultArgs<this, 202, ResponseParam<this, 202>>): CodePayload<this, 202, {
|
|
24
|
+
message: "accepted";
|
|
25
|
+
}>;
|
|
26
|
+
accepted(val: ResponseParam<this, 202>): CodePayload<this, 202>;
|
|
27
|
+
noContent(): CodePayload<this, 204, void>;
|
|
28
|
+
badRequest(...args: DefaultArgs<this, 400, ErrorMethodParam<this, 400>>): CodePayload<this, 400, {
|
|
29
|
+
message: "badRequest";
|
|
30
|
+
}>;
|
|
31
|
+
badRequest<TVal extends ErrorMethodParam<this, 400>>(val: TVal): ErrorCodePayload<TVal>;
|
|
32
|
+
unauthorized(...args: DefaultArgs<this, 401, ErrorMethodParam<this, 401>>): CodePayload<this, 401, {
|
|
33
|
+
message: "unauthorized";
|
|
34
|
+
}>;
|
|
35
|
+
unauthorized<TVal extends ErrorMethodParam<this, 401>>(val: TVal): ErrorCodePayload<TVal>;
|
|
36
|
+
forbidden(...args: DefaultArgs<this, 403, ErrorMethodParam<this, 403>>): CodePayload<this, 403, {
|
|
37
|
+
message: "forbidden";
|
|
38
|
+
}>;
|
|
39
|
+
forbidden<TVal extends ErrorMethodParam<this, 403>>(val: TVal): ErrorCodePayload<TVal>;
|
|
40
|
+
notFound(...args: DefaultArgs<this, 404, ErrorMethodParam<this, 404>>): CodePayload<this, 404, {
|
|
41
|
+
message: "notFound";
|
|
42
|
+
}>;
|
|
43
|
+
notFound<TVal extends ErrorMethodParam<this, 404>>(val: TVal): ErrorCodePayload<TVal>;
|
|
44
|
+
notAcceptable(...args: DefaultArgs<this, 406, ErrorMethodParam<this, 406>>): CodePayload<this, 406, {
|
|
45
|
+
message: "notAcceptable";
|
|
46
|
+
}>;
|
|
47
|
+
notAcceptable<TVal extends ErrorMethodParam<this, 406>>(val: TVal): ErrorCodePayload<TVal>;
|
|
48
|
+
conflict(...args: DefaultArgs<this, 409, ErrorMethodParam<this, 409>>): CodePayload<this, 409, {
|
|
49
|
+
message: "conflict";
|
|
50
|
+
}>;
|
|
51
|
+
conflict<TVal extends ErrorMethodParam<this, 409>>(val: TVal): ErrorCodePayload<TVal>;
|
|
52
|
+
internalServerError(...args: DefaultArgs<this, 500, ErrorMethodParam<this, 500>>): CodePayload<this, 500, {
|
|
53
|
+
message: "internalServerError";
|
|
54
|
+
}>;
|
|
55
|
+
internalServerError<TVal extends ErrorMethodParam<this, 500>>(val: TVal): ErrorCodePayload<TVal>;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export type StatusCode<TCode extends number> = {
|
|
2
59
|
statusCode: TCode;
|
|
3
60
|
payload: any;
|
|
4
61
|
};
|
|
@@ -18,15 +75,8 @@ export interface FastifyStatusCode {
|
|
|
18
75
|
export type FastifyReplyPluginOptions = {
|
|
19
76
|
statusCodes?: {
|
|
20
77
|
[key in keyof FastifyStatusCode]?: FastifyStatusCode[key];
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
export type DecoratedReply = {
|
|
24
|
-
[key in keyof FastifyStatusCode]: <T>(val?: T) => T;
|
|
78
|
+
} & Record<string, StatusCode<any>>;
|
|
25
79
|
};
|
|
26
|
-
declare module "fastify" {
|
|
27
|
-
interface FastifyReply extends DecoratedReply {
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
80
|
declare const _default: import("fastify").FastifyPluginCallback<FastifyReplyPluginOptions, import("fastify").RawServerDefault, import("fastify").FastifyTypeProviderDefault, import("fastify").FastifyBaseLogger>;
|
|
31
81
|
export default _default;
|
|
32
82
|
export { buildHTTPErrorObject, FastifyZodReplyError } from "./error.js";
|
package/lib/esm/index.js
CHANGED
|
@@ -13,10 +13,7 @@ const defaultOptions = {
|
|
|
13
13
|
notFound: { statusCode: 404, payload: { message: "notFound" } },
|
|
14
14
|
notAcceptable: { statusCode: 406, payload: { message: "notAcceptable" } },
|
|
15
15
|
conflict: { statusCode: 409, payload: { message: "conflict" } },
|
|
16
|
-
internalServerError: {
|
|
17
|
-
statusCode: 500,
|
|
18
|
-
payload: { message: "internalServerError" },
|
|
19
|
-
},
|
|
16
|
+
internalServerError: { statusCode: 500, payload: { message: "internalServerError" } },
|
|
20
17
|
},
|
|
21
18
|
};
|
|
22
19
|
export default fp(async (fastify, opts) => {
|
package/lib/esm/routeV4.d.ts
CHANGED
|
@@ -14,11 +14,16 @@ export type BaseZodV4Schema = {
|
|
|
14
14
|
Summary?: string;
|
|
15
15
|
Notes?: string;
|
|
16
16
|
};
|
|
17
|
+
type ErrorReplyFallback<TReply> = {
|
|
18
|
+
[K in Exclude<400 | 401 | 403 | 404 | 406 | 409 | 500, keyof TReply>]?: {
|
|
19
|
+
message: string;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
17
22
|
export type FastifyZodV4Schema<TZodSchema extends BaseZodV4Schema> = {
|
|
18
23
|
Body: TZodSchema["Body"] extends z.ZodTypeAny ? z.output<TZodSchema["Body"]> : undefined;
|
|
19
24
|
Params: TZodSchema["Params"] extends z.ZodTypeAny ? z.output<TZodSchema["Params"]> : undefined;
|
|
20
25
|
Querystring: TZodSchema["Query"] extends z.ZodTypeAny ? z.output<TZodSchema["Query"]> : undefined;
|
|
21
|
-
Reply: TZodSchema["Reply"] extends z.ZodTypeAny ? z.input<TZodSchema["Reply"]>
|
|
26
|
+
Reply: TZodSchema["Reply"] extends z.ZodTypeAny ? z.input<TZodSchema["Reply"]> & ErrorReplyFallback<z.input<TZodSchema["Reply"]>> : undefined;
|
|
22
27
|
};
|
|
23
28
|
export type RouteV4Options = {
|
|
24
29
|
strict?: boolean | {
|
|
@@ -34,3 +39,4 @@ export declare const createRouteV4: <RequestAugmentation extends object = {}, Re
|
|
|
34
39
|
export declare const routeV4: <TSchema extends BaseZodV4Schema, FastifySchema extends FastifyZodV4Schema<TSchema> = FastifyZodV4Schema<TSchema>>(schema: TSchema, handler: NoInfer<APIHandler<FastifySchema, {}, {}>>, options?: RouteV4Options) => APIOptions<FastifySchema> & {
|
|
35
40
|
handler: APIHandler<FastifySchema>;
|
|
36
41
|
};
|
|
42
|
+
export {};
|