Skip to content

Commit

Permalink
Merge fde6d31 into 44cec1d
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Apr 14, 2019
2 parents 44cec1d + fde6d31 commit 71c4787
Show file tree
Hide file tree
Showing 27 changed files with 431 additions and 336 deletions.
6 changes: 6 additions & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,11 @@ module.exports = {
}
}
}
},
markdown: {
lineNumbers: true,
config: md => {
md.use(require("vuepress-theme-tsed/plugins/markdown-it-symbol"));
}
}
};
49 changes: 28 additions & 21 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ sidebar: auto
prev: false
next: /configuration.html
otherTopics: true
meta:
- name: description
content: Start a new REST application with Ts.ED framework. Ts.ED is built on top of Express and use TypeScript language.
- name: keywords
content: getting started ts.ed express typescript node.js javascript decorators mvc class models
---

# Getting started

You can start your project from the [getting started project](https://github.com/TypedProject/ts-express-decorators/tree/master/integration/getting-started).
You can start your project from the [getting started project boilerplate](https://github.com/TypedProject/ts-express-decorators/tree/master/integration/getting-started).

## Installation from scratch

Expand Down Expand Up @@ -54,13 +59,13 @@ options in your `tsconfig.json` file.
"compilerOptions": {
"target": "es2016",
"lib": ["es2016"],
"types": ["reflect-metadata"],
"typeRoots": [
"./node_modules/@types"
],
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators":true,
"emitDecoratorMetadata": true,
"sourceMap": true,
"declaration": false,
"allowSyntheticDefaultImports": true
},
"exclude": [
Expand All @@ -69,11 +74,9 @@ options in your `tsconfig.json` file.
}
```

> **Note** : target must be set to ES2015/ES6 (or more).
### Optional

You can copy this example of package.json to develop your application:
You can copy this example of `package.json` to develop your application:

```json
{
Expand All @@ -89,7 +92,7 @@ You can copy this example of package.json to develop your application:
"test": "mocha --reporter spec --check-leaks --bail test/",
"tsc": "tsc --project tsconfig.json",
"tsc:w": "tsc -w",
"start": "nodemon --watch '**/*.ts' --ignore 'node_modules/**/*' --exec ts-node app.ts"
"start": "nodemon --watch '**/*.ts' --ignore 'node_modules/**/*' --exec ts-node src/index.ts"
},
"author": "",
"license": "ISC",
Expand Down Expand Up @@ -133,7 +136,6 @@ const rootDir = __dirname;
acceptMimes: ["application/json"]
})
export class Server extends ServerLoader {

/**
* This method let you configure the express middleware required by your application to works.
* @returns {Server}
Expand All @@ -151,37 +153,42 @@ export class Server extends ServerLoader {

return null;
}

public $onReady(){
console.log('Server started...');
}

public $onServerInitError(err){
console.error(err);
}
}

new Server().start();
```
> By default ServerLoader load controllers in `${rootDir}/controllers` and mount it to `/rest` endpoint.
To customize the server settings see [Configuration](configuration.md) page.

### Tree directories
Finally create an `index.ts` file to bootstrap your server, on the same level of the `Server.ts`:
```typescript
import {Server} from "./Server.ts"

new Server().start()
.then(() => {
console.log('Server started...');
})
.catch((err) => {
console.error(err);
})
```

By default ServerLoader load automatically Services, Controllers and Middlewares in a specific directories.
You should have this tree directories:

```
.
├── src
│ ├── controllers
│ ├── services
│ ├── middlewares
│ ├── index.ts
│ └── Server.ts
└── package.json
```

::: tip
By default ServerLoader load automatically Services, Controllers and Middlewares in a specific directories.
This behavior can be change by editing the [componentScan configuration](/configuration.md).
:::

## Create your first controller

Expand Down
12 changes: 12 additions & 0 deletions docs/tutorials/snippets/socketio/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {ServerLoader, ServerSettings} from "@tsed/common";
import "@tsed/socketio"; // import socketio Ts.ED module

@ServerSettings({
rootDir: __dirname,
socketIO: {
// ... see configuration
}
})
export class Server extends ServerLoader {

}
9 changes: 9 additions & 0 deletions docs/tutorials/snippets/socketio/socket-input-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Args, Input, Namespace, Socket, SocketService} from "@tsed/socketio";

@SocketService("/my-namespace")
export class MySocketService {
@Input("eventName")
myMethod(@Args(0) userName: string, @Socket socket: Socket, @Namespace nsp: Namespace) {
console.log(userName);
}
}
9 changes: 9 additions & 0 deletions docs/tutorials/snippets/socketio/socket-middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Socket, SocketErr, SocketEventName, SocketMiddlewareError} from "@tsed/socketio";

@SocketMiddlewareError()
export class ErrorHandlerSocketMiddleware {
async use(@SocketEventName eventName: string, @SocketErr err: any, @Socket socket: Socket) {
console.error(err);
socket.emit("error", {message: "An error has occured"});
}
}
10 changes: 10 additions & 0 deletions docs/tutorials/snippets/socketio/socket-send-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Args, Emit, Input, Socket, SocketService} from "@tsed/socketio";

@SocketService("/my-namespace")
export class MySocketService {
@Input("eventName")
@Emit("responseEventName") // or Broadcast or BroadcastOthers
async myMethod(@Args(0) userName: string, @Socket socket: Socket) {
return "Message " + userName;
}
}
17 changes: 17 additions & 0 deletions docs/tutorials/snippets/socketio/socket-service-di.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {Controller, Get} from "@tsed/common";
import {MySocketService} from "../services/MySocketService";

@Controller("/")
export class MyCtrl {

constructor(private mySocketService: MySocketService) {

}

@Get("/allo")
allo() {
this.mySocketService.helloAll();

return "is sent";
}
}
10 changes: 10 additions & 0 deletions docs/tutorials/snippets/socketio/socket-service-nsp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Namespace, SocketService} from "@tsed/socketio";

@SocketService()
export class MySocketService {
@Namespace nsp: Namespace;

helloAll() {
this.nsp.emit("hi", "everyone!");
}
}
36 changes: 36 additions & 0 deletions docs/tutorials/snippets/socketio/socket-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {IO, Nsp, Socket, SocketService, SocketSession} from "@tsed/socketio";
import * as SocketIO from "socket.io";

@SocketService("/my-namespace")
export class MySocketService {

@Nsp nsp: SocketIO.Namespace;

@Nsp("/my-other-namespace")
nspOther: SocketIO.Namespace; // communication between two namespace


constructor(@IO private io: SocketIO.Server) {
}

/**
* Triggered the namespace is created
*/
$onNamespaceInit(nsp: SocketIO.Namespace) {

}

/**
* Triggered when a new client connects to the Namespace.
*/
$onConnection(@Socket socket: SocketIO.Socket, @SocketSession session: SocketSession) {

}

/**
* Triggered when a client disconnects from the Namespace.
*/
$onDisconnect(@Socket socket: SocketIO.Socket) {

}
}
16 changes: 16 additions & 0 deletions docs/tutorials/snippets/socketio/socket-session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {Args, Emit, Input, SocketService, SocketSession} from "@tsed/socketio";

