add files into repos
This commit is contained in:
parent
5c3dda0daa
commit
980a40ddda
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
node_modules
|
@ -13,7 +13,7 @@ server.session = (params, sessionData) => {
|
|||||||
return 'Example error'; // Пример ошибки
|
return 'Example error'; // Пример ошибки
|
||||||
};
|
};
|
||||||
// Настраиваем вывод
|
// Настраиваем вывод
|
||||||
server.error = (error) => ({
|
server.errorHandler = (error) => ({
|
||||||
mainbody : { error },
|
mainbody : { error },
|
||||||
headers : {
|
headers : {
|
||||||
errored : 1
|
errored : 1
|
||||||
@ -24,7 +24,7 @@ server.error = (error) => ({
|
|||||||
// redirect_uri: ''; // if want redirect to another url
|
// redirect_uri: ''; // if want redirect to another url
|
||||||
code: 400
|
code: 400
|
||||||
});
|
});
|
||||||
server.resposne = (response) => ({
|
server.resposneHandler = (response) => ({
|
||||||
mainbody : { response },
|
mainbody : { response },
|
||||||
headers : {
|
headers : {
|
||||||
errored: 0
|
errored: 0
|
||||||
@ -58,7 +58,8 @@ class ExampleMethod extends backend.Method {
|
|||||||
throw { code: 'EXAMPLE_ERROR', details: new Object() };
|
throw { code: 'EXAMPLE_ERROR', details: new Object() };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var exampleMethod = new ExampleMethod('/example', {
|
|
||||||
|
var exampleMethod = new ExampleMethod('example', '/example', {
|
||||||
text: {
|
text: {
|
||||||
required : true,
|
required : true,
|
||||||
type : backend.types.string,
|
type : backend.types.string,
|
||||||
@ -76,4 +77,9 @@ exampleMethod.group(ExampleMethodGroup);
|
|||||||
server.method(exampleMethod);
|
server.method(exampleMethod);
|
||||||
|
|
||||||
// Запускаем сервер
|
// Запускаем сервер
|
||||||
server.server('/api/v1'/*, { Информация о SSL }*/).listen(8080);
|
server.server('/api/v1'/*, { Информация о SSL }*/).listen(8080, async (err) => {
|
||||||
|
if (err) { throw err; }
|
||||||
|
else {
|
||||||
|
console.log('SERVER RUNNED');
|
||||||
|
}
|
||||||
|
});
|
4
components/Group.js
Normal file
4
components/Group.js
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
class Group {}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = Group;
|
@ -1,29 +1,100 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
|
const bodyParser = require('body-parser')
|
||||||
const https = require('https');
|
const https = require('https');
|
||||||
const version = 'v 1.0.0';
|
const version = '1.0.0';
|
||||||
|
|
||||||
|
|
||||||
class Main {
|
class Main {
|
||||||
constructor (send_headers = true) {
|
constructor (sendHeaders = true) {
|
||||||
this.send_headers = send_headers;
|
this.sendHeaders = sendHeaders;
|
||||||
this.methods = new Object();
|
this.methods = new Object();
|
||||||
|
|
||||||
|
this.errorHadler = (error) => ({
|
||||||
|
mainbody : { error : error },
|
||||||
|
headers : {},
|
||||||
|
cookies : {},
|
||||||
|
// redirect_uri: ''; // if want redirect to another url
|
||||||
|
code: 400
|
||||||
|
});
|
||||||
|
|
||||||
|
this.resposneHandler = (response) => ({
|
||||||
|
mainbody : { response },
|
||||||
|
headers : {},
|
||||||
|
cookies : {},
|
||||||
|
// redirect_uri: ''; // if want redirect to another url
|
||||||
|
code: 200
|
||||||
|
});
|
||||||
|
|
||||||
|
this.paramsError = (required, additional) => ({ required, additional });
|
||||||
|
this.typeError = 'param {param} must be only {long_type} ({short_type})';
|
||||||
}
|
}
|
||||||
|
|
||||||
method (methodObj) {
|
method (methodObj) {
|
||||||
this.methods[methodObj.path] = methodObj;
|
this.methods[methodObj.name] = methodObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
call (method, params) {
|
call (method, params) {
|
||||||
return this.methods[method.path].pre_execute(params);
|
return this.methods[method.name].pre_execute(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
router (returnMiddlewareFunction = true, middlewareFunction = (req, res, next) => next()) {
|
router (returnMiddlewareFunction = false, middlewareFunction = (req, res, next) => next(), debug = (text) => console.log(text)) {
|
||||||
let router = express.Router();
|
let router = express.Router();
|
||||||
|
router.use(require('cookie-parser')());
|
||||||
|
// parse various different custom JSON types as 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' }))
|
||||||
|
|
||||||
for (let path in this.methods) {
|
for (let name in this.methods) {
|
||||||
for (let methodId in this.methods[path].allowedMethods) {
|
debug(`CONNECT METHOD : ${name}`);
|
||||||
router[this.methods[path].allowedMethods[methodId]](async (req, res) => {
|
for (let methodId in this.methods[name].allowedMethods) {
|
||||||
// (!) Mainbody
|
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 handledDataResponse = this.responseHandler(
|
||||||
|
this.methods[name].executeIntoExpressRouter(
|
||||||
|
req.headers,
|
||||||
|
(req.get('Content-Type').toLowerCase().indexOf('json') != -1) ? req.body : new Object(),
|
||||||
|
req.params,
|
||||||
|
req.query,
|
||||||
|
(req.get('Content-Type').toLowerCase().indexOf('json') == -1) ? req.body : new Object(),
|
||||||
|
req.files,
|
||||||
|
req.cookies
|
||||||
|
)
|
||||||
|
);
|
||||||
|
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) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -36,10 +107,10 @@ class Main {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
server (mountPath = '/', options = null) {
|
server (mountPath = '/', sslOptions = null) {
|
||||||
let app = express();
|
let app = express();
|
||||||
|
|
||||||
app.use(mountPath, this.router());
|
app.use(mountPath, this.router(true));
|
||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,27 @@
|
|||||||
|
const typesApi = require('./types');
|
||||||
|
|
||||||
|
|
||||||
class Method {
|
class Method {
|
||||||
constructor (path, params) {
|
constructor (name, path, params) {
|
||||||
|
this.name = name;
|
||||||
this.path = path;
|
this.path = path;
|
||||||
this.paramsCompiles = params;
|
this.paramsCompiles = params;
|
||||||
|
this.groupsConnected = new Array();
|
||||||
|
this.allowedMethods = new Array();
|
||||||
|
// 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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
executeIntoExpressRouter (
|
executeIntoExpressRouter (
|
||||||
@ -21,7 +41,7 @@ class Method {
|
|||||||
for (let param in this.paramsCompiles) {
|
for (let param in this.paramsCompiles) {
|
||||||
paramScheme = {
|
paramScheme = {
|
||||||
required : false,
|
required : false,
|
||||||
type : require('./types').unknown,
|
type : typesApi.unknown,
|
||||||
allow_methods : ['get', 'post', 'put', 'delete'],
|
allow_methods : ['get', 'post', 'put', 'delete'],
|
||||||
allow_params : ['headers', 'json', 'params', 'query', 'body', 'files', 'cookies']
|
allow_params : ['headers', 'json', 'params', 'query', 'body', 'files', 'cookies']
|
||||||
};
|
};
|
||||||
@ -50,12 +70,25 @@ class Method {
|
|||||||
[isSyntax, convertedValue] = paramScheme.type(query[param], true);
|
[isSyntax, convertedValue] = paramScheme.type(query[param], true);
|
||||||
break;
|
break;
|
||||||
case 'cookies' :
|
case 'cookies' :
|
||||||
[isSyntax, convertedValue] = paramScheme.type(query[param], true);
|
[isSyntax, convertedValue] = paramScheme.type(cookies[param], true);
|
||||||
|
break;
|
||||||
|
case 'headers' :
|
||||||
|
[isSyntax, convertedValue] = paramScheme.type(headers[param], true);
|
||||||
break;
|
break;
|
||||||
case 'json' :
|
case 'json' :
|
||||||
[isSyntax, convertedValue] = paramScheme.type(query[param], false);
|
[isSyntax, convertedValue] = paramScheme.type(json[param], false);
|
||||||
|
break;
|
||||||
|
case 'params' :
|
||||||
|
[isSyntax, convertedValue] = paramScheme.type(params[param], false);
|
||||||
|
break;
|
||||||
|
case 'body' :
|
||||||
|
[isSyntax, convertedValue] = paramScheme.type(body[param], false);
|
||||||
|
break;
|
||||||
|
case 'files' :
|
||||||
|
[isSyntax, convertedValue] = paramScheme.type(files[param], false);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (isSyntax) break;
|
||||||
}
|
}
|
||||||
if (isSyntax) {
|
if (isSyntax) {
|
||||||
paramsEndless[param] = convertedValue;
|
paramsEndless[param] = convertedValue;
|
||||||
@ -85,7 +118,7 @@ class Method {
|
|||||||
for (let param in this.paramsCompiles) {
|
for (let param in this.paramsCompiles) {
|
||||||
paramScheme = {
|
paramScheme = {
|
||||||
required : false,
|
required : false,
|
||||||
type : require('./types').unknown,
|
type : typesApi.unknown,
|
||||||
allow_methods : ['get', 'post', 'put', 'delete'],
|
allow_methods : ['get', 'post', 'put', 'delete'],
|
||||||
allow_params : ['headers', 'json', 'params', 'query', 'body', 'files', 'cookies']
|
allow_params : ['headers', 'json', 'params', 'query', 'body', 'files', 'cookies']
|
||||||
};
|
};
|
||||||
@ -98,9 +131,34 @@ class Method {
|
|||||||
additional.missed.push(param);
|
additional.missed.push(param);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
[isSyntax, _] = paramScheme.type(params[param], false);
|
||||||
|
if (!isSyntax) {
|
||||||
|
if (paramScheme.required) {
|
||||||
|
required.unsyntax.push({param : param, description : this.typeError.split('{param}').join(param).split('{long_type}').join(paramScheme.type.long_name).split('{short_type}').join(paramScheme.type.short_name)});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
additional.unsyntax.push({param : param, description : this.typeError.split('{param}').join(param).split('{long_type}').join(paramScheme.type.long_name).split('{short_type}').join(paramScheme.type.short_name)});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (required.missed.length > 0 || required.unsyntax.length > 0 || additional.unsyntax.length > 0) {
|
||||||
|
throw this.paramsError(required, additional);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return this.execute(params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.execute(params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
group (groupClass) {
|
||||||
|
this.groupsConnected.push(groupClass);
|
||||||
|
}
|
||||||
|
|
||||||
execute (params) {}
|
execute (params) {}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,9 @@ module.exports = {
|
|||||||
unknown : {
|
unknown : {
|
||||||
long_name : 'unknown',
|
long_name : 'unknown',
|
||||||
short_name : 'unk',
|
short_name : 'unk',
|
||||||
syntax : (value, needs_convert = true) => throw new Error('Undefined datatype')
|
syntax : (value, needs_convert = true) => {
|
||||||
|
throw new Error('Undefined datatype');
|
||||||
|
}
|
||||||
},
|
},
|
||||||
string : {
|
string : {
|
||||||
long_name : 'string',
|
long_name : 'string',
|
||||||
|
3
index.js
3
index.js
@ -1,3 +1,6 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
Main : require('./components/Main'),
|
Main : require('./components/Main'),
|
||||||
|
Method : require('./components/Method'),
|
||||||
|
types : require('./components/types'),
|
||||||
|
Group : require('./components/Group')
|
||||||
};
|
};
|
1063
package-lock.json
generated
Normal file
1063
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
8
package.json
Normal file
8
package.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"dependencies": {
|
||||||
|
"body-parser": "^1.20.1",
|
||||||
|
"cookie-parser": "^1.4.6",
|
||||||
|
"express": "^4.18.2",
|
||||||
|
"https": "^1.0.0"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user