Skip to content
This repository was archived by the owner on Jun 15, 2024. It is now read-only.
This repository was archived by the owner on Jun 15, 2024. It is now read-only.

Why not subclass moleculer.ServiceBroker instead? #3

Description

@ujwal-setlur

Screen Shot 2019-12-26 at 3 29 45 PM

Screen Shot 2019-12-26 at 3 29 26 PM

We would be super excited, if you can help us to introduce a better way how to extend Moleculer's origin definitions without cloning and rewriting them at all. If you are an expert in TypeScript, we would love to get some help with this :)

I am no TypeScript expert by any means, but I came up with a simpler approach of subclassing ServiceBroker and overloading the call/emit signatures instead of cloning and overriding the original definitions:

// Moleculer micro-services framework
import moleculer from 'moleculer';

// our supported service actions
import * as ServiceTypes from './types/service.types'; // eslint-disable-line import/extensions

class TypedServiceBroker extends moleculer.ServiceBroker {
  // Overload our call functions to allow for single or multiple arguments
  public call(type: ServiceTypes.SimpleActionType): PromiseLike<any>;

  // eslint-disable-next-line no-dupe-class-members
  public call<T extends ServiceTypes.ActionType>(
    type: T,
    args: ServiceTypes.ExtractActionParameters<
      ServiceTypes.Action,
      T
    >,
    opts?: moleculer.CallingOptions
  ): PromiseLike<any>;

  // eslint-disable-next-line no-dupe-class-members
  public call<T extends ServiceTypes.ActionType>(
    type: T | ServiceTypes.SimpleActionType,
    args?: ServiceTypes.ExtractActionParameters<
      ServiceTypes.Action,
      T
    >,
    opts?: moleculer.CallingOptions
  ): PromiseLike<any> {
    return super.call(type, <any>args, opts);
  }
}

export default TypedServiceBroker;

My service type definitions are also simpler, though I might be missing something :)

interface ActionWithParamsInterface {
  type: string;
  params: Object;
}

export type Action =
  | {
      type: 'order.hello';
    }
  | {
      type: 'order.boo';
      params: {
        foo: string;
        bar?: string;
      };
    }
  | {
      type: 'order.welcome';
      params: {
        name: string;
      };
    };

type ExcludeTypeField<A> = { [K in Exclude<keyof A, 'type'>]: A[K] };

export type ExtractActionParameters<
  A,
  T
> = A extends ActionWithParamsInterface
  ? Extract<A, { type: T }>['params']
  : never;

export type ActionType = Action['type'];

// Find action types without parameters
type ExtractSimpleAction<A> = A extends any
  ? {} extends ExcludeTypeField<A>
    ? A
    : never
  : never;
export type SimpleActionType = ExtractSimpleAction<Action>['type'];

I haven't added support for events and return types yet, but it should be straightforward I think.

Just a thought. Thank you for your great work!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions