https://github.com/hygraph/hygraph-utils
https://github.com/hygraph/hygraph-utils
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/hygraph/hygraph-utils
- Owner: hygraph
- Created: 2021-09-10T08:33:14.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2023-01-30T17:51:27.000Z (over 3 years ago)
- Last Synced: 2025-04-03T01:58:13.064Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 99.6 KB
- Stars: 1
- Watchers: 21
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Hygraph Utils
## Install
```bash
npm i @hygraph/utils
```
## Usage
### `verifyWebhookSignature`
You'll need the request body and headers to pass to `verifyWebhookSignature`.
If `isValid` is truthy then you can safely execute your webhook handler code knowing the request is genuine, otherwise you should abort any further action.
```js
const { verifyWebhookSignature } = require("@hygraph/utils");
const secret = "rCNwyiloY3oJYYkxgpBXaleIiUv5MYlx";
const body = {}; // Typically req.body
const signature = "..."; // Typically req.headers['gcms-signature']
const isValid = verifyWebhookSignature({ body, signature, secret });
```
`verifyWebhookSignature` also accepts a `rawPayload` in the case that the body
has not yet been parsed.
```js
const { verifyWebhookSignature } = require("@hygraph/utils");
const secret = "rCNwyiloY3oJYYkxgpBXaleIiUv5MYlx";
const rawPayload = '{"hello":"world"}';
const signature = "..."; // Typically req.headers['gcms-signature']
const isValid = verifyWebhookSignature({ rawPayload, signature, secret });
```
[Learn more about Webhooks](https://hygraph.com/docs/api-reference/basics/webhooks).
### `generateWebhookSignature`
This is useful for testing signed webhooks. You can generate a Hygraph webhook signature, and then use it to test your webhook.
```js
const { generateWebhookSignature } = require("@hygraph/utils");
const secret = "rCNwyiloY3oJYYkxgpBXaleIiUv5MYlx";
const body = {
hello: "world",
};
const signature = generateWebhookSignature({ body, secret });
```
[Learn more about Webhooks](https://hygraph.com/docs/api-reference/basics/webhooks).