mnemonica 1.3.7 → 1.3.9

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 (57) hide show
  1. package/FOR_HUMANS.md +79 -7
  2. package/build/api/errors/exceptionConstructor.js +3 -3
  3. package/build/api/errors/index.d.ts +3 -7
  4. package/build/api/errors/index.js +3 -5
  5. package/build/api/errors/throwModificationError.js +2 -4
  6. package/build/api/hooks/HookInvocation.js +1 -1
  7. package/build/api/hooks/flowCheckers.d.ts +3 -3
  8. package/build/api/hooks/flowCheckers.js +1 -1
  9. package/build/api/hooks/invokeHook.js +1 -1
  10. package/build/api/index.d.ts +3 -3
  11. package/build/api/types/InstanceCreator.js +6 -7
  12. package/build/api/types/InstanceModificator.js +6 -3
  13. package/build/api/types/TypeProxy.js +2 -2
  14. package/build/api/types/compileNewModificatorFunctionBody.js +1 -1
  15. package/build/api/types/index.d.ts +3 -3
  16. package/build/api/types/index.js +32 -19
  17. package/build/api/utils/index.d.ts +2 -2
  18. package/build/api/utils/index.js +31 -18
  19. package/build/constants/index.js +4 -1
  20. package/build/descriptors/errors/index.js +1 -1
  21. package/build/descriptors/types/index.js +21 -8
  22. package/build/index.d.ts +11 -13
  23. package/build/index.js +17 -18
  24. package/build/types/index.d.ts +116 -23
  25. package/build/types/index.js +1 -1
  26. package/build/utils/collectConstructors.js +1 -1
  27. package/build/utils/fork.d.ts +2 -1
  28. package/build/utils/fork.js +1 -1
  29. package/build/utils/index.js +3 -1
  30. package/build/utils/lineage.d.ts +2 -0
  31. package/build/utils/lineage.js +156 -0
  32. package/build/utils/parse.js +1 -1
  33. package/docs/UTILS.md +41 -2
  34. package/docs/async-constructors.md +6 -0
  35. package/package.json +4 -2
  36. package/src/api/errors/exceptionConstructor.ts +7 -5
  37. package/src/api/errors/index.ts +17 -16
  38. package/src/api/errors/throwModificationError.ts +4 -5
  39. package/src/api/hooks/HookInvocation.ts +2 -1
  40. package/src/api/hooks/flowCheckers.ts +8 -3
  41. package/src/api/hooks/invokeHook.ts +1 -1
  42. package/src/api/types/InstanceCreator.ts +20 -10
  43. package/src/api/types/InstanceModificator.ts +19 -4
  44. package/src/api/types/TypeProxy.ts +11 -2
  45. package/src/api/types/compileNewModificatorFunctionBody.ts +6 -0
  46. package/src/api/types/index.ts +84 -28
  47. package/src/api/utils/index.ts +60 -24
  48. package/src/constants/index.ts +6 -0
  49. package/src/descriptors/errors/index.ts +4 -2
  50. package/src/descriptors/types/index.ts +44 -17
  51. package/src/index.ts +86 -52
  52. package/src/types/index.ts +262 -36
  53. package/src/utils/collectConstructors.ts +0 -9
  54. package/src/utils/fork.ts +2 -1
  55. package/src/utils/index.ts +6 -1
  56. package/src/utils/lineage.ts +220 -0
  57. package/src/utils/parse.ts +2 -1
