Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Return webhook registration operation
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomarg committed Jun 2, 2023
1 parent 295fd3e commit 1867110
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-crabs-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/shopify-api': patch
---

Return the performed operation for each handler when registering webhooks
12 changes: 12 additions & 0 deletions docs/reference/webhooks/register.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,24 @@ Returns an object containing a list of results, indexed by topic. Each entry in

Whether the registration was successful.

### deliveryMethod

`string`

The configured delivery method for the registered webhook.

### result

`array`

The body from the Shopify request to register the webhook.

### operation

`WebhookOperation`

Which operation was performed to obtain this result.

> **Note**: This object will only contain results for a handler if any of its information was updated with Shopify.
[Back to shopify.webhooks](./README.md)
41 changes: 39 additions & 2 deletions lib/webhooks/__tests__/register.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import {Method, Header} from '@shopify/network';

import {RegisterParams, RegisterReturn, WebhookHandler} from '../types';
import {
RegisterParams,
RegisterReturn,
WebhookHandler,
WebhookOperation,
} from '../types';
import {gdprTopics, ShopifyHeader} from '../../types';
import {DataType} from '../../clients/types';
import {queueMockResponse, shopify} from '../../__tests__/test-helper';
import {
queueMockResponse,
queueMockResponses,
shopify,
} from '../../__tests__/test-helper';
import {mockTestRequests} from '../../../adapters/mock/mock_test_requests';
import {queryTemplate} from '../query-template';
import {TEMPLATE_GET_HANDLERS, TEMPLATE_MUTATION} from '../register';
Expand Down Expand Up @@ -357,6 +366,34 @@ describe('shopify.webhooks.register', () => {
);
assertRegisterResponse({registerReturn, topic, responses});
});

it('returns which operation was done for each handler', async () => {
shopify.webhooks.addHandlers({
PRODUCTS_UPDATE: {...HTTP_HANDLER, includeFields: ['id', 'title']},
});
shopify.webhooks.addHandlers({
PRODUCTS_DELETE: HTTP_HANDLER,
});

queueMockResponses(
[JSON.stringify(mockResponses.webhookCheckMultiHandlerResponse)],
[JSON.stringify(mockResponses.successResponse)],
[JSON.stringify(mockResponses.successUpdateResponse)],
[JSON.stringify(mockResponses.successDeleteResponse)],
);

const registerReturn = await shopify.webhooks.register({session});

expect(registerReturn.PRODUCTS_CREATE[0].operation).toEqual(
WebhookOperation.Delete,
);
expect(registerReturn.PRODUCTS_DELETE[0].operation).toEqual(
WebhookOperation.Create,
);
expect(registerReturn.PRODUCTS_UPDATE[0].operation).toEqual(
WebhookOperation.Update,
);
});
});

async function registerWebhook({
Expand Down
2 changes: 2 additions & 0 deletions lib/webhooks/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,15 @@ async function runMutation({
deliveryMethod: handler.deliveryMethod,
success: isSuccess(result.body, handler, operation),
result: result.body,
operation,
};
} catch (error) {
if (error instanceof InvalidDeliveryMethodError) {
registerResult = {
deliveryMethod: handler.deliveryMethod,
success: false,
result: {message: error.message},
operation,
};
} else {
throw error;
Expand Down
1 change: 1 addition & 0 deletions lib/webhooks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface RegisterResult {
success: boolean;
deliveryMethod: DeliveryMethod;
result: unknown;
operation: WebhookOperation;
}

export interface RegisterReturn {
Expand Down

0 comments on commit 1867110

Please sign in to comment.