
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
zod-custom-events
Advanced tools
Type safe, framework agnostic, Zod based, custom events extension library.
CustomEvents
with a custom APIZod
schemaJavaScript
environmentCustomEvent
native properties and methods inherited by the Event
interfaceInstall zod-custom-events using your favorite package manager or CDN, then include it in your project:
# Install with npm
npm install zod-custom-events
# Install with pnpm
pnpm add zod-custom-events
# Install with yarn
yarn add zod-custom-events
# Install with bun
bun add zod-custom-events
# Install in a node project
npx jsr add @georgecht/zod-custom-events
# Install in a deno project
deno add jsr:@georgecht/zod-custom-events
# Install in a bun project
bunx jsr add @georgecht/zod-custom-events
<!-- Via jsdelivr -->
<script src="https://cdn.jsdelivr.net/npm/zod-custom-events@latest/dist/index.min.js"></script>
<!-- Via unpkg -->
<script src="https://www.unpkg.com/zod-custom-events/dist/index.min.js"></script>
Here's a basic example of how to use zod-custom-events:
import { z } from 'zod';
import { EventController } from 'zod-custom-events';
// Define your event schema
const userSchema = z.object({
id: z.number(),
email: z.email(),
});
// Create an event controller
const userEventController = new EventController(userSchema, 'user-event');
// Subscribe to the event
userEventController.subscribe((event) => {
console.log('Received user event:', event.detail);
});
// Dispatch an event
userEventController.dispatch({
id: 123,
email: 'my@email.com',
});
// Cleanup
userEventController.unsubscribe();
The API reference is available on GitHub.
The main class for managing custom events. The EventController<T extends ZodSchema>
extends the CustomEvent
interface with a detail
property of type T
. Meaning it will match the CustomEvent
interface and infer all the functionality.
constructor(schema: T, eventName: string, options?: EventControllerOptions<T>)
Creates a new EventController
instance.
schema
- The Zod schema for validating the event payload.eventName
- The name of the custom event.options
optional - Configuration options for the EventController
.import { z } from 'zod';
import { EventController } from 'zod-custom-events';
const schema = z.object({
name: z.string(),
});
const controller = new EventController(schema, 'myEvent', {
onError: ({ error }) => console.error('Validation error:', error),
onDispatch: ({ payload }) => console.log('Dispatching event with payload:', payload),
});
element
optional - The element to bind the event to. Defaults to window
.onError
optional - Error handler for validation errors.onDispatch
optional - Callback function called before dispatching the event.onSubscribe
optional - Callback function called when the event listener is added.onUnsubscribe
optional - Callback function called when the event listener is removed.Subscribe to the event.
subscribe(listener: (event: TypedCustomEvent<EventPayload<T>>) => void, options?: AddEventListenerOptions): void
listener
- The function to be called when the event is triggered.options
- Optional parameters for the event listener.once
optional - A boolean value indicating that the listener
should be invoked at most once
after being added. If true
, the listener
would be automatically removed when invoked.passive
optional - A boolean indicating whether the event listener is a passive
listener. If set to true
, indicates that the function specified by listener will never call preventDefault()
. If a passive listener calls preventDefault()
, nothing will happen and a console warning may be generated.signal
optional - An AbortSignal
to signal when the listener should be removed.controller.subscribe((event) => {
console.log('Received user event:', event.detail);
});
// With abort signal and once option
const abortController = new AbortController();
const { signal } = abortController;
controller.subscribe((event) => {
console.log('Received user event:', event.detail);
}, { signal, once: true });
// Later in the code
abortController.abort();
Removes the previously registered event listener for the event.
unsubscribe(options?: EventListenerOptions): void
options
optional - Optional parameters to match the event listener.capture
optional - A boolean value indicating that events of this type will be dispatched to the registered listener
before being dispatched to any EventTarget
beneath it in the DOM tree.controller.unsubscribe();
Dispatches the event, validating its payload using the Zod schema and applying middleware.
dispatch(payload: EventPayload<T>, eventInitDict?: CustomEventInit<EventPayload<T>>): Promise<void>
payload
- The data associated with the event. Validated against the Zod
schema initially provided.eventInitDict
optional - Optional parameters for initializing the event.eventInitDict
optionsbubbles
optional - A boolean value indicating whether or not the event can bubble through the DOM.cancelable
optional - A boolean value indicating whether the event can be cancelled.composed
optional - A boolean value indicating whether the event will trigger listeners outside of a shadow root (see Event.composed
for more details).controller.dispatch({
id: 1,
name: 'John Doe',
}, {
bubbles: true
});
Sets a condition for the event, allowing control over whether the event is dispatched.
refine(condition: (payload: EventPayload<T>) => boolean, callback?: (ctx: EventPayloadContext<T>) => void): void
condition
- A function that takes the event payload as input and returns a boolean.callback
optional - An optional callback function to be called when the condition is not met.controller.refine(
(payload) => payload.id > 0,
(ctx) => {
const { payload } = ctx;
console.log("Invalid user ID:", payload.id)
}
);
Updates the EventController
options.
update(options: Partial<EventControllerOptions<T>>): void
options
optional - New configuration options for the EventController
.const controller = new EventController(schema, 'myEvent');
// Later in the code
controller.update({
onError: ({ error }) => console.warn('New error handler:', error),
onDispatch: ({ payload }) => console.log('New dispatch handler:', payload),
});
Adds a middleware function to the event processing pipeline.
use(middleware: Middleware<EventPayload<T>>): void
middleware
- A function that processes the event context and calls the next middleware.controller.use(async (ctx, next) => {
console.log("Processing user event:", ctx.payload);
await next();
});
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for more details.
FAQs
Type safe, framework agnostic, Zod based, custom events extension library.
We found that zod-custom-events demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.