Skip to content

Commit

Permalink
fix: unwrap webhooks into payload, not event
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Mar 3, 2023
1 parent e06dad8 commit a825f66
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ async function fetchAllCards(params) {

We provide helper methods for verifying that a webhook request came from Lithic, and not a malicious third party.

You can use `lithic.webhooks.verifySignature(body, headers, secret?) -> void` or `lithic.webhooks.unwrap(body, headers, secret?) -> Event` like so:
You can use `lithic.webhooks.verifySignature(body, headers, secret?) -> void` or `lithic.webhooks.unwrap(body, headers, secret?) -> Payload` like so:

```ts
export default function handler(req, res) {
const event = lithic.webhooks.unwrap(req.body, req.headers, process.env['LITHIC_WEBHOOK_SECRET']); // env var used by default; explicit here.
console.log(event.token, event.payload);
const payload = lithic.webhooks.unwrap(req.body, req.headers, process.env['LITHIC_WEBHOOK_SECRET']); // env var used by default; explicit here.
console.log(payload);
}
```

Expand Down
5 changes: 2 additions & 3 deletions resources/webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// File generated from our OpenAPI spec by Stainless.

import { APIResource } from '~/resource';
import type { Event } from '~/resources/events';
import { createHmac } from 'crypto';
import { getHeader, HeadersLike } from '~/core';

Expand All @@ -13,9 +12,9 @@ export class Webhooks extends APIResource {
payload: string,
headers: HeadersLike,
secret: string | undefined | null = this.client.webhookSecret,
): Event {
): Object {
this.verifySignature(payload, headers, secret);
return JSON.parse(payload) as Event;
return JSON.parse(payload);
}

private parseSecret(secret: string | null | undefined): Uint8Array {
Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/webhooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('resource webhooks', () => {
});

describe('unwrap', () => {
it('deserializes the event object', () => {
it('deserializes the payload object', () => {
lithic.webhooks.unwrap(payload, headers, secret);
});
});
Expand Down

0 comments on commit a825f66

Please sign in to comment.