Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/http",
"version": "5.45.0",
"version": "5.47.0",
"description": "The Athenna Http server. Built on top of fastify.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down Expand Up @@ -110,7 +110,8 @@
"ora": "^8.2.0",
"prettier": "^2.8.8",
"vite": "^6.4.1",
"vite-plugin-restart": "^0.4.2"
"vite-plugin-restart": "^0.4.2",
"zod": "^4.3.6"
},
"c8": {
"all": true,
Expand Down
23 changes: 23 additions & 0 deletions src/exceptions/ZodValidationException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @athenna/http
*
* (c) João Lenon <lenon@athenna.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import type { ZodError } from 'zod'
import { HttpException } from '#src/exceptions/HttpException'

export class ZodValidationException extends HttpException {
public constructor(error: ZodError) {
const name = 'ValidationException'
const code = 'E_VALIDATION_ERROR'
const status = 422
const message = 'Validation error happened.'
const details = error.issues

super({ name, message, status, code, details })
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ declare module 'fastify' {

export * from '#src/types'

export * from '#src/router/RouteSchema'
export * from '#src/context/Request'
export * from '#src/context/Response'
export * from '#src/annotations/Controller'
Expand Down
22 changes: 19 additions & 3 deletions src/router/Route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ import type {
InterceptorRouteType
} from '#src/types'

import type { HTTPMethods, FastifySchema, RouteOptions } from 'fastify'
import type { HTTPMethods, RouteOptions } from 'fastify'
import { Is, Options, Macroable, Route as RouteHelper } from '@athenna/common'
import { UndefinedMethodException } from '#src/exceptions/UndefinedMethodException'
import { NotFoundValidatorException } from '#src/exceptions/NotFoundValidatorException'
import {
type RouteSchemaOptions,
normalizeRouteSchema
} from '#src/router/RouteSchema'
import { NotFoundMiddlewareException } from '#src/exceptions/NotFoundMiddlewareException'

export class Route extends Macroable {
Expand Down Expand Up @@ -341,8 +345,20 @@ export class Route extends Macroable {
* })
* ```
*/
public schema(options: FastifySchema): Route {
this.route.fastify.schema = options
public schema(options: RouteSchemaOptions): Route {
const { schema, zod } = normalizeRouteSchema(options)

this.route.fastify.schema = schema

if (zod) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.route.fastify.config.zod = zod
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete this.route.fastify.config.zod
}

return this
}
Expand Down
44 changes: 36 additions & 8 deletions src/router/RouteResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {

import type { HTTPMethods } from 'fastify'
import { Route } from '#src/router/Route'
import type { RouteSchemaOptions } from '#src/router/RouteSchema'
import { Is, String, Macroable, Options } from '@athenna/common'

export class RouteResource extends Macroable {
Expand All @@ -37,7 +38,7 @@ export class RouteResource extends Macroable {
public constructor(resource: string, controller: any) {
super()

this.resource = resource
this.resource = resource.replace(/^\/|\/$/g, '')
this.controller = controller

this.buildRoutes()
Expand Down Expand Up @@ -89,15 +90,15 @@ export class RouteResource extends Macroable {

return this
}

if (options.except.length) {
this.filter(options.except, true).forEach(route => {
route.middleware(middleware, options.prepend)
})

return this
}

this.routes.forEach(route => route.middleware(middleware, options.prepend))

return this
Expand Down Expand Up @@ -132,16 +133,18 @@ export class RouteResource extends Macroable {

return this
}

if (options.except.length) {
this.filter(options.except, true).forEach(route => {
route.interceptor(interceptor, options.prepend)
})

return this
}

this.routes.forEach(route => route.interceptor(interceptor, options.prepend))

this.routes.forEach(route =>
route.interceptor(interceptor, options.prepend)
)

return this
}
Expand Down Expand Up @@ -175,15 +178,15 @@ export class RouteResource extends Macroable {

return this
}

if (options.except.length) {
this.filter(options.except, true).forEach(route => {
route.terminator(terminator, options.prepend)
})

return this
}

this.routes.forEach(route => route.terminator(terminator, options.prepend))

return this
Expand Down Expand Up @@ -254,6 +257,31 @@ export class RouteResource extends Macroable {
return this
}

/**
* Set up schema options for specific route resource methods.
*
* @example
* ```ts
* Route.resource('/test', 'TestController').schema({
* index: { response: { 200: { type: 'object' } } },
* store: { body: { type: 'object' } }
* })
* ```
*/
public schema(
options: Partial<Record<RouteResourceTypes, RouteSchemaOptions>>
): RouteResource {
Object.entries(options).forEach(([name, schema]) => {
if (!schema) {
return
}

this.filter([name]).forEach(route => route.schema(schema))
})

return this
}

/**
* Filter routes by name.
*/
Expand Down
Loading
Loading