@SocketService("/my-namespace")
export class MySocketService {
@Input("eventName")
@Emit("responseEventName") // or Broadcast or BroadcastOthers
async myMethod(@Args(0) userName: string, @SocketSession session: SocketSession) {

const user = session.get("user") || {};
user.name = userName;

session.set("user", user);

return user;
}
}
18 changes: 18 additions & 0 deletions docs/tutorials/snippets/socketio/socket-use-middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {ConverterService} from "@tsed/common";
import {Args, SocketMiddleware} from "@tsed/socketio";
import {User} from "../models/User";

@SocketMiddleware()
export class UserConverterSocketMiddleware {
constructor(private converterService: ConverterService) {
}

async use(@Args() args: any[]) {

let [user] = args;
// update Arguments
user = this.converterService.deserialize(user, User);

return [user];
}
}
20 changes: 20 additions & 0 deletions docs/tutorials/snippets/socketio/socket-use-middleware2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {SocketService, SocketUseAfter, SocketUseBefore, Emit, Input, Args} from "@tsed/socketio";
import {UserConverterSocketMiddleware, ErrorHandlerSocketMiddleware} from "../middlewares";
import {User} from "../models/User";

@SocketService("/my-namespace")
@SocketUseBefore(UserConverterSocketMiddleware) // global version
@SocketUseAfter(ErrorHandlerSocketMiddleware)
export class MySocketService {

@Input("eventName")
@Emit("responseEventName") // or Broadcast or BroadcastOthers
@SocketUseBefore(UserConverterSocketMiddleware)
@SocketUseAfter(ErrorHandlerSocketMiddleware)
async myMethod(@Args(0) user: User) {

console.log(user);

return user;
}
}
Loading

0 comments on commit 71c4787

Please sign in to comment.