Middleware Authoring
import type { IMiddleware, MiddlewareNext, SunglassesEvent } from '@drakkar.software/sunglasses-core';
class MyMiddleware implements IMiddleware {
readonly name = 'MyMiddleware'; // must be unique
async process(event: SunglassesEvent, next: MiddlewareNext): Promise<SunglassesEvent | null> {
if (shouldDrop(event)) return null;
return next({ ...event, properties: { ...event.properties, extra: 'value' } });
}
}
Rules
- Never throw — return
nullto drop - Always spread when modifying —
{ ...event, properties: { ...event.properties } } - Never log
distinctIdor user traits - Call
next(event)to continue the pipeline - Async work is fine — the pipeline
awaits each middleware
PiiSanitizer is always prepended before your middleware. See Privacy invariants.
Testing
See Testing.