🐛 Fix error when checking if webhook exists (#1129)

* 🐛 Fix error when checking if webhook exists

* zap: Small fix
This commit is contained in:
Ricardo Espinoza 2020-11-07 13:44:13 -05:00 committed by GitHub
parent a6cf9bceb3
commit 96cffdfd1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 87 deletions

View file

@ -24,7 +24,7 @@ export async function shopifyApiRequest(this: IHookFunctions | IExecuteFunctions
throw new Error('No credentials got returned!'); throw new Error('No credentials got returned!');
} }
const headerWithAuthentication = Object.assign({}, const headerWithAuthentication = Object.assign({},
{ Authorization: ` Basic ${Buffer.from(`${credentials.apiKey}:${credentials.password}`).toString(BINARY_ENCODING)}` }); { Authorization: `Basic ${Buffer.from(`${credentials.apiKey}:${credentials.password}`).toString(BINARY_ENCODING)}` });
const options: OptionsWithUri = { const options: OptionsWithUri = {
headers: headerWithAuthentication, headers: headerWithAuthentication,
@ -47,6 +47,7 @@ export async function shopifyApiRequest(this: IHookFunctions | IExecuteFunctions
try { try {
return await this.helpers.request!(options); return await this.helpers.request!(options);
} catch (error) { } catch (error) {
console.log(error.response.body);
if (error.response.body && error.response.body.errors) { if (error.response.body && error.response.body.errors) {
let message = ''; let message = '';
if (typeof error.response.body.errors === 'object') { if (typeof error.response.body.errors === 'object') {

View file

@ -25,6 +25,7 @@ export class ShopifyTrigger implements INodeType {
icon: 'file:shopify.png', icon: 'file:shopify.png',
group: ['trigger'], group: ['trigger'],
version: 1, version: 1,
subtitle: '={{$parameter["event"]}}',
description: 'Handle Shopify events via webhooks', description: 'Handle Shopify events via webhooks',
defaults: { defaults: {
name: 'Shopify Trigger', name: 'Shopify Trigger',
@ -55,271 +56,268 @@ export class ShopifyTrigger implements INodeType {
options: options:
[ [
{ {
name: 'App uninstalled', name: 'App Uninstalled',
value: 'app/uninstalled', value: 'app/uninstalled',
}, },
{ {
name: 'Carts create', name: 'Cart Created',
value: 'carts/create', value: 'carts/create',
}, },
{ {
name: 'Carts update', name: 'Cart Updated',
value: 'carts/update', value: 'carts/update',
}, },
{ {
name: 'Checkouts create', name: 'Checkout Created',
value: 'checkouts/create', value: 'checkouts/create',
}, },
{ {
name: 'Checkouts delete', name: 'Checkout Delete',
value: 'checkouts/delete', value: 'checkouts/delete',
}, },
{ {
name: 'Checkouts update', name: 'Checkout Update',
value: 'checkouts/update', value: 'checkouts/update',
}, },
{ {
name: 'Collection listings add', name: 'Collection Listings Added',
value: 'collection_listings/add', value: 'collection_listings/add',
}, },
{ {
name: 'Collection listings remove', name: 'Collection Listings Removed',
value: 'collection_listings/remove', value: 'collection_listings/remove',
}, },
{ {
name: 'Collection listings update', name: 'Collection Listings Updated',
value: 'collection_listings/update', value: 'collection_listings/update',
}, },
{ {
name: 'Collections create', name: 'Collection Created',
value: 'collections/create', value: 'collections/create',
}, },
{ {
name: 'Collections delete', name: 'Collection Deleted',
value: 'collections/delete', value: 'collections/delete',
}, },
{ {
name: 'Collections update', name: 'Collection Updated',
value: 'collections/update', value: 'collections/update',
}, },
{ {
name: 'Customer groups create', name: 'Customer Groups Created',
value: 'customer_groups/create', value: 'customer_groups/create',
}, },
{ {
name: 'Customer groups delete', name: 'Customer Groups Deleted',
value: 'customer_groups/delete', value: 'customer_groups/delete',
}, },
{ {
name: 'Customer groups update', name: 'Customer Groups Updated',
value: 'customer_groups/update', value: 'customer_groups/update',
}, },
{ {
name: 'Customers create', name: 'Customer Created',
value: 'customers/create', value: 'customers/create',
}, },
{ {
name: 'Customers delete', name: 'Customer Deleted',
value: 'customers/delete', value: 'customers/delete',
}, },
{ {
name: 'Customers disable', name: 'Customer disabled',
value: 'customers/disable', value: 'customers/disable',
}, },
{ {
name: 'Customers enable', name: 'Customer Enabled',
value: 'customers/enable', value: 'customers/enable',
}, },
{ {
name: 'Customers update', name: 'Customer Updated',
value: 'customers/update', value: 'customers/update',
}, },
{ {
name: 'Draft orders create', name: 'Draft Orders Created',
value: 'draft_orders/create', value: 'draft_orders/create',
}, },
{ {
name: 'Draft orders delete', name: 'Draft Orders Deleted',
value: 'draft_orders/delete', value: 'draft_orders/delete',
}, },
{ {
name: 'Draft orders update', name: 'Draft orders Updated',
value: 'draft_orders/update', value: 'draft_orders/update',
}, },
{ {
name: 'Fulfillment events create', name: 'Fulfillment Events Created',
value: 'fulfillment_events/create', value: 'fulfillment_events/create',
}, },
{ {
name: 'Fulfillment events delete', name: 'Fulfillment Events Deleted',
value: 'fulfillment_events/delete', value: 'fulfillment_events/delete',
}, },
{ {
name: 'Fulfillments create', name: 'Fulfillment created',
value: 'fulfillments/create', value: 'fulfillments/create',
}, },
{ {
name: 'Fulfillments update', name: 'Fulfillment Updated',
value: 'fulfillments/update', value: 'fulfillments/update',
}, },
{ {
name: 'Inventory_items create', name: 'Inventory Items Created',
value: 'inventory_items/create', value: 'inventory_items/create',
}, },
{ {
name: 'Inventory_items delete', name: 'Inventory Items Deleted',
value: 'inventory_items/delete', value: 'inventory_items/delete',
}, },
{ {
name: 'Inventory_items update', name: 'Inventory Items Updated',
value: 'inventory_items/update', value: 'inventory_items/update',
}, },
{ {
name: 'Inventory_levels connect', name: 'Inventory Levels Connected',
value: 'inventory_levels/connect', value: 'inventory_levels/connect',
}, },
{ {
name: 'Inventory_levels disconnect', name: 'Inventory Levels Disconnected',
value: 'inventory_levels/disconnect', value: 'inventory_levels/disconnect',
}, },
{ {
name: 'Inventory_levels update', name: 'Inventory Levels Updated',
value: 'inventory_levels/update', value: 'inventory_levels/update',
}, },
{ {
name: 'Locales create', name: 'Locale Created',
value: 'locales/create', value: 'locales/create',
}, },
{ {
name: 'Locales update', name: 'Locale Updated',
value: 'locales/update', value: 'locales/update',
}, },
{ {
name: 'Locations create', name: 'Location Created',
value: 'locations/create', value: 'locations/create',
}, },
{ {
name: 'Locations delete', name: 'Location Deleted',
value: 'locations/delete', value: 'locations/delete',
}, },
{ {
name: 'Locations update', name: 'Location Updated',
value: 'locations/update', value: 'locations/update',
}, },
{ {
name: 'Order transactions create', name: 'Order transactions Created',
value: 'order_transactions/create', value: 'order_transactions/create',
}, },
{ {
name: 'Orders cancelled', name: 'Order cancelled',
value: 'orders/cancelled', value: 'orders/cancelled',
}, },
{ {
name: 'Orders create', name: 'Order Created',
value: 'orders/create', value: 'orders/create',
}, },
{ {
name: 'Orders delete', name: 'Orders Deleted',
value: 'orders/delete', value: 'orders/delete',
}, },
{ {
name: 'Orders fulfilled', name: 'Order Fulfilled',
value: 'orders/fulfilled', value: 'orders/fulfilled',
}, },
{ {
name: 'Orders paid', name: 'Order Paid',
value: 'orders/paid', value: 'orders/paid',
}, },
{ {
name: 'Orders partially fulfilled', name: 'Order Partially Fulfilled',
value: 'orders/partially_fulfilled', value: 'orders/partially_fulfilled',
}, },
{ {
name: 'Orders updated', name: 'Order Updated',
value: 'orders/updated', value: 'orders/updated',
}, },
{ {
name: 'Product listings add', name: 'Product Listings Added',
value: 'product_listings/add', value: 'product_listings/add',
}, },
{ {
name: 'Product listings remove', name: 'Product Listings Removed',
value: 'product_listings/remove', value: 'product_listings/remove',
}, },
{ {
name: 'Product listings update', name: 'Product Listings Updated',
value: 'product_listings/update', value: 'product_listings/update',
}, },
{ {
name: 'Products create', name: 'Product Created',
value: 'products/create', value: 'products/create',
}, },
{ {
name: 'Products delete', name: 'Product Deleted',
value: 'products/delete', value: 'products/delete',
}, },
{ {
name: 'Products update', name: 'Product Updated',
value: 'products/update', value: 'products/update',
}, },
{ {
name: 'Refunds create', name: 'Refund Created',
value: 'refunds/create', value: 'refunds/create',
}, },
{ {
name: 'Shop update', name: 'Shop Updated',
value: 'shop/update', value: 'shop/update',
}, },
{ {
name: 'Tender transactions create', name: 'Tender Transactions Created',
value: 'tender_transactions/create', value: 'tender_transactions/create',
}, },
{ {
name: 'Themes create', name: 'Theme Created',
value: 'themes/create', value: 'themes/create',
}, },
{ {
name: 'Themes delete', name: 'Theme Deleted',
value: 'themes/delete', value: 'themes/delete',
}, },
{ {
name: 'Themes publish', name: 'Theme Published',
value: 'themes/publish', value: 'themes/publish',
}, },
{ {
name: 'Themes update', name: 'Theme Updated',
value: 'themes/update', value: 'themes/update',
}, },
], ],
description: 'Event that triggers the webhook', description: 'Event that triggers the webhook',
}, },
], ],
}; };
// @ts-ignore (because of request) // @ts-ignore (because of request)
webhookMethods = { webhookMethods = {
default: { default: {
async checkExists(this: IHookFunctions): Promise<boolean> { async checkExists(this: IHookFunctions): Promise<boolean> {
const topic = this.getNodeParameter('topic') as string;
const webhookData = this.getWorkflowStaticData('node'); const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId === undefined) { const webhookUrl = this.getNodeWebhookUrl('default');
return false; const endpoint = `/webhooks`;
}
const endpoint = `/webhooks/${webhookData.webhookId}.json`; const { webhooks } = await shopifyApiRequest.call(this, 'GET', endpoint, {}, { topic });
try { for (const webhook of webhooks) {
await shopifyApiRequest.call(this, 'GET', endpoint, {}); if (webhook.address === webhookUrl) {
} catch (e) { webhookData.webhookId = webhook.id;
if (e.statusCode === 404) { return true;
delete webhookData.webhookId;
return false;
} }
throw e;
} }
return true; return false;
}, },
async create(this: IHookFunctions): Promise<boolean> { async create(this: IHookFunctions): Promise<boolean> {
const credentials = this.getCredentials('shopifyApi');
const webhookUrl = this.getNodeWebhookUrl('default'); const webhookUrl = this.getNodeWebhookUrl('default');
const topic = this.getNodeParameter('topic') as string; const topic = this.getNodeParameter('topic') as string;
const webhookData = this.getWorkflowStaticData('node');
const endpoint = `/webhooks.json`; const endpoint = `/webhooks.json`;
const body = { const body = {
webhook: { webhook: {
@ -330,21 +328,15 @@ export class ShopifyTrigger implements INodeType {
}; };
let responseData; let responseData;
try {
responseData = await shopifyApiRequest.call(this, 'POST', endpoint, body); responseData = await shopifyApiRequest.call(this, 'POST', endpoint, body);
} catch (error) {
return false;
}
if (responseData.webhook === undefined || responseData.webhook.id === undefined) { if (responseData.webhook === undefined || responseData.webhook.id === undefined) {
// Required data is missing so was not successful // Required data is missing so was not successful
return false; return false;
} }
const webhookData = this.getWorkflowStaticData('node');
webhookData.webhookId = responseData.webhook.id as string; webhookData.webhookId = responseData.webhook.id as string;
webhookData.sharedSecret = credentials!.sharedSecret as string;
webhookData.topic = topic as string;
return true; return true;
}, },
async delete(this: IHookFunctions): Promise<boolean> { async delete(this: IHookFunctions): Promise<boolean> {
@ -357,8 +349,6 @@ export class ShopifyTrigger implements INodeType {
return false; return false;
} }
delete webhookData.webhookId; delete webhookData.webhookId;
delete webhookData.sharedSecret;
delete webhookData.topic;
} }
return true; return true;
}, },
@ -368,17 +358,18 @@ export class ShopifyTrigger implements INodeType {
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> { async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const headerData = this.getHeaderData() as IDataObject; const headerData = this.getHeaderData() as IDataObject;
const req = this.getRequestObject(); const req = this.getRequestObject();
const webhookData = this.getWorkflowStaticData('node') as IDataObject; const credentials = this.getCredentials('shopifyApi') as IDataObject;
const topic = this.getNodeParameter('topic') as string;
if (headerData['x-shopify-topic'] !== undefined if (headerData['x-shopify-topic'] !== undefined
&& headerData['x-shopify-hmac-sha256'] !== undefined && headerData['x-shopify-hmac-sha256'] !== undefined
&& headerData['x-shopify-shop-domain'] !== undefined && headerData['x-shopify-shop-domain'] !== undefined
&& headerData['x-shopify-api-version'] !== undefined) { && headerData['x-shopify-api-version'] !== undefined) {
// @ts-ignore // @ts-ignore
const computedSignature = createHmac('sha256', webhookData.sharedSecret as string).update(req.rawBody).digest('base64'); const computedSignature = createHmac('sha256', credentials.sharedSecret as string).update(req.rawBody).digest('base64');
if (headerData['x-shopify-hmac-sha256'] !== computedSignature) { if (headerData['x-shopify-hmac-sha256'] !== computedSignature) {
return {}; return {};
} }
if (webhookData.topic !== headerData['x-shopify-topic']) { if (topic !== headerData['x-shopify-topic']) {
return {}; return {};
} }
} else { } else {