Skip to content

Commit b3aa8f0

Browse files
authored
merge: release v0.2.0 (#639)
2 parents 80474e7 + 993e312 commit b3aa8f0

18 files changed

+1807
-1121
lines changed

Diff for: CHANGELOG.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,40 @@
22

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

5+
## [0.2.0](https://github.com/typestack/socket-controllers/compare/v0.1.2...v0.2.0) (2023-04-10)
6+
7+
### Breaking Changes
8+
9+
- Replaced `ScopedContainerGetterParams` with `SocketEventContext`
10+
11+
BEFORE:
12+
13+
```ts
14+
scopedContainerGetter: (args: ScopedContainerGetterParams) => {
15+
// ...
16+
}
17+
```
18+
19+
AFTER:
20+
21+
```ts
22+
scopedContainerGetter: (args: SocketEventContext) => {
23+
// ...
24+
}
25+
```
26+
Note: The new interface contains all properties of the previous
27+
28+
29+
### Added
30+
31+
- Added scoped container dispose support
32+
- Added interceptor support
33+
- Added ack support
34+
35+
### Changed
36+
37+
- `glob` package updated from `8.1.0` to `10.0.0`
38+
539
## [0.1.2](https://github.com/typestack/socket-controllers/compare/v0.1.1...v0.1.2) (2023-01-30)
640

741
### Added
@@ -19,7 +53,6 @@ _This changelog follows the [keep a changelog][keep-a-changelog]_ format to main
1953
});
2054
```
2155

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

2558
### Added

Diff for: README.md

+78-4
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,25 @@ export class MessageController {
169169
}
170170
```
171171

172+
#### `@MessageAck()` decorator
173+
174+
To get received message ack use `@MessageAck()` decorator:
175+
176+
```typescript
177+
import { SocketController, OnMessage, MessageAck, MessageBody } from 'socket-controllers';
178+
179+
@SocketController()
180+
export class MessageController {
181+
@OnMessage('save')
182+
save(@MessageBody() message: any, @MessageAck() ack: Function) {
183+
console.log('received message: ', message);
184+
ack('callback message');
185+
}
186+
}
187+
```
188+
189+
> note: ack must be the last parameter in `emit`, otherwise it will be `null`
190+
172191
#### `@SocketQueryParam()` decorator
173192

174193
To get received query parameter use `@SocketQueryParam()` decorator.
@@ -468,29 +487,84 @@ You can enable scoped controllers by providing a `scopedContainerGetter` functio
468487

469488
You will get a new instance for each event in the controller.
470489

471-
The `scopedContainerGetter` function receives a parameter which contains the socket, socket.io instance, event type, event name, namespace parameters and the message arguments if they are applicable.
490+
The `scopedContainerGetter` function receives the `SocketEventContext`.
491+
492+
The `scopedContainerDisposer` function receives the container instance you created with `scopedContainerGetter` after the socket action is finished. Use this function to dispose the container if needed.
472493

473494
```typescript
474495
import 'reflect-metadata';
475-
import { SocketControllers, ScopedContainerGetterParams } from 'socket-controllers';
476-
import { Container, Token } from "typedi";
496+
import { SocketControllers, SocketEventContext } from 'socket-controllers';
497+
import { Container, ContainerInstance, Token } from "typedi";
477498

478499
const myDiToken = new Token();
479500

480501
// create and run socket server
481502
const server = new SocketControllers({
482503
port: 3000,
483504
container: Container,
484-
scopedContainerGetter: (args: ScopedContainerGetterParams) => {
505+
scopedContainerGetter: (args: SocketEventContext) => {
485506
const container = Container.of(YOUR_REQUEST_CONTEXT);
486507
container.set(myDiToken, 'MY_VALUE');
487508
return container;
488509
},
510+
scopedContainerDisposer: (container: ContainerInstance) => {
511+
container.dispose();
512+
},
489513
controllers: [__dirname + '/controllers/*.js'],
490514
middlewares: [__dirname + '/middlewares/*.js'],
491515
});
492516
```
493517

518+
## Interceptors
519+
520+
Interceptors allow you to wrap your event handlers in higher order functions.
521+
With interceptors you can add logging or modify the incoming or outgoing data for event handlers.
522+
523+
```typescript
524+
import {
525+
SocketController,
526+
OnMessage,
527+
EmitOnSuccess,
528+
EmitOnFail,
529+
SkipEmitOnEmptyResult,
530+
UseInterceptor,
531+
MessageBody
532+
} from 'socket-controllers';
533+
534+
const interceptor: InterceptorInterface = {
535+
use: (ctx: SocketEventContext, next: () => any) => {
536+
ctx.messageArgs[0] = 'modified message from controller - ' + ctx.messageArgs[0];
537+
const resp = next();
538+
return 'modified response from controller - ' + resp; // modified response from controller - modified response from method - reponse
539+
},
540+
};
541+
542+
@Service()
543+
class Interceptor implements InterceptorInterface {
544+
async use(ctx: SocketEventContext, next: () => any) {
545+
ctx.messageArgs[0] = 'modified message from method - ' + ctx.messageArgs[0];
546+
const resp = await next();
547+
return 'modified response from method - ' + resp; // modified response from method - reponse
548+
}
549+
}
550+
551+
@SocketController()
552+
@UseInterceptor(interceptor)
553+
export class MessageController {
554+
@OnMessage('get')
555+
@EmitOnSuccess('get_success')
556+
@SkipEmitOnEmptyResult()
557+
@UseInterceptor(Interceptor)
558+
get(@MessageBody() message: string): Promise<Message[]> {
559+
console.log(message); // modified message from controller - modified message from method - original message
560+
return 'response';
561+
}
562+
}
563+
```
564+
565+
Interceptors are executed in order of definition, starting with the controller interceptors.
566+
567+
494568
## Decorators Reference
495569

496570
| Signature | Description |

0 commit comments

Comments
 (0)