Add a 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:
npm install @loopback/service-proxy -—save
Writing a service provider
Use the lb4 service
command and the following inputs to create a calculatorservice:
lb4 service
? Please select the datasource CalculatorDatasource
? Service name: Calculator
create src/services/calculator.service.ts
update src/services/index.ts
Service Calculator was created in src/services/
src/services/calculator.service.ts
import {getService} from '@loopback/service-proxy';
import {inject, Provider} from '@loopback/core';
import {CalculatorDataSource} from '../datasources';
export interface CalculatorService {
// this is where you define the Node.js methods that will be
// mapped to the SOAP operations as stated in the datasource
// json file.
}
export class CalculatorServiceProvider implements Provider<CalculatorService> {
constructor(
// calculator must match the name property in the datasource json file
@inject('datasources.calculator')
protected dataSource: CalculatorDataSource = new CalculatorDataSource(),
) {}
value(): Promise<CalculatorService> {
return getService(this.dataSource);
}
}
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:
export interface MultiplyResponse {
result: {
value: number;
};
}
export interface AddResponse {
result: {
value: number;
};
}
export interface SubtractResponse {
result: {
value: number;
};
}
export interface DivideResponse {
result: {
value: number;
};
}
export interface CalculatorParameters {
intA: number;
intB: number;
}
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:
export interface CalculatorService {
multiply(args: CalculatorParameters): Promise<MultiplyResponse>;
add(args: CalculatorParameters): Promise<AddResponse>;
divide(args: CalculatorParameters): Promise<DivideResponse>;
subtract(args: CalculatorParameters): Promise<SubtractResponse>;
}
Navigation
Previous step: Add a Datasource
Next step: Add a Controller