@kaokei/di 2.0.9 → 3.0.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.
Files changed (80) hide show
  1. package/README.md +27 -56
  2. package/dist/binding.d.cts +36 -0
  3. package/dist/binding.d.ts +36 -0
  4. package/dist/cachemap.d.cts +21 -0
  5. package/dist/cachemap.d.ts +21 -0
  6. package/dist/constants.d.cts +28 -0
  7. package/dist/constants.d.ts +28 -0
  8. package/dist/container.d.cts +29 -0
  9. package/dist/container.d.ts +29 -0
  10. package/dist/decorator.d.cts +18 -0
  11. package/dist/decorator.d.ts +18 -0
  12. package/dist/errors/BaseError.d.cts +4 -0
  13. package/dist/errors/BaseError.d.ts +4 -0
  14. package/dist/errors/BindingNotFoundError.d.cts +5 -0
  15. package/dist/errors/BindingNotFoundError.d.ts +5 -0
  16. package/dist/errors/BindingNotValidError.d.cts +5 -0
  17. package/dist/errors/BindingNotValidError.d.ts +5 -0
  18. package/dist/errors/CircularDependencyError.d.cts +5 -0
  19. package/dist/errors/CircularDependencyError.d.ts +5 -0
  20. package/dist/errors/DuplicateBindingError.d.cts +5 -0
  21. package/dist/errors/DuplicateBindingError.d.ts +5 -0
  22. package/dist/errors/PostConstructError.d.cts +5 -0
  23. package/dist/errors/PostConstructError.d.ts +5 -0
  24. package/dist/index.cjs +1 -0
  25. package/dist/index.d.cts +4 -0
  26. package/dist/index.d.ts +4 -0
  27. package/dist/index.js +402 -0
  28. package/dist/interfaces.d.cts +47 -0
  29. package/dist/interfaces.d.ts +47 -0
  30. package/dist/lazyinject.d.cts +4 -0
  31. package/dist/lazyinject.d.ts +4 -0
  32. package/dist/token.d.cts +12 -0
  33. package/dist/token.d.ts +12 -0
  34. package/package.json +35 -89
  35. package/dist/index.cjs.js +0 -746
  36. package/dist/index.cjs.js.map +0 -1
  37. package/dist/index.cjs.min.js +0 -11
  38. package/dist/index.cjs.min.js.map +0 -1
  39. package/dist/index.cjs.runtime.js +0 -735
  40. package/dist/index.cjs.runtime.js.map +0 -1
  41. package/dist/index.cjs.runtime.min.js +0 -11
  42. package/dist/index.cjs.runtime.min.js.map +0 -1
  43. package/dist/index.esm.js +0 -724
  44. package/dist/index.esm.js.map +0 -1
  45. package/dist/index.esm.min.js +0 -11
  46. package/dist/index.esm.min.js.map +0 -1
  47. package/dist/index.esm.runtime.js +0 -713
  48. package/dist/index.esm.runtime.js.map +0 -1
  49. package/dist/index.esm.runtime.min.js +0 -11
  50. package/dist/index.esm.runtime.min.js.map +0 -1
  51. package/dist/index.iife.js +0 -749
  52. package/dist/index.iife.js.map +0 -1
  53. package/dist/index.iife.min.js +0 -11
  54. package/dist/index.iife.min.js.map +0 -1
  55. package/dist/src/Injector.d.ts +0 -143
  56. package/dist/src/Injector.d.ts.map +0 -1
  57. package/dist/src/constants.d.ts +0 -17
  58. package/dist/src/constants.d.ts.map +0 -1
  59. package/dist/src/decorator.d.ts +0 -42
  60. package/dist/src/decorator.d.ts.map +0 -1
  61. package/dist/src/errors/CircularDependencyError.d.ts +0 -6
  62. package/dist/src/errors/CircularDependencyError.d.ts.map +0 -1
  63. package/dist/src/errors/ConstructorInjectMissTokenError.d.ts +0 -6
  64. package/dist/src/errors/ConstructorInjectMissTokenError.d.ts.map +0 -1
  65. package/dist/src/errors/InjectFailedError.d.ts +0 -6
  66. package/dist/src/errors/InjectFailedError.d.ts.map +0 -1
  67. package/dist/src/errors/PropertyInjectMissTokenError.d.ts +0 -6
  68. package/dist/src/errors/PropertyInjectMissTokenError.d.ts.map +0 -1
  69. package/dist/src/errors/ProviderNotValidError.d.ts +0 -6
  70. package/dist/src/errors/ProviderNotValidError.d.ts.map +0 -1
  71. package/dist/src/errors/TokenNotFoundError.d.ts +0 -6
  72. package/dist/src/errors/TokenNotFoundError.d.ts.map +0 -1
  73. package/dist/src/errors/index.d.ts +0 -7
  74. package/dist/src/errors/index.d.ts.map +0 -1
  75. package/dist/src/forwardRef.d.ts +0 -7
  76. package/dist/src/forwardRef.d.ts.map +0 -1
  77. package/dist/src/index.d.ts +0 -7
  78. package/dist/src/index.d.ts.map +0 -1
  79. package/dist/src/utils.d.ts +0 -4
  80. package/dist/src/utils.d.ts.map +0 -1
