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.
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);