4.2.7 • Published 4 months ago

express-yaschema-ws-api-handler v4.2.7

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

express-yaschema-ws-api-handler

Downloads Size

Express support for handling WebSocket APIs declared using yaschema-ws-api.

Basic Example

// API schema and metadata
// You'll typically define this in a separate package shared by your server and clients
export const stream = makeWsApi({
  routeType: 'stream',
  url: '/stream',
  requests: {
    ping: schema.object({ echo: schema.string().allowEmptyString().optional() }).optional(),
    hello: schema.any().optional()
  },
  responses: {
    pong: schema.object({
      body: schema.string()
    }),
    hello: schema.object({
      body: schema.string()
    })
  }
});
import expressWS, { WithWebsocketMethod } from 'express-ws';

…

// Add WebSocket support to Express
expressWs(app);
const appWithWs = app as typeof app & WithWebsocketMethod;
registerWsApiHandler(
  appWithWs,
  api.stream,
  {},
  {
    ping: async ({ input, output }) => output.pong({ body: `PONG${(input?.echo?.length ?? 0) > 0 ? ' ' : ''}${input?.echo ?? ''}` }),
    hello: async ({ output }) => output.hello({ body: 'world' })
  }
)

The options object passed to registerWsApiHandler lets you override the validation mode and/or specify middleware.

Initializing Multiple Express Servers Simultaneously

If you happen to be potentially initializing multiple Express servers simultaneously (e.g. on different ports) and you're using Yaschema API for more than one of these servers, you will need to extend your Express instance to support YaschemaApiExpressContextAccessor by calling addYaschemaApiExpressContextAccessorToExpress(express, context) before calling registerWsApiHandler -- where context should be unique for each Express server, and can be created using makeYaschemaApiExpressContext.

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.

4.2.7

4 months ago

4.2.6

4 months ago

4.2.5

6 months ago

4.2.4

6 months ago

4.2.3

7 months ago

4.2.2

7 months ago

4.2.1

7 months ago

4.2.0

8 months ago

4.0.2

8 months ago

4.1.0

8 months ago

4.0.1

8 months ago

4.0.0

8 months ago

2.2.10

8 months ago

2.2.7

10 months ago

2.2.9

9 months ago

2.2.8

9 months ago

2.2.5

11 months ago

2.2.4

11 months ago

2.2.1

1 year ago

2.2.0

1 year ago

2.2.3

1 year ago

2.2.2

1 year ago

2.1.17

1 year ago

2.1.16

1 year ago

2.1.15

1 year ago

2.1.14

1 year ago

2.1.13

1 year ago

2.1.12

1 year ago

2.1.10

1 year ago

2.1.11

1 year ago

2.1.9

1 year ago

2.1.7

1 year ago

2.1.6

1 year ago

2.1.5

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

2.0.0

2 years ago

1.2.0

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

0.0.0-alpha.10

2 years ago

0.0.0-alpha.9

2 years ago

0.0.0-alpha.8

2 years ago

0.0.0-alpha.7

2 years ago

0.0.0-alpha.6

2 years ago

0.0.0-alpha.5

2 years ago

0.0.0-alpha.4

2 years ago

0.0.0-alpha.3

2 years ago

0.0.0-alpha.2

2 years ago

0.0.0-alpha.1

2 years ago

0.0.0-alpha.0

2 years ago