Options
All
  • Public
  • Public/Protected
  • All
Menu

Contract for a service, defines methods that can be invoked and events that can be listened to.

const EchoService = new ServiceContract()
.defineMethod('echo', {
returnType: stringType,
parameters: [
{
name: 'message',
type: stringType
}
]
});

For TypeScript contracts are typed and can be used with interfaces to help guide in their definition:

interface EchoService {
echo(message: string): Promise<string>;
}

const EchoService = new ServiceContract<EchoService>()
.defineMethod('echo', {
returnType: stringType,
parameters: [
{
name: 'message',
type: stringType
}
]
});

Defining methods

Methods are defined with {@link defineMethod} and need to provide the name of the method, its return type and parameters.

For methods that return nothing voidType should be used:

.defineMethod('methodWithoutReturnValue', {
returnType: voidType,
parameters: []
})

Defining events

Events are defined with {@link defineEvent} and should provide information about the parameters the event emits. For implementations AsyncEvent is assumed to be used.

const contract = new ServiceContract()
.defineEvent('onEcho', {
parameters: [
{
name: 'message',
type: stringType
}
]
});

class Impl {
constructor() {
this.echoEvent = new AsyncEvent(this);
}

get onEcho() {
return this.echoEvent.subscribable;
}
}

Using contracts

When using classes the recommended way to mark what contract a class implements is to use the serviceContract decorator:

@serviceContract(EchoService)
class EchoServiceImpl {
async echo(message) {
return message;
}
}

If the decorator is not used you can define a static property called serviceContract instead:

class EchoServiceImpl {
static serviceContract = EchoService;

async echo(message) {
return message;
}
}

Contracts will traverse the prototype chain, so defining contract on extended classes work well:

@serviceContract(EchoService)
class AbstractEchoService {
async echo(message) {
return message;
}
}

class EchoServiceImpl extends AbstractEchoService {
}

For plain objects the easiest way to use a contract is to use implement:

const instance = EchoService.implement({
async echo(message) {
return message;
}
});

As with classes a property may be used instead:

const instance = {
serviceContract: EchoService,

async echo(message) {
return message;
}
};

Type parameters

  • T: object

Hierarchy

  • ServiceContract

Index

Constructors

constructor

Accessors

events

methods

Methods

describeEvent

describeMethod

getEvent

getMethod

implement

  • implement(handler: T): T

Static get

Static merge

Generated using TypeDoc