Skip to content

Commit

Permalink
feat(context): introduce AsyncProxy type for proxy with async interce…
Browse files Browse the repository at this point in the history
…ptors
  • Loading branch information
raymondfeng committed Apr 16, 2019
1 parent 6e8f6bb commit 2ce1402
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import {expect} from '@loopback/testlab';
import {
asInterceptor,
AsyncProxy,
Context,
createProxyWithInterceptors,
inject,
Expand Down Expand Up @@ -361,12 +362,34 @@ describe('Interceptor', () => {
]);
});

it('creates a proxy that converts sync method to be async', async () => {
// Apply `log` to all methods on the class
@intercept(log)
class MyController {
// Apply multiple interceptors. The order of `log` will be preserved as it
// explicitly listed at method level
@intercept(convertName, log)
greet(name: string) {
return `Hello, ${name}`;
}
}
const proxy = createProxyWithInterceptors(new MyController(), ctx);
const msg = await proxy.greet('John');
expect(msg).to.equal('Hello, JOHN');
expect(events).to.eql([
'convertName: before-greet',
'log: before-greet',
'log: after-greet',
'convertName: after-greet',
]);
});

it('invokes interceptors on a static method', async () => {
// Apply `log` to all methods on the class
@intercept(log)
class MyController {
// The class level `log` will be applied
static async greetStatic(name: string) {
static greetStatic(name: string) {
return `Hello, ${name}`;
}
}
Expand Down Expand Up @@ -420,7 +443,7 @@ describe('Interceptor', () => {
class DummyController {
constructor(
@inject('my-controller', {asProxyWithInterceptors: true})
public readonly myController: MyController,
public readonly myController: AsyncProxy<MyController>,
) {}
}
ctx.bind('my-controller').toClass(MyController);
Expand Down
49 changes: 47 additions & 2 deletions packages/context/src/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,51 @@ function invokeInterceptors(
return next();
}

/**
* The Promise type for `T`. If `T` extends `Promise`, the type is `T`,
* otherwise the type is `Promise<T>`.
*/
export type PromiseType<T> = T extends Promise<unknown> ? T : Promise<T>;

/**
* The async variant of a function to always return Promise<R>. If T is not a
* function, the type is `T`.
*/
// tslint:disable-next-line:no-unused (possible tslint bug to treat `R` as unused)
export type AsyncType<T> = T extends (...args: InvocationArgs) => infer R
? (...args: InvocationArgs) => PromiseType<R>
: T;

/**
* The proxy type for `T`. The return type for any method of `T` with original
* return type `R` becomes `Promise<R>` if `R` does not extend `Promise`.
* Property types stay untouched. For example:
*
* ```ts
* class MyController {
* name: string;
*
* greet(name: string): string {
* return `Hello, ${name}`;
* }
*
* async hello(name: string) {
* return `Hello, ${name}`;
* }
* }
* ```
*
* `AsyncProxy<MyController>` will be:
* ```ts
* {
* name: string; // the same as MyController
* greet(name: string): Promise<string>; // the return type becomes `Promise<string>`
* hello(name: string): Promise<string>; // the same as MyController
* }
* ```
*/
export type AsyncProxy<T> = {[P in keyof T]: AsyncType<T[P]>};

/**
* A proxy handler that applies interceptors
*
Expand Down Expand Up @@ -350,6 +395,6 @@ export class InterceptorHandler<T extends object> implements ProxyHandler<T> {
export function createProxyWithInterceptors<T extends object>(
target: T,
context?: Context,
) {
return new Proxy(target, new InterceptorHandler(context));
): AsyncProxy<T> {
return new Proxy(target, new InterceptorHandler(context)) as AsyncProxy<T>;
}

0 comments on commit 2ce1402

Please sign in to comment.