UNPKG

4.21 kBTypeScriptView Raw
1/**
2 * This file is part of the @egodigital/egoose distribution.
3 * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
4 *
5 * @egodigital/egoose is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation, version 3.
8 *
9 * @egodigital/egoose is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17import { StatisticProvider, StatisticResultRow } from '../statistics/index';
18import * as express from 'express';
19/**
20 * Options for 'registerStatisticsEndpoint()' function.
21 */
22export interface RegisterStatisticsEndpointOptions {
23 /**
24 * A custom function that is invoked AFTER a request.
25 *
26 * @param {any} err The error (if occurred).
27 * @param {StatisticApiContext} context The context.
28 */
29 afterRequest?: (err: any, context: StatisticApiContext) => any;
30 /**
31 * A function that checks if a request is authorized to access the endpoint or not.
32 *
33 * @param {StatisticApiContext} context The context.
34 *
35 * @return {boolean|PromiseLike<boolean>} The result that indicates if request is authorized or not.
36 */
37 authorizer?: (context: StatisticApiContext) => boolean | PromiseLike<boolean>;
38 /**
39 * A custom function that is invoked BEFORE a request is handled.
40 *
41 * @param {StatisticApiContext} context The context.
42 */
43 beforeRequest?: (context: StatisticApiContext) => any;
44 /**
45 * A function that detects a statistic provider by name.
46 */
47 providerDetector: StatisticProviderDetector;
48 /**
49 * The custom name of API root.
50 */
51 rootName?: string;
52 /**
53 * A custom response handler.
54 *
55 * @param {StatisticProviderApiResult} result The result to handle.
56 * @param {StatisticApiContext} context The context.
57 *
58 * @return {express.Response|PromiseLike<express.Response>} The (new) response context.
59 */
60 responseHandler?: (result: StatisticProviderApiResult, context: StatisticApiContext) => express.Response | PromiseLike<express.Response>;
61}
62/**
63 * An statistic API request context.
64 */
65export interface StatisticApiContext {
66 /**
67 * The HTTP request context.
68 */
69 readonly request: express.Request;
70 /**
71 * The HTTP response context.
72 */
73 readonly response: express.Response;
74 /**
75 * Gets or sets a value for the whole execution chain.
76 */
77 value?: any;
78}
79/**
80 * A result of statistic end point call.
81 */
82export interface StatisticProviderApiResult {
83 /**
84 * Indicates if there are more rows or not.
85 */
86 hasMore: boolean;
87 /**
88 * The zero based offset.
89 */
90 offset: number;
91 /**
92 * The list of rows.
93 */
94 rows: StatisticResultRow[];
95 /**
96 * The total number of rows.
97 */
98 totalCount: number;
99}
100/**
101 * A function that detects a statistic provider by name.
102 *
103 * @param {string} name The name of the provider, in lower case letters.
104 * @param {StatisticApiContext} context The context.
105 *
106 * @return {StatisticProviderDetectorResult|PromiseLike<StatisticProviderDetectorResult>} The result.
107 */
108export declare type StatisticProviderDetector = (name: string, context: StatisticApiContext) => StatisticProviderDetectorResult | PromiseLike<StatisticProviderDetectorResult>;
109/**
110 * The result of a statistic provider detection function.
111 */
112export declare type StatisticProviderDetectorResult = StatisticProvider | false;
113/**
114 * Registers an API endpoint for providing statistics.
115 *
116 * @param {express.Express | express.Router} hostOrRouter The host or router.
117 * @param {RegisterStatisticsEndpointOptions} opts The options for the registration.
118 */
119export declare function registerStatisticsEndpoint(hostOrRouter: express.Express | express.Router, opts: RegisterStatisticsEndpointOptions): void;