@canopy-io/node 0.3.5 → 0.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # @canopy-io/node
2
2
 
3
+ ## 0.3.6
4
+
5
+ ### Patch Changes
6
+
7
+ - 0708712: Typed webhook deliveries.
8
+
9
+ - Canopy now publishes a schema per webhook event, and the types are regenerated from it: 37 `Webhook…Event` schemas on the shared envelope, plus `payload_schema` and `resource_types` on `GET /api/v1/webhooks/event-types`. No operation was added or removed.
10
+ - New `WebhookDelivery`, `WebhookEventName`, `WebhookEvent<Name>` and `WebhookEventData<Name>` types, and an `isWebhookEvent(delivery, name)` guard, so a handler reads one event's `metadata` without a cast.
11
+ - `metadata` is now always an object on every delivery.
12
+
3
13
  ## 0.3.5
4
14
 
5
15
  ### Patch Changes
package/README.md CHANGED
@@ -169,6 +169,32 @@ const { token, base_url } = await canopy.organizations.mintDirectoryToken(
169
169
  );
170
170
  ```
171
171
 
172
+ ### Handling webhooks
173
+
174
+ Every webhook event has a published schema, and the SDK types deliveries from
175
+ it. Narrow by event name and read that event's `metadata` without a cast; a
176
+ field the API adds arrives with the next `npm run generate`.
177
+
178
+ ```ts
179
+ import { isWebhookEvent, type WebhookDelivery } from "@canopy-io/node";
180
+
181
+ app.post("/webhooks/canopy", (req, res) => {
182
+ const delivery = req.body as WebhookDelivery;
183
+
184
+ if (isWebhookEvent(delivery, "organization.member.added")) {
185
+ const { identity_id, role_id } = delivery.data.metadata;
186
+ // ...
187
+ }
188
+
189
+ res.sendStatus(204);
190
+ });
191
+ ```
192
+
193
+ `WebhookEvent<"assignment.updated">` is the delivery of one event, and
194
+ `WebhookEventName` is every event a subscription can receive. The API's
195
+ `GET /api/v1/webhooks/event-types` names each event's schema in
196
+ `payload_schema`.
197
+
172
198
  ### Authorizing without a call per request
173
199
 
174
200
  Asking "may this identity act _here_" on every request puts Canopy in your
package/dist/index.cjs CHANGED
@@ -1606,6 +1606,11 @@ var TokenVerifier = class {
1606
1606
  }
1607
1607
  };
1608
1608
 
1609
+ // src/webhooks.ts
1610
+ function isWebhookEvent(delivery, name) {
1611
+ return delivery.event === name;
1612
+ }
1613
+
1609
1614
  exports.Assignments = Assignments;
1610
1615
  exports.Canopy = Canopy;
1611
1616
  exports.CanopyAuthorizerError = CanopyAuthorizerError;
@@ -1625,6 +1630,7 @@ exports.isCanopyConnectionError = isCanopyConnectionError;
1625
1630
  exports.isCanopyError = isCanopyError;
1626
1631
  exports.isCanopyTokenError = isCanopyTokenError;
1627
1632
  exports.isCursorPagination = isCursorPagination;
1633
+ exports.isWebhookEvent = isWebhookEvent;
1628
1634
  exports.orgContext = orgContext;
1629
1635
  exports.paginate = paginate;
1630
1636
  exports.withConcurrency = withConcurrency;