mirror of
https://git.sr.ht/~cadence/cloudtube
synced 2026-05-26 12:32:25 +00:00
Move utils folder and fix published text
This commit is contained in:
parent
643f1e0889
commit
2e69dfc4b7
16 changed files with 53 additions and 619 deletions
83
utils/validate.js
Normal file
83
utils/validate.js
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
class V {
|
||||
constructor() {
|
||||
this.chain = []
|
||||
this.state = {}
|
||||
this.finished = false
|
||||
this.endValue = null
|
||||
}
|
||||
|
||||
with(preset) {
|
||||
this.check(...preset)
|
||||
return this
|
||||
}
|
||||
|
||||
check(conditionCallback, elseCallback) {
|
||||
this.chain.push(() => {
|
||||
if (!conditionCallback(this.state)) this._end(elseCallback(this.state))
|
||||
})
|
||||
return this
|
||||
}
|
||||
|
||||
last(callback) {
|
||||
this.chain.push(() => {
|
||||
this._end(callback(this.state))
|
||||
})
|
||||
return this
|
||||
}
|
||||
|
||||
go() {
|
||||
for (const s of this.chain) {
|
||||
s()
|
||||
if (this.finished) return this.endValue
|
||||
}
|
||||
return {
|
||||
statusCode: 500,
|
||||
contentType: "application/json",
|
||||
content: {
|
||||
error: "Reached end of V chain without response"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_end(value) {
|
||||
this.finished = true
|
||||
this.endValue = value
|
||||
}
|
||||
}
|
||||
|
||||
function presetLoad(additions) {
|
||||
return [
|
||||
state => {
|
||||
Object.assign(state, additions)
|
||||
return true
|
||||
},
|
||||
null
|
||||
]
|
||||
}
|
||||
|
||||
function presetURLParamsBody() {
|
||||
return [
|
||||
state => {
|
||||
try {
|
||||
state.params = new URLSearchParams(state.body.toString())
|
||||
return true
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
return false
|
||||
}
|
||||
},
|
||||
() => {
|
||||
return {
|
||||
statusCode: 400,
|
||||
contentType: "application/json",
|
||||
content: {
|
||||
error: "Could not parse body as URLSearchParams"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
module.exports.V = V
|
||||
module.exports.presetLoad = presetLoad
|
||||
module.exports.presetURLParamsBody = presetURLParamsBody
|
||||
Loading…
Add table
Add a link
Reference in a new issue