From a6e93c895c6f2a79b6aa6233f16edff34e840427 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Thu, 8 Feb 2024 11:03:54 +0100 Subject: [PATCH] chore(xo-server/rest-api): unify collections handling --- packages/xo-server/src/xo-mixins/rest-api.mjs | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/packages/xo-server/src/xo-mixins/rest-api.mjs b/packages/xo-server/src/xo-mixins/rest-api.mjs index 98692d9e1..e4848966b 100644 --- a/packages/xo-server/src/xo-mixins/rest-api.mjs +++ b/packages/xo-server/src/xo-mixins/rest-api.mjs @@ -100,6 +100,17 @@ async function sendObjects(iterable, req, res, path = req.path) { return pipeline(makeObjectsStream(iterable, makeResult, json, res), res) } +function handleArray(array, filter, limit) { + if (filter !== undefined) { + array = array.filter(filter) + } + if (limit < array.length) { + array.length = limit + } + + return array +} + const handleOptionalUserFilter = filter => filter && CM.parse(filter).createPredicate() const subRouter = (app, path) => { @@ -191,10 +202,16 @@ export default class RestApi { return object } + function getObjects(filter, limit) { + return app.getObjects({ + filter: every(this.isCorrectType, filter), + limit, + }) + } for (const type of types) { const id = type.toLocaleLowerCase() + 's' - collections[id] = { getObject, isCorrectType: _ => _.type === type, type } + collections[id] = { getObject, getObjects, isCorrectType: _ => _.type === type, type } } collections.hosts.routes = { @@ -308,6 +325,9 @@ export default class RestApi { getObject(id) { return app.getUser(id).then(getUserPublicProperties) }, + async getObjects(filter, limit) { + return handleArray(await app.getAllUsers(), filter, limit) + }, } // normalize collections @@ -504,32 +524,12 @@ export default class RestApi { }, true) ) - api.get( - '/users', - wrap(async (req, res) => { - let users = await app.getAllUsers() - - const { filter, limit } = req.query - if (filter !== undefined) { - users = users.filter(CM.parse(filter).createPredicate()) - } - if (limit < users.length) { - users.length = limit - } - - sendObjects(users.map(getUserPublicProperties), req, res) - }) - ) - api.get( '/:collection', wrap(async (req, res) => { const { query } = req await sendObjects( - await app.getObjects({ - filter: every(req.collection.isCorrectType, handleOptionalUserFilter(query.filter)), - limit: ifDef(query.limit, Number), - }), + await req.collection.getObjects(handleOptionalUserFilter(query.filter), ifDef(query.limit, Number)), req, res )