forked from tsedio/tsed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(socketio): allow injecting the disconnection reason
closes tsedio#2372
- Loading branch information
Showing
10 changed files
with
121 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/third-parties/socketio/src/decorators/reason.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {Store} from "@tsed/core"; | ||
import {Nsp, SocketErr} from "../index"; | ||
import {Reason} from "./reason"; | ||
|
||
describe("Reason", () => { | ||
it("should set metadata", () => { | ||
class Test {} | ||
|
||
Reason(Test, "test", 0); | ||
const store = Store.from(Test); | ||
|
||
expect(store.get("socketIO")).toEqual({ | ||
handlers: { | ||
test: { | ||
parameters: { | ||
"0": { | ||
filter: "reason", | ||
mapIndex: undefined | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {SocketFilter} from "./socketFilter"; | ||
import {SocketFilters} from "../interfaces/SocketFilters"; | ||
|
||
/** | ||
* Inject the disconnection reason into the decorated parameter. | ||
* | ||
* This decorator is used in conjunction with the `$onDisconnect` event handler to handle disconnection reasons in SocketIO services. | ||
* It allows you to access the reason for the disconnection in your method implementation. For details please refer to the [Socket.io documentation](https://socket.io/docs/v4/server-api/#event-disconnect). | ||
* | ||
* @example | ||
* ```typescript | ||
* @SocketService("/nsp") | ||
* export class MyWS { | ||
* public async $onDisconnect( | ||
* @Reason reason: string = '' | ||
* ) { | ||
* // your implementation | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* @experimental This decorator is experimental and may change or be removed in future versions. | ||
* @param target | ||
* @param {string} propertyKey | ||
* @param {number} index | ||
* @decorator | ||
*/ | ||
export function Reason(target: unknown, propertyKey: string, index: number) { | ||
return SocketFilter(SocketFilters.REASON)(target, propertyKey, index); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters