@studyportals/ws-client 0.1.1-beta.0 → 0.1.1-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  export { eventBus } from './event-bus';
2
2
  export type { EventBus } from './event-bus';
3
3
  export { WebSocketManager } from './websocket-manager';
4
- export type { WsEventMap, WsIdentifiedPayload, CampaignSavingPayload, WebSocketManagerConfig, IncomingMessage, } from './types';
5
- export { wsIdentifiedPayloadSchema, campaignSavingPayloadSchema, incomingMessageSchema, } from './types';
4
+ export type { WsEventMap, WsIdentifiedPayload, CampaignSavingPayload, WebSocketManagerConfig, IncomingMessage, ServerEventKey, ServerMessage, ServerMessageParsed, } from './types';
5
+ export { wsIdentifiedPayloadSchema, campaignSavingPayloadSchema, incomingMessageSchema, serverMessageSchema, } from './types';
6
6
  export type { IWebSocketTransport } from './transport.interface';
7
7
  export { RealWebSocketTransport } from './transports/real.transport';
8
8
  export { MockWebSocketTransport } from './transports/mock.transport';
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ export { eventBus } from './event-bus';
3
3
  // Manager
4
4
  export { WebSocketManager } from './websocket-manager';
5
5
  // Zod validators (useful for consumers and tests)
6
- export { wsIdentifiedPayloadSchema, campaignSavingPayloadSchema, incomingMessageSchema, } from './types';
6
+ export { wsIdentifiedPayloadSchema, campaignSavingPayloadSchema, incomingMessageSchema, serverMessageSchema, } from './types';
7
7
  // Transport implementations
8
8
  export { RealWebSocketTransport } from './transports/real.transport';
9
9
  export { MockWebSocketTransport } from './transports/mock.transport';
@@ -6,3 +6,5 @@ export { campaignSavingPayloadSchema } from './campaign-saving';
6
6
  export type { WebSocketManagerConfig } from './ws-manager-config';
7
7
  export type { IncomingMessage } from './ws-message';
8
8
  export { incomingMessageSchema } from './ws-message';
9
+ export type { ServerEventKey, ServerMessage, ServerMessageParsed } from './server-message';
10
+ export { serverMessageSchema } from './server-message';
@@ -1,3 +1,4 @@
1
1
  export { wsIdentifiedPayloadSchema } from './ws-identified';
2
2
  export { campaignSavingPayloadSchema } from './campaign-saving';
3
3
  export { incomingMessageSchema } from './ws-message';
