Skip to main content

User-defined services

Defining a service

The tarantula CLI can be utilized to generate scaffolding for services, which the user may then develop and use in their project. Below is an example of a basic service.

import { Service, WebTarantulaAPI } from "@demirtag/tarantula"

@Service //Use the @Service class decorator
export class ExampleService
{
constructor(private web: WebTarantulaAPI) {} //Use dependency injection in the constructor

serve() //Example method that the service offers
{
console.log("Hello from Example Service");
console.log(this.web.refresh());
}
}

Using the service

One can inject a service through a constructor in a test class just like one would do with an API.

@Test('Service Example')
export class Example {
//ExampleService gets injected through the constructor
constructor(private desktop: DesktopTarantulaAPI, private mobile: MobileTarantulaAPI, private ex: ExampleService) {
console.log(this.mobile.shake(500));
console.log(this.desktop.minimize());
this.ex.serve(); //The previously defined serve method is called
}
}