conode 0.1.24 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,8 +6,6 @@
6
6
  [![NPM version][npm-image]][npm-url]
7
7
  [![Downloads][downloads-image]][npm-url]
8
8
  [![Coverage Status][codecov-image]][codecov-url]
9
- [![Maintainability][codeclimate-image]][codeclimate-url]
10
- [![Snyk][snyk-image]][snyk-url]
11
9
 
12
10
  ## Table of Contents
13
11
 
@@ -70,14 +68,14 @@ auth.contextualize(jwt, action);
70
68
 
71
69
  Class approach with decorator
72
70
  ```typescript
73
- import { contextualize, Context } from '../index';
71
+ import { contextualize, Contextable } from '../index';
74
72
 
75
73
  const action = () => {
76
74
  const service = Service.getContext();
77
75
  console.log(service.getSomething());
78
76
  };
79
77
 
80
- class Service extends Context() {
78
+ class Service extends Contextable() {
81
79
  @contextualize
82
80
  async run() {
83
81
  await action();
@@ -104,8 +102,4 @@ Copyright (c) 2022-present Ivan Zakharchanka
104
102
  [github-image]: https://github.com/3axap4eHko/conode/actions/workflows/build.yml/badge.svg
105
103
  [codecov-url]: https://codecov.io/gh/3axap4eHko/conode
106
104
  [codecov-image]: https://codecov.io/gh/3axap4eHko/conode/branch/master/graph/badge.svg?token=JZ8QCGH6PI
107
- [codeclimate-url]: https://codeclimate.com/github/3axap4eHko/conode/maintainability
108
- [codeclimate-image]: https://api.codeclimate.com/v1/badges/0ba20f27f6db2b0fec8c/maintainability
109
- [snyk-url]: https://snyk.io/test/npm/conode/latest
110
- [snyk-image]: https://img.shields.io/snyk/vulnerabilities/github/3axap4eHko/conode.svg?maxAge=43200
111
105
 
package/build/index.cjs CHANGED
@@ -9,8 +9,8 @@ function _export(target, all) {
9
9
  });
10
10
  }
11
11
  _export(exports, {
12
- Context: function() {
13
- return Context;
12
+ Contextable: function() {
13
+ return Contextable;
14
14
  },
15
15
  contextualize: function() {
16
16
  return contextualize;
@@ -23,37 +23,42 @@ const _nodeasync_hooks = require("node:async_hooks");
23
23
  const createContext = ()=>{
24
24
  const storage = new _nodeasync_hooks.AsyncLocalStorage();
25
25
  return {
26
- contextualize (value, callback) {
27
- return storage.run(value, callback);
26
+ contextualize (context, callback) {
27
+ return storage.run({
28
+ context
29
+ }, callback);
28
30
  },
29
31
  getContext () {
30
- return storage.getStore();
32
+ const store = storage.getStore();
33
+ if (!store) {
34
+ throw new Error('Context not initialized');
35
+ }
36
+ return store.context;
31
37
  }
32
38
  };
33
39
  };
34
- function Context(BaseClass = class {
40
+ function Contextable(BaseClass = class {
35
41
  }) {
36
42
  const context = createContext();
37
43
  return class Contextual extends BaseClass {
38
- static get context() {
44
+ static getContext() {
39
45
  return context.getContext();
40
46
  }
41
- async contextualize(callback) {
47
+ constructor(...args){
48
+ super(...args);
49
+ }
50
+ contextualize(callback) {
42
51
  return context.contextualize(this, callback);
43
52
  }
44
53
  };
45
54
  }
46
- function contextualize(target, _, descriptor) {
47
- const property = descriptor.value;
55
+ function contextualize(...args) {
56
+ const [, , descriptor] = args;
57
+ const property = descriptor?.value;
48
58
  if (typeof property === 'function') {
49
- const method = property?.bind(target);
50
- if (method) {
51
- function contextualized(...args) {
52
- return target.contextualize(()=>method.apply(null, args));
53
- }
54
- ;
55
- descriptor.value = contextualized;
56
- }
59
+ descriptor.value = function(...args) {
60
+ return this.contextualize(()=>property.apply(this, args));
61
+ };
57
62
  }
58
63
  }
59
64
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\n\nexport type Callback = (...args: unknown[]) => PromiseLike<unknown> | unknown;\n\nexport const createContext = <T>() => {\n const storage = new AsyncLocalStorage<T>();\n\n return {\n contextualize(value: T, callback: Callback) {\n return storage.run(value, callback);\n },\n getContext() {\n return storage.getStore();\n }\n };\n};\n\ntype Constructor = new (...args: unknown[]) => object;\n\nexport function Context(BaseClass: Constructor = class { }) {\n const context = createContext();\n return class Contextual extends BaseClass {\n static get context() {\n return context.getContext();\n }\n\n async contextualize(callback: Callback) {\n return context.contextualize(this, callback);\n }\n };\n}\n\nexport interface Contextualized {\n contextualize(callback: Callback): Promise<any>;\n}\n\nexport function contextualize<T extends Contextualized, K extends keyof T>(target: T, _: K, descriptor: PropertyDescriptor) {\n const property = descriptor.value;\n if (typeof property === 'function') {\n const method = property?.bind(target);\n if (method) {\n function contextualized<A extends unknown[]>(...args: A) {\n return target.contextualize(() => method.apply(null, args));\n };\n descriptor.value = contextualized as T[K];\n }\n }\n}\n\n"],"names":["Context","contextualize","createContext","storage","AsyncLocalStorage","value","callback","run","getContext","getStore","BaseClass","context","Contextual","target","_","descriptor","property","method","bind","contextualized","args","apply"],"mappings":";;;;;;;;;;;IAmBgBA,OAAO;eAAPA;;IAiBAC,aAAa;eAAbA;;IAhCHC,aAAa;eAAbA;;;iCAJqB;AAI3B,MAAMA,gBAAgB;IAC3B,MAAMC,UAAU,IAAIC,kCAAiB;IAErC,OAAO;QACLH,eAAcI,KAAQ,EAAEC,QAAkB;YACxC,OAAOH,QAAQI,GAAG,CAACF,OAAOC;QAC5B;QACAE;YACE,OAAOL,QAAQM,QAAQ;QACzB;IACF;AACF;AAIO,SAAST,QAAQU,YAAyB;AAAQ,CAAC;IACxD,MAAMC,UAAUT;IAChB,OAAO,MAAMU,mBAAmBF;QAC9B,WAAWC,UAAU;YACnB,OAAOA,QAAQH,UAAU;QAC3B;QAEA,MAAMP,cAAcK,QAAkB,EAAE;YACtC,OAAOK,QAAQV,aAAa,CAAC,IAAI,EAAEK;QACrC;IACF;AACF;AAMO,SAASL,cAA2DY,MAAS,EAAEC,CAAI,EAAEC,UAA8B;IACxH,MAAMC,WAAWD,WAAWV,KAAK;IACjC,IAAI,OAAOW,aAAa,YAAY;QAClC,MAAMC,SAASD,UAAUE,KAAKL;QAC9B,IAAII,QAAQ;YACV,SAASE,eAAoC,GAAGC,IAAO;gBACrD,OAAOP,OAAOZ,aAAa,CAAC,IAAMgB,OAAOI,KAAK,CAAC,MAAMD;YACvD;;YACAL,WAAWV,KAAK,GAAGc;QACrB;IACF;AACF"}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\n\nexport type Fn<T extends unknown[], R> = (...args: T) => R;\n\nexport const createContext = <T>() => {\n const storage = new AsyncLocalStorage<{ context: T }>();\n return {\n contextualize<R>(context: T, callback: Fn<[], R>): R {\n return storage.run({ context }, callback);\n },\n getContext() {\n const store = storage.getStore();\n if (!store) {\n throw new Error('Context not initialized');\n }\n return store.context;\n }\n };\n};\n\ntype Constructor = new (...args: any) => any;\n\nexport interface Contextualized {\n contextualize<R>(callback: Fn<[], R>): Promise<R>;\n}\n\nexport function Contextable(BaseClass: Constructor = class { }) {\n const context = createContext();\n return class Contextual extends BaseClass {\n static getContext<C extends Constructor>(this: C) {\n return context.getContext() as InstanceType<C>;\n }\n constructor(...args: unknown[]) {\n super(...args);\n }\n\n contextualize<R>(callback: Fn<[], R>): R {\n return context.contextualize(this, callback);\n }\n };\n}\n\nexport function contextualize<T extends Contextualized, K extends keyof T>(...args: unknown[]) {\n const [, , descriptor] = args as [T, K, TypedPropertyDescriptor<T[K]>];\n const property = descriptor?.value;\n if (typeof property === 'function') {\n descriptor.value = function(this: Contextualized, ...args: unknown[]) {\n return this.contextualize(() => property.apply(this, args));\n } as T[K];\n }\n}\n\n"],"names":["Contextable","contextualize","createContext","storage","AsyncLocalStorage","context","callback","run","getContext","store","getStore","Error","BaseClass","Contextual","constructor","args","descriptor","property","value","apply"],"mappings":";;;;;;;;;;;IA0BgBA,WAAW;eAAXA;;IAgBAC,aAAa;eAAbA;;IAtCHC,aAAa;eAAbA;;;iCAJqB;AAI3B,MAAMA,gBAAgB;IAC3B,MAAMC,UAAU,IAAIC,kCAAiB;IACrC,OAAO;QACLH,eAAiBI,OAAU,EAAEC,QAAmB;YAC9C,OAAOH,QAAQI,GAAG,CAAC;gBAAEF;YAAQ,GAAGC;QAClC;QACAE;YACE,MAAMC,QAAQN,QAAQO,QAAQ;YAC9B,IAAI,CAACD,OAAO;gBACV,MAAM,IAAIE,MAAM;YAClB;YACA,OAAOF,MAAMJ,OAAO;QACtB;IACF;AACF;AAQO,SAASL,YAAYY,YAAyB;AAAQ,CAAC;IAC5D,MAAMP,UAAUH;IAChB,OAAO,MAAMW,mBAAmBD;QAC9B,OAAOJ,aAA2C;YAChD,OAAOH,QAAQG,UAAU;QAC3B;QACAM,YAAY,GAAGC,IAAe,CAAE;YAC9B,KAAK,IAAIA;QACX;QAEAd,cAAiBK,QAAmB,EAAK;YACvC,OAAOD,QAAQJ,aAAa,CAAC,IAAI,EAAEK;QACrC;IACF;AACF;AAEO,SAASL,cAA2D,GAAGc,IAAe;IAC3F,MAAM,KAAKC,WAAW,GAAGD;IACzB,MAAME,WAAWD,YAAYE;IAC7B,IAAI,OAAOD,aAAa,YAAY;QAClCD,WAAWE,KAAK,GAAG,SAA+B,GAAGH,IAAe;YAClE,OAAO,IAAI,CAACd,aAAa,CAAC,IAAMgB,SAASE,KAAK,CAAC,IAAI,EAAEJ;QACvD;IACF;AACF"}
package/build/index.d.ts CHANGED
@@ -1,17 +1,18 @@
1
- export type Callback = (...args: unknown[]) => PromiseLike<unknown> | unknown;
1
+ export type Fn<T extends unknown[], R> = (...args: T) => R;
2
2
  export declare const createContext: <T>() => {
3
- contextualize(value: T, callback: Callback): unknown;
4
- getContext(): T | undefined;
3
+ contextualize<R>(context: T, callback: Fn<[], R>): R;
4
+ getContext(): T;
5
5
  };
6
- type Constructor = new (...args: unknown[]) => object;
7
- export declare function Context(BaseClass?: Constructor): {
6
+ type Constructor = new (...args: any) => any;
7
+ export interface Contextualized {
8
+ contextualize<R>(callback: Fn<[], R>): Promise<R>;
9
+ }
10
+ export declare function Contextable(BaseClass?: Constructor): {
8
11
  new (...args: unknown[]): {
9
- contextualize(callback: Callback): Promise<unknown>;
12
+ [x: string]: any;
13
+ contextualize<R>(callback: Fn<[], R>): R;
10
14
  };
11
- readonly context: unknown;
15
+ getContext<C extends Constructor>(this: C): InstanceType<C>;
12
16
  };
13
- export interface Contextualized {
14
- contextualize(callback: Callback): Promise<any>;
15
- }
16
- export declare function contextualize<T extends Contextualized, K extends keyof T>(target: T, _: K, descriptor: PropertyDescriptor): void;
17
+ export declare function contextualize<T extends Contextualized, K extends keyof T>(...args: unknown[]): void;
17
18
  export {};
package/build/index.js CHANGED
@@ -2,37 +2,42 @@ import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  export const createContext = ()=>{
3
3
  const storage = new AsyncLocalStorage();
4
4
  return {
5
- contextualize (value, callback) {
6
- return storage.run(value, callback);
5
+ contextualize (context, callback) {
6
+ return storage.run({
7
+ context
8
+ }, callback);
7
9
  },
8
10
  getContext () {
9
- return storage.getStore();
11
+ const store = storage.getStore();
12
+ if (!store) {
13
+ throw new Error('Context not initialized');
14
+ }
15
+ return store.context;
10
16
  }
11
17
  };
12
18
  };
13
- export function Context(BaseClass = class {
19
+ export function Contextable(BaseClass = class {
14
20
  }) {
15
21
  const context = createContext();
16
22
  return class Contextual extends BaseClass {
17
- static get context() {
23
+ static getContext() {
18
24
  return context.getContext();
19
25
  }
20
- async contextualize(callback) {
26
+ constructor(...args){
27
+ super(...args);
28
+ }
29
+ contextualize(callback) {
21
30
  return context.contextualize(this, callback);
22
31
  }
23
32
  };
24
33
  }
25
- export function contextualize(target, _, descriptor) {
26
- const property = descriptor.value;
34
+ export function contextualize(...args) {
35
+ const [, , descriptor] = args;
36
+ const property = descriptor?.value;
27
37
  if (typeof property === 'function') {
28
- const method = property?.bind(target);
29
- if (method) {
30
- function contextualized(...args) {
31
- return target.contextualize(()=>method.apply(null, args));
32
- }
33
- ;
34
- descriptor.value = contextualized;
35
- }
38
+ descriptor.value = function(...args) {
39
+ return this.contextualize(()=>property.apply(this, args));
40
+ };
36
41
  }
37
42
  }
38
43
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\n\nexport type Callback = (...args: unknown[]) => PromiseLike<unknown> | unknown;\n\nexport const createContext = <T>() => {\n const storage = new AsyncLocalStorage<T>();\n\n return {\n contextualize(value: T, callback: Callback) {\n return storage.run(value, callback);\n },\n getContext() {\n return storage.getStore();\n }\n };\n};\n\ntype Constructor = new (...args: unknown[]) => object;\n\nexport function Context(BaseClass: Constructor = class { }) {\n const context = createContext();\n return class Contextual extends BaseClass {\n static get context() {\n return context.getContext();\n }\n\n async contextualize(callback: Callback) {\n return context.contextualize(this, callback);\n }\n };\n}\n\nexport interface Contextualized {\n contextualize(callback: Callback): Promise<any>;\n}\n\nexport function contextualize<T extends Contextualized, K extends keyof T>(target: T, _: K, descriptor: PropertyDescriptor) {\n const property = descriptor.value;\n if (typeof property === 'function') {\n const method = property?.bind(target);\n if (method) {\n function contextualized<A extends unknown[]>(...args: A) {\n return target.contextualize(() => method.apply(null, args));\n };\n descriptor.value = contextualized as T[K];\n }\n }\n}\n\n"],"names":["AsyncLocalStorage","createContext","storage","contextualize","value","callback","run","getContext","getStore","Context","BaseClass","context","Contextual","target","_","descriptor","property","method","bind","contextualized","args","apply"],"mappings":"AAAA,SAASA,iBAAiB,QAAQ,mBAAmB;AAIrD,OAAO,MAAMC,gBAAgB;IAC3B,MAAMC,UAAU,IAAIF;IAEpB,OAAO;QACLG,eAAcC,KAAQ,EAAEC,QAAkB;YACxC,OAAOH,QAAQI,GAAG,CAACF,OAAOC;QAC5B;QACAE;YACE,OAAOL,QAAQM,QAAQ;QACzB;IACF;AACF,EAAE;AAIF,OAAO,SAASC,QAAQC,YAAyB;AAAQ,CAAC;IACxD,MAAMC,UAAUV;IAChB,OAAO,MAAMW,mBAAmBF;QAC9B,WAAWC,UAAU;YACnB,OAAOA,QAAQJ,UAAU;QAC3B;QAEA,MAAMJ,cAAcE,QAAkB,EAAE;YACtC,OAAOM,QAAQR,aAAa,CAAC,IAAI,EAAEE;QACrC;IACF;AACF;AAMA,OAAO,SAASF,cAA2DU,MAAS,EAAEC,CAAI,EAAEC,UAA8B;IACxH,MAAMC,WAAWD,WAAWX,KAAK;IACjC,IAAI,OAAOY,aAAa,YAAY;QAClC,MAAMC,SAASD,UAAUE,KAAKL;QAC9B,IAAII,QAAQ;YACV,SAASE,eAAoC,GAAGC,IAAO;gBACrD,OAAOP,OAAOV,aAAa,CAAC,IAAMc,OAAOI,KAAK,CAAC,MAAMD;YACvD;;YACAL,WAAWX,KAAK,GAAGe;QACrB;IACF;AACF"}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\n\nexport type Fn<T extends unknown[], R> = (...args: T) => R;\n\nexport const createContext = <T>() => {\n const storage = new AsyncLocalStorage<{ context: T }>();\n return {\n contextualize<R>(context: T, callback: Fn<[], R>): R {\n return storage.run({ context }, callback);\n },\n getContext() {\n const store = storage.getStore();\n if (!store) {\n throw new Error('Context not initialized');\n }\n return store.context;\n }\n };\n};\n\ntype Constructor = new (...args: any) => any;\n\nexport interface Contextualized {\n contextualize<R>(callback: Fn<[], R>): Promise<R>;\n}\n\nexport function Contextable(BaseClass: Constructor = class { }) {\n const context = createContext();\n return class Contextual extends BaseClass {\n static getContext<C extends Constructor>(this: C) {\n return context.getContext() as InstanceType<C>;\n }\n constructor(...args: unknown[]) {\n super(...args);\n }\n\n contextualize<R>(callback: Fn<[], R>): R {\n return context.contextualize(this, callback);\n }\n };\n}\n\nexport function contextualize<T extends Contextualized, K extends keyof T>(...args: unknown[]) {\n const [, , descriptor] = args as [T, K, TypedPropertyDescriptor<T[K]>];\n const property = descriptor?.value;\n if (typeof property === 'function') {\n descriptor.value = function(this: Contextualized, ...args: unknown[]) {\n return this.contextualize(() => property.apply(this, args));\n } as T[K];\n }\n}\n\n"],"names":["AsyncLocalStorage","createContext","storage","contextualize","context","callback","run","getContext","store","getStore","Error","Contextable","BaseClass","Contextual","constructor","args","descriptor","property","value","apply"],"mappings":"AAAA,SAASA,iBAAiB,QAAQ,mBAAmB;AAIrD,OAAO,MAAMC,gBAAgB;IAC3B,MAAMC,UAAU,IAAIF;IACpB,OAAO;QACLG,eAAiBC,OAAU,EAAEC,QAAmB;YAC9C,OAAOH,QAAQI,GAAG,CAAC;gBAAEF;YAAQ,GAAGC;QAClC;QACAE;YACE,MAAMC,QAAQN,QAAQO,QAAQ;YAC9B,IAAI,CAACD,OAAO;gBACV,MAAM,IAAIE,MAAM;YAClB;YACA,OAAOF,MAAMJ,OAAO;QACtB;IACF;AACF,EAAE;AAQF,OAAO,SAASO,YAAYC,YAAyB;AAAQ,CAAC;IAC5D,MAAMR,UAAUH;IAChB,OAAO,MAAMY,mBAAmBD;QAC9B,OAAOL,aAA2C;YAChD,OAAOH,QAAQG,UAAU;QAC3B;QACAO,YAAY,GAAGC,IAAe,CAAE;YAC9B,KAAK,IAAIA;QACX;QAEAZ,cAAiBE,QAAmB,EAAK;YACvC,OAAOD,QAAQD,aAAa,CAAC,IAAI,EAAEE;QACrC;IACF;AACF;AAEA,OAAO,SAASF,cAA2D,GAAGY,IAAe;IAC3F,MAAM,KAAKC,WAAW,GAAGD;IACzB,MAAME,WAAWD,YAAYE;IAC7B,IAAI,OAAOD,aAAa,YAAY;QAClCD,WAAWE,KAAK,GAAG,SAA+B,GAAGH,IAAe;YAClE,OAAO,IAAI,CAACZ,aAAa,CAAC,IAAMc,SAASE,KAAK,CAAC,IAAI,EAAEJ;QACvD;IACF;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "conode",
3
- "version": "0.1.24",
3
+ "version": "0.2.1",
4
4
  "description": "0-Deps, simple and fast context library for NodeJS",
5
5
  "type": "module",
6
6
  "main": "build/index.cjs",
@@ -11,6 +11,11 @@
11
11
  ],
12
12
  "module": "build/index.js",
13
13
  "sideEffects": false,
14
+ "scripts": {
15
+ "build": "rm -rf build && inop src build -i __tests__ -i *.tmp.ts && tsc --declaration --emitDeclarationOnly",
16
+ "test": "jest",
17
+ "test:build": "node src/__tests__/index.cjs && node src/__tests__/index.mjs"
18
+ },
14
19
  "repository": {
15
20
  "type": "git",
16
21
  "url": "git+https://github.com/3axap4eHko/conode.git"
@@ -39,8 +44,5 @@
39
44
  "jest": "^29.7.0",
40
45
  "typescript": "^5.8.3"
41
46
  },
42
- "scripts": {
43
- "build": "rm -rf build && inop src build -i __tests__ -i *.tmp.ts && tsc --declaration --emitDeclarationOnly",
44
- "test": "jest"
45
- }
46
- }
47
+ "packageManager": "pnpm@10.11.0"
48
+ }
package/src/index.ts CHANGED
@@ -1,49 +1,52 @@
1
1
  import { AsyncLocalStorage } from 'node:async_hooks';
2
2
 
3
- export type Callback = (...args: unknown[]) => PromiseLike<unknown> | unknown;
3
+ export type Fn<T extends unknown[], R> = (...args: T) => R;
4
4
 
5
5
  export const createContext = <T>() => {
6
- const storage = new AsyncLocalStorage<T>();
7
-
6
+ const storage = new AsyncLocalStorage<{ context: T }>();
8
7
  return {
9
- contextualize(value: T, callback: Callback) {
10
- return storage.run(value, callback);
8
+ contextualize<R>(context: T, callback: Fn<[], R>): R {
9
+ return storage.run({ context }, callback);
11
10
  },
12
11
  getContext() {
13
- return storage.getStore();
12
+ const store = storage.getStore();
13
+ if (!store) {
14
+ throw new Error('Context not initialized');
15
+ }
16
+ return store.context;
14
17
  }
15
18
  };
16
19
  };
17
20
 
18
- type Constructor = new (...args: unknown[]) => object;
21
+ type Constructor = new (...args: any) => any;
19
22
 
20
- export function Context(BaseClass: Constructor = class { }) {
23
+ export interface Contextualized {
24
+ contextualize<R>(callback: Fn<[], R>): Promise<R>;
25
+ }
26
+
27
+ export function Contextable(BaseClass: Constructor = class { }) {
21
28
  const context = createContext();
22
29
  return class Contextual extends BaseClass {
23
- static get context() {
24
- return context.getContext();
30
+ static getContext<C extends Constructor>(this: C) {
31
+ return context.getContext() as InstanceType<C>;
32
+ }
33
+ constructor(...args: unknown[]) {
34
+ super(...args);
25
35
  }
26
36
 
27
- async contextualize(callback: Callback) {
37
+ contextualize<R>(callback: Fn<[], R>): R {
28
38
  return context.contextualize(this, callback);
29
39
  }
30
40
  };
31
41
  }
32
42
 
33
- export interface Contextualized {
34
- contextualize(callback: Callback): Promise<any>;
35
- }
36
-
37
- export function contextualize<T extends Contextualized, K extends keyof T>(target: T, _: K, descriptor: PropertyDescriptor) {
38
- const property = descriptor.value;
43
+ export function contextualize<T extends Contextualized, K extends keyof T>(...args: unknown[]) {
44
+ const [, , descriptor] = args as [T, K, TypedPropertyDescriptor<T[K]>];
45
+ const property = descriptor?.value;
39
46
  if (typeof property === 'function') {
40
- const method = property?.bind(target);
41
- if (method) {
42
- function contextualized<A extends unknown[]>(...args: A) {
43
- return target.contextualize(() => method.apply(null, args));
44
- };
45
- descriptor.value = contextualized as T[K];
46
- }
47
+ descriptor.value = function(this: Contextualized, ...args: unknown[]) {
48
+ return this.contextualize(() => property.apply(this, args));
49
+ } as T[K];
47
50
  }
48
51
  }
49
52