webhooks
Helper library for registering and triggering webhooks.
##Installation
npm install @testlio/webhooks
Usage
Changing webhook-service location and version
Default values for api
and version
are { api: 'https://api.testlio.com', version: 'v1' }
.
const webhooks = require('@testlio/webhooks');
webhooks.setConfig({
api: 'http://local.testlio.com',
version: 'v2',
authToken: 'Bearer eyJhbGciOiJIUzI1NiIsInR5c..'
});
Injecting promise dependency
If your environment doesn't support new Promise
you need to define promise dependency by yourself.
const webhooks = require('@testlio/webhooks');
webhooks.setPromisesDependency(require('Bluebird'));
GetHooks
Get one or more hooks. JWT token is required for authentication. Webhook-service is located using @testlio/discovered-request
.
const config = {
authToken: 'Bearer eyJhbGciOiJIUzI1NiIsInR5c..',
apiKey: 'bkayZOMvuy8aZOhIgxq94K9Oe7Y70Hw55'
};
const webhooks = require('@testlio/webhooks');
webhooks.setConfig(config);
const resource = 'https://api.testlio.com/issue/v1/collection/a692eef0-f122-4953-95c8-498c63a749ff';
const events = ['create', 'delete'];
webhooks.getHooks(resource, events).then(hooks => {
console.log(hooks);
}).catch(err => {
console.log(err);
});
Output
{
"create": { "href": "http://local.testlio:3030/hooks/9df2cb44132c5d12a9b24b8458ad238add4dea7f" },
"delete": { "href": "http://local.testlio:3030/hooks/f3d846d0d1d341caf13cb3cbb39ef303d05fb78b" }
}
TriggerEvents
Trigger events and receive receipts from the service. JWT token is required for authentication.
const config = {
authToken: 'Bearer eyJhbGciOiJIUzI1NiIsInR5c..',
apiKey: 'bkayZOMvuy8aZOhIgxq94K9Oe7Y70Hw55'
};
const webhooks = require('@testlio/webhooks');
webhooks.setConfig(config);
const resource = 'https://api.testlio.com/issue/v1/collection/a692eef0-f122-4953-95c8-498c63a749ff';
const events = ['create', 'delete'];
const payload = {
body: {...},
headers: {...}
};
webhooks.triggerEvents(resource, events, payload).then(response => {
console.log(response);
}).catch(err => {
console.log(err);
});
Output
{
"create": {
"hook": { "href": "http://local.testlio:3030/hooks/9df2cb44132c5d12a9b24b8458ad238add4dea7f" },
"receipt": { "href": "http://local.testlio:3030/events/90c26b7a-43a0-4c90-8b8e-a8ac204be0fc" }
},
"delete": {
"hook": { "href": "http://local.testlio:3030/hooks/f3d846d0d1d341caf13cb3cbb39ef303d05fb78b" },
"receipt": { "href": "http://local.testlio:3030/events/46dea6ed-23c1-4d43-b585-241ba573b3ac" }
}
}
ValidateSignature
Helper function for validating the signature of a received hook (the Authorization
header). Expects as parameters all available HTTP headers from the request, the payload of the request (as a String
) and the signing key (shared secret).
const headers = {
Authorization: ...,
X-testlio-callback: ...,
X-testlio-timestamp: ...
};
const payload = ...;
const signatureKey = ...;
webhooks.validateSignature(headers, payload, signatureKey); // Returns true if the signature that is generated matches the one that was provided