overridable-fn 1.0.2 → 2.0.0
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 +56 -2
- package/dist/index.cjs +6 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ Create functions whose behavior can be swapped out dynamically.
|
|
|
4
4
|
|
|
5
5
|
Intended to be a lightweight less-intrusive alternative to IoC containers.
|
|
6
6
|
|
|
7
|
-
Let's say you have this function:
|
|
7
|
+
Let's say you have this function:
|
|
8
8
|
|
|
9
9
|
```ts
|
|
10
10
|
const getUserById = async (id: string) => {
|
|
@@ -12,7 +12,7 @@ const getUserById = async (id: string) => {
|
|
|
12
12
|
}
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
Now if you just wrap this function with fn:
|
|
15
|
+
Now if you just wrap this function with fn:
|
|
16
16
|
|
|
17
17
|
```ts
|
|
18
18
|
import { fn } from "overridable-fn"
|
|
@@ -39,7 +39,61 @@ getUserById.override((prevImpl) => async (id: string) => {
|
|
|
39
39
|
|
|
40
40
|
So if you call `getUserById()` now, your overriden implementation be invoked instead.
|
|
41
41
|
|
|
42
|
+
**Note:** fn returns a wrapped function - it does not modify the function passed to it. So after wrapping you must always invoke the wrapper returned by fn.
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
const getUserByIdImpl = async (id: string) => { /* Get user from database */ }
|
|
46
|
+
const getUserById = fn(getUserByIdImpl) // getUserById is a wrapper which invokes getUserByIdImpl
|
|
47
|
+
getUserById.override(() => async (id: string) => { /* Return fake user */ })
|
|
48
|
+
getUserById() // Returns fake user
|
|
49
|
+
getUserByIdImpl() // Returns user from database, because wrapper is not used
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
You can access the wrapped function by using `unwrap`. Calling `unwrap` does not change the wrapper anyhow.
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
const getUserByIdImpl = async (id: string) => { /* Get user from database */ }
|
|
56
|
+
const getUserById = fn(getUserByIdImpl) // getUserById is a wrapper which invokes getUserByIdImpl
|
|
57
|
+
getUserById.unwrap() === getUserByIdImpl // true
|
|
58
|
+
getUserById.override(() => async (id: string) => { /* Return fake user */ })
|
|
59
|
+
getUserById.unwrap() === getUserByIdImpl // false
|
|
60
|
+
```
|
|
61
|
+
|
|
42
62
|
```ts
|
|
43
63
|
$ getUserById(10)
|
|
44
64
|
[WARN] Returning fake user
|
|
45
65
|
```
|
|
66
|
+
|
|
67
|
+
The handle returned from override has a restore function that can be used to restore the previous implementation.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
describe('Your feature', () => {
|
|
71
|
+
let restore;
|
|
72
|
+
|
|
73
|
+
before(() => {
|
|
74
|
+
({ restore } = getUserById.override((prevImpl) => async () => {
|
|
75
|
+
// ... mock implementation
|
|
76
|
+
}))
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
after(() => {
|
|
80
|
+
restore?.()
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test("use overriden impl", async () => {
|
|
84
|
+
const fakeUser = await getUserById(10)
|
|
85
|
+
})
|
|
86
|
+
})
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Please note that the handle returned from the override is able to restore only to the implementation before the override.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
const wrapper = fn(() => 1)
|
|
93
|
+
wrapper.override(() => () => 2)
|
|
94
|
+
const { restore } = wrapper.override(() => () => 3)
|
|
95
|
+
restore()
|
|
96
|
+
wrapper() // 2
|
|
97
|
+
restore() // Repeated calls to restore have no further effect
|
|
98
|
+
wrapper() // 2
|
|
99
|
+
```
|
package/dist/index.cjs
CHANGED
|
@@ -27,8 +27,12 @@ var fn = (defaultImpl) => {
|
|
|
27
27
|
let impl = defaultImpl;
|
|
28
28
|
const unwrap = () => impl;
|
|
29
29
|
const override = (provideImpl) => {
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
const curImpl = impl;
|
|
31
|
+
impl = provideImpl(curImpl);
|
|
32
|
+
const restore = () => {
|
|
33
|
+
impl = curImpl;
|
|
34
|
+
};
|
|
35
|
+
return { restore };
|
|
32
36
|
};
|
|
33
37
|
const invoker = (...args) => {
|
|
34
38
|
return impl(...args);
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["interface FnImpl<TArgs extends any[], TRet> {\n (...args: TArgs): TRet;\n}\n\ninterface WrappedFnImpl<TArgs extends any[], TRet> {\n (...args: TArgs): TRet;\n unwrap: () => FnImpl<TArgs, TRet>;\n override: (\n provideImpl: (curImpl: FnImpl<TArgs, TRet>) => FnImpl<TArgs, TRet>\n ) =>
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["interface FnImpl<TArgs extends any[], TRet> {\n (...args: TArgs): TRet;\n}\n\ninterface WrappedFnImpl<TArgs extends any[], TRet> {\n (...args: TArgs): TRet;\n unwrap: () => FnImpl<TArgs, TRet>;\n override: (\n provideImpl: (curImpl: FnImpl<TArgs, TRet>) => FnImpl<TArgs, TRet>\n ) => {\n restore: () => void\n }\n}\n\nexport const fn = <TArgs extends any[], TRet>(\n defaultImpl: FnImpl<TArgs, TRet>\n): WrappedFnImpl<TArgs, TRet> => {\n let impl = defaultImpl;\n\n const unwrap = () => impl;\n\n const override = (\n provideImpl: (curImpl: FnImpl<TArgs, TRet>) => FnImpl<TArgs, TRet>\n ) => {\n const curImpl = impl\n impl = provideImpl(curImpl);\n const restore = () => {\n impl = curImpl\n }\n return { restore }\n };\n\n const invoker: WrappedFnImpl<TArgs, TRet> = (...args) => {\n return impl(...args);\n };\n\n invoker.unwrap = unwrap;\n invoker.override = override;\n\n return invoker;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAcO,IAAM,KAAK,CACd,gBAC6B;AAC7B,MAAI,OAAO;AAEX,QAAM,SAAS,MAAM;AAErB,QAAM,WAAW,CACb,gBACC;AACD,UAAM,UAAU;AAChB,WAAO,YAAY,OAAO;AAC1B,UAAM,UAAU,MAAM;AAClB,aAAO;AAAA,IACX;AACA,WAAO,EAAE,QAAQ;AAAA,EACrB;AAEA,QAAM,UAAsC,IAAI,SAAS;AACrD,WAAO,KAAK,GAAG,IAAI;AAAA,EACvB;AAEA,UAAQ,SAAS;AACjB,UAAQ,WAAW;AAEnB,SAAO;AACX;","names":[]}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,9 @@ interface FnImpl<TArgs extends any[], TRet> {
|
|
|
4
4
|
interface WrappedFnImpl<TArgs extends any[], TRet> {
|
|
5
5
|
(...args: TArgs): TRet;
|
|
6
6
|
unwrap: () => FnImpl<TArgs, TRet>;
|
|
7
|
-
override: (provideImpl: (curImpl: FnImpl<TArgs, TRet>) => FnImpl<TArgs, TRet>) =>
|
|
7
|
+
override: (provideImpl: (curImpl: FnImpl<TArgs, TRet>) => FnImpl<TArgs, TRet>) => {
|
|
8
|
+
restore: () => void;
|
|
9
|
+
};
|
|
8
10
|
}
|
|
9
11
|
declare const fn: <TArgs extends any[], TRet>(defaultImpl: FnImpl<TArgs, TRet>) => WrappedFnImpl<TArgs, TRet>;
|
|
10
12
|
|
package/dist/index.js
CHANGED
|
@@ -3,8 +3,12 @@ var fn = (defaultImpl) => {
|
|
|
3
3
|
let impl = defaultImpl;
|
|
4
4
|
const unwrap = () => impl;
|
|
5
5
|
const override = (provideImpl) => {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
const curImpl = impl;
|
|
7
|
+
impl = provideImpl(curImpl);
|
|
8
|
+
const restore = () => {
|
|
9
|
+
impl = curImpl;
|
|
10
|
+
};
|
|
11
|
+
return { restore };
|
|
8
12
|
};
|
|
9
13
|
const invoker = (...args) => {
|
|
10
14
|
return impl(...args);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["interface FnImpl<TArgs extends any[], TRet> {\n (...args: TArgs): TRet;\n}\n\ninterface WrappedFnImpl<TArgs extends any[], TRet> {\n (...args: TArgs): TRet;\n unwrap: () => FnImpl<TArgs, TRet>;\n override: (\n provideImpl: (curImpl: FnImpl<TArgs, TRet>) => FnImpl<TArgs, TRet>\n ) =>
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["interface FnImpl<TArgs extends any[], TRet> {\n (...args: TArgs): TRet;\n}\n\ninterface WrappedFnImpl<TArgs extends any[], TRet> {\n (...args: TArgs): TRet;\n unwrap: () => FnImpl<TArgs, TRet>;\n override: (\n provideImpl: (curImpl: FnImpl<TArgs, TRet>) => FnImpl<TArgs, TRet>\n ) => {\n restore: () => void\n }\n}\n\nexport const fn = <TArgs extends any[], TRet>(\n defaultImpl: FnImpl<TArgs, TRet>\n): WrappedFnImpl<TArgs, TRet> => {\n let impl = defaultImpl;\n\n const unwrap = () => impl;\n\n const override = (\n provideImpl: (curImpl: FnImpl<TArgs, TRet>) => FnImpl<TArgs, TRet>\n ) => {\n const curImpl = impl\n impl = provideImpl(curImpl);\n const restore = () => {\n impl = curImpl\n }\n return { restore }\n };\n\n const invoker: WrappedFnImpl<TArgs, TRet> = (...args) => {\n return impl(...args);\n };\n\n invoker.unwrap = unwrap;\n invoker.override = override;\n\n return invoker;\n};\n"],"mappings":";AAcO,IAAM,KAAK,CACd,gBAC6B;AAC7B,MAAI,OAAO;AAEX,QAAM,SAAS,MAAM;AAErB,QAAM,WAAW,CACb,gBACC;AACD,UAAM,UAAU;AAChB,WAAO,YAAY,OAAO;AAC1B,UAAM,UAAU,MAAM;AAClB,aAAO;AAAA,IACX;AACA,WAAO,EAAE,QAAQ;AAAA,EACrB;AAEA,QAAM,UAAsC,IAAI,SAAS;AACrD,WAAO,KAAK,GAAG,IAAI;AAAA,EACvB;AAEA,UAAQ,SAAS;AACjB,UAAQ,WAAW;AAEnB,SAAO;AACX;","names":[]}
|
package/package.json
CHANGED