Skip to content

Commit

Permalink
Replace SeamWebookEvent with SeamEvent (#6)
Browse files Browse the repository at this point in the history
* Replace SeamWebhookEvent with SeamEvent

* Sync peerDependencies for types version

* ci: Generate code

* Use TIP and IMPORTANT in README

* Fix event types

---------

Co-authored-by: Seam Bot <devops@getseam.com>
  • Loading branch information
razor-x and seambot authored Apr 19, 2024
1 parent 9c60bc3 commit f9dc262
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 56 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/generate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ jobs:
uses: ./.github/actions/setup
with:
install_dependencies: 'false'
- name: Sync @seamapi/types version in peerDependencies
run: |
sudo apt-get install -y jq moreutils
jq '.peerDependencies."@seamapi/types" = .devDependencies."@seamapi/types"' package.json | sponge package.json
- name: Normalize package-lock.json
run: npm install
- name: Commit
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ in your application.

## Installation

_This is a low-level package meant for applications and libraries with particular dependency requirements.
Before using this package, ensure you understand the installation and updating instructions.
This SDK is entirely contained in the [seam package]. Seam recommends using that package instead
for simpler dependency management._
> [!IMPORTANT]
> This is a low-level package meant for applications and libraries with particular dependency requirements.
> Before using this package, ensure you understand the installation and updating instructions.
> This SDK is entirely contained in the [seam package].
> Seam recommends using that package instead for simpler dependency management.
Add this as a dependency to your project using [npm] with

Expand All @@ -55,30 +56,29 @@ Refer to the next section for keeping the types updated.

#### Keeping up with the latest types

This package depends on [seamapi-types] for the latest TypeScript types.
This package depends on [@seamapi/types] for the latest TypeScript types.
New versions of this SDK are generally not released when new types are published.
Unless your project frequently runs a blanket `npm update`,
the types will become outdated with the Seam API over time.
Thus, users of this package should explicitly install the types with

```
$ npm install -D seamapi-types
$ npm install -D @seamapi/types
```

and update them when consuming new API features with

```
$ npm install -D seamapi-types
$ npm install -D @seamapi/types@latest
```

[seamapi-types]: https://github.com/seamapi/seamapi-types/

## Usage

First, create a webhook using the Seam API or Seam Console
and obtain a Seam webhook secret.

_This example is for [Express], see the [Svix docs for more examples in specific frameworks](https://docs.svix.com/receiving/verifying-payloads/how)._
> [!TIP]
> This example is for [Express], see the [Svix docs for more examples in specific frameworks](https://docs.svix.com/receiving/verifying-payloads/how).
```js
import { SeamWebhook } from '@seamapi/webhook'
Expand Down
52 changes: 26 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,18 @@
"npm": ">= 9.0.0"
},
"peerDependencies": {
"seamapi-types": "^1.42.0",
"type-fest": "^4.0.0"
"@seamapi/types": "^1.166.0"
},
"peerDependenciesMeta": {
"seamapi-types": {
"optional": true
},
"type-fest": {
"@seamapi/types": {
"optional": true
}
},
"dependencies": {
"svix": "^1.15.0"
},
"devDependencies": {
"@seamapi/types": "^1.166.0",
"@types/node": "^20.8.10",
"ava": "^6.0.1",
"c8": "^9.0.0",
Expand All @@ -92,11 +89,9 @@
"eslint-plugin-simple-import-sort": "^12.0.0",
"eslint-plugin-unused-imports": "^3.0.0",
"prettier": "^3.0.0",
"seamapi-types": "^1.42.0",
"tsc-alias": "^1.8.2",
"tsup": "^8.0.1",
"tsx": "^4.6.2",
"type-fest": "^4.14.0",
"typescript": "^5.1.0"
}
}
15 changes: 3 additions & 12 deletions src/lib/seam-webhook.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
import type { SeamEvent } from 'seamapi-types'
import type { SeamEvent } from '@seamapi/types/connect'
import { Webhook } from 'svix'

// UPSTREAM: This type should come from @seamapi/types.
export type SeamWebhookEvent = Distribute<SeamEvent['event_type']>

export class SeamWebhook {
readonly #webhook: Webhook

constructor(secret: string) {
this.#webhook = new Webhook(secret)
}

verify(payload: string, headers: Record<string, string>): SeamWebhookEvent {
return this.#webhook.verify(payload, headers) as SeamWebhookEvent
verify(payload: string, headers: Record<string, string>): SeamEvent {
return this.#webhook.verify(payload, headers) as SeamEvent
}
}

type Distribute<EventType> = EventType extends SeamEvent['event_type']
? {
event_type: EventType
} & Extract<SeamEvent, { event_type: EventType }>['payload']
: never

0 comments on commit f9dc262

Please sign in to comment.