api-service-core
Version:
NodeJS api-service
53 lines • 1.93 kB
JavaScript
;
/*************************************************************************
*
* Troven CONFIDENTIAL
* __________________
*
* (c) 2017-2019 Troven Pty Ltd
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Troven Pty Ltd and its licensors,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Troven Pty Ltd
* and its suppliers and may be covered by International and Regional Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Troven Pty Ltd.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const _ = require("lodash");
/**
* Sort-By
* ---------
* return the request as the response
*
* @type {{name: string, description: string, title: string, defaults: {path: string}, fn: module.exports.fn}}
*/
class echo {
constructor() {
this.name = "api.sortby";
this.title = "return the response, sorted by an Array of 'fields' and an optional 'order' Array [ASC, DESC]";
}
fn(operation, _options) {
false && operation;
return function (req, res) {
let options = _.extend({ fields: [], order: [] }, req.query, _options, operation.feature);
let sort_fields = options.fields;
if (!sort_fields)
sort_fields = req.query.fields.split(",");
let sort_order = options.order;
if (!sort_order)
sort_order = req.query.order.split(",");
let payload = req.json || req.body;
let sorted = _.orderBy(payload, sort_fields, sort_order);
res.status(200);
res.send(sorted);
};
}
}
exports.default = echo;
;
//# sourceMappingURL=sortby.js.map