Create a new service
If you are here, you probably have a very great idea for Twake, like adding a brand new feature into Twake, maybe a coffee maker service ? ☕️
Please ensure you read the Start working into a service before
To create a new component, a new folder must be created under the src/services
one and an index.ts
file must export the a class. This class will be instantiated by the platform and will be linked to the required services automatically.
In order to illustrate how to create a component, let's create a fake Notification service.
Create the folder
src/services/notification
Create an
index.ts
file which exports aNotificationService
class
Our
NotificationService
class extends the genericTwakeService
class and we defined theNotificationServiceAPI
as its generic type parameter. It means that in the platform, the other components will be able to retrieve the component from its name and then consume the API defined in theNotificationServiceAPI
interface and exposed by theapi
method.We need to create this
NotificationServiceAPI
interface which must extend theTwakeServiceProvider
from the platform like:
Now that the interfaces are defined, we need to create the
NotificationServiceAPI
implementation (this is a dummy implementation which does nothing but illustrates the process):
NotificationServiceImpl
now needs to be instanciated from theNotificationService
class since this is where we choose to keep its reference and expose it. There are several places which can be used to instanciate it, in the constructor itself, or in one of theTwakeService
lifecycle hooks. TheTwakeService
abstract class has several lifecycle hooks which can be extended by the service implementation for customization pusposes:public async doInit(): Promise<this>;
Customize theinit
step of the component. This is generally the place where services are instanciated. From this step, you can retrieve services consumed by the current component which have been already initialized by the platform.public async doStart(): Promise<this>;
Customize thestart
step of the component. You have access to all other services which are already started.
Now that the service is fully created, we can consume it from any other service in the platform. To do this, we rely on Typescript decorators to define the links between components. For example, let's say that the a
MessageService
needs to call theNotificationServiceAPI
, we can create the link with the help of the@Consumes
decorator and get a reference to theNotificationServiceAPI
by calling thegetProvider
on the component context like:
Configuration
The platform and services configuration is defined in the config/default.json
file. It uses node-config under the hood and to configuration file inheritence is supported in the platform.
The list of services to start is defined in the services
array like:
Then each service can have its own configuration block which is accessible from its service name i.e. websocket
service configuration is defined in the websocket
element like:
On the component class side, the configuration object is directly accessible from the configuration
property like:
After creating a new service, you can add controllers, business services and entities, go back to the What is a service section
Create a new technical service
Now you are bringing things a step further, you are going to add new core services in Twake, like for instance a new database connector or encryption system.
Creating a new core service is as easy as creating a functional service. But it must be in src/core/platform/services
.
You can read the complete list of existing technical services here .
Last updated