Skip to content

Commit

Permalink
Merge pull request #269 from frouriojs/develop
Browse files Browse the repository at this point in the history
fix: append AdditionalRequest to ctrl level hooks
  • Loading branch information
solufa authored Dec 9, 2022
2 parents 9e49b0a + 661b063 commit b42f4d6
Show file tree
Hide file tree
Showing 43 changed files with 365 additions and 575 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 16 additions & 11 deletions servers/all/$server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,27 @@ type RequestParams<T extends AspidaMethodParams> = Pick<{
headers: Required<T>['reqHeaders'] extends {} | null ? 'headers' : never
}['query' | 'body' | 'headers']>

type ServerHandler<T extends AspidaMethodParams, U extends Record<string, any> = {}> = (
type ServerHandler<T extends AspidaMethodParams, U extends Record<string, unknown> = {}> = (
req: RequestParams<T> & U
) => ServerResponse<T>

type ServerHandlerPromise<T extends AspidaMethodParams, U extends Record<string, any> = {}> = (
type ServerHandlerPromise<T extends AspidaMethodParams, U extends Record<string, unknown> = {}> = (
req: RequestParams<T> & U
) => Promise<ServerResponse<T>>

export type ServerMethodHandler<T extends AspidaMethodParams, U extends Record<string, any> = {}> = ServerHandler<T, U> | ServerHandlerPromise<T, U> | {
type AddedHandler<T, R extends Record<string, unknown>> = T extends (req: infer U, ...args: infer V) => infer W ? (req: U & Partial<R>, ...args: V) => W : never

export type ServerHooks<R extends Record<string, unknown> = {}> = {
onRequest?: AddedHandler<onRequestHookHandler, R> | AddedHandler<onRequestHookHandler, R>[]
preParsing?: AddedHandler<preParsingHookHandler, R> | AddedHandler<preParsingHookHandler, R>[]
preValidation?: AddedHandler<preValidationHookHandler, R> | AddedHandler<preValidationHookHandler, R>[]
preHandler?: AddedHandler<preHandlerHookHandler, R> | AddedHandler<preHandlerHookHandler, R>[]
}

export type ServerMethodHandler<T extends AspidaMethodParams, U extends Record<string, unknown> = {}> = ServerHandler<T, U> | ServerHandlerPromise<T, U> | {
validators?: Partial<{ [Key in keyof RequestParams<T>]?: z.ZodType<RequestParams<T>[Key]>}>
schemas?: { response?: { [V in HttpStatusOk]?: Schema }}
hooks?: {
onRequest?: onRequestHookHandler | onRequestHookHandler[]
preParsing?: preParsingHookHandler | preParsingHookHandler[]
preValidation?: preValidationHookHandler | preValidationHookHandler[]
preHandler?: preHandlerHookHandler | preHandlerHookHandler[]
}
hooks?: ServerHooks<U>
handler: ServerHandler<T, U> | ServerHandlerPromise<T, U>
}

Expand Down Expand Up @@ -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(
Expand Down
21 changes: 7 additions & 14 deletions servers/all/api/$relay.ts
Original file line number Diff line number Diff line change
@@ -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<T extends { [U in keyof Methods]?: { [V in HttpStatusOk]?: Schema }}>(methods: () => T) {
return methods
}

export function defineHooks<T extends Hooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T
export function defineHooks<T extends Record<string, any>, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable<T, [FastifyInstance], U>
export function defineHooks<T extends Record<string, any>>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) {
export function defineHooks<T extends ServerHooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T
export function defineHooks<T extends Record<string, unknown>, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable<T, [FastifyInstance], U>
export function defineHooks<T extends Record<string, unknown>>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) {
return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks
}

Expand All @@ -28,7 +21,7 @@ type ServerMethods = {
}

export function defineController<M extends ServerMethods>(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M
export function defineController<M extends ServerMethods, T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable<T, [FastifyInstance], M>
export function defineController<M extends ServerMethods, T extends Record<string, any>>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) {
export function defineController<M extends ServerMethods, T extends Record<string, unknown>>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable<T, [FastifyInstance], M>
export function defineController<M extends ServerMethods, T extends Record<string, unknown>>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) {
return cb && typeof methods !== 'function' ? depend(methods, cb) : methods
}
21 changes: 7 additions & 14 deletions servers/all/api/500/$relay.ts
Original file line number Diff line number Diff line change
@@ -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<T extends { [U in keyof Methods]?: { [V in HttpStatusOk]?: Schema }}>(methods: () => T) {
return methods
}

export function defineHooks<T extends Hooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T
export function defineHooks<T extends Record<string, any>, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable<T, [FastifyInstance], U>
export function defineHooks<T extends Record<string, any>>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) {
export function defineHooks<T extends ServerHooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T
export function defineHooks<T extends Record<string, unknown>, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable<T, [FastifyInstance], U>
export function defineHooks<T extends Record<string, unknown>>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) {
return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks
}

Expand All @@ -28,7 +21,7 @@ type ServerMethods = {
}

export function defineController<M extends ServerMethods>(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M
export function defineController<M extends ServerMethods, T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable<T, [FastifyInstance], M>
export function defineController<M extends ServerMethods, T extends Record<string, any>>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) {
export function defineController<M extends ServerMethods, T extends Record<string, unknown>>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable<T, [FastifyInstance], M>
export function defineController<M extends ServerMethods, T extends Record<string, unknown>>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) {
return cb && typeof methods !== 'function' ? depend(methods, cb) : methods
}
21 changes: 7 additions & 14 deletions servers/all/api/empty/$relay.ts
Original file line number Diff line number Diff line change
@@ -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<T extends { [U in keyof Methods]?: { [V in HttpStatusOk]?: Schema }}>(methods: () => T) {
return methods
}

export function defineHooks<T extends Hooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T
export function defineHooks<T extends Record<string, any>, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable<T, [FastifyInstance], U>
export function defineHooks<T extends Record<string, any>>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) {
export function defineHooks<T extends ServerHooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T
export function defineHooks<T extends Record<string, unknown>, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable<T, [FastifyInstance], U>
export function defineHooks<T extends Record<string, unknown>>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) {
return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks
}

Expand All @@ -28,7 +21,7 @@ type ServerMethods = {
}

export function defineController<M extends ServerMethods>(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M
export function defineController<M extends ServerMethods, T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable<T, [FastifyInstance], M>
export function defineController<M extends ServerMethods, T extends Record<string, any>>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) {
export function defineController<M extends ServerMethods, T extends Record<string, unknown>>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable<T, [FastifyInstance], M>
export function defineController<M extends ServerMethods, T extends Record<string, unknown>>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) {
return cb && typeof methods !== 'function' ? depend(methods, cb) : methods
}
21 changes: 7 additions & 14 deletions servers/all/api/empty/noEmpty/$relay.ts
Original file line number Diff line number Diff line change
@@ -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<T extends { [U in keyof Methods]?: { [V in HttpStatusOk]?: Schema }}>(methods: () => T) {
return methods
}

export function defineHooks<T extends Hooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T
export function defineHooks<T extends Record<string, any>, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable<T, [FastifyInstance], U>
export function defineHooks<T extends Record<string, any>>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) {
export function defineHooks<T extends ServerHooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T
export function defineHooks<T extends Record<string, unknown>, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable<T, [FastifyInstance], U>
export function defineHooks<T extends Record<string, unknown>>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) {
return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks
}

