Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement: Added more context to Shopify webhook registering errors #1090

Merged
merged 5 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/blue-pumas-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@trigger.dev/shopify": patch
---

improved error messages when a shopify webhook fails to register
20 changes: 20 additions & 0 deletions integrations/shopify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ export class Shopify implements TriggerIntegration {
throw `Can't create Shopify integration (${options.id}) as apiKey was undefined`;
}

if (Object.keys(options).includes("apiSecretKey") && !options.apiSecretKey) {
throw `Can't create Shopify integration (${options.id}) as apiSecretKey was undefined`;
}

if (Object.keys(options).includes("adminAccessToken") && !options.adminAccessToken) {
throw `Can't create Shopify integration (${options.id}) as adminAccessToken was undefined`;
}

if (Object.keys(options).includes("hostName") && !options.hostName) {
throw `Can't create Shopify integration (${options.id}) as hostName was undefined`;
}

// Regular expression to ensure the hostname ends with `.myshopify.com`
const shopifyDomainPattern = /^[a-zA-Z0-9-]+\.myshopify\.com$/;

// Verify that the user has entered the .myshopify.com domain and not a custom primary domain
if (!shopifyDomainPattern.test(options.hostName)) {
throw `Can't create Shopify integration (${options.id}) because hostName should be a ".myshopify.com" domain, not a custom primary domain.`;
ramadanomar marked this conversation as resolved.
Show resolved Hide resolved
}

this._options = options;
this._shopDomain = this._options.hostName.replace("http://", "").replace("https://", "");
}
Expand Down
67 changes: 41 additions & 26 deletions integrations/shopify/src/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,26 @@ export function createWebhookEventSource(integration: Shopify) {
key: (params) => params.topic,
crud: {
create: async ({ io, ctx }) => {
const webhook = await io.integration.rest.Webhook.save("create-webhook", {
fromData: {
address: ctx.url,
topic: ctx.params.topic,
// fields: ctx.params.fields,
},
});

const clientSecret = await io.integration.runTask(
"get-client-secret",
async (client) => client.config.apiSecretKey
);

await io.store.job.set("set-id", "webhook-id", webhook.id);
await io.store.job.set("set-secret", "webhook-secret", clientSecret);
try {
const webhook = await io.integration.rest.Webhook.save("create-webhook", {
fromData: {
address: ctx.url,
topic: ctx.params.topic,
// fields: ctx.params.fields,
},
});

const clientSecret = await io.integration.runTask(
"get-client-secret",
async (client) => client.config.apiSecretKey
);

await io.store.job.set("set-id", "webhook-id", webhook.id);
await io.store.job.set("set-secret", "webhook-secret", clientSecret);
} catch (error) {
await io.logger.error(`Failed to create webhook: ${(error as Error).message}`);
throw error;
}
},
delete: async ({ io, ctx }) => {
const webhookId = await io.store.job.get<number>("get-webhook-id", "webhook-id");
Expand All @@ -109,23 +114,33 @@ export function createWebhookEventSource(integration: Shopify) {
throw new Error("Missing webhook ID for delete operation.");
}

await io.integration.rest.Webhook.delete("delete-webhook", {
id: webhookId,
});
try {
await io.integration.rest.Webhook.delete("delete-webhook", {
id: webhookId,
});
} catch (error) {
await io.logger.error(`Failed to delete webhook: ${(error as Error).message}`);
throw error;
}

await io.store.job.delete("delete-webhook-id", "webhook-id");
},
update: async ({ io, ctx }) => {
const webhookId = await io.store.job.get<number>("get-webhook-id", "webhook-id");

await io.integration.rest.Webhook.save("update-webhook", {
fromData: {
id: webhookId,
address: ctx.url,
topic: ctx.params.topic,
// fields: ctx.params.fields,
},
});
try {
await io.integration.rest.Webhook.save("update-webhook", {
fromData: {
id: webhookId,
address: ctx.url,
topic: ctx.params.topic,
// fields: ctx.params.fields,
},
});
} catch (error) {
await io.logger.error(`Failed to update webhook: ${(error as Error).message}`);
throw error;
}
},
},
verify: async ({ request, client, ctx }) => {
Expand Down