shopify-api-js icon indicating copy to clipboard operation
shopify-api-js copied to clipboard

Add context variable to webhooks process function

Open chr33s opened this issue 3 years ago • 3 comments

Overview

In Cloudflare workers/pages to access environment variables you need access to the context argument passed into functions. Without this context you are limited in what you can do within the process functions in webhooks (e.g. connect to databases, access Shopify tokens etc).

Proposal

Include a context variable to shopify.webhooks.process( e.g.

export const onRequestPost = async (context) => {
	return shopify(context).webhooks.process({
		context: context,
		rawBody: await context.request.text(),
		rawRequest: context.request,
	});
};

Then pass this context into the

const webhooks = {
	APP_UNINSTALLED: {
		deliveryMethod: "http",
		callbackUrl: "/",
		callback: async (
			topic,
			shop,
			body,
			webhookId,
			apiVersion,
			context,
		) => {
			const payload = JSON.parse(body);
			console.log({ apiVersion, payload, shop, topic, webhookId, context });
		}
	}
}

Thereby enabling workers/pages to access environment variables context.env.DATABASE_URL from callback.

chr33s avatar May 16 '23 07:05 chr33s

It would also be easier to grok the callback if we changed the signature to an object.

callback: ({
	topic,
	shop,
	body,
	webhookId,
	apiVersion,
	context,
}) => {}
``

chr33s avatar May 16 '23 07:05 chr33s

This issue is stale because it has been open for 90 days with no activity. It will be closed if no further action occurs in 14 days.

github-actions[bot] avatar Jul 16 '23 02:07 github-actions[bot]

We are closing this issue because it has been inactive for a few months. This probably means that it is not reproducible or it has been fixed in a newer version. If it’s an enhancement and hasn’t been taken on since it was submitted, then it seems other issues have taken priority.

If you still encounter this issue with the latest stable version, please reopen using the issue template. You can also contribute directly by submitting a pull request– see the CONTRIBUTING.md file for guidelines

Thank you!

github-actions[bot] avatar Jul 31 '23 01:07 github-actions[bot]