@leisurelink/hapi-adaptor
v1.0.0
Published
Provides plugins common to leisurelink adaptors
Downloads
4
Keywords
Readme
@leisurelink/hapi-adaptor
Common plugins used in LeisureLink adaptors
Installation
npm install @leisurelink/hapi-adaptor
Usage
This module exposes several plugins. Because they rely on a multi-level dependency tree which hapi doesn't support very well, you should be sure to register the Configuration
plugin, then the MagicBus
plugin, and the EventPublisher
plugin before any others.
import { Configuration, MagicBus, EventPublisher ... } from '@leisurelink/hapi-adaptor';
Plugins
Configuration
Provides environment-based configuration values.
server.register(Configuration)
// ...
let value = server.plugins.configuration.get('config_key');
MagicBus
Creates a MagicBus broker
for use throughout your application. Requires that the Configuration
plugin has been loaded. The plugin's register function uses the following options:
- serviceDomainName - Passed to
magicbus.createBroker
- appName - Passed to
magicbus.createBroker
- connectionInfo - Passed to
magicbus.createBroker
- If not present, configuration.get('RABBITMQ_URL') is used
- If no RABBITMQ_URL value is present, then these options are used to build connection info:
- RABBITMQ_HOST
- RABBITMQ_PORT
- RABBITMQ_USER
- RABBITMQ_PASS
server.register(MagicBus, { appName: 'my-app' });
// ...
let connectionInfo = server.plugins.magicbus.connectionInfo
let broker = server.plugins.magicbus.broker;
Event Publisher
Provides an easy way to publish an event to magicbus. Requires that the MagicBus
plugin has been loaded.
server.register(EventPublisher)
// ...
server.plugins['event-publisher'].publish('my-event-type', { some: 'data' });
Hook Router
Publishes events that come in on a route to the magic bus. Requires that the EventPublisher
plugin has been loaded. The plugin's register function uses the following options:
- eventType - global event type used for all published events (can be overridden per event)
- routes - array of routes to listen on
- method - HTTP method (usually
'POST'
or'GET'
) - path - the path to register
- eventType - the event type to publish
- response - either a response object (passed to
reply
) or a function with the signaturefunction (request, reply)
that is called to generate a response. This should be a lightweight function only intended to generate a response, any work should be in the magicbus event handler. Defaults to { status: 'success' }
- method - HTTP method (usually
server.register(HookRouter, { eventType: 'my-adaptor-hooks', routes: [ { method: 'POST', path: '/hooks/leisurelink', response: 'OK' }] });
LeisureLink Client
Wraps the leisurelink-hub-client
npm package with initialization from the Configuration
plugin. Requires the Configuration
and EventPublisher
plugins be loaded. Will publish all API traffic to magic bus with the event type 'llapi-traffic'
, useful for logging api traffic. The plugin's register function uses the following options:
- apiKey - the api key to use for communicating with LeisureLink's API. If not present, retrieved from LEISURELINK_API_KEY
- url (optional) - the URL where LeisureLink's API is available. If not present, retrieved from LEISURELINK_HUB_URL or defaulted to client's default value.
- version (optional) - which version of the API to use. Defaults to
'v1'
server.register(LeisureLinkClient, { version: 'v1' });
//server.plugins['leisurelink-client'] contains all methods available on the instantiated client.
Mapping
Exposes functions used to retrieve mappings from the common ID mapping service (aka Bonanza). Requires the Configuration
plugin be loaded. The plugin's register function uses the following options:
- host - the host of the mapping service, like
https://mapping-service.leisurelink.local/
. If not present, retrieved from BONANZA_URL - keyId - the id of the key used to sign the requests. If not present, retrieved from KEY_ID
- key - a
Buffer
object containing the key to sign with. If not present, keyFile is used - keyFile - path to a key file used to sign requests. If not present, retrieved from KEY_FILE
server.register(Mapping);
//...
let connectionTypes = server.plugins.mapping.connectionTypes;
let mapping = await server.plugins.mapping.get(targetDomain, connectionType, mapping);
let connections = await server.plugins.mapping.getAll(targetDomain, connectionType);
Transmogrify
Exposes functions used to transform data. Eventually will call LeisureLink's transmogrify service, but just runs the transforms in process today. The plugin's register function uses the following options:
- transformPath - where the transform files are located.
server.register(Transmogrify, { transformPath: __dirname });
//...
let results = await server.plugins.transmogrify.executeAll(['data-to-availability', 'data-to-stay-restrictions']);
let avail = results['data-to-availability'];