Skip to content

Commit

Permalink
Make it easier to get started with webhooks by:
Browse files Browse the repository at this point in the history
1. Providing the config for mandatory webhook topics
2. Adding a product create webhook, so partners can see a webhook trigger when pressing "generte a product"
3. Separating each webhook topic into it's own URL
  • Loading branch information
byrichardpowell committed Sep 11, 2024
1 parent 93b9952 commit 3bf02fb
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 34 deletions.
15 changes: 15 additions & 0 deletions app/routes/webhooks.app.uninstalled.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { ActionFunctionArgs } from "@remix-run/node";
import { authenticate } from "~/shopify.server";
import db from "~/db.server";

export const action = async ({ request }: ActionFunctionArgs) => {
const { shop, session } = await authenticate.webhook(request);

// Webhook requests can trigger after an app is uninstalled.
// If the app is already uninstalled, the session may be undefined.
if (session) {
db.session.deleteMany({ where: { shop } });
}

return new Response();
};
18 changes: 18 additions & 0 deletions app/routes/webhooks.customers.data_request.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { ActionFunctionArgs } from "@remix-run/node";
import { authenticate } from "~/shopify.server";

export const action = async ({ request }: ActionFunctionArgs) => {
const { session } = await authenticate.webhook(request);

// Webhook requests can trigger after an app is uninstalled
// If the app is already uninstalled, the session may be undefined.
if (!session) {
throw new Response();
}

// Implement handling of mandatory compliance topics
// See: https://shopify.dev/docs/apps/build/privacy-law-compliance
console.log("Received customers data_request webhook");

return new Response();
};
18 changes: 18 additions & 0 deletions app/routes/webhooks.customers.redact.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { ActionFunctionArgs } from "@remix-run/node";
import { authenticate } from "~/shopify.server";

export const action = async ({ request }: ActionFunctionArgs) => {
const {session} = await authenticate.webhook(request);

// Webhook requests can trigger after an app is uninstalled
// If the app is already uninstalled, the session may be undefined.
if (!session) {
throw new Response();
}

// Implement handling of mandatory compliance topics
// See: https://shopify.dev/docs/apps/build/privacy-law-compliance
console.log("Received customers redact webhook");

return new Response();
};
17 changes: 17 additions & 0 deletions app/routes/webhooks.products.create.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { ActionFunctionArgs } from "@remix-run/node";
import { authenticate } from "~/shopify.server";

export const action = async ({ request }: ActionFunctionArgs) => {
const { session, payload } = await authenticate.webhook(request);

// Webhook requests can trigger after an app is uninstalled
// If the app is already uninstalled, the session may be undefined.
if (!session) {
return new Response()
}

console.log("Received products create webhook");
console.log(JSON.stringify(payload));

return new Response();
};
18 changes: 18 additions & 0 deletions app/routes/webhooks.shop.redact.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { ActionFunctionArgs } from "@remix-run/node";
import { authenticate } from "~/shopify.server";

export const action = async ({ request }: ActionFunctionArgs) => {
const { session } = await authenticate.webhook(request);

// SHOP_REDACT will be fired up to 48 hours after app is uninstalled
// Therefore, for SHOP_REDACT we expect the admin to be undefined
if (!session) {
return new Response("", { status: 400 });
}

// Implement handling of mandatory compliance topics
// See: https://shopify.dev/docs/apps/build/privacy-law-compliance
console.log("Received shop redact webhook");

return new Response();
};
32 changes: 0 additions & 32 deletions app/routes/webhooks.tsx

This file was deleted.

24 changes: 23 additions & 1 deletion shopify.app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ scopes = "write_products"
[webhooks]
api_version = "2024-07"

# Mandatory compliance topics for public apps
# Delete only if the app is not public
# See: https://shopify.dev/docs/apps/build/privacy-law-compliance
[[webhooks.subscriptions]]
uri = "/webhooks/customers/data_request"
compliance_topics = [ "customers/data_request"]

[[webhooks.subscriptions]]
uri = "/webhooks/customers/redact"
compliance_topics = [ "customers/redact" ]

[[webhooks.subscriptions]]
uri = "/webhooks/shop/redact"
compliance_topics = [ "shop/redact" ]

# When a shop uninstalls the app, delete the session for that shop
[[webhooks.subscriptions]]
uri = "/webhooks/app/uninstalled"
topics = [ "app/uninstalled" ]
uri = "/webhooks"

# Optional: Configured for illustration purposes
# Delete if the app doesn't care about the product create event
[[webhooks.subscriptions]]
topics = ["products/create"]
uri = "/webhooks/products/create"
2 changes: 1 addition & 1 deletion shopify.web.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "remix"
roles = ["frontend", "backend"]
webhooks_path = "/webhooks"
webhooks_path = "/webhooks/app/uninstalled"

[commands]
predev = "npx prisma generate"
Expand Down

0 comments on commit 3bf02fb

Please sign in to comment.