diff --git a/README.md b/README.md index d5793b8..9caa1ad 100644 --- a/README.md +++ b/README.md @@ -1335,10 +1335,10 @@ because they depend on Node-specific packages. # Contributing -Clone repo, run `npm i` to install all dependencies, run `npm run build` to create an initial build, and then -`npm run test -- --watchAll` to start writing code. +Clone repo, run `yarn` to install all dependencies, run `yarn build` to create an initial build, and then +`yarn test --watchAll` to start writing code. -For code coverage, run `npm run cover`. +For code coverage, run `yarn cover`. If you submit a PR, please aim for 100% code coverage and no linting errors. Travis will fail if there are linting errors. Thank you for considering diff --git a/examples/typescript/src/index.ts b/examples/typescript/src/index.ts index 7190e58..3e15a73 100644 --- a/examples/typescript/src/index.ts +++ b/examples/typescript/src/index.ts @@ -3,8 +3,13 @@ import { createContainer, asClass, InjectionMode } from '../../../src/awilix' import TestService from './services/TestService' import DependentService from './services/DependentService' +interface ICradle { + testService: TestService + depService: DependentService +} + // Create the container -const container = createContainer({ +const container = createContainer({ injectionMode: InjectionMode.CLASSIC }) @@ -15,7 +20,7 @@ container.register({ }) // Resolve a dependency using the cradle. -let dep1: DependentService = container.cradle.depService +let dep1 = container.cradle.depService // Resolve a dependency using `resolve` let dep2 = container.resolve('depService') diff --git a/src/container.ts b/src/container.ts index cfcc75a..bb88fae 100644 --- a/src/container.ts +++ b/src/container.ts @@ -21,7 +21,7 @@ import { AwilixResolutionError, AwilixTypeError } from './errors' * The container returned from createContainer has some methods and properties. * @interface AwilixContainer */ -export interface AwilixContainer { +export interface AwilixContainer { /** * Options the container was configured with. */ @@ -30,7 +30,7 @@ export interface AwilixContainer { * The proxy injected when using `PROXY` injection mode. * Can be used as-is. */ - readonly cradle: any + readonly cradle: Cradle /** * Getter for the rolled up registrations that merges the container family tree. */ @@ -42,7 +42,7 @@ export interface AwilixContainer { /** * Creates a scoped container with this one as the parent. */ - createScope(): AwilixContainer + createScope(): AwilixContainer /** * Used by `util.inspect`. */ @@ -67,7 +67,7 @@ export interface AwilixContainer { /** * Pairs resolvers to registration names and registers them. */ - register(nameAndRegistrationPair: NameAndRegistrationPair): this + register(nameAndRegistrationPair: NameAndRegistrationPair): this /** * Resolves the registration with the given name. * @@ -122,23 +122,23 @@ export interface ResolveOptions { /** * Cache entry. */ -export interface CacheEntry { +export interface CacheEntry { /** * The resolver that resolved the value. */ - resolver: Resolver + resolver: Resolver /** * The resolved value. */ - value: any + value: T } /** * Register a Registration * @interface NameAndRegistrationPair */ -export interface NameAndRegistrationPair { - [key: string]: Resolver +export type NameAndRegistrationPair = { + [U in keyof T]?: Resolver } /** @@ -194,10 +194,10 @@ const ROLL_UP_REGISTRATIONS = Symbol('rollUpRegistrations') * @return {object} * The container. */ -export function createContainer( +export function createContainer( options?: ContainerOptions, - parentContainer?: AwilixContainer -): AwilixContainer { + parentContainer?: AwilixContainer +): AwilixContainer { options = { injectionMode: InjectionMode.PROXY, ...options @@ -221,7 +221,7 @@ export function createContainer( * knowing where they come from. I call it the "cradle" because * it is where registered things come to life at resolution-time. */ - const cradle: { [key: string]: any } = new Proxy( + const cradle = new Proxy( { [util.inspect.custom]: inspectCradle }, @@ -274,12 +274,12 @@ export function createContainer( return undefined } } - ) + ) as T // The container being exposed. const container = { options, - cradle: cradle as any, + cradle, inspect, cache: new Map(), loadModules, @@ -360,14 +360,14 @@ export function createContainer( * @return {object} * The scoped container. */ - function createScope(): AwilixContainer { - return createContainer(options, container) + function createScope

(): AwilixContainer

{ + return createContainer(options, container as AwilixContainer) } /** * Adds a registration for a resolver. */ - function register(arg1: any, arg2: any): AwilixContainer { + function register(arg1: any, arg2: any): AwilixContainer { const obj = nameValueToObject(arg1, arg2) const keys = [...Object.keys(obj), ...Object.getOwnPropertySymbols(obj)] diff --git a/src/resolvers.ts b/src/resolvers.ts index 130a231..bebaabe 100644 --- a/src/resolvers.ts +++ b/src/resolvers.ts @@ -18,13 +18,15 @@ export const RESOLVER = Symbol('Awilix Resolver Config') * * @type {Function} */ -export type InjectorFunction = (container: AwilixContainer) => object +export type InjectorFunction = ( + container: AwilixContainer +) => object /** * A resolver object returned by asClass(), asFunction() or asValue(). */ export interface Resolver extends ResolverOptions { - resolve(container: AwilixContainer): T + resolve(container: AwilixContainer): T } /** @@ -335,7 +337,10 @@ function updateResolver, B>( * @param {Object} locals * @return {Function} */ -function wrapWithLocals(container: AwilixContainer, locals: any) { +function wrapWithLocals( + container: AwilixContainer, + locals: any +) { return function wrappedResolve(name: string, resolveOpts: ResolveOptions) { if (name in locals) { return locals[name] @@ -353,8 +358,8 @@ function wrapWithLocals(container: AwilixContainer, locals: any) { * @param {Function} injector * @return {Proxy} */ -function createInjectorProxy( - container: AwilixContainer, +function createInjectorProxy( + container: AwilixContainer, injector: InjectorFunction ) { const locals = injector(container) as any @@ -444,9 +449,9 @@ function generateResolve(fn: Function, dependencyParseTarget?: Function) { const dependencies = parseDependencies(dependencyParseTarget) // Use a regular function instead of an arrow function to facilitate binding to the resolver. - return function resolve( + return function resolve( this: BuildResolver, - container: AwilixContainer + container: AwilixContainer ) { // Because the container holds a global reolutionMode we need to determine it in the proper order of precedence: // resolver -> container -> default value