udic 1.2.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.
Files changed (3) hide show
  1. package/index.d.ts +41 -64
  2. package/index.js +1 -1
  3. package/package.json +1 -18
package/index.d.ts CHANGED
@@ -1,76 +1,53 @@
1
- declare const _t: unique symbol;
2
- declare const _d: unique symbol;
3
- type UnionToIntersect<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
4
- type UnwrapReturn<T> = T extends () => infer R ? R : T;
5
- type Evaluate<T> = { [K in keyof T] : T[K] } & {};
6
- type List<T> = [T, ...T[]];
7
- export type Dependency = (() => Service) | Compute;
8
- export type TDependency<T extends Dependency | Impl> = UnionToIntersect<UnwrapReturn<T>[typeof _d]>;
9
- export type TResult<T extends Dependency | Impl> = UnwrapReturn<T>[typeof _t];
10
- export interface Service<
11
- K extends string = any,
12
- T = any
13
- > {
14
- [_t]: T;
15
- [_d]: undefined extends T ? { [k in K]? : T } : { [k in K] : T };
16
- }
17
- export interface Compute<
18
- T = any,
19
- R = any
20
- > {
21
- [_t]: R;
22
- [_d]: T;
23
- (c: T): R;
24
- }
1
+ type ObjectUnionToIntersect<T> = { [K in T as K & string] : K }[any];
2
+ export type ContextArgs = {
3
+ [key: string | symbol]: any
4
+ } | ((c: {}, ...args: any[]) => any) | Impl;
5
+ export type Context<Deps extends ContextArgs> = ObjectUnionToIntersect<Deps extends Impl ? Awaited<Deps["infer"]> : Deps extends (c: infer C, ...args: any[]) => any ? C : Deps>;
25
6
  export interface Impl<
26
- K extends string = any,
27
- D = any,
28
- R = any
7
+ In = any,
8
+ Out extends {} = {}
29
9
  > {
30
- [_t]: { [k in K] : R };
31
- [_d]: D;
10
+ infer: Out;
11
+ (c: In): Out;
12
+ }
13
+ export interface ImplFn {
14
+ <const F extends () => {}>(fn: F): F extends (() => infer Out extends {}) ? Impl<{}, Out> : never;
15
+ <const F extends (c: any) => {}>(fn: F): F extends ((c: infer In) => infer Out extends {}) ? Impl<In, Out> : never;
16
+ <const Out extends ContextArgs>(fn: (c: {}) => Context<Out>): Impl<{}, Context<Out>>;
17
+ <
18
+ const In extends ContextArgs,
19
+ const Out extends ContextArgs
20
+ >(fn: (c: Context<In>) => Context<Out>): Impl<Context<In>, Context<Out>>;
32
21
  }
33
22
  /**
34
- * Create a service
35
- * @param name - The service name
36
- */
37
- export declare const service: <const T extends string>(name: T) => (<K>() => Service<T, K>);
38
- /**
39
- * Create a compute that relies on other services or computes
40
- * @param deps - The service dependencies
41
- * @param f
23
+ * Create a sync implementation of a dependency.
42
24
  */
43
- export declare const use: <
44
- const T extends List<Dependency>,
45
- R
46
- >(deps: T, f: (...args: { [K in keyof T] : TResult<T[K]> }) => R) => Compute<TDependency<T[number]>, R>;
25
+ export declare const impl: ImplFn;
26
+ export interface ImplAsyncFn {
27
+ <const F extends () => Promise<{}>>(fn: F): F extends (() => infer Out extends {}) ? Impl<{}, Out> : never;
28
+ <const F extends (c: any) => Promise<{}>>(fn: F): F extends ((c: infer In) => infer Out extends {}) ? Impl<In, Out> : never;
29
+ <const Out extends ContextArgs>(fn: (c: {}) => Promise<Context<Out>>): Impl<{}, Promise<Context<Out>>>;
30
+ <
31
+ const In extends ContextArgs,
32
+ const Out extends ContextArgs
33
+ >(fn: (c: Context<In>) => Promise<Context<Out>>): Impl<Context<In>, Promise<Context<Out>>>;
34
+ }
47
35
  /**
48
- * Inject dependencies to the compute
49
- * @param compute
50
- * @param deps - Dependencies to inject
36
+ * Create an async implementation of a dependency.
51
37
  */
52
- export declare const inject: <
53
- T,
54
- R,
55
- D extends Partial<T>
56
- >(compute: Compute<T, R>, d: D) => Compute<Evaluate<Omit<T, keyof D>>, R>;
38
+ export declare const implAsync: ImplAsyncFn;
57
39
  /**
58
- * Create an implementation of the service that depends on other services
59
- * @param service
60
- * @param compute
40
+ * Link sync implementations.
61
41
  */
62
- export declare const impl: <
63
- K extends string,
64
- T,
65
- D
66
- >(service: () => Service<K, T>, compute: Compute<D, T>) => Impl<K, D, T>;
42
+ export declare const link: <
43
+ const In extends {},
44
+ Impls extends [Impl<In, any>, ...Impl<In, any>[]]
45
+ >(c: In, ...impls: Impls) => In & ReturnType<Impls[number]>;
67
46
  /**
68
- * Provide dependencies to implementations
69
- * @param impls
70
- * @param deps
47
+ * Link async implementations concurrently.
71
48
  */
72
- export declare const link: <
73
- const T extends List<Impl>,
74
- D extends TDependency<T[number]>
75
- >(impls: T, deps: D) => Evaluate<D & UnionToIntersect<TResult<T[number]>>>;
49
+ export declare const linkAsync: <
50
+ const In extends {},
51
+ Impls extends [Impl<In, any>, ...Impl<In, any>[]]
52
+ >(c: In, ...impls: Impls) => Promise<In & Awaited<ReturnType<Impls[number]>>>;
76
53
  export {};
package/index.js CHANGED
@@ -1 +1 @@
1
- let _t=Symbol();export let service=e=>e;let _=i=>(i[_t]=Symbol(),i);export let use=(i,o)=>_(a=>o(...i.map(i=>typeof i==`function`?a[i[_t]]??=i(a):a[i])));export let inject=(e,i)=>_(a=>e({...a,...i}));export let impl=(e,i)=>[e,i];export let link=(e,i)=>{i={...i};for(let a=0;a<e.length;a++)i[e[a][0]]=e[a][1](i);return i};
1
+ export let impl=e=>e;export let implAsync=e=>e;export let link=(e,t,...n)=>{e=Object.assign(t(e),e);for(let t=0;t<n.length;t++)Object.assign(e,n[t](e));return e};export let linkAsync=async(e,...t)=>{if(t.length===1)return Object.assign(await t[0](e),e);let n=Array(t.length);for(let r=0;r<t.length;r++)n[r]=t[r](e);return Object.assign(...await Promise.all(n),e)};
package/package.json CHANGED
@@ -1,18 +1 @@
1
- {
2
- "name": "udic",
3
- "version": "1.2.2",
4
- "description": "A simple dependency injection library",
5
- "keywords": ["di"],
6
- "license": "MIT",
7
- "homepage": "https://re-utils.pages.dev/di",
8
- "repository": {
9
- "type": "git",
10
- "url": "git+https://github.com/re-utils/di.git"
11
- },
12
- "type": "module",
13
- "main": "./index.js",
14
- "types": "./index.d.ts",
15
- "exports": {
16
- ".": "./index.js"
17
- }
18
- }
1
+ {"name":"udic","version":"2.0.0","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"}}