Page Contents

Service proxy

Your API implementation often needs to interact with REST APIs, SOAP Web Services, gRPC microservices, or other forms of APIs.

To facilitate calling other APIs or web services, we introduce @loopback/service-proxy module to provide a common set of interfaces for interacting with backend services. You can create a Service class to provide a common set of interfaces for interacting with backend services.

There are 3 major steps:

  1. Add a datasource: specify the service you’re trying to connect
  2. Add a service: define how the operations/methods in the external APIs will be mapped to the service methods.
  3. Add a controller: expose the REST APIs that will call the service methods

Make service proxies easier to test

While @serviceProxy decorator makes it easy to use service proxies in controllers, it makes it difficult to write integration tests to verify that service proxies are correctly configured/implemented in respect to the most recent API provided by the backend service implementation. To make service proxies easy to test, we recommend to write a Provider class that will allow both controllers and integration tests to access the same service proxy implementation.

import {getService, juggler} from '@loopback/service-proxy';
import {inject, Provider} from '@loopback/core';
import {GeocoderDataSource} from '../datasources/geocoder.datasource';

export class GeoServiceProvider implements Provider<GeoService> {
  constructor(
    @inject('datasources.geoService')
    protected dataSource: juggler.DataSource = new GeocoderDataSource(),
  ) {}

  value(): Promise<GeocoderService> {
    return getService(this.dataSource);
  }
}

Troubleshooting

If you get the error about the app.serviceProvider() function is needed for ServiceBooter, make sure you apply ServiceMixin to your Application class in the application.ts.

export class MyLoopBackApplication extends BootMixin(
  ServiceMixin(RepositoryMixin(RestApplication)),
)

Please refer to Testing Your Application for guidelines on integration testing.

Try it out