@sentzunhat/zacatl 0.0.0-alpha.5 → 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 CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@sentzunhat/zacatl",
3
3
  "main": "src/index.ts",
4
4
  "module": "src/index.ts",
5
- "version": "0.0.0-alpha.5",
5
+ "version": "0.0.0-alpha.7",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/sentzunhat/zacatl.git"
@@ -1,4 +1,5 @@
1
1
  export * from "./bad-request";
2
+ export * from "./bad-resource";
2
3
  export * from "./custom";
3
4
  export * from "./forbidden";
4
5
  export * from "./internal-server";
@@ -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<TBody, TParams = void> = (
6
- request: Request<TBody, TParams>,
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<TBody, TParams = void> = FastifyRequest<
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,
@@ -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);
@@ -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<TBody = void, TParams = void> = {
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
  };
@@ -25,7 +25,7 @@ class DummyRouteHandler extends AbstractRouteHandler {
25
25
  });
26
26
  }
27
27
 
28
- handler(_: Request<void, void>): void | Promise<void> {}
28
+ handler(_: Request<void, {}>): void | Promise<void> {}
29
29
  }
30
30
 
31
31
  const fakeConfig: ConfigApplication = {
@@ -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, 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, 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, unknown>;
28
+ const fakeRequest = createFakeFastifyRequest() as Request<unknown, string>;
29
29
  const fakeReply = createFakeFastifyReply();
30
30
 
31
31
  await testHandler.execute(fakeRequest, fakeReply);