is a group of tools for developing Rest-APIs.
-
reply
: generate an structured Object as a response for clients. -
validateObject
: validate an object by given fields. -
createNavigator
: return an object with navigator properties. -
saveFromLink
: save file from a URL. -
removeFile
: remove a file. -
sleep
: return a promse wich could waite for milliseconds.
Install using npm:
npm i modular-rest-toolkit --save
const tools = require('modular-rest-toolkit');
this tool will generate a simple structure based on 3 type of reply data, message, error
. each reply has two property: status, detail
;
here is how to generate a reply:
// base parameters
tools.reply(status, detail);
// generate a success state
tools.reply('s', {'d': ['product1', 'product2']});
/*
{
'status': 'success',
'data' : ['product1', 'product2'],
}
*/
passing char | result |
---|---|
s | success |
f | fail |
e | error |
passing char | result |
---|---|
{d: ''} | {data: ''} |
{e: ''} | {error: ''} |
{m: ''} | {message: ''} |
this tool could be useful for forms to validate required fields. here is how to generate a reply:
//== check form object to figure out 'name phone' fields are not null. ==
let form = {
'name' : 'navid',
'family': 'your family',
'phone' : null,
'city' : 'your city'
};
// simple validation
let validated = tools.validateObject(form, 'name phone');
// returned {isValid: false, requires: ['phone']}
// precise validation
let option = {
name : '', // could has any value except null
phone : '', // could has any value except null
city ; 'c1 c2 c3', // could has only one of these values.
};
let validated2 = tools.validateObject(form, option);
// returned {isValid: false, requires: ['phone', 'city']}
this is a tool for create a navigator object with "pages, page, from, to" properties. it would be useful when you have a service which provides items troughout pages.
let totalItems = 100;
let itemPerPage = 10;
let page = 1;
let navigator = tools.createNavigator(totalItems, itemPerPage, page),
/*
{
// total pages & current page
pages: 10, page: 1,
//for selecting items from 0 to 10
from: 0, to: 10,
}
*/
tools.saveFromLink(url, saveAs);
tools.removeFile(path);
let ms = 1000;
await tools.sleep(ms).then();
thank you for using Modular-Rest-toolkit :)