diff --git a/CHANGELOG.md b/CHANGELOG.md index 4850c1e7..33cc12d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Version 0 +### v0.19.0 + +- Added `client.getRequest()` method (proxy for `Socket::request`). + ### v0.18.0 - Fixed possibly invalid values of `type` property when depicting `z.literal()`, `z.enum()` and `z.nativeEnum()`; diff --git a/example/example-documentation.yaml b/example/example-documentation.yaml index 6b1165c4..4cb4ffa0 100644 --- a/example/example-documentation.yaml +++ b/example/example-documentation.yaml @@ -1,7 +1,7 @@ asyncapi: 3.0.0 info: title: Example APP - version: 0.18.0 + version: 0.19.0-beta2 contact: name: Anna Bocharova url: https://robintail.cz diff --git a/package.json b/package.json index 646d3e3b..8fc7c30c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zod-sockets", - "version": "0.18.0", + "version": "0.19.0-beta2", "description": "Socket.IO solution with I/O validation and the ability to generate AsyncAPI specification and a contract for consumers", "type": "module", "main": "dist/index.cjs", diff --git a/src/action.spec.ts b/src/action.spec.ts index aced7acb..226f0c97 100644 --- a/src/action.spec.ts +++ b/src/action.spec.ts @@ -62,6 +62,7 @@ describe("Action", () => { const emitMock = vi.fn(); const broadcastMock = vi.fn(); const isConnectedMock = vi.fn(); + const getRequestMock = vi.fn(); const withRoomsMock = vi.fn(); const getRoomsMock = vi.fn(); const getAllRoomsMock = vi.fn(); @@ -90,6 +91,7 @@ describe("Action", () => { broadcast: broadcastMock, getRooms: getRoomsMock, isConnected: isConnectedMock, + getRequest: getRequestMock, getData: getDataMock, setData: setDataMock, join: joinMock, @@ -112,6 +114,7 @@ describe("Action", () => { emit: emitMock, broadcast: broadcastMock, isConnected: isConnectedMock, + getRequest: getRequestMock, getRooms: getRoomsMock, getData: getDataMock, setData: setDataMock, @@ -140,6 +143,7 @@ describe("Action", () => { broadcast: broadcastMock, getRooms: getRoomsMock, isConnected: isConnectedMock, + getRequest: getRequestMock, getData: getDataMock, setData: setDataMock, join: joinMock, @@ -153,6 +157,7 @@ describe("Action", () => { handshake: { auth: {} }, getRooms: getRoomsMock, isConnected: isConnectedMock, + getRequest: getRequestMock, emit: emitMock, broadcast: broadcastMock, getData: getDataMock, @@ -188,6 +193,7 @@ describe("Action", () => { handshake: { auth: {} } as Socket["handshake"], getRooms: getRoomsMock, isConnected: isConnectedMock, + getRequest: getRequestMock, emit: emitMock, broadcast: broadcastMock, getData: getDataMock, diff --git a/src/attach.spec.ts b/src/attach.spec.ts index ead72847..8f9c05f4 100644 --- a/src/attach.spec.ts +++ b/src/attach.spec.ts @@ -17,6 +17,8 @@ describe("Attach", () => { onAnyOutgoing: vi.fn(), join: vi.fn(), leave: vi.fn(), + handshake: {}, + request: {}, }; const adapterMock = { rooms: new Map([ @@ -119,6 +121,8 @@ describe("Attach", () => { client: { id: "ID", isConnected: expect.any(Function), + getRequest: expect.any(Function), + handshake: {}, getRooms: expect.any(Function), emit: expect.any(Function), broadcast: expect.any(Function), @@ -189,6 +193,16 @@ describe("Attach", () => { name: "user", }); + // client.handshake + expect(actionsMock[0].execute.mock.lastCall[0].client.handshake).toEqual( + socketMock.handshake, + ); + + // client.getRequest + expect( + actionsMock[0].execute.mock.lastCall[0].client.getRequest(), + ).toEqual(socketMock.request); + // join/leave: for (const rooms of ["room1", ["room2", "room3"]]) { actionsMock[0].execute.mock.lastCall[0].client.join(rooms); diff --git a/src/attach.ts b/src/attach.ts index 19880866..e0c8d3c0 100644 --- a/src/attach.ts +++ b/src/attach.ts @@ -70,6 +70,7 @@ export const attachSockets = async ({ broadcast, id: socket.id, handshake: socket.handshake, + getRequest: () => socket.request as T, isConnected: () => socket.connected, getRooms: () => Array.from(socket.rooms), getData: () => socket.data || {}, diff --git a/src/client.ts b/src/client.ts index 4a22fd64..854c0544 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,3 +1,4 @@ +import type { IncomingMessage } from "node:http"; import type { Socket } from "socket.io"; import { z } from "zod"; import { Distribution } from "./distribution"; @@ -10,6 +11,11 @@ export interface Client /** @alias Socket.id */ id: Socket["id"]; handshake: Socket["handshake"]; + /** + * @desc When using express-session: + * @example getRequest().session + **/ + getRequest: () => T; /** @desc Returns the list of the rooms the client in */ getRooms: () => string[]; /** diff --git a/src/index.spec.ts b/src/index.spec.ts index c39ce2e7..d0c2a8b7 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -1,3 +1,4 @@ +import type { IncomingMessage } from "node:http"; import { expectNotType, expectType } from "tsd"; import { z } from "zod"; import { @@ -42,6 +43,7 @@ describe("Entrypoint", () => { >({ client: { isConnected: () => true, + getRequest: () => ({}) as IncomingMessage as T, id: "", handshake: { headers: {},