Skip to content

Commit

Permalink
Merge pull request #2490 from feathersjs/rename-legacy-hooks-to-regul…
Browse files Browse the repository at this point in the history
…ar-hooks

chore: rename legacy hooks to regular hooks
  • Loading branch information
marshallswain authored Nov 17, 2021
2 parents 7dfe592 + 713ca78 commit 37d1a30
Show file tree
Hide file tree
Showing 8 changed files with 355 additions and 322 deletions.
519 changes: 276 additions & 243 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions packages/feathers/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import {
HookOptions,
FeathersService,
HookMap,
LegacyHookMap
RegularHookMap
} from './declarations';
import { enableLegacyHooks } from './hooks/legacy';
import { enableRegularHooks } from './hooks/regular';

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

Expand All @@ -31,11 +31,11 @@ export class Feathers<Services, Settings> extends EventEmitter implements Feathe
[HOOKS]: [ (eventHook as any) ]
};

private legacyHooks: (this: any, allHooks: any) => any;
private regularHooks: (this: any, allHooks: any) => any;

constructor () {
super();
this.legacyHooks = enableLegacyHooks(this);
this.regularHooks = enableRegularHooks(this);
}

get<L extends keyof Settings & string> (name: L): Settings[L] {
Expand Down Expand Up @@ -118,10 +118,10 @@ export class Feathers<Services, Settings> extends EventEmitter implements Feathe
}

