Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Add context argument to webhooks process function for Cloudflare work…
Browse files Browse the repository at this point in the history
…ers #880
  • Loading branch information
chr33s committed Mar 18, 2024
1 parent dce7ce2 commit 87208ea
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-bobcats-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/shopify-api': minor
---

context argument to webhooks process function
25 changes: 25 additions & 0 deletions packages/shopify-api/docs/guides/webhooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const shopify = shopifyApi({

// Call shopify.webhooks.addHandlers here (see examples below)

## Node.js

const app = express();

// Register webhooks after OAuth completes
Expand Down Expand Up @@ -86,6 +88,7 @@ const handleWebhookRequest = async (
webhookRequestBody: string,
webhookId: string,
apiVersion: string,
context?: any,
) => {
const sessionId = shopify.session.getOfflineId(shop);

Expand Down Expand Up @@ -119,6 +122,28 @@ app.post('/webhooks', express.text({type: '*/*'}), async (req, res) => {
});
```

## Cloudflare workers

// Register webhooks after OAuth completes

```ts
async function handleFetch(
request: Request,
env: unknown,
context: any,
): Promise<Response> {
try {
await shopify.webhooks.process({
context: {env, ...context}, // is object or undefined
rawBody: await request.text(), // is a string
rawRequest: request,
});
} catch (error) {
console.log(error.message);
}
}
```

## Note regarding use of body parsers

Unlike `v5` and earlier versions of this library, `shopify.webhooks.process()` now expects to receive the body content (in string format) as a parameter and no longer reads in the request body directly.
Expand Down
2 changes: 2 additions & 0 deletions packages/shopify-api/lib/webhooks/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,15 @@ describe('webhooks', () => {
body,
webhookId,
shopify.config.apiVersion,
undefined,
);
expect(handler3.callback).toHaveBeenCalledWith(
topic,
session.shop,
body,
webhookId,
shopify.config.apiVersion,
undefined,
);
});

Expand Down
4 changes: 4 additions & 0 deletions packages/shopify-api/lib/webhooks/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export function process(
webhookRegistry: WebhookRegistry<HttpWebhookHandlerWithCallback>,
) {
return async function process({
context,
rawBody,
...adapterArgs
}: WebhookProcessParams): Promise<AdapterResponse> {
Expand All @@ -68,6 +69,7 @@ export function process(
webhookRegistry,
webhookCheck,
rawBody,
context,
);

response.statusCode = handlerResult.statusCode;
Expand Down Expand Up @@ -99,6 +101,7 @@ async function callWebhookHandlers(
webhookRegistry: WebhookRegistry<HttpWebhookHandlerWithCallback>,
webhookCheck: WebhookValidationValid,
rawBody: string,
context: any,
): Promise<HandlerCallResult> {
const log = logger(config);
const {hmac: _hmac, valid: _valid, ...loggingContext} = webhookCheck;
Expand Down Expand Up @@ -140,6 +143,7 @@ async function callWebhookHandlers(
webhookCheck.webhookId,
webhookCheck.apiVersion,
...(webhookCheck?.subTopic ? webhookCheck.subTopic : ''),
context,
);
} catch (error) {
response.statusCode = StatusCode.InternalServerError;
Expand Down
3 changes: 3 additions & 0 deletions packages/shopify-api/lib/webhooks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ export type WebhookHandlerFunction = (
webhookId: string,
apiVersion?: string,
subTopic?: string,
context?: any,
) => Promise<void>;

interface BaseWebhookHandler {
id?: string;
includeFields?: string[];
metafieldNamespaces?: string[];
subTopic?: string;
context?: any;
}

export interface HttpWebhookHandler extends BaseWebhookHandler {
Expand Down Expand Up @@ -118,6 +120,7 @@ export type AddHandlersParams = Record<

export interface WebhookProcessParams extends AdapterArgs {
rawBody: string;
context?: any;
}

export interface WebhookValidateParams extends WebhookProcessParams {}
Expand Down

0 comments on commit 87208ea

Please sign in to comment.