@sentzunhat/zacatl 0.0.0-alpha.6 → 0.0.0-alpha.7
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/package.json +1 -1
- package/src/micro-service/architecture/application/entry-points/rest/common/handler.ts +7 -2
- package/src/micro-service/architecture/application/entry-points/rest/common/request.ts +6 -3
- package/src/micro-service/architecture/application/entry-points/rest/route-handlers/abstract.ts +6 -4
- package/src/micro-service/architecture/application/entry-points/rest/route-handlers/route-handler.ts +7 -2
- package/test/unit/micro-service/architecture/application/application.test.ts +1 -1
- package/test/unit/micro-service/architecture/application/entry-points/rest/route-handlers/abstract.test.ts +1 -1
- package/test/unit/micro-service/architecture/application/entry-points/rest/route-handlers/get-route-handler.test.ts +1 -1
- package/test/unit/micro-service/architecture/application/entry-points/rest/route-handlers/post-route-handler.test.ts +1 -1
package/package.json
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
1
2
|
import { FastifyReply } from "fastify";
|
|
2
3
|
|
|
3
4
|
import { Request } from "./request";
|
|
4
5
|
|
|
5
|
-
export type Handler<
|
|
6
|
-
|
|
6
|
+
export type Handler<
|
|
7
|
+
TBody,
|
|
8
|
+
TQuerystring = z.ZodSchema<Record<string, string>>,
|
|
9
|
+
TParams = void
|
|
10
|
+
> = (
|
|
11
|
+
request: Request<TBody, TQuerystring, TParams>,
|
|
7
12
|
reply: FastifyReply
|
|
8
13
|
) => Promise<void>;
|
|
@@ -2,13 +2,16 @@ import { z } from "zod";
|
|
|
2
2
|
import { IncomingMessage } from "http";
|
|
3
3
|
import { FastifySchema, FastifyRequest, RawServerBase } from "fastify";
|
|
4
4
|
|
|
5
|
-
export type Request<
|
|
5
|
+
export type Request<
|
|
6
|
+
TBody,
|
|
7
|
+
TQuerystring = z.ZodSchema<Record<string, string>>,
|
|
8
|
+
TParams = void
|
|
9
|
+
> = FastifyRequest<
|
|
6
10
|
{
|
|
7
11
|
Body: TBody;
|
|
12
|
+
Querystring: TQuerystring;
|
|
8
13
|
Params: TParams;
|
|
9
|
-
Schema: z.ZodSchema<TBody>;
|
|
10
14
|
Headers: z.ZodSchema<Record<string, string>>;
|
|
11
|
-
Querystring: z.ZodSchema<Record<string, string>>;
|
|
12
15
|
},
|
|
13
16
|
RawServerBase,
|
|
14
17
|
IncomingMessage,
|
package/src/micro-service/architecture/application/entry-points/rest/route-handlers/abstract.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import i18n from "i18n";
|
|
2
|
+
import { z } from "zod";
|
|
2
3
|
import { HTTPMethods, FastifySchema, FastifyReply } from "fastify";
|
|
3
4
|
|
|
4
5
|
import { RouteHandler } from "./route-handler";
|
|
@@ -14,9 +15,10 @@ export type HandlerOutput<TResponse> = TResponse;
|
|
|
14
15
|
|
|
15
16
|
export abstract class AbstractRouteHandler<
|
|
16
17
|
TBody = void,
|
|
18
|
+
TQuerystring = z.ZodSchema<Record<string, string>>,
|
|
17
19
|
TResponse = void,
|
|
18
|
-
TParams = void
|
|
19
|
-
> implements RouteHandler<TBody, TParams>
|
|
20
|
+
TParams = void
|
|
21
|
+
> implements RouteHandler<TBody, TQuerystring, TParams>
|
|
20
22
|
{
|
|
21
23
|
public url: string;
|
|
22
24
|
public method: HTTPMethods;
|
|
@@ -29,12 +31,12 @@ export abstract class AbstractRouteHandler<
|
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
abstract handler(
|
|
32
|
-
request: Request<TBody, TParams>,
|
|
34
|
+
request: Request<TBody, TQuerystring, TParams>,
|
|
33
35
|
reply: FastifyReply
|
|
34
36
|
): Promise<HandlerOutput<TResponse>> | HandlerOutput<TResponse>;
|
|
35
37
|
|
|
36
38
|
public async execute(
|
|
37
|
-
request: Request<TBody, TParams>,
|
|
39
|
+
request: Request<TBody, TQuerystring, TParams>,
|
|
38
40
|
reply: FastifyReply
|
|
39
41
|
): Promise<void> {
|
|
40
42
|
const response = await this.handler(request, reply);
|
package/src/micro-service/architecture/application/entry-points/rest/route-handlers/route-handler.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
1
2
|
import { FastifySchema, HTTPMethods } from "fastify";
|
|
2
3
|
|
|
3
4
|
import { Handler } from "../common/handler";
|
|
4
5
|
|
|
5
|
-
export type RouteHandler<
|
|
6
|
+
export type RouteHandler<
|
|
7
|
+
TBody = void,
|
|
8
|
+
TQuerystring = z.ZodSchema<Record<string, string>>,
|
|
9
|
+
TParams = void
|
|
10
|
+
> = {
|
|
6
11
|
url: string;
|
|
7
12
|
method: HTTPMethods;
|
|
8
13
|
schema: FastifySchema;
|
|
9
|
-
execute: Handler<TBody, TParams>;
|
|
14
|
+
execute: Handler<TBody, TQuerystring, TParams>;
|
|
10
15
|
};
|
|
@@ -20,7 +20,7 @@ describe("AbstractRouteHandler", () => {
|
|
|
20
20
|
it("executes the handler and sends the proper response", async () => {
|
|
21
21
|
vi.spyOn(i18n, "__").mockReturnValue("Default success");
|
|
22
22
|
|
|
23
|
-
const fakeRequest = createFakeFastifyRequest() as Request<unknown,
|
|
23
|
+
const fakeRequest = createFakeFastifyRequest() as Request<unknown, string>;
|
|
24
24
|
|
|
25
25
|
const fakeReply: any = createFakeFastifyReply();
|
|
26
26
|
|
|
@@ -23,7 +23,7 @@ describe("GetRouteHandler", () => {
|
|
|
23
23
|
schema: {}, // Use an empty schema for test purposes.
|
|
24
24
|
});
|
|
25
25
|
|
|
26
|
-
const fakeRequest = createFakeFastifyRequest() as Request<unknown,
|
|
26
|
+
const fakeRequest = createFakeFastifyRequest() as Request<unknown, string>;
|
|
27
27
|
const fakeReply = createFakeFastifyReply();
|
|
28
28
|
|
|
29
29
|
await testHandler.execute(fakeRequest, fakeReply);
|
|
@@ -25,7 +25,7 @@ describe("PostRouteHandler", () => {
|
|
|
25
25
|
schema: {}, // Use an empty schema for test purposes.
|
|
26
26
|
});
|
|
27
27
|
|
|
28
|
-
const fakeRequest = createFakeFastifyRequest() as Request<unknown,
|
|
28
|
+
const fakeRequest = createFakeFastifyRequest() as Request<unknown, string>;
|
|
29
29
|
const fakeReply = createFakeFastifyReply();
|
|
30
30
|
|
|
31
31
|
await testHandler.execute(fakeRequest, fakeReply);
|