UNPKG

4.63 kBJavaScriptView Raw
1"use strict";
2/**
3 * This file is part of the @egodigital/egoose distribution.
4 * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
5 *
6 * @egodigital/egoose is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * @egodigital/egoose is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19const index_1 = require("../index");
20const index_2 = require("../apis/index");
21/**
22 * Registers an API endpoint for providing statistics.
23 *
24 * @param {express.Express | express.Router} hostOrRouter The host or router.
25 * @param {RegisterStatisticsEndpointOptions} opts The options for the registration.
26 */
27function registerStatisticsEndpoint(hostOrRouter, opts) {
28 let rootName = index_1.toStringSafe(opts.rootName)
29 .trim();
30 if ('' === rootName) {
31 rootName = 'stats';
32 }
33 hostOrRouter.get(`/${rootName}/:name`, async function (req, res) {
34 const CONTEXT = {
35 request: req,
36 response: res,
37 value: {},
38 };
39 try {
40 let authorized = true;
41 if (opts.authorizer) {
42 authorized = index_1.toBooleanSafe(await Promise.resolve(opts.authorizer(CONTEXT)));
43 }
44 if (!authorized) {
45 return index_2.sendResponse(CONTEXT.response, {
46 success: false,
47 }, {
48 code: 401,
49 });
50 }
51 let err;
52 try {
53 if (opts.beforeRequest) {
54 await Promise.resolve(opts.beforeRequest(CONTEXT));
55 }
56 // offset
57 let offset = parseInt(index_1.toStringSafe(req.query['o']).trim());
58 // limit
59 let limit = parseInt(index_1.toStringSafe(req.query['l']).trim());
60 const NAME = index_1.normalizeString(req.params['name']);
61 if ('' !== NAME) {
62 const PROVIDER = await Promise.resolve(opts.providerDetector(NAME, CONTEXT));
63 if (PROVIDER) {
64 const RESULT = await PROVIDER.load({
65 limit: limit,
66 offset: offset,
67 parameters: toStatisticParameters(req.query),
68 });
69 let handler = opts.responseHandler;
70 if (!handler) {
71 // use default
72 handler = (result, ctx) => {
73 return index_2.sendResponse(ctx.response, {
74 success: true,
75 data: result,
76 });
77 };
78 }
79 return await Promise.resolve(handler({
80 hasMore: RESULT.hasMore,
81 offset: RESULT.offset,
82 rows: RESULT.rows,
83 totalCount: RESULT.totalCount,
84 }, CONTEXT));
85 }
86 }
87 // not found
88 return index_2.sendResponse(CONTEXT.response, {
89 success: false,
90 }, {
91 code: 404,
92 });
93 }
94 catch (e) {
95 err = e;
96 throw e;
97 }
98 finally {
99 if (opts.afterRequest) {
100 await Promise.resolve(opts.afterRequest(err, CONTEXT));
101 }
102 }
103 }
104 catch (e) {
105 return index_2.sendResponse(CONTEXT.response, {
106 success: false,
107 }, {
108 code: 500,
109 });
110 }
111 });
112}
113exports.registerStatisticsEndpoint = registerStatisticsEndpoint;
114function toStatisticParameters(query) {
115 const PARAMS = {};
116 if (query) {
117 for (const P in query) {
118 PARAMS[index_1.normalizeString(P)] = query[P];
119 }
120 }
121 return PARAMS;
122}
123//# sourceMappingURL=statistics.js.map
\No newline at end of file