Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
d598d7d
build(deps-dev): bump typescript from 4.9.4 to 4.9.5 (#552)
dependabot[bot] Jan 31, 2023
270444d
build(deps-dev): bump @typescript-eslint/eslint-plugin (#554)
dependabot[bot] Feb 1, 2023
8cb9ec9
build(deps-dev): bump @typescript-eslint/parser from 5.49.0 to 5.50.0…
dependabot[bot] Feb 1, 2023
837f131
feat: add option to dispose scoped container (#556)
attilaorosz Feb 1, 2023
2436e4f
docs: document scopedContainerDisposer (#557)
attilaorosz Feb 1, 2023
e2062e1
build(deps-dev): bump @types/node from 18.11.18 to 18.11.19 (#558)
dependabot[bot] Feb 6, 2023
6b98135
build(deps-dev): bump @typescript-eslint/eslint-plugin (#560)
dependabot[bot] Feb 7, 2023
8875aa3
build(deps-dev): bump @types/node from 18.11.19 to 18.13.0 (#562)
dependabot[bot] Feb 7, 2023
b07a072
build(deps-dev): bump lint-staged from 13.1.0 to 13.1.1 (#561)
dependabot[bot] Feb 7, 2023
10b44ef
build(deps-dev): bump @typescript-eslint/parser from 5.50.0 to 5.51.0…
dependabot[bot] Feb 7, 2023
6d4b332
build(deps-dev): bump prettier from 2.8.3 to 2.8.4 (#565)
dependabot[bot] Feb 8, 2023
fc0a57a
build(deps-dev): bump jest from 29.4.1 to 29.4.2 (#564)
dependabot[bot] Feb 11, 2023
d7b44ba
build(deps-dev): bump eslint from 8.33.0 to 8.34.0 (#566)
dependabot[bot] Feb 13, 2023
897f287
build(deps-dev): bump @typescript-eslint/parser from 5.51.0 to 5.52.0…
dependabot[bot] Feb 14, 2023
c09e3b7
build(deps-dev): bump lint-staged from 13.1.1 to 13.1.2 (#568)
dependabot[bot] Feb 14, 2023
8ff4885
build(deps-dev): bump @typescript-eslint/eslint-plugin (#569)
dependabot[bot] Feb 14, 2023
c231d24
build(deps-dev): bump jest from 29.4.2 to 29.4.3 (#570)
dependabot[bot] Feb 16, 2023
0c1cd14
build(deps-dev): bump @types/node from 18.13.0 to 18.14.0 (#571)
dependabot[bot] Feb 20, 2023
5b09dd0
build(deps-dev): bump @typescript-eslint/parser from 5.52.0 to 5.53.0…
dependabot[bot] Feb 21, 2023
867ed10
build(deps-dev): bump @typescript-eslint/eslint-plugin (#573)
dependabot[bot] Feb 21, 2023
dcf3171
build(deps-dev): bump @types/glob from 8.0.1 to 8.1.0 (#575)
dependabot[bot] Feb 23, 2023
50e2eb5
build(deps-dev): bump @types/node from 18.14.0 to 18.14.1 (#576)
dependabot[bot] Feb 24, 2023
2ff50cf
build(deps-dev): bump @types/node from 18.14.1 to 18.14.2 (#577)
dependabot[bot] Feb 27, 2023
c57e64d
build(deps-dev): bump eslint from 8.34.0 to 8.35.0 (#578)
dependabot[bot] Feb 27, 2023
aa9e170
build(deps-dev): bump @typescript-eslint/parser from 5.53.0 to 5.54.0…
dependabot[bot] Feb 28, 2023
2a8f801
build(deps-dev): bump @typescript-eslint/eslint-plugin (#581)
dependabot[bot] Feb 28, 2023
beab68e
build(deps-dev): bump @types/node from 18.14.2 to 18.14.4 (#583)
dependabot[bot] Mar 2, 2023
128dc68
build(deps-dev): bump rimraf from 4.1.2 to 4.1.3 (#584)
dependabot[bot] Mar 2, 2023
dbe351f
build(deps-dev): bump @types/node from 18.14.4 to 18.14.5 (#586)
dependabot[bot] Mar 3, 2023
0f02232
build(deps-dev): bump rimraf from 4.1.3 to 4.2.0 (#588)
dependabot[bot] Mar 3, 2023
7711910
build(deps-dev): bump eslint-config-prettier from 8.6.0 to 8.7.0 (#589)
dependabot[bot] Mar 6, 2023
87b74ab
build(deps-dev): bump rimraf from 4.2.0 to 4.3.0 (#590)
dependabot[bot] Mar 6, 2023
e39e509
build(deps-dev): bump @types/node from 18.14.5 to 18.14.6 (#591)
dependabot[bot] Mar 6, 2023
d6f381b
build(deps-dev): bump rimraf from 4.3.0 to 4.3.1 (#592)
dependabot[bot] Mar 7, 2023
baf67df
build(deps-dev): bump @typescript-eslint/eslint-plugin (#594)
dependabot[bot] Mar 7, 2023
c5ddea2
build(deps-dev): bump @typescript-eslint/parser from 5.54.0 to 5.54.1…
dependabot[bot] Mar 7, 2023
8d8f8d4
build(deps-dev): bump jest from 29.4.3 to 29.5.0 (#593)
dependabot[bot] Mar 7, 2023
ec3aef3
build(deps-dev): bump rimraf from 4.3.1 to 4.4.0 (#596)
dependabot[bot] Mar 9, 2023
5dd5c6f
build(deps-dev): bump @types/node from 18.14.6 to 18.15.0 (#597)
dependabot[bot] Mar 10, 2023
712e578
build(deps-dev): bump eslint from 8.35.0 to 8.36.0 (#598)
dependabot[bot] Mar 13, 2023
978744e
build(deps-dev): bump @types/node from 18.15.0 to 18.15.1 (#599)
dependabot[bot] Mar 13, 2023
278fd82
build(deps-dev): bump lint-staged from 13.1.2 to 13.2.0 (#600)
dependabot[bot] Mar 13, 2023
ef7f85b
build(deps-dev): bump @types/jest from 29.4.0 to 29.4.1 (#602)
dependabot[bot] Mar 14, 2023
46fca39
build(deps-dev): bump @types/node from 18.15.1 to 18.15.3 (#601)
dependabot[bot] Mar 14, 2023
e16673a
build(deps-dev): bump @typescript-eslint/parser from 5.54.1 to 5.55.0…
dependabot[bot] Mar 14, 2023
35d4877
build(deps-dev): bump @typescript-eslint/eslint-plugin (#603)
dependabot[bot] Mar 14, 2023
f587fe9
build(deps): bump glob from 8.1.0 to 9.3.0 (#605)
dependabot[bot] Mar 15, 2023
10e780c
build(deps-dev): bump @types/jest from 29.4.1 to 29.4.2 (#606)
dependabot[bot] Mar 15, 2023
f9610b2
build(deps-dev): bump @types/jest from 29.4.2 to 29.4.4 (#607)
dependabot[bot] Mar 16, 2023
42401bb
build(deps-dev): bump typescript from 4.9.5 to 5.0.2 (#608)
dependabot[bot] Mar 17, 2023
7d230b7
build(deps-dev): bump @types/jest from 29.4.4 to 29.5.0 (#609)
dependabot[bot] Mar 17, 2023
019bb4d
build(deps-dev): bump prettier from 2.8.4 to 2.8.5 (#611)
dependabot[bot] Mar 20, 2023
9cfb0c9
build(deps): bump glob from 9.3.0 to 9.3.1 (#613)
dependabot[bot] Mar 21, 2023
4e41347
build(deps-dev): bump @types/node from 18.15.3 to 18.15.5 (#614)
dependabot[bot] Mar 21, 2023
e98f8e1
build(deps-dev): bump @typescript-eslint/parser from 5.55.0 to 5.56.0…
dependabot[bot] Mar 21, 2023
50c5b56
build(deps-dev): bump eslint-config-prettier from 8.7.0 to 8.8.0 (#616)
dependabot[bot] Mar 21, 2023
c42cee3
build(deps-dev): bump @typescript-eslint/eslint-plugin (#615)
dependabot[bot] Mar 21, 2023
8a522fa
build(deps-dev): bump prettier from 2.8.5 to 2.8.6 (#617)
dependabot[bot] Mar 22, 2023
304b099
build(deps): bump glob from 9.3.1 to 9.3.2 (#618)
dependabot[bot] Mar 23, 2023
af49932
build(deps-dev): bump rimraf from 4.4.0 to 4.4.1 (#619)
dependabot[bot] Mar 23, 2023
184fd3c
build(deps-dev): bump @types/node from 18.15.5 to 18.15.7 (#621)
dependabot[bot] Mar 24, 2023
52b280a
build(deps-dev): bump prettier from 2.8.6 to 2.8.7 (#622)
dependabot[bot] Mar 24, 2023
8e32537
feat: add ack support (#620)
xiamu33 Mar 24, 2023
34f0e84
build(deps-dev): bump @types/node from 18.15.7 to 18.15.10 (#623)
dependabot[bot] Mar 27, 2023
4e4dbc4
build(deps-dev): bump @typescript-eslint/eslint-plugin (#624)
dependabot[bot] Mar 28, 2023
35f749f
build(deps-dev): bump @typescript-eslint/parser from 5.56.0 to 5.57.0…
dependabot[bot] Mar 28, 2023
7d93480
build(deps-dev): bump eslint from 8.36.0 to 8.37.0 (#626)
dependabot[bot] Mar 29, 2023
1e2711e
build(deps-dev): bump @types/node from 18.15.10 to 18.15.11 (#627)
dependabot[bot] Mar 29, 2023
f2af6ec
build(deps-dev): bump typescript from 5.0.2 to 5.0.3 (#628)
dependabot[bot] Mar 31, 2023
c3673c8
build(deps-dev): bump ts-jest from 29.0.5 to 29.1.0 (#629)
dependabot[bot] Apr 3, 2023
6953a83
build(deps): bump glob from 9.3.2 to 9.3.4 (#630)
dependabot[bot] Apr 3, 2023
7e90903
build(deps-dev): bump @typescript-eslint/parser from 5.57.0 to 5.57.1…
dependabot[bot] Apr 4, 2023
77f74f9
build(deps-dev): bump @typescript-eslint/eslint-plugin (#633)
dependabot[bot] Apr 4, 2023
363fea1
feat: interceptor support (#631)
attilaorosz Apr 4, 2023
77fab7a
build(deps-dev): bump typescript from 5.0.3 to 5.0.4 (#634)
dependabot[bot] Apr 10, 2023
0c0ad95
build(deps): bump glob from 9.3.4 to 10.0.0 (#637)
dependabot[bot] Apr 10, 2023
c9f3c23
build(deps-dev): bump eslint from 8.37.0 to 8.38.0 (#635)
dependabot[bot] Apr 10, 2023
3573398
build(deps-dev): bump rimraf from 4.4.1 to 5.0.0 (#636)
dependabot[bot] Apr 10, 2023
e92e5fd
build(deps-dev): bump lint-staged from 13.2.0 to 13.2.1 (#638)
dependabot[bot] Apr 10, 2023
7c21256
docs: add changelog for v0.2.0
attilaorosz Apr 10, 2023
993e312
build: bump version to v0.2.0
attilaorosz Apr 10, 2023
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
35 changes: 34 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,40 @@

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

## [0.2.0](https://github.com/typestack/socket-controllers/compare/v0.1.2...v0.2.0) (2023-04-10)

### Breaking Changes

- Replaced `ScopedContainerGetterParams` with `SocketEventContext`

BEFORE:

```ts
scopedContainerGetter: (args: ScopedContainerGetterParams) => {
// ...
}
```

AFTER:

```ts
scopedContainerGetter: (args: SocketEventContext) => {
// ...
}
```
Note: The new interface contains all properties of the previous


### Added

- Added scoped container dispose support
- Added interceptor support
- Added ack support

### Changed

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

## [0.1.2](https://github.com/typestack/socket-controllers/compare/v0.1.1...v0.1.2) (2023-01-30)

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


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

### Added
Expand Down
82 changes: 78 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,25 @@ export class MessageController {
}
```

#### `@MessageAck()` decorator

To get received message ack use `@MessageAck()` decorator:

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

@SocketController()
export class MessageController {
@OnMessage('save')
save(@MessageBody() message: any, @MessageAck() ack: Function) {
console.log('received message: ', message);
ack('callback message');
}
}
```

> note: ack must be the last parameter in `emit`, otherwise it will be `null`

#### `@SocketQueryParam()` decorator

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

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

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.
The `scopedContainerGetter` function receives the `SocketEventContext`.

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.

```typescript
import 'reflect-metadata';
import { SocketControllers, ScopedContainerGetterParams } from 'socket-controllers';
import { Container, Token } from "typedi";
import { SocketControllers, SocketEventContext } from 'socket-controllers';
import { Container, ContainerInstance, Token } from "typedi";

const myDiToken = new Token();

// create and run socket server
const server = new SocketControllers({
port: 3000,
container: Container,
scopedContainerGetter: (args: ScopedContainerGetterParams) => {
scopedContainerGetter: (args: SocketEventContext) => {
const container = Container.of(YOUR_REQUEST_CONTEXT);
container.set(myDiToken, 'MY_VALUE');
return container;
},
scopedContainerDisposer: (container: ContainerInstance) => {
container.dispose();
},
controllers: [__dirname + '/controllers/*.js'],
middlewares: [__dirname + '/middlewares/*.js'],
});
```

## Interceptors

Interceptors allow you to wrap your event handlers in higher order functions.
With interceptors you can add logging or modify the incoming or outgoing data for event handlers.

```typescript
import {
SocketController,
OnMessage,
EmitOnSuccess,
EmitOnFail,
SkipEmitOnEmptyResult,
UseInterceptor,
MessageBody
} from 'socket-controllers';

const interceptor: InterceptorInterface = {
use: (ctx: SocketEventContext, next: () => any) => {
ctx.messageArgs[0] = 'modified message from controller - ' + ctx.messageArgs[0];
const resp = next();
return 'modified response from controller - ' + resp; // modified response from controller - modified response from method - reponse
},
};

@Service()
class Interceptor implements InterceptorInterface {
async use(ctx: SocketEventContext, next: () => any) {
ctx.messageArgs[0] = 'modified message from method - ' + ctx.messageArgs[0];
const resp = await next();
return 'modified response from method - ' + resp; // modified response from method - reponse
}
}

@SocketController()
@UseInterceptor(interceptor)
export class MessageController {
@OnMessage('get')
@EmitOnSuccess('get_success')
@SkipEmitOnEmptyResult()
@UseInterceptor(Interceptor)
get(@MessageBody() message: string): Promise<Message[]> {
console.log(message); // modified message from controller - modified message from method - original message
return 'response';
}
}
```

Interceptors are executed in order of definition, starting with the controller interceptors.


## Decorators Reference

| Signature | Description |
Expand Down
Loading