github-webhook-schemas 0.1.0 → 0.1.1
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/README.md +142 -92
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
Zod schemas for validating GitHub webhook payloads with full TypeScript support.
|
|
4
4
|
|
|
5
|
+
## What this package provides
|
|
6
|
+
|
|
7
|
+
- Runtime validation for GitHub webhook payloads using Zod
|
|
8
|
+
- One schema per event or action (for example, `PushEventSchema`, `PullRequestOpenedEventSchema`)
|
|
9
|
+
- Shared schemas for common objects (for example, `Repository`, `User`)
|
|
10
|
+
- Type guards and exported TypeScript types for every schema
|
|
11
|
+
- Tree-shakeable subpath exports
|
|
12
|
+
|
|
13
|
+
This package does not:
|
|
14
|
+
|
|
15
|
+
- verify webhook signatures
|
|
16
|
+
- run an HTTP server or route requests
|
|
17
|
+
|
|
5
18
|
## Installation
|
|
6
19
|
|
|
7
20
|
```bash
|
|
@@ -15,20 +28,16 @@ pnpm add github-webhook-schemas zod
|
|
|
15
28
|
bun add github-webhook-schemas zod
|
|
16
29
|
```
|
|
17
30
|
|
|
18
|
-
|
|
31
|
+
Notes:
|
|
19
32
|
|
|
20
|
-
|
|
33
|
+
- `zod` is a peer dependency and must be installed separately.
|
|
34
|
+
- This package is ESM only.
|
|
21
35
|
|
|
22
|
-
|
|
36
|
+
## Quick start
|
|
23
37
|
|
|
24
|
-
```
|
|
25
|
-
import {
|
|
26
|
-
PushEventSchema,
|
|
27
|
-
PullRequestOpenedEventSchema,
|
|
28
|
-
IssuesOpenedEventSchema,
|
|
29
|
-
} from 'github-webhook-schemas';
|
|
38
|
+
```ts
|
|
39
|
+
import { PushEventSchema } from 'github-webhook-schemas';
|
|
30
40
|
|
|
31
|
-
// Validate a webhook payload
|
|
32
41
|
const result = PushEventSchema.safeParse(payload);
|
|
33
42
|
|
|
34
43
|
if (result.success) {
|
|
@@ -38,113 +47,154 @@ if (result.success) {
|
|
|
38
47
|
}
|
|
39
48
|
```
|
|
40
49
|
|
|
41
|
-
|
|
50
|
+
## Importing schemas
|
|
51
|
+
|
|
52
|
+
Import from the main entry point for convenience:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import {
|
|
56
|
+
IssuesOpenedEventSchema,
|
|
57
|
+
PullRequestOpenedEventSchema,
|
|
58
|
+
PushEventSchema,
|
|
59
|
+
} from 'github-webhook-schemas';
|
|
60
|
+
```
|
|
42
61
|
|
|
43
|
-
|
|
62
|
+
Import from subpaths for smaller bundles:
|
|
44
63
|
|
|
45
|
-
```
|
|
64
|
+
```ts
|
|
46
65
|
import { PushEventSchema } from 'github-webhook-schemas/push-event';
|
|
47
66
|
import { PullRequestOpenedEventSchema } from 'github-webhook-schemas/pull-request-opened-event';
|
|
48
67
|
```
|
|
49
68
|
|
|
50
|
-
|
|
69
|
+
Shared schemas live under `shared/`:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { RepositorySchema } from 'github-webhook-schemas/shared/repository';
|
|
73
|
+
import { UserSchema } from 'github-webhook-schemas/shared/user';
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Event names and actions
|
|
77
|
+
|
|
78
|
+
GitHub sends event names in the `x-github-event` header and includes `action` on many
|
|
79
|
+
payloads. This library exposes schemas for specific actions (for example, opened, closed).
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import {
|
|
83
|
+
PullRequestClosedEventSchema,
|
|
84
|
+
PullRequestOpenedEventSchema,
|
|
85
|
+
} from 'github-webhook-schemas';
|
|
86
|
+
|
|
87
|
+
function parsePullRequest(payload: unknown) {
|
|
88
|
+
const action = (payload as { action?: string }).action;
|
|
89
|
+
|
|
90
|
+
switch (action) {
|
|
91
|
+
case 'opened':
|
|
92
|
+
return PullRequestOpenedEventSchema.parse(payload);
|
|
93
|
+
case 'closed':
|
|
94
|
+
return PullRequestClosedEventSchema.parse(payload);
|
|
95
|
+
default:
|
|
96
|
+
throw new Error(`Unsupported action: ${action ?? 'unknown'}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
```
|
|
51
100
|
|
|
52
|
-
|
|
101
|
+
You can also access the canonical event names list:
|
|
53
102
|
|
|
54
|
-
```
|
|
103
|
+
```ts
|
|
104
|
+
import { eventTypes } from 'github-webhook-schemas';
|
|
105
|
+
|
|
106
|
+
console.log(eventTypes);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Type guards and TypeScript types
|
|
110
|
+
|
|
111
|
+
Every schema exports:
|
|
112
|
+
|
|
113
|
+
- `XxxEventSchema` (the Zod schema)
|
|
114
|
+
- `XxxEvent` (the TypeScript type)
|
|
115
|
+
- `isXxxEvent` (a type guard)
|
|
116
|
+
|
|
117
|
+
Example:
|
|
118
|
+
|
|
119
|
+
```ts
|
|
55
120
|
import { isPushEvent, type PushEvent } from 'github-webhook-schemas/push-event';
|
|
56
121
|
|
|
57
122
|
function handleWebhook(payload: unknown) {
|
|
58
123
|
if (isPushEvent(payload)) {
|
|
59
|
-
|
|
60
|
-
console.log(`Push to ${
|
|
124
|
+
const event: PushEvent = payload;
|
|
125
|
+
console.log(`Push to ${event.repository.full_name}`);
|
|
61
126
|
}
|
|
62
127
|
}
|
|
63
128
|
```
|
|
64
129
|
|
|
65
|
-
|
|
130
|
+
You can also infer types directly from a schema:
|
|
66
131
|
|
|
67
|
-
|
|
132
|
+
```ts
|
|
133
|
+
import { z } from 'zod';
|
|
134
|
+
import { PushEventSchema } from 'github-webhook-schemas/push-event';
|
|
68
135
|
|
|
69
|
-
|
|
70
|
-
import { RepositorySchema } from 'github-webhook-schemas/shared/repository';
|
|
71
|
-
import { UserSchema } from 'github-webhook-schemas/shared/user';
|
|
136
|
+
type PushEvent = z.infer<typeof PushEventSchema>;
|
|
72
137
|
```
|
|
73
138
|
|
|
74
|
-
##
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
- Branch protection events
|
|
79
|
-
- Check run/suite events
|
|
80
|
-
- Code scanning alerts
|
|
81
|
-
- Commit comments
|
|
82
|
-
- Deployments
|
|
83
|
-
- Discussions
|
|
84
|
-
- Forks
|
|
85
|
-
- Issues and issue comments
|
|
86
|
-
- Labels
|
|
87
|
-
- Milestones
|
|
88
|
-
- Organizations
|
|
89
|
-
- Packages
|
|
90
|
-
- Pull requests and reviews
|
|
91
|
-
- Pushes
|
|
92
|
-
- Releases
|
|
93
|
-
- Repositories
|
|
94
|
-
- Secret scanning alerts
|
|
95
|
-
- Security advisories
|
|
96
|
-
- Sponsorships
|
|
97
|
-
- Stars
|
|
98
|
-
- Teams
|
|
99
|
-
- Workflows
|
|
100
|
-
- And more...
|
|
101
|
-
|
|
102
|
-
## TypeScript Support
|
|
103
|
-
|
|
104
|
-
All schemas export their inferred types:
|
|
105
|
-
|
|
106
|
-
```typescript
|
|
107
|
-
import { PushEventSchema, type PushEvent } from 'github-webhook-schemas/push-event';
|
|
108
|
-
|
|
109
|
-
// Use the type directly
|
|
110
|
-
function processPush(event: PushEvent) {
|
|
111
|
-
console.log(`${event.pusher.name} pushed to ${event.ref}`);
|
|
112
|
-
}
|
|
139
|
+
## Working with Zod
|
|
140
|
+
|
|
141
|
+
These are regular Zod schemas, so you can compose them as needed:
|
|
113
142
|
|
|
114
|
-
|
|
115
|
-
|
|
143
|
+
```ts
|
|
144
|
+
import { PushEventSchema } from 'github-webhook-schemas/push-event';
|
|
145
|
+
|
|
146
|
+
const MinimalPushSchema = PushEventSchema.pick({
|
|
147
|
+
ref: true,
|
|
148
|
+
repository: true,
|
|
149
|
+
}).required();
|
|
116
150
|
```
|
|
117
151
|
|
|
118
|
-
##
|
|
152
|
+
## Development
|
|
153
|
+
|
|
154
|
+
This project is built and tested with Bun.
|
|
155
|
+
|
|
156
|
+
### Prerequisites
|
|
157
|
+
|
|
158
|
+
- Bun >= 1.3.0
|
|
159
|
+
|
|
160
|
+
### Repo layout
|
|
119
161
|
|
|
120
|
-
```typescript
|
|
121
|
-
import { PushEventSchema, isPushEvent } from 'github-webhook-schemas/push-event';
|
|
122
|
-
import {
|
|
123
|
-
PullRequestOpenedEventSchema,
|
|
124
|
-
isPullRequestOpenedEvent,
|
|
125
|
-
} from 'github-webhook-schemas/pull-request-opened-event';
|
|
126
|
-
|
|
127
|
-
async function handleGitHubWebhook(request: Request) {
|
|
128
|
-
const event = request.headers.get('x-github-event');
|
|
129
|
-
const payload = await request.json();
|
|
130
|
-
|
|
131
|
-
switch (event) {
|
|
132
|
-
case 'push': {
|
|
133
|
-
const result = PushEventSchema.safeParse(payload);
|
|
134
|
-
if (result.success) {
|
|
135
|
-
await handlePush(result.data);
|
|
136
|
-
}
|
|
137
|
-
break;
|
|
138
|
-
}
|
|
139
|
-
case 'pull_request': {
|
|
140
|
-
if (isPullRequestOpenedEvent(payload)) {
|
|
141
|
-
await handlePullRequestOpened(payload);
|
|
142
|
-
}
|
|
143
|
-
break;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
162
|
```
|
|
163
|
+
src/
|
|
164
|
+
index.ts # main entry point
|
|
165
|
+
schemas/ # generated event schemas and tests
|
|
166
|
+
schemas/shared/ # generated shared schemas
|
|
167
|
+
scripts/
|
|
168
|
+
generate-webhook-schemas.ts # generates schemas from @octokit/webhooks-types
|
|
169
|
+
update-exports.ts # keeps package.json exports in sync
|
|
170
|
+
build.ts # builds dist/ and type declarations
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Key scripts
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
bun run generate:schemas # regenerate src/schemas from @octokit/webhooks-types
|
|
177
|
+
bun run update:exports # update package.json exports after schema changes
|
|
178
|
+
bun run build # build dist/ and .d.ts files
|
|
179
|
+
bun run test # run tests
|
|
180
|
+
bun run lint # lint the codebase
|
|
181
|
+
bun run typecheck # run TypeScript type checking
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Regenerating schemas
|
|
185
|
+
|
|
186
|
+
The schemas are generated from `@octokit/webhooks-types`. When GitHub adds or changes
|
|
187
|
+
event payloads:
|
|
188
|
+
|
|
189
|
+
1. Update the `@octokit/webhooks-types` version in `package.json`.
|
|
190
|
+
2. Run `bun install`.
|
|
191
|
+
3. Run `bun run generate:schemas`.
|
|
192
|
+
4. Run `bun run update:exports`.
|
|
193
|
+
5. Run `bun run test` and `bun run build` to validate output.
|
|
194
|
+
|
|
195
|
+
Do not hand edit files in `src/schemas` or `src/schemas/shared`. They are overwritten
|
|
196
|
+
on every generation run. If you need to change output, update
|
|
197
|
+
`scripts/generate-webhook-schemas.ts` instead.
|
|
148
198
|
|
|
149
199
|
## License
|
|
150
200
|
|