Skip to main content

Calling TypeScript services from Java with Service Registry

As Red App developer you can call TypeScript services from java backend and receive a response.

In TypeScript, you can create new service by extending AbstractService class.

You have to add SERVICE_NAME that starts with web-module name (com-sabre-redapp-example3-desktop-serviceregistry-web-module in the example)

export class EchoService extends AbstractService {
  static SERVICE_NAME =
    "com-sabre-redapp-example3-desktop-serviceregistry-web-module-echo-service"; // (1)

  echo(text: string) {
    //...
  }
}
  1. Set service name

Next step is to register the service

//...
import { registerService } from "./Context";
import { cf } from "./Context";
//...
import { EchoService } from "./service/EchoService"; (1)

export class Main extends Module {
  init(): void {
    //...

    registerService(EchoService); (2)
  }

  //...
}
  1. Import created service

  2. Register service

In the last step, service has to be declared in src/manifest.json file in serviceRegistry field, under services array.

{
  "name": "com-sabre-redapp-example3-desktop-serviceregistry-web-module",
  "serviceRegistry": {
    "services": [
      {
        "name": "com-sabre-redapp-example3-desktop-serviceregistry-web-module-echo-service",
        "impl": "EchoService"
      }
    ]
  }
}
  1. Import created service

  2. Register service

After this setup service can be called from Java backend using RegistryServiceClient and corresponding RegistryServiceRequest where service and mehtod names can be set as well as parameters for given method.

Response from JavaScript is kept as String values - so primitives will be "stringified", arrays will have all elements separated by coma and put inside brackets ([ "x", "y", "z"]) and objects will be returned as JSON string.

Errors can be accessed via getErrors method on response

    @Override
    public FlowExtPointCommand execute(FlowExtPointCommand extPointCommand)
    {
        RegistryServiceClient client = new RegistryServiceClient(com);
        RegistryServiceRequest request = new RegistryServiceRequest(
            "com-sabre-redapp-example3-desktop-serviceregistry-web-module-echo-service",
            "echo",
            new String[]{"param from server"});

        ClientResponse <RegistryServiceResponse> resp =
            (ClientResponse <RegistryServiceResponse>) client.send(request);

        if (resp.getErrors().isEmpty())
        {
            System.out.println(resp.getPayload().getReturnedValue());
        }
        else
        {
            resp.getErrors().stream().map(error -> error.getDescription())
                .forEach(message -> System.out.println(message));
        }

        return extPointCommand;
    }

Working sample can be found in samples directory under com.sabre.redapp.example3.desktop.serviceregistry name.