Open saracalihan opened 1 year ago
Module Based Architecture: i prefer module based structure because in big projects services based structure is more complicated and folder finding is more difficult
.
├── main.ts
├── wasapi.config.json # or wasapi.config.js|ts
├── test
├── configs
└── src
├── utils
└── modules
├── ...
└── user
├── user.event.ts
├── dtos
│ └── message.dto.ts
├── user.middleware.ts # optional
├── user.hook.ts # optional (pre/post)
├── user.service.ts # optional
└── user.model.ts # optional
Component Based Architecture: this will be optionally support.
.
├── main.ts
├── wasapi.config.json # or wasapi.config.js|ts
├── test
├── configs
└── src
├── events
│ ├── user.ts
│ └── message.ts
├── middlewares # optional
├── hooks # optional
├── models # optional
└── services # optional
Object style: all flows convert to this style. Most powerful style
// In order to export more than one object from a file, the object must be returned in the array.
export default {
prefix: 'user',
middleware: funcD, // its local middleware. Global middleware bind at wasapi object .
events: [{
event: 'create-message', // 'user:create-message'
handler: funcA,
preHook: funcB,
postHook: funcC,
}]
};
Function style:
type SocketInfo = {socket: Socket, io: IO};
const
eventName = 'user-register',
handlerFunction = (data: any, socket: SocketInfo) => {},
hook = (data, socket: SocketInfo) => {},
middleware = (data, next, socket: SocketInfo) => {
console.log(data);
next();
};
app.on(eventName, handlerFunction, {
preHook: hook,
postHook: hook,
middleware,
});
Class Style:
class Message extend Event{
// event handler must be start with 'on' prefix
onUserWelcomeMessage(data, {socket, io}){
}
}
Defining:
const middleware = (data: any, next: MiddlewareNextFunction, {socket: Socket, io: IO}) => {
// some code
data.isChecked = false;
next(data);
// The next function must be called so that it can be passed to the next middleware and finally to the handler function.
// if you pass a value to next function, the value pass to next middleware.
// else original `data` pass to next middleware
}
Usage: It is added globally to the application in the order in which middleware functions are called, and this must be used before the listen function
// main.ts
app.use(middlewareFunc)
app.use(middlewareFunc2)
app.listen()
Its start to initialize stage. Main purpose of this stage is defining the technical and usage fundementals the developing V0