From 5c3dda0daa375af44cb46943a1e488bc720ed66e Mon Sep 17 00:00:00 2001 From: FullGreaM Date: Thu, 6 Oct 2022 14:55:40 +0300 Subject: [PATCH] upload files into repos --- codeExample.js | 1 + components/Main.js | 2 +- components/Method.js | 84 +++++++++++++++++++++++++++++++++++++++----- components/types.js | 14 ++++++-- 4 files changed, 88 insertions(+), 13 deletions(-) diff --git a/codeExample.js b/codeExample.js index cd070cf..8dcf985 100644 --- a/codeExample.js +++ b/codeExample.js @@ -34,6 +34,7 @@ server.resposne = (response) => ({ code: 200 }); server.paramsError = (required, additional) => ({ required, additional }); +server.typeError = 'param {param} must be only {long_type} ({short_type})'; // Создаём класс группы методов class ExampleMethodGroup extends backend.Group { diff --git a/components/Main.js b/components/Main.js index b25913b..bcea716 100644 --- a/components/Main.js +++ b/components/Main.js @@ -14,7 +14,7 @@ class Main { } call (method, params) { - return this.methods[method.path].execute(params); + return this.methods[method.path].pre_execute(params); } router (returnMiddlewareFunction = true, middlewareFunction = (req, res, next) => next()) { diff --git a/components/Method.js b/components/Method.js index 9be7664..e47f85d 100644 --- a/components/Method.js +++ b/components/Method.js @@ -4,7 +4,7 @@ class Method { this.paramsCompiles = params; } - compileParams ( + executeIntoExpressRouter ( headers, json, params, @@ -13,13 +13,15 @@ class Method { files, cookies ) { + let paramsEndless = new Object(); let paramScheme; - let required = { missed : [], unsyntax : [] }; + let required = { missed : [], unsyntax : [] }; let additional = { missed : [], unsyntax : [] }; let checkKeys = new Array(); for (let param in this.paramsCompiles) { paramScheme = { required : false, + type : require('./types').unknown, allow_methods : ['get', 'post', 'put', 'delete'], allow_params : ['headers', 'json', 'params', 'query', 'body', 'files', 'cookies'] }; @@ -27,16 +29,80 @@ class Method { for (let key in this.paramsCompiles[param]) { paramScheme[key] = this.paramsCompiles[param][key]; } // check missible if (headers[param] !== undefined & paramScheme.allow_params.indexOf('headers') != -1) { checkKeys.push('headers'); } - if (json[param] !== undefined) { checkKeys.push('json'); } - if (query[param] !== undefined) { checkKeys.push('query'); } - if (body[param] !== undefined) { checkKeys.push('body'); } - if (files[param] !== undefined) { checkKeys.push('files'); } - if (cookies[param] !== undefined) { checkKeys.push('cookies'); } - if (params[param] !== undefined) { checkKeys.push('params'); } + if (json[param] !== undefined & paramScheme.allow_params.indexOf('json') != -1) { checkKeys.push('json'); } + if (query[param] !== undefined & paramScheme.allow_params.indexOf('query') != -1) { checkKeys.push('query'); } + if (body[param] !== undefined & paramScheme.allow_params.indexOf('body') != -1) { checkKeys.push('body'); } + if (files[param] !== undefined & paramScheme.allow_params.indexOf('files') != -1) { checkKeys.push('files'); } + if (cookies[param] !== undefined & paramScheme.allow_params.indexOf('cookies') != -1) { checkKeys.push('cookies'); } + if (params[param] !== undefined & paramScheme.allow_params.indexOf('params') != -1) { checkKeys.push('params'); } + + if (checkKeys.length == 0) { + if (paramScheme.required) { required.missed.push(param); } + else { additional.missed.push(param); } + } + else { + checkKeys = checkKeys.sort((a, b) => Number(b == 'query' || b == 'cookies') - Number(a == 'query' || a == 'cookies')); + let isSyntax; + let convertedValue; + for (let key in checkKeys) { + switch (key) { + case 'query' : + [isSyntax, convertedValue] = paramScheme.type(query[param], true); + break; + case 'cookies' : + [isSyntax, convertedValue] = paramScheme.type(query[param], true); + break; + case 'json' : + [isSyntax, convertedValue] = paramScheme.type(query[param], false); + break; + } + } + if (isSyntax) { + paramsEndless[param] = convertedValue; + } + else { + 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.pre_execute(paramsEndless, false); } } - execute () {} + pre_execute (params, needsChecking = true) { + if (needsChecking) { + let required = { missed : [], unsyntax : [] }; + let additional = { missed : [], unsyntax : [] }; + let isSyntax; + let _; + let paramScheme; + + for (let param in this.paramsCompiles) { + paramScheme = { + required : false, + type : require('./types').unknown, + allow_methods : ['get', 'post', 'put', 'delete'], + allow_params : ['headers', 'json', 'params', 'query', 'body', 'files', 'cookies'] + }; + for (let key in this.paramsCompiles[param]) { paramScheme[key] = this.paramsCompiles[param][key]; } + if (params[param] === undefined) { + if (paramScheme.required) { + required.missed.push(param); + } + else { + additional.missed.push(param); + } + } + } + } + } + + execute (params) {} } diff --git a/components/types.js b/components/types.js index 24c8fa5..d9b6180 100644 --- a/components/types.js +++ b/components/types.js @@ -1,13 +1,21 @@ module.exports = { + unknown : { + long_name : 'unknown', + short_name : 'unk', + syntax : (value, needs_convert = true) => throw new Error('Undefined datatype') + }, string : { - long_name : 'string', + long_name : 'string', short_name : 'str', - syntax : (value, needs_convert = true) => { + syntax : (value, needs_convert = false) => { if (typeof(value) == 'string') { return true, value; } + else if (needs_convert) { + return true, needs_convert.toString() + } else { - return false, needs_convert ? value.toString() : undefined; + return false, undefined; } } }