Skip to content

Commit

Permalink
merge: release v0.1.1 (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
attilaorosz authored Jan 27, 2023
2 parents 7ffb712 + 8eeedc2 commit 795ef99
Show file tree
Hide file tree
Showing 15 changed files with 1,305 additions and 923 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/auto-approve-dependabot-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Dependabot auto-merge
on:
pull_request_target
jobs:
dependabot:
runs-on: ubuntu-latest
if: github.actor == 'dependabot[bot]'
steps:
- name: 'Auto approve PR by Dependabot'
uses: hmarr/auto-approve-action@v2.0.0
with:
github-token: "${{ secrets.TYPESTACK_BOT_TOKEN }}"
- name: 'Comment merge command'
uses: actions/github-script@v3
with:
github-token: ${{secrets.TYPESTACK_BOT_TOKEN }}
script: |
await github.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '@dependabot squash and merge'
})
23 changes: 0 additions & 23 deletions .github/workflows/auto-merge-dependabot-workflow.yml

This file was deleted.

24 changes: 21 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

_This changelog follows the [keep a changelog][keep-a-changelog]_ format to maintain a human readable changelog.

## [0.1.1](https://github.com/typestack/socket-controllers/compare/v0.1.0...v0.1.1) (2023-01-27)

### Added

- Added `@OnDisconnecting()` decorator
- Added error type filter option to `@EmitOnFail()` decorator

Example: `@EmitOnFail('message', {errorType: TypeError})`

- Added `index` option to `@MessageBody()` decorator to be able to get multiple event arguments

Note: If you don't specify the index it will return the first

- Added support to use the same namespace for multiple controllers

Note: The namespaces must match exactly, providing a differnet pattern will not work due to a socket.io limitation

### Changed

- `glob` package updated from `8.0.3` to `8.1.0`

## [0.1.0](https://github.com/typestack/socket-controllers/compare/v0.0.5...v0.1.0) (2023-01-18)

Expand Down Expand Up @@ -107,7 +127,7 @@ _This changelog follows the [keep a changelog][keep-a-changelog]_ format to main

### Added
- Namespace scope support for middlewares
- `transport: boolean` option to decorators that support class-transformer
- `transform: boolean` option to decorators that support class-transformer

### Changed

Expand All @@ -117,8 +137,6 @@ _This changelog follows the [keep a changelog][keep-a-changelog]_ format to main
- `socket.io` package updated from `2.0.1` to `4.5.4`
- updated various dev dependencies

### Breaking changes


### [0.0.5][v0.0.5] - 2020-02-04

Expand Down
69 changes: 47 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,14 @@ Use class-based controllers to handle websocket events. Helps to organize your c

## More usage examples

#### Run code on socket client connect / disconnect
#### Run code on socket client connect / disconnect / disconnecting

Controller action marked with `@OnConnect()` decorator is called once new client connected.
Controller action marked with `@OnDisconnect()` decorator is called once client disconnected.
Controller action marked with `@OnDisconnecting()` decorator is called when the client is disconnecting, before the disconnect event.

```typescript
import { SocketController, OnConnect, OnDisconnect } from 'socket-controllers';
import { SocketController, OnConnect, OnDisconnect, OnDisconnecting } from 'socket-controllers';

@SocketController()
export class MessageController {
Expand All @@ -109,6 +110,11 @@ export class MessageController {
save() {
console.log('client disconnected');
}

@OnDisconnecting()
save() {
console.log('client is disconnecting');
}
}
```

