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

Add type definitions for webhook payloads

Open kyle-villeneuve opened this issue 4 years ago • 1 comments

Overview

Adding in the type definitions of webhook payloads would be immensely helpful, either here or in your documentation somewhere. As far as I know the only payload documentation you offer is a single example payload per topic which is not great when trying to deal with all possible type nuances that can exist.

Type

  • [ ] New feature
  • [x] Changes to existing features

Motivation

As of right now my strategy is to accumulate a reasonable sample size of webhooks in my application's db and use a typegen tool based on the sample data, which does work but it's incredibly tedious to deal with. I imagine you probably have the type definitions in your codebase somewhere and it's just a matter of making it publicly available.

Area

  • [ ] webhooks
  • [ ] type definitions

kyle-villeneuve avatar Apr 07 '22 22:04 kyle-villeneuve

Hey Kyle,

looks like its related to issue #193. I've added a pull request over the weekend to add types to the Rest & Graphql clients. I've also added a forked package to npm that runs with the types integrated already. You can install shopify-typed-node-api and import the required types directly from there.

I.e. a webhook for orders/updated could be typed like this:

import { _Order } from "shopify-typed-node-api/dist/clients/rest/dataTypes";

function handleWebhook(req, res) {
  const bodyBuffer = await getRawBody(req);
  const body = JSON.parse(bodyBuffer.toString());
  const {
    "x-shopify-hmac-sha256": hmac,
    "x-shopify-shop-domain": shop,
    "x-shopify-topic": topic,
  } = headers;

  if (!verifyWebhookHmac(hmac, bodyBuffer)) {
    res.status(401).send("Unauthorized Access");
    return;
  }

  switch (topic) {
    case "orders/updated": {
      const order = body as _Order;

      // do stuff with order and have types...

      return res.status(200).send("success");
    }
  }
}

FelixTellmann avatar Apr 08 '22 08:04 FelixTellmann

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 Sep 30 '22 02:09 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 Oct 15 '22 02:10 github-actions[bot]