-
Notifications
You must be signed in to change notification settings - Fork 622
/
Copy pathtest.ts
47 lines (40 loc) · 1.25 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { GenericRecord } from "@webiny/cli/types";
const context = {
api: {
createFile(file: string, tags: string[]) {
console.log("Create file", file, tags);
},
updateFile(file: string, enabled: boolean) {
console.log("Update file", file, enabled);
}
}
};
type Decorators<TDecoratee> = {
[K in keyof TDecoratee]?: (decoratee: TDecoratee[K]) => TDecoratee[K];
};
const decorateContext = <T extends GenericRecord<string, CallableFunction>>(
decoratee: T,
decorators: Decorators<T>
) => {
return Object.keys(decorators).reduce((decoratee, key) => {
const decoratedMethod = decoratee[key];
return {
...decoratee,
[key]: (...args: any[]) => {
// @ts-expect-error
return decorators[key](decoratedMethod)(...args);
}
};
}, decoratee);
};
context.api = decorateContext(context.api, {
createFile: decoratee => (file, tags) => {
return decoratee(file, [...tags, "thread-scan:in-progress"]);
}
});
context.api = decorateContext(context.api, {
createFile: decoratee => (file, tags) => {
return decoratee(file, [...tags, "another-tag"]);
}
});
context.api.createFile("file1", []);