mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 19:14:16 +00:00
refactor lib
This commit is contained in:
parent
cbc791db35
commit
6f50106fd3
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 * as Token from './token'
|
||||||
export { default as Schematized } from './schematized'
|
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) => {
|
return async (req, res, next) => {
|
||||||
const missingKeys = []
|
// if is nullish
|
||||||
const requiredKeys = Array.isArray(schema) ? schema : []
|
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) {
|
for await (let key of requiredKeys) {
|
||||||
if (typeof req.body[key] === "undefined") {
|
if (typeof req.body[key] === "undefined" && typeof req.query[key] === "undefined") {
|
||||||
missingKeys.push(key)
|
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) {
|
if (schema.select) {
|
||||||
return res.status(400).json({ error: `Missing required keys > ${missingKeys}` })
|
if (Array.isArray(schema.select)) {
|
||||||
} else {
|
if (typeof req.selection !== "object") {
|
||||||
if (typeof fn === "function") {
|
req.selection = {}
|
||||||
return await fn(req, res, next)
|
}
|
||||||
|
|
||||||
|
// 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