2021-02-08 18:22:07 +01:00
|
|
|
class ResourceIdGetter {
|
|
|
|
constructor(ctxProperty) {
|
|
|
|
this.parameter = ctxProperty
|
|
|
|
this.main = null
|
|
|
|
this.sub = null
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
mainResource(field) {
|
|
|
|
this.main = field
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
subResource(field) {
|
|
|
|
this.sub = field
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
build() {
|
|
|
|
const parameter = this.parameter,
|
|
|
|
main = this.main,
|
|
|
|
sub = this.sub
|
|
|
|
return (ctx, next) => {
|
2021-02-09 14:01:45 +01:00
|
|
|
const request = ctx.request[parameter] || ctx[parameter]
|
|
|
|
if (request == null) {
|
|
|
|
return next()
|
2021-02-08 18:22:07 +01:00
|
|
|
}
|
2021-02-09 14:01:45 +01:00
|
|
|
if (main != null && request[main]) {
|
|
|
|
ctx.resourceId = request[main]
|
2021-02-08 18:22:07 +01:00
|
|
|
}
|
2021-02-09 14:01:45 +01:00
|
|
|
if (sub != null && request[sub]) {
|
|
|
|
ctx.subResourceId = request[sub]
|
|
|
|
}
|
|
|
|
return next()
|
2021-02-08 18:22:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 13:39:32 +01:00
|
|
|
module.exports.ResourceIdGetter = ResourceIdGetter
|
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
module.exports.paramResource = main => {
|
2021-02-08 18:22:07 +01:00
|
|
|
return new ResourceIdGetter("params").mainResource(main).build()
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.paramSubResource = (main, sub) => {
|
|
|
|
return new ResourceIdGetter("params")
|
|
|
|
.mainResource(main)
|
|
|
|
.subResource(sub)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
module.exports.bodyResource = main => {
|
2021-02-08 18:22:07 +01:00
|
|
|
return new ResourceIdGetter("body").mainResource(main).build()
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.bodySubResource = (main, sub) => {
|
|
|
|
return new ResourceIdGetter("body")
|
|
|
|
.mainResource(main)
|
|
|
|
.subResource(sub)
|
|
|
|
.build()
|
|
|
|
}
|