yaschema-ws-api-client
TypeScript icon, indicating that this package has built-in type declarations

2.1.9 • Public • Published

yaschema-ws-api-client

Downloads Size

Client supporting WebSockets via yaschema-ws-api

Basic Example

export const stream = makeWsApi({
  routeType: 'stream',
  url: '/stream',
  schemas: {
    requests: {
      ping: schema.object({ echo: schema.string().allowEmptyString().optional() }).optional()
    },
    responses: {
      pong: schema.object({
        body: schema.string()
      })
    }
  }
});
const connection = await apiWs(
  stream,
  {},
  {
    pong: async ({ input }) => {
      console.log(`GOT ${input.body}`)
    }
  }
);

await connection.output.ping({ echo: 'Hello World!' });

The above example demonstrates basic use of a yaschema-ws-api-client, which lets you define runtime and compile-time types for bidirectionally working with WebSockets. See the express-yaschema-ws-api-handler package for adding handlers to Express.

This library supports web-based WebSockets by default and supports the Node ws package by calling:

setWebSocket(WebSocket as any as CommonWebSocket);

While the Node ws WebSocket interface doesn't perfectly align with the web equivalent, for the aspects we use, it does.

Thanks

Thanks for checking it out. Feel free to create issues or otherwise provide feedback.

API Docs

Be sure to check out our other TypeScript OSS projects as well.

Package Sidebar

Install

npm i yaschema-ws-api-client

Weekly Downloads

8

Version

2.1.9

License

MIT

Unpacked Size

111 kB

Total Files

136

Last publish

Collaborators

  • bwestphal