Skip to content

Commit

Permalink
feat: update velona to use Injectable type
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Dec 29, 2020
1 parent 5359ff4 commit 2e782fa
Show file tree
Hide file tree
Showing 38 changed files with 122 additions and 116 deletions.
18 changes: 12 additions & 6 deletions __test__/index.spec.ts
Expand Up @@ -124,12 +124,18 @@ test('POST: 400', async () => {
test('controller dependency injection', async () => {
let val = 0
const id = '5'
const injectedController = controller.inject({
log: (n: number) => {
val = n
return Promise.resolve(n)
}
})(server)
const injectedController = controller
.inject({
log: () => {
throw new Error()
}
})
.inject(() => ({
log: (n: number) => {
val = n
return Promise.resolve(n)
}
}))(server)

await expect(
injectedController.get({
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -85,7 +85,7 @@
},
"dependencies": {
"aspida": "^1.1.1",
"velona": "^0.6.0"
"velona": "^0.7.0"
},
"devDependencies": {
"@aspida/axios": "^1.1.1",
Expand Down
6 changes: 3 additions & 3 deletions servers/all/api/$relay.ts
Expand Up @@ -4,7 +4,7 @@ import type { Schema } from 'fast-json-stringify'
import type { HttpStatusOk } from 'aspida'
import type { ServerMethods } from '../$server'
import type { Methods } from './'
import { depend } from 'velona'
import { Injectable, depend } from 'velona'

type Hooks = {
onRequest?: onRequestHookHandler | onRequestHookHandler[]
Expand All @@ -19,13 +19,13 @@ export function defineResponseSchema<T extends { [U in keyof ControllerMethods]?
}

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): { (fastify: FastifyInstance): U; inject(d: Partial<T>): (fastify: FastifyInstance) => U }
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) {
return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks
}

export function defineController(methods: (fastify: FastifyInstance) => ControllerMethods): (fastify: FastifyInstance) => ControllerMethods
export function defineController<T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => ControllerMethods): { (fastify: FastifyInstance): ControllerMethods; inject(d: Partial<T>): (fastify: FastifyInstance) => ControllerMethods }
export function defineController<T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => ControllerMethods): Injectable<T, [FastifyInstance], ControllerMethods>
export function defineController<T extends Record<string, any>>(methods: (fastify: FastifyInstance) => ControllerMethods | T, cb?: (deps: T, fastify: FastifyInstance) => ControllerMethods) {
return cb && typeof methods !== 'function' ? depend(methods, cb) : methods
}
6 changes: 3 additions & 3 deletions servers/all/api/500/$relay.ts
Expand Up @@ -4,7 +4,7 @@ import type { Schema } from 'fast-json-stringify'
import type { HttpStatusOk } from 'aspida'
import type { ServerMethods } from '../../$server'
import type { Methods } from './'
import { depend } from 'velona'
import { Injectable, depend } from 'velona'

type Hooks = {
onRequest?: onRequestHookHandler | onRequestHookHandler[]
Expand All @@ -19,13 +19,13 @@ export function defineResponseSchema<T extends { [U in keyof ControllerMethods]?
}

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): { (fastify: FastifyInstance): U; inject(d: Partial<T>): (fastify: FastifyInstance) => U }
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) {
return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks
}

export function defineController(methods: (fastify: FastifyInstance) => ControllerMethods): (fastify: FastifyInstance) => ControllerMethods
export function defineController<T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => ControllerMethods): { (fastify: FastifyInstance): ControllerMethods; inject(d: Partial<T>): (fastify: FastifyInstance) => ControllerMethods }
export function defineController<T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => ControllerMethods): Injectable<T, [FastifyInstance], ControllerMethods>
export function defineController<T extends Record<string, any>>(methods: (fastify: FastifyInstance) => ControllerMethods | T, cb?: (deps: T, fastify: FastifyInstance) => ControllerMethods) {
return cb && typeof methods !== 'function' ? depend(methods, cb) : methods
}
6 changes: 3 additions & 3 deletions servers/all/api/empty/$relay.ts
Expand Up @@ -4,7 +4,7 @@ import type { Schema } from 'fast-json-stringify'
import type { HttpStatusOk } from 'aspida'
import type { ServerMethods } from '../../$server'
import type { Methods } from './'
import { depend } from 'velona'
import { Injectable, depend } from 'velona'

type Hooks = {
onRequest?: onRequestHookHandler | onRequestHookHandler[]
Expand All @@ -19,13 +19,13 @@ export function defineResponseSchema<T extends { [U in keyof ControllerMethods]?
}

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): { (fastify: FastifyInstance): U; inject(d: Partial<T>): (fastify: FastifyInstance) => U }
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) {
return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks
}

