There are three components that allows webhooks to work in Shopify Apps.
A: Loading the handlers
Shopify.Webhooks.Registry.addHandler("APP_UNINSTALLED", {
path: "/webhook/uninstall",
webhookHandler: async (topic, shop, body) => {
// I don't want to process it here. Instead, I want to do it in C directly.
console.log(topic, shop, body);
},
});
B: Register the webhook
const response = await Shopify.Webhooks.Registry.register({
shop: shopSession.shop,
accessToken: String(shopSession.accessToken),
topic: "APP_UNINSTALLED",
path: "/webhook/uninstall",
});
if (!response.APP_UNINSTALLED.success) {
console.log(
`Failed to register APP_UNINSTALLED webhook: ${response.result}`
);
} else {
console.log("webhook registered");
}
C: Process webhook
app.post("/webhook/uninstall", async (req, res) => {
try {
// I want to process the webhook here instead of doing it in A because I need to fetch some user information in the req object.
await Shopify.Webhooks.Registry.process(req, res);
console.log("Webhook processed, returned status code 200");
} catch (error) {
console.log(`Failed to process webhook: ${error}`);
if (!res.headersSent) {
res.status(500).send(error.message);
}
}
});
Is it possible to process the webhook in C instead of doing it in the callback function in A ? If the answer is no. How can I do it so that I can access the req or res object when a webhook is called.