UNPKG

@egodigital/egoose

Version:

Helper classes and functions for Node.js 10 or later.

124 lines 4.68 kB
"use strict"; /** * This file is part of the @egodigital/egoose distribution. * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/) * * @egodigital/egoose is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, version 3. * * @egodigital/egoose is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.registerStatisticsEndpoint = void 0; const index_1 = require("../index"); const index_2 = require("../apis/index"); /** * Registers an API endpoint for providing statistics. * * @param {express.Express | express.Router} hostOrRouter The host or router. * @param {RegisterStatisticsEndpointOptions} opts The options for the registration. */ function registerStatisticsEndpoint(hostOrRouter, opts) { let rootName = index_1.toStringSafe(opts.rootName) .trim(); if ('' === rootName) { rootName = 'stats'; } hostOrRouter.get(`/${rootName}/:name`, async function (req, res) { const CONTEXT = { request: req, response: res, value: {}, }; try { let authorized = true; if (opts.authorizer) { authorized = index_1.toBooleanSafe(await Promise.resolve(opts.authorizer(CONTEXT))); } if (!authorized) { return index_2.sendResponse(CONTEXT.response, { success: false, }, { code: 401, }); } let err; try { if (opts.beforeRequest) { await Promise.resolve(opts.beforeRequest(CONTEXT)); } // offset let offset = parseInt(index_1.toStringSafe(req.query['o']).trim()); // limit let limit = parseInt(index_1.toStringSafe(req.query['l']).trim()); const NAME = index_1.normalizeString(req.params['name']); if ('' !== NAME) { const PROVIDER = await Promise.resolve(opts.providerDetector(NAME, CONTEXT)); if (PROVIDER) { const RESULT = await PROVIDER.load({ limit: limit, offset: offset, parameters: toStatisticParameters(req.query), }); let handler = opts.responseHandler; if (!handler) { // use default handler = (result, ctx) => { return index_2.sendResponse(ctx.response, { success: true, data: result, }); }; } return await Promise.resolve(handler({ hasMore: RESULT.hasMore, offset: RESULT.offset, rows: RESULT.rows, totalCount: RESULT.totalCount, }, CONTEXT)); } } // not found return index_2.sendResponse(CONTEXT.response, { success: false, }, { code: 404, }); } catch (e) { err = e; throw e; } finally { if (opts.afterRequest) { await Promise.resolve(opts.afterRequest(err, CONTEXT)); } } } catch (e) { return index_2.sendResponse(CONTEXT.response, { success: false, }, { code: 500, }); } }); } exports.registerStatisticsEndpoint = registerStatisticsEndpoint; function toStatisticParameters(query) { const PARAMS = {}; if (query) { for (const P in query) { PARAMS[index_1.normalizeString(P)] = query[P]; } } return PARAMS; } //# sourceMappingURL=statistics.js.map