Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const defaultContainer: { get<T>(someClass: { new (...args: any[]): T } | Functi
})();

let userContainer: { get<T>(someClass: { new (...args: any[]): T } | Function): T };
let userAsyncContainer: { get<T>(someClass: { new (...args: any[]): T } | Function): Promise<T> };
let userContainerOptions: UseContainerOptions;

/**
Expand All @@ -41,6 +42,11 @@ export function useContainer(iocContainer: { get(someClass: any): any }, options
userContainerOptions = options;
}

export function useAsyncContainer(iocContainer: { get(someClass: any): Promise<any> }, options?: UseContainerOptions): void {
userAsyncContainer = iocContainer;
userContainerOptions = options;
}

/**
* Gets the IOC container used by this library.
*/
Expand All @@ -57,3 +63,17 @@ export function getFromContainer<T>(someClass: { new (...args: any[]): T } | Fun
}
return defaultContainer.get<T>(someClass);
}

export async function getFromAsyncContainer<T>(someClass: { new (...args: any[]): T } | Function): Promise<T> {
if (userAsyncContainer) {
try {
const instance = await userAsyncContainer.get(someClass);
if (instance) return instance;

if (!userContainerOptions || !userContainerOptions.fallback) return instance;
} catch (error) {
if (!userContainerOptions || !userContainerOptions.fallbackOnErrors) throw error;
}
}
return getFromContainer(someClass);
}
24 changes: 13 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ValidatorOptions } from './validation/ValidatorOptions';
import { ValidationSchema } from './validation-schema/ValidationSchema';
import { getMetadataStorage } from './metadata/MetadataStorage';
import { Validator } from './validation/Validator';
import { getFromContainer } from './container';
import { getFromAsyncContainer, getFromContainer } from './container';

// -------------------------------------------------------------------------
// Export everything api users needs
Expand Down Expand Up @@ -49,11 +49,12 @@ export function validate(
maybeValidatorOptions?: ValidatorOptions
): Promise<ValidationError[]> {
if (typeof schemaNameOrObject === 'string') {
return getFromContainer(Validator).validate(
schemaNameOrObject,
objectOrValidationOptions as object,
maybeValidatorOptions
);
return getFromAsyncContainer(Validator)
.then((validator) => validator.validate(
schemaNameOrObject,
objectOrValidationOptions as object,
maybeValidatorOptions
));
} else {
return getFromContainer(Validator).validate(schemaNameOrObject, objectOrValidationOptions as ValidatorOptions);
}
Expand Down Expand Up @@ -82,11 +83,12 @@ export function validateOrReject(
maybeValidatorOptions?: ValidatorOptions
): Promise<void> {
if (typeof schemaNameOrObject === 'string') {
return getFromContainer(Validator).validateOrReject(
schemaNameOrObject,
objectOrValidationOptions as object,
maybeValidatorOptions
);
return getFromAsyncContainer(Validator).then((validator) => validator
.validateOrReject(
schemaNameOrObject,
objectOrValidationOptions as object,
maybeValidatorOptions
));
} else {
return getFromContainer(Validator).validateOrReject(
schemaNameOrObject,
Expand Down