UNPKG

@scalar/oas-utils

Version:

Open API spec and Yaml handling utilities

39 lines (38 loc) 1.25 kB
/** * Subscribes a single plugin's `on` listener to the given event bus via `onAny`. * * The plugin's `on` is passed straight through to `bus.onAny`, so it will * receive every event emitted on the bus as a `{ event, payload }` object. * Plugins without an `on` listener get a no-op unsubscribe. * * Returns an unsubscribe function. Call it when the plugin is torn down or * the bus is destroyed to remove the wildcard listener. * * @example * const unsubscribe = subscribePluginEvents(eventBus, plugin) * // later... * unsubscribe() */ export const subscribePluginEvents = (eventBus, plugin) => { if (!plugin.on) { return () => { // no-op }; } return eventBus.onAny(plugin.on); }; /** * Execute any hook with type-safe payload handling. * The payload type is inferred from the hook name to ensure correct usage. */ export const executeHook = async (payload, hookName, plugins) => { let currentPayload = payload; for (const plugin of plugins) { const hook = plugin.hooks?.[hookName]; if (hook) { const modifiedPayload = await hook(currentPayload); currentPayload = (modifiedPayload ?? currentPayload); } } return currentPayload; };