0.0.69 • Published 3 years ago

@zoranwong/acc-engine.js v0.0.69

Weekly downloads
166
License
MIT
Repository
-
Last release
3 years ago

acc-engine.js

acc-engine.js is a js framework for web development. It provides a container for our application to organize the objects in the application.

What

acc-engine.js is a js framework for web development. It provides a container for our application to organize the objects in the application.And also acc-engine.js is a tool box.It provides http client, websocket client, worker pool, config manager, event bus , command interface and so on.

Why

acc-engine.js can help you to make your code more expandable and readable, and organize objects in your projects easier.For example, you can intercept http by middleware( which you can write you logic code before or after send request to server).

How

  • provider

    When you run the acc-engine.js, the framework will put some system's providers into your app.
    - System's providers
        
            - WorkerServiceProvider // provider worker service
            - HttpServiceProvider, // provider http service 
            - CommandServiceProvider, // provider command serive 
            - EventServiceProvider, // provider event bus service
            - ModelServiceProvider // provider global model state service
    // implement ServiceProvider interface
    import {ServiceProvider} from "@zoranwong/acc-engine.js";
    import VuexServiceProvider from "VuexServiceProvider";// vuex state service for acc-engine.js
    class ConfigServiceProvider extends ServiceProvider {
        constructor(app) {
            super(app);
        }
        register(){
            this.app.config.app = {
                providers: [VuexServiceProvider]// you can config the providers 
            };// set config
        }
    }
    
    // in the entry script add ConfigServiceProvider to app 
    // main.js (vue)
    import App from './App.vue'
    import Application from "@zoranwong/acc-engine.js";
    /**@var {Application} app*/
    const app = Application.getInstance();
    app.registerProvider(ConfigServiceProvider);
    app.rootComponent = App;
    app.run();
  • http service

  • Adapter class // http based Adapter class, you can implement it to adapt you http client sdk.

  • XMLHttpAdapter // XMLHttpRequest Adapter class, set http Adapter to http client to send request.

  • Client class // http client class

  • Request class // you will implement this class to define a new request class which contains http method, uri,

      _headers = {}; // http headers 
      
      _data = {}; // http request data or params
      
      _uri = ''; // http request uri
          
    //_uri is http request uri 
    class OderRequest extends Request {
        _uri = '/orders/{id}'
        _data = {
            id: '',// replace {id} in _uri
            status: '', // put in post or put http body or concat params on http queryStr
        }
    }
    _method = '';// http method
    
    _middleware = []; // http interceptors array, you can put some common logic in middleware
    
    _responseClass = Response // http response class, you can extend the Response for your request
    
    requestMiddleware () // you can append some to the _middleware and also not overwrite the parent's data
    
    static async send(...params) // static method to send http request, you can call this method through the request child class which you extend
    
    constructor(HttpRequestOption options)
    
    class HttpRequestOption {
             headers = null;
             data = null;
             uri = null;
             method = HttpMethod.GET;
             middleware = null;
             responseClass = Response 
      }
      
      //You can send data using the object of HttpRequestOption's struct
      
      Request.send(new HttpRequestOption({headers, data, uri, method, middleware, responseClass}));
      
      Request.send({headers, data, uri, method, middleware, responseClass});
    setHeader(name, value)
    
    getHeader(name)
    
    rules() define an array of validation rules for request data in the return data.
    
    messages() define an array of validation error messages for request data in the return data.
    
    errors() return the validation errors
    
    passed() determine whether the verification is passed
    
    get data() 
    
    get headers()
    
    get uri()
    
    get middleware()
    
    get method()
    
    get responseClass()
    
        
  • Response class

  • Middleware class

  • HttpMethod http method dictionary

    {
        GET: 'GET',
        POST: 'POST',
        PUT: 'PUT',
        DELETE: 'DELETE',
    }
    // define login response
    import {Response, HttpMethod} from "@zoranwong/acc-engine.js";
    class LoginResponse extends Response{
        token = null;
        ttl = null;
        refreshTtl = null;
        constructor(status, httpStatus, body, headers = {}){
            super(status, httpStatus, body, headers);
            let data = this.body['data'];
            this.token = data['token'];
            this.ttl = data['ttl'];
            this.refreshTtl = data['refresh_ttl'];
        }
    }
    // define a middleware to intercept http 
    import {Middleware} from '@zoranwong/acc-engine.js';
    // password will be hashed before request send to server
    class PasswordHashMiddleware extends Middleware{
        async handle(request, next) {
            let password = request.data['password'];
            request.data['password'] = hash(password);
            // before send 
            let res = await next(request);
            // after send
            return res;
        }
    }
    //define login request
    import {Request} from "@zoranwong/acc-engine.js";
    class LoginRequest extends Request {
        _data  = {
            user_name: null,
            password: null
        };
        _method = HttpMethod.GET;
        _uri = '/login';
        _responseClass = LoginResponse;
        constructor(userName, password) {
            super();
            this._data['user_name'] = userName;
            this._data['password'] = password;
        }
        get middleware(){
            return [...super.middleware, PasswordHashMiddleware];
        }
    }

    // set http gateway
    import {Applocation} from '@zoranwong/acc-engine.js'; 

    let app = Applocation.getInstance();
    app.config.http = {
            gateway: 'http://test.dev',
            headers: {},
            middleware: []
        };// you can set the http config in you config provider
    
    async function login() {
      let response = await  app.http.send(new LoginRequest('xxxx', 'yyyyyy'));
      // or let response = await app.http.send(new LoginRequest('xx','xx'), LoginResponse);
      let b = await LoginRequest.send('xxx', 'xxxx');
    }

    // You can define a login command
    import {Command} from '@zoranwong/acc-engine.js';
    class LoginCommand extends Command{
        async handle(userName, password){
            let res = await LoginRequest.send(userName, password);
        }
    } 

    // register command to container
    app.registerCommand('login', LoginCommand);
    let userName = 'xx';
    let password = 'xxx';
    // execute command to login
    app.command('login', userName, password);
  • command service

    Command service is the practice of command design pattern.We abstract the behavior surrounding data into interactive commands one by one.So the framework lets you can use the command-based interactive mode to initiate the behavior you want to complete.
    // You can define a login command
    import {Command} from '@zoranwong/acc-engine.js';
    import LoginRequest from 'LoginRequest';
    class LoginCommand extends Command{
        async handle(userName, password){
            let res = await LoginRequest.send(userName, password);
        }
    } 

    // register command to container
    app.registerCommand('login', LoginCommand);
    // you can create a command provider to manage and provider commands for application
    let userName = 'xx';
    let password = 'xxx';
    // execute command to login
    app.command('login', userName, password);
  • validate request

    You only need to implement the rules() and messages() method which in the abstract of Request, the application will auto to validate the data which you send to the server.

    In our system it provides a set of rules for programming.

    KeySyntaxdescription
    betweenbetween:10,10010 <= a < 100
    emailemailthe value must be a valid email address
    floatfloatthe value must be a valid float
    inin:2,5,7,9the value must be in 2,5,7,9
    integerintegerthe value must be an integer
    maxmax:1000the value must smaller than 1000 (a < 1000)
    minmin:10the value must be greater than 10 (a >= 10)
    not_innot_in:1,7,8,10the value must be not in 1,7,8,10
    not_nullnot_nullthe value mustn`t be null
    numbernumberthe value must be a number
    requiredrequiredthe value is required
    sizesize:16the value`s size must smaller than 16
    stringstringthe value must be a valid string
    not_emptynot_emptythe value mustn`t be null, empty string and undefined
    //define login request
    import {Request} from "@zoranwong/acc-engine.js";
    class LoginRequest extends Request {
        _data  = {
            user_name: null,
            password: null
        };
        _method = 'POST';
        _uri = '/login';
        _responseClass = LoginResponse;
        constructor(userName, password) {
            super();
            this._data['user_name'] = userName;
            this._data['password'] = password;
        }
        get middleware(){
            return [...super.middleware, PasswordHashMiddleware];
        }
        rules(){
            return {
                user_name: 'required|string|size:32',//['required', 'string', 'size:32']
                password: ['required', 'string', 'size:1024']
            };
        }
        
        messages(){
            return  {
                'user_name.required': 'user_name is must attribute!',
                'user_name.string': 'user_name`s value must be a string',
                'user_name.size': 'user_name`s value length must smaller then 32'    
            };
        }
    }

    LoginRequest.send('Mike Jackson', 'xxxxxxxx')

