Skip to content

Commit 05bde46

Browse files
authored
feat: Move to TypeScript 5 non-experimental decorators (#109)
1 parent 55b910c commit 05bde46

File tree

3 files changed

+201
-155
lines changed

3 files changed

+201
-155
lines changed

main/hooks/src/hooks.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { AsyncMiddleware, compose } from './compose.ts';
2-
import { convertOptions, HookContext, HookContextData, HookOptions, setManager, setMiddleware } from './base.ts';
2+
import {
3+
convertOptions,
4+
HookContext,
5+
HookContextData,
6+
HookOptions,
7+
setManager,
8+
setMiddleware
9+
} from './base.ts';
310
import { copyFnProperties, copyProperties } from './utils.ts';
411

512
export function getOriginal(fn: any): any {
@@ -23,7 +30,7 @@ export function functionHooks<F>(fn: F, managerOrMiddleware: HookOptions) {
2330
// Assemble the hook chain
2431
const hookChain: AsyncMiddleware[] = [
2532
// Return `ctx.result` or the context
26-
(ctx, next) => next().then(() => returnContext ? ctx : ctx.result),
33+
(ctx, next) => next().then(() => (returnContext ? ctx : ctx.result))
2734
];
2835

2936
// Create the hook chain by calling the `collectMiddleware function
@@ -41,7 +48,7 @@ export function functionHooks<F>(fn: F, managerOrMiddleware: HookOptions) {
4148
ctx.result = result;
4249

4350
return next();
44-
},
51+
}
4552
);
4653
}
4754

@@ -60,7 +67,7 @@ export function functionHooks<F>(fn: F, managerOrMiddleware: HookOptions) {
6067
Context: manager.getContextClass(),
6168
createContext: (data: HookContextData = {}) => {
6269
return new wrapper.Context(data);
63-
},
70+
}
6471
});
6572
}
6673

@@ -90,10 +97,26 @@ export function objectHooks(obj: any, hooks: HookMap | AsyncMiddleware[]) {
9097
}
9198

9299
export const hookDecorator = (managerOrMiddleware?: HookOptions) => {
100+
return (target: any, context: DecoratorContext) => {
101+
const manager = convertOptions(managerOrMiddleware);
102+
103+
if (context.kind === 'class') {
104+
setManager(target.prototype, manager);
105+
return target;
106+
} else if (context.kind === 'method') {
107+
const method = String(context.name);
108+
return functionHooks(target, manager.props({ method }));
109+
}
110+
111+
throw new Error('Can not apply hooks.');
112+
};
113+
};
114+
115+
export const legacyDecorator = (managerOrMiddleware?: HookOptions) => {
93116
const wrapper: any = (
94117
_target: any,
95118
method: string,
96-
descriptor: TypedPropertyDescriptor<any>,
119+
descriptor: TypedPropertyDescriptor<any>
97120
): TypedPropertyDescriptor<any> => {
98121
const manager = convertOptions(managerOrMiddleware);
99122

main/hooks/test/decorator.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ it('hook decorator on method and classes with inheritance', async () => {
1515
])
1616
class TopLevel {}
1717

18-
@hooks([async (ctx, next) => {
19-
ctx.arguments[0] += ' NameFromDummyClass';
18+
@hooks([
19+
async (ctx, next) => {
20+
ctx.arguments[0] += ' NameFromDummyClass';
2021

21-
await next();
22+
await next();
2223

23-
ctx.result += ' ResultFromDummyClass';
24-
}])
24+
ctx.result += ' ResultFromDummyClass';
25+
},
26+
])
2527
class DummyClass extends TopLevel {
2628
@hooks(
2729
middleware([
@@ -63,6 +65,6 @@ it('error cases', () => {
6365
assertThrows(
6466
() => hooks([])({}, 'test', { value: 'not a function' }),
6567
undefined,
66-
`Can not apply hooks. 'test' is not a function`,
68+
`Can not apply hooks.`,
6769
);
6870
});

0 commit comments

Comments
 (0)