datastar/sdk/typescript
Mateusz Knapik a3a05c993c Fix exports 2025-04-09 23:52:21 +00:00
..
examples Typescript sdk standardization (#700) 2025-02-26 07:33:57 -06:00
src Unify types and move to types.ts file 2025-04-09 23:52:10 +00:00
.gitignore Ts sdk snippets (#483) 2025-01-15 11:10:47 -06:00
README.md Typescript sdk improvements (#698) 2025-02-23 17:11:35 -06:00
build.ts Fix exports 2025-04-09 23:52:21 +00:00
deno.json Ts sdk snippets (#483) 2025-01-15 11:10:47 -06:00
deno.lock Sdk ts web improvements (#551) 2025-02-04 16:39:41 -06:00
package.json Typescript sdk standardization (#700) 2025-02-26 07:33:57 -06:00
pnpm-lock.yaml Sdk ts web improvements (#551) 2025-02-04 16:39:41 -06:00
tsconfig.json Make plugin order depth-first per element, then per `data` attribute (#499) 2025-01-20 12:08:34 -08:00

README.md

TypeScript SDK for Datastar

Implements the SDK spec and exposes an abstract ServerSentEventGenerator class that can be used to implement runtime specific classes. NodeJS and web standard runtimes are currently implemented.

Currently it only exposes an http1 server, if you want http2 I recommend you use a reverse proxy until http2 support is added.

Deno is used for building the npm package: deno run -A build.ts VERSION

Usage is straightforward:

// this example is for node
const reader = await ServerSentEventGenerator.readSignals(req);

if (!reader.success) {
    console.error('Error while reading signals', reader.error);
    res.end('Error while reading signals`);
    return;
}

if (!('foo' in reader.signals)) {
    console.error('The foo signal is not present');
    res.end('The foo signal is not present');
    return;
}

ServerSentEventGenerator.stream(req, res, (stream) => {
     stream.mergeSignals({ foo: reader.signals.foo });
     stream.mergeFragments(`<div id="toMerge">Hello <span data-text="$foo">${reader.signals.foo}</span></div>`);
});

The stream static method can receive an extra options object that can contain onError and onAbort callbacks as well as the keepalive option. The keepalive option will stop the stream from being closed once the onStart callback is finished. That means the user is responsible for ending the stream with this.close().

Examples

Follow the links for more complete (and executable) examples

Frameworks / Alternate runtimes

If you can't simply use the node / web versions, then you can extend the abstract class in ./src/abstractServerSentEventGenerator.ts. You will need to provide implementations of the constructor, readSignals, stream and send methods.

Testing

A shell based testing suite is provided; see the readme for more information.

Testing node

Start by building and running the node server

$ deno run -A build.ts xxx
$ node ./npm/esm/node/node.js

Then run the test suite

$ cd ../test
$ ./test-all.sh http://127.0.0.1:3000
Running tests with argument: http://127.0.0.1:3000
Processing GET cases...
Processing POST cases...

Testing deno

Start by running the deno server

$ deno --allow-net  ./src/web/deno.ts

Then run the test suite

$ cd ../test
$ ./test-all.sh http://localhost:8000/
Running tests with argument: http://localhost:8000/
Processing GET cases...
Processing POST cases...