Skip to content

Commit

Permalink
Merge pull request #1219 from openintegrationhub/fix-payload-format
Browse files Browse the repository at this point in the history
Fix webhooks message format
  • Loading branch information
heggert committed Mar 10, 2021
2 parents e7f8c7f + 502cf13 commit 00d8524
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/component-orchestrator/package.json
@@ -1,6 +1,6 @@
{
"name": "@openintegrationhub/component-orchestrator",
"version": "1.2.4",
"version": "1.2.5",
"description": "Orchestrates cluster resources",
"main": "src/index.js",
"scripts": {
Expand Down
12 changes: 11 additions & 1 deletion lib/component-orchestrator/src/ComponentOrchestrator.js
Expand Up @@ -341,7 +341,6 @@ class ComponentOrchestrator {
apiKey,
msg,
}) {

const nodeProperties = flow.getPropertiesByNodeId(stepId)

const functionName = nodeProperties.function
Expand Down Expand Up @@ -378,6 +377,17 @@ class ComponentOrchestrator {
metadata: msg.metadata || {},
};

// add webhook specific keys if method exists
if (msg.method) {
newMessage.method = msg.method;
newMessage.headers = msg.headers || {};
newMessage.params = msg.params || {};
newMessage.query = msg.query || {};
newMessage.url = msg.url || '';
newMessage.originalUrl = msg.originalUrl || '';
newMessage.pathSuffix = msg.pathSuffix || '';
}

let config;

if (component.isGlobal) {
Expand Down
2 changes: 1 addition & 1 deletion lib/webhooks/package.json
@@ -1,7 +1,7 @@
{
"name": "@openintegrationhub/webhooks",
"description": "webhooks",
"version": "1.2.0",
"version": "1.2.1",
"author": "Open Integration Hub",
"engines": {
"node": ">=12"
Expand Down
8 changes: 4 additions & 4 deletions lib/webhooks/spec/http-api.spec.js
Expand Up @@ -105,7 +105,7 @@ describe('HttpApi', () => {
expect(msgArg).to.be.a('object');
expect(msgArg).to.to.have.all.keys([
'attachments',
'body',
'data',
'headers',
'id',
'metadata',
Expand All @@ -123,7 +123,7 @@ describe('HttpApi', () => {
expect(msgArg.url).to.equal('/hook/123?a=b');
expect(msgArg.params).to.deep.equal({});
expect(msgArg.query).to.deep.equal({a: 'b'});
expect(msgArg.body).to.deep.equal({
expect(msgArg.data).to.deep.equal({
a: 'b'
});
expect(msgArg.id).to.be.a('string');
Expand Down Expand Up @@ -197,7 +197,7 @@ describe('HttpApi', () => {
expect(msgArg).to.be.a('object');
expect(msgArg).to.to.have.all.keys([
'attachments',
'body',
'data',
'headers',
'id',
'metadata',
Expand All @@ -215,7 +215,7 @@ describe('HttpApi', () => {
expect(msgArg.url).to.equal('/hook/123?a=b');
expect(msgArg.params).to.deep.equal({});
expect(msgArg.query).to.deep.equal({a: 'b'});
expect(msgArg.body).to.deep.equal({
expect(msgArg.data).to.deep.equal({
some: 'data'
});
expect(msgArg.id).to.be.a('string');
Expand Down
6 changes: 3 additions & 3 deletions lib/webhooks/spec/request-handlers/get.spec.js
Expand Up @@ -88,7 +88,7 @@ describe('Get Request Handler', () => {
delete msg.id;
expect(msg).to.deep.equal({
attachments: {},
body: {
data: {
some: 'query'
},
headers: {
Expand Down Expand Up @@ -259,7 +259,7 @@ describe('Get Request Handler', () => {
expect(msg).to.be.a('object');
expect(msg).to.to.have.all.keys([
'attachments',
'body',
'data',
'headers',
'id',
'metadata',
Expand All @@ -277,7 +277,7 @@ describe('Get Request Handler', () => {
expect(msg.url).to.equal('/?a=b');
expect(msg.params).to.deep.equal({});
expect(msg.query).to.deep.equal({a: 'b'});
expect(msg.body).to.deep.equal({a: 'b'});
expect(msg.data).to.deep.equal({a: 'b'});
expect(msg.id).to.be.a('string');
expect(msg.headers).to.have.all.keys([
'accept-encoding',
Expand Down
6 changes: 3 additions & 3 deletions lib/webhooks/spec/request-handlers/post.spec.js
Expand Up @@ -91,7 +91,7 @@ describe('Post Request Handler', () => {
delete msg.id;
expect(msg).to.deep.equal({
attachments: {},
body: {
data: {
some: 'body'
},
headers: {
Expand Down Expand Up @@ -262,7 +262,7 @@ describe('Post Request Handler', () => {
expect(msg).to.be.a('object');
expect(msg).to.to.have.all.keys([
'attachments',
'body',
'data',
'headers',
'id',
'metadata',
Expand All @@ -280,7 +280,7 @@ describe('Post Request Handler', () => {
expect(msg.url).to.equal('/?a=b');
expect(msg.params).to.deep.equal({});
expect(msg.query).to.deep.equal({a: 'b'});
expect(msg.body).to.deep.equal({
expect(msg.data).to.deep.equal({
data: {
some: 'payload'
}
Expand Down
30 changes: 23 additions & 7 deletions lib/webhooks/src/request-handlers/post.js
@@ -1,6 +1,5 @@
const _ = require('lodash');
const BaseHandler = require('./base');
const { messages } = require('elasticio-node'); //@todo: replace with OIH???
const MessagePublishers = require('../message-publishers');
const assert = require('assert');
const uuid = require('uuid/v1');
Expand All @@ -14,6 +13,19 @@ const REQUEST_FIELDS = [
'body'
];

function newEmptyMessage() {

var msg = {
id: uuid(),
attachments: {},
data: {},
headers: {},
metadata: {}
};

return msg;
}

/**
* Configuration object for a client response.
* @typedef {Object} ClientResponseConfig
Expand Down Expand Up @@ -70,10 +82,14 @@ class PostHandler extends BaseHandler {
const log = this.getLogger();
log.trace('Creating webhook msg from request');

const msg = messages.newEmptyMessage();
const msg = newEmptyMessage();

_.each(REQUEST_FIELDS, (key) => {
msg[key] = req[key];
if (key === 'body') {
msg.data = req[key];
} else {
msg[key] = req[key];
}
});

msg.params = {};
Expand All @@ -85,13 +101,13 @@ class PostHandler extends BaseHandler {
//extracting pathSuffix from url that may contain get parameters
msg.pathSuffix = req.path.replace(new RegExp(`/hook/${req.params.taskId}`, 'g'), '');

if (typeof msg.body === 'string' || Buffer.isBuffer(msg.body)) {
// Body should be JSON object and not string and not buffer
msg.body = {};
if (typeof msg.data === 'string' || Buffer.isBuffer(msg.data)) {
// data should be JSON object and not string and not buffer
msg.data = {};
}

if (req.method === 'GET') {
msg.body = req.query;
msg.data = req.query;
}

log.debug({ msg: msg }, 'Message to be pushed to the webhook queue');
Expand Down
2 changes: 1 addition & 1 deletion services/component-orchestrator/package.json
@@ -1,6 +1,6 @@
{
"name": "component-orchestrator",
"version": "1.2.6",
"version": "1.2.7",
"description": "Resource coordinator",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion services/webhooks/package.json
@@ -1,6 +1,6 @@
{
"name": "webhooks",
"version": "1.1.1",
"version": "1.1.2",
"description": "Standalone version of elastic.io platform",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 00d8524

Please sign in to comment.