From 21dccdc7171a92196f741d8a1760c7183142ba6d Mon Sep 17 00:00:00 2001 From: Nikiroy78 Date: Tue, 18 Oct 2022 08:08:30 +0300 Subject: [PATCH] connection /brigade-catalog --- codeExample.js | 8 ---- components/Method.js | 12 ++++++ package.json | 2 +- readme.md | 89 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 9 deletions(-) diff --git a/codeExample.js b/codeExample.js index dfd1593..136d1fd 100644 --- a/codeExample.js +++ b/codeExample.js @@ -2,14 +2,6 @@ const backend = require('./index'); // Создаём класс бэкенда, наследующий класс backend.Main class Main extends backend.Main { - session (params, sessionData) { // Настраиваем сессию (опционально) - sessionData._setValue('example', 1); // Задать значение - console.log(sessionData.example); // Получить значение из сессии - sessionData._remove('example'); // Убрать значение - return 1; // Успешно - throw 'Example error'; // Пример ошибки - }; - /* errorHandler (error) { return { mainbody : JSON.stringify({ error : {}, error }), headers : { diff --git a/components/Method.js b/components/Method.js index 47e2508..d4328f4 100644 --- a/components/Method.js +++ b/components/Method.js @@ -2,6 +2,9 @@ const typesApi = require('./types'); const Session = require('./Session'); +var errHandlers = new Object(); + + const formatMessage = (message, errorType, scheme, value, param) => { value = String(value); switch (errorType) { @@ -15,6 +18,8 @@ const formatMessage = (message, errorType, scheme, value, param) => { 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); } }; @@ -60,6 +65,13 @@ class Method { } } + setError (errorCode, message, handler=null) { + this.error[errorCode] = message; + if (!!handler) { + errHandlers[errorCode] = handler; + } + } + useDynamicType (condition) { this.isDynamic = !!condition; } diff --git a/package.json b/package.json index 14668a3..306cdd6 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "express-fileupload": "^1.4.0" }, "name": "njsbacker", - "version": "0.9.0", + "version": "0.9.1", "description": "Module for easy developing back-end projects at express.js", "main": "index.js", "devDependencies": {}, diff --git a/readme.md b/readme.md index e69de29..bfe2c86 100644 --- a/readme.md +++ b/readme.md @@ -0,0 +1,89 @@ +# 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. +![simple scheme of njsbacker work](https://sun9-71.userapi.com/impg/VjRpi90eSMsV05NPRU3GVNxwEbem8ITGjErioQ/lyD9S-F_qI4.jpg?size=726x341&quality=96&sign=d48f6eff6f6da9747675287096eb24b3&type=album) +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: +![simple scheme of njsbacker handling data an executing](https://sun9-78.userapi.com/impg/etOt-TcZ1KVxUnIm3laKJgAveXaMEOGfbGY0Wg/dIekdeOOKeo.jpg?size=649x353&quality=96&sign=958f40b23ab188ea03612bc98def05fe&type=album) +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 + } + } +} +``` +#### errorHadler +Hadling and format errors. +## Additional clases and objects +## Examples \ No newline at end of file