mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 10:34:17 +00:00
refactor lib
This commit is contained in:
parent
4d86d9cee3
commit
166a113467
11
packages/server/src/lib/additionsHandler/handlers/essc.js
Normal file
11
packages/server/src/lib/additionsHandler/handlers/essc.js
Normal file
@ -0,0 +1,11 @@
|
||||
import { genV1 } from "../../essc"
|
||||
|
||||
export default (obj) => {
|
||||
obj.essc = genV1({
|
||||
type: obj.vaultItemTypeSelector ?? obj.type,
|
||||
serial: obj.vaultItemSerial ?? obj.serial,
|
||||
manufacturer: obj.vaultItemManufacturer ?? obj.manufacturer,
|
||||
})
|
||||
|
||||
return obj
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
export default (obj) => {
|
||||
return {
|
||||
...obj,
|
||||
...obj.properties,
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
export default (obj) => {
|
||||
const fixedKeys = {
|
||||
vaultItemManufacturer: "manufacturer",
|
||||
vaultItemSerial: "serial",
|
||||
vaultItemTypeSelector: "type",
|
||||
vaultItemManufacturedYear: "manufacturedYear",
|
||||
}
|
||||
|
||||
Object.keys(obj).forEach(key => {
|
||||
if (fixedKeys[key]) {
|
||||
obj[fixedKeys[key]] = obj[key]
|
||||
delete obj[key]
|
||||
}
|
||||
})
|
||||
|
||||
return obj
|
||||
}
|
22
packages/server/src/lib/additionsHandler/index.js
Normal file
22
packages/server/src/lib/additionsHandler/index.js
Normal file
@ -0,0 +1,22 @@
|
||||
export default async (additions, obj) => {
|
||||
let query = []
|
||||
|
||||
if (Array.isArray(additions)) {
|
||||
query = additions
|
||||
}else {
|
||||
query.push(additions)
|
||||
}
|
||||
|
||||
for await(let addition of query) {
|
||||
try {
|
||||
let script = await import(`./handlers/${addition}.js`)
|
||||
script = script.default || script
|
||||
|
||||
obj = await script(obj)
|
||||
} catch (error) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return obj
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
export * as Token from './token'
|
||||
export { default as Schematized } from './schematized'
|
||||
export { default as selectValues } from './selectValues'
|
||||
export { default as additionsHandler } from './additionsHandler'
|
@ -1,20 +1,54 @@
|
||||
export default (schema, fn) => {
|
||||
export default (schema = {}, fn) => {
|
||||
return async (req, res, next) => {
|
||||
const missingKeys = []
|
||||
const requiredKeys = Array.isArray(schema) ? schema : []
|
||||
// if is nullish
|
||||
req.body = req.body ?? {}
|
||||
req.query = req.query ?? {}
|
||||
|
||||
if (schema.required) {
|
||||
if (Array.isArray(schema.required)) {
|
||||
const missingKeys = []
|
||||
const requiredKeys = Array.isArray(schema.required) ? schema.required : []
|
||||
|
||||
for await (let key of requiredKeys) {
|
||||
if (typeof req.body[key] === "undefined") {
|
||||
missingKeys.push(key)
|
||||
for await (let key of requiredKeys) {
|
||||
if (typeof req.body[key] === "undefined" && typeof req.query[key] === "undefined") {
|
||||
req.selection[key] = req.body[key]
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if (missingKeys.length > 0) {
|
||||
return res.status(400).json({ error: `Missing ${missingKeys}` })
|
||||
}
|
||||
} else {
|
||||
console.warn("[INVALID SCHEMA] schema.required is defined but is not an array")
|
||||
}
|
||||
}
|
||||
|
||||
if (missingKeys.length > 0) {
|
||||
return res.status(400).json({ error: `Missing required keys > ${missingKeys}` })
|
||||
} else {
|
||||
if (typeof fn === "function") {
|
||||
return await fn(req, res, next)
|
||||
if (schema.select) {
|
||||
if (Array.isArray(schema.select)) {
|
||||
if (typeof req.selection !== "object") {
|
||||
req.selection = {}
|
||||
}
|
||||
|
||||
// assign objects along request body and query.
|
||||
for await (let key of schema.select) {
|
||||
if (req.body && typeof req.body[key] !== "undefined") {
|
||||
req.selection[key] = req.body[key]
|
||||
continue
|
||||
}
|
||||
|
||||
if (req.query && typeof req.query[key] !== "undefined") {
|
||||
req.selection[key] = req.query[key]
|
||||
continue
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.warn("[INVALID SCHEMA] schema.select is defined but is not an array")
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof fn === "function") {
|
||||
return await fn(req, res, next)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
export default (query = [], fn) => {
|
||||
return async (req, res, next) => {
|
||||
if (typeof fn === "function") {
|
||||
const obj = {}
|
||||
|
||||
if (!req.body) {
|
||||
req.body = {}
|
||||
}
|
||||
if (!req.query) {
|
||||
req.query = {}
|
||||
}
|
||||
|
||||
if (Array.isArray(query)) {
|
||||
query.forEach(key => {
|
||||
const value = req.body[key] ?? req.query[key]
|
||||
if (typeof value !== "undefined") {
|
||||
obj[key] = value
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
req.selectedValues = obj
|
||||
|
||||
return await fn(req, res, next)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user