feat(xo.getAllObjects): accepts filter and limit params (#604)

This commit is contained in:
Julien Fontanet 2017-09-25 10:56:12 +02:00 committed by GitHub
parent 22772a5fac
commit 1b76ad4252
3 changed files with 44 additions and 11 deletions

View File

@ -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 () {

View File

@ -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]

View File

@ -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
}
// -----------------------------------------------------------------