Skip to content

v0.13.0

Choose a tag to compare

@MatthewWid MatthewWid released this 23 Aug 13:49
· 174 commits to master since this release

Better SSE Version 0.13.0

npm install better-sse@latest
yarn add better-sse@latest
pnpm add better-sse@latest

This release adds the ability to pass an initial value for the state property when creating sessions and channels.

Changes

Session and Channel state initialization

You can now initialize the value of the state property in the Session and Channel constructor options objects.

While this enables you to construct and set the session/channel state in a single statement, this also allows the TypeScript compiler to automatically infer the state type from the given state value, rather than you having to explicitly define it by passing a type/interface to the State generic parameter.

Before:

interface SessionState {
  name: string;
}

const session = await createSession<SessionState>(req, res);

session.state.name = "Bob";

session.state.name = 123; // Error!

After:

const session = await createSession(req, res, {
  state: {
    name: "Bob"
  }
});

session.state.name = 123; // Error!

Removed index signature constraint on state type

The state type for sessions and channels is now no longer required to have an index signature, meaning your state can be well-defined to have only the exact properties you specify.

Before:

interface SessionState {
  name: string;
}

const session = await createSession<SessionState>(req, res); // Error! Index signature for type 'string' is missing in type 'SessionState'.

session.state.someUnknownProperty; // unknown

After:

interface SessionState {
  name: string;
}

const session = await createSession<SessionState>(req, res); // Works!

session.state.someUnknownProperty; // Error! Property 'someUnknownProperty' does not exist on type 'SessionState'.

Note that if you do not pass an explicit state type, either via the constructor options or by passing a type/interface to the State generic, the state type will still default to Record<string, unknown>, allowing you to access unknown properties on the state object without an error being thrown.

Full Changelog

Added

  • Added the ability to set an initial value for the state property in the Session and Channel constructor options objects.

Removed

  • Removed constraints that enforced that State generics passed to Session and Channel extend from Record<string, unknown>.