2022-11-16 18:24:13 +01:00
|
|
|
import { BBContext } from "@budibase/types"
|
|
|
|
|
|
|
|
export class ResourceIdGetter {
|
|
|
|
parameter: string
|
|
|
|
main: string | null
|
|
|
|
sub: string | null
|
|
|
|
|
|
|
|
constructor(ctxProperty: string) {
|
2021-02-08 18:22:07 +01:00
|
|
|
this.parameter = ctxProperty
|
|
|
|
this.main = null
|
|
|
|
this.sub = null
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2022-11-16 18:24:13 +01:00
|
|
|
mainResource(field: string) {
|
2021-02-08 18:22:07 +01:00
|
|
|
this.main = field
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2022-11-16 18:24:13 +01:00
|
|
|
subResource(field: string) {
|
2021-02-08 18:22:07 +01:00
|
|
|
this.sub = field
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
build() {
|
|
|
|
const parameter = this.parameter,
|
|
|
|
main = this.main,
|
|
|
|
sub = this.sub
|
2022-11-16 18:24:13 +01:00
|
|
|
return (ctx: BBContext, next: any) => {
|
|
|
|
// @ts-ignore
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 09:11:54 +02:00
|
|
|
/** @deprecated we should use the authorizedResource middleware instead */
|
2022-11-16 18:24:13 +01:00
|
|
|
export function paramResource(main: string) {
|
2021-02-08 18:22:07 +01:00
|
|
|
return new ResourceIdGetter("params").mainResource(main).build()
|
|
|
|
}
|
|
|
|
|
2022-11-16 18:24:13 +01:00
|
|
|
export function paramSubResource(main: string, sub: string) {
|
2021-02-08 18:22:07 +01:00
|
|
|
return new ResourceIdGetter("params")
|
|
|
|
.mainResource(main)
|
|
|
|
.subResource(sub)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
2022-11-16 18:24:13 +01:00
|
|
|
export function bodyResource(main: string) {
|
2021-02-08 18:22:07 +01:00
|
|
|
return new ResourceIdGetter("body").mainResource(main).build()
|
|
|
|
}
|
|
|
|
|
2022-11-16 18:24:13 +01:00
|
|
|
export function bodySubResource(main: string, sub: string) {
|
2021-02-08 18:22:07 +01:00
|
|
|
return new ResourceIdGetter("body")
|
|
|
|
.mainResource(main)
|
|
|
|
.subResource(sub)
|
|
|
|
.build()
|
|
|
|
}
|