@adonisjs/fold
Advanced tools
Comparing version 9.9.3-0 to 9.9.3-1
@@ -1,2 +0,2 @@ | ||
import type { Make, BindingKey, Constructor, HookCallback, BindingResolver, ExtractFunctions, ContainerOptions, AbstractConstructor } from './types.js'; | ||
import type { Make, BindingKey, Constructor, ErrorCreator, HookCallback, BindingResolver, ExtractFunctions, ContainerOptions, AbstractConstructor } from './types.js'; | ||
import { ContainerResolver } from './resolver.js'; | ||
@@ -12,5 +12,5 @@ import { ContextBindingsBuilder } from './contextual_bindings_builder.js'; | ||
hasAllBindings(binding: BindingKey[]): boolean; | ||
make<Binding extends keyof KnownBindings>(binding: Binding, runtimeValues?: any[]): Promise<Binding extends string | symbol ? KnownBindings[Binding] : Make<Binding>>; | ||
make<Binding>(binding: Binding, runtimeValues?: any[]): Promise<Make<Binding>>; | ||
call<Value extends Record<any, any>, Method extends ExtractFunctions<Value>>(value: Value, method: Method, runtimeValues?: any[]): Promise<ReturnType<Value[Method]>>; | ||
make<Binding extends keyof KnownBindings>(binding: Binding, runtimeValues?: any[], createError?: ErrorCreator): Promise<Binding extends string | symbol ? KnownBindings[Binding] : Make<Binding>>; | ||
make<Binding>(binding: Binding, runtimeValues?: any[], createError?: ErrorCreator): Promise<Make<Binding>>; | ||
call<Value extends Record<any, any>, Method extends ExtractFunctions<Value>>(value: Value, method: Method, runtimeValues?: any[], createError?: ErrorCreator): Promise<ReturnType<Value[Method]>>; | ||
alias<Alias extends keyof KnownBindings>(alias: Alias extends string | symbol ? Alias : never, value: AbstractConstructor<KnownBindings[Alias]> | Exclude<{ | ||
@@ -17,0 +17,0 @@ [K in keyof KnownBindings]: KnownBindings[K] extends KnownBindings[Alias] ? K : never; |
@@ -34,7 +34,7 @@ import { inspect } from 'node:util'; | ||
} | ||
make(binding, runtimeValues) { | ||
return this.createResolver().make(binding, runtimeValues); | ||
make(binding, runtimeValues, createError) { | ||
return this.createResolver().make(binding, runtimeValues, createError); | ||
} | ||
call(value, method, runtimeValues) { | ||
return this.createResolver().call(value, method, runtimeValues); | ||
call(value, method, runtimeValues, createError) { | ||
return this.createResolver().call(value, method, runtimeValues, createError); | ||
} | ||
@@ -41,0 +41,0 @@ alias(alias, value) { |
@@ -1,8 +0,18 @@ | ||
import { defineStaticProperty } from '@poppinss/utils'; | ||
import { defineStaticProperty, RuntimeException } from '@poppinss/utils'; | ||
import debug from '../debug.js'; | ||
function initiateContainerInjections(target, method) { | ||
function createDebuggingError(original) { | ||
return function createError(message) { | ||
const error = new RuntimeException(message); | ||
error.stack = original.stack; | ||
return error; | ||
}; | ||
} | ||
function initiateContainerInjections(target, method, createError) { | ||
defineStaticProperty(target, 'containerInjections', { initialValue: {}, strategy: 'inherit' }); | ||
target.containerInjections[method] = []; | ||
target.containerInjections[method] = { | ||
createError, | ||
dependencies: [], | ||
}; | ||
} | ||
function defineConstructorInjections(target) { | ||
function defineConstructorInjections(target, createError) { | ||
const params = Reflect.getMetadata('design:paramtypes', target); | ||
@@ -12,3 +22,3 @@ if (!params) { | ||
} | ||
initiateContainerInjections(target, '_constructor'); | ||
initiateContainerInjections(target, '_constructor', createError); | ||
if (debug.enabled) { | ||
@@ -18,6 +28,6 @@ debug('defining constructor injections for %O, params %O', `[class: ${target.name}]`, params); | ||
for (const param of params) { | ||
target.containerInjections._constructor.push(param); | ||
target.containerInjections._constructor.dependencies.push(param); | ||
} | ||
} | ||
function defineMethodInjections(target, method) { | ||
function defineMethodInjections(target, method, createError) { | ||
const constructor = target.constructor; | ||
@@ -28,3 +38,3 @@ const params = Reflect.getMetadata('design:paramtypes', target, method); | ||
} | ||
initiateContainerInjections(constructor, method); | ||
initiateContainerInjections(constructor, method, createError); | ||
if (debug.enabled) { | ||
@@ -34,14 +44,15 @@ debug('defining method injections for %O, method %O, params %O', `[class ${constructor.name}]`, method, params); | ||
for (const param of params) { | ||
constructor.containerInjections[method].push(param); | ||
constructor.containerInjections[method].dependencies.push(param); | ||
} | ||
} | ||
export function inject() { | ||
const createError = createDebuggingError(new Error()); | ||
function injectDecorator(target, propertyKey) { | ||
if (!propertyKey) { | ||
defineConstructorInjections(target); | ||
defineConstructorInjections(target, createError); | ||
return; | ||
} | ||
defineMethodInjections(target, propertyKey); | ||
defineMethodInjections(target, propertyKey, createError); | ||
} | ||
return injectDecorator; | ||
} |
@@ -7,3 +7,4 @@ import debug from './debug.js'; | ||
} | ||
const injections = binding.containerInjections[property]; | ||
const injections = binding.containerInjections[property].dependencies; | ||
const createError = binding.containerInjections[property].createError; | ||
if (values.length > injections.length) { | ||
@@ -23,3 +24,3 @@ if (debug.enabled) { | ||
const injection = injections[index]; | ||
return resolver.resolveFor(binding, injection); | ||
return resolver.resolveFor(binding, injection, undefined, createError); | ||
})); | ||
@@ -39,4 +40,4 @@ } | ||
} | ||
return resolver.resolveFor(binding, injection); | ||
return resolver.resolveFor(binding, injection, undefined, createError); | ||
})); | ||
} |
@@ -1,2 +0,2 @@ | ||
import type { Make, Hooks, Swaps, Bindings, BindingKey, Constructor, BindingValues, ExtractFunctions, ContainerOptions, ContextualBindings, AbstractConstructor } from './types.js'; | ||
import type { Make, Hooks, Swaps, Bindings, BindingKey, Constructor, ErrorCreator, BindingValues, ExtractFunctions, ContainerOptions, ContextualBindings, AbstractConstructor } from './types.js'; | ||
export declare class ContainerResolver<KnownBindings extends Record<any, any>> { | ||
@@ -16,8 +16,8 @@ #private; | ||
hasAllBindings(bindings: BindingKey[]): boolean; | ||
resolveFor<Binding>(parent: unknown, binding: Binding, runtimeValues?: any[]): Promise<Make<Binding>>; | ||
make<Binding extends keyof KnownBindings>(binding: Binding, runtimeValues?: any[]): Promise<Binding extends string | symbol ? KnownBindings[Binding] : Make<Binding>>; | ||
make<Binding>(binding: Binding, runtimeValues?: any[]): Promise<Make<Binding>>; | ||
call<Value extends Record<any, any>, Method extends ExtractFunctions<Value>>(value: Value, method: Method, runtimeValues?: any[]): Promise<ReturnType<Value[Method]>>; | ||
resolveFor<Binding>(parent: unknown, binding: Binding, runtimeValues?: any[], createError?: ErrorCreator): Promise<Make<Binding>>; | ||
make<Binding extends keyof KnownBindings>(binding: Binding, runtimeValues?: any[], createError?: ErrorCreator): Promise<Binding extends string | symbol ? KnownBindings[Binding] : Make<Binding>>; | ||
make<Binding>(binding: Binding, runtimeValues?: any[], createError?: ErrorCreator): Promise<Make<Binding>>; | ||
call<Value extends Record<any, any>, Method extends ExtractFunctions<Value>>(value: Value, method: Method, runtimeValues?: any[], createError?: ErrorCreator): Promise<ReturnType<Value[Method]>>; | ||
bindValue<Binding extends keyof KnownBindings>(binding: Binding extends string | symbol ? Binding : never, value: KnownBindings[Binding]): void; | ||
bindValue<Binding extends AbstractConstructor<any>>(binding: Binding, value: InstanceType<Binding>): void; | ||
} |
@@ -24,7 +24,7 @@ import { inspect } from 'node:util'; | ||
} | ||
#invalidBindingException(parent, binding) { | ||
#invalidBindingException(parent, binding, createError) { | ||
if (parent) { | ||
return new InvalidArgumentsException(`Cannot inject "${inspect(binding)}" in "[class: ${parent.name}]". The value cannot be constructed`); | ||
return createError(`Cannot inject "${inspect(binding)}" in "[class: ${parent.name}]". The value cannot be constructed`); | ||
} | ||
return new InvalidArgumentsException(`Cannot construct value "${inspect(binding)}" using container`); | ||
return createError(`Cannot construct value "${inspect(binding)}" using container`); | ||
} | ||
@@ -69,6 +69,6 @@ #getBindingProvider(binding) { | ||
} | ||
async resolveFor(parent, binding, runtimeValues) { | ||
async resolveFor(parent, binding, runtimeValues, createError = (message) => new RuntimeException(message)) { | ||
const isAClass = isClass(binding); | ||
if (typeof binding !== 'string' && typeof binding !== 'symbol' && !isAClass) { | ||
throw this.#invalidBindingException(parent, binding); | ||
throw this.#invalidBindingException(parent, binding, createError); | ||
} | ||
@@ -134,11 +134,12 @@ if (isAClass && this.#containerSwaps.has(binding)) { | ||
let dependencies = []; | ||
const bindingProvider = this.#getBindingProvider(binding); | ||
const classConstructor = binding; | ||
const bindingProvider = this.#getBindingProvider(classConstructor); | ||
if (bindingProvider) { | ||
dependencies = await bindingProvider(binding, '_constructor', this, containerProvider, runtimeValues); | ||
dependencies = await bindingProvider(classConstructor, '_constructor', this, containerProvider, runtimeValues); | ||
} | ||
else { | ||
dependencies = await containerProvider(binding, '_constructor', this, runtimeValues); | ||
dependencies = await containerProvider(classConstructor, '_constructor', this, runtimeValues); | ||
} | ||
if (dependencies.length < binding.length) { | ||
throw new RuntimeException(`Cannot construct "${binding.name}" class. Container is not able to resolve its dependencies`); | ||
if (dependencies.length < classConstructor.length) { | ||
throw createError(`Cannot construct "${binding.name}" class. Container is not able to resolve its dependencies`); | ||
} | ||
@@ -153,13 +154,13 @@ const value = new binding(...dependencies); | ||
} | ||
throw new InvalidArgumentsException(`Cannot resolve binding "${String(binding)}" from the container`); | ||
throw createError(`Cannot resolve binding "${String(binding)}" from the container`); | ||
} | ||
async make(binding, runtimeValues) { | ||
async make(binding, runtimeValues, createError) { | ||
if (this.#containerAliases.has(binding)) { | ||
return this.resolveFor(null, this.#containerAliases.get(binding), runtimeValues); | ||
return this.resolveFor(null, this.#containerAliases.get(binding), runtimeValues, createError); | ||
} | ||
return this.resolveFor(null, binding, runtimeValues); | ||
return this.resolveFor(null, binding, runtimeValues, createError); | ||
} | ||
async call(value, method, runtimeValues) { | ||
async call(value, method, runtimeValues, createError = (message) => new RuntimeException(message)) { | ||
if (typeof value[method] !== 'function') { | ||
throw new InvalidArgumentsException(`Missing method "${String(method)}" on "${inspect(value)}"`); | ||
throw createError(`Missing method "${String(method)}" on "${inspect(value)}"`); | ||
} | ||
@@ -179,3 +180,3 @@ if (debug.enabled) { | ||
if (dependencies.length < value[method].length) { | ||
throw new RuntimeException(`Cannot call "${binding.name}.${String(method)}" method. Container is not able to resolve its dependencies`); | ||
throw createError(`Cannot call "${binding.name}.${String(method)}" method. Container is not able to resolve its dependencies`); | ||
} | ||
@@ -182,0 +183,0 @@ return value[method](...dependencies); |
@@ -0,3 +1,5 @@ | ||
import type { Exception } from '@poppinss/utils'; | ||
import type { Container } from './container.js'; | ||
import type { ContainerResolver } from './resolver.js'; | ||
export type ErrorCreator = (message: string) => Exception; | ||
export type ExtractFunctions<T> = { | ||
@@ -9,3 +11,6 @@ [P in keyof T]: T[P] extends (...args: any[]) => any ? P : never; | ||
export type InspectableConstructor = Function & { | ||
containerInjections?: Record<string | number | symbol, any[]>; | ||
containerInjections?: Record<string | number | symbol, { | ||
dependencies: any[]; | ||
createError?: ErrorCreator; | ||
}>; | ||
containerProvider?: ContainerProvider; | ||
@@ -12,0 +17,0 @@ }; |
{ | ||
"name": "@adonisjs/fold", | ||
"version": "9.9.3-0", | ||
"version": "9.9.3-1", | ||
"description": "A simple and straight forward implementation for IoC container in JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -67,3 +67,5 @@ # AdonisJS Fold | ||
static containerInjections = { | ||
_constructor: [Database], | ||
_constructor: { | ||
dependencies: [Database] | ||
}, | ||
} | ||
@@ -160,3 +162,5 @@ | ||
static containerInjections = { | ||
find: [Database], | ||
find: { | ||
dependencies: [Database], | ||
}, | ||
} | ||
@@ -163,0 +167,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62753
777
674