From 526e3fd7060b161444eda43e0bb7b5afe9e3f544 Mon Sep 17 00:00:00 2001 From: solufa Date: Fri, 9 Dec 2022 21:09:19 +0900 Subject: [PATCH 1/2] fix: append AdditionalRequest to ctrl level hooks --- servers/all/$server.ts | 27 ++++++---- servers/all/api/$relay.ts | 21 +++----- servers/all/api/500/$relay.ts | 21 +++----- servers/all/api/empty/$relay.ts | 21 +++----- servers/all/api/empty/noEmpty/$relay.ts | 21 +++----- servers/all/api/multiForm/$relay.ts | 21 +++----- servers/all/api/texts/$relay.ts | 21 +++----- servers/all/api/texts/_label@string/$relay.ts | 20 +++---- servers/all/api/texts/sample/$relay.ts | 21 +++----- servers/all/api/users/$relay.ts | 21 +++----- .../all/api/users/_userId@number/$relay.ts | 22 +++----- .../api/users/_userId@number/_name/$relay.ts | 22 +++----- .../api/users/_userId@number/controller.ts | 27 ++++++---- servers/minimum/$server.ts | 22 ++++---- servers/minimum/api/$relay.ts | 21 +++----- servers/noMulter/$server.ts | 22 ++++---- servers/noMulter/api/$relay.ts | 21 +++----- servers/noMulter/api/empty/$relay.ts | 21 +++----- servers/noMulter/api/empty/noEmpty/$relay.ts | 21 +++----- servers/noMulter/api/texts/$relay.ts | 21 +++----- servers/noMulter/api/texts/sample/$relay.ts | 21 +++----- servers/noMulter/api/users/$relay.ts | 22 +++----- .../api/users/_userId@number/$relay.ts | 21 +++----- servers/noTypedParams/$server.ts | 22 ++++---- servers/noTypedParams/api/$relay.ts | 21 +++----- servers/noTypedParams/api/empty/$relay.ts | 21 +++----- .../noTypedParams/api/empty/noEmpty/$relay.ts | 21 +++----- servers/noTypedParams/api/multiForm/$relay.ts | 21 +++----- servers/noTypedParams/api/texts/$relay.ts | 21 +++----- .../noTypedParams/api/texts/sample/$relay.ts | 21 +++----- servers/noTypedParams/api/users/$relay.ts | 22 +++----- servers/noValidator/$server.ts | 22 ++++---- servers/noValidator/api/$relay.ts | 21 +++----- servers/noValidator/api/empty/$relay.ts | 21 +++----- .../noValidator/api/empty/noEmpty/$relay.ts | 21 +++----- servers/noValidator/api/multiForm/$relay.ts | 21 +++----- servers/noValidator/api/texts/$relay.ts | 21 +++----- .../noValidator/api/texts/sample/$relay.ts | 21 +++----- servers/noValidator/api/users/$relay.ts | 22 +++----- .../api/users/_userId@number/$relay.ts | 21 +++----- src/buildServerFile.ts | 22 ++++---- src/createControllersText.ts | 52 ++++++++----------- 42 files changed, 359 insertions(+), 575 deletions(-) diff --git a/servers/all/$server.ts b/servers/all/$server.ts index 92d145c..eae975a 100644 --- a/servers/all/$server.ts +++ b/servers/all/$server.ts @@ -81,23 +81,27 @@ type RequestParams = Pick<{ headers: Required['reqHeaders'] extends {} | null ? 'headers' : never }['query' | 'body' | 'headers']> -type ServerHandler = {}> = ( +type ServerHandler = {}> = ( req: RequestParams & U ) => ServerResponse -type ServerHandlerPromise = {}> = ( +type ServerHandlerPromise = {}> = ( req: RequestParams & U ) => Promise> -export type ServerMethodHandler = {}> = ServerHandler | ServerHandlerPromise | { +type AddedHandler> = T extends (req: infer U, ...args: infer V) => infer W ? (req: U & Partial, ...args: V) => W : never + +export type ServerHooks = {}> = { + onRequest?: AddedHandler | AddedHandler[] + preParsing?: AddedHandler | AddedHandler[] + preValidation?: AddedHandler | AddedHandler[] + preHandler?: AddedHandler | AddedHandler[] +} + +export type ServerMethodHandler = {}> = ServerHandler | ServerHandlerPromise | { validators?: Partial<{ [Key in keyof RequestParams]?: z.ZodType[Key]>}> schemas?: { response?: { [V in HttpStatusOk]?: Schema }} - hooks?: { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] - } + hooks?: ServerHooks handler: ServerHandler | ServerHandlerPromise } @@ -452,9 +456,10 @@ export default (fastify: FastifyInstance, options: FrourioOptions = {}) => { validatorCompiler, onRequest: [...hooks0.onRequest, hooks2.onRequest], preParsing: hooks0.preParsing, - preValidation: createTypedParamsHandler(['userId']) + preValidation: createTypedParamsHandler(['userId']), + preHandler: controller8.get.hooks.preHandler } as RouteShorthandOptions, - methodToHandler(controller8.get) + methodToHandler(controller8.get.handler) ) fastify.get( diff --git a/servers/all/api/$relay.ts b/servers/all/api/$relay.ts index 72f7c87..a4b8ddc 100644 --- a/servers/all/api/$relay.ts +++ b/servers/all/api/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../$server' +import type { ServerHooks, ServerMethodHandler } from '../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/all/api/500/$relay.ts b/servers/all/api/500/$relay.ts index 83f9f50..85d6ae1 100644 --- a/servers/all/api/500/$relay.ts +++ b/servers/all/api/500/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/all/api/empty/$relay.ts b/servers/all/api/empty/$relay.ts index 83f9f50..85d6ae1 100644 --- a/servers/all/api/empty/$relay.ts +++ b/servers/all/api/empty/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/all/api/empty/noEmpty/$relay.ts b/servers/all/api/empty/noEmpty/$relay.ts index 9a64363..c676b93 100644 --- a/servers/all/api/empty/noEmpty/$relay.ts +++ b/servers/all/api/empty/noEmpty/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/all/api/multiForm/$relay.ts b/servers/all/api/multiForm/$relay.ts index 83f9f50..85d6ae1 100644 --- a/servers/all/api/multiForm/$relay.ts +++ b/servers/all/api/multiForm/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/all/api/texts/$relay.ts b/servers/all/api/texts/$relay.ts index 83f9f50..85d6ae1 100644 --- a/servers/all/api/texts/$relay.ts +++ b/servers/all/api/texts/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/all/api/texts/_label@string/$relay.ts b/servers/all/api/texts/_label@string/$relay.ts index f9bc757..d277c72 100644 --- a/servers/all/api/texts/_label@string/$relay.ts +++ b/servers/all/api/texts/_label@string/$relay.ts @@ -1,18 +1,12 @@ import { z } from 'zod' import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} type Params = { label: string } @@ -27,9 +21,9 @@ export function defineResponseSchema(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -38,7 +32,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/all/api/texts/sample/$relay.ts b/servers/all/api/texts/sample/$relay.ts index 9a64363..c676b93 100644 --- a/servers/all/api/texts/sample/$relay.ts +++ b/servers/all/api/texts/sample/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/all/api/users/$relay.ts b/servers/all/api/users/$relay.ts index c443d82..f6372cb 100644 --- a/servers/all/api/users/$relay.ts +++ b/servers/all/api/users/$relay.ts @@ -1,29 +1,22 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../$server' import type { AdditionalRequest as AdditionalRequest0 } from './hooks' import type { AdditionalRequest as AdditionalRequest1 } from './controller' import type { Methods } from './' type AdditionalRequest = AdditionalRequest0 & AdditionalRequest1 -type AddedHandler = T extends (req: infer U, ...args: infer V) => infer W ? (req: U & Partial, ...args: V) => W : never -type Hooks = { - onRequest?: AddedHandler | AddedHandler[] - preParsing?: AddedHandler | AddedHandler[] - preValidation?: AddedHandler | AddedHandler[] - preHandler?: AddedHandler | AddedHandler[] -} export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -32,7 +25,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/all/api/users/_userId@number/$relay.ts b/servers/all/api/users/_userId@number/$relay.ts index 649b796..88eded7 100644 --- a/servers/all/api/users/_userId@number/$relay.ts +++ b/servers/all/api/users/_userId@number/$relay.ts @@ -1,22 +1,16 @@ import { z } from 'zod' import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../../$server' import type { AdditionalRequest as AdditionalRequest0 } from '../hooks' import type { AdditionalRequest as AdditionalRequest1 } from './controller' import type { Methods } from './' type AdditionalRequest = AdditionalRequest0 & AdditionalRequest1 -type AddedHandler = T extends (req: infer U, ...args: infer V) => infer W ? (req: U & Partial, ...args: V) => W : never -type Hooks = { - onRequest?: AddedHandler | AddedHandler[] - preParsing?: AddedHandler | AddedHandler[] - preValidation?: AddedHandler | AddedHandler[] - preHandler?: AddedHandler | AddedHandler[] -} + type Params = { userId: number } @@ -31,9 +25,9 @@ export function defineResponseSchema(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -42,7 +36,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/all/api/users/_userId@number/_name/$relay.ts b/servers/all/api/users/_userId@number/_name/$relay.ts index 48a3a31..1ca5a4d 100644 --- a/servers/all/api/users/_userId@number/_name/$relay.ts +++ b/servers/all/api/users/_userId@number/_name/$relay.ts @@ -1,22 +1,16 @@ import { z } from 'zod' import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../../../$server' import type { AdditionalRequest as AdditionalRequest0 } from '../../hooks' import type { AdditionalRequest as AdditionalRequest1 } from './hooks' import type { Methods } from './' type AdditionalRequest = AdditionalRequest0 & AdditionalRequest1 -type AddedHandler = T extends (req: infer U, ...args: infer V) => infer W ? (req: U & Partial, ...args: V) => W : never -type Hooks = { - onRequest?: AddedHandler | AddedHandler[] - preParsing?: AddedHandler | AddedHandler[] - preValidation?: AddedHandler | AddedHandler[] - preHandler?: AddedHandler | AddedHandler[] -} + type Params = { userId: number name: string @@ -32,9 +26,9 @@ export function defineResponseSchema(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -43,7 +37,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/all/api/users/_userId@number/controller.ts b/servers/all/api/users/_userId@number/controller.ts index 5889807..9c9ed23 100644 --- a/servers/all/api/users/_userId@number/controller.ts +++ b/servers/all/api/users/_userId@number/controller.ts @@ -5,15 +5,22 @@ export type AdditionalRequest = { } export default defineController(() => ({ - get: ({ params }) => ({ - status: 200, - body: { - id: params.userId, - name: 'bbb', - location: { - country: 'JP', - stateProvince: 'Tokyo' + get: { + hooks: { + preHandler: (req, _, done) => { + !req.name && done() } - } - }) + }, + handler: ({ params }) => ({ + status: 200, + body: { + id: params.userId, + name: 'bbb', + location: { + country: 'JP', + stateProvince: 'Tokyo' + } + } + }) + } })) diff --git a/servers/minimum/$server.ts b/servers/minimum/$server.ts index 18863f0..00cdb5c 100644 --- a/servers/minimum/$server.ts +++ b/servers/minimum/$server.ts @@ -41,23 +41,27 @@ type RequestParams = Pick<{ headers: Required['reqHeaders'] extends {} | null ? 'headers' : never }['query' | 'body' | 'headers']> -type ServerHandler = {}> = ( +type ServerHandler = {}> = ( req: RequestParams & U ) => ServerResponse -type ServerHandlerPromise = {}> = ( +type ServerHandlerPromise = {}> = ( req: RequestParams & U ) => Promise> -export type ServerMethodHandler = {}> = ServerHandler | ServerHandlerPromise | { +type AddedHandler> = T extends (req: infer U, ...args: infer V) => infer W ? (req: U & Partial, ...args: V) => W : never + +export type ServerHooks = {}> = { + onRequest?: AddedHandler | AddedHandler[] + preParsing?: AddedHandler | AddedHandler[] + preValidation?: AddedHandler | AddedHandler[] + preHandler?: AddedHandler | AddedHandler[] +} + +export type ServerMethodHandler = {}> = ServerHandler | ServerHandlerPromise | { validators?: Partial<{ [Key in keyof RequestParams]?: z.ZodType[Key]>}> schemas?: { response?: { [V in HttpStatusOk]?: Schema }} - hooks?: { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] - } + hooks?: ServerHooks handler: ServerHandler | ServerHandlerPromise } diff --git a/servers/minimum/api/$relay.ts b/servers/minimum/api/$relay.ts index 72f7c87..a4b8ddc 100644 --- a/servers/minimum/api/$relay.ts +++ b/servers/minimum/api/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../$server' +import type { ServerHooks, ServerMethodHandler } from '../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noMulter/$server.ts b/servers/noMulter/$server.ts index bf3bf42..a9f138c 100644 --- a/servers/noMulter/$server.ts +++ b/servers/noMulter/$server.ts @@ -59,23 +59,27 @@ type RequestParams = Pick<{ headers: Required['reqHeaders'] extends {} | null ? 'headers' : never }['query' | 'body' | 'headers']> -type ServerHandler = {}> = ( +type ServerHandler = {}> = ( req: RequestParams & U ) => ServerResponse -type ServerHandlerPromise = {}> = ( +type ServerHandlerPromise = {}> = ( req: RequestParams & U ) => Promise> -export type ServerMethodHandler = {}> = ServerHandler | ServerHandlerPromise | { +type AddedHandler> = T extends (req: infer U, ...args: infer V) => infer W ? (req: U & Partial, ...args: V) => W : never + +export type ServerHooks = {}> = { + onRequest?: AddedHandler | AddedHandler[] + preParsing?: AddedHandler | AddedHandler[] + preValidation?: AddedHandler | AddedHandler[] + preHandler?: AddedHandler | AddedHandler[] +} + +export type ServerMethodHandler = {}> = ServerHandler | ServerHandlerPromise | { validators?: Partial<{ [Key in keyof RequestParams]?: z.ZodType[Key]>}> schemas?: { response?: { [V in HttpStatusOk]?: Schema }} - hooks?: { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] - } + hooks?: ServerHooks handler: ServerHandler | ServerHandlerPromise } diff --git a/servers/noMulter/api/$relay.ts b/servers/noMulter/api/$relay.ts index 72f7c87..a4b8ddc 100644 --- a/servers/noMulter/api/$relay.ts +++ b/servers/noMulter/api/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../$server' +import type { ServerHooks, ServerMethodHandler } from '../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noMulter/api/empty/$relay.ts b/servers/noMulter/api/empty/$relay.ts index 83f9f50..85d6ae1 100644 --- a/servers/noMulter/api/empty/$relay.ts +++ b/servers/noMulter/api/empty/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noMulter/api/empty/noEmpty/$relay.ts b/servers/noMulter/api/empty/noEmpty/$relay.ts index 9a64363..c676b93 100644 --- a/servers/noMulter/api/empty/noEmpty/$relay.ts +++ b/servers/noMulter/api/empty/noEmpty/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noMulter/api/texts/$relay.ts b/servers/noMulter/api/texts/$relay.ts index 83f9f50..85d6ae1 100644 --- a/servers/noMulter/api/texts/$relay.ts +++ b/servers/noMulter/api/texts/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noMulter/api/texts/sample/$relay.ts b/servers/noMulter/api/texts/sample/$relay.ts index 9a64363..c676b93 100644 --- a/servers/noMulter/api/texts/sample/$relay.ts +++ b/servers/noMulter/api/texts/sample/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noMulter/api/users/$relay.ts b/servers/noMulter/api/users/$relay.ts index 667289a..06f3524 100644 --- a/servers/noMulter/api/users/$relay.ts +++ b/servers/noMulter/api/users/$relay.ts @@ -1,27 +1,19 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../$server' import type { AdditionalRequest } from './hooks' import type { Methods } from './' -type AddedHandler = T extends (req: infer U, ...args: infer V) => infer W ? (req: U & Partial, ...args: V) => W : never -type Hooks = { - onRequest?: AddedHandler | AddedHandler[] - preParsing?: AddedHandler | AddedHandler[] - preValidation?: AddedHandler | AddedHandler[] - preHandler?: AddedHandler | AddedHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -30,7 +22,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noMulter/api/users/_userId@number/$relay.ts b/servers/noMulter/api/users/_userId@number/$relay.ts index fd4add9..9fa6de1 100644 --- a/servers/noMulter/api/users/_userId@number/$relay.ts +++ b/servers/noMulter/api/users/_userId@number/$relay.ts @@ -1,20 +1,13 @@ import { z } from 'zod' import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../../$server' import type { AdditionalRequest } from './../hooks' import type { Methods } from './' -type AddedHandler = T extends (req: infer U, ...args: infer V) => infer W ? (req: U & Partial, ...args: V) => W : never -type Hooks = { - onRequest?: AddedHandler | AddedHandler[] - preParsing?: AddedHandler | AddedHandler[] - preValidation?: AddedHandler | AddedHandler[] - preHandler?: AddedHandler | AddedHandler[] -} type Params = { userId: number } @@ -29,9 +22,9 @@ export function defineResponseSchema(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -40,7 +33,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noTypedParams/$server.ts b/servers/noTypedParams/$server.ts index a8045f5..fa90bba 100644 --- a/servers/noTypedParams/$server.ts +++ b/servers/noTypedParams/$server.ts @@ -72,23 +72,27 @@ type RequestParams = Pick<{ headers: Required['reqHeaders'] extends {} | null ? 'headers' : never }['query' | 'body' | 'headers']> -type ServerHandler = {}> = ( +type ServerHandler = {}> = ( req: RequestParams & U ) => ServerResponse -type ServerHandlerPromise = {}> = ( +type ServerHandlerPromise = {}> = ( req: RequestParams & U ) => Promise> -export type ServerMethodHandler = {}> = ServerHandler | ServerHandlerPromise | { +type AddedHandler> = T extends (req: infer U, ...args: infer V) => infer W ? (req: U & Partial, ...args: V) => W : never + +export type ServerHooks = {}> = { + onRequest?: AddedHandler | AddedHandler[] + preParsing?: AddedHandler | AddedHandler[] + preValidation?: AddedHandler | AddedHandler[] + preHandler?: AddedHandler | AddedHandler[] +} + +export type ServerMethodHandler = {}> = ServerHandler | ServerHandlerPromise | { validators?: Partial<{ [Key in keyof RequestParams]?: z.ZodType[Key]>}> schemas?: { response?: { [V in HttpStatusOk]?: Schema }} - hooks?: { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] - } + hooks?: ServerHooks handler: ServerHandler | ServerHandlerPromise } diff --git a/servers/noTypedParams/api/$relay.ts b/servers/noTypedParams/api/$relay.ts index 72f7c87..a4b8ddc 100644 --- a/servers/noTypedParams/api/$relay.ts +++ b/servers/noTypedParams/api/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../$server' +import type { ServerHooks, ServerMethodHandler } from '../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noTypedParams/api/empty/$relay.ts b/servers/noTypedParams/api/empty/$relay.ts index 83f9f50..85d6ae1 100644 --- a/servers/noTypedParams/api/empty/$relay.ts +++ b/servers/noTypedParams/api/empty/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noTypedParams/api/empty/noEmpty/$relay.ts b/servers/noTypedParams/api/empty/noEmpty/$relay.ts index 9a64363..c676b93 100644 --- a/servers/noTypedParams/api/empty/noEmpty/$relay.ts +++ b/servers/noTypedParams/api/empty/noEmpty/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noTypedParams/api/multiForm/$relay.ts b/servers/noTypedParams/api/multiForm/$relay.ts index 83f9f50..85d6ae1 100644 --- a/servers/noTypedParams/api/multiForm/$relay.ts +++ b/servers/noTypedParams/api/multiForm/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noTypedParams/api/texts/$relay.ts b/servers/noTypedParams/api/texts/$relay.ts index 83f9f50..85d6ae1 100644 --- a/servers/noTypedParams/api/texts/$relay.ts +++ b/servers/noTypedParams/api/texts/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noTypedParams/api/texts/sample/$relay.ts b/servers/noTypedParams/api/texts/sample/$relay.ts index 9a64363..c676b93 100644 --- a/servers/noTypedParams/api/texts/sample/$relay.ts +++ b/servers/noTypedParams/api/texts/sample/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noTypedParams/api/users/$relay.ts b/servers/noTypedParams/api/users/$relay.ts index 667289a..06f3524 100644 --- a/servers/noTypedParams/api/users/$relay.ts +++ b/servers/noTypedParams/api/users/$relay.ts @@ -1,27 +1,19 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../$server' import type { AdditionalRequest } from './hooks' import type { Methods } from './' -type AddedHandler = T extends (req: infer U, ...args: infer V) => infer W ? (req: U & Partial, ...args: V) => W : never -type Hooks = { - onRequest?: AddedHandler | AddedHandler[] - preParsing?: AddedHandler | AddedHandler[] - preValidation?: AddedHandler | AddedHandler[] - preHandler?: AddedHandler | AddedHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -30,7 +22,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noValidator/$server.ts b/servers/noValidator/$server.ts index 2082b43..fffd0e4 100644 --- a/servers/noValidator/$server.ts +++ b/servers/noValidator/$server.ts @@ -64,23 +64,27 @@ type RequestParams = Pick<{ headers: Required['reqHeaders'] extends {} | null ? 'headers' : never }['query' | 'body' | 'headers']> -type ServerHandler = {}> = ( +type ServerHandler = {}> = ( req: RequestParams & U ) => ServerResponse -type ServerHandlerPromise = {}> = ( +type ServerHandlerPromise = {}> = ( req: RequestParams & U ) => Promise> -export type ServerMethodHandler = {}> = ServerHandler | ServerHandlerPromise | { +type AddedHandler> = T extends (req: infer U, ...args: infer V) => infer W ? (req: U & Partial, ...args: V) => W : never + +export type ServerHooks = {}> = { + onRequest?: AddedHandler | AddedHandler[] + preParsing?: AddedHandler | AddedHandler[] + preValidation?: AddedHandler | AddedHandler[] + preHandler?: AddedHandler | AddedHandler[] +} + +export type ServerMethodHandler = {}> = ServerHandler | ServerHandlerPromise | { validators?: Partial<{ [Key in keyof RequestParams]?: z.ZodType[Key]>}> schemas?: { response?: { [V in HttpStatusOk]?: Schema }} - hooks?: { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] - } + hooks?: ServerHooks handler: ServerHandler | ServerHandlerPromise } diff --git a/servers/noValidator/api/$relay.ts b/servers/noValidator/api/$relay.ts index 72f7c87..a4b8ddc 100644 --- a/servers/noValidator/api/$relay.ts +++ b/servers/noValidator/api/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../$server' +import type { ServerHooks, ServerMethodHandler } from '../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noValidator/api/empty/$relay.ts b/servers/noValidator/api/empty/$relay.ts index 83f9f50..85d6ae1 100644 --- a/servers/noValidator/api/empty/$relay.ts +++ b/servers/noValidator/api/empty/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noValidator/api/empty/noEmpty/$relay.ts b/servers/noValidator/api/empty/noEmpty/$relay.ts index 9a64363..c676b93 100644 --- a/servers/noValidator/api/empty/noEmpty/$relay.ts +++ b/servers/noValidator/api/empty/noEmpty/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noValidator/api/multiForm/$relay.ts b/servers/noValidator/api/multiForm/$relay.ts index 83f9f50..85d6ae1 100644 --- a/servers/noValidator/api/multiForm/$relay.ts +++ b/servers/noValidator/api/multiForm/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noValidator/api/texts/$relay.ts b/servers/noValidator/api/texts/$relay.ts index 83f9f50..85d6ae1 100644 --- a/servers/noValidator/api/texts/$relay.ts +++ b/servers/noValidator/api/texts/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noValidator/api/texts/sample/$relay.ts b/servers/noValidator/api/texts/sample/$relay.ts index 9a64363..c676b93 100644 --- a/servers/noValidator/api/texts/sample/$relay.ts +++ b/servers/noValidator/api/texts/sample/$relay.ts @@ -1,25 +1,18 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../../$server' import type { Methods } from './' -type Hooks = { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -28,7 +21,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noValidator/api/users/$relay.ts b/servers/noValidator/api/users/$relay.ts index 667289a..06f3524 100644 --- a/servers/noValidator/api/users/$relay.ts +++ b/servers/noValidator/api/users/$relay.ts @@ -1,27 +1,19 @@ import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../$server' import type { AdditionalRequest } from './hooks' import type { Methods } from './' -type AddedHandler = T extends (req: infer U, ...args: infer V) => infer W ? (req: U & Partial, ...args: V) => W : never -type Hooks = { - onRequest?: AddedHandler | AddedHandler[] - preParsing?: AddedHandler | AddedHandler[] - preValidation?: AddedHandler | AddedHandler[] - preHandler?: AddedHandler | AddedHandler[] -} - export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -30,7 +22,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/servers/noValidator/api/users/_userId@number/$relay.ts b/servers/noValidator/api/users/_userId@number/$relay.ts index fd4add9..9fa6de1 100644 --- a/servers/noValidator/api/users/_userId@number/$relay.ts +++ b/servers/noValidator/api/users/_userId@number/$relay.ts @@ -1,20 +1,13 @@ import { z } from 'zod' import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '../../../$server' +import type { ServerHooks, ServerMethodHandler } from '../../../$server' import type { AdditionalRequest } from './../hooks' import type { Methods } from './' -type AddedHandler = T extends (req: infer U, ...args: infer V) => infer W ? (req: U & Partial, ...args: V) => W : never -type Hooks = { - onRequest?: AddedHandler | AddedHandler[] - preParsing?: AddedHandler | AddedHandler[] - preValidation?: AddedHandler | AddedHandler[] - preHandler?: AddedHandler | AddedHandler[] -} type Params = { userId: number } @@ -29,9 +22,9 @@ export function defineResponseSchema(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -40,7 +33,7 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } diff --git a/src/buildServerFile.ts b/src/buildServerFile.ts index 73b78f0..6075fe7 100644 --- a/src/buildServerFile.ts +++ b/src/buildServerFile.ts @@ -143,23 +143,27 @@ type RequestParams = Pick<{ headers: Required['reqHeaders'] extends {} | null ? 'headers' : never }['query' | 'body' | 'headers']> -type ServerHandler = {}> = ( +type ServerHandler = {}> = ( req: RequestParams & U ) => ServerResponse -type ServerHandlerPromise = {}> = ( +type ServerHandlerPromise = {}> = ( req: RequestParams & U ) => Promise> -export type ServerMethodHandler = {}> = ServerHandler | ServerHandlerPromise | { +type AddedHandler> = T extends (req: infer U, ...args: infer V) => infer W ? (req: U & Partial, ...args: V) => W : never + +export type ServerHooks = {}> = { + onRequest?: AddedHandler | AddedHandler[] + preParsing?: AddedHandler | AddedHandler[] + preValidation?: AddedHandler | AddedHandler[] + preHandler?: AddedHandler | AddedHandler[] +} + +export type ServerMethodHandler = {}> = ServerHandler | ServerHandlerPromise | { validators?: Partial<{ [Key in keyof RequestParams]?: z.ZodType[Key]>}> schemas?: { response?: { [V in HttpStatusOk]?: Schema }} - hooks?: { - onRequest?: onRequestHookHandler | onRequestHookHandler[] - preParsing?: preParsingHookHandler | preParsingHookHandler[] - preValidation?: preValidationHookHandler | preValidationHookHandler[] - preHandler?: preHandlerHookHandler | preHandlerHookHandler[] - } + hooks?: ServerHooks handler: ServerHandler | ServerHandlerPromise } ${ diff --git a/src/createControllersText.ts b/src/createControllersText.ts index e4b8e92..2d48b76 100644 --- a/src/createControllersText.ts +++ b/src/createControllersText.ts @@ -61,10 +61,10 @@ const createRelayFile = ( currentParam ? "import { z } from 'zod'\n" : '' }import type { Injectable } from 'velona' import { depend } from 'velona' -import type { FastifyInstance, onRequestHookHandler, preParsingHookHandler, preValidationHookHandler, preHandlerHookHandler } from 'fastify' +import type { FastifyInstance } from 'fastify' import type { Schema } from 'fast-json-stringify' import type { HttpStatusOk } from 'aspida' -import type { ServerMethodHandler } from '${appText}' +import type { ServerHooks, ServerMethodHandler } from '${appText}' ${ hasMultiAdditionals ? additionalReqs @@ -85,45 +85,35 @@ ${ hasMultiAdditionals ? `type AdditionalRequest = ${additionalReqs .map((_, i) => `AdditionalRequest${i}`) - .join(' & ')}\n` + .join(' & ')}\n\n` : '' }${ - hasAdditionals - ? 'type AddedHandler = T extends (req: infer U, ...args: infer V) => infer W ? (req: U & Partial, ...args: V) => W : never\n' + params.length + ? `type Params = {\n${params.map(v => ` ${v[0]}: ${v[1]}`).join('\n')}\n}\n\n` : '' - }type Hooks = { -${[ - ['onRequest', 'onRequestHookHandler'], - ['preParsing', 'preParsingHookHandler'], - ['preValidation', 'preValidationHookHandler'], - ['preHandler', 'preHandlerHookHandler'] -] - .map(([key, val]) => - hasAdditionals - ? ` ${key}?: AddedHandler<${val}> | AddedHandler<${val}>[]\n` - : ` ${key}?: ${val} | ${val}[]\n` - ) - .join('')}}${ - params.length ? `\ntype Params = {\n${params.map(v => ` ${v[0]}: ${v[1]}`).join('\n')}\n}` : '' }${ currentParam - ? ` - -export function defineValidators(validator: (fastify: FastifyInstance) => { + ? `export function defineValidators(validator: (fastify: FastifyInstance) => { params: z.ZodType<{ ${currentParam[0]}: ${currentParam[1]} }> }) { return validator -}` +}\n\n` : '' - } - -export function defineResponseSchema(methods: () => T) { + }export function defineResponseSchema(methods: () => T) { return methods } -export function defineHooks(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T -export function defineHooks, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable -export function defineHooks>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) { +export function defineHooks' : '' + }>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T +export function defineHooks, U extends ServerHooks${ + hasAdditionals ? '' : '' + }>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable +export function defineHooks>(hooks: (fastify: FastifyInstance) => ServerHooks${ + hasAdditionals ? '' : '' + } | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks${ + hasAdditionals ? '' : '' + })) { return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks } @@ -136,8 +126,8 @@ type ServerMethods = { } export function defineController(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M -export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable -export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { +export function defineController>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable +export function defineController>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) { return cb && typeof methods !== 'function' ? depend(methods, cb) : methods } ` From 661b063ba106776c2d47239ca0e5b6b667aa8850 Mon Sep 17 00:00:00 2001 From: solufa Date: Fri, 9 Dec 2022 21:12:30 +0900 Subject: [PATCH 2/2] chore: update CHANGELOG --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 357fc14..22c7a60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +## [0.31.1](https://github.com/frouriojs/frourio/compare/v0.31.0...v0.31.1) (2022-12-09) + +### Bug Fixes + +- append AdditionalRequest to ctrl level hooks (https://github.com/frouriojs/frourio/pull/269) + ## [0.31.0](https://github.com/frouriojs/frourio/compare/v0.30.2...v0.31.0) (2022-11-24) ### Features