0.2.1 • Published 6 years ago
node-service-layer v0.2.1
Service-Layer
Realization of Service Layer pattern
Table of Contents
Installation
npm install github:Maxwell88/service-layeror
yarn add github:Maxwell88/service-layerUsage
// serviceLayer.js
import { ServiceLayer } from "service-layer";
const koaResolver = result => this.body = result;
const koaArgsBuilder = args => args[0];
export const new ServiceLayer(
koaResolver,
koaArgsBuilder
{
before:[
/* Your before rules for example: */
{
name: "exampleRequireRule",
type: "require",
execute: (ctx,args,serviceData) => {
/*
rule logic and return changed context to another rule
or if it last than to service
*/
return ctx
}
}
],
after: [
/* Your after rules */
]
}
});// router.js
import Router from "koa-router";
import ServiceLayer from "./serviceLayer";
import ExampleServiceClass from "./ExampleServiceClass";
const router = new Router({ prefix: config.PREFIX });
router.post("/example", ServiceLayer.useService(ExampleServiceClass));
export default router;// router.js
import { Service } from "service-layer";
export default class Create extends Service {
/*
there you can iniate your required and custom rules for example:
*/
static exampleRequireRule = "simple args"
static exampleCustomRule = "simple args"
async execute(ctx) {
/*
service logic
*/
let result;
return result; // return data to resolver
}
}Documentation
Service Layerconstructor(resolver:function, argumentBuilder: function, rules: object): ServiceLayeruseService(service: Service): FunctionargumentBuilder- function that have one field that is array of arguments that receives middleware of Koa/express/hapi etc. and must return changed context that would been used at future.resolver- function that have one argument -result, that argument contain a final result of service,thisof resolver is a context fromargumentsBuilder. Rules don`t have influence at resolverthisrules- object that have two fieldbefore:arrayandafter:arraywith objects of rule that have 3 required fieldname,type,execute:Key Type Required Note nameString+ Name of rule, and name of static param for Serviceclass if type of rule nothiddentypeone of hidden,requiredorcustomstrings+ type of function behavior . hidden- execute always, don`t use static field ofServiceclass,required- execute always, static field ofServiceclass is required otherwiseRULE_IS_REQUIREDexeption,custom- execute only if static field is not emptyexecuteFunction+ Name of rule, and name of static param for Serviceclass
ServiceService class for inheritance with required
execute: functionfield (look usage).
Exeptionconstructor(data: object): ErrorThe data object has the following fields:Key Type Required Note fieldsobjectwith string fields or empty+ Errored fields codeString+ Exeption code
License
Licensed under the MIT license.
0.2.1
6 years ago