Skip to content

Commit

Permalink
feat(context): relax parameter type for isProviderClass()
Browse files Browse the repository at this point in the history
This change makes it possible to test if a unknown value is provider class
without casting to Constructor first.
  • Loading branch information
raymondfeng committed May 5, 2020
1 parent 3bee0a5 commit aafe7d0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
5 changes: 2 additions & 3 deletions packages/context/src/binding-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
isProviderClass,
removeNameAndKeyTags,
} from './binding-inspector';
import {Provider} from './provider';
import {Constructor} from './value-promise';

/**
Expand Down Expand Up @@ -97,8 +96,8 @@ export namespace bind {
*/
export function provider(
...specs: BindingSpec[]
): (target: Constructor<Provider<unknown>>) => void {
return (target: Constructor<Provider<unknown>>) => {
): (target: Constructor<unknown>) => void {
return (target: Constructor<unknown>) => {
if (!isProviderClass(target)) {
throw new Error(`Target ${target} is not a Provider`);
}
Expand Down
6 changes: 4 additions & 2 deletions packages/context/src/binding-inspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ export type BindingSpec<T = unknown> = BindingTemplate<T> | BindingScopeAndTags;
* @typeParam T - Value type
*/
export function isProviderClass<T>(
cls: Constructor<T | Provider<T>>,
cls: unknown,
): cls is Constructor<Provider<T>> {
return typeof cls?.prototype?.value === 'function';
return (
typeof cls === 'function' && typeof cls.prototype?.value === 'function'
);
}

/**
Expand Down
13 changes: 5 additions & 8 deletions packages/context/src/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,11 @@ export function registerInterceptor(
: LOCAL_INTERCEPTOR_NAMESPACE;

let binding: Binding<Interceptor>;
if (isProviderClass(interceptor as Constructor<Provider<Interceptor>>)) {
binding = createBindingFromClass(
interceptor as Constructor<Provider<Interceptor>>,
{
defaultNamespace: namespace,
...options,
},
);
if (isProviderClass(interceptor)) {
binding = createBindingFromClass(interceptor, {
defaultNamespace: namespace,
...options,
});
if (binding.tagMap[ContextTags.GLOBAL_INTERCEPTOR]) {
global = true;
}
Expand Down

0 comments on commit aafe7d0

Please sign in to comment.