generated from idrinth-api-bench/project-defaults
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 565 complexity reduction (#11)
* refactor validation fixes idrinth-api-bench/issues#565 * refactor validation fixes idrinth-api-bench/issues#565 * fix merge fixes idrinth-api-bench/issues#565 * fix merge fixes idrinth-api-bench/issues#565
- Loading branch information
Showing
11 changed files
with
205 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/node_modules | ||
/coverage | ||
/.idea | ||
/node_modules | ||
/src/locales | ||
/coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Task from '../task.js'; | ||
import error from './error.js'; | ||
|
||
const checkMiddleware = (type: 'pre' | 'post', route: Task,) => { | ||
if (typeof route[type] === 'undefined') { | ||
return true; | ||
} | ||
const data = route[type]; | ||
delete route[type]; | ||
if (typeof data !== 'object' || ! Array.isArray(data,)) { | ||
error(`invalid_${ type }_definition`, route.id,); | ||
return false; | ||
} | ||
for (const middleware of data) { | ||
if (typeof middleware !== 'string') { | ||
error(`invalid_${ type }_definition`, route.id,); | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
export default checkMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import Request from '../request.js'; | ||
import error from './error.js'; | ||
import { | ||
EMPTY, | ||
} from '../constants.js'; | ||
import warn from './warn.js'; | ||
|
||
interface Property { | ||
name: string; | ||
type: string|string[]; | ||
required: boolean; | ||
} | ||
|
||
const properties: Property[] = [ | ||
{ | ||
name: 'method', | ||
type: 'string', | ||
required: true, | ||
}, | ||
{ | ||
name: 'headers', | ||
type: 'object', | ||
required: false, | ||
}, | ||
{ | ||
name: 'cookies', | ||
type: 'object', | ||
required: false, | ||
}, | ||
{ | ||
name: 'body', | ||
type: [ | ||
'string', | ||
'object', | ||
], | ||
required: false, | ||
}, | ||
{ | ||
name: 'autohandle', | ||
type: 'string', | ||
required: false, | ||
}, | ||
{ | ||
name: 'url', | ||
type: 'string', | ||
required: true, | ||
}, | ||
{ | ||
name: 'maxDuration', | ||
type: 'number', | ||
required: false, | ||
}, | ||
]; | ||
|
||
const validateProperty = (property: Property, id: string, main: Request,) => { | ||
if (property.required && typeof main[property.name] === 'undefined') { | ||
error('invalid_request_property', id, property.name,); | ||
return false; | ||
} | ||
const allowedTypes: string[] = Array.isArray(property.type,) ? property.type : [ property.type, ]; | ||
if (! allowedTypes.includes(typeof main[property.name],)) { | ||
error('invalid_request_property', id, property.name,); | ||
delete main[property.name]; | ||
return false; | ||
} | ||
delete main[property.name]; | ||
return true; | ||
}; | ||
const checkRequest = (main: Request, id: string,): { | ||
invalid: boolean, | ||
risky: boolean, | ||
} => { | ||
let valid = true; | ||
for (const property of properties) { | ||
valid = validateProperty(property, id, main,) && valid; | ||
} | ||
if (Object.keys(main,).length !== EMPTY) { | ||
warn('invalid_request', id,); | ||
return { | ||
invalid: ! valid, | ||
risky: true, | ||
}; | ||
} | ||
return { | ||
invalid: ! valid, | ||
risky: false, | ||
}; | ||
}; | ||
export default checkRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Job from '../job.js'; | ||
import taskType from '../task-type.js'; | ||
import { | ||
EMPTY, | ||
} from '../constants.js'; | ||
import checkRequest from './check-request.js'; | ||
import warn from './warn.js'; | ||
import checkMiddleware from './check-middleware.js'; | ||
|
||
const checkType = (job: Job, type: taskType,) => { | ||
if (typeof job[type] === 'undefined') { | ||
return { | ||
errors: 0, | ||
warnings: 0, | ||
}; | ||
} | ||
let errors = 0; | ||
let warnings = 0; | ||
if (job[type].length > EMPTY) { | ||
for (const route of job[type]) { | ||
if (! checkMiddleware('pre', route,)) { | ||
errors ++; | ||
} | ||
if (! checkMiddleware('post', route,)) { | ||
errors ++; | ||
} | ||
const id = route.id; | ||
delete route.id; | ||
const result = checkRequest(route.main, id,); | ||
if (result.invalid) { | ||
errors ++; | ||
} | ||
if (result.risky) { | ||
warnings ++; | ||
} | ||
delete route.main; | ||
for (const key of Object.keys(route,)) { | ||
warn('unknown_route_property', key, id,); | ||
warnings ++; | ||
} | ||
} | ||
} | ||
return { | ||
errors, | ||
warnings, | ||
}; | ||
}; | ||
|
||
export default checkType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* eslint no-console: 0 */ | ||
import language from '../helper/language.js'; | ||
import logSymbols from 'log-symbols'; | ||
import languageKey from '../locales/language-key.js'; | ||
|
||
const error = (key: languageKey, ...argList: string[]) => { | ||
console.error(logSymbols.error + ' ' + language(key, ...argList,),); | ||
}; | ||
|
||
export default error; |
Oops, something went wrong.