udic 2.0.5 → 2.0.7

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.
Files changed (3) hide show
  1. package/index.d.ts +25 -17
  2. package/index.js +1 -1
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -2,48 +2,56 @@ type ObjectUnionToIntersect<T> = { [K in T as K & string] : K }[any];
2
2
  export type ContextArgs = {
3
3
  [key: string | symbol]: any
4
4
  } | ((...args: any[]) => any);
5
- export type Context<Deps extends ContextArgs> = ObjectUnionToIntersect<Deps extends ImplTag<infer C> ? C : Deps extends (c: infer C, ...args: any[]) => any ? C : Deps>;
6
- export interface ImplTag<Context> {
7
- "~infer": Context;
5
+ export type Context<Deps extends ContextArgs> = ObjectUnionToIntersect<Deps extends ImplTag<any, any> ? Deps["~infer"] : Deps extends (c: infer C, ...args: any[]) => any ? C : Deps>;
6
+ export interface ImplTag<
7
+ In,
8
+ Out
9
+ > {
10
+ "~in": In;
11
+ "~infer": In & Out;
12
+ }
13
+ export interface SyncImplTag<
14
+ In,
15
+ Out
16
+ > extends ImplTag<In, Out> {
17
+ "~sync": 1;
8
18
  }
9
19
  export interface Impl {
10
- <const F>(fn: F): F & ImplTag<F extends (() => infer Out extends {}) ? Out : F extends ((c: infer In extends {}) => infer Out extends {}) ? In & Out : never>;
11
- <const Out extends ContextArgs>(fn: () => Context<Out>): (() => Context<Out>) & ImplTag<Context<Out>>;
20
+ <const F extends (...args: any[]) => any>(fn: F): F & (F extends (() => infer Out extends {}) ? SyncImplTag<{}, Out> : F extends ((c: infer In extends {}) => infer Out extends {}) ? SyncImplTag<In, Out> : never);
21
+ <const Out extends ContextArgs>(fn: (c: {}) => Context<Out>): ((c: {}) => Context<Out>) & SyncImplTag<{}, Context<Out>>;
12
22
  <
13
23
  const In extends ContextArgs,
14
24
  const Out extends ContextArgs
15
- >(fn: (c: Context<In>) => Context<Out>): ((c: Context<In>) => Context<Out>) & ImplTag<Context<In> & Context<Out>>;
25
+ >(fn: (c: Context<In>) => Context<Out>): ((c: Context<In>) => Context<Out>) & SyncImplTag<Context<In>, Context<Out>>;
16
26
  }
17
27
  /**
18
28
  * Create a sync implementation of a dependency.
19
29
  */
20
30
  export declare const impl: Impl;
21
31
  export interface ImplAsync {
22
- <const F>(fn: F): F & ImplTag<F extends (() => infer Out extends {} | Promise<{}>) ? Awaited<Out> : F extends ((c: infer In extends {}) => infer Out extends {} | Promise<{}>) ? In & Awaited<Out> : never>;
23
- <const Out extends ContextArgs>(fn: () => Context<Out> | Promise<Context<Out>>): (() => Context<Out>) & ImplTag<Context<Out>>;
32
+ <const F extends (...args: any[]) => any>(fn: F): F & (F extends (() => infer Out extends {} | Promise<{}>) ? ImplTag<{}, Awaited<Out>> : F extends ((c: infer In extends {}) => infer Out extends {} | Promise<{}>) ? ImplTag<In, Awaited<Out>> : never);
33
+ <const Out extends ContextArgs>(fn: (c: {}) => Context<Out> | Promise<Context<Out>>): ((c: {}) => Context<Out>) & ImplTag<{}, Context<Out>>;
24
34
  <
25
35
  const In extends ContextArgs,
26
36
  const Out extends ContextArgs
27
- >(fn: (c: Context<In>) => Context<Out> | Promise<Context<Out>>): ((c: Context<In>) => Context<Out> | Promise<Context<Out>>) & ImplTag<Context<In> & Context<Out>>;
37
+ >(fn: (c: Context<In>) => Context<Out> | Promise<Context<Out>>): ((c: Context<In>) => Context<Out> | Promise<Context<Out>>) & ImplTag<Context<In>, Context<Out>>;
28
38
  }
29
39
  /**
30
40
  * Create an async implementation of a dependency.
31
41
  */
32
42
  export declare const implAsync: ImplAsync;
33
- type _ImplFn = (() => {} | Promise<{}>) | ((c: any) => {} | Promise<{}>);
34
- type _ImplFns = [_ImplFn, ..._ImplFn[]];
35
43
  /**
36
44
  * Link sync implementations.
37
45
  */
38
46
  export declare const link: <
39
- const In extends {},
40
- Impls extends _ImplFns
41
- >(c: In, ...impls: Impls) => Context<Impls[number]>;
47
+ Impls extends [SyncImplTag<any, any>, ...SyncImplTag<any, any>[]],
48
+ const In extends ObjectUnionToIntersect<Impls[number]["~in"]>
49
+ >(c: In, ...impls: Impls) => In & Context<Impls[number]>;
42
50
  /**
43
51
  * Link async implementations concurrently.
44
52
  */
45
53
  export declare const linkAsync: <
46
- const In extends {},
47
- Impls extends _ImplFns
48
- >(c: In, ...impls: Impls) => Promise<Context<Impls[number]>>;
54
+ Impls extends [ImplTag<any, any>, ...ImplTag<any, any>[]],
55
+ const In extends ObjectUnionToIntersect<Impls[number]["~in"]>
56
+ >(c: In, ...impls: Impls) => Promise<In & Context<Impls[number]>>;
49
57
  export {};
package/index.js CHANGED
@@ -1 +1 @@
1
- export let impl=e=>e;export let implAsync=e=>e;export let link=(e,...t)=>{for(let n=0;n<t.length;n++)Object.assign(e,t[n](e));return e};export let linkAsync=async(e,...t)=>{if(t.length===1)return Object.assign(e,await t[0](e));let n=Array(t.length);for(let r=0;r<t.length;r++)n[r]=t[r](e);return Object.assign(e,...await Promise.all(n))};
1
+ export let impl=e=>e;export let implAsync=impl;export let link=(e,...t)=>{for(let n=0;n<t.length;n++)Object.assign(e,t[n](e));return e};export let linkAsync=async(e,...t)=>{if(t.length===1)return Object.assign(e,await t[0](e));let n=Array(t.length);for(let r=0;r<t.length;r++)n[r]=t[r](e);return Object.assign(e,...await Promise.all(n))};
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"udic","version":"2.0.5","description":"A simple dependency injection library","keywords":["di"],"license":"MIT","homepage":"https://re-utils.pages.dev/di","repository":{"type":"git","url":"git+https://github.com/re-utils/di.git"},"type":"module","main":"./index.js","types":"./index.d.ts","exports":{".":"./index.js"}}
1
+ {"name":"udic","version":"2.0.7","description":"A simple dependency injection library","keywords":["di"],"license":"MIT","homepage":"https://re-utils.pages.dev/di","repository":{"type":"git","url":"git+https://github.com/re-utils/di.git"},"type":"module","main":"./index.js","types":"./index.d.ts","exports":{".":"./index.js"}}