diff --git a/src/api/xo.js b/src/api/xo.js index eac87152c..a049fe8b5 100644 --- a/src/api/xo.js +++ b/src/api/xo.js @@ -28,13 +28,18 @@ exportConfig.permission = 'admin' // ------------------------------------------------------------------- -export function getAllObjects () { - return this.getObjects() +export function getAllObjects ({ filter, limit }) { + return this.getObjects({ filter, limit }) } getAllObjects.permission = '' getAllObjects.description = 'Returns all XO objects' +getAllObjects.params = { + filter: { type: 'object', optional: true }, + limit: { type: 'number', optional: true } +} + // ------------------------------------------------------------------- export async function importConfig () { diff --git a/src/xo-mixins/api.js b/src/xo-mixins/api.js index f0f105706..cd6e061ac 100644 --- a/src/xo-mixins/api.js +++ b/src/xo-mixins/api.js @@ -210,7 +210,7 @@ export default class Api { return remove } - async callApiMethod (session, name, params) { + async callApiMethod (session, name, params = {}) { const startTime = Date.now() const method = this._methods[name] diff --git a/src/xo.js b/src/xo.js index ab05747a1..62c507b29 100644 --- a/src/xo.js +++ b/src/xo.js @@ -1,9 +1,18 @@ -import includes from 'lodash/includes' import XoCollection from 'xo-collection' import XoUniqueIndex from 'xo-collection/unique-index' import {createClient as createRedisClient} from 'redis' import {EventEmitter} from 'events' import { noSuchObject } from 'xo-common/api-errors' +import { + forEach, + includes, + isEmpty, + isFunction, + isString, + iteratee, + map as mapToArray, + stubTrue +} from 'lodash' import mixins from './xo-mixins' import Connection from './connection' @@ -13,12 +22,7 @@ import { } from './decorators' import { createRawObject, - forEach, generateToken, - isEmpty, - isFunction, - isString, - mapToArray, noop } from './utils' @@ -160,8 +164,32 @@ export default class Xo extends EventEmitter { return obj } - getObjects () { - return this._objects.all + getObjects ({ filter, limit } = {}) { + const { all } = this._objects + + if (filter === undefined) { + if (limit === undefined || limit === Infinity) { + return all + } + filter = stubTrue + } else { + filter = iteratee(filter) + if (limit === undefined) { + limit = Infinity + } + } + + const results = createRawObject(null) + for (const id in all) { + const object = all[id] + if (filter(object, id, all)) { + if (limit-- <= 0) { + break + } + results[id] = object + } + } + return results } // -----------------------------------------------------------------