Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: client.getRequest() #130

Merged
merged 4 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`;
Expand Down
2 changes: 1 addition & 1 deletion example/example-documentation.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 6 additions & 0 deletions src/action.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -90,6 +91,7 @@ describe("Action", () => {
broadcast: broadcastMock,
getRooms: getRoomsMock,
isConnected: isConnectedMock,
getRequest: getRequestMock,
getData: getDataMock,
setData: setDataMock,
join: joinMock,
Expand All @@ -112,6 +114,7 @@ describe("Action", () => {
emit: emitMock,
broadcast: broadcastMock,
isConnected: isConnectedMock,
getRequest: getRequestMock,
getRooms: getRoomsMock,
getData: getDataMock,
setData: setDataMock,
Expand Down Expand Up @@ -140,6 +143,7 @@ describe("Action", () => {
broadcast: broadcastMock,
getRooms: getRoomsMock,
isConnected: isConnectedMock,
getRequest: getRequestMock,
getData: getDataMock,
setData: setDataMock,
join: joinMock,
Expand All @@ -153,6 +157,7 @@ describe("Action", () => {
handshake: { auth: {} },
getRooms: getRoomsMock,
isConnected: isConnectedMock,
getRequest: getRequestMock,
emit: emitMock,
broadcast: broadcastMock,
getData: getDataMock,
Expand Down Expand Up @@ -188,6 +193,7 @@ describe("Action", () => {
handshake: { auth: {} } as Socket["handshake"],
getRooms: getRoomsMock,
isConnected: isConnectedMock,
getRequest: getRequestMock,
emit: emitMock,
broadcast: broadcastMock,
getData: getDataMock,
Expand Down
14 changes: 14 additions & 0 deletions src/attach.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ describe("Attach", () => {
onAnyOutgoing: vi.fn(),
join: vi.fn(),
leave: vi.fn(),
handshake: {},
request: {},
};
const adapterMock = {
rooms: new Map([
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/attach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const attachSockets = async <NS extends Namespaces>({
broadcast,
id: socket.id,
handshake: socket.handshake,
getRequest: <T extends http.IncomingMessage>() => socket.request as T,
isConnected: () => socket.connected,
getRooms: () => Array.from(socket.rooms),
getData: () => socket.data || {},
Expand Down
6 changes: 6 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { IncomingMessage } from "node:http";
import type { Socket } from "socket.io";
import { z } from "zod";
import { Distribution } from "./distribution";
Expand All @@ -10,6 +11,11 @@ export interface Client<E extends EmissionMap, D extends z.SomeZodObject>
/** @alias Socket.id */
id: Socket["id"];
handshake: Socket["handshake"];
/**
* @desc When using express-session:
* @example getRequest<express.Request>().session
**/
getRequest: <T extends IncomingMessage = Socket["request"]>() => T;
/** @desc Returns the list of the rooms the client in */
getRooms: () => string[];
/**
Expand Down
2 changes: 2 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { IncomingMessage } from "node:http";
import { expectNotType, expectType } from "tsd";
import { z } from "zod";
import {
Expand Down Expand Up @@ -42,6 +43,7 @@ describe("Entrypoint", () => {
>({
client: {
isConnected: () => true,
getRequest: <T>() => ({}) as IncomingMessage as T,
id: "",
handshake: {
headers: {},
Expand Down
Loading