package/src/index.ts CHANGED
@@ -5,7 +5,6 @@ import type {
5
5
  CreateTypesCollectionFunction,
6
6
  CtorParameter,
7
7
  IDEF,
8
- hook,
9
8
  hooksTypes,
10
9
  constructorOptions,
11
10
  Proto,
@@ -13,6 +12,7 @@ import type {
13
12
  Constructor,
14
13
  DecoratedClass,
15
14
  TypeClass,
15
+ TypeConstructorBase,
16
16
  TypeAbsorber,
17
17
  LazyAbsorber,
18
18
  MnemonicaModule,
@@ -21,7 +21,13 @@ import type {
21
21
  TypeLookup,
22
22
  RegistryHolderBase,
23
23
  RegistryEntry,
24
- LookedUpConstructor
24
+ LookedUpConstructor,
25
+ WithHookRegister,
26
+ typedHook,
27
+ HookableConstructor,
28
+ DefineNewableOrCallable,
29
+ LookupSource,
30
+ LazyDef
25
31
  } from './types';
26
32
 
27
33
  import TypesUtils from './api/utils/index';
@@ -61,6 +67,13 @@ export type {
61
67
  RegistryEntry,
62
68
  LookedUpConstructor,
63
69
  LookedUpInstance,
70
+ typedHook,
71
+ typedHookOpts,
72
+ WithHookRegister,
73
+ HookableConstructor,
74
+ // what a failed construction throws — the catch side of `new T()`
75
+ CreationError,
76
+ ErrorProps,
64
77
  } from './types';
65
78
 
66
79
  /**
@@ -148,8 +161,9 @@ export function define <
148
161
  ): IDefinitorInstance<
149
162
  F,
150
163
  InstanceResult<F>,
151
- Reg & Record<ChildPath, RegistryEntry<F, ChildPath>>,
152
- ChildPath
164
+ Reg & Record<ChildPath, RegistryEntry<F, ChildPath, Parent>>,
165
+ ChildPath,
166
+ Parent
153
167
  >;
154
168
  export function define <
155
169
  T extends object,
@@ -159,9 +173,9 @@ export function define <
159
173
  R extends IDefinitorInstance<N> = IDefinitorInstance<N>,
160
174
  >(
161
175
  this: unknown,
162
- TypeName?: string | CallableFunction | NewableFunction,
176
+ TypeName?: string | DefineNewableOrCallable,
163
177
  // Allow both strict IDEF and more flexible function signatures
164
- constructHandler?: IDEF<T> | CallableFunction | NewableFunction | object | boolean,
178
+ constructHandler?: IDEF<T> | DefineNewableOrCallable | object | boolean,
165
179
  config?: constructorOptions,
166
180
  ): R;
167
181
  export function define <
@@ -171,25 +185,30 @@ export function define <
171
185
  R extends IDefinitorInstance<N> = IDefinitorInstance<N>,
172
186
  >(
173
187
  this: unknown,
174
- TypeNameOrSource?: string | CallableFunction | NewableFunction | RegistryHolderBase<object, object, string>,
175
- constructHandlerOrName?: IDEF<T> | CallableFunction | NewableFunction | object | boolean | string,
176
- configOrHandler?: constructorOptions | IDEF<T> | CallableFunction | NewableFunction | object | boolean,
188
+ TypeNameOrSource?: string | DefineNewableOrCallable | RegistryHolderBase<object, object, string>,
189
+ constructHandlerOrName?: IDEF<T> | DefineNewableOrCallable | object | boolean | string,
190
+ configOrHandler?: constructorOptions | IDEF<T> | DefineNewableOrCallable | object | boolean,
177
191
  config?: constructorOptions,
178
192
  ): R {
179
193
 
180
194
  // explicit-source form: define(source, name, handler, config?)
181
195
  // the source may be a TypesCollection (object) or a TypeProxy (function),
182
196
  // so detection is: has a callable .define AND the next arg is a string name
197
+ const sourceCandidate = TypeNameOrSource as { define?: unknown };
183
198
  const mayBeSource = (
184
199
  (typeof TypeNameOrSource === 'object' && TypeNameOrSource !== null) ||
185
200
  typeof TypeNameOrSource === 'function'
186
- ) && typeof (TypeNameOrSource as { define?: unknown }).define === 'function';
201
+ ) && typeof sourceCandidate.define === 'function';
187
202
  if (mayBeSource && typeof constructHandlerOrName === 'string') {
188
- const source = TypeNameOrSource as unknown as { define: TypeAbsorber };
203
+ // .define was verified callable above; the cast only adds its signature
204
+ const source = sourceCandidate as { define: TypeAbsorber };
189
205
  const sourceDefineResult = source.define(
190
206
  constructHandlerOrName,
191
207
  configOrHandler as IDEF<T>,
192
208
  config
209
+ // facade return: the overload promises R (the caller's constructor
210
+ // view); the delegated define produces that same runtime
211
+ // constructor under a different generic instantiation
193
212
  ) as unknown as R;
194
213
  return sourceDefineResult;
195
214
  }
@@ -201,6 +220,9 @@ export function define <
201
220
  TypeNameOrSource as string,
202
221
  constructHandlerOrName as IDEF<T>,
203
222
  configOrHandler as constructorOptions
223
+ // facade return: the overload promises R (the caller's constructor
224
+ // view); the delegated define produces that same runtime
225
+ // constructor under a different generic instantiation
204
226
  ) as unknown as R;
205
227
  return defineResult;
206
228
  }
@@ -222,36 +244,36 @@ export function lazy<T extends object>(
222
244
  // named form: lazy(TypeName, getter, config?)
223
245
  this: unknown,
224
246
  TypeName: string,
225
- getter: () => IDEF<T>,
247
+ getter: LazyDef<T>,
226
248
  config?: constructorOptions
227
249
  ): IDefinitorInstance<T>;
228
250
  export function lazy<T extends object>(
229
251
  // unnamed form: lazy(getter, config?)
230
252
  this: unknown,
231
- getter: () => IDEF<T>,
253
+ getter: LazyDef<T>,
232
254
  config?: constructorOptions
233
255
  ): IDefinitorInstance<T>;
234
256
  export function lazy(
235
257
  this: unknown,
236
- arg1: unknown,
237
- arg2?: unknown,
238
- arg3?: unknown,
239
- arg4?: unknown
258
+ sourceOrTypeNameOrGetter: unknown,
259
+ TypeNameOrGetterOrConfig?: unknown,
260
+ getterOrConfig?: unknown,
261
+ config?: unknown
240
262
  ): unknown {
241
263
 
242
264
  // explicit-source form: lazy(source, name?, getter, config?)
243
265
  // the source may be a TypesCollection (object) or a TypeProxy (function),
244
266
  // so detection is: has a callable .lazy
245
267
  const mayBeSource = (
246
- (typeof arg1 === 'object' && arg1 !== null) ||
247
- typeof arg1 === 'function'
248
- ) && typeof (arg1 as { lazy?: unknown }).lazy === 'function';
268
+ (typeof sourceOrTypeNameOrGetter === 'object' && sourceOrTypeNameOrGetter !== null) ||
269
+ typeof sourceOrTypeNameOrGetter === 'function'
270
+ ) && typeof (sourceOrTypeNameOrGetter as { lazy?: unknown }).lazy === 'function';
249
271
  if (mayBeSource) {
250
- const source = arg1 as unknown as { lazy: LazyAbsorber };
272
+ const source = sourceOrTypeNameOrGetter as { lazy: LazyAbsorber };
251
273
  const sourceLazyResult = source.lazy(
252
- arg2 as string | (() => IDEF<object>),
253
- arg3 as (() => IDEF<object>) | constructorOptions,
254
- arg4 as constructorOptions
274
+ TypeNameOrGetterOrConfig as string | LazyDef<object>,
275
+ getterOrConfig as LazyDef<object> | constructorOptions,
276
+ config as constructorOptions
255
277
  ) as unknown;
256
278
  return sourceLazyResult;
257
279
  }
@@ -260,9 +282,9 @@ export function lazy(
260
282
  // Type assertion needed because TypesCollectionProxy is a Proxy
261
283
  const lazyResult = (types as { lazy: LazyAbsorber })
262
284
  .lazy(
263
- arg1 as string | (() => IDEF<object>),
264
- arg2 as (() => IDEF<object>) | constructorOptions,
265
- arg3 as constructorOptions
285
+ sourceOrTypeNameOrGetter as string | LazyDef<object>,
286
+ TypeNameOrGetterOrConfig as LazyDef<object> | constructorOptions,
287
+ getterOrConfig as constructorOptions
266
288
  ) as unknown;
267
289
  return lazyResult;
268
290
  }
@@ -270,34 +292,34 @@ export function lazy(
270
292
  export function lookup<const K extends keyof TypeRegistry>(
271
293
  this: unknown,
272
294
  TypeNestedPath: K
273
- ): TypeRegistry[K];
295
+ ): WithHookRegister<TypeRegistry[K]>;
274
296
  export function lookup<Reg extends object, const K extends keyof Reg & string>(
275
297
  // explicit-source form: lookup(source, path)
276
298
  // resolves against the registry carried by a builder/collection value
277
299
  source: { lookup: TypeLookup<Reg> },
278
300
  TypeNestedPath: K
279
- ): LookedUpConstructor<Reg, K>;
301
+ ): WithHookRegister<LookedUpConstructor<Reg, K>>;
280
302
  export function lookup(this: unknown, TypeNestedPath: string): TypeClass | undefined;
281
303
  export function lookup(
282
- source: { lookup: (path: string) => TypeClass | undefined },
304
+ source: LookupSource,
283
305
  TypeNestedPath: string
284
306
  ): TypeClass | undefined;
285
307
  export function lookup(
286
308
  this: unknown,
287
- arg1: unknown,
288
- arg2?: unknown
309
+ sourceOrPath: unknown,
310
+ TypeNestedPath?: unknown
289
311
  ): unknown {
290
312
 
291
313
  // explicit-source form: lookup(source, path)
292
- if (typeof arg1 !== 'string' && typeof arg2 === 'string') {
293
- const source = arg1 as { lookup: (path: string) => TypeClass | undefined };
294
- const sourceResult = source.lookup(arg2);
314
+ if (typeof sourceOrPath !== 'string' && typeof TypeNestedPath === 'string') {
315
+ const source = sourceOrPath as LookupSource;
316
+ const sourceResult = source.lookup(TypeNestedPath);
295
317
  return sourceResult;
296
318
  }
297
319
 
298
320
  const types = checkThis(this) ? defaultTypes : this || defaultTypes;
299
321
  // Type assertion needed because TypesCollectionProxy is a Proxy
300
- const lookupResult = (types as { lookup: (path: string) => TypeClass | undefined }).lookup(arg1 as string);
322
+ const lookupResult = (types as LookupSource).lookup(sourceOrPath as string);
301
323
  return lookupResult;
302
324
  }
303
325
 
@@ -313,14 +335,15 @@ const $run = function <E extends object, T extends object, S extends Proto<E, T>
313
335
  // debugger;
314
336
  // @ts-expect-error - extracting TypeName from function
315
337
  const { TypeName } = Ctor;
338
+ // the returned SubTypeProxy is a plain function at runtime — constructable —
339
+ // so TypeConstructorBase (the named minimal constructor shape) is its
340
+ // honest static type here
316
341
  const Cstr = prepareSubtypeForConstruction(
317
342
  TypeName,
318
343
  entity
319
- ) as unknown as { new(...ars: unknown[]): unknown };
320
- // TODO: check lines below and if Constructor is not mnemonized ...
344
+ ) as TypeConstructorBase;
321
345
  if (Cstr === undefined) {
322
- const ErrorCtor = WRONG_MODIFICATION_PATTERN as unknown as new (msg: string) => Error;
323
- throw new ErrorCtor(`[ ${TypeName} ] is not defined as a Type Constructor on used instance`);
346
+ throw new WRONG_MODIFICATION_PATTERN(`[ ${TypeName} ] is not defined as a Type Constructor on used instance`);
324
347
  }
325
348
  const runResult = new Cstr(...args);
326
349
  // @ts-expect-error - returning result as merged proto type
@@ -338,7 +361,9 @@ export const apply = function <E extends object, T extends object, S extends Pro
338
361
  Ctor,
339
362
  args
340
363
  );
341
- const result = runResult as unknown as InstanceResult<Merge<E, T>>;
364
+ // S extends Proto<E, T>, which is the same merged entity+ctor shape
365
+ // apply/call/bind promise — the named public result type
366
+ const result = runResult as InstanceResult<Merge<E, T>>;
342
367
  return result;
343
368
  };
344
369
 
@@ -353,7 +378,8 @@ export const call = function <E extends object, T extends object, S extends Prot
353
378
  Ctor,
354
379
  args
355
380
  );
356
- const result = runResult as unknown as InstanceResult<Merge<E, T>>;
381
+ // same merge narrowing as apply() above
382
+ const result = runResult as InstanceResult<Merge<E, T>>;
357
383
  return result;
358
384
  };
359
385
 
@@ -368,7 +394,8 @@ export const bind = function <E extends object, T extends object, S extends Prot
368
394
  Ctor,
369
395
  args
370
396
  );
371
- const typedResult = runResult as unknown as InstanceResult<Merge<E, T>>;
397
+ // same merge narrowing as apply() above
398
+ const typedResult = runResult as InstanceResult<Merge<E, T>>;
372
399
  return typedResult;
373
400
  };
374
401
  return result;
@@ -384,8 +411,11 @@ export const decorate = function <
384
411
  ? target as constructorOptions
385
412
  : config;
386
413
 
414
+ // decorate(SomeType, config?) expects a constructor already carrying
415
+ // .define (a previously defined type) — declared on the value so the
416
+ // decorator body below needs no cast
387
417
  const parentType = (target instanceof Function)
388
- ? target as Constructor<object>
418
+ ? target as Constructor<object> & { define: TypeAbsorber }
389
419
  : undefined;
390
420
 
391
421
  const decorator = function <U extends Constructor<object>>(cstr: U): DecoratedClass<U> {
@@ -395,16 +425,17 @@ export const decorate = function <
395
425
  name,
396
426
  cstr as IDEF<object>,
397
427
  opts
428
+ // decorator contract: define() returns the constructor that
429
+ // wraps cstr; the decorator replaces the class binding with
430
+ // it, so the result is presented as DecoratedClass<U>
398
431
  ) as unknown as DecoratedClass<U>;
399
432
  return decoratorResult;
400
433
  }
401
- const parent = parentType as unknown as {
402
- define: TypeAbsorber;
403
- };
404
- const defineResult = parent.define(
434
+ const defineResult = parentType.define(
405
435
  name,
406
436
  cstr as IDEF<object>,
407
437
  opts
438
+ // same decorator contract as the branch above
408
439
  ) as unknown as DecoratedClass<U>;
409
440
  return defineResult;
410
441
  };
@@ -412,10 +443,13 @@ export const decorate = function <
412
443
  };
413
444
 
414
445
 
415
- export const registerHook = function <T extends Constructor<T>>(
416
- Ctor: DecoratedClass<T>,
417
- hookType: hooksTypes,
418
- cb: hook
446
+ export const registerHook = function <
447
+ T extends object,
448
+ HT extends hooksTypes = hooksTypes,
449
+ >(
450
+ Ctor: HookableConstructor<T>,
451
+ hookType: HT,
452
+ cb: typedHook<HT, object, T>
419
453
  ): void {
420
454
  Ctor.registerHook(
421
455
  hookType,