package/dist/index.js ADDED
@@ -0,0 +1,402 @@
1
+ var V = Object.defineProperty;
2
+ var R = (e, t, n) => t in e ? V(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
+ var a = (e, t, n) => R(e, typeof t != "symbol" ? t + "" : t, n);
4
+ const l = {
5
+ // 记录构造函数参数装饰器对应的数据的key
6
+ INJECTED_PARAMS: "injected:params",
7
+ // 记录实例属性装饰器对应的数据的key
8
+ INJECTED_PROPS: "injected:props",
9
+ // Inject装饰器的key
10
+ INJECT: "inject",
11
+ // Self装饰器的key
12
+ SELF: "self",
13
+ // SkipSelf装饰器的key
14
+ SKIP_SELF: "skipSelf",
15
+ // Optional装饰器的key
16
+ OPTIONAL: "optional",
17
+ // PostConstruct装饰器的key
18
+ POST_CONSTRUCT: "postConstruct",
19
+ // PreDestroy装饰器的key
20
+ PRE_DESTROY: "preDestroy"
21
+ }, p = {
22
+ DEFAULT: "default",
23
+ INITING: "initing",
24
+ ACTIVATED: "activated"
25
+ }, d = {
26
+ Invalid: "Invalid",
27
+ Instance: "Instance",
28
+ ConstantValue: "ConstantValue",
29
+ DynamicValue: "DynamicValue"
30
+ }, C = {
31
+ POST_CONSTRUCT: "Cannot apply @PostConstruct decorator multiple times in the same class.",
32
+ PRE_DESTROY: "Cannot apply @PreDestroy decorator multiple times in the same class.",
33
+ MISS_INJECT: "Expected a @Inject decorator to explicitly specify the token.",
34
+ MISS_CONTAINER: "@LazyInject decorator cannot find the corresponding container."
35
+ }, S = Symbol(), v = /* @__PURE__ */ new WeakMap();
36
+ function w(e) {
37
+ return typeof e == "function" && Object.getPrototypeOf(e) !== Function.prototype;
38
+ }
39
+ function A(e, t, n) {
40
+ const s = v.get(n) || {};
41
+ s[e] = t, v.set(n, s);
42
+ }
43
+ function I(e, t) {
44
+ return (v.get(t) || {})[e];
45
+ }
46
+ function y(e, t) {
47
+ const n = I(e, t);
48
+ if (!w(t))
49
+ return n;
50
+ const s = y(
51
+ e,
52
+ Object.getPrototypeOf(t)
53
+ );
54
+ if (s || n)
55
+ return {
56
+ ...s || {},
57
+ ...n || {}
58
+ };
59
+ }
60
+ class J {
61
+ constructor(t) {
62
+ a(this, "_", "");
63
+ a(this, "name");
64
+ this.name = t;
65
+ }
66
+ }
67
+ class _ {
68
+ constructor(t) {
69
+ a(this, "callback");
70
+ this.callback = t;
71
+ }
72
+ resolve() {
73
+ return this.callback();
74
+ }
75
+ }
76
+ function D(e) {
77
+ if (!e)
78
+ throw new Error(C.MISS_INJECT);
79
+ return e instanceof _ ? e.resolve() : e;
80
+ }
81
+ class E extends Error {
82
+ constructor(t, n) {
83
+ super(), this.name = this.constructor.name, this.message = `${t}${n == null ? void 0 : n.name}`;
84
+ }
85
+ }
86
+ class P extends E {
87
+ constructor(t) {
88
+ super("");
89
+ const n = [];
90
+ let s = t;
91
+ for (; s && s.token; )
92
+ n.push(s.token), s = s.parent;
93
+ const i = n.reverse().map((o) => o.name).join(" --> ");
94
+ this.message = `Circular dependency found: ${i}`;
95
+ }
96
+ }
97
+ class B extends E {
98
+ constructor(t) {
99
+ super("Invalid binding: ", t);
100
+ }
101
+ }
102
+ class k extends P {
103
+ constructor(t) {
104
+ super(t), this.name = "CircularDependencyError inside @PostConstruct";
105
+ }
106
+ }
107
+ class j {
108
+ constructor(t, n) {
109
+ a(this, "container");
110
+ a(this, "context");
111
+ a(this, "token");
112
+ a(this, "type", d.Invalid);
113
+ a(this, "status", p.DEFAULT);
114
+ a(this, "classValue");
115
+ a(this, "constantValue");
116
+ a(this, "dynamicValue");
117
+ a(this, "cache");
118
+ a(this, "postConstructResult", S);
119
+ a(this, "onActivationHandler");
120
+ a(this, "onDeactivationHandler");
121
+ this.container = n, this.context = { container: this.container }, this.token = t;
122
+ }
123
+ onActivation(t) {
124
+ this.onActivationHandler = t;
125
+ }
126
+ onDeactivation(t) {
127
+ this.onDeactivationHandler = t;
128
+ }
129
+ activate(t) {
130
+ const n = this.onActivationHandler ? this.onActivationHandler(this.context, t) : t;
131
+ return this.container.activate(n, this.token);
132
+ }
133
+ deactivate() {
134
+ this.onDeactivationHandler && this.onDeactivationHandler(this.cache);
135
+ }
136
+ to(t) {
137
+ return this.type = d.Instance, this.classValue = t, this;
138
+ }
139
+ toSelf() {
140
+ return this.to(this.token);
141
+ }
142
+ toConstantValue(t) {
143
+ return this.type = d.ConstantValue, this.constantValue = t, this;
144
+ }
145
+ toDynamicValue(t) {
146
+ return this.type = d.DynamicValue, this.dynamicValue = t, this;
147
+ }
148
+ toService(t) {
149
+ return this.toDynamicValue(
150
+ (n) => n.container.get(t, { parent: { token: this.token } })
151
+ );
152
+ }
153
+ get(t) {
154
+ if (p.INITING === this.status)
155
+ throw new P(t);
156
+ if (p.ACTIVATED === this.status)
157
+ return this.cache;
158
+ if (d.Instance === this.type)
159
+ return this.resolveInstanceValue(t);
160
+ if (d.ConstantValue === this.type)
161
+ return this.resolveConstantValue();
162
+ if (d.DynamicValue === this.type)
163
+ return this.resolveDynamicValue();
164
+ throw new B(this.token);
165
+ }
166
+ getAwaitBindings(t, n) {
167
+ return n === !0 ? t : Array.isArray(n) ? t.filter((s) => n.includes(s.token)) : typeof n == "function" ? t.filter(n) : [];
168
+ }
169
+ postConstruct(t, n, s) {
170
+ if (d.Instance === this.type) {
171
+ const { key: i, value: o } = y(l.POST_CONSTRUCT, this.token) || {};
172
+ if (i)
173
+ if (o) {
174
+ const r = [...n, ...s].filter(
175
+ (u) => d.Instance === (u == null ? void 0 : u.type)
176
+ ), c = this.getAwaitBindings(r, o);
177
+ for (const u of c)
178
+ if (u && u.postConstructResult === S)
179
+ throw new k({
180
+ token: u.token,
181
+ parent: t
182
+ });
183
+ const h = c.map((u) => u.postConstructResult);
184
+ this.postConstructResult = Promise.all(h).then(
185
+ () => this.execute(i)
186
+ );
187
+ } else
188
+ this.postConstructResult = this.execute(i);
189
+ else
190
+ this.postConstructResult = void 0;
191
+ }
192
+ }
193
+ preDestroy() {
194
+ if (d.Instance === this.type) {
195
+ const { key: t } = y(l.PRE_DESTROY, this.token) || {};
196
+ if (t)
197
+ return this.execute(t);
198
+ }
199
+ this.container = null, this.context = null;
200
+ }
201
+ execute(t) {
202
+ const n = this.cache[t];
203
+ return n == null ? void 0 : n.call(this.cache);
204
+ }
205
+ resolveInstanceValue(t) {
206
+ this.status = p.INITING;
207
+ const n = this.classValue, [s, i] = this.getConstructorParameters(t), o = new n(...s);
208
+ this.cache = this.activate(o), this.status = p.ACTIVATED, b.set(this.cache, this.container);
209
+ const [r, c] = this.getInjectProperties(t);
210
+ return Object.assign(this.cache, r), this.postConstruct(t, i, c), this.cache;
211
+ }
212
+ resolveConstantValue() {
213
+ return this.status = p.INITING, this.cache = this.activate(this.constantValue), this.status = p.ACTIVATED, this.cache;
214
+ }
215
+ resolveDynamicValue() {
216
+ this.status = p.INITING;
217
+ const t = this.dynamicValue.call(this, this.context);
218
+ return this.cache = this.activate(t), this.status = p.ACTIVATED, this.cache;
219
+ }
220
+ getConstructorParameters(t) {
221
+ const n = I(l.INJECTED_PARAMS, this.classValue) || [], s = [], i = [];
222
+ for (let o = 0; o < n.length; o++) {
223
+ const r = n[o], { inject: c, ...h } = r;
224
+ h.parent = t;
225
+ const u = this.container.get(D(c), h);
226
+ s.push(u), i.push(h.binding);
227
+ }
228
+ return [s, i];
229
+ }
230
+ getInjectProperties(t) {
231
+ const n = y(l.INJECTED_PROPS, this.classValue) || {}, s = Object.keys(n), i = /* @__PURE__ */ Object.create(null), o = [];
232
+ for (let r = 0; r < s.length; r++) {
233
+ const c = s[r], h = n[c], { inject: u, ...f } = h;
234
+ f.parent = t;
235
+ const g = this.container.get(D(u), f);
236
+ g === void 0 && h.optional || (i[c] = g), o.push(f.binding);
237
+ }
238
+ return [i, o];
239
+ }
240
+ }
241
+ class M extends E {
242
+ constructor(t) {
243
+ super("No matching binding found for token: ", t);
244
+ }
245
+ }
246
+ class x extends E {
247
+ constructor(t) {
248
+ super("Cannot bind token multiple times: ", t);
249
+ }
250
+ }
251
+ class N {
252
+ constructor() {
253
+ a(this, "parent");
254
+ a(this, "bindings", /* @__PURE__ */ new Map());
255
+ a(this, "onActivationHandler");
256
+ a(this, "onDeactivationHandler");
257
+ }
258
+ bind(t) {
259
+ if (this.bindings.has(t))
260
+ throw new x(t);
261
+ const n = this.buildBinding(t);
262
+ return this.bindings.set(t, n), n;
263
+ }
264
+ unbind(t) {
265
+ if (this.bindings.has(t)) {
266
+ const n = this.getBinding(t);
267
+ this.deactivate(n), n.preDestroy(), this.bindings.delete(t);
268
+ }
269
+ }
270
+ unbindAll() {
271
+ this.bindings.forEach((t) => {
272
+ this.unbind(t.token);
273
+ });
274
+ }
275
+ isCurrentBound(t) {
276
+ return this.bindings.has(t);
277
+ }
278
+ isBound(t) {
279
+ return this.isCurrentBound(t) || !!this.parent && this.parent.isBound(t);
280
+ }
281
+ createChild() {
282
+ const t = new N();
283
+ return t.parent = this, t;
284
+ }
285
+ get(t, n = {}) {
286
+ const s = this.getBinding(t);
287
+ if (n.skipSelf) {
288
+ if (this.parent)
289
+ return n.skipSelf = !1, this.parent.get(t, n);
290
+ } else if (n.self || s) {
291
+ if (s)
292
+ return n.token = t, n.binding = s, s.get(n);
293
+ } else if (this.parent)
294
+ return this.parent.get(t, n);
295
+ return this.checkBindingNotFoundError(t, n);
296
+ }
297
+ onActivation(t) {
298
+ this.onActivationHandler = t;
299
+ }
300
+ onDeactivation(t) {
301
+ this.onDeactivationHandler = t;
302
+ }
303
+ activate(t, n) {
304
+ return this.onActivationHandler ? this.onActivationHandler({ container: this }, t, n) : t;
305
+ }
306
+ deactivate(t) {
307
+ this.onDeactivationHandler && this.onDeactivationHandler(t.cache, t.token), t.deactivate();
308
+ }
309
+ buildBinding(t) {
310
+ return new j(t, this);
311
+ }
312
+ getBinding(t) {
313
+ return this.bindings.get(t);
314
+ }
315
+ checkBindingNotFoundError(t, n) {
316
+ if (!n.optional)
317
+ throw new M(t);
318
+ }
319
+ }
320
+ const b = /* @__PURE__ */ new WeakMap();
321
+ function T(e, t) {
322
+ return function(n) {
323
+ return function(s, i, o) {
324
+ const r = typeof o == "number", c = r ? s : s.constructor, h = r ? o : i, u = r ? l.INJECTED_PARAMS : l.INJECTED_PROPS, f = r ? I(u, c) || [] : y(u, c) || {}, g = f[h] || {};
325
+ g[e] = n === void 0 ? t : n, f[h] = g, A(u, f, c);
326
+ };
327
+ };
328
+ }
329
+ function O(e, t) {
330
+ return (n) => (s, i) => {
331
+ if (I(e, s.constructor))
332
+ throw new Error(t);
333
+ A(
334
+ e,
335
+ { key: i, value: n },
336
+ s.constructor
337
+ );
338
+ };
339
+ }
340
+ const U = T(l.INJECT), G = T(l.SELF, !0), Y = T(l.SKIP_SELF, !0), z = T(l.OPTIONAL, !0), K = O(
341
+ l.POST_CONSTRUCT,
342
+ C.POST_CONSTRUCT
343
+ ), $ = O(
344
+ l.PRE_DESTROY,
345
+ C.PRE_DESTROY
346
+ );
347
+ function m(e, t, n, s) {
348
+ for (let i = e.length - 1; i >= 0; i--)
349
+ e[i](t, n, s);
350
+ }
351
+ function W(e, t, n) {
352
+ e = Array.isArray(e) ? e : [e], typeof n == "number" ? m(e, t, void 0, n) : typeof n == "string" && m(e, t.prototype, n);
353
+ }
354
+ function H(e, t, n, s) {
355
+ function i() {
356
+ const r = Symbol.for(t);
357
+ if (!this.hasOwnProperty(r)) {
358
+ const c = s || b.get(this), h = this.constructor;
359
+ if (!c)
360
+ throw new Error(`${C.MISS_CONTAINER} ${h.name}`);
361
+ this[r] = c.get(D(n), {
362
+ parent: { token: h }
363
+ });
364
+ }
365
+ return this[r];
366
+ }
367
+ function o(r) {
368
+ const c = Symbol.for(t);
369
+ this[c] = r;
370
+ }
371
+ Object.defineProperty(e, t, {
372
+ configurable: !0,
373
+ enumerable: !0,
374
+ get: i,
375
+ set: o
376
+ });
377
+ }
378
+ function L(e, t) {
379
+ return function(n, s) {
380
+ H(n, s, e, t);
381
+ };
382
+ }
383
+ function q(e) {
384
+ return function(t) {
385
+ return L(t, e);
386
+ };
387
+ }
388
+ export {
389
+ b as CONTAINER_MAP,
390
+ N as Container,
391
+ U as Inject,
392
+ L as LazyInject,
393
+ _ as LazyToken,
394
+ z as Optional,
395
+ K as PostConstruct,
396
+ $ as PreDestroy,
397
+ G as Self,
398
+ Y as SkipSelf,
399
+ J as Token,
400
+ q as createLazyInject,
401
+ W as decorate
402
+ };
@@ -0,0 +1,47 @@
1
+ import { Token, LazyToken } from './token';
2
+ import { Container } from './container';
3
+ import { Binding } from './binding';
4
+ import { KEYS } from './constants';
5
+ export type Newable<TInstance = unknown, TArgs extends unknown[] = any[]> = new (...args: TArgs) => TInstance;
6
+ export type InjectFunction<R extends (...args: any) => any> = (token: GenericToken) => ReturnType<R>;
7
+ export type CommonToken<T = unknown> = Token<T> | Newable<T>;
8
+ export type GenericToken<T = unknown> = Token<T> | Newable<T> | LazyToken<T>;
9
+ export type LazyTokenCallback<T = unknown> = () => CommonToken<T>;
10
+ export interface Context {
11
+ container: Container;
12
+ }
13
+ export type DynamicValue<T> = (ctx: Context) => T;
14
+ export type RecordObject = Record<string, unknown>;
15
+ export interface Options<T = unknown> {
16
+ inject?: GenericToken<T>;
17
+ optional?: boolean;
18
+ self?: boolean;
19
+ skipSelf?: boolean;
20
+ token?: CommonToken<T>;
21
+ binding?: Binding<T>;
22
+ parent?: Options<any>;
23
+ }
24
+ export type ActivationHandler<T = unknown> = (ctx: Context, input: T, token?: CommonToken<T>) => T;
25
+ export type DeactivationHandler<T = unknown> = (input: T, token?: CommonToken<T>) => void;
26
+ export type PostConstructParam = void | true | CommonToken[] | ((item: Binding, index: number, arr: Binding[]) => boolean);
27
+ export type META_KEY_INJECTED_PARAMS = typeof KEYS.INJECTED_PARAMS;
28
+ export type META_KEY_INJECTED_PROPS = typeof KEYS.INJECTED_PROPS;
29
+ export type META_KEY_POST_CONSTRUCT = typeof KEYS.POST_CONSTRUCT;
30
+ export type META_KEY_PRE_DESTROY = typeof KEYS.PRE_DESTROY;
31
+ export type META_KEYS = META_KEY_INJECTED_PARAMS | META_KEY_INJECTED_PROPS | META_KEY_POST_CONSTRUCT | META_KEY_PRE_DESTROY;
32
+ export type META_VALUE_INJECTED_PARAMS = Options[];
33
+ export type META_VALUE_INJECTED_PROPS = Record<string, Options>;
34
+ export type META_VALUE_POST_CONSTRUCT = KV<PostConstructParam>;
35
+ export type META_VALUE_PRE_DESTROY = KV<void>;
36
+ export type META_VALUES = META_VALUE_INJECTED_PARAMS | META_VALUE_INJECTED_PROPS | META_VALUE_POST_CONSTRUCT | META_VALUE_PRE_DESTROY;
37
+ export interface KV<T> {
38
+ key: string;
39
+ value: T;
40
+ }
41
+ export type ExtractKV<T> = T extends META_KEY_POST_CONSTRUCT ? PostConstructParam : T extends META_KEY_PRE_DESTROY ? void : never;
42
+ export interface CacheMapValue {
43
+ [KEYS.INJECTED_PARAMS]: Options[];
44
+ [KEYS.INJECTED_PROPS]: Record<string, Options>;
45
+ [KEYS.POST_CONSTRUCT]: KV<PostConstructParam>;
46
+ [KEYS.PRE_DESTROY]: KV<void>;
47
+ }
@@ -0,0 +1,47 @@
1
+ import { Token, LazyToken } from './token';
2
+ import { Container } from './container';
3
+ import { Binding } from './binding';
4
+ import { KEYS } from './constants';
5
+ export type Newable<TInstance = unknown, TArgs extends unknown[] = any[]> = new (...args: TArgs) => TInstance;
6
+ export type InjectFunction<R extends (...args: any) => any> = (token: GenericToken) => ReturnType<R>;
7
+ export type CommonToken<T = unknown> = Token<T> | Newable<T>;
8
+ export type GenericToken<T = unknown> = Token<T> | Newable<T> | LazyToken<T>;
9
+ export type LazyTokenCallback<T = unknown> = () => CommonToken<T>;
10
+ export interface Context {
11
+ container: Container;
12
+ }
13
+ export type DynamicValue<T> = (ctx: Context) => T;
14
+ export type RecordObject = Record<string, unknown>;
15
+ export interface Options<T = unknown> {
16
+ inject?: GenericToken<T>;
17
+ optional?: boolean;
18
+ self?: boolean;
19
+ skipSelf?: boolean;
20
+ token?: CommonToken<T>;
21
+ binding?: Binding<T>;
22
+ parent?: Options<any>;
23
+ }
24
+ export type ActivationHandler<T = unknown> = (ctx: Context, input: T, token?: CommonToken<T>) => T;
25
+ export type DeactivationHandler<T = unknown> = (input: T, token?: CommonToken<T>) => void;
26
+ export type PostConstructParam = void | true | CommonToken[] | ((item: Binding, index: number, arr: Binding[]) => boolean);
27
+ export type META_KEY_INJECTED_PARAMS = typeof KEYS.INJECTED_PARAMS;
28
+ export type META_KEY_INJECTED_PROPS = typeof KEYS.INJECTED_PROPS;
29
+ export type META_KEY_POST_CONSTRUCT = typeof KEYS.POST_CONSTRUCT;
30
+ export type META_KEY_PRE_DESTROY = typeof KEYS.PRE_DESTROY;
31
+ export type META_KEYS = META_KEY_INJECTED_PARAMS | META_KEY_INJECTED_PROPS | META_KEY_POST_CONSTRUCT | META_KEY_PRE_DESTROY;
32
+ export type META_VALUE_INJECTED_PARAMS = Options[];
33
+ export type META_VALUE_INJECTED_PROPS = Record<string, Options>;
34
+ export type META_VALUE_POST_CONSTRUCT = KV<PostConstructParam>;
35
+ export type META_VALUE_PRE_DESTROY = KV<void>;
36
+ export type META_VALUES = META_VALUE_INJECTED_PARAMS | META_VALUE_INJECTED_PROPS | META_VALUE_POST_CONSTRUCT | META_VALUE_PRE_DESTROY;
37
+ export interface KV<T> {
38
+ key: string;
39
+ value: T;
40
+ }
41
+ export type ExtractKV<T> = T extends META_KEY_POST_CONSTRUCT ? PostConstructParam : T extends META_KEY_PRE_DESTROY ? void : never;
42
+ export interface CacheMapValue {
43
+ [KEYS.INJECTED_PARAMS]: Options[];
44
+ [KEYS.INJECTED_PROPS]: Record<string, Options>;
45
+ [KEYS.POST_CONSTRUCT]: KV<PostConstructParam>;
46
+ [KEYS.PRE_DESTROY]: KV<void>;
47
+ }
@@ -0,0 +1,4 @@
1
+ import { GenericToken } from './interfaces';
2
+ import { Container } from './container';
3
+ export declare function LazyInject<T>(token: GenericToken<T>, container?: Container): (proto: any, key: string) => void;
4
+ export declare function createLazyInject(container: Container): <T>(token: GenericToken<T>) => (proto: any, key: string) => void;
@@ -0,0 +1,4 @@
1
+ import { GenericToken } from './interfaces';
2
+ import { Container } from './container';
3
+ export declare function LazyInject<T>(token: GenericToken<T>, container?: Container): (proto: any, key: string) => void;
4
+ export declare function createLazyInject(container: Container): <T>(token: GenericToken<T>) => (proto: any, key: string) => void;
@@ -0,0 +1,12 @@
1
+ import { GenericToken, LazyTokenCallback } from './interfaces';
2
+ export declare class Token<T> {
3
+ _: T;
4
+ name: string;
5
+ constructor(name: string);
6
+ }
7
+ export declare class LazyToken<T> {
8
+ private callback;
9
+ constructor(callback: LazyTokenCallback<T>);
10
+ resolve(): import('./interfaces').CommonToken<T>;
11
+ }
12
+ export declare function resolveToken<T>(token?: GenericToken<T>): import('./interfaces').CommonToken<T>;
@@ -0,0 +1,12 @@
1
+ import { GenericToken, LazyTokenCallback } from './interfaces';
2
+ export declare class Token<T> {
3
+ _: T;
4
+ name: string;
5
+ constructor(name: string);
6
+ }
7
+ export declare class LazyToken<T> {
8
+ private callback;
9
+ constructor(callback: LazyTokenCallback<T>);
10
+ resolve(): import('./interfaces').CommonToken<T>;
11
+ }
12
+ export declare function resolveToken<T>(token?: GenericToken<T>): import('./interfaces').CommonToken<T>;
package/package.json CHANGED
@@ -1,38 +1,24 @@
1
1
  {
2
2
  "name": "@kaokei/di",
3
- "version": "2.0.9",
4
- "description": "Tiny di library depends on typescript and reflect-metadata",
3
+ "version": "3.0.1",
5
4
  "type": "module",
6
- "main": "./dist/index.cjs.runtime.min.js",
7
- "module": "./dist/index.esm.runtime.min.js",
8
- "types": "./dist/src/index.d.ts",
9
- "unpkg": "dist/index.iife.min.js",
10
- "jsdelivr": "dist/index.iife.min.js",
5
+ "description": "Tiny di library depends on typescript decorator.",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "unpkg": "./dist/index.cjs",
10
+ "jsdelivr": "./dist/index.cjs",
11
11
  "files": [
12
12
  "dist/**/*"
13
13
  ],
14
14
  "exports": {
15
15
  ".": {
16
- "types": "./dist/src/index.d.ts",
17
- "import": "./dist/index.esm.js",
18
- "browser": "./dist/index.iife.js",
19
- "node": {
20
- "import": {
21
- "production": "./dist/index.esm.runtime.min.js",
22
- "development": "./dist/index.esm.js",
23
- "default": "./dist/index.esm.js"
24
- },
25
- "require": {
26
- "production": "./dist/index.cjs.runtime.min.js",
27
- "development": "./dist/index.cjs.js",
28
- "default": "./dist/index.cjs.js"
29
- }
30
- }
16
+ "import": "./dist/index.js",
17
+ "require": "./dist/index.cjs"
31
18
  },
32
19
  "./dist/*": "./dist/*",
33
20
  "./package.json": "./package.json"
34
21
  },
35
- "browserVariableName": "DI",
36
22
  "sideEffects": false,
37
23
  "homepage": "https://github.com/kaokei/di",
38
24
  "author": "kaokei",
@@ -44,80 +30,40 @@
44
30
  "type": "git",
45
31
  "url": "git+https://github.com/kaokei/di.git"
46
32
  },
47
- "keywords": [],
33
+ "keywords": [
34
+ "IoC",
35
+ "DI",
36
+ "Dependency injection"
37
+ ],
48
38
  "scripts": {
49
- "check": "tsc -p tsconfig.app.json --noEmit",
50
- "rp": "rollup -c rollup.config.cjs",
51
- "unit": "jest --silent",
52
- "test": "echo \"Error: no test specified\" && exit 1",
53
- "lint": "eslint --fix --ext js,ts,jsx,tsx src tests playground",
54
- "build": "npm run check && npm run rp",
39
+ "test": "vitest",
40
+ "coverage": "vitest run --coverage",
41
+ "build": "tsc && vite build",
55
42
  "clean": "rm -rf dist/*",
43
+ "public": "npm publish --access public",
56
44
  "postpublish": "git push origin --follow-tags",
57
45
  "prepublishOnly": "npm run clean && npm run build",
58
46
  "release": "standard-version -r",
59
- "release:first": "standard-version --first-release",
60
- "coverage": "open ./coverage/lcov-report/index.html",
61
- "commit": "git-cz"
47
+ "release:first": "standard-version --first-release"
62
48
  },
63
49
  "devDependencies": {
64
- "@babel/core": "^7.23.3",
65
- "@babel/plugin-transform-runtime": "^7.23.4",
66
- "@babel/preset-env": "^7.23.3",
67
- "@babel/types": "^7.23.4",
68
- "@commitlint/cli": "^18.4.3",
69
- "@commitlint/config-conventional": "^18.4.3",
70
- "@rollup/plugin-babel": "^6.0.4",
71
- "@rollup/plugin-commonjs": "^25.0.7",
72
- "@rollup/plugin-json": "^6.0.1",
73
- "@rollup/plugin-node-resolve": "^15.2.3",
74
- "@rollup/plugin-replace": "^5.0.5",
75
- "@types/jest": "^29.5.10",
76
- "@types/node": "^20.10.0",
77
- "@typescript-eslint/eslint-plugin": "^6.12.0",
78
- "@typescript-eslint/parser": "^6.12.0",
79
- "babel-jest": "^29.7.0",
80
- "commitizen": "^4.3.0",
81
- "cz-conventional-changelog": "^3.3.0",
82
- "eslint": "^8.54.0",
83
- "eslint-config-prettier": "^9.0.0",
84
- "eslint-plugin-prettier": "^5.0.1",
85
- "fs-extra": "^11.1.1",
86
- "git-cz": "^4.9.0",
87
- "jest": "^29.7.0",
88
- "jest-environment-jsdom": "^29.7.0",
89
- "lint-staged": "^15.1.0",
90
- "prettier": "^3.1.0",
50
+ "@types/fs-extra": "^11.0.4",
51
+ "@types/node": "^22.13.4",
52
+ "@vitejs/plugin-vue": "^5.2.1",
53
+ "@vitest/coverage-v8": "^3.0.5",
54
+ "@vue/test-utils": "^2.4.6",
55
+ "fs-extra": "^11.3.0",
56
+ "inversify": "^6.2.2",
57
+ "jsdom": "^25.0.1",
91
58
  "reflect-metadata": "^0.2.2",
92
- "rollup": "^4.5.2",
93
- "rollup-plugin-node-builtins": "^2.1.2",
94
- "rollup-plugin-node-globals": "^1.4.0",
95
- "rollup-plugin-polyfill-node": "^0.13.0",
96
- "@rollup/plugin-terser": "^0.4.4",
97
- "rollup-plugin-typescript2": "^0.36.0",
98
59
  "standard-version": "^9.5.0",
99
- "ts-jest": "^29.1.1",
100
- "tslib": "^2.6.2",
101
- "typescript": "^5.3.2",
102
- "yorkie": "^2.0.0"
103
- },
104
- "gitHooks": {
105
- "pre-commit": "lint-staged",
106
- "commit-msg": "commitlint -e $GIT_PARAMS"
107
- },
108
- "lint-staged": {
109
- "*.{ts,js,tsx,jsx}": [
110
- "eslint --fix"
111
- ]
112
- },
113
- "config": {
114
- "commitizen": {
115
- "path": "node_modules/cz-conventional-changelog"
116
- }
60
+ "typescript": "~5.6.3",
61
+ "vite": "^5.4.14",
62
+ "vite-plugin-dts": "^4.5.0",
63
+ "vitest": "^3.0.5",
64
+ "vue": "^3.5.13",
65
+ "vue-router": "^4.5.0",
66
+ "vue-tsc": "^2.2.2"
117
67
  },
118
- "commitlint": {
119
- "extends": [
120
- "@commitlint/config-conventional"
121
- ]
122
- }
68
+ "packageManager": "pnpm@9.9.0+sha512.60c18acd138bff695d339be6ad13f7e936eea6745660d4cc4a776d5247c540d0edee1a563695c183a66eb917ef88f2b4feb1fc25f32a7adcadc7aaf3438e99c1"
123
69
  }