Expand Down Expand Up @@ -146,7 +152,22 @@ export class MessageController {

If you specify a class type to parameter that is decorated with `@MessageBody()`,
socket-controllers will use [class-transformer][1] to create instance of the given class type with the data received in the message.
To disable this behaviour you need to specify a `{ transformOption: { transform: false ] }` in SocketControllerOptions when creating a server.
To disable this behaviour you need to specify `{ transformOption: { transform: false ] }` in SocketControllerOptions when creating a server.

You can define an index to get multiple parameters from the socket event.

```typescript
import { SocketController, OnMessage, MessageBody } from 'socket-controllers';

@SocketController()
export class MessageController {
@OnMessage('save')
save(@MessageBody({index: 0}) param1: any, @MessageBody({index: 1}) param2: any) {
console.log('received message: ', message1);
console.log('received message: ', message2);
}
}
```

#### `@SocketQueryParam()` decorator

Expand Down Expand Up @@ -237,6 +258,8 @@ import { SocketController, OnMessage, EmitOnSuccess, EmitOnFail } from 'socket-c
export class MessageController {
@OnMessage('save')
@EmitOnSuccess('save_successfully')
@EmitOnFail('save_error_range', {errorType: RangeError})
@EmitOnFail('save_error_type', {errorType: TypeError})
@EmitOnFail('save_error')
save() {
if (1 === 1) {
Expand All @@ -251,6 +274,7 @@ export class MessageController {
```

In this case `save_error` message will be sent to the client with `One is equal to one! Fatal error!` error message.
The order is important when defining multiple `@EmitOnFail()` decorators, the first matching errorType will be served

Sometimes you may want to not emit success/error message if returned result is null or undefined.
In such cases you can use `@SkipEmitOnEmptyResult()` decorator.
Expand Down Expand Up @@ -440,25 +464,26 @@ export class MessageController {
## Decorators Reference

| Signature | Description |
| ---------------------------------------------- | -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `@SocketController(namespace?: string\|Regex)` | Registers a class to be a socket controller that can listen to websocket events and respond to them. |
| `@OnMessage(messageName: string)` | Registers controller's action to be executed when socket receives message with given name. |
| `@OnConnect()` | Registers controller's action to be executed when client connects to the socket. |
| `@OnDisconnect()` | Registers controller's action to be executed when client disconnects from the socket. |
| `@ConnectedSocket()` | Injects connected client's socket object to the controller action. |
| `@SocketIO()` | Injects socket.io object that initialized a connection. |
| `@MessageBody()` | Injects received message body. |
| `@SocketQueryParam(paramName: string)` | Injects query parameter from the received socket request. |
| `@SocketId()` | Injects socket id from the received request. |
| `@SocketRequest()` | Injects request object received by socket. |
| `@SocketRooms()` | Injects rooms of the connected socket client. |
| `@NspParams()` | Injects dynamic namespace params. |
| `@NspParam(paramName: string)` | Injects param from the dynamic namespace. |
| `@Middleware()` | Registers a new middleware to be registered in the socket.io. |
| `@EmitOnSuccess(messageName: string)` | If this decorator is set then after controller action will emit message with the given name after action execution. It will emit message only if controller succeed without errors. If result is a Promise then it will wait until promise is resolved and emit a message. |
| `@EmitOnFail(messageName: string)` | If this decorator is set then after controller action will emit message with the given name after action execution. It will emit message only if controller throw an exception. If result is a Promise then it will wait until promise throw an error and emit a message. |
| `@SkipEmitOnEmptyResult()` | Used in conjunction with @EmitOnSuccess and @EmitOnFail decorators. If result returned by controller action is null or undefined then messages will not be emitted by @EmitOnSuccess or @EmitOnFail decorators. | |
| Signature | Description |
|----------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `@SocketController(namespace?: string\ | Regex)` | Registers a class to be a socket controller that can listen to websocket events and respond to them. |
| `@OnMessage(messageName: string)` | Registers controller's action to be executed when socket receives message with given name. |
| `@OnConnect()` | Registers controller's action to be executed when client connects to the socket. |
| `@OnDisconnect()` | Registers controller's action to be executed when client disconnects from the socket. |
| `@OnDisconnecting()` | Registers controller's action to be executed when client is disconnecting from the socket. |
| `@ConnectedSocket()` | Injects connected client's socket object to the controller action. |
| `@SocketIO()` | Injects socket.io object that initialized a connection. |
| `@MessageBody()` | Injects received message body. |
| `@SocketQueryParam(paramName: string)` | Injects query parameter from the received socket request. |
| `@SocketId()` | Injects socket id from the received request. |
| `@SocketRequest()` | Injects request object received by socket. |
| `@SocketRooms()` | Injects rooms of the connected socket client. |
| `@NspParams()` | Injects dynamic namespace params. |
| `@NspParam(paramName: string)` | Injects param from the dynamic namespace. |
| `@Middleware()` | Registers a new middleware to be registered in the socket.io. |
| `@EmitOnSuccess(messageName: string)` | If this decorator is set then after controller action will emit message with the given name after action execution. It will emit message only if controller succeed without errors. If result is a Promise then it will wait until promise is resolved and emit a message. |
| `@EmitOnFail(messageName: string)` | If this decorator is set then after controller action will emit message with the given name after action execution. It will emit message only if controller throw an exception. If result is a Promise then it will wait until promise throw an error and emit a message. |
| `@SkipEmitOnEmptyResult()` | Used in conjunction with @EmitOnSuccess and @EmitOnFail decorators. If result returned by controller action is null or undefined then messages will not be emitted by @EmitOnSuccess or @EmitOnFail decorators. | |

## Samples

Expand Down
Loading

0 comments on commit 795ef99

Please sign in to comment.