Refactoring how endpoints are specified so that the middlewares can be applied in a more generic fashion.
This commit is contained in:
parent
baf806275b
commit
6a416cad59
|
@ -10,6 +10,7 @@ const PermissionLevels = {
|
||||||
|
|
||||||
// these are the global types, that govern the underlying default behaviour
|
// these are the global types, that govern the underlying default behaviour
|
||||||
const PermissionTypes = {
|
const PermissionTypes = {
|
||||||
|
APP: "app",
|
||||||
TABLE: "table",
|
TABLE: "table",
|
||||||
USER: "user",
|
USER: "user",
|
||||||
AUTOMATION: "automation",
|
AUTOMATION: "automation",
|
||||||
|
|
|
@ -20,7 +20,6 @@ function fixRow(row, params) {
|
||||||
|
|
||||||
exports.search = async ctx => {
|
exports.search = async ctx => {
|
||||||
await rowController.search(ctx)
|
await rowController.search(ctx)
|
||||||
console.log(ctx.body)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.create = ctx => {
|
exports.create = ctx => {
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
const Router = require("@koa/router")
|
|
||||||
const controller = require("../../controllers/public/applications")
|
const controller = require("../../controllers/public/applications")
|
||||||
|
const Endpoint = require("./utils/Endpoint")
|
||||||
|
|
||||||
const router = Router()
|
const read = [],
|
||||||
|
write = []
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -34,7 +35,7 @@ const router = Router()
|
||||||
* applications:
|
* applications:
|
||||||
* $ref: '#/components/examples/applications'
|
* $ref: '#/components/examples/applications'
|
||||||
*/
|
*/
|
||||||
router.post("/applications/search", controller.search)
|
read.push(new Endpoint("post", "/applications/search", controller.search))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -62,7 +63,7 @@ router.post("/applications/search", controller.search)
|
||||||
* application:
|
* application:
|
||||||
* $ref: '#/components/examples/application'
|
* $ref: '#/components/examples/application'
|
||||||
*/
|
*/
|
||||||
router.post("/applications", controller.create)
|
write.push(new Endpoint("post", "/applications", controller.create))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -90,7 +91,7 @@ router.post("/applications", controller.create)
|
||||||
* application:
|
* application:
|
||||||
* $ref: '#/components/examples/application'
|
* $ref: '#/components/examples/application'
|
||||||
*/
|
*/
|
||||||
router.put("/applications/:appId", controller.update)
|
write.push(new Endpoint("put", "/applications/:appId", controller.update))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -112,7 +113,7 @@ router.put("/applications/:appId", controller.update)
|
||||||
* application:
|
* application:
|
||||||
* $ref: '#/components/examples/application'
|
* $ref: '#/components/examples/application'
|
||||||
*/
|
*/
|
||||||
router.delete("/applications/:appId", controller.delete)
|
write.push(new Endpoint("delete", "/applications/:appId", controller.delete))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -134,6 +135,6 @@ router.delete("/applications/:appId", controller.delete)
|
||||||
* application:
|
* application:
|
||||||
* $ref: '#/components/examples/application'
|
* $ref: '#/components/examples/application'
|
||||||
*/
|
*/
|
||||||
router.get("/applications/:appId", controller.read)
|
read.push(new Endpoint("get", "/applications/:appId", controller.read))
|
||||||
|
|
||||||
module.exports = router
|
module.exports = { read, write }
|
||||||
|
|
|
@ -1,22 +1,55 @@
|
||||||
const appRoute = require("./applications")
|
const appEndpoints = require("./applications")
|
||||||
const queryRoute = require("./queries")
|
const queryEndpoints = require("./queries")
|
||||||
const tableRoute = require("./tables")
|
const tableEndpoints = require("./tables")
|
||||||
const rowRoute = require("./rows")
|
const rowEndpoints = require("./rows")
|
||||||
const userRoute = require("./users")
|
const userEndpoints = require("./users")
|
||||||
const Router = require("@koa/router")
|
const Router = require("@koa/router")
|
||||||
const usage = require("../../../middleware/usageQuota")
|
const usage = require("../../../middleware/usageQuota")
|
||||||
|
const authorized = require("../../../middleware/authorized")
|
||||||
|
const {
|
||||||
|
paramResource,
|
||||||
|
paramSubResource,
|
||||||
|
} = require("../../../middleware/resourceId")
|
||||||
|
const {
|
||||||
|
PermissionLevels,
|
||||||
|
PermissionTypes,
|
||||||
|
} = require("@budibase/backend-core/permissions")
|
||||||
|
|
||||||
const PREFIX = "/api/public/v1"
|
const PREFIX = "/api/public/v1"
|
||||||
const ROUTES = [appRoute, queryRoute, tableRoute, rowRoute, userRoute]
|
|
||||||
|
|
||||||
const router = new Router({
|
const publicRouter = new Router({
|
||||||
prefix: PREFIX,
|
prefix: PREFIX,
|
||||||
})
|
})
|
||||||
for (let route of ROUTES) {
|
|
||||||
// apply usage to everything, middleware will work out whats needs it
|
function addMiddleware(endpoints, middleware) {
|
||||||
route.use(usage)
|
for (let endpoint of endpoints) {
|
||||||
router.use(route.routes())
|
endpoint.addMiddleware(middleware)
|
||||||
router.use(route.allowedMethods())
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = router
|
function addToRouter(endpoints) {
|
||||||
|
for (let endpoint of endpoints) {
|
||||||
|
endpoint.apply(publicRouter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyRoutes(endpoints, permType, resource, subResource = null) {
|
||||||
|
const paramMiddleware = subResource
|
||||||
|
? paramSubResource(resource, subResource)
|
||||||
|
: paramResource(resource)
|
||||||
|
addMiddleware(endpoints.read, paramMiddleware)
|
||||||
|
addMiddleware(endpoints.write, paramMiddleware)
|
||||||
|
addMiddleware(endpoints.read, authorized(permType, PermissionLevels.READ))
|
||||||
|
addMiddleware(endpoints.write, authorized(permType, PermissionLevels.WRITE))
|
||||||
|
addMiddleware(endpoints.write, usage)
|
||||||
|
addToRouter(endpoints.read)
|
||||||
|
addToRouter(endpoints.write)
|
||||||
|
}
|
||||||
|
|
||||||
|
applyRoutes(rowEndpoints, PermissionTypes.TABLE, "tableId", "rowId")
|
||||||
|
applyRoutes(appEndpoints, PermissionTypes.APP, "appId")
|
||||||
|
applyRoutes(tableEndpoints, PermissionTypes.TABLE, "tableId")
|
||||||
|
applyRoutes(userEndpoints, PermissionTypes.USER, "userId")
|
||||||
|
applyRoutes(queryEndpoints, PermissionTypes.QUERY, "queryId")
|
||||||
|
|
||||||
|
module.exports = publicRouter
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
const Router = require("@koa/router")
|
|
||||||
const controller = require("../../controllers/public/queries")
|
const controller = require("../../controllers/public/queries")
|
||||||
|
const Endpoint = require("./utils/Endpoint")
|
||||||
|
|
||||||
const router = Router()
|
const read = [],
|
||||||
|
write = []
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -34,7 +35,7 @@ const router = Router()
|
||||||
* queries:
|
* queries:
|
||||||
* $ref: '#/components/examples/queries'
|
* $ref: '#/components/examples/queries'
|
||||||
*/
|
*/
|
||||||
router.post("/queries/search", controller.search)
|
read.push(new Endpoint("post", "/queries/search", controller.search))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -59,6 +60,6 @@ router.post("/queries/search", controller.search)
|
||||||
* query:
|
* query:
|
||||||
* $ref: '#/components/examples/query'
|
* $ref: '#/components/examples/query'
|
||||||
*/
|
*/
|
||||||
router.post("/queries/:queryId", controller.execute)
|
write.push(new Endpoint("post", "/queries/:queryId", controller.execute))
|
||||||
|
|
||||||
module.exports = router
|
module.exports = { read, write }
|
||||||
|
|
|
@ -1,15 +1,8 @@
|
||||||
const Router = require("@koa/router")
|
|
||||||
const controller = require("../../controllers/public/rows")
|
const controller = require("../../controllers/public/rows")
|
||||||
const authorized = require("../../../middleware/authorized")
|
const Endpoint = require("./utils/Endpoint")
|
||||||
const { paramSubResource } = require("../../../middleware/resourceId")
|
|
||||||
const {
|
|
||||||
PermissionLevels,
|
|
||||||
PermissionTypes,
|
|
||||||
} = require("@budibase/backend-core/permissions")
|
|
||||||
|
|
||||||
const router = Router()
|
const read = [],
|
||||||
|
write = []
|
||||||
router.use(paramSubResource("tableId", "rowId"))
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -127,10 +120,8 @@ router.use(paramSubResource("tableId", "rowId"))
|
||||||
* search:
|
* search:
|
||||||
* $ref: '#/components/examples/rows'
|
* $ref: '#/components/examples/rows'
|
||||||
*/
|
*/
|
||||||
router.post(
|
read.push(
|
||||||
"/tables/:tableId/rows/search",
|
new Endpoint("post", "/tables/:tableId/rows/search", controller.search)
|
||||||
authorized(PermissionTypes.TABLE, PermissionLevels.READ),
|
|
||||||
controller.search
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -164,11 +155,7 @@ router.post(
|
||||||
* row:
|
* row:
|
||||||
* $ref: '#/components/examples/row'
|
* $ref: '#/components/examples/row'
|
||||||
*/
|
*/
|
||||||
router.post(
|
write.push(new Endpoint("post", "/tables/:tableId/rows", controller.create))
|
||||||
"/tables/:tableId/rows",
|
|
||||||
authorized(PermissionTypes.TABLE, PermissionLevels.WRITE),
|
|
||||||
controller.create
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -201,10 +188,8 @@ router.post(
|
||||||
* row:
|
* row:
|
||||||
* $ref: '#/components/examples/row'
|
* $ref: '#/components/examples/row'
|
||||||
*/
|
*/
|
||||||
router.put(
|
write.push(
|
||||||
"/tables/:tableId/rows/:rowId",
|
new Endpoint("put", "/tables/:tableId/rows/:rowId", controller.update)
|
||||||
authorized(PermissionTypes.TABLE, PermissionLevels.WRITE),
|
|
||||||
controller.update
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -229,10 +214,8 @@ router.put(
|
||||||
* row:
|
* row:
|
||||||
* $ref: '#/components/examples/row'
|
* $ref: '#/components/examples/row'
|
||||||
*/
|
*/
|
||||||
router.delete(
|
write.push(
|
||||||
"/tables/:tableId/rows/:rowId",
|
new Endpoint("delete", "/tables/:tableId/rows/:rowId", controller.delete)
|
||||||
authorized(PermissionTypes.TABLE, PermissionLevels.WRITE),
|
|
||||||
controller.delete
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -257,10 +240,6 @@ router.delete(
|
||||||
* row:
|
* row:
|
||||||
* $ref: '#/components/examples/row'
|
* $ref: '#/components/examples/row'
|
||||||
*/
|
*/
|
||||||
router.get(
|
read.push(new Endpoint("get", "/tables/:tableId/rows/:rowId", controller.read))
|
||||||
"/tables/:tableId/rows/:rowId",
|
|
||||||
authorized(PermissionTypes.TABLE, PermissionLevels.READ),
|
|
||||||
controller.read
|
|
||||||
)
|
|
||||||
|
|
||||||
module.exports = router
|
module.exports = { read, write }
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
const Router = require("@koa/router")
|
|
||||||
const controller = require("../../controllers/public/tables")
|
const controller = require("../../controllers/public/tables")
|
||||||
|
const Endpoint = require("./utils/Endpoint")
|
||||||
|
|
||||||
const router = Router()
|
const read = [],
|
||||||
|
write = []
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -34,7 +35,7 @@ const router = Router()
|
||||||
* tables:
|
* tables:
|
||||||
* $ref: '#/components/examples/tables'
|
* $ref: '#/components/examples/tables'
|
||||||
*/
|
*/
|
||||||
router.post("/tables/search", controller.search)
|
read.push(new Endpoint("post", "/tables/search", controller.search))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -65,7 +66,7 @@ router.post("/tables/search", controller.search)
|
||||||
* table:
|
* table:
|
||||||
* $ref: '#/components/examples/table'
|
* $ref: '#/components/examples/table'
|
||||||
*/
|
*/
|
||||||
router.post("/tables", controller.create)
|
write.push(new Endpoint("post", "/tables", controller.create))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -96,7 +97,7 @@ router.post("/tables", controller.create)
|
||||||
* table:
|
* table:
|
||||||
* $ref: '#/components/examples/table'
|
* $ref: '#/components/examples/table'
|
||||||
*/
|
*/
|
||||||
router.put("/tables/:tableId", controller.update)
|
write.push(new Endpoint("put", "/tables/:tableId", controller.update))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -119,7 +120,7 @@ router.put("/tables/:tableId", controller.update)
|
||||||
* table:
|
* table:
|
||||||
* $ref: '#/components/examples/table'
|
* $ref: '#/components/examples/table'
|
||||||
*/
|
*/
|
||||||
router.delete("/tables/:tableId", controller.delete)
|
write.push(new Endpoint("delete", "/tables/:tableId", controller.delete))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -142,6 +143,6 @@ router.delete("/tables/:tableId", controller.delete)
|
||||||
* table:
|
* table:
|
||||||
* $ref: '#/components/examples/table'
|
* $ref: '#/components/examples/table'
|
||||||
*/
|
*/
|
||||||
router.get("/tables/:tableId", controller.read)
|
read.push(new Endpoint("get", "/tables/:tableId", controller.read))
|
||||||
|
|
||||||
module.exports = router
|
module.exports = { read, write }
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
const Router = require("@koa/router")
|
|
||||||
const controller = require("../../controllers/public/users")
|
const controller = require("../../controllers/public/users")
|
||||||
|
const Endpoint = require("./utils/Endpoint")
|
||||||
|
|
||||||
const router = Router()
|
const read = [],
|
||||||
|
write = []
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -31,7 +32,7 @@ const router = Router()
|
||||||
* users:
|
* users:
|
||||||
* $ref: '#/components/examples/users'
|
* $ref: '#/components/examples/users'
|
||||||
*/
|
*/
|
||||||
router.post("/users/search", controller.search)
|
read.push(new Endpoint("post", "/users/search", controller.search))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -59,7 +60,7 @@ router.post("/users/search", controller.search)
|
||||||
* user:
|
* user:
|
||||||
* $ref: '#/components/examples/user'
|
* $ref: '#/components/examples/user'
|
||||||
*/
|
*/
|
||||||
router.post("/users", controller.create)
|
write.push(new Endpoint("post", "/users", controller.create))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -88,7 +89,7 @@ router.post("/users", controller.create)
|
||||||
* user:
|
* user:
|
||||||
* $ref: '#/components/examples/user'
|
* $ref: '#/components/examples/user'
|
||||||
*/
|
*/
|
||||||
router.put("/users/:userId", controller.update)
|
write.push(new Endpoint("put", "/users/:userId", controller.update))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -111,7 +112,7 @@ router.put("/users/:userId", controller.update)
|
||||||
* user:
|
* user:
|
||||||
* $ref: '#/components/examples/user'
|
* $ref: '#/components/examples/user'
|
||||||
*/
|
*/
|
||||||
router.delete("/users/:userId", controller.delete)
|
write.push(new Endpoint("delete", "/users/:userId", controller.delete))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @openapi
|
* @openapi
|
||||||
|
@ -134,6 +135,6 @@ router.delete("/users/:userId", controller.delete)
|
||||||
* user:
|
* user:
|
||||||
* $ref: '#/components/examples/user'
|
* $ref: '#/components/examples/user'
|
||||||
*/
|
*/
|
||||||
router.get("/users/:userId", controller.read)
|
read.push(new Endpoint("get", "/users/:userId", controller.read))
|
||||||
|
|
||||||
module.exports = router
|
module.exports = { read, write }
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
class Endpoint {
|
||||||
|
constructor(method, url, controller) {
|
||||||
|
this.method = method
|
||||||
|
this.url = url
|
||||||
|
this.controller = controller
|
||||||
|
this.middlewares = []
|
||||||
|
}
|
||||||
|
|
||||||
|
addMiddleware(middleware) {
|
||||||
|
this.middlewares.push(middleware)
|
||||||
|
}
|
||||||
|
|
||||||
|
apply(router) {
|
||||||
|
const method = this.method,
|
||||||
|
url = this.url
|
||||||
|
const middlewares = this.middlewares,
|
||||||
|
controller = this.controller
|
||||||
|
const params = [url, ...middlewares, controller]
|
||||||
|
router[method](...params)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Endpoint
|
Loading…
Reference in New Issue