Skip to content

Commit

Permalink
improve d.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Jun 14, 2018
1 parent fc3be04 commit f2d281d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 59 deletions.
125 changes: 77 additions & 48 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ declare namespace Moleculer {
broker: ServiceBroker;
endpoint: Endpoint;
action: Action;
service: Service;
service?: Service;
nodeID?: string;

options: CallOptions;
options: CallingOptions;

parentID?: string;

Expand Down Expand Up @@ -107,19 +107,19 @@ declare namespace Moleculer {
interface ServiceSettingSchema {
$noVersionPrefix?: boolean;
$noServiceNamePrefix?: boolean;
$dependencyTimeout?: number;
[name: string]: any;
}

type ServiceEventHandler = ((payload: any, sender: string, eventName: string) => void) & ThisType<Service>;
type ServiceLocalEventHandler = ((node: GenericObject) => void) & ThisType<Service>;

interface ServiceEvent {
name: string;
name?: string;
group?: string;
handler: ServiceEventHandler | ServiceLocalEventHandler;
handler: ServiceEventHandler;
}

type ServiceEvents = { [key: string]: ServiceEventHandler | ServiceLocalEventHandler };
type ServiceEvents = { [key: string]: ServiceEventHandler | ServiceEvent };

type ServiceMethods = { [key: string]: ((...args: any[]) => any) } & ThisType<Service>;

Expand Down Expand Up @@ -156,16 +156,13 @@ declare namespace Moleculer {
broker: ServiceBroker;
logger: LoggerInstance;
actions?: Actions;
mixins?: Array<ServiceSchema>;
methods?: ServiceMethods;
Promise: typeof Bluebird;

waitForServices(serviceNames: string | Array<string>, timeout?: number, interval?: number): Bluebird<void>;

events?: ServiceEvents;
created: () => void;
started: () => Bluebird<void>;
stopped: () => Bluebird<void>;
_init: () => void;
_start: () => Bluebird<void>;
_stop: () => Bluebird<void>;
[name: string]: any;
}

Expand Down Expand Up @@ -204,28 +201,31 @@ declare namespace Moleculer {
nodeID?: string;

logger?: Logger | boolean;
logLevel?: string;
logLevel?: string | GenericObject;
logFormatter?: Function | string;
logObjectPrinter?: Function;

transporter?: Transporter | string | GenericObject;
requestTimeout?: number;
requestRetry?: number;
retryPolicy?: RetryPolicyOptions;

maxCallLevel?: number;
heartbeatInterval?: number;
heartbeatTimeout?: number;

disableBalancer?: boolean;
trackContext?: boolean;
gracefulStopTimeout?: number;

transit?: BrokerTransitOptions;
disableBalancer?: boolean;

registry?: BrokerRegistryOptions;

circuitBreaker?: BrokerCircuitBreakerOptions;

retryPolicy?: RetryPolicyOptions;

bulkhead?: BulkheadOptions;

transit?: BrokerTransitOptions;

cacher?: Cacher | string | GenericObject;
serializer?: Serializer | string | GenericObject;

Expand All @@ -234,14 +234,16 @@ declare namespace Moleculer {
metrics?: boolean;
metricsRate?: number;
internalServices?: boolean;
internalMiddlewares?: boolean;

hotReload?: boolean;

middlewares?: Array<Middleware>;
replCommands?: Array<GenericObject>;

ServiceFactory?: Service;
ContextFactory?: Context;

middlewares?: Array<Middleware>;

created?: (broker: ServiceBroker) => void;
started?: (broker: ServiceBroker) => void;
stopped?: (broker: ServiceBroker) => void;
Expand Down Expand Up @@ -297,12 +299,14 @@ declare namespace Moleculer {
type FallbackResponse = string | number | GenericObject;
type FallbackResponseHandler = (ctx: Context, err: Errors.MoleculerError) => Bluebird<any>;

interface CallOptions {
interface CallingOptions {
timeout?: number;
retries?: number;
fallbackResponse?: FallbackResponse | Array<FallbackResponse> | FallbackResponseHandler;
nodeID?: string;
meta?: GenericObject;
parentCtx?: Context;
requestID?: string;
}

type CallDefinition<P extends GenericObject = GenericObject> = {
Expand All @@ -325,6 +329,12 @@ declare namespace Moleculer {
action: Action;
}

interface PongResponse {
nodeID: string;
elapsedTime: number;
timeDiff: number
}

class ServiceBroker {
constructor(options?: BrokerOptions);

Expand All @@ -337,27 +347,33 @@ declare namespace Moleculer {
serializer?: Serializer;
validator?: Validator;
transit: GenericObject;
middlewares: GenericObject;

start(): Bluebird<void>;
stop(): Bluebird<void>;

repl(): void;

getLogger(module: string, service?: string, version?: number | string): LoggerInstance;
getLogger(module: string, props?: string | GenericObject): LoggerInstance;
fatal(message: string, err?: Error, needExit?: boolean): void;
loadServices(folder?: string, fileMask?: string): number;
loadService(filePath: string): Service;
watchService(service: Service): void;
hotReloadService(service: Service): Service;
createService(schema: ServiceSchema): Service;
createService(schema: ServiceSchema, schemaMods?: ServiceSchema): Service;
destroyService(service: Service): Bluebird<void>;

getLocalService(serviceName: string, version?: string | number): Service;
waitForServices(serviceNames: string | Array<string>, timeout?: number, interval?: number, logger?: LoggerInstance): Bluebird<void>;

/**
*
* @param mws
* @deprecated
*/
use(...mws: Array<Function>): void;

findNextActionEndpoint(actionName: string, opts?: GenericObject): ActionEndpoint | Errors.MoleculerRetryableError;
findNextActionEndpoint(actionName: string, nodeID?: string): ActionEndpoint | Errors.MoleculerRetryableError;

/**
* Call an action (local or global)
Expand All @@ -369,7 +385,7 @@ declare namespace Moleculer {
*
* @memberof ServiceBroker
*/
call<T = any, P extends GenericObject = GenericObject>(actionName: string, params?: P, opts?: CallOptions): Bluebird<T>;
call<T = any, P extends GenericObject = GenericObject>(actionName: string, params?: P, opts?: CallingOptions): Bluebird<T>;

/**
* Multiple action calls.
Expand Down Expand Up @@ -441,12 +457,12 @@ declare namespace Moleculer {
*/
broadcastLocal(eventName: string, payload?: any, groups?: string | Array<string>): void;

ping(): Bluebird<any>;
ping(nodeID: string): Bluebird<any>;
ping(nodeID: Array<string>): Bluebird<any>;
ping(): Bluebird<Array<PongResponse>>;
ping(nodeID: string, timeout?: number): Bluebird<PongResponse>;
ping(nodeID: Array<string>, timeout?: number): Bluebird<Array<PongResponse>>;

getHealthStatus(): NodeHealthStatus;
getLocalNodeInfo(force?: boolean): {
getLocalNodeInfo(): {
ipList: string[];
hostname: string;
client: any;
Expand All @@ -455,6 +471,8 @@ declare namespace Moleculer {
services: Array<any>;
};

getCpuUsage(): Bluebird<any>;

MOLECULER_VERSION: string;
PROTOCOL_VERSION: string;
[name: string]: any;
Expand Down Expand Up @@ -516,13 +534,11 @@ declare namespace Moleculer {

class Transporter {
constructor(opts?: GenericObject);
init(broker: ServiceBroker, messageHandler: (cmd: string, msg: string) => void): void;
init(transit: Transit, messageHandler: (cmd: string, msg: string) => void, afterConnect: (wasReconnect: boolean) => void): void;
connect(): Bluebird<any>;
disconnect(): Bluebird<any>;

getTopicName(cmd: string, nodeID?: string): string;
makeSubscriptions(topics: Array<GenericObject>): Bluebird<void>;
makeBalancedSubscriptions(): Bluebird<void>;
subscribe(cmd: string, nodeID?: string): Bluebird<void>;
subscribeBalancedRequest(action: string): Bluebird<void>;
subscribeBalancedEvent(event: string, group: string): Bluebird<void>;
Expand All @@ -535,6 +551,9 @@ declare namespace Moleculer {
publishBalancedEvent(packet: Packet, group: string): Bluebird<void>;
publishBalancedRequest(packet: Packet): Bluebird<void>;

getTopicName(cmd: string, nodeID?: string): string;
makeBalancedSubscriptions(): Bluebird<void>;

serialize(packet: Packet): Buffer;
deserialize(type: string, data: Buffer): Packet;
}
Expand All @@ -544,16 +563,16 @@ declare namespace Moleculer {
init(broker: ServiceBroker): void;
close(): Bluebird<any>;
get(key: string): Bluebird<null | GenericObject>;
set(key: string, data: any): Bluebird<any>;
set(key: string, data: any, ttl?: number): Bluebird<any>;
del(key: string): Bluebird<any>;
clean(match?: string): Bluebird<any>;
}

class Serializer {
constructor();
init(broker: ServiceBroker): void;
serialize(obj: GenericObject, type: string): string | Buffer;
deserialize(str: Buffer | string, type: string): string;
serialize(obj: GenericObject, type: string): Buffer;
deserialize(buf: Buffer, type: string): string;
}

class Validator {
Expand Down Expand Up @@ -648,7 +667,8 @@ declare namespace Moleculer {
JSON: Serializer,
Avro: Serializer,
MsgPack: Serializer,
ProtoBuf: Serializer
ProtoBuf: Serializer,
Thrift: Serializer
};

namespace Errors {
Expand All @@ -668,46 +688,55 @@ declare namespace Moleculer {
class MoleculerClientError extends MoleculerError { }

class ServiceNotFoundError extends MoleculerRetryableError {
constructor(action: string, nodeID: string);
constructor(action: string);
constructor(data: any);
}
class ServiceNotAvailableError extends MoleculerRetryableError {
constructor(action: string, nodeID: string);
constructor(action: string);
constructor(data: any);
}

class RequestTimeoutError extends MoleculerRetryableError {
constructor(action: string, nodeID: string);
constructor(data: any);
}
class RequestSkippedError extends MoleculerError {
constructor(action: string, nodeID: string);
constructor(data: any);
}
class RequestRejectedError extends MoleculerRetryableError {
constructor(action: string, nodeID: string);
constructor(data: any);
}

class QueueIsFullError extends MoleculerRetryableError {
constructor(action: string, nodeID: string, size: number, limit: number);
constructor(data: any);
}
class ValidationError extends MoleculerClientError {
constructor(message: string, type: string, data: GenericObject);
constructor(message: string, type: string);
constructor(message: string);
}
class MaxCallLevelError extends MoleculerError {
constructor(nodeID: string, level: number);
constructor(data: any);
}

class ServiceSchemaError extends MoleculerError {
constructor(message: string);
constructor(message: string, data: any);
}

class BrokerOptionsError extends MoleculerError {
constructor(message: string, data: any);
}

class GracefulStopTimeoutError extends MoleculerError {
constructor(data: any);
}

class ProtocolVersionMismatchError extends MoleculerError {
constructor(nodeID: string, actual: string, received: string);
constructor(data: any);
}

class InvalidPacketDataError extends MoleculerError {
constructor(type: string, packet: Packet);
constructor(data: any);
}


}

namespace Strategies {
Expand Down
10 changes: 4 additions & 6 deletions src/service-broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const defaultOptions = {
logger: null,
logLevel: null,
logFormatter: "default",
logObjectPrinter: null,

transporter: null, //"TCP",

Expand Down Expand Up @@ -206,9 +207,6 @@ class ServiceBroker {
this.call = this.callWithoutBalancer;
}

// Counter for metricsRate
this._sampleCount = 0;

// Register middlewares
this.registerMiddlewares(this.options.middlewares);

Expand Down Expand Up @@ -433,8 +431,7 @@ class ServiceBroker {
* Get a custom logger for sub-modules (service, transporter, cacher, context...etc)
*
* @param {String} module Name of module
* @param {String} service Service name
* @param {String|Number} version Service version
* @param {String|object} props Module properties (service name, version, ...etc
* @returns {Logger}
*
* @memberof ServiceBroker
Expand Down Expand Up @@ -1118,7 +1115,8 @@ class ServiceBroker {
/**
* Send ping to a node (or all nodes if nodeID is null)
*
* @param {String?} nodeID
* @param {String|Array<String>?} nodeID
* @param {Number?} timeout
* @returns {Promise}
* @memberof ServiceBroker
*/
Expand Down
Loading

0 comments on commit f2d281d

Please sign in to comment.