# Handler

# Concept

An handler is a class which contains only the business logic :

  • no transport (http, redis, etc.) ;
  • no serialization (json, protobuf, etc.) ;
  • no direct connection with dependencies (use provider instead) ;
  • no implementation strongly bound to external dependencies (use interfaces!) ;

# Wrong

import { Types } from '@ilos/core';

async call({ params }) {
  return this.mongo.find({ name: {$eq: params.name }});
}
1
2
3
4
5

# Good

async call({ params }) {
  return this.repository.findByName(params.name);
}
1
2
3

# Configuration

You must decorate your handler :

import { Container } from '@ilos/core';

@Container.handler({
  service: 'greeting',
  method: 'hello',
  version: '0.0.1',
})
class Action implements HandlerInterface {}
1
2
3
4
5
6
7
8

Notes:

  • The version default is 'latest', you may omit this params ;
  • The method could be '*' to catch all service request (used by remote handler by example) ;

# Usage

# Dependencies

You can use the ioc engine to get providers (among others) in your handler. Just add it to the constructor:

  constructor(
    private repository: RepositoryProviderInterfaceResolver, // get an implementation from interface bindings
    private custom: CustomProvider, // get an implementation
  ) {
    //
  }
1
2
3
4
5
6

# Hooks

The handler can have hooks by implementing RegisterHookInterface, InitHookInterface, or DestroyHookInterface. See hooks.

# Call/Handle

The business logic is in the call function but you may use action (which includes some sugar for most use case). If so, you can write:

import { Parents, Container, Types } from '@ilos/core';

type HelloParamsType = {
  name: string,
};

@Container.handler({
  service: 'greeting',
  method: 'hello',
})
export class HelloAction extends Parents.Action {
  constructor(
  ) {
    super();
  }

  protected async handle(
    params: HelloParamsType,
    context: Types.ContextType,
  ): Promise<string> {
    return `Hello ${params.name}`;
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# Middlewares

  public readonly middlewares: (string|[string, any])[] = [
    'isConnected',
    ['can', ['user.list']]
  ];
1
2
3
4

You can add middlewares to you handler by adding a middlewares property. This will use middlewares bindings declared in your service provider. You can pass arguments with a tuple.