1.10.0 • Published 3 years ago

pelias-microservice-wrapper v1.10.0

Weekly downloads
207
License
MIT
Repository
github
Last release
3 years ago

Pelias Microservice Wrapper

Overview

Module that provides a convenience wrapper for calling Pelias microservices such as Placeholder or Point-in-Polygon Service over HTTP GET.

Installation

$ npm install pelias-microservice-wrapper

NPM

NPM Module

The pelias-microservice-wrapper npm module can be found here:

https://npmjs.org/package/pelias-microservice-wrapper

Usage

This module is primarily used in the Pelias API to call microservices. To add support for a microservice in the API, define a class that derives from ServiceConfiguration, override any methods, and inject into a service instance.

The ServiceConfiguration constructor accepts a string service name (returned by getName() and an object containing the three optional properties:

propertyrequireddefaultdescription
baseUrlnononethe base URL used for contacting a service, causes isEnabled() to return false when an empty string
timeoutno1000the number of milliseconds a request should wait for a server response before timing out
retriesno3the number of retries to attempt before returning an error

Requests are logged at the debug level when enabled in pelias-config.

Example

const DemoServiceConfig = class extends ServiceConfiguration {
  constructor(configBlob) {
    super('demo microservice', configBlob);
  }
  getUrl(req) {
    return this.baseUrl + '/demo';
  }
  getParameters(req) {
    return {
      size: req.clean.size,
      offset: 0
    };
  }
  getHeaders(req) {
    return {
      'some-header': req.clean.some_header
    };
  }
};

const demoService = serviceWrapper(new DemoServiceConfig({
  url: 'http://localhost:1234'
}));

const req = {
  size: 15,
  some_header: 'header value'
};

// pseudocode tests for illustration purposes
demoService.getUrl() === 'http://localhost:1234/demo';

demoService.isEnabled() === true

demoService.getParameters(req) === {
  size: 15,
  offset: 0
};

demoService.getHeaders(req) === {
  some_header: 'header value'
};

Methods

methodoverride?returnsdescription
getNamenot recommendedvalue passed to constructorreturns the name of the service
getBaseUrlnot recommendedurl property of configuration passed to constructorbase URL of microservice
isEnablednot recommendedtrue if baseUrl is a non-empty stringhelper method for determining if the service should be considered enabled
getUrlyesvalue of getBaseUrl unless overriddenused for appending other value to the baseUrl (but not request parameters)
getParametersyes{} unless overriddenany request parameters to pass to the microservice
getHeadersyes{} unless overriddenany request headers to pass to the microservice
getTimeoutnot recommended1000 or value passed to constructorhow long a request should wait for a server response before timing out
getRetriesnot recommended3 or value passed to constructorhow many attempts should be made before returning an error

Recommended ServiceConfiguration Method Overrides

The following methods can be overridden in an implementation of ServiceConfiguration:

methoddefault returndescription
getUrlvalue of getBaseUrlused for appending other value to the baseUrl (but not request parameters)
getParameters{}any request parameters to pass to the microservice
getHeaders{}any request headers to pass to the microservice

It is not recommended to override any other methods.

1.10.0

3 years ago

1.9.0

4 years ago

1.8.3

5 years ago

1.8.2

5 years ago

1.8.1

5 years ago

1.8.0

5 years ago

1.7.1

5 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago