currentrequest
v2.2.3
Published
Request handling engine for handling server or small task. for more visit : readme.md
Downloads
337
Maintainers
Readme
CurrentRequest
CurrentRequest is engine which can execute any given batch of method's or function's which you want to execute small or big task based on your need. with some inbuilt functionality which are pretty much help full for developing application's in javascript.
Use case's :
- Creating REST Api's
- Creating javascript application's
- Request Handler
Below are considiration what CurrentRequest is about:
- Dry Code
- Scalable Project Structuring
- Pauseable Server
- Multiple dataBase or without database.
- Machine Learing support
- Smart Cache
- Integration [AWS, Sentry, Ejs and Pug].
Getting Started
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
Documentation : comming soon
Prerequisites
1. Enviornment Variable's.
Details: CurrentRequest need some environment variable which will going to be needed for handling project. below are all environment variable's on which currentRequest depend's on. you can change them if your project folder location's are different.
NAME ='CurrentRequest' // projectname : used as Database name and Server name
DMDir = 'dyna_modules' // directory: directory name containg your method's.
MLDir = 'ml_modules' // directory: machine learning directory name module's directory.
GUIDir = 'GUI' // directory : directory name containg view in following languages [pug, ejs]
ENVIRONMENT=development // debug: used when debuging error and return's by method.
CR_RETURN_TYPE=configuration // output : current request process your request and can return output [single object] or internal configuration containg every information form who runned and when.
AWS_SECRET_ACCESS_KEY="Your AWS sceret accesskey" // AWS : used when connect's with AWS account.
AWS_ACCESS_KEY="access_key" // AWS : used when connect's with AWS account.
SENTRY_DSN="dsn_key" // ERROR : useful for handling any error occured internally in queue.
2. Database Driver
Details: Each batch of task output return's [array] containg output of each method.now you can store these output to database for that just run your database server currentRequest currentlly support's mongoDB[stable] and mySql[comming soon] or if you want to see just output no database run than just pass _noDatabase key in configuration.
Note 1: Query will be auto generated so in database you will going to see structure to your every collection > document
Note 2: By default mongoDB is used is database option for other database's pass { _connectTo : "databaseName" } in configuration.
Structure :
_RAW: {
_method1 : "methodOutput",
_method2 : "methodOutput",
_method3 : "methodOutput"
}
Installing
CurrentRequest can installed with following flavour's.
Npm :
npm i currentrequest --save
Yarn :
yarn add currentrequest
Clone :
git clone https://gitlab.com/rootandleaves/currentrequest.git
Configuration
CurrentRequest require's configuration in-order to run. each configuration key relates to special work in engine.
- whichDoc : whichDoc defines on which document or table name you want to work on in database.
- hooks : hooks object can contain for way's or all way's { _onLoad: {}, _onExecute: {}, _onReturn: {}, _onError: {} } to execute external method's..
- RAWInclude : allowed method's name which will be going to stored in database.
- FileManager_Config, : file manager for handling buffer or stream in request.
- _POST: request payload [form-data etc..] need to be JSON format.
- _POST_SCHEMA : schema for handling POST any key which is not defined in schema will result in delete POST[key] for safe POST.
- returnFormat : currentRequest can return data in two format's either 'data' || 'configuration'
- task : which curd operation you wan't to perform [read, create, drop, update, optimizedCreate]
Please note : This is not complete list of configuration for that please read doc's
X-Header
CurrentRequest has special method named xHeader which allow's currentrequest to validate user's or client's based on interaction. these is done as follow's :
Step's
- When xHeader is called by engine it check's for header key : "y" which contain unique identifier [of your choice..]
- Check's if y belong's to someone or if it does than it check's what "masterEmail" you provided with that fail's it will start counting for blocking given "ip" address after 3 max trie's. if not than it continue's to step 3.
- if "masterEmail" and it's "y" key match's than it continue's execution..skipping step 4
- It Take's "y" key value from header and than generate new unique key based on "y" and pair them together.
- Re-update request with two new key's : "x" and "y"
- Continue request as usual.
Why xHeader.
- it stop's abuse of server and queue execution. as validation fail's engine will not going to continue with given ip address.
- It allow's request to structure itself with engine as each request "y" is going to contain unqiue identifier. you can create anything you like ex: request.headers['y'] = 'SMARTAPP::uniqueString' for your application's.
How to use ?
Below is simple example of account registration api in less than 1 min.
MongoDB Server :
sudo mongod --master
Redis Server :
redis-server /usr/local/etc/redis.conf; sudo redis-server /usr/local/etc/redis.conf
Code Setup :
import CurrentRequest from 'currentRequest' // npm: modern code handler tool.
import Joi from 'joi' // npm: object validation libary.
/*
GLOBAL'S.
*/
let _ProceedLogin, _loginConfiguration
// ServerAccountRegistrationRequest is conceptual method describing
that this request handler for any given route let say /accountRegister.
ServerAccountRegistrationRequest(request, reply) => {
// variable assignment
_loginConfiguration = {
name: 'SomeFancyRegistration'
whichDoc: 'Users',
hooks: {
'_onLoad': {
'registration': await (Context) => {
// [Context](http://currentrequest.com/docs#Context) api as lot other feature's
// out of them one is Configuration it allow's you
// to load data or execution update's..here we are
// loading our payload passed in configuration.
// using configuration you can get any key written
// in configuration you are passing.
let { _POST } = await Context.Configuration.get()
...after your code...
// return data to queue.if you want
// to pause queue so that it don't execute
// further than return queue with CR_Paused
// options set to true. also not that
// every method should return _RAW_SCHEMA
// as this acts as return schema for this
// method which is used while writing output's
// to database.
if(...JustStopExecution...) {
// return _POST.
return {
..._POST,
'options': {
'_RAW_SCHEMA': Joi.object(),
'CR_Paused': true
}
}
} else {
// return _POST.
return {
..._POST,
'options': {
'_RAW_SCHEMA': Joi.object()
}
}
}
}
}
},
'FileManager_Config': {
'whereToStore': 'local', // change to S3 and update your AWS credential's in enviornment it will start putting all update's to s3 bucket.
'path': './userAccountPhotoDirectory' // auto create directory and store any file uploaded to this route.
},
'_POST': __request.payload, // request payload..
'_POST_SCHEMA': { // Joi schema for validating payload's..only allow those whose name is present in _POST_SCHEMA else are removed..
'email': Joi.string().email(),
'username': Joi.string(),
'password': Joi.string(),
'profilePicture': Joi.object()
},
'returnFormat': 'data', // return only method's output stored in _RAW key..if you wan't to return configuration which you are reciving inside hook's than
just change 'data' => 'configuration'
'task': 'CREATE' // this task define's which crud operation will going to happen.
}
// Before execution please update request with "y" header
// you can do this on client side and then send request
// or you can just create and manage key here by itself
// just by passing "y": "unqieue string"
if(...ClientSideHeaderCreated...) {
// run as normal request.
_ProceedLogin = await new CurrentRequest({ request, 'options': _loginConfiguration })
} else {
// create new "y" key.
request.headers['y'] = 'somefancyYKey'
// continue normal execution.
_ProceedLogin = await new CurrentRequest({ request, 'options': _loginConfiguration })
}
// if _ProceedLogin contain's error than report failure.
// else return Output.
if(!_.propertyOf(_ProceedLogin)) {
// local variable.
let _replyOutput
...do something with output...
_replyOutput = ...did something with _ProceedLogin.Output...
// return data.
return _replyOutput
} else {
// report failure.
return _ProceedLogin.error
}
}
Built With
- no-external-weight-dependency - Most of the libraries are created in pure node.js.
- AWS - For AWS Sdk.
- Sentry - For Bug reporting and catching.
- MongoDB - Mongodb driver for mongodb database.
- MySql - Mysql driver for handling mysql dataBase.
- Redis - For cache management in queue.
Versioning
We use Semanticversioning for versioning. For the versions available, see the tags on this repository.
Authors
- Nitesh Yadav - Initial work - Rootandleaves
License
This project is licensed under the MIT License - see the LICENSE.md file for details
Acknowledgments
- For any special attention please mail me email: [email protected], [email protected]