export function defineController(methods: (fastify: FastifyInstance) => ControllerMethods): (fastify: FastifyInstance) => ControllerMethods
export function defineController<T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => ControllerMethods): { (fastify: FastifyInstance): ControllerMethods; inject(d: Partial<T>): (fastify: FastifyInstance) => ControllerMethods }
export function defineController<T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => ControllerMethods): Injectable<T, [FastifyInstance], ControllerMethods>
export function defineController<T extends Record<string, any>>(methods: (fastify: FastifyInstance) => ControllerMethods | T, cb?: (deps: T, fastify: FastifyInstance) => ControllerMethods) {
return cb && typeof methods !== 'function' ? depend(methods, cb) : methods
}
6 changes: 3 additions & 3 deletions servers/all/api/empty/noEmpty/$relay.ts
Expand Up @@ -4,7 +4,7 @@ import type { Schema } from 'fast-json-stringify'
import type { HttpStatusOk } from 'aspida'
import type { ServerMethods } from '../../../$server'
import type { Methods } from './'
import { depend } from 'velona'
import { Injectable, depend } from 'velona'

type Hooks = {
onRequest?: onRequestHookHandler | onRequestHookHandler[]
Expand All @@ -19,13 +19,13 @@ export function defineResponseSchema<T extends { [U in keyof ControllerMethods]?
}

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): { (fastify: FastifyInstance): U; inject(d: Partial<T>): (fastify: FastifyInstance) => U }
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) {
return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks
}

export function defineController(methods: (fastify: FastifyInstance) => ControllerMethods): (fastify: FastifyInstance) => ControllerMethods
export function defineController<T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => ControllerMethods): { (fastify: FastifyInstance): ControllerMethods; inject(d: Partial<T>): (fastify: FastifyInstance) => ControllerMethods }
export function defineController<T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => ControllerMethods): Injectable<T, [FastifyInstance], ControllerMethods>
export function defineController<T extends Record<string, any>>(methods: (fastify: FastifyInstance) => ControllerMethods | T, cb?: (deps: T, fastify: FastifyInstance) => ControllerMethods) {
return cb && typeof methods !== 'function' ? depend(methods, cb) : methods
}
6 changes: 3 additions & 3 deletions servers/all/api/multiForm/$relay.ts
Expand Up @@ -4,7 +4,7 @@ import type { Schema } from 'fast-json-stringify'
import type { HttpStatusOk } from 'aspida'
import type { ServerMethods } from '../../$server'
import type { Methods } from './'
import { depend } from 'velona'
import { Injectable, depend } from 'velona'

type Hooks = {
onRequest?: onRequestHookHandler | onRequestHookHandler[]
Expand All @@ -19,13 +19,13 @@ export function defineResponseSchema<T extends { [U in keyof ControllerMethods]?
}

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): { (fastify: FastifyInstance): U; inject(d: Partial<T>): (fastify: FastifyInstance) => U }
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) {
return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks
}

export function defineController(methods: (fastify: FastifyInstance) => ControllerMethods): (fastify: FastifyInstance) => ControllerMethods
export function defineController<T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => ControllerMethods): { (fastify: FastifyInstance): ControllerMethods; inject(d: Partial<T>): (fastify: FastifyInstance) => ControllerMethods }
export function defineController<T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => ControllerMethods): Injectable<T, [FastifyInstance], ControllerMethods>
export function defineController<T extends Record<string, any>>(methods: (fastify: FastifyInstance) => ControllerMethods | T, cb?: (deps: T, fastify: FastifyInstance) => ControllerMethods) {
return cb && typeof methods !== 'function' ? depend(methods, cb) : methods
}
6 changes: 3 additions & 3 deletions servers/all/api/texts/$relay.ts
Expand Up @@ -4,7 +4,7 @@ import type { Schema } from 'fast-json-stringify'
import type { HttpStatusOk } from 'aspida'
import type { ServerMethods } from '../../$server'
import type { Methods } from './'
import { depend } from 'velona'
import { Injectable, depend } from 'velona'

type Hooks = {
onRequest?: onRequestHookHandler | onRequestHookHandler[]
Expand All @@ -19,13 +19,13 @@ export function defineResponseSchema<T extends { [U in keyof ControllerMethods]?
}

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): { (fastify: FastifyInstance): U; inject(d: Partial<T>): (fastify: FastifyInstance) => U }
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) {
return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks
}

export function defineController(methods: (fastify: FastifyInstance) => ControllerMethods): (fastify: FastifyInstance) => ControllerMethods
export function defineController<T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => ControllerMethods): { (fastify: FastifyInstance): ControllerMethods; inject(d: Partial<T>): (fastify: FastifyInstance) => ControllerMethods }
export function defineController<T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => ControllerMethods): Injectable<T, [FastifyInstance], ControllerMethods>
export function defineController<T extends Record<string, any>>(methods: (fastify: FastifyInstance) => ControllerMethods | T, cb?: (deps: T, fastify: FastifyInstance) => ControllerMethods) {
return cb && typeof methods !== 'function' ? depend(methods, cb) : methods
}
6 changes: 3 additions & 3 deletions servers/all/api/texts/_label@string/$relay.ts
Expand Up @@ -4,7 +4,7 @@ import type { Schema } from 'fast-json-stringify'
import type { HttpStatusOk } from 'aspida'
import type { ServerMethods } from '../../../$server'
import type { Methods } from './'
import { depend } from 'velona'
import { Injectable, depend } from 'velona'

type Hooks = {
onRequest?: onRequestHookHandler | onRequestHookHandler[]
Expand All @@ -23,13 +23,13 @@ export function defineResponseSchema<T extends { [U in keyof ControllerMethods]?
}

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): { (fastify: FastifyInstance): U; inject(d: Partial<T>): (fastify: FastifyInstance) => U }
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) {
return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks
}

export function defineController(methods: (fastify: FastifyInstance) => ControllerMethods): (fastify: FastifyInstance) => ControllerMethods
export function defineController<T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => ControllerMethods): { (fastify: FastifyInstance): ControllerMethods; inject(d: Partial<T>): (fastify: FastifyInstance) => ControllerMethods }
export function defineController<T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => ControllerMethods): Injectable<T, [FastifyInstance], ControllerMethods>
export function defineController<T extends Record<string, any>>(methods: (fastify: FastifyInstance) => ControllerMethods | T, cb?: (deps: T, fastify: FastifyInstance) => ControllerMethods) {
return cb && typeof methods !== 'function' ? depend(methods, cb) : methods
}
6 changes: 3 additions & 3 deletions servers/all/api/texts/sample/$relay.ts
Expand Up @@ -4,7 +4,7 @@ import type { Schema } from 'fast-json-stringify'
import type { HttpStatusOk } from 'aspida'
import type { ServerMethods } from '../../../$server'
import type { Methods } from './'
import { depend } from 'velona'
import { Injectable, depend } from 'velona'

type Hooks = {
onRequest?: onRequestHookHandler | onRequestHookHandler[]
Expand All @@ -19,13 +19,13 @@ export function defineResponseSchema<T extends { [U in keyof ControllerMethods]?
}

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): { (fastify: FastifyInstance): U; inject(d: Partial<T>): (fastify: FastifyInstance) => U }
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) {
return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks
}

export function defineController(methods: (fastify: FastifyInstance) => ControllerMethods): (fastify: FastifyInstance) => ControllerMethods
export function defineController<T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => ControllerMethods): { (fastify: FastifyInstance): ControllerMethods; inject(d: Partial<T>): (fastify: FastifyInstance) => ControllerMethods }
export function defineController<T extends Record<string, any>>(deps: T, cb: (d: T, fastify: FastifyInstance) => ControllerMethods): Injectable<T, [FastifyInstance], ControllerMethods>
export function defineController<T extends Record<string, any>>(methods: (fastify: FastifyInstance) => ControllerMethods | T, cb?: (deps: T, fastify: FastifyInstance) => ControllerMethods) {
return cb && typeof methods !== 'function' ? depend(methods, cb) : methods
}
6 changes: 3 additions & 3 deletions servers/all/api/users/$relay.ts
Expand Up @@ -6,7 +6,7 @@ import type { ServerMethods } from '../../$server'
import type { AdditionalRequest as AdditionalRequest0 } from './hooks'
import type { AdditionalRequest as AdditionalRequest1 } from './controller'
import type { Methods } from './'
import { depend } from 'velona'
import { Injectable, depend } from 'velona'

type AdditionalRequest = AdditionalRequest0 & AdditionalRequest1
type AddedHandler<T> = T extends (req: infer U, ...args: infer V) => infer W ? (req: U & Partial<AdditionalRequest>, ...args: V) => W : never
Expand All @@ -23,13 +23,13 @@ export function defineResponseSchema<T extends { [U in keyof ControllerMethods]?
}

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): { (fastify: FastifyInstance): U; inject(d: Partial<T>): (fastify: FastifyInstance) => U }
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) {
return cb && typeof hooks !== 'function' ? depend(hooks, cb) : hooks
}

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

0 comments on commit 2e782fa

Please sign in to comment.