SearchRequest SearchRequest SearchRequest

  • model service

    • setModel(data) this method can reset the property of model which you defined,and it can case snake's key to camel's key which you defined in model.

    • cacheAttributes which attributes will be cached in.

    class User extends Model {
        username = null;
        headImage = null;
        constructor(options) {
            super(options);
            this.initial(options);//initial the model
        }
    }
    // User.instance(app, 'model');
    let user = new User({username: 'Jack',head_image: '--------------'});
  • websocket service

  • worker service

  • create a config provider to contain your project's config objects

Using acc-engine.js in Vue project

  • use vuex

  • use vue-router

  • use command

Using acc-engine.js in uniapp project

Using acc-engine.js in react project

0.0.67

3 years ago

0.0.68

3 years ago

0.0.69

3 years ago

0.0.66

3 years ago

0.0.65

3 years ago

0.0.64

3 years ago

0.0.62

3 years ago

0.0.63

3 years ago

0.0.61

3 years ago

0.0.60

3 years ago

0.0.59

3 years ago

0.0.58

3 years ago

0.0.57

3 years ago

0.0.56

3 years ago

0.0.54

3 years ago

0.0.55

3 years ago

0.0.51

3 years ago

0.0.52

3 years ago

0.0.53

3 years ago

0.0.50

3 years ago

0.0.49

3 years ago

0.0.46

3 years ago

0.0.47

3 years ago

0.0.48

3 years ago

0.0.43

3 years ago

0.0.44

3 years ago

0.0.45

3 years ago

0.0.42

3 years ago

0.0.41

3 years ago

0.0.40

3 years ago

0.0.39

3 years ago

0.0.38

3 years ago

0.0.37

3 years ago

0.0.36

4 years ago

0.0.35

4 years ago

0.0.34

4 years ago

0.0.25

4 years ago

0.0.30

4 years ago

0.0.31

4 years ago

0.0.32

4 years ago

0.0.26

4 years ago

0.0.27

4 years ago

0.0.28

4 years ago

0.0.29

4 years ago

0.0.24

4 years ago

0.0.22

4 years ago

0.0.23

4 years ago

0.0.21

4 years ago

0.0.20

4 years ago

0.0.18

4 years ago

0.0.19

4 years ago

0.0.17

4 years ago

0.0.16

4 years ago

0.0.14

4 years ago

0.0.15

4 years ago

0.0.13

4 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago