8000 feat(core): Rename async hooks to around hooks, allow usual registration format by daffl · Pull Request #2652 · feathersjs/feathers · GitHub
[go: up one dir, main page]

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

Filter by extension

Filter by extension


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

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

2 changes: 1 addition & 1 deletion packages/feathers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
"dependencies": {
"@feathersjs/commons": "^5.0.0-pre.22",
"@feathersjs/hooks": "^0.7.4",
"@feathersjs/hooks": "^0.7.5",
"events": "^3.3.0"
},
"devDependencies": {
Expand Down
34 changes: 15 additions & 19 deletions packages/feathers/src/application.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import version from './version'
import { EventEmitter } from 'events'
import { stripSlashes, createDebug } from '@feathersjs/commons'
import { HOOKS, hooks, middleware } from '@feathersjs/hooks'
import { hooks, middleware } from '@feathersjs/hooks'
import { eventHook, eventMixin } from './events'
import { hookMixin } from './hooks/index'
import { hookMixin } from './hooks'
import { wrapService, getServiceOptions, protectedMethods } from './service'
import {
FeathersApplication,
Expand All @@ -13,10 +13,9 @@ import {
ServiceInterface,
Application,
FeathersService,
AroundHookMap,
ApplicationHookOptions
} from './declarations'
import { enableRegularHooks } from './hooks/regular'
import { enableHooks } from './hooks'

const debug = createDebug('@feathersjs/feathers')

Expand All @@ -29,15 +28,11 @@ export class Feathers<Services, Settings>
mixins: ServiceMixin<Application<Services, Settings>>[] = [hookMixin, eventMixin]
version: string = version
_isSetup = false
appHooks: AroundHookMap<Application<Services, Settings>, any> = {
[HOOKS]: [eventHook as any]
}

private regularHooks: (this: any, allHooks: any) => any
protected registerHooks: (this: any, allHooks: any) => any

constructor() {
super()
this.regularHooks = enableRegularHooks(this)
hooks(this, {
setup: middleware().params('server').props({
app: this
Expand All @@ -46,6 +41,10 @@ export class Feathers<Services, Settings>
app: this
})
})
this.registerHooks = enableHooks(this)
this.registerHooks({
around: [eventHook]
})
}

get<L extends keyof Settings & string>(name: L): Settings[L] {
Expand Down Expand Up @@ -130,19 +129,16 @@ export class Feathers<Services, Settings>
hooks(hookMap: ApplicationHookOptions<this>) {
const untypedMap = hookMap as any

if (untypedMap.before || untypedMap.after || untypedMap.error) {
this.regularHooks(untypedMap)
if (untypedMap.before || untypedMap.after || untypedMap.error || untypedMap.around) {
// regular hooks for all service methods
this.registerHooks(untypedMap)
} else if (untypedMap.setup || untypedMap.teardown) {
// .setup and .teardown application hooks
hooks(this, untypedMap)
} else if (Array.isArray(hookMap)) {
this.appHooks[HOOKS].push(...(hookMap as any))
} else {
const methodHookMap = hookMap as AroundHookMap<Application<Services, Settings>, any>

Object.keys(methodHookMap).forEach((key) => {
const methodHooks = this.appHooks[key] || []

this.appHooks[key] = methodHooks.concat(methodHookMap[key])
// Other registration formats are just `around` hooks
this.registerHooks({
around: untypedMap
})
}

Expand Down
25 changes: 10 additions & 15 deletions packages/feathers/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,6 @@ export interface FeathersApplication<Services = any, Settings = any> {
*/
_isSetup: boolean

/**
* Contains all registered application level hooks.
*/
appHooks: AroundHookMap<Application<Services, Settings>, any>

/**
* Retrieve an application setting by name
*
Expand Down Expand Up @@ -332,18 +327,18 @@ export interface HookContext<A = Application, S = any> extends BaseHookContext<S
}

// Regular hook typings
export type RegularHookFunction<A = Application, S = Service> = (
export type HookFunction<A = Application, S = Service> = (
this: S,
context: HookContext<A, S>
) => Promise<HookContext<Application, S> | void> | HookContext<Application, S> | void

export type Hook<A = Application, S = Service> = RegularHookFunction<A, S>
export type Hook<A = Application, S = Service> = HookFunction<A, S>

type RegularHookMethodMap<A, S> = {
[L in keyof S]?: SelfOrArray<RegularHookFunction<A, S>>
} & { all?: SelfOrArray<RegularHookFunction<A, S>> }
type HookMethodMap<A, S> = {
[L in keyof S]?: SelfOrArray<HookFunction<A, S>>
} & { all?: SelfOrArray<HookFunction<A, S>> }

type RegularHookTypeMap<A, S> = SelfOrArray<RegularHookFunction<A, S>> | RegularHookMethodMap<A, S>
type HookTypeMap<A, S> = SelfOrArray<HookFunction<A, S>> | HookMethodMap<A, S>

// New @feathersjs/hook typings
export type AroundHookFunction<A = Application, S = Service> = (
Expand All @@ -353,13 +348,13 @@ export type AroundHookFunction<A = Application, S = Service> = (

export type AroundHookMap<A, S> = {
[L in keyof S]?: AroundHookFunction<A, S>[]
}
} & { all?: AroundHookFunction<A, S>[] }

export type HookMap<A, S> = {
around?: AroundHookMap<A, S>
before?: RegularHookTypeMap<A, S>
after?: RegularHookTypeMap<A, S>
error?: RegularHookTypeMap<A, S>
before?: HookTypeMap<A, S>
after?: HookTypeMap<A, S>
error?: HookTypeMap<A, S>
}

export type HookOptions<A, S> = AroundHookMap<A, S> | AroundHookFunction<A, S>[] | HookMap<A, S>
Expand Down
Loading
0