-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore!(feat: app/controller): add validation decorator and inject on …
…the base app (#1) * fix(app/function): false import * feat(app/decorator): new validator decorator and implement it * fix(app/types): fix Input decorator It's fucked up using Partial... * refactor(app/controller): extend Todo target * refactor(app/controller): extend Todo target * refactor(app/decorator/validator): improve typing for zod validator * refactor(app/controller): move the typing
- Loading branch information
Showing
11 changed files
with
105 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import "reflect-metadata"; | ||
import { zValidator } from "@hono/zod-validator"; | ||
import type { ValidationTargets } from "hono"; | ||
import { z } from "zod"; | ||
import { MetadataValidatorConstant } from "App/Types/ControllerConstant.js"; | ||
import type Controller from "Controller/Controller.js"; | ||
|
||
type ZodData<V> = { [VK in keyof V]: any }; | ||
|
||
/** | ||
* Validate data for route. | ||
* | ||
* @param method - Target of request | ||
* @param data - Data to validate | ||
*/ | ||
export default function validator<T extends {}>(method: keyof ValidationTargets, data: ZodData<T>): Function { | ||
return function decorate(target: Controller, propKey: string, descriptor: PropertyDescriptor): void { | ||
const targetFunc = descriptor.value as Function; | ||
Reflect.defineMetadata( | ||
MetadataValidatorConstant, | ||
zValidator(method, z.object(data)), | ||
targetFunc | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { Context, Env } from "hono"; | ||
|
||
export default function onError(err: Error, c: Context<Env, any, any>): Response { | ||
const errMessage = err.message; | ||
|
||
console.error(err); | ||
return c.json({ | ||
message: "Something happened, but don't worry maybe it's you or our developer.", | ||
errMessage | ||
}, 500); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export const MetadataConstant = "agniRouting"; | ||
export const MetadataValidatorConstant = "agniValidator"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,25 @@ | ||
import { z } from "zod"; | ||
import httpRoute from "App/Decorator/HttpRoute.js"; | ||
import type { DefaultHonoContext } from "App/Types/ControllerTypes.js"; | ||
import validator from "App/Decorator/Validator.js"; | ||
import type { DefaultHonoContext, HonoInputContext } from "App/Types/ControllerTypes.js"; | ||
import Controller from "./Controller.js"; | ||
|
||
type HelloValidator = { | ||
message: string; | ||
}; | ||
|
||
export default class HelloController extends Controller { | ||
@httpRoute("get", "/") | ||
public hellow(c: DefaultHonoContext): Response { | ||
return c.text("Say hello world!"); | ||
} | ||
|
||
@httpRoute("post", "/api") | ||
@validator<HelloValidator>("json", { | ||
message: z.string() | ||
}) | ||
public helloApi(c: HonoInputContext<HelloValidator>): Response { | ||
const { message } = c.req.valid("json"); | ||
return c.json(`Message: ${message}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters