hapii-js 0.0.0-stage → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +13 -0
- package/README.md +4 -2
- package/lib/auth.js +571 -0
- package/lib/compression.js +119 -0
- package/lib/config.js +446 -0
- package/lib/core.js +718 -0
- package/lib/cors.js +208 -0
- package/lib/ext.js +96 -0
- package/lib/handler.js +165 -0
- package/lib/headers.js +189 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +11 -0
- package/lib/methods.js +126 -0
- package/lib/request.js +806 -0
- package/lib/response.js +751 -0
- package/lib/route.js +456 -0
- package/lib/security.js +86 -0
- package/lib/server.js +604 -0
- package/lib/streams.js +62 -0
- package/lib/toolkit.js +258 -0
- package/lib/transmit.js +388 -0
- package/lib/types/index.d.ts +21 -0
- package/lib/types/plugin.d.ts +266 -0
- package/lib/types/request.d.ts +531 -0
- package/lib/types/response.d.ts +568 -0
- package/lib/types/route.d.ts +982 -0
- package/lib/types/server/auth.d.ts +201 -0
- package/lib/types/server/cache.d.ts +85 -0
- package/lib/types/server/encoders.d.ts +19 -0
- package/lib/types/server/events.d.ts +217 -0
- package/lib/types/server/ext.d.ts +152 -0
- package/lib/types/server/index.d.ts +11 -0
- package/lib/types/server/info.d.ts +53 -0
- package/lib/types/server/inject.d.ts +89 -0
- package/lib/types/server/methods.d.ts +95 -0
- package/lib/types/server/options.d.ts +235 -0
- package/lib/types/server/server.d.ts +701 -0
- package/lib/types/server/state.d.ts +79 -0
- package/lib/types/utils.d.ts +113 -0
- package/lib/validation.js +250 -0
- package/package.json +63 -4
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { Server } from './server';
|
|
2
|
+
import {
|
|
3
|
+
MergeType,
|
|
4
|
+
ReqRef,
|
|
5
|
+
ReqRefDefaults,
|
|
6
|
+
MergeRefs,
|
|
7
|
+
Request,
|
|
8
|
+
RequestAuth} from '../request';
|
|
9
|
+
import { ResponseToolkit, AuthenticationData } from '../response';
|
|
10
|
+
import { RouteOptionsAccess, InternalRouteOptionType, RouteOptionTypes} from '../route';
|
|
11
|
+
import { Lifecycle } from '../utils';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The scheme options argument passed to server.auth.strategy() when instantiation a strategy.
|
|
15
|
+
* For context [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverauthschemename-scheme)
|
|
16
|
+
*/
|
|
17
|
+
export type ServerAuthSchemeOptions = object;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* the method implementing the scheme with signature function(server, options) where:
|
|
21
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverauthschemename-scheme)
|
|
22
|
+
* @param server - a reference to the server object the scheme is added to.
|
|
23
|
+
* @param options - (optional) the scheme options argument passed to server.auth.strategy() when instantiation a strategy.
|
|
24
|
+
*/
|
|
25
|
+
export type ServerAuthScheme<
|
|
26
|
+
// tslint:disable-next-line no-unnecessary-generics
|
|
27
|
+
Options extends ServerAuthSchemeOptions = ServerAuthSchemeOptions,
|
|
28
|
+
// tslint:disable-next-line no-unnecessary-generics
|
|
29
|
+
Refs extends ReqRef = ReqRefDefaults
|
|
30
|
+
> = (server: Server, options?: Options) => ServerAuthSchemeObject<Refs>;
|
|
31
|
+
|
|
32
|
+
export interface ServerAuthSchemeObjectApi {}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The scheme method must return an object with the following
|
|
36
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#authentication-scheme)
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
export interface ServerAuthSchemeObject<Refs extends ReqRef = ReqRefDefaults> {
|
|
40
|
+
/**
|
|
41
|
+
* optional object which is exposed via the [server.auth.api](https://github.com/hapijs/hapi/blob/master/API.md#server.auth.api) object.
|
|
42
|
+
*/
|
|
43
|
+
api?: MergeRefs<Refs>['AuthApi'] | undefined;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A lifecycle method function called for each incoming request configured with the authentication scheme. The
|
|
47
|
+
* method is provided with two special toolkit methods for returning an authenticated or an unauthenticated result:
|
|
48
|
+
* * h.authenticated() - indicate request authenticated successfully.
|
|
49
|
+
* * h.unauthenticated() - indicate request failed to authenticate.
|
|
50
|
+
* @param request the request object.
|
|
51
|
+
* @param h the ResponseToolkit
|
|
52
|
+
* @return the Lifecycle.ReturnValue
|
|
53
|
+
*/
|
|
54
|
+
authenticate(request: Request<Refs>, h: ResponseToolkit<Refs>): Lifecycle.ReturnValue<Refs>;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A lifecycle method to authenticate the request payload.
|
|
58
|
+
* When the scheme payload() method returns an error with a message, it means payload validation failed due to bad
|
|
59
|
+
* payload. If the error has no message but includes a scheme name (e.g. Boom.unauthorized(null, 'Custom')),
|
|
60
|
+
* authentication may still be successful if the route auth.payload configuration is set to 'optional'.
|
|
61
|
+
* @param request the request object.
|
|
62
|
+
* @param h the ResponseToolkit
|
|
63
|
+
* @return the Lifecycle.ReturnValue
|
|
64
|
+
*/
|
|
65
|
+
payload?(request: Request<Refs>, h: ResponseToolkit<Refs>): Lifecycle.ReturnValue<Refs>;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* A lifecycle method to decorate the response with authentication headers before the response headers or payload is written.
|
|
69
|
+
* @param request the request object.
|
|
70
|
+
* @param h the ResponseToolkit
|
|
71
|
+
* @return the Lifecycle.ReturnValue
|
|
72
|
+
*/
|
|
73
|
+
response?(request: Request<Refs>, h: ResponseToolkit<Refs>): Lifecycle.ReturnValue<Refs>;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* a method used to verify the authentication credentials provided
|
|
77
|
+
* are still valid (e.g. not expired or revoked after the initial authentication).
|
|
78
|
+
* the method throws an `Error` when the credentials passed are no longer valid (e.g. expired or
|
|
79
|
+
* revoked). Note that the method does not have access to the original request, only to the
|
|
80
|
+
* credentials and artifacts produced by the `authenticate()` method.
|
|
81
|
+
*/
|
|
82
|
+
verify?(
|
|
83
|
+
auth: RequestAuth<
|
|
84
|
+
MergeRefs<Refs>['AuthUser'],
|
|
85
|
+
MergeRefs<Refs>['AuthApp'],
|
|
86
|
+
MergeRefs<Refs>['AuthCredentialsExtra'],
|
|
87
|
+
MergeRefs<Refs>['AuthArtifactsExtra']
|
|
88
|
+
>
|
|
89
|
+
): Promise<void>;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* An object with the following keys:
|
|
93
|
+
* * payload
|
|
94
|
+
*/
|
|
95
|
+
options?: {
|
|
96
|
+
/**
|
|
97
|
+
* if true, requires payload validation as part of the scheme and forbids routes from disabling payload auth validation. Defaults to false.
|
|
98
|
+
*/
|
|
99
|
+
payload?: boolean | undefined;
|
|
100
|
+
} | undefined;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* An authentication configuration object using the same format as the route auth handler options.
|
|
105
|
+
* For reference [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverauthdefaultoptions)
|
|
106
|
+
*/
|
|
107
|
+
|
|
108
|
+
export interface ServerAuthConfig extends RouteOptionsAccess {
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface ServerAuth {
|
|
112
|
+
/**
|
|
113
|
+
* An object where each key is an authentication strategy name and the value is the exposed strategy API.
|
|
114
|
+
* Available only when the authentication scheme exposes an API by returning an api key in the object
|
|
115
|
+
* returned from its implementation function.
|
|
116
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverauthapi)
|
|
117
|
+
*/
|
|
118
|
+
api: Record<string, ServerAuthSchemeObjectApi>;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Contains the default authentication configuration is a default strategy was set via
|
|
122
|
+
* [server.auth.default()](https://github.com/hapijs/hapi/blob/master/API.md#server.auth.default()).
|
|
123
|
+
*/
|
|
124
|
+
readonly settings: {
|
|
125
|
+
default: ServerAuthConfig;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Sets a default strategy which is applied to every route where:
|
|
130
|
+
* @param options - one of:
|
|
131
|
+
* * a string with the default strategy name
|
|
132
|
+
* * an authentication configuration object using the same format as the route auth handler options.
|
|
133
|
+
* @return void.
|
|
134
|
+
* The default does not apply when a route config specifies auth as false, or has an authentication strategy
|
|
135
|
+
* configured (contains the strategy or strategies authentication settings). Otherwise, the route authentication
|
|
136
|
+
* config is applied to the defaults.
|
|
137
|
+
* Note that if the route has authentication configured, the default only applies at the time of adding the route,
|
|
138
|
+
* not at runtime. This means that calling server.auth.default() after adding a route with some authentication
|
|
139
|
+
* config will have no impact on the routes added prior. However, the default will apply to routes added
|
|
140
|
+
* before server.auth.default() is called if those routes lack any authentication config.
|
|
141
|
+
* The default auth strategy configuration can be accessed via server.auth.settings.default. To obtain the active
|
|
142
|
+
* authentication configuration of a route, use server.auth.lookup(request.route).
|
|
143
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverauthdefaultoptions)
|
|
144
|
+
*/
|
|
145
|
+
default(options: string | ServerAuthConfig): void;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Registers an authentication scheme where:
|
|
149
|
+
* @param name the scheme name.
|
|
150
|
+
* @param scheme - the method implementing the scheme with signature function(server, options) where:
|
|
151
|
+
* * server - a reference to the server object the scheme is added to.
|
|
152
|
+
* * options - (optional) the scheme options argument passed to server.auth.strategy() when instantiation a strategy.
|
|
153
|
+
* @return void.
|
|
154
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverauthschemename-scheme)
|
|
155
|
+
*/
|
|
156
|
+
|
|
157
|
+
scheme <
|
|
158
|
+
Refs extends ReqRef = ReqRefDefaults,
|
|
159
|
+
Options extends object = {}
|
|
160
|
+
// tslint:disable-next-line no-unnecessary-generics
|
|
161
|
+
>(name: string, scheme: ServerAuthScheme<Options, Refs>): void;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Registers an authentication strategy where:
|
|
165
|
+
* @param name - the strategy name.
|
|
166
|
+
* @param scheme - the scheme name (must be previously registered using server.auth.scheme()).
|
|
167
|
+
* @param options - scheme options based on the scheme requirements.
|
|
168
|
+
* @return Return value: none.
|
|
169
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverauthstrategyname-scheme-options)
|
|
170
|
+
*/
|
|
171
|
+
strategy(
|
|
172
|
+
name: MergeType<InternalRouteOptionType, RouteOptionTypes>['Strategy'],
|
|
173
|
+
scheme: string,
|
|
174
|
+
options?: object
|
|
175
|
+
): void;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Tests a request against an authentication strategy where:
|
|
179
|
+
* @param strategy - the strategy name registered with server.auth.strategy().
|
|
180
|
+
* @param request - the request object.
|
|
181
|
+
* @return an object containing the authentication credentials and artifacts if authentication was successful, otherwise throws an error.
|
|
182
|
+
* Note that the test() method does not take into account the route authentication configuration. It also does not
|
|
183
|
+
* perform payload authentication. It is limited to the basic strategy authentication execution. It does not
|
|
184
|
+
* include verifying scope, entity, or other route properties.
|
|
185
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-await-serverauthteststrategy-request)
|
|
186
|
+
*/
|
|
187
|
+
test(strategy: string, request: Request): Promise<AuthenticationData>;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Verify a request's authentication credentials against an authentication strategy.
|
|
191
|
+
* Returns nothing if verification was successful, otherwise throws an error.
|
|
192
|
+
*
|
|
193
|
+
* Note that the `verify()` method does not take into account the route authentication configuration
|
|
194
|
+
* or any other information from the request other than the `request.auth` object. It also does not
|
|
195
|
+
* perform payload authentication. It is limited to verifying that the previously valid credentials
|
|
196
|
+
* are still valid (e.g. have not been revoked or expired). It does not include verifying scope,
|
|
197
|
+
* entity, or other route properties.
|
|
198
|
+
*/
|
|
199
|
+
// tslint:disable-next-line no-unnecessary-generics
|
|
200
|
+
verify <Refs extends ReqRef = ReqRefDefaults>(request: Request<Refs>): Promise<void>;
|
|
201
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { PolicyOptionVariants, Policy, ClientApi, ClientOptions, EnginePrototype, PolicyOptions } from '@hapi/catbox';
|
|
2
|
+
|
|
3
|
+
export type CachePolicyOptions<T> = PolicyOptionVariants<T> & {
|
|
4
|
+
/**
|
|
5
|
+
* @default '_default'
|
|
6
|
+
*/
|
|
7
|
+
cache?: string | undefined;
|
|
8
|
+
segment?: string | undefined;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-servercacheoptions)
|
|
13
|
+
*/
|
|
14
|
+
export interface ServerCache {
|
|
15
|
+
/**
|
|
16
|
+
* Provisions a cache segment within the server cache facility where:
|
|
17
|
+
* @param options - [catbox policy](https://github.com/hapijs/catbox#policy) configuration where:
|
|
18
|
+
* * expiresIn - relative expiration expressed in the number of milliseconds since the item was saved in the cache. Cannot be used together with expiresAt.
|
|
19
|
+
* * expiresAt - time of day expressed in 24h notation using the 'HH:MM' format, at which point all cache records expire. Uses local time. Cannot be used together with expiresIn.
|
|
20
|
+
* * generateFunc - a function used to generate a new cache item if one is not found in the cache when calling get(). The method's signature is async function(id, flags) where:
|
|
21
|
+
* - `id` - the `id` string or object provided to the `get()` method.
|
|
22
|
+
* - `flags` - an object used to pass back additional flags to the cache where:
|
|
23
|
+
* - `ttl` - the cache ttl value in milliseconds. Set to `0` to skip storing in the cache. Defaults to the cache global policy.
|
|
24
|
+
* * staleIn - number of milliseconds to mark an item stored in cache as stale and attempt to regenerate it when generateFunc is provided. Must be less than expiresIn.
|
|
25
|
+
* * staleTimeout - number of milliseconds to wait before checking if an item is stale.
|
|
26
|
+
* * generateTimeout - number of milliseconds to wait before returning a timeout error when the generateFunc function takes too long to return a value. When the value is eventually returned, it
|
|
27
|
+
* is stored in the cache for future requests. Required if generateFunc is present. Set to false to disable timeouts which may cause all get() requests to get stuck forever.
|
|
28
|
+
* * generateOnReadError - if false, an upstream cache read error will stop the cache.get() method from calling the generate function and will instead pass back the cache error. Defaults to true.
|
|
29
|
+
* * generateIgnoreWriteError - if false, an upstream cache write error when calling cache.get() will be passed back with the generated value when calling. Defaults to true.
|
|
30
|
+
* * dropOnError - if true, an error or timeout in the generateFunc causes the stale value to be evicted from the cache. Defaults to true.
|
|
31
|
+
* * pendingGenerateTimeout - number of milliseconds while generateFunc call is in progress for a given id, before a subsequent generateFunc call is allowed. Defaults to 0 (no blocking of
|
|
32
|
+
* concurrent generateFunc calls beyond staleTimeout).
|
|
33
|
+
* * cache - the cache name configured in server.cache. Defaults to the default cache.
|
|
34
|
+
* * segment - string segment name, used to isolate cached items within the cache partition. When called within a plugin, defaults to '!name' where 'name' is the plugin name. When called within a
|
|
35
|
+
* server method, defaults to '#name' where 'name' is the server method name. Required when called outside of a plugin.
|
|
36
|
+
* * shared - if true, allows multiple cache provisions to share the same segment. Default to false.
|
|
37
|
+
* @return Catbox Policy.
|
|
38
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-servercacheoptions)
|
|
39
|
+
*/
|
|
40
|
+
<T, O extends CachePolicyOptions<T> = CachePolicyOptions<T>>(options: O): Policy<T, O>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Provisions a server cache as described in server.cache where:
|
|
44
|
+
* @param options - same as the server cache configuration options.
|
|
45
|
+
* @return Return value: none.
|
|
46
|
+
* Note that if the server has been initialized or started, the cache will be automatically started to match the state of any other provisioned server cache.
|
|
47
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-await-servercacheprovisionoptions)
|
|
48
|
+
*/
|
|
49
|
+
provision(options: ServerOptionsCache): Promise<void>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type CacheProvider<T extends ClientOptions = ClientOptions> = EnginePrototype<any> | {
|
|
53
|
+
constructor: EnginePrototype<any>;
|
|
54
|
+
options?: T | undefined;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* hapi uses catbox for its cache implementation which includes support for common storage solutions (e.g. Redis,
|
|
59
|
+
* MongoDB, Memcached, Riak, among others). Caching is only utilized if methods and plugins explicitly store their state in the cache.
|
|
60
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-cache)
|
|
61
|
+
*/
|
|
62
|
+
export interface ServerOptionsCache extends PolicyOptions<any> {
|
|
63
|
+
/** catbox engine object. */
|
|
64
|
+
engine?: ClientApi<any> | undefined;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* a class or a prototype function
|
|
68
|
+
*/
|
|
69
|
+
provider?: CacheProvider | undefined;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* an identifier used later when provisioning or configuring caching for server methods or plugins. Each cache name must be unique. A single item may omit the name option which defines
|
|
73
|
+
* the default cache. If every cache includes a name, a default memory cache is provisioned as well.
|
|
74
|
+
*/
|
|
75
|
+
name?: string | undefined;
|
|
76
|
+
|
|
77
|
+
/** if true, allows multiple cache users to share the same segment (e.g. multiple methods using the same cache storage container). Default to false. */
|
|
78
|
+
shared?: boolean | undefined;
|
|
79
|
+
|
|
80
|
+
/** (optional) string used to isolate cached data. Defaults to 'hapi-cache'. */
|
|
81
|
+
partition?: string | undefined;
|
|
82
|
+
|
|
83
|
+
/** other options passed to the catbox strategy used. Other options are only passed to catbox when engine above is a class or function and ignored if engine is a catbox engine object). */
|
|
84
|
+
[s: string]: any;
|
|
85
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { createDeflate, createGunzip, createGzip, createInflate } from 'zlib';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Available [content encoders](https://github.com/hapijs/hapi/blob/master/API.md#-serverencoderencoding-encoder).
|
|
5
|
+
*/
|
|
6
|
+
export interface ContentEncoders {
|
|
7
|
+
|
|
8
|
+
deflate: typeof createDeflate;
|
|
9
|
+
gzip: typeof createGzip;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Available [content decoders](https://github.com/hapijs/hapi/blob/master/API.md#-serverdecoderencoding-decoder).
|
|
14
|
+
*/
|
|
15
|
+
export interface ContentDecoders {
|
|
16
|
+
|
|
17
|
+
deflate: typeof createInflate;
|
|
18
|
+
gzip: typeof createGunzip;
|
|
19
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { Podium } from '@hapi/podium';
|
|
2
|
+
|
|
3
|
+
import { Request, RequestRoute } from '../request';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* an event name string.
|
|
7
|
+
* an event options object.
|
|
8
|
+
* a podium emitter object.
|
|
9
|
+
* For context [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-servereventevents)
|
|
10
|
+
*/
|
|
11
|
+
export type ServerEventsApplication = string | ServerEventsApplicationObject | Podium;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Object that it will be used in Event
|
|
15
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-servereventevents)
|
|
16
|
+
*/
|
|
17
|
+
export interface ServerEventsApplicationObject {
|
|
18
|
+
/** the event name string (required). */
|
|
19
|
+
name: string;
|
|
20
|
+
/** a string or array of strings specifying the event channels available. Defaults to no channel restrictions (event updates can specify a channel or not). */
|
|
21
|
+
channels?: string | string[] | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* if true, the data object passed to server.events.emit() is cloned before it is passed to the listeners (unless an override specified by each listener). Defaults to false (data is passed as-is).
|
|
24
|
+
*/
|
|
25
|
+
clone?: boolean | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* if true, the data object passed to server.event.emit() must be an array and the listener method is called with each array element passed as a separate argument (unless an override specified
|
|
28
|
+
* by each listener). This should only be used when the emitted data structure is known and predictable. Defaults to false (data is emitted as a single argument regardless of its type).
|
|
29
|
+
*/
|
|
30
|
+
spread?: boolean | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* if true and the criteria object passed to server.event.emit() includes tags, the tags are mapped to an object (where each tag string is the key and the value is true) which is appended to
|
|
33
|
+
* the arguments list at the end. A configuration override can be set by each listener. Defaults to false.
|
|
34
|
+
*/
|
|
35
|
+
tags?: boolean | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* if true, the same event name can be registered multiple times where the second registration is ignored. Note that if the registration config is changed between registrations, only the first
|
|
38
|
+
* configuration is used. Defaults to false (a duplicate registration will throw an error).
|
|
39
|
+
*/
|
|
40
|
+
shared?: boolean | undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A criteria object with the following optional keys (unless noted otherwise):
|
|
45
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-servereventsoncriteria-listener)
|
|
46
|
+
*
|
|
47
|
+
* The type parameter T is the type of the name of the event.
|
|
48
|
+
*/
|
|
49
|
+
export interface ServerEventCriteria<T> {
|
|
50
|
+
/** (required) the event name string. */
|
|
51
|
+
name: T;
|
|
52
|
+
/**
|
|
53
|
+
* a string or array of strings specifying the event channels to subscribe to. If the event registration specified a list of allowed channels, the channels array must match the allowed
|
|
54
|
+
* channels. If channels are specified, event updates without any channel designation will not be included in the subscription. Defaults to no channels filter.
|
|
55
|
+
*/
|
|
56
|
+
channels?: string | string[] | undefined;
|
|
57
|
+
/** if true, the data object passed to server.event.emit() is cloned before it is passed to the listener method. Defaults to the event registration option (which defaults to false). */
|
|
58
|
+
clone?: boolean | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* a positive integer indicating the number of times the listener can be called after which the subscription is automatically removed. A count of 1 is the same as calling server.events.once().
|
|
61
|
+
* Defaults to no limit.
|
|
62
|
+
*/
|
|
63
|
+
count?: number | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* filter - the event tags (if present) to subscribe to which can be one of:
|
|
66
|
+
* * a tag string.
|
|
67
|
+
* * an array of tag strings.
|
|
68
|
+
* * an object with the following:
|
|
69
|
+
* * * tags - a tag string or array of tag strings.
|
|
70
|
+
* * * all - if true, all tags must be present for the event update to match the subscription. Defaults to false (at least one matching tag).
|
|
71
|
+
*/
|
|
72
|
+
filter?: string | string[] | { tags: string | string[] | undefined, all?: boolean | undefined } | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* if true, and the data object passed to server.event.emit() is an array, the listener method is called with each array element passed as a separate argument. This should only be used
|
|
75
|
+
* when the emitted data structure is known and predictable. Defaults to the event registration option (which defaults to false).
|
|
76
|
+
*/
|
|
77
|
+
spread?: boolean | undefined;
|
|
78
|
+
/**
|
|
79
|
+
* if true and the criteria object passed to server.event.emit() includes tags, the tags are mapped to an object (where each tag string is the key and the value is true) which is appended
|
|
80
|
+
* to the arguments list at the end. Defaults to the event registration option (which defaults to false).
|
|
81
|
+
*/
|
|
82
|
+
tags?: boolean | undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface LogEvent<T = object | string> {
|
|
86
|
+
/** the event timestamp. */
|
|
87
|
+
timestamp: string;
|
|
88
|
+
/** an array of tags identifying the event (e.g. ['error', 'http']) */
|
|
89
|
+
tags: string[];
|
|
90
|
+
/** set to 'internal' for internally generated events, otherwise 'app' for events generated by server.log() */
|
|
91
|
+
channel: 'internal' | 'app';
|
|
92
|
+
/** the request identifier. */
|
|
93
|
+
request: string;
|
|
94
|
+
/** event-specific information. Available when event data was provided and is not an error. Errors are passed via error. */
|
|
95
|
+
data: T;
|
|
96
|
+
/** the error object related to the event if applicable. Cannot appear together with data */
|
|
97
|
+
error: object;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface RequestEvent {
|
|
101
|
+
/** the event timestamp. */
|
|
102
|
+
timestamp: string;
|
|
103
|
+
/** an array of tags identifying the event (e.g. ['error', 'http']) */
|
|
104
|
+
tags: string[];
|
|
105
|
+
/** set to 'internal' for internally generated events, otherwise 'app' for events generated by server.log() */
|
|
106
|
+
channel: 'internal' | 'app' | 'error';
|
|
107
|
+
/** event-specific information. Available when event data was provided and is not an error. Errors are passed via error. */
|
|
108
|
+
data: object | string;
|
|
109
|
+
/** the error object related to the event if applicable. Cannot appear together with data */
|
|
110
|
+
error: object;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export type LogEventHandler = (event: LogEvent, tags: { [key: string]: true }) => void;
|
|
114
|
+
export type RequestEventHandler = (request: Request, event: RequestEvent, tags: { [key: string]: true }) => void;
|
|
115
|
+
export type ResponseEventHandler = (request: Request) => void;
|
|
116
|
+
export type RouteEventHandler = (route: RequestRoute) => void;
|
|
117
|
+
export type StartEventHandler = () => void;
|
|
118
|
+
export type StopEventHandler = () => void;
|
|
119
|
+
|
|
120
|
+
export interface PodiumEvent<K extends string, T> {
|
|
121
|
+
emit(criteria: K, listener: (value: T) => void): void;
|
|
122
|
+
|
|
123
|
+
on(criteria: K, listener: (value: T) => void): void;
|
|
124
|
+
|
|
125
|
+
once(criteria: K, listener: (value: T) => void): void;
|
|
126
|
+
|
|
127
|
+
once(criteria: K): Promise<T>;
|
|
128
|
+
|
|
129
|
+
removeListener(criteria: K, listener: Podium.Listener): this;
|
|
130
|
+
|
|
131
|
+
removeAllListeners(criteria: K): this;
|
|
132
|
+
|
|
133
|
+
hasListeners(criteria: K): this;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Access: podium public interface.
|
|
138
|
+
* The server events emitter. Utilizes the podium with support for event criteria validation, channels, and filters.
|
|
139
|
+
* Use the following methods to interact with server.events:
|
|
140
|
+
* [server.event(events)](https://github.com/hapijs/hapi/blob/master/API.md#server.event()) - register application events.
|
|
141
|
+
* [server.events.emit(criteria, data)](https://github.com/hapijs/hapi/blob/master/API.md#server.events.emit()) - emit server events.
|
|
142
|
+
* [server.events.on(criteria, listener)](https://github.com/hapijs/hapi/blob/master/API.md#server.events.on()) - subscribe to all events.
|
|
143
|
+
* [server.events.once(criteria, listener)](https://github.com/hapijs/hapi/blob/master/API.md#server.events.once()) - subscribe to
|
|
144
|
+
* Other methods include: server.events.removeListener(name, listener), server.events.removeAllListeners(name), and server.events.hasListeners(name).
|
|
145
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverevents)
|
|
146
|
+
*/
|
|
147
|
+
export interface ServerEvents extends Podium {
|
|
148
|
+
/**
|
|
149
|
+
* Subscribe to an event where:
|
|
150
|
+
* @param criteria - the subscription criteria which must be one of:
|
|
151
|
+
* * event name string which can be any of the built-in server events
|
|
152
|
+
* * a custom application event registered with server.event().
|
|
153
|
+
* * a criteria object
|
|
154
|
+
* @param listener - the handler method set to receive event updates. The function signature depends on the event argument, and the spread and tags options.
|
|
155
|
+
* @return Return value: none.
|
|
156
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-servereventsoncriteria-listener)
|
|
157
|
+
* See ['log' event](https://github.com/hapijs/hapi/blob/master/API.md#-log-event)
|
|
158
|
+
* See ['request' event](https://github.com/hapijs/hapi/blob/master/API.md#-request-event)
|
|
159
|
+
* See ['response' event](https://github.com/hapijs/hapi/blob/master/API.md#-response-event)
|
|
160
|
+
* See ['route' event](https://github.com/hapijs/hapi/blob/master/API.md#-route-event)
|
|
161
|
+
* See ['start' event](https://github.com/hapijs/hapi/blob/master/API.md#-start-event)
|
|
162
|
+
* See ['stop' event](https://github.com/hapijs/hapi/blob/master/API.md#-stop-event)
|
|
163
|
+
*/
|
|
164
|
+
on(criteria: 'log' | ServerEventCriteria<'log'>, listener: LogEventHandler): this;
|
|
165
|
+
on(criteria: 'request' | ServerEventCriteria<'request'>, listener: RequestEventHandler): this;
|
|
166
|
+
on(criteria: 'response' | ServerEventCriteria<'response'>, listener: ResponseEventHandler): this;
|
|
167
|
+
on(criteria: 'route' | ServerEventCriteria<'route'>, listener: RouteEventHandler): this;
|
|
168
|
+
on(criteria: 'start' | ServerEventCriteria<'start'>, listener: StartEventHandler): this;
|
|
169
|
+
on(criteria: 'stop' | ServerEventCriteria<'stop'>, listener: StopEventHandler): this;
|
|
170
|
+
on(criteria: string | ServerEventCriteria<string>, listener: (value: any) => void): this;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Same as calling [server.events.on()](https://github.com/hapijs/hapi/blob/master/API.md#server.events.on()) with the count option set to 1.
|
|
174
|
+
* @param criteria - the subscription criteria which must be one of:
|
|
175
|
+
* * event name string which can be any of the built-in server events
|
|
176
|
+
* * a custom application event registered with server.event().
|
|
177
|
+
* * a criteria object
|
|
178
|
+
* @param listener - the handler method set to receive event updates. The function signature depends on the event argument, and the spread and tags options.
|
|
179
|
+
* @return Return value: none.
|
|
180
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-servereventsoncecriteria-listener)
|
|
181
|
+
*/
|
|
182
|
+
once(criteria: 'log' | ServerEventCriteria<'log'>, listener: LogEventHandler): this;
|
|
183
|
+
once(criteria: 'request' | ServerEventCriteria<'request'>, listener: RequestEventHandler): this;
|
|
184
|
+
once(criteria: 'response' | ServerEventCriteria<'response'>, listener: ResponseEventHandler): this;
|
|
185
|
+
once(criteria: 'route' | ServerEventCriteria<'route'>, listener: RouteEventHandler): this;
|
|
186
|
+
once(criteria: 'start' | ServerEventCriteria<'start'>, listener: StartEventHandler): this;
|
|
187
|
+
once(criteria: 'stop' | ServerEventCriteria<'stop'>, listener: StopEventHandler): this;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Same as calling server.events.on() with the count option set to 1.
|
|
191
|
+
* @param criteria - the subscription criteria which must be one of:
|
|
192
|
+
* * event name string which can be any of the built-in server events
|
|
193
|
+
* * a custom application event registered with server.event().
|
|
194
|
+
* * a criteria object
|
|
195
|
+
* @return Return value: a promise that resolves when the event is emitted.
|
|
196
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-await-servereventsoncecriteria)
|
|
197
|
+
*/
|
|
198
|
+
once(criteria: string | ServerEventCriteria<string>): Promise<any>;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* The follow method is only mentioned in Hapi API. The doc about that method can be found [here](https://github.com/hapijs/podium/blob/master/API.md#podiumremovelistenername-listener)
|
|
202
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverevents)
|
|
203
|
+
*/
|
|
204
|
+
removeListener(name: string, listener: Podium.Listener): this;
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The follow method is only mentioned in Hapi API. The doc about that method can be found [here](https://github.com/hapijs/podium/blob/master/API.md#podiumremovealllistenersname)
|
|
208
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverevents)
|
|
209
|
+
*/
|
|
210
|
+
removeAllListeners(name: string): this;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* The follow method is only mentioned in Hapi API. The doc about that method can be found [here](https://github.com/hapijs/podium/blob/master/API.md#podiumhaslistenersname)
|
|
214
|
+
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverevents)
|
|
215
|
+
*/
|
|
216
|
+
hasListeners(name: string): boolean;
|
|
217
|
+
}
|