@decisit/chirujs

Asynchronous service micro library

Stats

StarsIssuesVersionUpdatedCreatedSize
@decisit/chirujs
2.0.33 years ago3 years agoMinified + gzip package size for @decisit/chirujs in KB

Readme

ChiruJS

Asynchronous service micro library (no runtime dependencies)

Build Status codecov

Documentation: https://decisit.com/community/chirujs/

Installation

ChiruJS is available via NPM:

$ npm install --save @decisit/chirujs

Source code

ChiruJS source code is available on GitHub:

https://github.com/decisit/ChiruJS

Your first service

const Service = require('@decisit/chirujs').Service;

class MyAwesomeService extends Service {
  async execute() {
    return (this.params.n * 2);
  }
}

new MyAwesomeService({ n: 21 }).run().then((result) => {
  console.log(result.success); // true
  console.log(result.output);  // 42
});

Requiring parameters

const Service = require('@decisit/chirujs').Service;

class MyAwesomeService extends Service {
  async prepare() {
    this.n = this.requireParam('n');
  }

  async execute() {
    return (this.n * 2);
  }
}

new MyAwesomeService().run().then((result) => { // Not supplying 'n'
  console.log(result.output);                   // This will never happen
}).catch((error) => {
  console.log(error.result.success);            // false
  console.log(error.result.messages);           // [{ scope: "base", message: "MissingParameter: n" }]
});

Requiring parameters (more)

const Service = require('@decisit/chirujs').Service;

class MyAwesomeService extends Service {
  async prepare() {
    this.n = this.requireParam('p1', { default: 12 });
    this.n = this.requireParam('p2', { default() { return (34) } });
    this.n = this.requireParam('p3', { of: Number });
    this.n = this.requireParam('p4', { allowNull: false });
  }
}

Requiring parameters (rules)

requireParam fails if:

  • parameter name is not in params (hasOwnProperty) and options.default is absent
  • parameter value is null/undefined, options.allowNull is false and options.default is absent
  • options.of is present and parameter value is not instance of it

Try/catch

const Service = require('@decisit/chirujs').Service;

class MyAwesomeService extends Service {
  async prepare() {
    this.n = this.requireParam('n');
  }

  async execute() {
    return (this.n * 2);
  }
}

try {
  const result = await new MyAwesomeService().run();
  // ...
} catch (error) {
  console.log(error.error);  // original error
  console.log(error.result); // result
}

Subservice(s)

const Service = require('@decisit/chirujs').Service;

class MyHelloService extends Service {
  async prepare() {
    this.subject = this.requireParam('subject');
  }

  async execute() {
    return (`Hello ${this.subject}`);
  }
}

class MySuffixedHelloService extends Service {
  async prepare() {
    this.suffix = this.requireParam('suffix');
    this.subject = this.requireParam('subject');
  }
  
  async execute() {
    const hello = await this.subserviceOutput(MyHelloService, 'hello', { subject: this.subject });
    return (`${hello}${this.suffix}`);
  }
}

const result = await new MySuffixedHelloService({ subject: 'World', suffix: '!' }).run();
console.log(result.output); // Hello World!

Rollback

const Service = require('@decisit/chirujs').Service;

class MyAwesomeService extends Service {
  async prepare() {
    this.model = this.requireParam('model');
  }

  async execute() {
    this.model.set('someAttribute', 'newValue');
    // ...
    // ...
    this.fail("Can't do this anymore!");
  }
  
  // rollback implementation should restore state of domain object(s)
  async rollback() {
    this.model.reset();
  }
}

Rollback with subservice(s)

const Service = require('@decisit/chirujs').Service;

class UpdateProfileService extends Service {
  async prepare() {
    this.profile = this.requireParam('profile', { allowNull: false });
  }

  async execute() {
    if (this.profile.get('login') == null || this.profile.get('login') == '') {
      this.fail("Profile login can't be empty");
    }
    this.profile.save();
  }
  
  async rollback() {
    // subservice is rollbacked, then service is
    this.profile.reset();
  }
}

class UpdateUserAndProfileService extends Service {
  async prepare() {
    this.user = this.requireParam('user', { allowNull: false });
    this.profile = this.requireParam('profile', { allowNull: false });
  }

  async execute() {
    // ...
    await this.subserviceRun(UpdateProfileService, 'profile', { profile: this.profile });
    // ...
  }
  
  // rollback implementation should restore state of domain object(s)
  async rollback() {
    // subservice is rollbacked, then service is 
    this.user.reset();
  }
}

Contact us

Email: community@decisit.com

Discord: https://discord.gg/k8KPHeW

Licence

The MIT License

Copyright (c) 2018 DECISIT SAS https://decisit.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.