@zap-socket/server 0.0.11 → 0.0.12

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/events.d.ts CHANGED
@@ -1,18 +1,20 @@
1
1
  import { z } from "zod";
2
- import type { EventInput, ZapEvent, ZapStream, ZapServerEvent, MiddlewareType, ZapServerType, MiddlwareContext } from "@zap-socket/types";
2
+ import type { EventInput, ZapEvent, ZapStream, ZapServerEvent, MiddlewareType, MiddlwareContext } from "@zap-socket/types";
3
3
  import { ZapServer } from "./server";
4
4
  export type Context = {
5
- server: ZapServer<any> & ZapServerType<any>;
5
+ server: ZapServer<any>;
6
6
  id: string;
7
7
  buffer: MiddlwareContext;
8
8
  };
9
- export declare const zapEvent: <T extends EventInput, R>(eventObj: T extends z.ZodTypeAny ? {
9
+ export declare const zapEvent: <T extends EventInput, R, E>(eventObj: T extends z.ZodTypeAny ? {
10
10
  input: T;
11
11
  middleware?: MiddlewareType[];
12
12
  process: (input: z.infer<T>, ctx: Context) => R;
13
+ emitType?: E;
13
14
  } : {
14
15
  middleware?: MiddlewareType[];
15
16
  process: (ctx: Context) => R;
17
+ emitType?: E;
16
18
  }) => ZapEvent<T, R>;
17
19
  export declare const zapStream: <T extends EventInput, R>(eventObj: T extends z.ZodTypeAny ? {
18
20
  input: T;
package/dist/events.js CHANGED
@@ -1,10 +1,16 @@
1
1
  import { z } from "zod";
2
2
  export const zapEvent = (eventObj) => {
3
3
  if ("input" in eventObj) {
4
- return eventObj;
4
+ // Explicitly construct the return object to match ZapEvent structure
5
+ return {
6
+ input: eventObj.input,
7
+ middleware: eventObj.middleware,
8
+ process: eventObj.process
9
+ };
5
10
  }
6
11
  return {
7
12
  input: z.void(),
13
+ middleware: eventObj.middleware,
8
14
  process: eventObj.process
9
15
  };
10
16
  };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  export * from "./events";
2
2
  export * from "./server";
3
- export * from "./utils";
package/dist/index.js CHANGED
@@ -1,3 +1,2 @@
1
1
  export * from "./events";
2
2
  export * from "./server";
3
- export * from "./utils";
package/dist/server.d.ts CHANGED
@@ -25,7 +25,7 @@ export declare class ZapServer<T extends EventMap> {
25
25
  send: (clientId: string, data?: (T[K] extends ZapServerEvent<any> ? T[K]["data"] : T[K] extends ZapEvent<any, any> ? ReturnType<T[K]["process"]> : never)) => void;
26
26
  broadcast: (data?: (T[K] extends ZapServerEvent<any> ? T[K]["data"] : T[K] extends ZapEvent<any, any> ? ReturnType<T[K]["process"]> : never)) => void;
27
27
  }; };
28
- get clients(): Set<string>;
28
+ get clients(): string[];
29
29
  get socketMap(): Map<string, WebSocket>;
30
30
  }
31
31
  export declare const createZapServer: <T extends EventMap>({ port, events }: ZapServerConstructorT, callback?: () => void) => ZapServer<T>;
package/dist/server.js CHANGED
@@ -203,7 +203,7 @@ export class ZapServer {
203
203
  }));
204
204
  }
205
205
  get clients() {
206
- return new Set(this.idToWs.keys());
206
+ return this.idToWs.keys().toArray();
207
207
  }
208
208
  get socketMap() {
209
209
  return this.idToWs;
@@ -0,0 +1,52 @@
1
+ import { z } from 'zod';
2
+ export type ZapServerType<T extends EventMap> = {
3
+ sendMessageRaw: (clientId: string, data: any) => void;
4
+ event: {
5
+ [K in keyof T as T[K] extends (ZapServerEvent<any> | ZapEvent<any, any>) ? K : never]: {
6
+ send: (clientId: string, data?: (T[K] extends ZapServerEvent<any> ? T[K]["data"] : T[K] extends ZapEvent<any, any> ? ReturnType<T[K]["process"]> : never)) => void;
7
+ };
8
+ };
9
+ };
10
+ export type Context = {
11
+ server: any;
12
+ id: string;
13
+ buffer: MiddlwareContext;
14
+ };
15
+ export type MiddlwareContext = Record<string, any>;
16
+ export type MiddlewareMetadata = {
17
+ id: string;
18
+ ip: string;
19
+ timestamp: number;
20
+ size: number;
21
+ };
22
+ export type MiddlwareMsg = {
23
+ event: string;
24
+ data: any;
25
+ metadata: MiddlewareMetadata;
26
+ };
27
+ export type MiddlewareType = (ctx: MiddlwareContext, msg: MiddlwareMsg) => boolean;
28
+ export type EventInput = z.ZodTypeAny | undefined;
29
+ export type ZapEvent<T extends EventInput, R = any, E = unknown> = T extends z.ZodTypeAny ? {
30
+ input: T;
31
+ middleware?: MiddlewareType[];
32
+ process: (input: z.infer<T>, ctx: Context) => R;
33
+ emitType?: E;
34
+ } : {
35
+ input: z.ZodVoid;
36
+ middleware?: MiddlewareType[];
37
+ process: (ctx: Context) => R;
38
+ emitType?: E;
39
+ };
40
+ export type ZapStream<T extends EventInput, R> = T extends z.ZodTypeAny ? {
41
+ input: T;
42
+ middleware?: MiddlewareType[];
43
+ process: (input: z.infer<T>, ctx: Context) => AsyncGenerator<R, void, unknown>;
44
+ } : {
45
+ input: z.ZodVoid;
46
+ middleware?: MiddlewareType[];
47
+ process: (ctx: Context) => AsyncGenerator<R, void, unknown>;
48
+ };
49
+ export type ZapServerEvent<T extends z.ZodTypeAny> = {
50
+ data: z.infer<T>;
51
+ };
52
+ export type EventMap = Record<string, ZapEvent<any, any> | ZapServerEvent<any> | ZapStream<any, any>>;
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zap-socket/server",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "description": "A fully typesafe tRPC-inspired WebSocket library with Zod validation, req-res model, and native subscriptions.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -23,7 +23,7 @@
23
23
  "vitest": "^3.0.9"
24
24
  },
25
25
  "dependencies": {
26
- "@zap-socket/types": "^0.0.4",
26
+ "@zap-socket/types": "^0.0.6",
27
27
  "zod": "^3.24.2"
28
28
  }
29
29
  }