Expand All @@ -28,7 +21,7 @@ type ServerMethods = {
}

export function defineController<M extends ServerMethods>(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M
export function defineController<M extends ServerMethods, T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable<T, [FastifyInstance], M>
export function defineController<M extends ServerMethods, T extends Record<string, any>>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) {
export function defineController<M extends ServerMethods, T extends Record<string, unknown>>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable<T, [FastifyInstance], M>
export function defineController<M extends ServerMethods, T extends Record<string, unknown>>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) {
return cb && typeof methods !== 'function' ? depend(methods, cb) : methods
}
21 changes: 7 additions & 14 deletions servers/all/api/multiForm/$relay.ts
Original file line number Diff line number Diff line change
@@ -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<T extends { [U in keyof Methods]?: { [V in HttpStatusOk]?: Schema }}>(methods: () => T) {
return methods
}

export function defineHooks<T extends Hooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T
export function defineHooks<T extends Record<string, any>, U extends Hooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable<T, [FastifyInstance], U>
export function defineHooks<T extends Record<string, any>>(hooks: (fastify: FastifyInstance) => Hooks | T, cb?: ((deps: T, fastify: FastifyInstance) => Hooks)) {
export function defineHooks<T extends ServerHooks>(hooks: (fastify: FastifyInstance) => T): (fastify: FastifyInstance) => T
export function defineHooks<T extends Record<string, unknown>, U extends ServerHooks>(deps: T, cb: (d: T, fastify: FastifyInstance) => U): Injectable<T, [FastifyInstance], U>
export function defineHooks<T extends Record<string, unknown>>(hooks: (fastify: FastifyInstance) => ServerHooks | T, cb?: ((deps: T, fastify: FastifyInstance) => ServerHooks)) {
return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks
}

Expand All @@ -28,7 +21,7 @@ type ServerMethods = {
}

export function defineController<M extends ServerMethods>(methods: (fastify: FastifyInstance) => M): (fastify: FastifyInstance) => M
export function defineController<M extends ServerMethods, T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable<T, [FastifyInstance], M>
export function defineController<M extends ServerMethods, T extends Record<string, any>>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) {
export function defineController<M extends ServerMethods, T extends Record<string, unknown>>(deps: T, cb: (d: T, fastify: FastifyInstance) => M): Injectable<T, [FastifyInstance], M>
export function defineController<M extends ServerMethods, T extends Record<string, unknown>>(methods: ((fastify: FastifyInstance) => M) | T, cb?: ((deps: T, fastify: FastifyInstance) => M)) {
return cb && typeof methods !== 'function' ? depend(methods, cb) : methods
}
Loading

0 comments on commit b42f4d6

Please sign in to comment.