Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Commit cc68919

Browse files
authored
feat: Merge pull request #32 from seamapi/feat-webhooks
2 parents 32a232b + 1327a11 commit cc68919

21 files changed

+689
-148
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,42 @@ console.log(await seam.accessCodes.list({ device_id: myLockId }))
7676
]
7777
*/
7878
```
79+
80+
### Receiving Webhooks
81+
82+
Although you don't need to use this package when receiving webhooks, we **strongly** recommend that you do. Using the included helper class will verify payloads (preventing malicious requests) and return a well-typed event.
83+
84+
`SeamWebhook` is a thin wrapper around the `Webhook` class from the [Svix library](https://docs.svix.com/receiving/verifying-payloads/how).
85+
86+
Example for Express:
87+
88+
```ts
89+
// Replace with
90+
// const {SeamWebhook} = require("seamapi")
91+
// if not using ES6 modules and/or TypeScript.
92+
import {SeamWebhook} from "seamapi";
93+
94+
import bodyParser from "body-parser";
95+
96+
// Get this from the Seam dashboard
97+
const secret = "sample-secret";
98+
99+
app.post('/webhook', bodyParser.raw({type: 'application/json'}), (req, res) => {
100+
const payload = req.body;
101+
const headers = req.headers;
102+
103+
const wh = new SeamWebhook(secret);
104+
let msg;
105+
try {
106+
msg = wh.verify(payload, headers);
107+
} catch (err) {
108+
res.status(400).json({});
109+
}
110+
111+
// Do something with the message...
112+
113+
res.json({});
114+
});
115+
```
116+
117+
For examples using other frameworks, see the [Svix docs](https://docs.svix.com/receiving/verifying-payloads/how#framework-specific-examples).

cjs-wrapper.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
module.exports = require("./dist/index.js").default
1+
module.exports = {
2+
...require("./dist/index.js").default,
3+
...require("./dist/index.js"),
4+
}

docs/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,42 @@ console.log(await seam.accessCodes.list({ device_id: myLockId }))
7878
]
7979
*/
8080
```
81+
82+
### Receiving Webhooks
83+
84+
Although you don't need to use this package when receiving webhooks, we **strongly** recommend that you do. Using the included helper class will verify payloads (preventing malicious requests) and return a well-typed event.
85+
86+
`SeamWebhook` is a thin wrapper around the `Webhook` class from the [Svix library](https://docs.svix.com/receiving/verifying-payloads/how).
87+
88+
Example for Express:
89+
90+
```ts
91+
// Replace with
92+
// const {SeamWebhook} = require("seamapi")
93+
// if not using ES6 modules and/or TypeScript.
94+
import {SeamWebhook} from "seamapi";
95+
96+
import bodyParser from "body-parser";
97+
98+
// Get this from the Seam dashboard
99+
const secret = "sample-secret";
100+
101+
app.post('/webhook', bodyParser.raw({type: 'application/json'}), (req, res) => {
102+
const payload = req.body;
103+
const headers = req.headers;
104+
105+
const wh = new SeamWebhook(secret);
106+
let msg;
107+
try {
108+
msg = wh.verify(payload, headers);
109+
} catch (err) {
110+
res.status(400).json({});
111+
}
112+
113+
// Do something with the message...
114+
115+
res.json({});
116+
});
117+
```
118+
119+
For examples using other frameworks, see the [Svix docs](https://docs.svix.com/receiving/verifying-payloads/how#framework-specific-examples).

docs/classes/default.md renamed to docs/classes/Seam.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
[seamapi](../README.md) / [Exports](../modules.md) / default
1+
[seamapi](../README.md) / [Exports](../modules.md) / Seam
22

3-
# Class: default
3+
# Class: Seam
44

55
## Hierarchy
66

77
- `Routes`
88

9-
**`default`**
9+
**`Seam`**
1010

1111
## Table of contents
1212

1313
### Constructors
1414

15-
- [constructor](default.md#constructor)
15+
- [constructor](Seam.md#constructor)
1616

1717
### Properties
1818

19-
- [accessCodes](default.md#accesscodes)
20-
- [actionAttempts](default.md#actionattempts)
21-
- [client](default.md#client)
22-
- [connectWebviews](default.md#connectwebviews)
23-
- [connectedAccounts](default.md#connectedaccounts)
24-
- [devices](default.md#devices)
25-
- [locks](default.md#locks)
26-
- [workspaces](default.md#workspaces)
19+
- [accessCodes](Seam.md#accesscodes)
20+
- [actionAttempts](Seam.md#actionattempts)
21+
- [client](Seam.md#client)
22+
- [connectWebviews](Seam.md#connectwebviews)
23+
- [connectedAccounts](Seam.md#connectedaccounts)
24+
- [devices](Seam.md#devices)
25+
- [locks](Seam.md#locks)
26+
- [workspaces](Seam.md#workspaces)
2727

2828
### Methods
2929

30-
- [makeRequest](default.md#makerequest)
30+
- [makeRequest](Seam.md#makerequest)
3131

3232
## Constructors
3333

3434
### constructor
3535

36-
**new default**(`apiKeyOrOptions?`)
36+
**new Seam**(`apiKeyOrOptions?`)
3737

3838
#### Parameters
3939

@@ -47,7 +47,7 @@ Routes.constructor
4747

4848
#### Defined in
4949

50-
[src/index.ts:38](https://github.com/seamapi/seamapi-javascript/blob/main/src/index.ts#L38)
50+
[src/client.ts:38](https://github.com/seamapi/seamapi-javascript/blob/main/src/client.ts#L38)
5151

5252
## Properties
5353

@@ -59,7 +59,7 @@ Routes.constructor
5959

6060
| Name | Type |
6161
| :------ | :------ |
62-
| `create` | (`params`: [`AccessCodeCreateBaseRequest`](../interfaces/AccessCodeCreateBaseRequest.md)) => `Promise`<[`OngoingAccessCode`](../interfaces/OngoingAccessCode.md)\> \| (`params`: [`AccessCodeCreateScheduledRequest`](../interfaces/AccessCodeCreateScheduledRequest.md)) => `Promise`<[`TimeBoundAccessCode`](../interfaces/TimeBoundAccessCode.md)\> |
62+
| `create` | (`params`: [`AccessCodeCreateOngoingRequest`](../interfaces/AccessCodeCreateOngoingRequest.md)) => `Promise`<[`OngoingAccessCode`](../interfaces/OngoingAccessCode.md)\>(`params`: [`AccessCodeCreateScheduledRequest`](../interfaces/AccessCodeCreateScheduledRequest.md)) => `Promise`<[`TimeBoundAccessCode`](../interfaces/TimeBoundAccessCode.md)\> |
6363
| `delete` | (`params`: [`AccessCodeDeleteRequest`](../modules.md#accesscodedeleterequest)) => `Promise`<`unknown`\> |
6464
| `list` | (`params`: { `device_id`: `string` }) => `Promise`<[`AccessCode`](../modules.md#accesscode)[]\> |
6565

@@ -89,7 +89,7 @@ Routes.actionAttempts
8989

9090
#### Defined in
9191

92-
[src/routes.ts:269](https://github.com/seamapi/seamapi-javascript/blob/main/src/routes.ts#L269)
92+
[src/routes.ts:268](https://github.com/seamapi/seamapi-javascript/blob/main/src/routes.ts#L268)
9393

9494
___
9595

@@ -99,7 +99,7 @@ ___
9999

100100
#### Defined in
101101

102-
[src/index.ts:36](https://github.com/seamapi/seamapi-javascript/blob/main/src/index.ts#L36)
102+
[src/client.ts:36](https://github.com/seamapi/seamapi-javascript/blob/main/src/client.ts#L36)
103103

104104
___
105105

@@ -142,7 +142,7 @@ Routes.connectedAccounts
142142

143143
#### Defined in
144144

145-
[src/routes.ts:248](https://github.com/seamapi/seamapi-javascript/blob/main/src/routes.ts#L248)
145+
[src/routes.ts:247](https://github.com/seamapi/seamapi-javascript/blob/main/src/routes.ts#L247)
146146

147147
___
148148

@@ -239,4 +239,4 @@ Routes.makeRequest
239239

240240
#### Defined in
241241

242-
[src/index.ts:68](https://github.com/seamapi/seamapi-javascript/blob/main/src/index.ts#L68)
242+
[src/client.ts:68](https://github.com/seamapi/seamapi-javascript/blob/main/src/client.ts#L68)

docs/classes/SeamWebhook.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[seamapi](../README.md) / [Exports](../modules.md) / SeamWebhook
2+
3+
# Class: SeamWebhook
4+
5+
Parse and verify webhook payloads.
6+
7+
## Table of contents
8+
9+
### Constructors
10+
11+
- [constructor](SeamWebhook.md#constructor)
12+
13+
### Properties
14+
15+
- [wh](SeamWebhook.md#wh)
16+
17+
### Methods
18+
19+
- [verify](SeamWebhook.md#verify)
20+
21+
## Constructors
22+
23+
### constructor
24+
25+
**new SeamWebhook**(`secret`)
26+
27+
Create a new instance of SeamWebhook.
28+
29+
#### Parameters
30+
31+
| Name | Type | Description |
32+
| :------ | :------ | :------ |
33+
| `secret` | `string` | your webhook secret from the dashboard |
34+
35+
#### Defined in
36+
37+
[src/webhooks.ts:14](https://github.com/seamapi/seamapi-javascript/blob/main/src/webhooks.ts#L14)
38+
39+
## Properties
40+
41+
### wh
42+
43+
`Private` **wh**: `Webhook`
44+
45+
#### Defined in
46+
47+
[src/webhooks.ts:8](https://github.com/seamapi/seamapi-javascript/blob/main/src/webhooks.ts#L8)
48+
49+
## Methods
50+
51+
### verify
52+
53+
**verify**(`payload`, `headers`): [`SeamEvent`](../modules.md#seamevent)
54+
55+
Verify a payload received from a webhook and return the typed event.
56+
57+
#### Parameters
58+
59+
| Name | Type | Description |
60+
| :------ | :------ | :------ |
61+
| `payload` | `string` | must be a string (should not be the parsed JSON object) |
62+
| `headers` | `Record`<`string`, `string`\> | request headers (used to verify against the secret) |
63+
64+
#### Returns
65+
66+
[`SeamEvent`](../modules.md#seamevent)
67+
68+
event
69+
70+
#### Defined in
71+
72+
[src/webhooks.ts:24](https://github.com/seamapi/seamapi-javascript/blob/main/src/webhooks.ts#L24)

docs/interfaces/AccessCodeCreateBaseRequest.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
- **`AccessCodeCreateBaseRequest`**
88

9+
[`AccessCodeCreateOngoingRequest`](AccessCodeCreateOngoingRequest.md)
10+
911
[`AccessCodeCreateScheduledRequest`](AccessCodeCreateScheduledRequest.md)
1012

1113
## Table of contents
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[seamapi](../README.md) / [Exports](../modules.md) / AccessCodeCreateOngoingRequest
2+
3+
# Interface: AccessCodeCreateOngoingRequest
4+
5+
## Hierarchy
6+
7+
- [`AccessCodeCreateBaseRequest`](AccessCodeCreateBaseRequest.md)
8+
9+
**`AccessCodeCreateOngoingRequest`**
10+
11+
## Table of contents
12+
13+
### Properties
14+
15+
- [code](AccessCodeCreateOngoingRequest.md#code)
16+
- [device\_id](AccessCodeCreateOngoingRequest.md#device_id)
17+
- [name](AccessCodeCreateOngoingRequest.md#name)
18+
19+
## Properties
20+
21+
### code
22+
23+
`Optional` **code**: `string`
24+
25+
#### Inherited from
26+
27+
[AccessCodeCreateBaseRequest](AccessCodeCreateBaseRequest.md).[code](AccessCodeCreateBaseRequest.md#code)
28+
29+
#### Defined in
30+
31+
[src/types/route-requests.ts:10](https://github.com/seamapi/seamapi-javascript/blob/main/src/types/route-requests.ts#L10)
32+
33+
___
34+
35+
### device\_id
36+
37+
**device\_id**: `string`
38+
39+
#### Inherited from
40+
41+
[AccessCodeCreateBaseRequest](AccessCodeCreateBaseRequest.md).[device_id](AccessCodeCreateBaseRequest.md#device_id)
42+
43+
#### Defined in
44+
45+
[src/types/route-requests.ts:8](https://github.com/seamapi/seamapi-javascript/blob/main/src/types/route-requests.ts#L8)
46+
47+
___
48+
49+
### name
50+
51+
`Optional` **name**: `string`
52+
53+
#### Inherited from
54+
55+
[AccessCodeCreateBaseRequest](AccessCodeCreateBaseRequest.md).[name](AccessCodeCreateBaseRequest.md#name)
56+
57+
#### Defined in
58+
59+
[src/types/route-requests.ts:9](https://github.com/seamapi/seamapi-javascript/blob/main/src/types/route-requests.ts#L9)

docs/interfaces/AccessCodeCreateScheduledRequest.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ ___
5454

5555
#### Defined in
5656

57-
[src/types/route-requests.ts:18](https://github.com/seamapi/seamapi-javascript/blob/main/src/types/route-requests.ts#L18)
57+
[src/types/route-requests.ts:19](https://github.com/seamapi/seamapi-javascript/blob/main/src/types/route-requests.ts#L19)
5858

5959
___
6060

@@ -78,4 +78,4 @@ ___
7878

7979
#### Defined in
8080

81-
[src/types/route-requests.ts:17](https://github.com/seamapi/seamapi-javascript/blob/main/src/types/route-requests.ts#L17)
81+
[src/types/route-requests.ts:18](https://github.com/seamapi/seamapi-javascript/blob/main/src/types/route-requests.ts#L18)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[seamapi](../README.md) / [Exports](../modules.md) / CommonDeviceEvent
2+
3+
# Interface: CommonDeviceEvent<EventType, Payload\>
4+
5+
## Type parameters
6+
7+
| Name | Type |
8+
| :------ | :------ |
9+
| `EventType` | extends `string` |
10+
| `Payload` | extends `Record`<`string`, `unknown`\> \| {} = {} |
11+
12+
## Table of contents
13+
14+
### Properties
15+
16+
- [data](CommonDeviceEvent.md#data)
17+
- [event\_type](CommonDeviceEvent.md#event_type)
18+
19+
## Properties
20+
21+
### data
22+
23+
**data**: `Payload` & { `device_id`: `string` ; `workspace_id`: `string` }
24+
25+
#### Defined in
26+
27+
[src/types/webhook-events.ts:7](https://github.com/seamapi/seamapi-javascript/blob/main/src/types/webhook-events.ts#L7)
28+
29+
___
30+
31+
### event\_type
32+
33+
**event\_type**: `EventType`
34+
35+
#### Defined in
36+
37+
[src/types/webhook-events.ts:6](https://github.com/seamapi/seamapi-javascript/blob/main/src/types/webhook-events.ts#L6)

0 commit comments

Comments
 (0)