Add a Service

soap-calculator-add-service

LB4 has the package @loopback/proxy-server that contains the artifactsneeded to implement the link between the methods described in the .json file andthe Node.js methods. All we need to do is to write the service provider thatwill serve as the glue to make this implementation real.

Installing the proxy-server

Make sure you are inside the soap-calculator directory and run the followingcommand:

  1. npm install @loopback/service-proxy -—save

Writing a service provider

Use the lb4 service command and the following inputs to create a calculatorservice:

  1. lb4 service
  2. ? Please select the datasource CalculatorDatasource
  3. ? Service name: Calculator
  4. create src/services/calculator.service.ts
  5. update src/services/index.ts
  6. Service Calculator was created in src/services/

src/services/calculator.service.ts

  1. import {getService} from '@loopback/service-proxy';
  2. import {inject, Provider} from '@loopback/core';
  3. import {CalculatorDataSource} from '../datasources';
  4. export interface CalculatorService {
  5. // this is where you define the Node.js methods that will be
  6. // mapped to the SOAP operations as stated in the datasource
  7. // json file.
  8. }
  9. export class CalculatorServiceProvider implements Provider<CalculatorService> {
  10. constructor(
  11. // calculator must match the name property in the datasource json file
  12. @inject('datasources.calculator')
  13. protected dataSource: CalculatorDataSource = new CalculatorDataSource(),
  14. ) {}
  15. value(): Promise<CalculatorService> {
  16. return getService(this.dataSource);
  17. }
  18. }

Adding our interfaces

When we reviewed the remote SOAP web service, we found that there were fourdifferent results for the four operations and each of these operations wereexpecting the same pair of arguments intA and intB. Now, it is time to definethis scenario using interfaces as follows:

  1. export interface MultiplyResponse {
  2. result: {
  3. value: number;
  4. };
  5. }
  6. export interface AddResponse {
  7. result: {
  8. value: number;
  9. };
  10. }
  11. export interface SubtractResponse {
  12. result: {
  13. value: number;
  14. };
  15. }
  16. export interface DivideResponse {
  17. result: {
  18. value: number;
  19. };
  20. }
  21. export interface CalculatorParameters {
  22. intA: number;
  23. intB: number;
  24. }

One important interface we need to add now is the one that describes the fourNode.js methods that will be mapped to the SOAP operations. At this point wehave just mentioned them in the .json data source file, so let’s add them now asfollows:

  1. export interface CalculatorService {
  2. multiply(args: CalculatorParameters): Promise<MultiplyResponse>;
  3. add(args: CalculatorParameters): Promise<AddResponse>;
  4. divide(args: CalculatorParameters): Promise<DivideResponse>;
  5. subtract(args: CalculatorParameters): Promise<SubtractResponse>;
  6. }

Navigation

Previous step: Add a Datasource

Next step: Add a Controller