4
+ export { serverMessageSchema } from './server-message';
@@ -0,0 +1,28 @@
1
+ import { z } from 'zod';
2
+ import type { WsEventMap } from './ws-event-map';
3
+ type ClientOnlyEvent = 'ws:connected' | 'ws:disconnected' | 'ws:error' | 'ws:max-retries-exceeded' | 'ws:unparseable';
4
+ export type ServerEventKey = Exclude<keyof WsEventMap, ClientOnlyEvent>;
5
+ export type ServerMessage = {
6
+ [K in ServerEventKey]: {
7
+ type: K;
8
+ payload: WsEventMap[K];
9
+ };
10
+ }[ServerEventKey];
11
+ export declare const serverMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
12
+ type: z.ZodLiteral<"ws:identified">;
13
+ payload: z.ZodObject<{
14
+ connectionId: z.ZodString;
15
+ }, z.core.$strip>;
16
+ }, z.core.$strip>, z.ZodObject<{
17
+ type: z.ZodLiteral<"campaign:saving">;
18
+ payload: z.ZodDiscriminatedUnion<[z.ZodObject<{
19
+ status: z.ZodLiteral<"start">;
20
+ }, z.core.$strip>, z.ZodObject<{
21
+ status: z.ZodLiteral<"success">;
22
+ }, z.core.$strip>, z.ZodObject<{
23
+ status: z.ZodLiteral<"failed">;
24
+ error: z.ZodRecord<z.ZodString, z.ZodUnknown>;
25
+ }, z.core.$strip>], "status">;
26
+ }, z.core.$strip>], "type">;
27
+ export type ServerMessageParsed = z.infer<typeof serverMessageSchema>;
28
+ export {};
@@ -0,0 +1,14 @@
1
+ import { z } from 'zod';
2
+ import { wsIdentifiedPayloadSchema } from './ws-identified';
3
+ import { campaignSavingPayloadSchema } from './campaign-saving';
4
+ // ---------------------------------------------------------------------------
5
+ // serverMessageSchema — Zod discriminated union over every server event.
6
+ // Validates that an incoming frame is both structurally and semantically valid.
7
+ //
8
+ // NOTE: if you extend WsEventMap via module augmentation you must also add
9
+ // the corresponding member to this schema.
10
+ // ---------------------------------------------------------------------------
11
+ export const serverMessageSchema = z.discriminatedUnion('type', [
12
+ z.object({ type: z.literal('ws:identified'), payload: wsIdentifiedPayloadSchema }),
13
+ z.object({ type: z.literal('campaign:saving'), payload: campaignSavingPayloadSchema }),
14
+ ]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@studyportals/ws-client",
3
- "version": "0.1.1-beta.0",
3
+ "version": "0.1.1-beta.1",
4
4
  "description": "WebSocket client with reconnect, heartbeat, and typed event bus",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -12,7 +12,7 @@
12
12
  "scripts": {
13
13
  "build": "tsc",
14
14
  "type-check": "tsc --noEmit",
15
- "release": "npm publish --access public",
15
+ "release": "npm publish --access public",
16
16
  "release:major": "npm run build && npm version major && npm run release",
17
17
  "release:minor": "npm run build && npm version minor && npm run release",
18
18
  "release:patch": "npm run build && npm version patch && npm run release",
package/dist/types.d.ts DELETED
@@ -1,46 +0,0 @@
1
- import type { IWebSocketTransport } from './transport.interface';
2
- export type WsIdentifiedPayload = {
3
- connectionId: string;
4
- };
5
- export type CampaignSavingPayload = {
6
- status: 'success' | 'start' | 'failed';
7
- error?: Record<string, unknown>;
8
- };
9
- export type UserUpdatedPayload = {
10
- userId: string;
11
- [key: string]: unknown;
12
- };
13
- export type NotificationReceivedPayload = {
14
- id: string;
15
- message: string;
16
- [key: string]: unknown;
17
- };
18
- export interface WsEventMap {
19
- 'ws:connected': undefined;
20
- 'ws:disconnected': CloseEvent;
21
- 'ws:error': Event;
22
- 'ws:max-retries-exceeded': undefined;
23
- 'ws:unparseable': string;
24
- /** Emitted when the server sends a message with type "ws:identified". */
25
- 'ws:identified': WsIdentifiedPayload;
26
- 'campaign:saving': CampaignSavingPayload;
27
- 'user:updated': UserUpdatedPayload;
28
- 'notification:received': NotificationReceivedPayload;
29
- }
30
- export interface WebSocketManagerConfig {
31
- /** WebSocket endpoint URL (e.g. "wss://api.example.com/ws"). */
32
- url: string;
33
- /** Transport implementation. Use RealWebSocketTransport in production,
34
- * MockWebSocketTransport in tests. */
35
- transport: IWebSocketTransport;
36
- /** Maximum number of reconnect attempts before giving up. Default: 10. */
37
- maxReconnectAttempts?: number;
38
- /** Base delay (ms) for exponential backoff. Default: 500. */
39
- baseReconnectDelayMs?: number;
40
- /** Upper cap (ms) for reconnect delay. Default: 30 000. */
41
- maxReconnectDelayMs?: number;
42
- /** Interval (ms) between heartbeat messages while connected. Default: 30 000. */
43
- heartbeatIntervalMs?: number;
44
- /** Payload sent as the heartbeat message. Default: `{ "type": "ping" }`. */
45
- heartbeatMessage?: Record<string, unknown>;
46
- }
package/dist/types.js DELETED
@@ -1 +0,0 @@
1
- export {};