Skip to content

Commit

Permalink
Merge pull request #3 from abbl/controller-pattern
Browse files Browse the repository at this point in the history
Append controller message patterns with controller scoped pattern
  • Loading branch information
przucidlo authored Aug 3, 2021
2 parents 7d7340f + 3cb5faf commit 53a8ce8
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 12 deletions.
3 changes: 2 additions & 1 deletion lib/constants/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const PATTERN_METADATA = 'PATTERN_METADATA';
export const CONTROLLER_PATTERN_METADATA = 'CONTROLLER_PATTERN_METADATA';
export const HANDLER_PATTERN_METADATA = 'HANDLER_PATTERN_METADATA';
export const HANDLER_ARGS_METADATA = 'HANDLER_ARGS_METADATA';
export const MIDDLEWARE_METADATA = 'MIDDLEWARE_METADATA';
7 changes: 7 additions & 0 deletions lib/decorators/controller.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { CONTROLLER_PATTERN_METADATA } from '../constants/decorators';

export default function Controller(pattern: string): ClassDecorator {
return (target) => {
Reflect.defineMetadata(CONTROLLER_PATTERN_METADATA, pattern, target.prototype);
};
}
5 changes: 0 additions & 5 deletions lib/decorators/message-controller.decorator.ts

This file was deleted.

4 changes: 2 additions & 2 deletions lib/decorators/message-pattern.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/ban-types */
import 'reflect-metadata';
import { PATTERN_METADATA } from '../constants/decorators';
import { HANDLER_PATTERN_METADATA } from '../constants/decorators';

export default function MessagePattern(pattern: string): MethodDecorator {
return (target, propertyKey, descriptor) => {
Reflect.defineMetadata(PATTERN_METADATA, pattern, descriptor.value);
Reflect.defineMetadata(HANDLER_PATTERN_METADATA, pattern, descriptor.value);
};
}
2 changes: 2 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export * from './decorators/create-param-decorator';
export { default as createParamDecorator } from './decorators/create-param-decorator';
export * from './decorators/use-middleware.decorator';
export { default as UseMiddleware } from './decorators/use-middleware.decorator';
export * from './decorators/controller.decorator';
export { default as Controller } from './decorators/controller.decorator';

export * from './middleware/internal/param-transformer.middleware';
export { default as ParamTransformerMiddleware } from './middleware/internal/param-transformer.middleware';
Expand Down
20 changes: 17 additions & 3 deletions lib/metadata-readers/controller-handlers-metadata.reader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import 'reflect-metadata';
import { injectable } from 'inversify';
import { PATTERN_METADATA, HANDLER_ARGS_METADATA, MIDDLEWARE_METADATA } from '../constants/decorators';
import {
HANDLER_PATTERN_METADATA,
HANDLER_ARGS_METADATA,
MIDDLEWARE_METADATA,
CONTROLLER_PATTERN_METADATA,
} from '../constants/decorators';
import { ControllerHandlerMetadata } from '../interfaces/controller-handler-metadata.interface';
import { MessageHandler } from '../types/message-handler.type';
import { AbstractMetadataReader } from './abstract-metadata.reader';
Expand All @@ -16,7 +21,12 @@ export class ControllerHandlersMetadataReader extends AbstractMetadataReader {
const messageHandler = (controller as any)[handlerName];

if (this.isReadable(messageHandler, handlerName)) {
const pattern = this.getHandlerPattern(controller, handlerName);
const controllerPattern = this.getControllerPattern(controller);
let pattern = this.getHandlerPattern(controller, handlerName);

if (controllerPattern) {
pattern = controllerPattern + pattern;
}

if (this.isMessageHandler(pattern)) {
const paramsMetadata = this.getHandlerParamsMetadata(controller, handlerName);
Expand All @@ -43,7 +53,11 @@ export class ControllerHandlersMetadataReader extends AbstractMetadataReader {
}

private getHandlerPattern(controller: unknown, handlerName: string) {
return Reflect.getMetadata(PATTERN_METADATA, controller[handlerName]);
return Reflect.getMetadata(HANDLER_PATTERN_METADATA, controller[handlerName]);
}

private getControllerPattern(controller: unknown) {
return Reflect.getMetadata(CONTROLLER_PATTERN_METADATA, controller);
}

private getHandlerParamsMetadata(controller: unknown, handlerName: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'reflect-metadata';
import { MessagePattern, UseMiddleware } from '../../../lib';
import Data from '../../../lib/decorators/data.decorator';
import Controller from '../../../lib/decorators/controller.decorator';
import { isHandlerParamMetadata } from '../../../lib/interfaces/handler-param-metadata.interface';
import { ControllerHandlersMetadataReader } from '../../../lib/metadata-readers/controller-handlers-metadata.reader';
import { MockMiddleware } from '../__mocks__/mock-middleware';
Expand Down Expand Up @@ -34,8 +35,8 @@ describe('ControllersHandlersMetadataReader', () => {
});

describe('Should read handler metadata', () => {
const controllerPattern = 'controller-pattern';
const pattern = 'message-pattern';

@UseMiddleware(MockMiddleware)
class Test {
@MessagePattern(pattern)
Expand All @@ -45,6 +46,14 @@ describe('ControllersHandlersMetadataReader', () => {
}
}

@Controller(controllerPattern)
class ControllerTest {
@MessagePattern(pattern)
public coolerHandler() {
return 'text';
}
}

it('Should read and save reference to handler function', () => {
const testClassObject = new Test();

Expand All @@ -53,6 +62,14 @@ describe('ControllersHandlersMetadataReader', () => {
expect(handlersMetadata[pattern].handler('')).toStrictEqual(testClassObject.coolHandler(''));
});

it('Should combine controller pattern and handler pattern', () => {
const controllerTest = new ControllerTest();

const metadata = metadataReader.read(controllerTest);

expect(metadata[controllerPattern + pattern].handler()).toBe(controllerTest.coolerHandler());
});

it('Should read and save param metadata', () => {
const paramsHandlersMetadata = metadataReader.read(new Test())[pattern].paramsMetadata;

Expand Down

0 comments on commit 53a8ce8

Please sign in to comment.