roccomuso / node-webhooks

:arrow_right_hook: Node.js module to create and trigger your own webHooks.
190 stars 47 forks source link

additional data on trigger event #14

Open mrbraca15 opened 7 years ago

mrbraca15 commented 7 years ago

hello i need to send a additional data on trigger event for use it after on succes event, for example:

webHooks.trigger(shortName, payloadData, additionalData);

emitter.on('*.success', (shortname, statusCode, body, additionalData)=> {
            //here i need to use the data that i send as parameter in the trigger event
           //how can i do that?
});

thanks

roccomuso commented 7 years ago

You can't do this:

webHooks.trigger(shortName, payloadData, additionalData);

The third parameter is reserved for the headers. If you wanna pass more data, just add it as an additional property of your payloadData object.

Example:

webHooks.trigger(shortName, {myData: 'hello', moreData: 'even more data'});

emitter.on('*.success', (shortname, statusCode, body)=> {
         var myData = body.myData
         var moreData = body.moreData
         // ...
});
mrbraca15 commented 7 years ago

hello thanks for the answer but in the body parameter i received the request response of the url configured in the shorname.

roccomuso commented 7 years ago

Ok, I was supposing that the web-server "mirrors" your body request. You can't manipulate the server's response. It's not either a good practice.

kurdi89 commented 3 years ago
webHooks.trigger(shortName, {myData: 'hello', moreData: 'even more data'});

emitter.on('*.success', (shortname, statusCode, body)=> {
         var myData = body.myData
         var moreData = body.moreData
         // ...
});

did the above but the body contains the response body, any help to track what have been sent from my side as in the request body,

thank you

kurdi89 commented 3 years ago

this how i am triggering the webhook :

// dispatch the webhook : 
    await webHooks.trigger('OrderCreated', {data: data}, {Authorization: Authorization});

and here is the function I am using to log all the webhooks dispatched :

emitter.on('*.failure', async function (shortname, statusCode, body) {
  let TIMESTAMP = new Date().toLocaleString("en-US", {timeZone: "Asia/Riyadh"});
  let DATE = new Date().toLocaleDateString("en-US", {timeZone: "Asia/Riyadh"}).replace("/","-").replace("/","-").substring(0,9);
  console.log(colors.error('ERROR on trigger webhook ' + shortname + ' with status code', statusCode, 'and body', body , 'At : ',TIMESTAMP))

    let obj = { 
      shortname,
      statusCode,
      TIMESTAMP,
      body,
    }
    // console.log({obj})
    await (async () => {
        let event = []
        event.push({} = obj)
        const csv = new ObjectsToCsv(event);

        // Save to file:
        await csv.toDisk(`./logs/FAILED-WEBHOOKS-${DATE}-${shortname}.csv`, { append: true });

        // Return the CSV file as string:
        // console.log(await csv.toString());
    })();
})

anyway to track the body of the request I am dispatching, the data object, thanks again

roccomuso commented 3 years ago

it's up to your app logic to keep track of what you're dispatching.