@octokit/webhooks
Advanced tools
| import { emitterEventNames } from "../generated/webhook-names.js"; | ||
| function validateEventName(eventName, options = {}) { | ||
| if (typeof eventName !== "string") { | ||
| throw new TypeError("eventName must be of type string"); | ||
| } | ||
| if (eventName === "*") { | ||
| throw new TypeError( | ||
| `Using the "*" event with the regular Webhooks.on() function is not supported. Please use the Webhooks.onAny() method instead` | ||
| ); | ||
| } | ||
| if (eventName === "error") { | ||
| throw new TypeError( | ||
| `Using the "error" event with the regular Webhooks.on() function is not supported. Please use the Webhooks.onError() method instead` | ||
| ); | ||
| } | ||
| if (options.onUnknownEventName === "ignore") { | ||
| return; | ||
| } | ||
| if (!emitterEventNames.includes(eventName)) { | ||
| if (options.onUnknownEventName !== "warn") { | ||
| throw new TypeError( | ||
| `"${eventName}" is not a known webhook name (https://developer.github.com/v3/activity/events/types/)` | ||
| ); | ||
| } else { | ||
| (options.log || console).warn( | ||
| `"${eventName}" is not a known webhook name (https://developer.github.com/v3/activity/events/types/)` | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| export { | ||
| validateEventName | ||
| }; |
| import type { Logger } from "../create-logger.ts"; | ||
| import type { EmitterWebhookEventName } from "../types.ts"; | ||
| type ValidateEventNameOptions = { | ||
| onUnknownEventName?: undefined | "throw"; | ||
| } | { | ||
| onUnknownEventName: "ignore"; | ||
| } | { | ||
| onUnknownEventName: "warn"; | ||
| log?: Pick<Logger, "warn">; | ||
| }; | ||
| export declare function validateEventName<TOptions extends ValidateEventNameOptions = ValidateEventNameOptions>(eventName: EmitterWebhookEventName | (string & Record<never, never>), options?: TOptions): asserts eventName is TOptions extends { | ||
| onUnknownEventName: "throw"; | ||
| } ? EmitterWebhookEventName : Exclude<string, "*" | "error">; | ||
| export {}; |
+37
-11
@@ -332,2 +332,33 @@ // pkg/dist-src/create-logger.js | ||
| // pkg/dist-src/event-handler/validate-event-name.js | ||
| function validateEventName(eventName, options = {}) { | ||
| if (typeof eventName !== "string") { | ||
| throw new TypeError("eventName must be of type string"); | ||
| } | ||
| if (eventName === "*") { | ||
| throw new TypeError( | ||
| `Using the "*" event with the regular Webhooks.on() function is not supported. Please use the Webhooks.onAny() method instead` | ||
| ); | ||
| } | ||
| if (eventName === "error") { | ||
| throw new TypeError( | ||
| `Using the "error" event with the regular Webhooks.on() function is not supported. Please use the Webhooks.onError() method instead` | ||
| ); | ||
| } | ||
| if (options.onUnknownEventName === "ignore") { | ||
| return; | ||
| } | ||
| if (!emitterEventNames.includes(eventName)) { | ||
| if (options.onUnknownEventName !== "warn") { | ||
| throw new TypeError( | ||
| `"${eventName}" is not a known webhook name (https://developer.github.com/v3/activity/events/types/)` | ||
| ); | ||
| } else { | ||
| (options.log || console).warn( | ||
| `"${eventName}" is not a known webhook name (https://developer.github.com/v3/activity/events/types/)` | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| // pkg/dist-src/event-handler/on.js | ||
@@ -347,12 +378,6 @@ function handleEventHandlers(state, webhookName, handler) { | ||
| } | ||
| if (["*", "error"].includes(webhookNameOrNames)) { | ||
| const webhookName = webhookNameOrNames === "*" ? "any" : webhookNameOrNames; | ||
| const message = `Using the "${webhookNameOrNames}" event with the regular Webhooks.on() function is not supported. Please use the Webhooks.on${webhookName.charAt(0).toUpperCase() + webhookName.slice(1)}() method instead`; | ||
| throw new Error(message); | ||
| } | ||
| if (!emitterEventNames.includes(webhookNameOrNames)) { | ||
| state.log.warn( | ||
| `"${webhookNameOrNames}" is not a known webhook name (https://developer.github.com/v3/activity/events/types/)` | ||
| ); | ||
| } | ||
| validateEventName(webhookNameOrNames, { | ||
| onUnknownEventName: "warn", | ||
| log: state.log | ||
| }); | ||
| handleEventHandlers(state, webhookNameOrNames, handler); | ||
@@ -810,3 +835,4 @@ } | ||
| createWebMiddleware, | ||
| emitterEventNames | ||
| emitterEventNames, | ||
| validateEventName | ||
| }; |
@@ -1,2 +0,2 @@ | ||
| import { emitterEventNames } from "../generated/webhook-names.js"; | ||
| import { validateEventName } from "./validate-event-name.js"; | ||
| function handleEventHandlers(state, webhookName, handler) { | ||
@@ -15,12 +15,6 @@ if (!state.hooks[webhookName]) { | ||
| } | ||
| if (["*", "error"].includes(webhookNameOrNames)) { | ||
| const webhookName = webhookNameOrNames === "*" ? "any" : webhookNameOrNames; | ||
| const message = `Using the "${webhookNameOrNames}" event with the regular Webhooks.on() function is not supported. Please use the Webhooks.on${webhookName.charAt(0).toUpperCase() + webhookName.slice(1)}() method instead`; | ||
| throw new Error(message); | ||
| } | ||
| if (!emitterEventNames.includes(webhookNameOrNames)) { | ||
| state.log.warn( | ||
| `"${webhookNameOrNames}" is not a known webhook name (https://developer.github.com/v3/activity/events/types/)` | ||
| ); | ||
| } | ||
| validateEventName(webhookNameOrNames, { | ||
| onUnknownEventName: "warn", | ||
| log: state.log | ||
| }); | ||
| handleEventHandlers(state, webhookNameOrNames, handler); | ||
@@ -27,0 +21,0 @@ } |
@@ -5,2 +5,3 @@ import { createLogger } from "./create-logger.js"; | ||
| } from "./event-handler/index.js"; | ||
| import { validateEventName } from "./event-handler/validate-event-name.js"; | ||
| import { sign, verify } from "@octokit/webhooks-methods"; | ||
@@ -46,3 +47,4 @@ import { verifyAndReceive } from "./verify-and-receive.js"; | ||
| createWebMiddleware, | ||
| emitterEventNames | ||
| emitterEventNames, | ||
| validateEventName | ||
| }; |
| import { createEventHandler } from "./event-handler/index.ts"; | ||
| import { validateEventName } from "./event-handler/validate-event-name.ts"; | ||
| import type { EmitterWebhookEvent, EmitterWebhookEventName, HandlerFunction, RemoveHandlerFunction, Options, WebhookError, WebhookEventHandlerError, EmitterWebhookEventWithStringPayloadAndSignature } from "./types.ts"; | ||
@@ -19,2 +20,2 @@ export { createNodeMiddleware } from "./middleware/node/index.ts"; | ||
| } | ||
| export { createEventHandler, Webhooks, type EmitterWebhookEvent, type EmitterWebhookEventName, type WebhookError, }; | ||
| export { createEventHandler, validateEventName, Webhooks, type EmitterWebhookEvent, type EmitterWebhookEventName, type WebhookError, }; |
+1
-1
| { | ||
| "name": "@octokit/webhooks", | ||
| "version": "14.0.2", | ||
| "version": "14.1.0", | ||
| "description": "GitHub webhook events toolset for Node.js", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
+42
-12
@@ -25,2 +25,3 @@ # @octokit/webhooks | ||
| - [emitterEventNames](#emittereventnames) | ||
| - [validateEventName](#validateeventname) | ||
| - [TypeScript](#typescript) | ||
@@ -91,14 +92,15 @@ - [`EmitterWebhookEventName`](#emitterwebhookeventname) | ||
| 1. [Constructor](#constructor) | ||
| 2. [webhooks.sign()](#webhookssign) | ||
| 3. [webhooks.verify()](#webhooksverify) | ||
| 4. [webhooks.verifyAndReceive()](#webhooksverifyandreceive) | ||
| 5. [webhooks.receive()](#webhooksreceive) | ||
| 6. [webhooks.on()](#webhookson) | ||
| 7. [webhooks.onAny()](#webhooksonany) | ||
| 8. [webhooks.onError()](#webhooksonerror) | ||
| 9. [webhooks.removeListener()](#webhooksremovelistener) | ||
| 10. [createNodeMiddleware()](#createnodemiddleware) | ||
| 11. [createWebMiddleware()](#createwebmiddleware) | ||
| 12. [Webhook events](#webhook-events) | ||
| 13. [emitterEventNames](#emittereventnames) | ||
| 1. [webhooks.sign()](#webhookssign) | ||
| 1. [webhooks.verify()](#webhooksverify) | ||
| 1. [webhooks.verifyAndReceive()](#webhooksverifyandreceive) | ||
| 1. [webhooks.receive()](#webhooksreceive) | ||
| 1. [webhooks.on()](#webhookson) | ||
| 1. [webhooks.onAny()](#webhooksonany) | ||
| 1. [webhooks.onError()](#webhooksonerror) | ||
| 1. [webhooks.removeListener()](#webhooksremovelistener) | ||
| 1. [createNodeMiddleware()](#createnodemiddleware) | ||
| 1. [createWebMiddleware()](#createwebmiddleware) | ||
| 1. [Webhook events](#webhook-events) | ||
| 1. [emitterEventNames](#emittereventnames) | ||
| 1. [validateEventName](#validateeventname) | ||
@@ -729,2 +731,30 @@ ### Constructor | ||
| ### validateEventName | ||
| The function `validateEventName` asserts that the provided event name is a valid event name or event/action combination. | ||
| It throws an error if the event name is not valid, or '\*' or 'error' is passed. | ||
| The second parameter is an optional options object that can be used to customize the behavior of the validation. You can set | ||
| a `onUnknownEventName` property to `"warn"` to log a warning instead of throwing an error, and a `log` property to provide a custom logger object, which should have a `"warn"` method. You can also set `onUnknownEventName` to `"ignore"` to disable logging or throwing an error for unknown event names. | ||
| ```ts | ||
| import { validateEventName } from "@octokit/webhooks"; | ||
| validateEventName("push"); // no error | ||
| validateEventName("invalid_event"); // throws an error | ||
| validateEventName("*"); // throws an error | ||
| validateEventName("error"); // throws an error | ||
| validateEventName("invalid_event", { onUnknownEventName: "warn" }); // logs a warning | ||
| validateEventName("invalid_event", { | ||
| onUnknownEventName: false, | ||
| log: { | ||
| warn: console.info, // instead of warning we just log it via console.info | ||
| }, | ||
| }); | ||
| validateEventName("*", { onUnkownEventName: "ignore" }); // throws an error | ||
| validateEventName("invalid_event", { onUnkownEventName: "ignore" }); // no error, no warning | ||
| ``` | ||
| ## TypeScript | ||
@@ -731,0 +761,0 @@ |
Sorry, the diff of this file is not supported yet
181437
2.61%49
4.26%1981
3.61%786
3.97%