v0.9.0
Better SSE Version 0.9.0
npm install better-sse@latestyarn add better-sse@latestpnpm add better-sse@latestThis release includes some minor fixes and quality-of-life improvements to the TypeScript types shipped with Better SSE.
Changes
⚠ BREAKING: SessionState interface renamed to DefaultSessionState
The exported SessionState interface has been renamed to DefaultSessionState.
This is to avoid confusion as the type of each sessions' state property can vary between different session instances if you provide one as a generic argument to its constructor, whereas the exported interface here is designed to only be used to define the default type if you do not explicitly give one when you create the session.
// Before
declare module "better-sse" {
interface SessionState {
myProperty: string;
}
}// After
declare module "better-sse" {
interface DefaultSessionState {
myProperty: string;
}
}Strong typing for the session state property of sessions registered with a channel
You can now pass an optional second generic argument to the Channel constructor and the createChannel factory function that allows you to define the type of the state property for all Sessions registered to that channel.
This allows you to have strong typing when accessing the state property of sessions in the activeSessions list and in the filter callback option in the broadcast method, as well as allowing you to enforce that only sessions with the same state type can be registered with that channel.
Strong typing for channel session states:
// Before
type AuthedSessionState = { isAdmin: boolean };
const channel = createChannel();
const session = createSession<AuthedSessionState>(req, res);
channel.register(session);
channel.activeSessions[0].state.isAdmin; // `unknown`// After
type AuthedSessionState = { isAdmin: boolean };
const channel = createChannel<*, AuthedSessionState>(); // Provide the type for the state of every session on this channel
const session = createSession<AuthedSessionState>(req, res);
channel.register(session);
channel.activeSessions[0].state.isAdmin; // `boolean`Only sessions with the same state type given to the channel may be registered:
const channel = createChannel<*, { propertyA: string }>();
const session = await createSession<{ propertyB: string }>(req, res);
channel.register(session); // TypeScript error! Only sessions with the same type can be registeredChannels may now have a default state type defined globally
A new DefaultChannelState interface is exported that allows you to use module augmentation and declaration merging to define a default type for the Channel#state property if you do not provide one during construction.
This functions identically to the DefaultSessionState interface, but for Channels instead.
declare module "better-sse" {
interface DefaultChannelState {
myProperty: string;
}
}Full Changelog
Added
- Added the ability to type the
stateproperty of sessions registered with a Channel via an optional second generic argument to theChannelconstructor. - Added the
DefaultChannelStateinterface that may be used via module augmentation to alter the default channel state type for all channels.
Changed
- Update the
SessionStateinterface to be namedDefaultSessionState.