mixsts
v1.0.7
Published
MVC library for faster development for typescript
Downloads
14
Readme
Mixs.ts
MVC library for faster development for typescript
Let's start
Installation
npm install mixsts
init typescript tsconfig.json
tsc --init
We need to do some configuration in tsconfig.json
"experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
"emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
Optional
"path":{
"@mix":[
"mixsts/core"
]
}
if you set path as above you need to insall tsconfig-paths npm packages or you can take a look inside node_modules/mixsts/libs exmple project structure
Folder's structure
libs
│ app.ts // contain server file
└───configs
│ │ app.ts
└───controllers
│ app.controller.ts
middlewares/
etc...
tsconfig.json
Simple usage
// configs/app.ts
import { setCoreConfig } from "@mix/config"
// app configuration
setCoreConfig({
port:3000 // set app port
// you can also set middlewares, aws, socket, etc... time to write
})
// app.ts
import "configs/app.ts"
import MixServer from "mixsts"
const mix = new MixServer()
mix.run() // start application
Let's create fitst controller
controllers/index.controllers.ts
import { Get, Controller } from "@mix/controllers"
import { Context } from "@mix/options"
@Controller("index") // localhost:3000/index
export default class IndexController {
@Get()
async index(context:Context) :Promise<void> {
contex.json("Hello world")
}
}
create global call controller and add controller to configs/app.ts
// configs/controllers.ts
import { controllerConfig, loadController } from "@mix/controllers"
loadController({
require:[
require("../controllers/index.controller.ts")
// or set path in tsconfig.json for best practice
require("@controllers/index.controller.ts")
]
})
export default controllerConfig
// configs/app.ts
import { setCoreConfig } from "@mix/config"
import controllerConfig from "./controllers"
// app configuration
setCoreConfig({
port:3000 // set app port
controllerConfig: controllerConfig
// you can also set middlewares, aws, socket, etc... time to write
})
Socket.IO
// configs/app.ts
socket:{
transport: ["websocket"]
}
Create event folder
// events/index.ts
import { OnConnection, OnDisconnect, OnEvent, OnEventError } from "@mix/socket";
import { Socket } from "socket.io";
export default class Event {
@OnConnection()
async onConnection(socket: any): Promise<void> {
const socketId = socket.id // socket id
socket.context = socket.handshake.auth
console.log("connected:", socketId)
}
// @OnEventMiddleware(1)
// async auth(socket: Socket, next: any): Promise<void> {
// next()
// }
@OnEventError()
async onError(error: any, socket: Socket): Promise<void> {
console.log(socket.id)
console.log("error: ", error.message)
}
@OnDisconnect()
async disconnect(socket: any): Promise<void> {
console.log("Disconnected:", socket.id)
}
@OnEvent("chat")
async chat(payload: any, socket: any): Promise<void> {
console.log(payload)
console.log(socket.id)
// your logic here
}
@OnEvent("message")
async message(payload: any, socket: any): Promise<void> {
console.log(payload)
console.log(socket.id)
// your logic here
}
}
add config file for socket global load
configs/events.ts
import { loadEvent, socketConfig } from "@mix/socket";
loadEvent({
require: [
require("@events/socket")
]
})
export default socketConfig
adjust configs/app.ts`
socket: {
transports: ["websocket"],
enableConnectionLog: true,
events: socketConfig
}