hooks (hookMap: HookOptions<this, any>) {
const legacyMap = hookMap as LegacyHookMap<this, any>;
const regularMap = hookMap as RegularHookMap<this, any>;

if (legacyMap.before || legacyMap.after || legacyMap.error) {
return this.legacyHooks(legacyMap);
if (regularMap.before || regularMap.after || regularMap.error) {
return this.regularHooks(regularMap);
}

if (Array.isArray(hookMap)) {
Expand All @@ -146,10 +146,10 @@ export class Feathers<Services, Settings> extends EventEmitter implements Feathe
for (const path of Object.keys(this.services)) {
promise = promise.then(() => {
const service: any = this.service(path as any);

if (typeof service.setup === 'function') {
debug(`Setting up service for \`${path}\``);

return service.setup(this, path);
}
});
Expand Down
26 changes: 13 additions & 13 deletions packages/feathers/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,23 +318,23 @@ export interface HookContext<A = Application, S = any> extends BaseHookContext<S
event: string|null;
}

// Legacy hook typings
export type LegacyHookFunction<A = Application, S = Service> =
// Regular hook typings
export type RegularHookFunction<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> = LegacyHookFunction<A, S>;
export type Hook<A = Application, S = Service> = RegularHookFunction<A, S>;

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

type LegacyHookTypeMap<A, S> =
SelfOrArray<LegacyHookFunction<A, S>> | LegacyHookMethodMap<A, S>;
type RegularHookTypeMap<A, S> =
SelfOrArray<RegularHookFunction<A, S>> | RegularHookMethodMap<A, S>;

export type LegacyHookMap<A, S> = {
before?: LegacyHookTypeMap<A, S>,
after?: LegacyHookTypeMap<A, S>,
error?: LegacyHookTypeMap<A, S>
export type RegularHookMap<A, S> = {
before?: RegularHookTypeMap<A, S>,
after?: RegularHookTypeMap<A, S>,
error?: RegularHookTypeMap<A, S>
}

// New @feathersjs/hook typings
Expand All @@ -346,4 +346,4 @@ export type HookMap<A, S> = {
};

export type HookOptions<A, S> =
HookMap<A, S> | HookFunction<A, S>[] | LegacyHookMap<A, S>;
HookMap<A, S> | HookFunction<A, S>[] | RegularHookMap<A, S>;
18 changes: 9 additions & 9 deletions packages/feathers/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
} from '../declarations';
import { defaultServiceArguments, getHookMethods } from '../service';
import {
collectLegacyHooks,
enableLegacyHooks
} from './legacy';
collectRegularHooks,
enableRegularHooks
} from './regular';

export {
fromBeforeHook,
Expand All @@ -17,7 +17,7 @@ export {
fromAfterHooks,
fromErrorHook,
fromErrorHooks
} from './legacy';
} from './regular';

export function createContext (service: Service, method: string, data: HookContextData = {}) {
const createContext = (service as any)[method].createContext;
Expand All @@ -38,11 +38,11 @@ export class FeathersHookManager<A> extends HookManager {
collectMiddleware (self: any, args: any[]): Middleware[] {
const app = this.app as any as Application;
const appHooks = app.appHooks[HOOKS].concat(app.appHooks[this.method] || []);
const legacyAppHooks = collectLegacyHooks(this.app, this.method);
const regularAppHooks = collectRegularHooks(this.app, this.method);
const middleware = super.collectMiddleware(self, args);
const legacyHooks = collectLegacyHooks(self, this.method);
const regularHooks = collectRegularHooks(self, this.method);

return [...appHooks, ...legacyAppHooks, ...middleware, ...legacyHooks];
return [...appHooks, ...regularAppHooks, ...middleware, ...regularHooks];
}

initializeContext (self: any, args: any[], context: HookContext) {
Expand Down Expand Up @@ -86,13 +86,13 @@ export function hookMixin<A> (
return res;
}, {} as HookMap);

const handleLegacyHooks = enableLegacyHooks(service, hookMethods);
const handleRegularHooks = enableRegularHooks(service, hookMethods);

hooks(service, serviceMethodHooks);

service.hooks = function (this: any, hookOptions: any) {
if (hookOptions.before || hookOptions.after || hookOptions.error) {
return handleLegacyHooks.call(this, hookOptions);
return handleRegularHooks.call(this, hookOptions);
}

if (Array.isArray(hookOptions)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HookFunction, LegacyHookFunction, LegacyHookMap } from '../declarations';
import { HookFunction, RegularHookFunction, RegularHookMap } from '../declarations';
import { defaultServiceMethods } from '../service';

const runHook = <A, S> (hook: LegacyHookFunction<A, S>, context: any, type?: string) => {
const runHook = <A, S> (hook: RegularHookFunction<A, S>, context: any, type?: string) => {
if (type) context.type = type;
return Promise.resolve(hook.call(context.self, context))
.then((res: any) => {
Expand All @@ -12,19 +12,19 @@ const runHook = <A, S> (hook: LegacyHookFunction<A, S>, context: any, type?: str
});
};

export function fromBeforeHook<A, S> (hook: LegacyHookFunction<A, S>): HookFunction<A, S> {
export function fromBeforeHook<A, S> (hook: RegularHookFunction<A, S>): HookFunction<A, S> {
return (context, next) => {
return runHook(hook, context, 'before').then(next);
};
}

export function fromAfterHook<A, S> (hook: LegacyHookFunction<A, S>): HookFunction<A, S> {
export function fromAfterHook<A, S> (hook: RegularHookFunction<A, S>): HookFunction<A, S> {
return (context, next) => {
return next().then(() => runHook(hook, context, 'after'));
}
}

export function fromErrorHook<A, S> (hook: LegacyHookFunction<A, S>): HookFunction<A, S> {
export function fromErrorHook<A, S> (hook: RegularHookFunction<A, S>): HookFunction<A, S> {
return (context, next) => {
return next().catch((error: any) => {
if (context.error !== error || context.result !== undefined) {
Expand All @@ -42,32 +42,32 @@ export function fromErrorHook<A, S> (hook: LegacyHookFunction<A, S>): HookFuncti
}
}

const RunHooks = <A, S> (hooks: LegacyHookFunction<A, S>[]) => (context: any) => {
const RunHooks = <A, S> (hooks: RegularHookFunction<A, S>[]) => (context: any) => {
return hooks.reduce((promise, hook) => {
return promise.then(() => runHook(hook, context))
}, Promise.resolve(undefined));
};

export function fromBeforeHooks<A, S> (hooks: LegacyHookFunction<A, S>[]) {
export function fromBeforeHooks<A, S> (hooks: RegularHookFunction<A, S>[]) {
return fromBeforeHook(RunHooks(hooks));
}

export function fromAfterHooks<A, S> (hooks: LegacyHookFunction<A, S>[]) {
export function fromAfterHooks<A, S> (hooks: RegularHookFunction<A, S>[]) {
return fromAfterHook(RunHooks(hooks));
}

export function fromErrorHooks<A, S> (hooks: LegacyHookFunction<A, S>[]) {
export function fromErrorHooks<A, S> (hooks: RegularHookFunction<A, S>[]) {
return fromErrorHook(RunHooks(hooks));
}

export function collectLegacyHooks (target: any, method: string) {
export function collectRegularHooks (target: any, method: string) {
return target.__hooks.hooks[method] || [];
}

// Converts different hook registration formats into the
// same internal format
export function convertHookData (input: any) {
const result: { [ method: string ]: LegacyHookFunction[] } = {};
const result: { [ method: string ]: RegularHookFunction[] } = {};

if (Array.isArray(input)) {
result.all = input;
Expand All @@ -83,31 +83,31 @@ export function convertHookData (input: any) {
return result;
}

type LegacyType = 'before' | 'after' | 'error';
type RegularType = 'before' | 'after' | 'error';

type LegacyMap = { [ type in LegacyType ]: ReturnType< typeof convertHookData > };
type RegularMap = { [ type in RegularType ]: ReturnType< typeof convertHookData > };

type LegacyAdapter = HookFunction & { hooks: LegacyHookFunction[] };
type RegularAdapter = HookFunction & { hooks: RegularHookFunction[] };

type LegacyStore = {
before: { [ method: string ]: LegacyAdapter },
after: { [ method: string ]: LegacyAdapter },
error: { [ method: string ]: LegacyAdapter },
type RegularStore = {
before: { [ method: string ]: RegularAdapter },
after: { [ method: string ]: RegularAdapter },
error: { [ method: string ]: RegularAdapter },
hooks: { [ method: string ]: HookFunction[] }
};

const types: LegacyType[] = ['before', 'after', 'error'];
const types: RegularType[] = ['before', 'after', 'error'];

const isType = (value: any): value is LegacyType => types.includes(value);
const isType = (value: any): value is RegularType => types.includes(value);

const wrappers = {
before: fromBeforeHooks,
after: fromAfterHooks,
error: fromErrorHooks,
error: fromErrorHooks
};

const createStore = (methods: string[]) => {
const store: LegacyStore = {
const store: RegularStore = {
before: {},
after: {},
error: {},
Expand All @@ -121,18 +121,18 @@ const createStore = (methods: string[]) => {
return store;
};

const setStore = (object: any, store: LegacyStore) => {
const setStore = (object: any, store: RegularStore) => {
Object.defineProperty(object, '__hooks', {
configurable: true,
value: store,
writable: true
});
};

const getStore = (object: any): LegacyStore => object.__hooks;
const getStore = (object: any): RegularStore => object.__hooks;

const createMap = (input: LegacyHookMap<any, any>, methods: string[]) => {
const map = {} as LegacyMap;
const createMap = (input: RegularHookMap<any, any>, methods: string[]) => {
const map = {} as RegularMap;

Object.keys(input).forEach((type) => {
if (!isType(type)) {
Expand All @@ -153,20 +153,20 @@ const createMap = (input: LegacyHookMap<any, any>, methods: string[]) => {
return map;
};

const createAdapter = (type: LegacyType) => {
const hooks: LegacyHookFunction[] = [];
const createAdapter = (type: RegularType) => {
const hooks: RegularHookFunction[] = [];
const hook = wrappers[type](hooks);
const adapter = Object.assign(hook, { hooks });

return adapter;
};

const updateStore = (store: LegacyStore, map: LegacyMap) => {
const updateStore = (store: RegularStore, map: RegularMap) => {
Object.keys(store.hooks).forEach((method) => {
let adapted = false;

Object.keys(map).forEach((key) => {
const type = key as LegacyType;
const type = key as RegularType;
const allHooks = map[type].all || [];
const methodHooks = map[type][method] || [];

Expand All @@ -188,15 +188,15 @@ const updateStore = (store: LegacyStore, map: LegacyMap) => {
};

// Add `.hooks` functionality to an object
export function enableLegacyHooks (
export function enableRegularHooks (
object: any,
methods: string[] = defaultServiceMethods
) {
const store = createStore(methods);

setStore(object, store);

return function legacyHooks (this: any, input: LegacyHookMap<any, any>) {
return function regularHooks (this: any, input: RegularHookMap<any, any>) {
const store = getStore(this);
const map = createMap(input, methods);

Expand Down
6 changes: 3 additions & 3 deletions packages/feathers/test/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ describe('Feathers application', () => {
app1.service('testing').create({ message: 'Hi' });
});

it('async hooks run before legacy hooks', async () => {
it('async hooks run before regular hooks', async () => {
const app = feathers();

app.use('/dummy', {
Expand All @@ -197,11 +197,11 @@ describe('Feathers application', () => {
ctx.data.order = [ 'async' ];
await next();
}]);

const result = await dummy.create({
message: 'hi'
});

assert.deepStrictEqual(result, {
message: 'hi',
order: ['async', 'before']
Expand Down
Loading

0 comments on commit 37d1a30

Please sign in to comment.