anchovycation / wassapi

Easy to use and file system based WebSocket API server creator
GNU General Public License v3.0
0 stars 0 forks source link

INITIALIZE #1

Open saracalihan opened 1 year ago

saracalihan commented 1 year ago

Its start to initialize stage. Main purpose of this stage is defining the technical and usage fundementals the developing V0

saracalihan commented 1 year ago

Request Lifecycle

  1. Incoming request
  2. Global middleware
  3. Module middleware
  4. Event pre-hook
  5. Event handler/triger 5.1 Handle error then return response
  6. Event Post-hook
  7. Pipe
  8. Response
saracalihan commented 1 year ago

User Folder Structure

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

Event Usage

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}){

  }
}
saracalihan commented 1 year ago

Middleware defining and usage

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()