Skip to content

Commit

Permalink
Add method to run webhook test (#181)
Browse files Browse the repository at this point in the history
* feat(webhooks): Add a method for webhook test

* test(webhook): Add test webhook tests

* chore: bump package version and add changelog

* chore: correct semver-minor bump
  • Loading branch information
jirimoravcik authored Jul 20, 2021
1 parent 63b88c5 commit 8e0be0d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.3.0 / 2021/07/15
===================
- Added new method `.test()` to the `WebhookClient` class

1.2.6 / 2021/06/21
===================
- Added gracefully parameter for abort run function
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apify-client",
"version": "1.2.6",
"version": "1.3.0",
"description": "Apify API client for JavaScript",
"main": "src/index.js",
"keywords": [
Expand Down
24 changes: 24 additions & 0 deletions src/resource_clients/webhook.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const ow = require('ow').default;
const ResourceClient = require('../base/resource_client');
const WebhookDispatchCollectionClient = require('./webhook_dispatch_collection');
const {
pluckData,
parseDateFields,
catchNotFoundOrThrow,
} = require('../utils');

/**
* @hideconstructor
Expand Down Expand Up @@ -42,6 +47,25 @@ class WebhookClient extends ResourceClient {
return this._delete();
}

/**
* https://docs.apify.com/api/v2#/reference/webhooks/webhook-test/test-webhook
* @return {Promise<WebhookDispatch>}
*/
async test() {
const request = {
url: this._url('test'),
method: 'POST',
params: this._params(),
};

try {
const response = await this.httpClient.call(request);
return parseDateFields(pluckData(response.data));
} catch (err) {
return catchNotFoundOrThrow(err);
}
}

/**
* https://docs.apify.com/api/v2#/reference/webhooks/dispatches-collection
* @return {WebhookDispatchCollectionClient}
Expand Down
1 change: 1 addition & 0 deletions test/mock_server/routes/webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ROUTES = [
{ id: 'get-webhook', method: 'GET', path: '/:webhookId' },
{ id: 'update-webhook', method: 'PUT', path: '/:webhookId' },
{ id: 'delete-webhook', method: 'DELETE', path: '/:webhookId' },
{ id: 'test-webhook', method: 'POST', path: '/:webhookId/test' },
{ id: 'list-dispatches', method: 'GET', path: '/:webhookId/dispatches' },

];
Expand Down
12 changes: 12 additions & 0 deletions test/webhooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ describe('Webhook methods', () => {
validateRequest({}, { webhookId });
});

test('test() works', async () => {
const webhookId = 'webhook_test_id';

const res = await client.webhook(webhookId).test();
expect(res.id).toEqual('test-webhook');
validateRequest({}, { webhookId });

const browserRes = await page.evaluate((id) => client.webhook(id).test(), webhookId);
expect(browserRes).toEqual(res);
validateRequest({}, { webhookId });
});

test('listDispatches() works', async () => {
const webhookId = 'webhook_id';
const opts = {
Expand Down

0 comments on commit 8e0be0d

Please sign in to comment.