What is @octokit/webhooks-types?
@octokit/webhooks-types is an npm package that provides TypeScript definitions for GitHub webhook events. It helps developers to type-check their code when handling GitHub webhooks, ensuring that they are using the correct event payloads and properties.
What are @octokit/webhooks-types's main functionalities?
Type Definitions for Webhook Events
This feature provides type definitions for various GitHub webhook events, such as 'push', 'pull_request', etc. The code sample demonstrates how to handle a 'push' event with type safety.
const { WebhookEvent } = require('@octokit/webhooks-types');
/**
* Example of handling a 'push' event
* @param {WebhookEvent<'push'>} event - The push event payload
*/
function handlePushEvent(event) {
console.log(`Received a push event for ${event.payload.repository.full_name}`);
console.log(`Pushed by: ${event.payload.pusher.name}`);
}
// Example payload
const pushEventPayload = {
payload: {
repository: { full_name: 'octocat/Hello-World' },
pusher: { name: 'octocat' }
}
};
handlePushEvent(pushEventPayload);
TypeScript Support
This feature ensures that developers can use TypeScript to handle GitHub webhook events with type safety. The code sample demonstrates handling a 'pull_request' event with TypeScript.
import { WebhookEvent } from '@octokit/webhooks-types';
/**
* Example of handling a 'pull_request' event
* @param {WebhookEvent<'pull_request'>} event - The pull request event payload
*/
function handlePullRequestEvent(event: WebhookEvent<'pull_request'>) {
console.log(`Received a pull request event for ${event.payload.repository.full_name}`);
console.log(`Action: ${event.payload.action}`);
}
// Example payload
const pullRequestEventPayload = {
payload: {
repository: { full_name: 'octocat/Hello-World' },
action: 'opened'
}
};
handlePullRequestEvent(pullRequestEventPayload);
Other packages similar to @octokit/webhooks-types
probot
Probot is a framework for building GitHub Apps to automate and improve workflows. It provides tools for handling webhook events, managing app authentication, and more. While Probot offers a higher-level abstraction for building GitHub Apps, @octokit/webhooks-types focuses on providing type definitions for webhook events.
node-github-webhook
node-github-webhook is a lightweight package for handling GitHub webhook events in Node.js. It provides basic functionality for setting up a webhook listener and processing events. Compared to @octokit/webhooks-types, it is more focused on the event handling aspect and does not provide TypeScript type definitions.
Octokit Webhooks
machine-readable, always up-to-date GitHub Webhooks specifications
Download
Download the latest specification at
octokit.github.io/webhooks/payload-examples/api.github.com/index.json
Usage
This package ships with types for the webhook events generated from the
respective json schemas, which you can use like so:
import { WebhookEvent, IssuesOpenedEvent } from "@octokit/webhooks-types";
const handleWebhookEvent = (event: WebhookEvent) => {
if ("action" in event && event.action === "completed") {
console.log(`${event.sender.login} completed something!`);
}
};
const handleIssuesOpenedEvent = (event: IssuesOpenedEvent) => {
console.log(
`${event.sender.login} opened "${event.issue.title}" on ${event.repository.full_name}`,
);
};
⚠️ Caution ⚠️: Webhooks Types are expected to be used with the strictNullChecks
option enabled in your tsconfig
. If you don't have this option enabled, there's the possibility that you get never
as the inferred type in some use cases. See #395 for details.
See also
LICENSE
MIT