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 ? ☕️
Last updated
Was this helpful?
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 ? ☕️
Last updated
Was this helpful?
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 a NotificationService
class
Our NotificationService
class extends the generic TwakeService
class and we defined the NotificationServiceAPI
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 the NotificationServiceAPI
interface and exposed by the api
method.
We need to create this NotificationServiceAPI
interface which must extend the TwakeServiceProvider
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 the NotificationService
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 the TwakeService
lifecycle hooks. The TwakeService
abstract class has several lifecycle hooks which can be extended by the service implementation for customization pusposes:
public async doInit(): Promise<this>;
Customize the init
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 the start
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 the NotificationServiceAPI
, we can create the link with the help of the @Consumes
decorator and get a reference to the NotificationServiceAPI
by calling the getProvider
on the component context like:
Configuration
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:
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
.
The platform and services configuration is defined in the config/default.json
file. It uses under the hood and to configuration file inheritence is supported in the platform.
After creating a new service, you can add controllers, business services and entities, go back to the
You can read the .