UNPKG

7.63 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 * as bodyParser from 'body-parser';
18import * as errorHandler from 'errorhandler';
19import { MongoDatabase, MongoDatabaseOptions } from '../mongo/index';
20import { Logger, LogType } from '../diagnostics/logger';
21import * as express from 'express';
22/**
23 * An API authorizer.
24 *
25 * @param {express.Request} req The current request context.
26 *
27 * @return {boolean|Promise<boolean>} The result, which indicates if authorization is valid or not.
28 */
29export declare type ApiAuthorizer = (req: express.Request) => boolean | Promise<boolean>;
30/**
31 * A Basic Auth authorizer.
32 *
33 * @param {string} user The username.
34 * @param {string} password The password.
35 *
36 * @return {boolean|Promise<boolean>} The result, which indicates if authorization is valid or not.
37 */
38export declare type BasicAuthAuthorizer = (user: string, password: string) => boolean | Promise<boolean>;
39/**
40 * Body parser options.
41 */
42export declare type BodyParserOptions = bodyParser.OptionsJson & bodyParser.OptionsText & bodyParser.OptionsUrlencoded;
43/**
44 * Options for an 'initialize' method of an 'ApiHost' object.
45 */
46export interface InitializeApiHostOptions {
47 /**
48 * Is invoked after an Express app instance has been created.
49 */
50 onAppCreated?: (app: express.Express) => void;
51}
52/**
53 * A token based authorizer.
54 *
55 * @param {string} token The token to check.
56 *
57 * @return {boolean|Promise<boolean>} The result, which indicates if authorization is valid or not.
58 */
59export declare type TokenAuthorizer = (token: string) => boolean | Promise<boolean>;
60/**
61 * Value for (or from) 'useBodyParser()' method of an ApiHost instance.
62 */
63export declare type UseBodyParserSetting = boolean | BodyParserOptions;
64/**
65 * Value for (or from) 'useBodyParser()' method of an ApiHost instance.
66 */
67export declare type UseErrorHandlerSetting = boolean | errorHandler.Options;
68/**
69 * An API host.
70 */
71export declare class ApiHost {
72 private _app;
73 private _authorizer;
74 private _logger;
75 private _poweredBy;
76 private _root;
77 private _server;
78 private _useBodyParser;
79 private _useErrorHandler;
80 /**
81 * Gets the underlying Express app instance.
82 */
83 readonly app: express.Express;
84 /**
85 * Gets or sets if an authorizer should be used or not.
86 *
87 * @param {ApiAuthorizer} [newValue] The new value.
88 *
89 * @return {ApiAuthorizer|this} The current value or that instance if new value has been set.
90 */
91 authorizer(): ApiAuthorizer;
92 authorizer(newValue: ApiAuthorizer): this;
93 /**
94 * (Re-)Initializes the host.
95 *
96 * @param {InitializeApiHostOptions} [opts] Custom options.
97 */
98 initialize(opts?: InitializeApiHostOptions): void;
99 /**
100 * Gets if the host is currently running or not.
101 */
102 readonly isRunning: boolean;
103 /**
104 * Gets the underlying logger.
105 */
106 readonly logger: Logger;
107 /**
108 * Gets or sets the 'X-Powered-By' header.
109 *
110 * @param {string} [newValue] The new value.
111 *
112 * @return {string|this} The current value or that instance if new value has been set.
113 */
114 poweredBy(): string;
115 poweredBy(newValue: string): this;
116 /**
117 * Gets the root endpoint.
118 */
119 readonly root: express.Router;
120 /**
121 * Sets a 'Basic Auth' based authorizer.
122 *
123 * @param {BasicAuthAuthorizer} authorizer The authorizer.
124 *
125 * @return this
126 */
127 setBasicAuth(authorizer: BasicAuthAuthorizer): this;
128 /**
129 * Sets a prefixed based authorizer.
130 *
131 * @param {TokenAuthorizer} authorizer The authorizer.
132 * @param {string} [prefix] The prefix.
133 *
134 * @return this
135 */
136 setPrefixedAuthorizer(authorizer: TokenAuthorizer, prefix?: string): this;
137 /**
138 * Sets up a new api / app instance.
139 *
140 * @param {express.Express} newApp The instance to setup.
141 * @param {express.Router} newRoot The API root.
142 */
143 protected setupApi(newApp: express.Express, newRoot: express.Router): void;
144 /**
145 * Sets up a new logger instance.
146 *
147 * @param {Logger} newLogger The instance to setup.
148 */
149 protected setupLogger(newLogger: Logger): void;
150 /**
151 * Starts the host.
152 *
153 * @param {number} [port] The custom port to use. By default 'APP_PORT' environment variable is used.
154 * Otherwise 80 is the default port.
155 *
156 * @return {Promise<boolean>} The promise, which indicates if operation successful or not.
157 */
158 start(port?: number): Promise<boolean>;
159 /**
160 * Stops the host.
161 *
162 * @return {Promise<boolean>} The promise, which indicates if operation successful or not.
163 */
164 stop(): Promise<boolean>;
165 /**
166 * Gets or sets if 'body-parser' module should be used or not.
167 *
168 * @param {UseBodyParserSetting} [newValue] The new value.
169 *
170 * @return {UseBodyParserSetting|this} The current value or that instance if new value has been set.
171 */
172 useBodyParser(): UseBodyParserSetting;
173 useBodyParser(newValue: UseBodyParserSetting): this;
174 /**
175 * Gets or sets if 'errorhandler' module should be used or not.
176 *
177 * @param {UseErrorHandlerSetting} [newValue] The new value.
178 *
179 * @return {UseErrorHandlerSetting|this} The current value or that instance if new value has been set.
180 */
181 useErrorHandler(): UseErrorHandlerSetting;
182 useErrorHandler(newValue: UseErrorHandlerSetting): this;
183}
184/**
185 * An API with MongoDB helper methods.
186 */
187export declare class MongoApiHost<TDatabase extends MongoDatabase = MongoDatabase, TOptions extends MongoDatabaseOptions = MongoDatabaseOptions> extends ApiHost {
188 /**
189 * Returns the database class.
190 *
191 * @return {MongoDatabase} The class.
192 */
193 protected getDatabaseClass(): typeof MongoDatabase;
194 /**
195 * Log something into the database.
196 * This requires a 'logs' collection, described by 'LogsDocument' interface.
197 *
198 * @param {any} message The message.
199 * @param {LogType} type The type.
200 * @param {any} [payload] The (optional) payload.
201 */
202 log(message: any, type: LogType, payload?: any): Promise<void>;
203 /**
204 * Options a new connection to a database.
205 *
206 * @param {TOptions} [opts] The custom options to use.
207 *
208 * @return {TDatabase} The new, opened, database.
209 */
210 openDatabase(opts?: TOptions): Promise<TDatabase>;
211 /**
212 * Opens a data connection and invokes an action for it.
213 * After invokation, the database is closed automatically.
214 *
215 * @param {Function} action The action to invoke.
216 * @param {TOptions} [opts] Custom database options.
217 *
218 * @return {Promise<TResult>} The promise with the result of the action.
219 */
220 withDatabase<TResult = any>(action: (db: TDatabase) => (TResult | PromiseLike<TResult>), opts?: TOptions): Promise<TResult>;
221}
222
\No newline at end of file