@everestate/serverless-router-dynamics ![npm version](https://badge.fury.io/js/%40everestate%2Fserverless-router-dynamics.svg)
Serverless Router plugin to handle Microsoft Dynamics Webhooks
Installation
npm install @everestate/serverless-router @everestate/serverless-router-dynamics --save
Usage
const Router = require('@everestate/serverless-router');
const Dynamics = require('@everestate/serverless-router-dynamics');
const leadService = require('../services/userService');
function dispatch(event) {
const router = new Router([Dynamics]);
router.dynamics
.create('lead', ({ id, entity }) => {
console.log(`Lead "${id} is created"`);
return leadService.create(id, entity);
})
.update('lead', ({ id, entity }) => {
console.log(`Lead "${id} is updated"`);
return leadService.update(id, entity);
})
.delete('lead', ({ id }) => {
console.log(`Lead "${id} is deleted"`);
return leadService.delete(id);
})
.assign('lead', ({ id, entity }) => {
console.log(`New agent assigned to lead with "id: ${id}"`);
return leadService.assign(id, entity);
});
router.mismatch(() => {
const {
'x-ms-dynamics-request-name': requestName,
'x-ms-dynamics-entity-name': entityName,
} = event.headers;
return Promise.reject(new Error(`Unknown route: ${requestName} ${entityName}`));
});
return router.dispatch(event);
}
function myLambdaHandler(event, context, callback) {
return dispatch(event)
.then(result =>
callback(null, { statusCode: result.code, body: JSON.stringify({ payload: result.payload }) }))
.catch(error =>
callback(null, { statusCode: '500', body: JSON.stringify({ message: error.message }) }));
}
By default serveless-router
will throw error
on route mismatch.
It's possible to define custom behaviour on mismatch.
License
MIT