Add async functions supports into <Method>.execute
This commit is contained in:
parent
35799245ed
commit
0f9ba4fc7b
352
codeExample.js
352
codeExample.js
@ -1,177 +1,177 @@
|
||||
const njsbacker = require('./index');
|
||||
|
||||
// Создаём класс бэкенда, наследующий класс njsbacker.Main
|
||||
class Main extends njsbacker.Main {
|
||||
/* errorHandler (error) { return {
|
||||
mainbody : JSON.stringify({ error : {}, error }),
|
||||
headers : {
|
||||
errored : 1
|
||||
},
|
||||
cookies : {
|
||||
avava : 1
|
||||
},
|
||||
// redirect_uri: ''; // if want redirect to another url
|
||||
code: 400
|
||||
}};*/
|
||||
|
||||
session (params, sessionData) {
|
||||
sessionData._setValue('example', 1); // Задать значение
|
||||
console.log(sessionData.example); // Получить значение из сессии
|
||||
sessionData._remove('example'); // Убрать значение
|
||||
return 1; // Успешно
|
||||
throw 'Example error' // Пример ошибки
|
||||
}
|
||||
|
||||
responseHandler (response) { return ({
|
||||
mainbody : { response },
|
||||
headers : {
|
||||
errored: 0
|
||||
},
|
||||
cookies : {},
|
||||
// redirect_uri: ''; // if want redirect to another url
|
||||
code: 200
|
||||
}) };
|
||||
|
||||
/* paramsError (required, additional) { return({ required, additional }) }; */
|
||||
}
|
||||
// Создаём экземпляр класса Main
|
||||
var server = new Main(
|
||||
false // Отобразить в заголовках информацию о текущем фреймворке
|
||||
);
|
||||
server.setSessionParams( // Зададим необходимые параметры для сессии
|
||||
{
|
||||
session_id : {
|
||||
required : false,
|
||||
type : njsbacker.types.integer
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// Создаём класс группы методов
|
||||
class ExampleMethodGroup extends njsbacker.Group {
|
||||
handler (params, session) { // Путевая обработка
|
||||
session._setValue('example', 1); // Задать значение
|
||||
console.log(session.example); // Получить значение из сессии
|
||||
session._remove('example'); // Убрать значение
|
||||
return 1; // Успешно
|
||||
throw 'Example error' // Пример ошибки
|
||||
}
|
||||
}
|
||||
// Создаём классы методов
|
||||
class ExampleAnyMethodsOfHandlingInformation extends njsbacker.Method {
|
||||
execute (params, session, groups) {
|
||||
return {
|
||||
json_data : params.json_name,
|
||||
query_data : params.query_name,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class ExampleMethod extends njsbacker.Method {
|
||||
/*
|
||||
var result = this.MainObject.call(method : string, params : object) // Вызов подключённого метода
|
||||
*/
|
||||
|
||||
// Обработчик параметров
|
||||
execute (params, session, groups) {
|
||||
return {
|
||||
text : params.text,
|
||||
result : this.MainObject.call('sum', {
|
||||
a : 15,
|
||||
b : 17,
|
||||
session_id : params.session_id
|
||||
})
|
||||
};
|
||||
throw new njsbacker.ApiError('EXAMPLE_ERROR', new Object());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class SumMethod extends njsbacker.Method {
|
||||
execute (params, session, groups) {
|
||||
return params.a + params.b;
|
||||
}
|
||||
}
|
||||
|
||||
class FileMethod extends njsbacker.Method {
|
||||
execute (params, session, groups) {
|
||||
return JSON.stringify(params.file);
|
||||
}
|
||||
}
|
||||
|
||||
// Создаём экземпляры классов
|
||||
var eamohi = new ExampleAnyMethodsOfHandlingInformation('handler', '/handler', {
|
||||
queryName : {
|
||||
required : true,
|
||||
type : njsbacker.types.string,
|
||||
import_key : 'name',
|
||||
allow_params : ['query']
|
||||
},
|
||||
jsonName : {
|
||||
required : true,
|
||||
type : njsbacker.types.string,
|
||||
import_key : 'name',
|
||||
allow_methods : ['post'],
|
||||
allow_params : ['json']
|
||||
}
|
||||
});
|
||||
|
||||
var fileMethod = new FileMethod('file', '/file', {
|
||||
file : {
|
||||
required : true,
|
||||
type : njsbacker.types.file()
|
||||
}
|
||||
});
|
||||
|
||||
var sumMethod = new SumMethod('sum', '/sum', {
|
||||
a : {
|
||||
required : true,
|
||||
type : njsbacker.types.integer,
|
||||
conversion : false,
|
||||
// allow_methods : ['post'],
|
||||
},
|
||||
b : {
|
||||
required : true,
|
||||
type : njsbacker.types.integer,
|
||||
conversion : false,
|
||||
// allow_methods : ['post'],
|
||||
}
|
||||
});
|
||||
|
||||
var exampleMethod = new ExampleMethod('example', '/example', {
|
||||
text : {
|
||||
required : true,
|
||||
type : njsbacker.types.string,
|
||||
conversion : false,
|
||||
values : ['123', 'test'],
|
||||
min_length : 1,
|
||||
max_length : 255,
|
||||
// allow_methods : ['post'],
|
||||
// allow_params : ['json'],
|
||||
}
|
||||
});
|
||||
// Привяжем метод к группе
|
||||
exampleMethod.group(new ExampleMethodGroup({
|
||||
ses : {
|
||||
type : njsbacker.types.string
|
||||
}
|
||||
}));
|
||||
sumMethod.group(new ExampleMethodGroup({
|
||||
ses : {
|
||||
type : njsbacker.types.string
|
||||
}
|
||||
}));
|
||||
// Привяжем метод к основному проекту
|
||||
server.method(exampleMethod);
|
||||
server.method(sumMethod);
|
||||
server.method(fileMethod);
|
||||
server.method(eamohi);
|
||||
|
||||
// Запускаем сервер
|
||||
server.server('/api/v1').listen(8080, async (err) => {
|
||||
if (err) { throw err; }
|
||||
else {
|
||||
console.log('SERVER RUNNED');
|
||||
}
|
||||
const njsbacker = require('./index');
|
||||
|
||||
// Создаём класс бэкенда, наследующий класс njsbacker.Main
|
||||
class Main extends njsbacker.Main {
|
||||
/* errorHandler (error) { return {
|
||||
mainbody : JSON.stringify({ error : {}, error }),
|
||||
headers : {
|
||||
errored : 1
|
||||
},
|
||||
cookies : {
|
||||
avava : 1
|
||||
},
|
||||
// redirect_uri: ''; // if want redirect to another url
|
||||
code: 400
|
||||
}};*/
|
||||
|
||||
session (params, sessionData) {
|
||||
sessionData._setValue('example', 1); // Задать значение
|
||||
console.log(sessionData.example); // Получить значение из сессии
|
||||
sessionData._remove('example'); // Убрать значение
|
||||
return 1; // Успешно
|
||||
throw 'Example error' // Пример ошибки
|
||||
}
|
||||
|
||||
responseHandler (response) { return ({
|
||||
mainbody : { response },
|
||||
headers : {
|
||||
errored: 0
|
||||
},
|
||||
cookies : {},
|
||||
// redirect_uri: ''; // if want redirect to another url
|
||||
code: 200
|
||||
}) };
|
||||
|
||||
/* paramsError (required, additional) { return({ required, additional }) }; */
|
||||
}
|
||||
// Создаём экземпляр класса Main
|
||||
var server = new Main(
|
||||
false // Отобразить в заголовках информацию о текущем фреймворке
|
||||
);
|
||||
server.setSessionParams( // Зададим необходимые параметры для сессии
|
||||
{
|
||||
session_id : {
|
||||
required : false,
|
||||
type : njsbacker.types.integer
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// Создаём класс группы методов
|
||||
class ExampleMethodGroup extends njsbacker.Group {
|
||||
handler (params, session) { // Путевая обработка
|
||||
session._setValue('example', 1); // Задать значение
|
||||
console.log(session.example); // Получить значение из сессии
|
||||
session._remove('example'); // Убрать значение
|
||||
return 1; // Успешно
|
||||
throw 'Example error' // Пример ошибки
|
||||
}
|
||||
}
|
||||
// Создаём классы методов
|
||||
class ExampleAnyMethodsOfHandlingInformation extends njsbacker.Method {
|
||||
execute (params, session, groups) {
|
||||
return {
|
||||
json_data : params.json_name,
|
||||
query_data : params.query_name,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class ExampleMethod extends njsbacker.Method {
|
||||
/*
|
||||
var result = this.MainObject.call(method : string, params : object) // Вызов подключённого метода
|
||||
*/
|
||||
|
||||
// Обработчик параметров
|
||||
execute (params, session, groups) {
|
||||
return {
|
||||
text : params.text,
|
||||
result : this.MainObject.call('sum', {
|
||||
a : 15,
|
||||
b : 17,
|
||||
session_id : params.session_id
|
||||
})
|
||||
};
|
||||
throw new njsbacker.ApiError('EXAMPLE_ERROR', new Object());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class SumMethod extends njsbacker.Method {
|
||||
execute (params, session, groups) {
|
||||
return params.a + params.b;
|
||||
}
|
||||
}
|
||||
|
||||
class FileMethod extends njsbacker.Method {
|
||||
execute (params, session, groups) {
|
||||
return JSON.stringify(params.file);
|
||||
}
|
||||
}
|
||||
|
||||
// Создаём экземпляры классов
|
||||
var eamohi = new ExampleAnyMethodsOfHandlingInformation('handler', '/handler', {
|
||||
queryName : {
|
||||
required : true,
|
||||
type : njsbacker.types.string,
|
||||
import_key : 'name',
|
||||
allow_params : ['query']
|
||||
},
|
||||
jsonName : {
|
||||
required : true,
|
||||
type : njsbacker.types.string,
|
||||
import_key : 'name',
|
||||
allow_methods : ['post'],
|
||||
allow_params : ['json']
|
||||
}
|
||||
});
|
||||
|
||||
var fileMethod = new FileMethod('file', '/file', {
|
||||
file : {
|
||||
required : true,
|
||||
type : njsbacker.types.file()
|
||||
}
|
||||
});
|
||||
|
||||
var sumMethod = new SumMethod('sum', '/sum', {
|
||||
a : {
|
||||
required : true,
|
||||
type : njsbacker.types.integer,
|
||||
conversion : false,
|
||||
// allow_methods : ['post'],
|
||||
},
|
||||
b : {
|
||||
required : true,
|
||||
type : njsbacker.types.integer,
|
||||
conversion : false,
|
||||
// allow_methods : ['post'],
|
||||
}
|
||||
});
|
||||
|
||||
var exampleMethod = new ExampleMethod('example', '/example', {
|
||||
text : {
|
||||
required : true,
|
||||
type : njsbacker.types.string,
|
||||
conversion : false,
|
||||
values : ['123', 'test'],
|
||||
min_length : 1,
|
||||
max_length : 255,
|
||||
// allow_methods : ['post'],
|
||||
// allow_params : ['json'],
|
||||
}
|
||||
});
|
||||
// Привяжем метод к группе
|
||||
exampleMethod.group(new ExampleMethodGroup({
|
||||
ses : {
|
||||
type : njsbacker.types.string
|
||||
}
|
||||
}));
|
||||
sumMethod.group(new ExampleMethodGroup({
|
||||
ses : {
|
||||
type : njsbacker.types.string
|
||||
}
|
||||
}));
|
||||
// Привяжем метод к основному проекту
|
||||
server.method(exampleMethod);
|
||||
server.method(sumMethod);
|
||||
server.method(fileMethod);
|
||||
server.method(eamohi);
|
||||
|
||||
// Запускаем сервер
|
||||
server.server('/api/v1').listen(8080, async (err) => {
|
||||
if (err) { throw err; }
|
||||
else {
|
||||
console.log('SERVER RUNNED');
|
||||
}
|
||||
});
|
@ -1,10 +1,10 @@
|
||||
class Group {
|
||||
constructor (sessionData) {
|
||||
this.sessionData = sessionData
|
||||
}
|
||||
|
||||
handler (params, session) { return 1; }
|
||||
}
|
||||
|
||||
|
||||
class Group {
|
||||
constructor (sessionData) {
|
||||
this.sessionData = sessionData
|
||||
}
|
||||
|
||||
handler (params, session) { return 1; }
|
||||
}
|
||||
|
||||
|
||||
module.exports = Group;
|
@ -1,182 +1,182 @@
|
||||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
const version = '1.0.0';
|
||||
|
||||
|
||||
class ApiError extends Error {
|
||||
constructor (opt, data) {
|
||||
super(opt);
|
||||
this.name = 'API Error';
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const debug = (text) => {}
|
||||
|
||||
|
||||
class Main {
|
||||
constructor (sendHeaders = true) {
|
||||
this.sendHeaders = sendHeaders;
|
||||
this.methods = new Object();
|
||||
this.sessionData = new Object();
|
||||
|
||||
this.fileUploadConfig = {
|
||||
limits: { fileSize : 1024 * 1024 * 256 }, // 256 Mb maximum
|
||||
abortOnLimit: true,
|
||||
responseOnLimit: '{"error":"FILE_SIZE_LIMIT_HAS_BEEN_REACHED"}'
|
||||
};
|
||||
}
|
||||
|
||||
paramsError (required, additional) {
|
||||
return new ApiError('UNSYNTAX_OR_MISSED_REQUIRED_PARAMS', { required, additional });
|
||||
}
|
||||
|
||||
errorHadler (error) {
|
||||
let errorData;
|
||||
if (error.name == "API Error") {
|
||||
errorData = {
|
||||
code : error.message,
|
||||
details : error.data
|
||||
};
|
||||
}
|
||||
else {
|
||||
errorData = {
|
||||
name : error.name,
|
||||
stack : error.stack
|
||||
};
|
||||
}
|
||||
return {
|
||||
mainbody : { error : errorData },
|
||||
headers : {},
|
||||
cookies : {},
|
||||
// redirect_uri: ''; // if want redirect to another url
|
||||
code: 400
|
||||
}
|
||||
}
|
||||
|
||||
setSessionParams (sessionParams) {
|
||||
this.sessionData = sessionParams;
|
||||
}
|
||||
|
||||
session (params, sessionData) { return 1; }
|
||||
|
||||
responseHandler (result) { return ({
|
||||
mainbody : { result },
|
||||
headers : {},
|
||||
cookies : {},
|
||||
// redirect_uri: ''; // if want redirect to another url
|
||||
code: 200
|
||||
}) };
|
||||
|
||||
method (methodObj) {
|
||||
methodObj._pinMain(this);
|
||||
this.methods[methodObj.name] = methodObj;
|
||||
}
|
||||
|
||||
call (method, params) {
|
||||
return this.methods[method].pre_execute(params);
|
||||
}
|
||||
|
||||
fileSetting (fileUploadConfig) {
|
||||
this.fileUploadConfig = fileUploadConfig;
|
||||
}
|
||||
|
||||
router (returnMiddlewareFunction = false, middlewareFunction = (req, res, next) => next()) {
|
||||
let router = express.Router();
|
||||
router.use(require('cookie-parser')());
|
||||
// parse various different custom JSON types as JSON
|
||||
router.use(bodyParser.json({ type: 'application/*+json' }))
|
||||
router.use(bodyParser.json({ type: 'application/json' }))
|
||||
// parse some custom thing into a Buffer
|
||||
router.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))
|
||||
// parse an HTML body into a string
|
||||
router.use(bodyParser.text({ type: 'text/html' }));
|
||||
// parse application/x-www-form-urlencoded
|
||||
router.use(bodyParser.urlencoded({ type: 'application/x-www-form-urlencoded', extended: false }))
|
||||
|
||||
// parse files
|
||||
router.use(require('express-fileupload')(this.fileUploadConfig));
|
||||
|
||||
for (let name in this.methods) {
|
||||
debug(`CONNECT METHOD : ${name}`);
|
||||
for (let methodId in this.methods[name].allowedMethods) {
|
||||
debug(` >> CONNECT METHOD : ${name} [${this.methods[name].allowedMethods[methodId]}] to ${this.methods[name].path}`);
|
||||
router[this.methods[name].allowedMethods[methodId]](this.methods[name].path, async (req, res) => {
|
||||
if (this.sendHeaders) {
|
||||
res.set('njsbacker-version', version);
|
||||
}
|
||||
try {
|
||||
let contentType = req.get('Content-Type');
|
||||
if (contentType != undefined) {
|
||||
contentType = contentType.toLowerCase();
|
||||
}
|
||||
else {
|
||||
contentType = 'unknown';
|
||||
}
|
||||
|
||||
let result = this.methods[name].executeIntoExpressRouter(
|
||||
this.methods[name].allowedMethods[methodId],
|
||||
req.headers,
|
||||
(contentType.indexOf('json') != -1) ? req.body : new Object(),
|
||||
req.params,
|
||||
req.query,
|
||||
(contentType.indexOf('json') == -1) ? req.body : new Object(),
|
||||
req.files != undefined ? req.files : new Object(),
|
||||
req.cookies
|
||||
);
|
||||
let handledDataResponse = this.responseHandler(
|
||||
result
|
||||
);
|
||||
if (!handledDataResponse.redirect_uri) {
|
||||
for (let header in handledDataResponse.headers) {
|
||||
res.set(header, handledDataResponse.headers[header].toString());
|
||||
}
|
||||
for (let cookie in handledDataResponse.cookies) {
|
||||
res.cookie(cookie, handledDataResponse.cookies[cookie].toString());
|
||||
}
|
||||
res.status(handledDataResponse.code).send(handledDataResponse.mainbody);
|
||||
}
|
||||
else {
|
||||
res.status(handledDataResponse.code).redirect(handledDataResponse.redirect_uri);
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
debug('ERROR RISED:');
|
||||
debug(err);
|
||||
let handledDataError = this.errorHadler(err);
|
||||
if (!handledDataError.redirect_uri) {
|
||||
for (let header in handledDataError.headers) {
|
||||
res.set(header, handledDataError.headers[header].toString());
|
||||
}
|
||||
for (let cookie in handledDataError.cookies) {
|
||||
res.cookie(cookie, handledDataError.cookies[cookie].toString());
|
||||
}
|
||||
res.status(handledDataError.code).send(handledDataError.mainbody);
|
||||
}
|
||||
else {
|
||||
res.status(handledDataError.code).redirect(handledDataError.redirect_uri);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (returnMiddlewareFunction) {
|
||||
return middlewareFunction, router;
|
||||
}
|
||||
else {
|
||||
return router;
|
||||
}
|
||||
}
|
||||
|
||||
server (mountPath = '/') {
|
||||
let app = express();
|
||||
|
||||
app.use(mountPath, this.router(true));
|
||||
return app;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
const version = '1.0.0';
|
||||
|
||||
|
||||
class ApiError extends Error {
|
||||
constructor (opt, data) {
|
||||
super(opt);
|
||||
this.name = 'API Error';
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const debug = (text) => {}
|
||||
|
||||
|
||||
class Main {
|
||||
constructor (sendHeaders = true) {
|
||||
this.sendHeaders = sendHeaders;
|
||||
this.methods = new Object();
|
||||
this.sessionData = new Object();
|
||||
|
||||
this.fileUploadConfig = {
|
||||
limits: { fileSize : 1024 * 1024 * 256 }, // 256 Mb maximum
|
||||
abortOnLimit: true,
|
||||
responseOnLimit: '{"error":"FILE_SIZE_LIMIT_HAS_BEEN_REACHED"}'
|
||||
};
|
||||
}
|
||||
|
||||
paramsError (required, additional) {
|
||||
return new ApiError('UNSYNTAX_OR_MISSED_REQUIRED_PARAMS', { required, additional });
|
||||
}
|
||||
|
||||
errorHadler (error) {
|
||||
let errorData;
|
||||
if (error.name == "API Error") {
|
||||
errorData = {
|
||||
code : error.message,
|
||||
details : error.data
|
||||
};
|
||||
}
|
||||
else {
|
||||
errorData = {
|
||||
name : error.name,
|
||||
stack : error.stack
|
||||
};
|
||||
}
|
||||
return {
|
||||
mainbody : { error : errorData },
|
||||
headers : {},
|
||||
cookies : {},
|
||||
// redirect_uri: ''; // if want redirect to another url
|
||||
code: 400
|
||||
}
|
||||
}
|
||||
|
||||
setSessionParams (sessionParams) {
|
||||
this.sessionData = sessionParams;
|
||||
}
|
||||
|
||||
session (params, sessionData) { return 1; }
|
||||
|
||||
responseHandler (result) { return ({
|
||||
mainbody : { result },
|
||||
headers : {},
|
||||
cookies : {},
|
||||
// redirect_uri: ''; // if want redirect to another url
|
||||
code: 200
|
||||
}) };
|
||||
|
||||
method (methodObj) {
|
||||
methodObj._pinMain(this);
|
||||
this.methods[methodObj.name] = methodObj;
|
||||
}
|
||||
|
||||
call (method, params) {
|
||||
return this.methods[method].pre_execute(params);
|
||||
}
|
||||
|
||||
fileSetting (fileUploadConfig) {
|
||||
this.fileUploadConfig = fileUploadConfig;
|
||||
}
|
||||
|
||||
router (returnMiddlewareFunction = false, middlewareFunction = (req, res, next) => next()) {
|
||||
let router = express.Router();
|
||||
router.use(require('cookie-parser')());
|
||||
// parse various different custom JSON types as JSON
|
||||
router.use(bodyParser.json({ type: 'application/*+json' }))
|
||||
router.use(bodyParser.json({ type: 'application/json' }))
|
||||
// parse some custom thing into a Buffer
|
||||
router.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))
|
||||
// parse an HTML body into a string
|
||||
router.use(bodyParser.text({ type: 'text/html' }));
|
||||
// parse application/x-www-form-urlencoded
|
||||
router.use(bodyParser.urlencoded({ type: 'application/x-www-form-urlencoded', extended: false }))
|
||||
|
||||
// parse files
|
||||
router.use(require('express-fileupload')(this.fileUploadConfig));
|
||||
|
||||
for (let name in this.methods) {
|
||||
debug(`CONNECT METHOD : ${name}`);
|
||||
for (let methodId in this.methods[name].allowedMethods) {
|
||||
debug(` >> CONNECT METHOD : ${name} [${this.methods[name].allowedMethods[methodId]}] to ${this.methods[name].path}`);
|
||||
router[this.methods[name].allowedMethods[methodId]](this.methods[name].path, async (req, res) => {
|
||||
if (this.sendHeaders) {
|
||||
res.set('njsbacker-version', version);
|
||||
}
|
||||
try {
|
||||
let contentType = req.get('Content-Type');
|
||||
if (contentType != undefined) {
|
||||
contentType = contentType.toLowerCase();
|
||||
}
|
||||
else {
|
||||
contentType = 'unknown';
|
||||
}
|
||||
|
||||
let result = await this.methods[name].executeIntoExpressRouter(
|
||||
this.methods[name].allowedMethods[methodId],
|
||||
req.headers,
|
||||
(contentType.indexOf('json') != -1) ? req.body : new Object(),
|
||||
req.params,
|
||||
req.query,
|
||||
(contentType.indexOf('json') == -1) ? req.body : new Object(),
|
||||
req.files != undefined ? req.files : new Object(),
|
||||
req.cookies
|
||||
);
|
||||
let handledDataResponse = this.responseHandler(
|
||||
result
|
||||
);
|
||||
if (!handledDataResponse.redirect_uri) {
|
||||
for (let header in handledDataResponse.headers) {
|
||||
res.set(header, handledDataResponse.headers[header].toString());
|
||||
}
|
||||
for (let cookie in handledDataResponse.cookies) {
|
||||
res.cookie(cookie, handledDataResponse.cookies[cookie].toString());
|
||||
}
|
||||
res.status(handledDataResponse.code).send(handledDataResponse.mainbody);
|
||||
}
|
||||
else {
|
||||
res.status(handledDataResponse.code).redirect(handledDataResponse.redirect_uri);
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
debug('ERROR RISED:');
|
||||
debug(err);
|
||||
let handledDataError = this.errorHadler(err);
|
||||
if (!handledDataError.redirect_uri) {
|
||||
for (let header in handledDataError.headers) {
|
||||
res.set(header, handledDataError.headers[header].toString());
|
||||
}
|
||||
for (let cookie in handledDataError.cookies) {
|
||||
res.cookie(cookie, handledDataError.cookies[cookie].toString());
|
||||
}
|
||||
res.status(handledDataError.code).send(handledDataError.mainbody);
|
||||
}
|
||||
else {
|
||||
res.status(handledDataError.code).redirect(handledDataError.redirect_uri);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (returnMiddlewareFunction) {
|
||||
return middlewareFunction, router;
|
||||
}
|
||||
else {
|
||||
return router;
|
||||
}
|
||||
}
|
||||
|
||||
server (mountPath = '/') {
|
||||
let app = express();
|
||||
|
||||
app.use(mountPath, this.router(true));
|
||||
return app;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = { Main, ApiError };
|
@ -1,268 +1,268 @@
|
||||
const typesApi = require('./types');
|
||||
const Session = require('./Session');
|
||||
|
||||
|
||||
var errHandlers = new Object();
|
||||
|
||||
|
||||
const formatMessage = (message, errorType, scheme, value, param) => {
|
||||
value = String(value);
|
||||
switch (errorType) {
|
||||
case 'typeError' :
|
||||
return message.split('{param}').join(param).split('{long_type}').join(scheme.type.long_name).split('{short_type}').join(scheme.type.short_name);
|
||||
case 'valuesError' :
|
||||
return message.split('{param}').join(param).split('{value}').join(value).split('{values}').join(scheme.values.join(', '));
|
||||
case 'minLengthError' :
|
||||
return message.split('{value}').join(scheme.min_length);
|
||||
case 'maxLengthError' :
|
||||
return message.split('{value}').join(scheme.max_length);
|
||||
case 'httpMethodError' :
|
||||
return message.split('{method}').join(value).split('{methods}').join(scheme.allow_methods.join(', '));
|
||||
default :
|
||||
return errHandlers[errorType](message, errorType, scheme, value, param);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const compileParams = (params) => {
|
||||
let compiled = new Object();
|
||||
for (let objKey in params) {
|
||||
compiled = Object.assign(compiled, params[objKey]);
|
||||
}
|
||||
return compiled;
|
||||
}
|
||||
|
||||
|
||||
class Method {
|
||||
constructor (name, path, params) {
|
||||
this.name = name;
|
||||
this.path = path;
|
||||
this.paramsCompiles = params;
|
||||
this.groupsConnected = new Array();
|
||||
this.allowedMethods = new Array();
|
||||
// Errors
|
||||
this.error = new Object();
|
||||
this.error.typeError = 'param {param} must be only {long_type} ({short_type})';
|
||||
this.error.valuesError = 'value "{value}" not finded into {values}';
|
||||
this.error.minLengthError = 'value must be more or equal {value}';
|
||||
this.error.maxLengthError = 'value must be less or equal {value}';
|
||||
this.error.httpMethodError = 'Method {method} does not supported. Methods supports: {methods}';
|
||||
|
||||
this.isDynamic = false;
|
||||
// Setting allow methods
|
||||
let allowedMethods;
|
||||
|
||||
for (let param in params) {
|
||||
if (!params[param].allow_methods) allowedMethods = ['get', 'post', 'put', 'delete'];
|
||||
else {
|
||||
allowedMethods = params[param].allow_methods;
|
||||
}
|
||||
for (let allowMethod in allowedMethods) {
|
||||
if (this.allowedMethods.indexOf(allowedMethods[allowMethod]) == -1) {
|
||||
this.allowedMethods.push(allowedMethods[allowMethod]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setError (errorCode, message, handler=null) {
|
||||
this.error[errorCode] = message;
|
||||
if (!!handler) {
|
||||
errHandlers[errorCode] = handler;
|
||||
}
|
||||
}
|
||||
|
||||
useDynamicType (condition) {
|
||||
this.isDynamic = !!condition;
|
||||
}
|
||||
|
||||
_pinMain (mainObject) {
|
||||
this.MainObject = mainObject;
|
||||
}
|
||||
|
||||
executeIntoExpressRouter (
|
||||
currentMethod,
|
||||
headers,
|
||||
json,
|
||||
params,
|
||||
query,
|
||||
body,
|
||||
files,
|
||||
cookies
|
||||
) {
|
||||
let paramsEndless = new Object();
|
||||
let paramScheme;
|
||||
let required = { missed : [], unsyntax : [] };
|
||||
let additional = { missed : [], unsyntax : [] };
|
||||
let checkKeys;
|
||||
let paramsCompiles = Object.assign({}, this.paramsCompiles, this.MainObject.sessionData);
|
||||
for (let groupId in this.groupsConnected) {
|
||||
paramsCompiles = Object.assign({}, paramsCompiles, this.groupsConnected[groupId].sessionData);
|
||||
}
|
||||
|
||||
for (let param in paramsCompiles) {
|
||||
checkKeys = new Array();
|
||||
paramScheme = {
|
||||
required : false,
|
||||
import_key : param,
|
||||
save_key : param,
|
||||
type : this.isDynamic ? typesApi.dynamic : typesApi.unknown,
|
||||
allow_methods : ['get', 'post', 'put', 'delete'],
|
||||
conversion : false,
|
||||
allow_params : ['headers', 'json', 'params', 'query', 'body', 'files', 'cookies']
|
||||
};
|
||||
// Configure paramScheme
|
||||
for (let key in paramsCompiles[param]) { paramScheme[key] = paramsCompiles[param][key]; }
|
||||
// check missible
|
||||
if (headers[paramScheme.import_key] != undefined & paramScheme.allow_params.indexOf('headers') != -1) { checkKeys.push('headers'); }
|
||||
if (json[paramScheme.import_key] != undefined & paramScheme.allow_params.indexOf('json') != -1) { checkKeys.push('json'); }
|
||||
if (query[paramScheme.import_key] != undefined & paramScheme.allow_params.indexOf('query') != -1) { checkKeys.push('query'); }
|
||||
if (body[paramScheme.import_key] != undefined & paramScheme.allow_params.indexOf('body') != -1) { checkKeys.push('body'); }
|
||||
if (files[paramScheme.import_key] != undefined & paramScheme.allow_params.indexOf('files') != -1) { checkKeys.push('files'); }
|
||||
if (cookies[paramScheme.import_key] != undefined & paramScheme.allow_params.indexOf('cookies') != -1) { checkKeys.push('cookies'); }
|
||||
if (params[paramScheme.import_key] != undefined & paramScheme.allow_params.indexOf('params') != -1) { checkKeys.push('params'); }
|
||||
|
||||
if (checkKeys.length == 0) {
|
||||
if (paramScheme.required) { required.missed.push(paramScheme.import_key); }
|
||||
else { additional.missed.push(paramScheme.import_key); }
|
||||
}
|
||||
else if (paramScheme.allow_methods.indexOf(currentMethod) == -1) {
|
||||
if (paramScheme.required) { required.unsyntax.push({param : paramScheme.import_key, description : formatMessage(this.error.httpMethodError, 'httpMethodError', paramScheme, currentMethod, paramScheme.import_key) }); }
|
||||
else { additional.unsyntax.push({param : paramScheme.import_key, description : formatMessage(this.error.httpMethodError, 'httpMethodError', paramScheme, currentMethod, paramScheme.import_key)}); }
|
||||
}
|
||||
else {
|
||||
checkKeys = checkKeys.sort((a, b) => Number(b == 'query' || b == 'cookies') - Number(a == 'query' || a == 'cookies'));
|
||||
let isSyntax;
|
||||
let convertedValue;
|
||||
let selectedSyntaxError = 'typeError';
|
||||
for (let key in checkKeys) {
|
||||
switch (checkKeys[key]) {
|
||||
case 'query' :
|
||||
[isSyntax, convertedValue] = paramScheme.type.syntax(query[paramScheme.import_key], true);
|
||||
break;
|
||||
case 'cookies' :
|
||||
[isSyntax, convertedValue] = paramScheme.type.syntax(cookies[paramScheme.import_key], true);
|
||||
break;
|
||||
case 'headers' :
|
||||
[isSyntax, convertedValue] = paramScheme.type.syntax(headers[paramScheme.import_key], true);
|
||||
break;
|
||||
case 'body' :
|
||||
[isSyntax, convertedValue] = paramScheme.type.syntax(body[paramScheme.import_key], true);
|
||||
break;
|
||||
case 'json' :
|
||||
[isSyntax, convertedValue] = paramScheme.type.syntax(json[paramScheme.import_key], paramScheme.conversion);
|
||||
break;
|
||||
case 'params' :
|
||||
[isSyntax, convertedValue] = paramScheme.type.syntax(params[paramScheme.import_key], paramScheme.conversion);
|
||||
break;
|
||||
case 'files' :
|
||||
[isSyntax, convertedValue] = paramScheme.type.syntax(files[paramScheme.import_key], paramScheme.conversion);
|
||||
break;
|
||||
}
|
||||
if (isSyntax) {
|
||||
[isSyntax, selectedSyntaxError] = paramScheme.type.checkSchema(convertedValue, paramScheme);
|
||||
if (isSyntax) break;
|
||||
}
|
||||
}
|
||||
if (isSyntax) {
|
||||
paramsEndless[paramScheme.save_key] = convertedValue;
|
||||
}
|
||||
else {
|
||||
if (paramScheme.required) { required.unsyntax.push({param : paramScheme.import_key, description : formatMessage(this.error[selectedSyntaxError], selectedSyntaxError, paramScheme, convertedValue, paramScheme.import_key) }); }
|
||||
else { additional.unsyntax.push({param : paramScheme.import_key, description : formatMessage(this.error[selectedSyntaxError], selectedSyntaxError, paramScheme, convertedValue, paramScheme.import_key)}); }
|
||||
}
|
||||
}
|
||||
}
|
||||
if (required.missed.length > 0 || required.unsyntax.length > 0 || additional.unsyntax.length > 0) {
|
||||
throw this.MainObject.paramsError(required, additional);
|
||||
}
|
||||
else {
|
||||
return this.pre_execute(paramsEndless, false);
|
||||
}
|
||||
}
|
||||
|
||||
pre_execute (params, needsChecking = true) {
|
||||
if (needsChecking) {
|
||||
let required = { missed : [], unsyntax : [] };
|
||||
let additional = { missed : [], unsyntax : [] };
|
||||
let isSyntax;
|
||||
let value;
|
||||
let paramScheme;
|
||||
let paramsCompiles = Object.assign(this.paramsCompiles, this.MainObject.sessionData);
|
||||
|
||||
for (let param in paramsCompiles) {
|
||||
paramScheme = {
|
||||
required : false,
|
||||
type : this.isDynamic ? typesApi.dynamic : typesApi.unknown,
|
||||
import_key : param,
|
||||
save_key : param,
|
||||
allow_methods : ['get', 'post', 'put', 'delete'],
|
||||
conversion : false,
|
||||
allow_params : ['headers', 'json', 'params', 'query', 'body', 'files', 'cookies']
|
||||
};
|
||||
for (let key in paramsCompiles[param]) { paramScheme[key] = paramsCompiles[param][key]; }
|
||||
|
||||
if (params[paramScheme.import_key] === undefined) {
|
||||
if (paramScheme.required) {
|
||||
required.missed.push(paramScheme.import_key);
|
||||
}
|
||||
else {
|
||||
additional.missed.push(paramScheme.import_key);
|
||||
}
|
||||
}
|
||||
else {
|
||||
let selectedSyntaxError = 'typeError';
|
||||
[isSyntax, value] = paramScheme.type.syntax(params[paramScheme.import_key], paramScheme.conversion);
|
||||
if (!isSyntax) {
|
||||
if (paramScheme.required) {
|
||||
required.unsyntax.push({param : paramScheme.import_key, description : this.error.typeError.split('{param}').join(paramScheme.import_key).split('{long_type}').join(paramScheme.type.long_name).split('{short_type}').join(paramScheme.type.short_name)});
|
||||
}
|
||||
else {
|
||||
additional.unsyntax.push({param : paramScheme.import_key, description : this.error.typeError.split('{param}').join(paramScheme.import_key).split('{long_type}').join(paramScheme.type.long_name).split('{short_type}').join(paramScheme.type.short_name)});
|
||||
}
|
||||
}
|
||||
else {
|
||||
[isSyntax, selectedSyntaxError] = paramScheme.type.checkSchema(value, paramScheme);
|
||||
if (!isSyntax) {
|
||||
if (paramScheme.required) {
|
||||
required.unsyntax.push({param : paramScheme.import_key, description : formatMessage(this.error[selectedSyntaxError], selectedSyntaxError, paramScheme, value, paramScheme.import_key)});
|
||||
}
|
||||
else {
|
||||
additional.unsyntax.push({param : paramScheme.import_key, description : formatMessage(this.error[selectedSyntaxError], selectedSyntaxError, paramScheme, value, paramScheme.import_key)});
|
||||
}
|
||||
}
|
||||
else {
|
||||
params[paramScheme.save_key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (required.missed.length > 0 || required.unsyntax.length > 0 || additional.unsyntax.length > 0) {
|
||||
throw this.MainObject.paramsError(required, additional);
|
||||
}
|
||||
}
|
||||
// Исполнение сессии
|
||||
let sessionData = new Session();
|
||||
let groupData = new Session();
|
||||
this.MainObject.session(params, sessionData);
|
||||
sessionData = sessionData._getValues();
|
||||
|
||||
for (let groupId in this.groupsConnected) {
|
||||
this.groupsConnected[groupId].handler(params, groupData);
|
||||
}
|
||||
// for (let key in sessionData) { params[key] = sessionData[key]; }
|
||||
// Исполнение группы
|
||||
|
||||
return this.execute(params, sessionData, groupData._getValues());
|
||||
}
|
||||
|
||||
group (group) {
|
||||
this.groupsConnected.push(group);
|
||||
}
|
||||
|
||||
execute (params, sessionData, groupData) {}
|
||||
}
|
||||
|
||||
|
||||
const typesApi = require('./types');
|
||||
const Session = require('./Session');
|
||||
|
||||
|
||||
var errHandlers = new Object();
|
||||
|
||||
|
||||
const formatMessage = (message, errorType, scheme, value, param) => {
|
||||
value = String(value);
|
||||
switch (errorType) {
|
||||
case 'typeError' :
|
||||
return message.split('{param}').join(param).split('{long_type}').join(scheme.type.long_name).split('{short_type}').join(scheme.type.short_name);
|
||||
case 'valuesError' :
|
||||
return message.split('{param}').join(param).split('{value}').join(value).split('{values}').join(scheme.values.join(', '));
|
||||
case 'minLengthError' :
|
||||
return message.split('{value}').join(scheme.min_length);
|
||||
case 'maxLengthError' :
|
||||
return message.split('{value}').join(scheme.max_length);
|
||||
case 'httpMethodError' :
|
||||
return message.split('{method}').join(value).split('{methods}').join(scheme.allow_methods.join(', '));
|
||||
default :
|
||||
return errHandlers[errorType](message, errorType, scheme, value, param);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const compileParams = (params) => {
|
||||
let compiled = new Object();
|
||||
for (let objKey in params) {
|
||||
compiled = Object.assign(compiled, params[objKey]);
|
||||
}
|
||||
return compiled;
|
||||
}
|
||||
|
||||
|
||||
class Method {
|
||||
constructor (name, path, params) {
|
||||
this.name = name;
|
||||
this.path = path;
|
||||
this.paramsCompiles = params;
|
||||
this.groupsConnected = new Array();
|
||||
this.allowedMethods = new Array();
|
||||
// Errors
|
||||
this.error = new Object();
|
||||
this.error.typeError = 'param {param} must be only {long_type} ({short_type})';
|
||||
this.error.valuesError = 'value "{value}" not finded into {values}';
|
||||
this.error.minLengthError = 'value must be more or equal {value}';
|
||||
this.error.maxLengthError = 'value must be less or equal {value}';
|
||||
this.error.httpMethodError = 'Method {method} does not supported. Methods supports: {methods}';
|
||||
|
||||
this.isDynamic = false;
|
||||
// Setting allow methods
|
||||
let allowedMethods;
|
||||
|
||||
for (let param in params) {
|
||||
if (!params[param].allow_methods) allowedMethods = ['get', 'post', 'put', 'delete'];
|
||||
else {
|
||||
allowedMethods = params[param].allow_methods;
|
||||
}
|
||||
for (let allowMethod in allowedMethods) {
|
||||
if (this.allowedMethods.indexOf(allowedMethods[allowMethod]) == -1) {
|
||||
this.allowedMethods.push(allowedMethods[allowMethod]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setError (errorCode, message, handler=null) {
|
||||
this.error[errorCode] = message;
|
||||
if (!!handler) {
|
||||
errHandlers[errorCode] = handler;
|
||||
}
|
||||
}
|
||||
|
||||
useDynamicType (condition) {
|
||||
this.isDynamic = !!condition;
|
||||
}
|
||||
|
||||
_pinMain (mainObject) {
|
||||
this.MainObject = mainObject;
|
||||
}
|
||||
|
||||
async executeIntoExpressRouter (
|
||||
currentMethod,
|
||||
headers,
|
||||
json,
|
||||
params,
|
||||
query,
|
||||
body,
|
||||
files,
|
||||
cookies
|
||||
) {
|
||||
let paramsEndless = new Object();
|
||||
let paramScheme;
|
||||
let required = { missed : [], unsyntax : [] };
|
||||
let additional = { missed : [], unsyntax : [] };
|
||||
let checkKeys;
|
||||
let paramsCompiles = Object.assign({}, this.paramsCompiles, this.MainObject.sessionData);
|
||||
for (let groupId in this.groupsConnected) {
|
||||
paramsCompiles = Object.assign({}, paramsCompiles, this.groupsConnected[groupId].sessionData);
|
||||
}
|
||||
|
||||
for (let param in paramsCompiles) {
|
||||
checkKeys = new Array();
|
||||
paramScheme = {
|
||||
required : false,
|
||||
import_key : param,
|
||||
save_key : param,
|
||||
type : this.isDynamic ? typesApi.dynamic : typesApi.unknown,
|
||||
allow_methods : ['get', 'post', 'put', 'delete'],
|
||||
conversion : false,
|
||||
allow_params : ['headers', 'json', 'params', 'query', 'body', 'files', 'cookies']
|
||||
};
|
||||
// Configure paramScheme
|
||||
for (let key in paramsCompiles[param]) { paramScheme[key] = paramsCompiles[param][key]; }
|
||||
// check missible
|
||||
if (headers[paramScheme.import_key] != undefined & paramScheme.allow_params.indexOf('headers') != -1) { checkKeys.push('headers'); }
|
||||
if (json[paramScheme.import_key] != undefined & paramScheme.allow_params.indexOf('json') != -1) { checkKeys.push('json'); }
|
||||
if (query[paramScheme.import_key] != undefined & paramScheme.allow_params.indexOf('query') != -1) { checkKeys.push('query'); }
|
||||
if (body[paramScheme.import_key] != undefined & paramScheme.allow_params.indexOf('body') != -1) { checkKeys.push('body'); }
|
||||
if (files[paramScheme.import_key] != undefined & paramScheme.allow_params.indexOf('files') != -1) { checkKeys.push('files'); }
|
||||
if (cookies[paramScheme.import_key] != undefined & paramScheme.allow_params.indexOf('cookies') != -1) { checkKeys.push('cookies'); }
|
||||
if (params[paramScheme.import_key] != undefined & paramScheme.allow_params.indexOf('params') != -1) { checkKeys.push('params'); }
|
||||
|
||||
if (checkKeys.length == 0) {
|
||||
if (paramScheme.required) { required.missed.push(paramScheme.import_key); }
|
||||
else { additional.missed.push(paramScheme.import_key); }
|
||||
}
|
||||
else if (paramScheme.allow_methods.indexOf(currentMethod) == -1) {
|
||||
if (paramScheme.required) { required.unsyntax.push({param : paramScheme.import_key, description : formatMessage(this.error.httpMethodError, 'httpMethodError', paramScheme, currentMethod, paramScheme.import_key) }); }
|
||||
else { additional.unsyntax.push({param : paramScheme.import_key, description : formatMessage(this.error.httpMethodError, 'httpMethodError', paramScheme, currentMethod, paramScheme.import_key)}); }
|
||||
}
|
||||
else {
|
||||
checkKeys = checkKeys.sort((a, b) => Number(b == 'query' || b == 'cookies') - Number(a == 'query' || a == 'cookies'));
|
||||
let isSyntax;
|
||||
let convertedValue;
|
||||
let selectedSyntaxError = 'typeError';
|
||||
for (let key in checkKeys) {
|
||||
switch (checkKeys[key]) {
|
||||
case 'query' :
|
||||
[isSyntax, convertedValue] = paramScheme.type.syntax(query[paramScheme.import_key], true);
|
||||
break;
|
||||
case 'cookies' :
|
||||
[isSyntax, convertedValue] = paramScheme.type.syntax(cookies[paramScheme.import_key], true);
|
||||
break;
|
||||
case 'headers' :
|
||||
[isSyntax, convertedValue] = paramScheme.type.syntax(headers[paramScheme.import_key], true);
|
||||
break;
|
||||
case 'body' :
|
||||
[isSyntax, convertedValue] = paramScheme.type.syntax(body[paramScheme.import_key], true);
|
||||
break;
|
||||
case 'json' :
|
||||
[isSyntax, convertedValue] = paramScheme.type.syntax(json[paramScheme.import_key], paramScheme.conversion);
|
||||
break;
|
||||
case 'params' :
|
||||
[isSyntax, convertedValue] = paramScheme.type.syntax(params[paramScheme.import_key], paramScheme.conversion);
|
||||
break;
|
||||
case 'files' :
|
||||
[isSyntax, convertedValue] = paramScheme.type.syntax(files[paramScheme.import_key], paramScheme.conversion);
|
||||
break;
|
||||
}
|
||||
if (isSyntax) {
|
||||
[isSyntax, selectedSyntaxError] = paramScheme.type.checkSchema(convertedValue, paramScheme);
|
||||
if (isSyntax) break;
|
||||
}
|
||||
}
|
||||
if (isSyntax) {
|
||||
paramsEndless[paramScheme.save_key] = convertedValue;
|
||||
}
|
||||
else {
|
||||
if (paramScheme.required) { required.unsyntax.push({param : paramScheme.import_key, description : formatMessage(this.error[selectedSyntaxError], selectedSyntaxError, paramScheme, convertedValue, paramScheme.import_key) }); }
|
||||
else { additional.unsyntax.push({param : paramScheme.import_key, description : formatMessage(this.error[selectedSyntaxError], selectedSyntaxError, paramScheme, convertedValue, paramScheme.import_key)}); }
|
||||
}
|
||||
}
|
||||
}
|
||||
if (required.missed.length > 0 || required.unsyntax.length > 0 || additional.unsyntax.length > 0) {
|
||||
throw this.MainObject.paramsError(required, additional);
|
||||
}
|
||||
else {
|
||||
return await this.pre_execute(paramsEndless, false);
|
||||
}
|
||||
}
|
||||
|
||||
async pre_execute (params, needsChecking = true) {
|
||||
if (needsChecking) {
|
||||
let required = { missed : [], unsyntax : [] };
|
||||
let additional = { missed : [], unsyntax : [] };
|
||||
let isSyntax;
|
||||
let value;
|
||||
let paramScheme;
|
||||
let paramsCompiles = Object.assign(this.paramsCompiles, this.MainObject.sessionData);
|
||||
|
||||
for (let param in paramsCompiles) {
|
||||
paramScheme = {
|
||||
required : false,
|
||||
type : this.isDynamic ? typesApi.dynamic : typesApi.unknown,
|
||||
import_key : param,
|
||||
save_key : param,
|
||||
allow_methods : ['get', 'post', 'put', 'delete'],
|
||||
conversion : false,
|
||||
allow_params : ['headers', 'json', 'params', 'query', 'body', 'files', 'cookies']
|
||||
};
|
||||
for (let key in paramsCompiles[param]) { paramScheme[key] = paramsCompiles[param][key]; }
|
||||
|
||||
if (params[paramScheme.import_key] === undefined) {
|
||||
if (paramScheme.required) {
|
||||
required.missed.push(paramScheme.import_key);
|
||||
}
|
||||
else {
|
||||
additional.missed.push(paramScheme.import_key);
|
||||
}
|
||||
}
|
||||
else {
|
||||
let selectedSyntaxError = 'typeError';
|
||||
[isSyntax, value] = paramScheme.type.syntax(params[paramScheme.import_key], paramScheme.conversion);
|
||||
if (!isSyntax) {
|
||||
if (paramScheme.required) {
|
||||
required.unsyntax.push({param : paramScheme.import_key, description : this.error.typeError.split('{param}').join(paramScheme.import_key).split('{long_type}').join(paramScheme.type.long_name).split('{short_type}').join(paramScheme.type.short_name)});
|
||||
}
|
||||
else {
|
||||
additional.unsyntax.push({param : paramScheme.import_key, description : this.error.typeError.split('{param}').join(paramScheme.import_key).split('{long_type}').join(paramScheme.type.long_name).split('{short_type}').join(paramScheme.type.short_name)});
|
||||
}
|
||||
}
|
||||
else {
|
||||
[isSyntax, selectedSyntaxError] = paramScheme.type.checkSchema(value, paramScheme);
|
||||
if (!isSyntax) {
|
||||
if (paramScheme.required) {
|
||||
required.unsyntax.push({param : paramScheme.import_key, description : formatMessage(this.error[selectedSyntaxError], selectedSyntaxError, paramScheme, value, paramScheme.import_key)});
|
||||
}
|
||||
else {
|
||||
additional.unsyntax.push({param : paramScheme.import_key, description : formatMessage(this.error[selectedSyntaxError], selectedSyntaxError, paramScheme, value, paramScheme.import_key)});
|
||||
}
|
||||
}
|
||||
else {
|
||||
params[paramScheme.save_key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (required.missed.length > 0 || required.unsyntax.length > 0 || additional.unsyntax.length > 0) {
|
||||
throw this.MainObject.paramsError(required, additional);
|
||||
}
|
||||
}
|
||||
// Исполнение сессии
|
||||
let sessionData = new Session();
|
||||
let groupData = new Session();
|
||||
this.MainObject.session(params, sessionData);
|
||||
sessionData = sessionData._getValues();
|
||||
|
||||
for (let groupId in this.groupsConnected) {
|
||||
this.groupsConnected[groupId].handler(params, groupData);
|
||||
}
|
||||
// for (let key in sessionData) { params[key] = sessionData[key]; }
|
||||
// Исполнение группы
|
||||
|
||||
return await this.execute(params, sessionData, groupData._getValues());
|
||||
}
|
||||
|
||||
group (group) {
|
||||
this.groupsConnected.push(group);
|
||||
}
|
||||
|
||||
execute (params, sessionData, groupData) {}
|
||||
}
|
||||
|
||||
|
||||
module.exports = Method;
|
@ -1,31 +1,31 @@
|
||||
class Session {
|
||||
constructor () {
|
||||
this._namesReserved = ['_getValues', '_namesReserved', '_setValue', '_remove'];
|
||||
}
|
||||
|
||||
_setValue (key, value) {
|
||||
if (this._namesReserved.indexOf(key) == -1) {
|
||||
this[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
_remove (key, value) {
|
||||
if (this._namesReserved.indexOf(key) == -1) {
|
||||
delete this[key];
|
||||
}
|
||||
}
|
||||
|
||||
_getValues () {
|
||||
let values = new Object();
|
||||
for (let value in this) {
|
||||
if (this._namesReserved.indexOf(value) == -1) {
|
||||
values[value] = this[value];
|
||||
}
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class Session {
|
||||
constructor () {
|
||||
this._namesReserved = ['_getValues', '_namesReserved', '_setValue', '_remove'];
|
||||
}
|
||||
|
||||
_setValue (key, value) {
|
||||
if (this._namesReserved.indexOf(key) == -1) {
|
||||
this[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
_remove (key, value) {
|
||||
if (this._namesReserved.indexOf(key) == -1) {
|
||||
delete this[key];
|
||||
}
|
||||
}
|
||||
|
||||
_getValues () {
|
||||
let values = new Object();
|
||||
for (let value in this) {
|
||||
if (this._namesReserved.indexOf(value) == -1) {
|
||||
values[value] = this[value];
|
||||
}
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = Session;
|
@ -1,217 +1,217 @@
|
||||
const types = {
|
||||
dynamic : {
|
||||
long_name : 'dynamic',
|
||||
short_name : 'dyn',
|
||||
checkSchema : (value, schema) => {
|
||||
if (schema.values != undefined) { // values
|
||||
if (schema.values.indexOf(value) == -1) {
|
||||
return [false, 'valuesError'];
|
||||
}
|
||||
}
|
||||
|
||||
return [true, 'ok'];
|
||||
},
|
||||
syntax : (value, needs_convert = true) => [true, value]
|
||||
},
|
||||
unknown : {
|
||||
long_name : 'unknown',
|
||||
short_name : 'unk',
|
||||
checkSchema : (value, schema) => { throw new Error('Undefined datatype'); },
|
||||
syntax : (value, needs_convert = true) => {
|
||||
throw new Error('Undefined datatype');
|
||||
}
|
||||
},
|
||||
float : {
|
||||
long_name : 'float',
|
||||
short_name : 'float',
|
||||
checkSchema : (value, schema) => {
|
||||
if (schema.values != undefined) { // values
|
||||
if (schema.values.indexOf(value) == -1) {
|
||||
return [false, 'valuesError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (schema.min_length != undefined) { // min_length
|
||||
if (value < schema.min_length) {
|
||||
return [false, 'minLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (schema.max_length != undefined) { // max_length
|
||||
if (value > schema.max_length) {
|
||||
return [false, 'maxLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
return [true, 'ok'];
|
||||
},
|
||||
syntax : (value, needs_convert = true) => {
|
||||
function isFloat (value) {
|
||||
if (String(parseFloat(value)) == 'NaN') return false;
|
||||
return String(parseFloat(value)) == String(Number(value));
|
||||
}
|
||||
|
||||
if (typeof(value) == 'number' & isFloat(value)) {
|
||||
return [true, value];
|
||||
}
|
||||
else if (needs_convert & isFloat(value)) {
|
||||
return [true, parseFloat(value)];
|
||||
}
|
||||
else {
|
||||
return [false, undefined];
|
||||
}
|
||||
}
|
||||
},
|
||||
array : (splitter, type=types.dynamic) => ({
|
||||
long_name : `array (${type.long_name})`,
|
||||
short_name : `arr (${type.short_name})`,
|
||||
checkSchema : (value, schema) => {
|
||||
if (schema.min_length != undefined) { // min_length
|
||||
if (value.length < schema.min_length) {
|
||||
return [false, 'minLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (schema.max_length != undefined) { // max_length
|
||||
if (value.length > schema.max_length) {
|
||||
return [false, 'maxLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
return [true, 'ok'];
|
||||
},
|
||||
syntax : (value, needs_convert = false) => {
|
||||
if (typeof(value) == 'string' & !!needs_convert) {
|
||||
value = value.split(splitter);
|
||||
}
|
||||
else if (typeof(value) != 'object') {
|
||||
return [false, undefined];
|
||||
}
|
||||
// checking type of array
|
||||
let isSyntax = Object.assign({}, value).filter(item => type.syntax(item, needs_convert)[0]).length == value.length;
|
||||
return [isSyntax, isSyntax ? value : undefined]
|
||||
}
|
||||
}),
|
||||
integer : {
|
||||
long_name : 'integer',
|
||||
short_name : 'int',
|
||||
checkSchema : (value, schema) => {
|
||||
if (schema.values != undefined) { // values
|
||||
if (schema.values.indexOf(value) == -1) {
|
||||
return [false, 'valuesError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (schema.min_length != undefined) { // min_length
|
||||
if (value < schema.min_length) {
|
||||
return [false, 'minLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (schema.max_length != undefined) { // max_length
|
||||
if (value > schema.max_length) {
|
||||
return [false, 'maxLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
return [true, 'ok'];
|
||||
},
|
||||
syntax : (value, needs_convert = false) => {
|
||||
function isInt (value) {
|
||||
if (String(parseInt(value)) == 'NaN') return false;
|
||||
return String(parseInt(value)) == String(Number(value));
|
||||
}
|
||||
|
||||
if (typeof(value) == 'number' & isInt(value)) {
|
||||
return [true, value];
|
||||
}
|
||||
else if (needs_convert & isInt(value)) {
|
||||
return [true, parseInt(value)];
|
||||
}
|
||||
else {
|
||||
return [false, undefined];
|
||||
}
|
||||
}
|
||||
},
|
||||
file : (allowed_types=null) => ({
|
||||
long_name : 'file',
|
||||
short_name : 'file',
|
||||
checkSchema : (value, schema) => {
|
||||
if (schema.min_length != undefined) { // min_length
|
||||
if (value.size < schema.min_length) {
|
||||
return [false, 'minLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (schema.max_length != undefined) { // max_length
|
||||
if (value.size > schema.max_length) {
|
||||
return [false, 'maxLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (allowed_types != null) {
|
||||
let file_extension = value.name.split(".")[value.name.split(".").length - 1];
|
||||
if (allowed_types.indexOf(file_extension.toLowerCase()) == -1) {
|
||||
return [false, 'unAllowExtension'];
|
||||
}
|
||||
}
|
||||
|
||||
return [true, 'ok'];
|
||||
},
|
||||
syntax : (value, needs_convert = false) => {
|
||||
if (typeof(value) != 'object') {
|
||||
return [false, undefined];
|
||||
}
|
||||
else {
|
||||
let syntaxed = value.name != undefined &
|
||||
value.mv != undefined &
|
||||
value.mimetype != undefined &
|
||||
value.data != undefined &
|
||||
value.tempFilePath != undefined &
|
||||
value.truncated != undefined &
|
||||
value.size != undefined &
|
||||
value.md5 != undefined;
|
||||
return [syntaxed, syntaxed ? value : undefined];
|
||||
}
|
||||
}
|
||||
}),
|
||||
string : {
|
||||
long_name : 'string',
|
||||
short_name : 'str',
|
||||
checkSchema : (value, schema) => {
|
||||
if (schema.values != undefined) { // values
|
||||
if (schema.values.indexOf(value) == -1) {
|
||||
return [false, 'valuesError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (schema.min_length != undefined) { // min_length
|
||||
if (value.length < schema.min_length) {
|
||||
return [false, 'minLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (schema.max_length != undefined) { // max_length
|
||||
if (value.length > schema.max_length) {
|
||||
return [false, 'maxLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
return [true, 'ok'];
|
||||
},
|
||||
syntax : (value, needs_convert = false) => {
|
||||
if (typeof(value) == 'string') {
|
||||
return [true, value];
|
||||
}
|
||||
else if (needs_convert) {
|
||||
return [true, value.toString()];
|
||||
}
|
||||
else {
|
||||
return [false, undefined];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const types = {
|
||||
dynamic : {
|
||||
long_name : 'dynamic',
|
||||
short_name : 'dyn',
|
||||
checkSchema : (value, schema) => {
|
||||
if (schema.values != undefined) { // values
|
||||
if (schema.values.indexOf(value) == -1) {
|
||||
return [false, 'valuesError'];
|
||||
}
|
||||
}
|
||||
|
||||
return [true, 'ok'];
|
||||
},
|
||||
syntax : (value, needs_convert = true) => [true, value]
|
||||
},
|
||||
unknown : {
|
||||
long_name : 'unknown',
|
||||
short_name : 'unk',
|
||||
checkSchema : (value, schema) => { throw new Error('Undefined datatype'); },
|
||||
syntax : (value, needs_convert = true) => {
|
||||
throw new Error('Undefined datatype');
|
||||
}
|
||||
},
|
||||
float : {
|
||||
long_name : 'float',
|
||||
short_name : 'float',
|
||||
checkSchema : (value, schema) => {
|
||||
if (schema.values != undefined) { // values
|
||||
if (schema.values.indexOf(value) == -1) {
|
||||
return [false, 'valuesError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (schema.min_length != undefined) { // min_length
|
||||
if (value < schema.min_length) {
|
||||
return [false, 'minLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (schema.max_length != undefined) { // max_length
|
||||
if (value > schema.max_length) {
|
||||
return [false, 'maxLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
return [true, 'ok'];
|
||||
},
|
||||
syntax : (value, needs_convert = true) => {
|
||||
function isFloat (value) {
|
||||
if (String(parseFloat(value)) == 'NaN') return false;
|
||||
return String(parseFloat(value)) == String(Number(value));
|
||||
}
|
||||
|
||||
if (typeof(value) == 'number' & isFloat(value)) {
|
||||
return [true, value];
|
||||
}
|
||||
else if (needs_convert & isFloat(value)) {
|
||||
return [true, parseFloat(value)];
|
||||
}
|
||||
else {
|
||||
return [false, undefined];
|
||||
}
|
||||
}
|
||||
},
|
||||
array : (splitter, type=types.dynamic) => ({
|
||||
long_name : `array (${type.long_name})`,
|
||||
short_name : `arr (${type.short_name})`,
|
||||
checkSchema : (value, schema) => {
|
||||
if (schema.min_length != undefined) { // min_length
|
||||
if (value.length < schema.min_length) {
|
||||
return [false, 'minLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (schema.max_length != undefined) { // max_length
|
||||
if (value.length > schema.max_length) {
|
||||
return [false, 'maxLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
return [true, 'ok'];
|
||||
},
|
||||
syntax : (value, needs_convert = false) => {
|
||||
if (typeof(value) == 'string' & !!needs_convert) {
|
||||
value = value.split(splitter);
|
||||
}
|
||||
else if (typeof(value) != 'object') {
|
||||
return [false, undefined];
|
||||
}
|
||||
// checking type of array
|
||||
let isSyntax = Object.assign({}, value).filter(item => type.syntax(item, needs_convert)[0]).length == value.length;
|
||||
return [isSyntax, isSyntax ? value : undefined]
|
||||
}
|
||||
}),
|
||||
integer : {
|
||||
long_name : 'integer',
|
||||
short_name : 'int',
|
||||
checkSchema : (value, schema) => {
|
||||
if (schema.values != undefined) { // values
|
||||
if (schema.values.indexOf(value) == -1) {
|
||||
return [false, 'valuesError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (schema.min_length != undefined) { // min_length
|
||||
if (value < schema.min_length) {
|
||||
return [false, 'minLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (schema.max_length != undefined) { // max_length
|
||||
if (value > schema.max_length) {
|
||||
return [false, 'maxLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
return [true, 'ok'];
|
||||
},
|
||||
syntax : (value, needs_convert = false) => {
|
||||
function isInt (value) {
|
||||
if (String(parseInt(value)) == 'NaN') return false;
|
||||
return String(parseInt(value)) == String(Number(value));
|
||||
}
|
||||
|
||||
if (typeof(value) == 'number' & isInt(value)) {
|
||||
return [true, value];
|
||||
}
|
||||
else if (needs_convert & isInt(value)) {
|
||||
return [true, parseInt(value)];
|
||||
}
|
||||
else {
|
||||
return [false, undefined];
|
||||
}
|
||||
}
|
||||
},
|
||||
file : (allowed_types=null) => ({
|
||||
long_name : 'file',
|
||||
short_name : 'file',
|
||||
checkSchema : (value, schema) => {
|
||||
if (schema.min_length != undefined) { // min_length
|
||||
if (value.size < schema.min_length) {
|
||||
return [false, 'minLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (schema.max_length != undefined) { // max_length
|
||||
if (value.size > schema.max_length) {
|
||||
return [false, 'maxLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (allowed_types != null) {
|
||||
let file_extension = value.name.split(".")[value.name.split(".").length - 1];
|
||||
if (allowed_types.indexOf(file_extension.toLowerCase()) == -1) {
|
||||
return [false, 'unAllowExtension'];
|
||||
}
|
||||
}
|
||||
|
||||
return [true, 'ok'];
|
||||
},
|
||||
syntax : (value, needs_convert = false) => {
|
||||
if (typeof(value) != 'object') {
|
||||
return [false, undefined];
|
||||
}
|
||||
else {
|
||||
let syntaxed = value.name != undefined &
|
||||
value.mv != undefined &
|
||||
value.mimetype != undefined &
|
||||
value.data != undefined &
|
||||
value.tempFilePath != undefined &
|
||||
value.truncated != undefined &
|
||||
value.size != undefined &
|
||||
value.md5 != undefined;
|
||||
return [syntaxed, syntaxed ? value : undefined];
|
||||
}
|
||||
}
|
||||
}),
|
||||
string : {
|
||||
long_name : 'string',
|
||||
short_name : 'str',
|
||||
checkSchema : (value, schema) => {
|
||||
if (schema.values != undefined) { // values
|
||||
if (schema.values.indexOf(value) == -1) {
|
||||
return [false, 'valuesError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (schema.min_length != undefined) { // min_length
|
||||
if (value.length < schema.min_length) {
|
||||
return [false, 'minLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
if (schema.max_length != undefined) { // max_length
|
||||
if (value.length > schema.max_length) {
|
||||
return [false, 'maxLengthError'];
|
||||
}
|
||||
}
|
||||
|
||||
return [true, 'ok'];
|
||||
},
|
||||
syntax : (value, needs_convert = false) => {
|
||||
if (typeof(value) == 'string') {
|
||||
return [true, value];
|
||||
}
|
||||
else if (needs_convert) {
|
||||
return [true, value.toString()];
|
||||
}
|
||||
else {
|
||||
return [false, undefined];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
module.exports = types;
|
12
index.js
12
index.js
@ -1,7 +1,7 @@
|
||||
module.exports = {
|
||||
Main : require('./components/Main').Main,
|
||||
ApiError : require('./components/Main').ApiError,
|
||||
Method : require('./components/Method'),
|
||||
types : require('./components/types'),
|
||||
Group : require('./components/Group')
|
||||
module.exports = {
|
||||
Main : require('./components/Main').Main,
|
||||
ApiError : require('./components/Main').ApiError,
|
||||
Method : require('./components/Method'),
|
||||
types : require('./components/types'),
|
||||
Group : require('./components/Group')
|
||||
};
|
@ -6,7 +6,7 @@
|
||||
"express-fileupload": "^1.4.0"
|
||||
},
|
||||
"name": "njsbacker",
|
||||
"version": "0.9.1",
|
||||
"version": "0.9.2",
|
||||
"description": "Module for easy developing back-end projects at express.js",
|
||||
"main": "index.js",
|
||||
"devDependencies": {},
|
||||
|
882
readme.md
882
readme.md
@ -1,441 +1,441 @@
|
||||
# Njsbacker
|
||||
Njsbacker is framework for backend developing at node.js/express.js *(in future versions can be supports any frameworks)*
|
||||
## How it works?
|
||||
Below you can see simple scheme as framework works: from **session** response get into **Main class** where or getting at **Group** and next into **Method**, or at **Method Class** where response data from http-params, **Group** and **Session** hadling at execution method.
|
||||

|
||||
After init main logic, logic can be converted at **express.js router** or **express.js server (app)**. Below you can see example code:
|
||||
```javascript
|
||||
// ... another code
|
||||
mainserverObject.server(
|
||||
'/api' // mount path of api
|
||||
).listen(8080, '127.0.0.1', async (err) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
else {
|
||||
console.log('SERVER RUNNED');
|
||||
}
|
||||
});
|
||||
```
|
||||
Below you can see how responses from http/call-function hadless into reponse at scheme:
|
||||

|
||||
Http data or Data from call method gets into pre_execution binary function where checks data: syntax/missed or no: if all successfuly data get into Session/Group/Method execute method else throwing error.
|
||||
### Before work.
|
||||
Before work you must install this package:
|
||||
```bash
|
||||
# for npm
|
||||
npm install njsbacker
|
||||
# for yarn
|
||||
yarn add njsbacker
|
||||
```
|
||||
And import at yoy project
|
||||
```javascript
|
||||
// from nodejs
|
||||
const njsbacker = require('njsbacker');
|
||||
```
|
||||
```typescript
|
||||
// from typescript
|
||||
import njsbacker from 'njsbacker';
|
||||
```
|
||||
*(We will be use node.js)*
|
||||
## General clases and objects
|
||||
### Main
|
||||
Main class is main backend-application, mainbased skelet of project. Mainclass object containts methods, groups, sessionHandler, etc. For all works you must create your class from njsbacker.Main
|
||||
```javascript
|
||||
const njsbacker = require('njsbacker');
|
||||
|
||||
class App extends njsbacker.Main {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
If you want handling your format errors, responses *(default JSON)* you must add methods into your body of class:
|
||||
```javascript
|
||||
const njsbacker = require('njsbacker');
|
||||
|
||||
class App extends njsbacker.Main {
|
||||
errorHadler (error) { // Handling and show errors at backend application.
|
||||
let errorData;
|
||||
let codeStatus = 400;
|
||||
if (error.name == "API Error") {
|
||||
errorData = {
|
||||
code : error.message,
|
||||
details : error.data
|
||||
};
|
||||
}
|
||||
else {
|
||||
errorData = {
|
||||
name : error.name,
|
||||
stack : error.stack
|
||||
};
|
||||
codeStatus = 502;
|
||||
}
|
||||
return {
|
||||
mainbody : { error : errorData },
|
||||
headers : {
|
||||
error : error.name
|
||||
},
|
||||
cookies : {
|
||||
error_rised_at : Math.round(new Date().getTime() / 1000)
|
||||
},
|
||||
// redirect_uri: ''; // if want redirect to another url
|
||||
code: codeStatus
|
||||
}
|
||||
}
|
||||
|
||||
responseHandler (response) { return ({ // Handling responses at backend application
|
||||
mainbody : { response },
|
||||
headers : {
|
||||
errored: 0
|
||||
},
|
||||
cookies : {},
|
||||
// redirect_uri: ''; // if want redirect to another url
|
||||
code: 200
|
||||
}) };
|
||||
|
||||
session (params, sessionData) { // Session function
|
||||
sessionData._setValue('example', 1); // Set value of sessionData object
|
||||
console.log(sessionData.example); // Get value from sessionData object
|
||||
sessionData._remove('example'); // Remove value
|
||||
return; // Successful
|
||||
throw 'Example error'; // Example of error
|
||||
}
|
||||
|
||||
paramsError (required, additional) { // Handling missed/unsyntax params
|
||||
return new njsbacker.ApiError('UNSYNTAX_OR_MISSED_REQUIRED_PARAMS', { required, additional });
|
||||
}
|
||||
}
|
||||
```
|
||||
#### Method: errorHadler
|
||||
Hadling and format errors. First argument is error object when may be hadled by this method. Returns handled error schema when must be containts next params:
|
||||
```typescript
|
||||
mainbody : < buffer / object / string > // content when will be returns
|
||||
headers : object // headers into http-response
|
||||
cookies : object // cookies when will be applyed
|
||||
code : number // http-code
|
||||
redirect_uri : string // redirect url (non-required, undefined if not redirect)
|
||||
```
|
||||
#### Method: responseHandler
|
||||
Handling responses at backend application. First argument is reponse from .execute method at **Method** object. Must be containts next params:
|
||||
```typescript
|
||||
mainbody : < buffer / object / string > // content when will be returns
|
||||
headers : object // headers into http-response
|
||||
cookies : object // cookies when will be applyed
|
||||
code : number // http-code
|
||||
redirect_uri : string // redirect url (non-required, undefined if not redirect)
|
||||
```
|
||||
#### Method: session
|
||||
Method that call before executing method or groups of method. First argument - params from http/call method, second argument - **Session** object. This method setting **Session** object params and return error at http-response if error throws.
|
||||
#### Method: paramsError
|
||||
Method that return error when missing or unsyntax params.
|
||||
#### Create object of njsbacker.Main and configuration
|
||||
Before work you must create object of njsbacker.Main:
|
||||
```javascript
|
||||
var app = new App(
|
||||
false // Returns information about njsbacker in headers.
|
||||
);
|
||||
```
|
||||
If you're planning use session you can configure him:
|
||||
```javascript
|
||||
app.setSessionParams(
|
||||
{
|
||||
example_session_param : {
|
||||
required : false,
|
||||
type : njsbacker.types.string
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
**First param** - information about inputed params [(Params inforamtion)](#params-information)
|
||||
### Method
|
||||
After you create **njsbacker.Main** object you must create class that extends from **njsbacker.Method**:
|
||||
```javascript
|
||||
class ExampleMethod extends njsbacker.Method {
|
||||
// Params handler
|
||||
execute (params, session, groups) {
|
||||
// Any code...
|
||||
return anotherResult;
|
||||
// If you needs raising error:
|
||||
throw new njsbacker.ApiError(
|
||||
'EXAMPLE_ERROR', // Error code
|
||||
new Object() // Error details
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
#### Method: execute
|
||||
Method of executing current method. Return data when will be sended to njsbacker.Main.responseHandler method.
|
||||
#### Connection and configure njsbacker.Method
|
||||
After creating class you must create object of njsbacker.Method:
|
||||
```javascript
|
||||
var exampleMethod = new ExampleMethod('example', '/example', {
|
||||
text : {
|
||||
required : true,
|
||||
type : njsbacker.types.string,
|
||||
min_length : 1,
|
||||
max_length : 255
|
||||
}
|
||||
});
|
||||
```
|
||||
**First param** - name of method into system.
|
||||
**Second param** - path to method into http-server
|
||||
**Third param** - information about inputed params [(Params inforamtion)](#params-information)
|
||||
If you create object of **njsbacker.Group** you can pin it with use next method:
|
||||
```javascript
|
||||
exampleMethod.group(groupObject);
|
||||
```
|
||||
And that this method work you must pin this method to object of **njsbacker.Main** with use next method:
|
||||
```javascript
|
||||
app.method(exampleMethod);
|
||||
```
|
||||
#### Additional tool into njsbacker.Method: this.MainObject.call
|
||||
Inside execute method you can refer to existing methods into API.
|
||||
**Annotation**: this.MainObject is object of **njsbacker.Main** there was been included to **njsbacker.Method**
|
||||
```javascript
|
||||
class ExampleMethod extends njsbacker.Method {
|
||||
execute (params, session, groups) {
|
||||
// Another code here...
|
||||
this.MainObject.call('SecondExampleMethodName', {
|
||||
param : "value"
|
||||
});
|
||||
// Another code here...
|
||||
return result;
|
||||
}
|
||||
}
|
||||
```
|
||||
**First param** - name of method.
|
||||
**Second param** - params there sends into method.
|
||||
## Additional clases and objects
|
||||
### Params Information
|
||||
Before reading this part we tolds about inputed params into http.
|
||||
**Example:**
|
||||
```javascript
|
||||
var paramsInfo = {
|
||||
paramNameCookie : {
|
||||
required : true,
|
||||
type : njsbacker.types.string,
|
||||
import_key : 'param',
|
||||
allow_params : ['cookies']
|
||||
},
|
||||
paramNameQuery : {
|
||||
type : njsbacker.types.string,
|
||||
import_key : 'param',
|
||||
allow_params : ['query']
|
||||
}
|
||||
}
|
||||
|
||||
var exampleMethod = new ExampleMethod('example', '/example', paramsInfo);
|
||||
```
|
||||
**Objects keys and default values**:
|
||||
```typescript
|
||||
required : boolean = false // Required param or no.
|
||||
import_key : string = param // Key when using as param int http headers/string (etc.)
|
||||
save_key : string = param // Key when will be saved at param object
|
||||
type : object = njsbacker.types.unknown // Param's datatype
|
||||
allow_methods : array = ['get', 'post', 'put', 'delete'] // Methods when method will be listen.
|
||||
conversion : boolean = false // Covert datatype if inputed datatype not equal "type" param.
|
||||
allow_params : array = ['headers', 'json', 'params', 'query', 'body', 'files', 'cookies'] // Http-params where will be reads into method.
|
||||
```
|
||||
### Group
|
||||
This is njbacker object where executing before **njsbacker.Method** if was been pinned to **njsbacker.Method**.
|
||||
Before work you must create class when extends from **njsbacker.Method** and create into class method **handler**:
|
||||
```javascript
|
||||
class ExampleGroup extends njsbacker.Method {
|
||||
handler (params, session) { // Path handling
|
||||
session._setValue('example', 1); // Set value
|
||||
console.log(session.example); // Get value from session
|
||||
session._remove('example'); // Remove value
|
||||
return 1; // Successful
|
||||
throw 'Example error' // Error example
|
||||
}
|
||||
}
|
||||
```
|
||||
### types
|
||||
**dynamic** - Dynamic datatype
|
||||
**unknown** - Unknown datatype *(Raising error)*
|
||||
**float** - Float *(real)* datatype
|
||||
**array** *(function)* - array of data.
|
||||
```javascript
|
||||
// Example of array
|
||||
var datatype = njsbacker.types.array(
|
||||
splitSymbol, // symbol where will be used for splits queries, cookies and other string params. (required)
|
||||
typeOfArray, // type of array data. (default: dynamic)
|
||||
);
|
||||
```
|
||||
**integer** - Integer datatype.
|
||||
**file** *(function)* - file object [read more in express-fileupload module](https://www.npmjs.com/package/express-fileupload)
|
||||
```javascript
|
||||
// Example of file
|
||||
var datatype = njsbacker.types.file(
|
||||
allowedExtensions, // allowed extensions (default: null (all extensions allowed))
|
||||
);
|
||||
```
|
||||
**string** - String datatype.
|
||||
## Example code
|
||||
```javascript
|
||||
const njsbacker = require('./index');
|
||||
|
||||
// Create backend mainclass then extends from njsbacker.Main:
|
||||
class Main extends njsbacker.Main {
|
||||
session (params, sessionData) {
|
||||
sessionData._setValue('example', 1); // Set value
|
||||
console.log(sessionData.example); // Get value from session
|
||||
sessionData._remove('example'); // Remove value
|
||||
return 1; // Successful
|
||||
throw 'Example error' // Example of error
|
||||
}
|
||||
|
||||
responseHandler (response) { return ({
|
||||
mainbody : { response },
|
||||
headers : {
|
||||
errored: 0
|
||||
},
|
||||
cookies : {},
|
||||
// redirect_uri: ''; // if want redirect to another url
|
||||
code: 200
|
||||
}) };
|
||||
|
||||
/* paramsError (required, additional) { return({ required, additional }) }; */
|
||||
}
|
||||
// Create object of Main class.
|
||||
var server = new Main(
|
||||
false // Show information about this library into headers.
|
||||
);
|
||||
server.setSessionParams( // Set required params for session.
|
||||
{
|
||||
session_id : {
|
||||
required : false,
|
||||
type : njsbacker.types.integer
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// Create class from method's group.
|
||||
class ExampleMethodGroup extends njsbacker.Group {
|
||||
handler (params, session) { // Path handling
|
||||
session._setValue('example', 1); // Set value
|
||||
console.log(session.example); // Get value from session
|
||||
session._remove('example'); // Remove value
|
||||
return 1; // Successful
|
||||
throw 'Example error' // Example of error
|
||||
}
|
||||
}
|
||||
// Create classes of method
|
||||
class ExampleAnyMethodsOfHandlingInformation extends njsbacker.Method {
|
||||
execute (params, session, groups) {
|
||||
return {
|
||||
json_data : params.json_name,
|
||||
query_data : params.query_name,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class ExampleMethod extends njsbacker.Method {
|
||||
/*
|
||||
var result = this.MainObject.call(method : string, params : object) // Вызов подключённого метода
|
||||
*/
|
||||
|
||||
// Params handler
|
||||
execute (params, session, groups) {
|
||||
return {
|
||||
text : params.text,
|
||||
result : this.MainObject.call('sum', {
|
||||
a : 15,
|
||||
b : 17,
|
||||
session_id : params.session_id
|
||||
})
|
||||
};
|
||||
throw new njsbacker.ApiError('EXAMPLE_ERROR', new Object());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class SumMethod extends njsbacker.Method {
|
||||
execute (params, session, groups) {
|
||||
return params.a + params.b;
|
||||
}
|
||||
}
|
||||
|
||||
class FileMethod extends njsbacker.Method {
|
||||
execute (params, session, groups) {
|
||||
return JSON.stringify(params.file);
|
||||
}
|
||||
}
|
||||
|
||||
// Create class objects
|
||||
var eamohi = new ExampleAnyMethodsOfHandlingInformation('handler', '/handler', {
|
||||
queryName : {
|
||||
required : true,
|
||||
type : njsbacker.types.string,
|
||||
import_key : 'name',
|
||||
allow_params : ['query']
|
||||
},
|
||||
jsonName : {
|
||||
required : true,
|
||||
type : njsbacker.types.string,
|
||||
import_key : 'name',
|
||||
allow_methods : ['post'],
|
||||
allow_params : ['json']
|
||||
}
|
||||
});
|
||||
|
||||
var fileMethod = new FileMethod('file', '/file', {
|
||||
file : {
|
||||
required : true,
|
||||
type : njsbacker.types.file()
|
||||
}
|
||||
});
|
||||
|
||||
var sumMethod = new SumMethod('sum', '/sum', {
|
||||
a : {
|
||||
required : true,
|
||||
type : njsbacker.types.integer,
|
||||
conversion : false,
|
||||
// allow_methods : ['post'],
|
||||
},
|
||||
b : {
|
||||
required : true,
|
||||
type : njsbacker.types.integer,
|
||||
conversion : false,
|
||||
// allow_methods : ['post'],
|
||||
}
|
||||
});
|
||||
|
||||
var exampleMethod = new ExampleMethod('example', '/example', {
|
||||
text : {
|
||||
required : true,
|
||||
type : njsbacker.types.string,
|
||||
conversion : false,
|
||||
values : ['123', 'test'],
|
||||
min_length : 1,
|
||||
max_length : 255,
|
||||
// allow_methods : ['post'],
|
||||
// allow_params : ['json'],
|
||||
}
|
||||
});
|
||||
// Pins methods to group
|
||||
exampleMethod.group(new ExampleMethodGroup({
|
||||
ses : {
|
||||
type : njsbacker.types.string
|
||||
}
|
||||
}));
|
||||
sumMethod.group(new ExampleMethodGroup({
|
||||
ses : {
|
||||
type : njsbacker.types.string
|
||||
}
|
||||
}));
|
||||
// Pin methods to mein project
|
||||
server.method(exampleMethod);
|
||||
server.method(sumMethod);
|
||||
server.method(fileMethod);
|
||||
server.method(eamohi);
|
||||
|
||||
// Run server
|
||||
server.server('/api/v1').listen(8080, async (err) => {
|
||||
if (err) { throw err; }
|
||||
else {
|
||||
console.log('SERVER RUNNED');
|
||||
}
|
||||
});
|
||||
```
|
||||
You can show example code in file **codeExample.js**
|
||||
# Njsbacker
|
||||
Njsbacker is framework for backend developing at node.js/express.js *(in future versions can be supports any frameworks)*
|
||||
## How it works?
|
||||
Below you can see simple scheme as framework works: from **session** response get into **Main class** where or getting at **Group** and next into **Method**, or at **Method Class** where response data from http-params, **Group** and **Session** hadling at execution method.
|
||||

|
||||
After init main logic, logic can be converted at **express.js router** or **express.js server (app)**. Below you can see example code:
|
||||
```javascript
|
||||
// ... another code
|
||||
mainserverObject.server(
|
||||
'/api' // mount path of api
|
||||
).listen(8080, '127.0.0.1', async (err) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
else {
|
||||
console.log('SERVER RUNNED');
|
||||
}
|
||||
});
|
||||
```
|
||||
Below you can see how responses from http/call-function hadless into reponse at scheme:
|
||||

|
||||
Http data or Data from call method gets into pre_execution binary function where checks data: syntax/missed or no: if all successfuly data get into Session/Group/Method execute method else throwing error.
|
||||
### Before work.
|
||||
Before work you must install this package:
|
||||
```bash
|
||||
# for npm
|
||||
npm install njsbacker
|
||||
# for yarn
|
||||
yarn add njsbacker
|
||||
```
|
||||
And import at yoy project
|
||||
```javascript
|
||||
// from nodejs
|
||||
const njsbacker = require('njsbacker');
|
||||
```
|
||||
```typescript
|
||||
// from typescript
|
||||
import njsbacker from 'njsbacker';
|
||||
```
|
||||
*(We will be use node.js)*
|
||||
## General clases and objects
|
||||
### Main
|
||||
Main class is main backend-application, mainbased skelet of project. Mainclass object containts methods, groups, sessionHandler, etc. For all works you must create your class from njsbacker.Main
|
||||
```javascript
|
||||
const njsbacker = require('njsbacker');
|
||||
|
||||
class App extends njsbacker.Main {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
If you want handling your format errors, responses *(default JSON)* you must add methods into your body of class:
|
||||
```javascript
|
||||
const njsbacker = require('njsbacker');
|
||||
|
||||
class App extends njsbacker.Main {
|
||||
errorHadler (error) { // Handling and show errors at backend application.
|
||||
let errorData;
|
||||
let codeStatus = 400;
|
||||
if (error.name == "API Error") {
|
||||
errorData = {
|
||||
code : error.message,
|
||||
details : error.data
|
||||
};
|
||||
}
|
||||
else {
|
||||
errorData = {
|
||||
name : error.name,
|
||||
stack : error.stack
|
||||
};
|
||||
codeStatus = 502;
|
||||
}
|
||||
return {
|
||||
mainbody : { error : errorData },
|
||||
headers : {
|
||||
error : error.name
|
||||
},
|
||||
cookies : {
|
||||
error_rised_at : Math.round(new Date().getTime() / 1000)
|
||||
},
|
||||
// redirect_uri: ''; // if want redirect to another url
|
||||
code: codeStatus
|
||||
}
|
||||
}
|
||||
|
||||
responseHandler (response) { return ({ // Handling responses at backend application
|
||||
mainbody : { response },
|
||||
headers : {
|
||||
errored: 0
|
||||
},
|
||||
cookies : {},
|
||||
// redirect_uri: ''; // if want redirect to another url
|
||||
code: 200
|
||||
}) };
|
||||
|
||||
session (params, sessionData) { // Session function
|
||||
sessionData._setValue('example', 1); // Set value of sessionData object
|
||||
console.log(sessionData.example); // Get value from sessionData object
|
||||
sessionData._remove('example'); // Remove value
|
||||
return; // Successful
|
||||
throw 'Example error'; // Example of error
|
||||
}
|
||||
|
||||
paramsError (required, additional) { // Handling missed/unsyntax params
|
||||
return new njsbacker.ApiError('UNSYNTAX_OR_MISSED_REQUIRED_PARAMS', { required, additional });
|
||||
}
|
||||
}
|
||||
```
|
||||
#### Method: errorHadler
|
||||
Hadling and format errors. First argument is error object when may be hadled by this method. Returns handled error schema when must be containts next params:
|
||||
```typescript
|
||||
mainbody : < buffer / object / string > // content when will be returns
|
||||
headers : object // headers into http-response
|
||||
cookies : object // cookies when will be applyed
|
||||
code : number // http-code
|
||||
redirect_uri : string // redirect url (non-required, undefined if not redirect)
|
||||
```
|
||||
#### Method: responseHandler
|
||||
Handling responses at backend application. First argument is reponse from .execute method at **Method** object. Must be containts next params:
|
||||
```typescript
|
||||
mainbody : < buffer / object / string > // content when will be returns
|
||||
headers : object // headers into http-response
|
||||
cookies : object // cookies when will be applyed
|
||||
code : number // http-code
|
||||
redirect_uri : string // redirect url (non-required, undefined if not redirect)
|
||||
```
|
||||
#### Method: session
|
||||
Method that call before executing method or groups of method. First argument - params from http/call method, second argument - **Session** object. This method setting **Session** object params and return error at http-response if error throws.
|
||||
#### Method: paramsError
|
||||
Method that return error when missing or unsyntax params.
|
||||
#### Create object of njsbacker.Main and configuration
|
||||
Before work you must create object of njsbacker.Main:
|
||||
```javascript
|
||||
var app = new App(
|
||||
false // Returns information about njsbacker in headers.
|
||||
);
|
||||
```
|
||||
If you're planning use session you can configure him:
|
||||
```javascript
|
||||
app.setSessionParams(
|
||||
{
|
||||
example_session_param : {
|
||||
required : false,
|
||||
type : njsbacker.types.string
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
**First param** - information about inputed params [(Params inforamtion)](#params-information)
|
||||
### Method
|
||||
After you create **njsbacker.Main** object you must create class that extends from **njsbacker.Method**:
|
||||
```javascript
|
||||
class ExampleMethod extends njsbacker.Method {
|
||||
// Params handler
|
||||
execute (params, session, groups) {
|
||||
// Any code...
|
||||
return anotherResult;
|
||||
// If you needs raising error:
|
||||
throw new njsbacker.ApiError(
|
||||
'EXAMPLE_ERROR', // Error code
|
||||
new Object() // Error details
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
#### Method: execute
|
||||
Method of executing current method. Return data when will be sended to njsbacker.Main.responseHandler method.
|
||||
#### Connection and configure njsbacker.Method
|
||||
After creating class you must create object of njsbacker.Method:
|
||||
```javascript
|
||||
var exampleMethod = new ExampleMethod('example', '/example', {
|
||||
text : {
|
||||
required : true,
|
||||
type : backend.types.string,
|
||||
min_length : 1,
|
||||
max_length : 255
|
||||
}
|
||||
});
|
||||
```
|
||||
**First param** - name of method into system.
|
||||
**Second param** - path to method into http-server
|
||||
**Third param** - information about inputed params [(Params inforamtion)](#params-information)
|
||||
If you create object of **njsbacker.Group** you can pin it with use next method:
|
||||
```javascript
|
||||
exampleMethod.group(groupObject);
|
||||
```
|
||||
And that this method work you must pin this method to object of **njsbacker.Main** with use next method:
|
||||
```javascript
|
||||
app.method(exampleMethod);
|
||||
```
|
||||
#### Additional tool into njsbacker.Method: this.MainObject.call
|
||||
Inside execute method you can refer to existing methods into API.
|
||||
**Annotation**: this.MainObject is object of **njsbacker.Main** there was been included to **njsbacker.Method**
|
||||
```javascript
|
||||
class ExampleMethod extends njsbacker.Method {
|
||||
execute (params, session, groups) {
|
||||
// Another code here...
|
||||
this.MainObject.call('SecondExampleMethodName', {
|
||||
param : "value"
|
||||
});
|
||||
// Another code here...
|
||||
return result;
|
||||
}
|
||||
}
|
||||
```
|
||||
**First param** - name of method.
|
||||
**Second param** - params there sends into method.
|
||||
## Additional clases and objects
|
||||
### Params Information
|
||||
Before reading this part we tolds about inputed params into http.
|
||||
**Example:**
|
||||
```javascript
|
||||
var paramsInfo = {
|
||||
paramNameCookie : {
|
||||
required : true,
|
||||
type : njsbacker.types.string,
|
||||
import_key : 'param',
|
||||
allow_params : ['cookies']
|
||||
},
|
||||
paramNameQuery : {
|
||||
type : njsbacker.types.string,
|
||||
import_key : 'param',
|
||||
allow_params : ['query']
|
||||
}
|
||||
}
|
||||
|
||||
var exampleMethod = new ExampleMethod('example', '/example', paramsInfo);
|
||||
```
|
||||
**Objects keys and default values**:
|
||||
```typescript
|
||||
required : boolean = false // Required param or no.
|
||||
import_key : string = param // Key when using as param int http headers/string (etc.)
|
||||
save_key : string = param // Key when will be saved at param object
|
||||
type : object = njsbacker.types.unknown // Param's datatype
|
||||
allow_methods : array = ['get', 'post', 'put', 'delete'] // Methods when method will be listen.
|
||||
conversion : boolean = false // Covert datatype if inputed datatype not equal "type" param.
|
||||
allow_params : array = ['headers', 'json', 'params', 'query', 'body', 'files', 'cookies'] // Http-params where will be reads into method.
|
||||
```
|
||||
### Group
|
||||
This is njbacker object where executing before **njsbacker.Method** if was been pinned to **njsbacker.Method**.
|
||||
Before work you must create class when extends from **njsbacker.Method** and create into class method **handler**:
|
||||
```javascript
|
||||
class ExampleGroup extends njsbacker.Method {
|
||||
handler (params, session) { // Path handling
|
||||
session._setValue('example', 1); // Set value
|
||||
console.log(session.example); // Get value from session
|
||||
session._remove('example'); // Remove value
|
||||
return 1; // Successful
|
||||
throw 'Example error' // Error example
|
||||
}
|
||||
}
|
||||
```
|
||||
### types
|
||||
**dynamic** - Dynamic datatype
|
||||
**unknown** - Unknown datatype *(Raising error)*
|
||||
**float** - Float *(real)* datatype
|
||||
**array** *(function)* - array of data.
|
||||
```javascript
|
||||
// Example of array
|
||||
var datatype = njsbacker.types.array(
|
||||
splitSymbol, // symbol where will be used for splits queries, cookies and other string params. (required)
|
||||
typeOfArray, // type of array data. (default: dynamic)
|
||||
);
|
||||
```
|
||||
**integer** - Integer datatype.
|
||||
**file** *(function)* - file object [read more in express-fileupload module](https://www.npmjs.com/package/express-fileupload)
|
||||
```javascript
|
||||
// Example of file
|
||||
var datatype = njsbacker.types.file(
|
||||
allowedExtensions, // allowed extensions (default: null (all extensions allowed))
|
||||
);
|
||||
```
|
||||
**string** - String datatype.
|
||||
## Example code
|
||||
```javascript
|
||||
const njsbacker = require('./index');
|
||||
|
||||
// Create backend mainclass then extends from njsbacker.Main:
|
||||
class Main extends njsbacker.Main {
|
||||
session (params, sessionData) {
|
||||
sessionData._setValue('example', 1); // Set value
|
||||
console.log(sessionData.example); // Get value from session
|
||||
sessionData._remove('example'); // Remove value
|
||||
return 1; // Successful
|
||||
throw 'Example error' // Example of error
|
||||
}
|
||||
|
||||
responseHandler (response) { return ({
|
||||
mainbody : { response },
|
||||
headers : {
|
||||
errored: 0
|
||||
},
|
||||
cookies : {},
|
||||
// redirect_uri: ''; // if want redirect to another url
|
||||
code: 200
|
||||
}) };
|
||||
|
||||
/* paramsError (required, additional) { return({ required, additional }) }; */
|
||||
}
|
||||
// Create object of Main class.
|
||||
var server = new Main(
|
||||
false // Show information about this library into headers.
|
||||
);
|
||||
server.setSessionParams( // Set required params for session.
|
||||
{
|
||||
session_id : {
|
||||
required : false,
|
||||
type : njsbacker.types.integer
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// Create class from method's group.
|
||||
class ExampleMethodGroup extends njsbacker.Group {
|
||||
handler (params, session) { // Path handling
|
||||
session._setValue('example', 1); // Set value
|
||||
console.log(session.example); // Get value from session
|
||||
session._remove('example'); // Remove value
|
||||
return 1; // Successful
|
||||
throw 'Example error' // Example of error
|
||||
}
|
||||
}
|
||||
// Create classes of method
|
||||
class ExampleAnyMethodsOfHandlingInformation extends njsbacker.Method {
|
||||
execute (params, session, groups) {
|
||||
return {
|
||||
json_data : params.json_name,
|
||||
query_data : params.query_name,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class ExampleMethod extends njsbacker.Method {
|
||||
/*
|
||||
var result = this.MainObject.call(method : string, params : object) // Вызов подключённого метода
|
||||
*/
|
||||
|
||||
// Params handler
|
||||
execute (params, session, groups) {
|
||||
return {
|
||||
text : params.text,
|
||||
result : this.MainObject.call('sum', {
|
||||
a : 15,
|
||||
b : 17,
|
||||
session_id : params.session_id
|
||||
})
|
||||
};
|
||||
throw new njsbacker.ApiError('EXAMPLE_ERROR', new Object());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class SumMethod extends njsbacker.Method {
|
||||
execute (params, session, groups) {
|
||||
return params.a + params.b;
|
||||
}
|
||||
}
|
||||
|
||||
class FileMethod extends njsbacker.Method {
|
||||
execute (params, session, groups) {
|
||||
return JSON.stringify(params.file);
|
||||
}
|
||||
}
|
||||
|
||||
// Create class objects
|
||||
var eamohi = new ExampleAnyMethodsOfHandlingInformation('handler', '/handler', {
|
||||
queryName : {
|
||||
required : true,
|
||||
type : njsbacker.types.string,
|
||||
import_key : 'name',
|
||||
allow_params : ['query']
|
||||
},
|
||||
jsonName : {
|
||||
required : true,
|
||||
type : njsbacker.types.string,
|
||||
import_key : 'name',
|
||||
allow_methods : ['post'],
|
||||
allow_params : ['json']
|
||||
}
|
||||
});
|
||||
|
||||
var fileMethod = new FileMethod('file', '/file', {
|
||||
file : {
|
||||
required : true,
|
||||
type : njsbacker.types.file()
|
||||
}
|
||||
});
|
||||
|
||||
var sumMethod = new SumMethod('sum', '/sum', {
|
||||
a : {
|
||||
required : true,
|
||||
type : njsbacker.types.integer,
|
||||
conversion : false,
|
||||
// allow_methods : ['post'],
|
||||
},
|
||||
b : {
|
||||
required : true,
|
||||
type : njsbacker.types.integer,
|
||||
conversion : false,
|
||||
// allow_methods : ['post'],
|
||||
}
|
||||
});
|
||||
|
||||
var exampleMethod = new ExampleMethod('example', '/example', {
|
||||
text : {
|
||||
required : true,
|
||||
type : njsbacker.types.string,
|
||||
conversion : false,
|
||||
values : ['123', 'test'],
|
||||
min_length : 1,
|
||||
max_length : 255,
|
||||
// allow_methods : ['post'],
|
||||
// allow_params : ['json'],
|
||||
}
|
||||
});
|
||||
// Pins methods to group
|
||||
exampleMethod.group(new ExampleMethodGroup({
|
||||
ses : {
|
||||
type : njsbacker.types.string
|
||||
}
|
||||
}));
|
||||
sumMethod.group(new ExampleMethodGroup({
|
||||
ses : {
|
||||
type : njsbacker.types.string
|
||||
}
|
||||
}));
|
||||
// Pin methods to mein project
|
||||
server.method(exampleMethod);
|
||||
server.method(sumMethod);
|
||||
server.method(fileMethod);
|
||||
server.method(eamohi);
|
||||
|
||||
// Run server
|
||||
server.server('/api/v1').listen(8080, async (err) => {
|
||||
if (err) { throw err; }
|
||||
else {
|
||||
console.log('SERVER RUNNED');
|
||||
}
|
||||
});
|
||||
```
|
||||
You can show example code in file **codeExample.js**
|
Loading…
Reference in New Issue
Block a user