diff --git a/types/boom/index.d.ts b/types/boom/index.d.ts index bb2f5e4bc0..4d9d0b3a5b 100644 --- a/types/boom/index.d.ts +++ b/types/boom/index.d.ts @@ -3,8 +3,6 @@ // Definitions by: Igor Rogatty , AJP // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -/// - export = Boom; declare namespace Boom { diff --git a/types/boom/tsconfig.json b/types/boom/tsconfig.json index d64d51130b..c3bee46396 100644 --- a/types/boom/tsconfig.json +++ b/types/boom/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" diff --git a/types/catbox/index.d.ts b/types/catbox/index.d.ts new file mode 100644 index 0000000000..e832f58446 --- /dev/null +++ b/types/catbox/index.d.ts @@ -0,0 +1,247 @@ +// Type definitions for catbox 7.1 +// Project: https://github.com/hapijs/catbox +// Definitions by: Jason Swearingen , AJP +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +import * as Boom from 'boom'; + +export interface CallBackNoResult { + (err?: Boom.BoomError): void; +} +export interface CallBackWithResult { + (err: Boom.BoomError | null | undefined, result: T): void; +} + +/** + * Client + * The Client object provides a low-level cache abstraction. The object is constructed using new Client(engine, options) where: + * engine - is an object or a prototype function implementing the cache strategy: + * * function - a prototype function with the signature function(options). catbox will call new func(options). + * * object - a pre instantiated client implementation object. Does not support passing options. + * options - the strategy configuration object. Each strategy defines its own configuration options with the following common options: + * * partition - the partition name used to isolate the cached results across multiple clients. The partition name is used as the MongoDB database name, the Riak bucket, or as a key prefix in Redis and Memcached. To share the cache across multiple clients, use the same partition name. + * @see {@link https://github.com/hapijs/catbox#client} + */ +export interface Client extends ClientApi { + new(engine: EnginePrototypeOrObject, options: ClientOptions): Client; +} + +type EnginePrototypeOrObject = EnginePrototype | ClientApi; + +/** + * A prototype CatBox engine function + */ +export interface EnginePrototype { + new(settings: ClientOptions): ClientApi; +} + +/** + * Client API + * The Client object provides the following methods: + * @see {@link https://github.com/hapijs/catbox#api} + */ +export interface ClientApi { + /** start(callback) - creates a connection to the cache server. Must be called before any other method is available. The callback signature is function(err). */ + start(callback: CallBackNoResult): void; + /** stop() - terminates the connection to the cache server. */ + stop(): void; + /** + * get(key, callback) - retrieve an item from the cache engine if found where: + * * key - a cache key object (see [ICacheKey]). + * * callback - a function with the signature function(err, cached). If the item is not found, both err and cached are null. If found, the cached object is returned + */ + get(key: CacheKey, callback: CallBackWithResult): CacheItem; + /** + * set(key, value, ttl, callback) - store an item in the cache for a specified length of time, where: + * * key - a cache key object (see [ICacheKey]). + * * value - the string or object value to be stored. + * * ttl - a time-to-live value in milliseconds after which the item is automatically removed from the cache (or is marked invalid). + * * callback - a function with the signature function(err). + */ + set(key: CacheKey, value: CacheItem, ttl: number, callback: CallBackNoResult): void; + /** + * drop(key, callback) - remove an item from cache where: + * * key - a cache key object (see [ICacheKey]). + * * callback - a function with the signature function(err). + */ + drop(key: CacheKey, callback: CallBackNoResult): void; + /** isReady() - returns true if cache engine determines itself as ready, false if it is not ready. */ + isReady(): boolean; + /** validateSegmentName(segment) - returns null if the segment name is valid (see below), otherwise should return an instance of Error with an appropriate message. */ + validateSegmentName(segment: string): null | Boom.BoomError; +} + +/** + * Any method with a key argument takes an object with the following required properties: + */ +export interface CacheKey { + /** segment - a caching segment name string. Enables using a single cache server for storing different sets of items with overlapping ids. */ + segment: string; + /** id - a unique item identifier string (per segment). Can be an empty string. */ + id: string; +} + +/** Cached object contains the following: */ +export interface CachedObject { + /** item - the value stored in the cache using set(). */ + item: any; + /** stored - the timestamp when the item was stored in the cache (in milliseconds). */ + stored: number; + /** ttl - the remaining time-to-live (not the original value used when storing the object). */ + ttl: number; +} + +type CacheItem = any; + +export interface ClientOptions { + partition: string; +} + +/** + * The Policy object provides a convenient cache interface by setting a global policy which is automatically applied to every storage action. The object is constructed using new Policy(options, [cache, segment]) where: + * * options - an object with the IPolicyOptions structure + * * cache - a Client instance (which has already been started). + * * segment - required when cache is provided. The segment name used to isolate cached items within the cache partition. + * @see {@link https://github.com/hapijs/catbox#policy} + */ +export interface Policy extends PolicyAPI { + new(options: PolicyOptions, cache: Client, segment: string): Policy; +} + +/** + * Policy API + * The Policy object provides the following methods: + * @see {@link https://github.com/hapijs/catbox#api-1} + */ +export interface PolicyAPI { + /** + * get(id, callback) - retrieve an item from the cache. If the item is not found and the generateFunc method was provided, a new value is generated, stored in the cache, and returned. Multiple concurrent requests are queued and processed once. The method arguments are: + * * id - the unique item identifier (within the policy segment). Can be a string or an object with the required 'id' key. + * * callback - the return function. + */ + get(id: string | {id: string}, callback: PolicyGetCallback): CacheItem; + /** + * set(id, value, ttl, callback) - store an item in the cache where: + * * id - the unique item identifier (within the policy segment). + * * value - the string or object value to be stored. + * * ttl - a time-to-live override value in milliseconds after which the item is automatically removed from the cache (or is marked invalid). This should be set to 0 in order to use the caching rules configured when creating the Policy object. + * * callback - a function with the signature function(err). + */ + set(id: string | {id: string}, value: CacheItem, ttl: number | null, callback: CallBackNoResult): void; + /** + * drop(id, callback) - remove the item from cache where: + * * id - the unique item identifier (within the policy segment). + * * callback - a function with the signature function(err). + */ + drop(id: string | {id: string}, callback: CallBackNoResult): void; + /** ttl(created) - given a created timestamp in milliseconds, returns the time-to-live left based on the configured rules. */ + ttl(created: number): number; + /** rules(options) - changes the policy rules after construction (note that items already stored will not be affected) */ + rules(options: PolicyOptions): void; + /** isReady() - returns true if cache engine determines itself as ready, false if it is not ready or if there is no cache engine set. */ + isReady(): boolean; + /** stats - an object with cache statistics */ + stats(): CacheStatisticsObject; +} + +/** + * The return function. The function signature is function(err, value, cached, report) where: + * @param err - any errors encountered. + * @param value - the fetched or generated value. + * @param cached - null if a valid item was not found in the cache, or IPolicyGetCallbackCachedOptions + * @param report - an object with logging information about the generation operation + */ +export interface PolicyGetCallback{ + (err: null | Boom.BoomError, value: CacheItem, cached: PolicyGetCallbackCachedOptions, report: PolicyGetCallbackReportLog): void; +} + +export interface PolicyGetCallbackCachedOptions { + /** item - the cached value. */ + item: CacheItem; + /** stored - the timestamp when the item was stored in the cache. */ + stored: number; + /** ttl - the cache ttl value for the record. */ + ttl: number; + /** isStale - true if the item is stale. */ + isStale: boolean; +} + +/** + * @see {@link https://github.com/hapijs/catbox#policy} + */ +export interface PolicyOptions { + /** expiresIn - relative expiration expressed in the number of milliseconds since the item was saved in the cache. Cannot be used together with expiresAt. */ + expiresIn?: number; + /** expiresAt - time of day expressed in 24h notation using the 'HH:MM' format, at which point all cache records for the route expire. Uses local time. Cannot be used together with expiresIn. */ + expiresAt?: string; + /** 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 function(id, next) where: */ + generateFunc?: GenerateFunc; + /** + * 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. Alternatively function that returns staleIn value in milliseconds. The function signature is function(stored, ttl) where: + * * stored - the timestamp when the item was stored in the cache (in milliseconds). + * * ttl - the remaining time-to-live (not the original value used when storing the object). + */ + staleIn?: number | ((stored: number, ttl: number) => number); + /** staleTimeout - number of milliseconds to wait before returning a stale value while generateFunc is generating a fresh value. */ + staleTimeout?: number; + /** 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 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. */ + generateTimeout?: number | false; + /** dropOnError - if true, an error or timeout in the generateFunc causes the stale value to be evicted from the cache. Defaults to true. */ + dropOnError?: boolean; + /** generateOnReadError - if false, an upstream cache read error will stop the get() method from calling the generate function and will instead pass back the cache error. Defaults to true. */ + generateOnReadError?: boolean; + /** generateIgnoreWriteError - if false, an upstream cache write error will be passed back with the generated value when calling the get() method. Defaults to true. */ + generateIgnoreWriteError?: boolean; + /** 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 concurrent generateFunc calls beyond staleTimeout. */ + pendingGenerateTimeout?: number; +} + +/** + * generateFunc + * Is used in PolicyOptions + * 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 function(id, next) + * @param id - the id string or object provided to the get() method. + * @param next - the method called when the new item is returned with the signature function(err, value, ttl) where: + * * err - an error condition. + * * value - the new value generated. + * * ttl - the cache ttl value in milliseconds. Set to 0 to skip storing in the cache. Defaults to the cache global policy. + * @see {@link https://github.com/hapijs/catbox#policy} + */ +export interface GenerateFunc { + (id: string, next: ((err: null | Boom.BoomError, value: CacheItem, ttl?: number) => void)): void; +} + +/** + * An object with logging information about the generation operation containing the following keys (as relevant): + */ +export interface PolicyGetCallbackReportLog { + /** msec - the cache lookup time in milliseconds. */ + msec: number; + /** stored - the timestamp when the item was stored in the cache. */ + stored: number; + /** isStale - true if the item is stale. */ + isStale: boolean; + /** ttl - the cache ttl value for the record. */ + ttl: number; + /** error - lookup error. */ + error?: Boom.BoomError; +} + +/** + * an object with cache statistics where: + */ +export interface CacheStatisticsObject { + /** sets - number of cache writes. */ + sets: number; + /** gets - number of cache get() requests. */ + gets: number; + /** hits - number of cache get() requests in which the requested id was found in the cache (can be stale). */ + hits: number; + /** stales - number of cache reads with stale requests (only counts the first request in a queued get() operation). */ + stales: number; + /** generates - number of calls to the generate function. */ + generates: number; + /** errors - cache operations errors. TODO check this*/ + errors: number; +} diff --git a/types/catbox/tsconfig.json b/types/catbox/tsconfig.json new file mode 100644 index 0000000000..3bc13b0278 --- /dev/null +++ b/types/catbox/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts" + ] +} \ No newline at end of file diff --git a/types/h2o2/h2o2-tests.ts b/types/h2o2/h2o2-tests.ts new file mode 100644 index 0000000000..f8e88c5c9b --- /dev/null +++ b/types/h2o2/h2o2-tests.ts @@ -0,0 +1,165 @@ +import http = require("http"); +import * as Boom from 'boom'; +import * as Hapi from 'hapi'; +import * as Wreck from 'wreck'; +import h2o2 = require('h2o2'); + +// https://github.com/hapijs/h2o2#manual-loading + +const server = new Hapi.Server({}); + +server.register({ + register: h2o2 +}, function (err) { + + if (err) { + console.log('Failed to load h2o2'); + } + + server.start(function (err) { + + console.log('Server started at: ' + server.info!.uri); + }); +}); + +// https://github.com/hapijs/h2o2#replyproxyoptions + +const handler: Hapi.RouteHandler = function (request, reply) { + + return reply.proxy({ host: 'example.com', port: 80, protocol: 'http' }); +}; + +// https://github.com/hapijs/h2o2#using-the-host-port-protocol-options + +server.route({ + method: 'GET', + path: '/', + handler: { + proxy: { + host: '10.33.33.1', + port: '443', + protocol: 'https' + } + } +}); + +// https://github.com/hapijs/h2o2#using-the-uri-option + +server.route({ + method: 'GET', + path: '/', + handler: { + proxy: { + uri: 'https://some.upstream.service.com/that/has?what=you&want=todo' + } + } +}); + +// https://github.com/hapijs/h2o2#custom-uri-template-values + +server.route({ + method: 'GET', + path: '/foo', + handler: { + proxy: { + uri: '{protocol}://{host}:{port}/go/to/{path}' + } + } +}); + +server.route({ + method: 'GET', + path: '/foo/{bar}', + handler: { + proxy: { + uri: 'https://some.upstream.service.com/some/path/to/{bar}' + } + } +}); + +// https://github.com/hapijs/h2o2#using-the-mapuri-and-onresponse-options + +server.route({ + method: 'GET', + path: '/', + handler: { + proxy: { + mapUri: function (request, callback) { + + console.log('doing some aditional stuff before redirecting'); + callback(null, 'https://some.upstream.service.com/'); + }, + onResponse: function (err, res, request, reply, settings, ttl) { + + console.log('receiving the response from the upstream.'); + Wreck.read(res, { json: true }, function (err, payload) { + + console.log('some payload manipulation if you want to.') + reply(payload).headers = res.headers; + }); + } + } + } +}); + +/** + * test code added in additional to code in docs. Demonstrates that for the moment + * you need flat + * objects with typing along the way to benefit from typescript catching + * misspelt, or unsupported keys. + * This is because of an unknown reason. Expecting this to work because: + * "Object literals get special treatment and undergo excess + * property checking when assigning them to other variables, or passing them + * as arguments", see github.com/Microsoft/TypeScript + */ + +var proxyOptions: h2o2.ProxyHandlerOptions = { + host: '10.33.33.1', + port: '443', + protocol: 'https' // errors correctly if misspelt +} + +server.route({ + method: 'GET', + path: '/', + handler: { + proxy: { + host: '10.33.33.1', + port: '443', + BAD_protocol: 'https' // TODO change typings to fix this / submit bug report + } + } +}) + +server.route({ + method: 'GET', + path: '/', + handler: { + proxy: { + uri: 'https://some.upstream.service.com/that/has?what=you&want=todo' + } + } +}) + +server.route({ + method: 'GET', + path: '/', + handler: { + proxy: { + mapUri: function (request: Hapi.Request, callback: (err: null | Boom.BoomError, value: string) => void) { + + console.log('doing some aditional stuff before redirecting'); + callback(null, 'https://some.upstream.service.com/'); + }, + onResponse: function (err: null | Boom.BoomError, res: http.IncomingMessage, request: Hapi.Request, reply: Hapi.ReplyWithContinue, settings: h2o2.ProxyHandlerOptions, ttl: number) { + + console.log('receiving the response from the upstream.'); + Wreck.read(res, { json: true }, function (err: null | Boom.BoomError, payload: any) { + + console.log('some payload manipulation if you want to.') + reply(payload).headers = res.headers; + }); + } + } + } +}); diff --git a/types/h2o2/index.d.ts b/types/h2o2/index.d.ts new file mode 100644 index 0000000000..a1a3680e5c --- /dev/null +++ b/types/h2o2/index.d.ts @@ -0,0 +1,91 @@ +// Type definitions for h2o2 5.4 +// Project: https://github.com/hapijs/catbox +// Definitions by: Jason Swearingen , AJP +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +/// + +import http = require("http"); +import * as hapi from 'hapi'; +import * as Boom from 'boom'; + +declare namespace H2o2 { + /** Proxy handler options */ + export interface ProxyHandlerOptions { + /** host - upstream service host to proxy requests to. It will have the same path as the client request. */ + host?: string; + /** port - upstream service port. */ + port?: number | string; + /** protocol - protocol to use when making the request to the proxied host: */ + protocol?: 'http' | 'https'; + /** uri - absolute URI used instead of host, port, protocol, path, and query. Cannot be used with host, port, protocol, or mapUri. */ + uri?: string; + /** passThrough - if set to true, it forwards the headers from the client to the upstream service, headers sent from the upstream service will also be forwarded to the client. Defaults to false. */ + passThrough?: boolean; + /** localStatePassThrough - if set tofalse, any locally defined state is removed from incoming requests before being sent to the upstream service. This value can be overridden on a per state basis via the server.state()``passThrough option. Defaults to false */ + localStatePassThrough?: boolean; + /** acceptEncoding - if set to false, does not pass-through the 'Accept-Encoding' HTTP header which is useful for the onResponse post-processing to avoid receiving an encoded response. Can only be used together with passThrough. Defaults to true (passing header). */ + acceptEncoding?: boolean; + /** rejectUnauthorized - sets the rejectUnauthorized property on the https agent making the request. This value is only used when the proxied server uses TLS/SSL. If set it will override the node.js rejectUnauthorized property. If false then ssl errors will be ignored. When true the server certificate is verified and an 500 response will be sent when verification fails. This shouldn't be used alongside the agent setting as the agent will be used instead. Defaults to the https agent default value of true. */ + rejectUnauthorized?: boolean; + /** xforward - if set to true, sets the 'X-Forwarded-For', 'X-Forwarded-Port', 'X-Forwarded-Proto', 'X-Forwarded-Host' headers when making a request to the proxied upstream endpoint. Defaults to false. */ + xforward?: boolean; + /** redirects - the maximum number of HTTP redirections allowed to be followed automatically by the handler. Set to false or 0 to disable all redirections (the response will contain the redirection received from the upstream service). If redirections are enabled, no redirections (301, 302, 307, 308) will be passed along to the client, and reaching the maximum allowed redirections will return an error response. Defaults to false. */ + redirects?: number | false; + /** timeout - number of milliseconds before aborting the upstream request. Defaults to 180000 (3 minutes). */ + timeout?: number; + /** mapUri - a function used to map the request URI to the proxied URI. Cannot be used together with host, port, protocol, or uri. The function signature is function (request, callback) where: + * @param request - is the incoming request object. + * @param callback - is function (err, uri, headers) where: + * @param err - internal error condition. TODO: check this is of type BoomError or just Error. + * @param uri - the absolute proxy URI. + * @param headers - optional object where each key is an HTTP request header and the value is the header content. + */ + mapUri?: (request: hapi.Request, callback: (err: null | Boom.BoomError, uri: string, headers?: { [key: string]: string }) => void) => void; + /** + * onResponse - a custom function for processing the response from the upstream service before sending to the client. Useful for custom error handling of responses from the proxied endpoint or other payload manipulation. Function signature is function (err, res, request, reply, settings, ttl) where: + * @param err - internal or upstream error returned from attempting to contact the upstream proxy. TODO: check this is of type BoomError or just Error. + * @param res - the node response object received from the upstream service. res is a readable stream (use the wreck module read method to easily convert it to a Buffer or string). + * @param request - is the incoming request object. + * @param reply - the reply interface function. + * @param settings - the proxy handler configuration. + * @param ttl - the upstream TTL in milliseconds if proxy.ttl it set to 'upstream' and the upstream response included a valid 'Cache-Control' header with 'max-age'. + */ + onResponse?: (err: null | Boom.BoomError, + res: http.IncomingMessage, + req: hapi.Request, + reply: hapi.ReplyWithContinue, // TODO, check it has continue + settings: ProxyHandlerOptions, + ttl: number) => void; + /** ttl - if set to 'upstream', applies the upstream response caching policy to the response using the response.ttl() method (or passed as an argument to the onResponse method if provided). */ + ttl?: 'upstream'; + /** agent - a node http(s) agent to be used for connections to upstream server. @see {@link https://nodejs.org/api/http.html#http_class_http_agent} */ + agent?: http.Agent; + /** maxSockets - sets the maximum number of sockets available per outgoing proxy host connection. false means use the wreck module default value (Infinity). Does not affect non-proxy outgoing client connections. Defaults to Infinity. */ + maxSockets?: false | number; + } +} + +declare module 'hapi' { + /** + * As one of the handlers for hapi, it is used through the route configuration object. + * [see docs](https://github.com/hapijs/h2o2#usage) + */ + interface RouteHandlerPlugins { + proxy?: H2o2.ProxyHandlerOptions; + } + + interface Base_Reply { + /** + * Proxies the request to an upstream endpoint + * @param options an object including the same keys and restrictions defined by the [route proxy handler options](https://github.com/hapijs/h2o2#options). + * [see docs](https://github.com/hapijs/h2o2#replyproxyoptions) + */ + proxy(options: H2o2.ProxyHandlerOptions): void; + } +} + +declare var H2o2: hapi.PluginFunction<{}>; + +export = H2o2; diff --git a/types/h2o2/tsconfig.json b/types/h2o2/tsconfig.json new file mode 100644 index 0000000000..1abbac7fb0 --- /dev/null +++ b/types/h2o2/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "h2o2-tests.ts" + ] +} \ No newline at end of file diff --git a/types/hapi-auth-basic/hapi-auth-basic-tests.ts b/types/hapi-auth-basic/hapi-auth-basic-tests.ts new file mode 100644 index 0000000000..7f78fe9f65 --- /dev/null +++ b/types/hapi-auth-basic/hapi-auth-basic-tests.ts @@ -0,0 +1,42 @@ +// from https://github.com/hapijs/hapi-auth-basic#hapi-auth-basic + +import Bcrypt = require('bcrypt'); +import Basic = require('hapi-auth-basic'); +import * as Hapi from 'hapi'; + +const server = new Hapi.Server(); + +interface User { + username: string; + password: string; + name: string; + id: string; +} + +const users: {[index: string]: User} = { + john: { + username: 'john', + password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm', // 'secret' + name: 'John Doe', + id: '2133d32a' + } +}; + +const validate: Basic.ValidateFunc = function (request, username, password, callback) { + + const user = users[username]; + if (!user) { + return callback(null, false); + } + + Bcrypt.compare(password, user.password, (err, isValid) => { + + callback(err, isValid, { id: user.id, name: user.name }); + }); +}; + +server.register(Basic, (err) => { + + server.auth.strategy('simple', 'basic', { validateFunc: validate }); + server.route({ method: 'GET', path: '/', config: { auth: 'simple' } }); +}); diff --git a/types/hapi-auth-basic/index.d.ts b/types/hapi-auth-basic/index.d.ts new file mode 100644 index 0000000000..23b06de111 --- /dev/null +++ b/types/hapi-auth-basic/index.d.ts @@ -0,0 +1,21 @@ +// Type definitions for hapi 4.2 +// Project: https://github.com/hapijs/hapi-auth-basic +// Definitions by: AJP +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +import * as Hapi from 'hapi'; + +declare namespace Basic { + interface ValidateFuncCallback { + (err: Error | null, isValid: boolean, userCredentials?: any): void; + } + + interface ValidateFunc { + (request: Hapi.Request, username: string, password: string, callback: ValidateFuncCallback): void; + } +} + +declare var Basic: Hapi.PluginFunction<{}>; + +export = Basic; diff --git a/types/hapi-auth-basic/tsconfig.json b/types/hapi-auth-basic/tsconfig.json new file mode 100644 index 0000000000..9e26f11c47 --- /dev/null +++ b/types/hapi-auth-basic/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "hapi-auth-basic-tests.ts" + ] +} diff --git a/types/hapi-auth-jwt2/index.d.ts b/types/hapi-auth-jwt2/index.d.ts index c12a1f8c04..2e5826e044 100644 --- a/types/hapi-auth-jwt2/index.d.ts +++ b/types/hapi-auth-jwt2/index.d.ts @@ -2,125 +2,129 @@ // Project: http://github.com/dwyl/hapi-auth-jwt2 // Definitions by: Warren Seymour // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -import {Request, Response} from 'hapi'; +import {Request, Response, PluginFunction} from 'hapi'; -/** - * A key lookup function - * - * @param decoded the *decoded* but *unverified* JWT received from client - * @param callback the key lookup callback - */ -export type KeyLookup = (decoded: any, callback: KeyLookupCallback) => void; - -/** - * Called when key lookup function has completed - * - * @param err an internal error - * @param key the secret key - * @param extraInfo any additional information that you would like - * to use in `validateFunc` which can be accessed via - * `request.plugins['hapi-auth-jwt2'].extraInfo` - */ -export type KeyLookupCallback = (err: any, key: string, extraInfo?: any) => void; - -/** - * Called when Validation has completed - * - * @param err an internal error - * @param valid `true` if the JWT was valid, otherwise `false` - * @param credentials alternative credentials to be set instead of `decoded` - */ -export type ValidateCallback = (err: any, valid: boolean, credentials?: any) => void; - -/** - * Options passed to `hapi.auth.strategy` when this plugin is used - */ -export interface Options { - /** - * The secret key used to check the signature of the token *or* a *key lookup function* - */ - key?: string | KeyLookup; - - /** - * The function which is run once the Token has been decoded - * - * @param decoded the *decoded* and *verified* JWT received from the client in *request.headers.authorization* - * @param request the original *request* received from the client - * @param callback the validation callback - */ - validateFunc(decoded: {}, request: Request, callback: ValidateCallback): void; - - /** - * Settings to define how tokens are verified by the jsonwebtoken library - */ - verifyOptions?: { +declare namespace hapiAuthJwt2 { /** - * Ignore expired tokens + * A key lookup function + * + * @param decoded the *decoded* but *unverified* JWT received from client + * @param callback the key lookup callback */ - ignoreExpiration?: boolean; + type KeyLookup = (decoded: any, callback: KeyLookupCallback) => void; /** - * Do not enforce token audience + * Called when key lookup function has completed + * + * @param err an internal error + * @param key the secret key + * @param extraInfo any additional information that you would like + * to use in `validateFunc` which can be accessed via + * `request.plugins['hapi-auth-jwt2'].extraInfo` */ - audience?: boolean; + type KeyLookupCallback = (err: any, key: string, extraInfo?: any) => void; /** - * Do not require the issuer to be valid + * Called when Validation has completed + * + * @param err an internal error + * @param valid `true` if the JWT was valid, otherwise `false` + * @param credentials alternative credentials to be set instead of `decoded` */ - issuer?: boolean; + type ValidateCallback = (err: any, valid: boolean, credentials?: any) => void; /** - * List of allowed algorithms + * Options passed to `hapi.auth.strategy` when this plugin is used */ - algorithms?: string[]; - }; + interface Options { + /** + * The secret key used to check the signature of the token *or* a *key lookup function* + */ + key?: string | KeyLookup; - /** - * function called to decorate the response with authentication headers - * before the response headers or payload is written - * - * @param request the Request object - * @param reply is called if an error occurred - */ - responseFunc?(request: Request, reply: (err: any, response: Response) => void): void; + /** + * The function which is run once the Token has been decoded + * + * @param decoded the *decoded* and *verified* JWT received from the client in *request.headers.authorization* + * @param request the original *request* received from the client + * @param callback the validation callback + */ + validateFunc(decoded: {}, request: Request, callback: ValidateCallback): void; - /** - * If you prefer to pass your token via url, simply add a token url - * parameter to your request or use a custom parameter by setting `urlKey. - * To disable the url parameter set urlKey to `false` or ''. - * @default 'token' - */ - urlKey?: string | boolean; + /** + * Settings to define how tokens are verified by the jsonwebtoken library + */ + verifyOptions?: { + /** + * Ignore expired tokens + */ + ignoreExpiration?: boolean; - /** - * If you prefer to set your own cookie key or your project has a cookie - * called 'token' for another purpose, you can set a custom key for your - * cookie by setting `options.cookieKey='yourkeyhere'`. To disable cookies - * set cookieKey to `false` or ''. - * @default 'token' - */ - cookieKey?: string | boolean; + /** + * Do not enforce token audience + */ + audience?: boolean; - /** - * If you want to set a custom key for your header token use the - * `headerKey` option. To disable header token set headerKey to `false` or - * ''. - * @default 'authorization' - */ - headerKey?: string | boolean; + /** + * Do not require the issuer to be valid + */ + issuer?: boolean; - /** - * Allow custom token type, e.g. `Authorization: 12345678` - */ - tokenType?: string; + /** + * List of allowed algorithms + */ + algorithms?: string[]; + }; - /** - * Set to `true` to receive the complete token (`decoded.header`, - * `decoded.payload` and `decoded.signature`) as decoded argument to key - * lookup and `verifyFunc` callbacks (*not `validateFunc`*) - * @default false - */ - complete?: boolean; + /** + * function called to decorate the response with authentication headers + * before the response headers or payload is written + * + * @param request the Request object + * @param reply is called if an error occurred + */ + responseFunc?(request: Request, reply: (err: any, response: Response) => void): void; + + /** + * If you prefer to pass your token via url, simply add a token url + * parameter to your request or use a custom parameter by setting `urlKey. + * To disable the url parameter set urlKey to `false` or ''. + * @default 'token' + */ + urlKey?: string | boolean; + + /** + * If you prefer to set your own cookie key or your project has a cookie + * called 'token' for another purpose, you can set a custom key for your + * cookie by setting `options.cookieKey='yourkeyhere'`. To disable cookies + * set cookieKey to `false` or ''. + * @default 'token' + */ + cookieKey?: string | boolean; + + /** + * If you want to set a custom key for your header token use the + * `headerKey` option. To disable header token set headerKey to `false` or + * ''. + * @default 'authorization' + */ + headerKey?: string | boolean; + + /** + * Allow custom token type, e.g. `Authorization: 12345678` + */ + tokenType?: string; + + /** + * Set to `true` to receive the complete token (`decoded.header`, + * `decoded.payload` and `decoded.signature`) as decoded argument to key + * lookup and `verifyFunc` callbacks (*not `validateFunc`*) + * @default false + */ + complete?: boolean; + } } -export default function hapiAuthJwt2(): void; +declare var hapiAuthJwt2: PluginFunction<{}>; + +export = hapiAuthJwt2; diff --git a/types/hapi-decorators/hapi-decorators-tests.ts b/types/hapi-decorators/hapi-decorators-tests.ts index 49a112152a..2a6f2df5b3 100644 --- a/types/hapi-decorators/hapi-decorators-tests.ts +++ b/types/hapi-decorators/hapi-decorators-tests.ts @@ -4,7 +4,7 @@ import { controller, get, post, put, cache, config, route, validate, Controller @controller('/test') class TestController implements Controller { baseUrl: string; - routes: () => hapi.IRouteConfiguration[]; + routes: () => hapi.RouteConfiguration[]; @get('/') @config({ @@ -16,22 +16,22 @@ class TestController implements Controller { @validate({ payload: false }) - getHandler(request: hapi.Request, reply: hapi.IReply) { + getHandler(request: hapi.Request, reply: hapi.ReplyNoContinue) { reply({ success: true }); } @post('/') - postHandler(request: hapi.Request, reply: hapi.IReply) { + postHandler(request: hapi.Request, reply: hapi.ReplyNoContinue) { reply({ success: true }); } @put('/{id}') - putHandler(request: hapi.Request, reply: hapi.IReply) { + putHandler(request: hapi.Request, reply: hapi.ReplyNoContinue) { reply({ success: true }); } @route('delete', '/{id}') - deleteHandler(request: hapi.Request, reply: hapi.IReply) { + deleteHandler(request: hapi.Request, reply: hapi.ReplyNoContinue) { reply({ success: true }); } } @@ -50,7 +50,7 @@ class SimpleTestController implements Controller { } baseUrl: string; - routes: () => hapi.IRouteConfiguration[]; + routes: () => hapi.RouteConfiguration[]; } server.route(new SimpleTestController('bar').routes()); diff --git a/types/hapi-decorators/index.d.ts b/types/hapi-decorators/index.d.ts index fc32ae8ad5..2933945e64 100644 --- a/types/hapi-decorators/index.d.ts +++ b/types/hapi-decorators/index.d.ts @@ -4,12 +4,13 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped import * as hapi from 'hapi'; +import * as Joi from 'joi'; interface ControllerStatic { new (...args: any[]): Controller; } export interface Controller { baseUrl: string; - routes: () => hapi.IRouteConfiguration[]; + routes: () => hapi.RouteConfiguration[]; } export function controller(baseUrl: string): (target: ControllerStatic) => void; interface IRouteSetup { @@ -28,61 +29,9 @@ export const put: IRouteConfig; // export const delete: IRouteConfig; export const patch: IRouteConfig; export const all: IRouteConfig; -export function config(config: hapi.IRouteAdditionalConfigurationOptions): (target: any, key: any, descriptor: any) => any; -interface IValidateConfig { - /** validation rules for incoming request headers.Values allowed: - * trueany headers allowed (no validation performed).This is the default. - falseno headers allowed (this will cause all valid HTTP requests to fail). - a Joi validation object. - a validation function using the signature function(value, options, next) where: - valuethe object containing the request headers. - optionsthe server validation options. - next(err, value)the callback function called when validation is completed. - */ - headers?: boolean | hapi.IJoi | hapi.IValidationFunction; - /** validation rules for incoming request path parameters, after matching the path against the route and extracting any parameters then stored in request.params.Values allowed: - trueany path parameters allowed (no validation performed).This is the default. - falseno path variables allowed. - a Joi validation object. - a validation function using the signature function(value, options, next) where: - valuethe object containing the path parameters. - optionsthe server validation options. - next(err, value)the callback function called when validation is completed. */ - params?: boolean | hapi.IJoi | hapi.IValidationFunction; - /** validation rules for an incoming request URI query component (the key- value part of the URI between '?' and '#').The query is parsed into its individual key- value pairs (using the qs module) and stored in request.query prior to validation.Values allowed: - trueany query parameters allowed (no validation performed).This is the default. - falseno query parameters allowed. - a Joi validation object. - a validation function using the signature function(value, options, next) where: - valuethe object containing the query parameters. - optionsthe server validation options. - next(err, value)the callback function called when validation is completed. */ - query?: boolean | hapi.IJoi | hapi.IValidationFunction; - /** validation rules for an incoming request payload (request body).Values allowed: - trueany payload allowed (no validation performed).This is the default. - falseno payload allowed. - a Joi validation object. - a validation function using the signature function(value, options, next) where: - valuethe object containing the payload object. - optionsthe server validation options. - next(err, value)the callback function called when validation is completed. */ - payload?: boolean | hapi.IJoi | hapi.IValidationFunction; - /** an optional object with error fields copied into every validation error response. */ - errorFields?: any; - /** determines how to handle invalid requests.Allowed values are: - 'error'return a Bad Request (400) error response.This is the default value. - 'log'log the error but continue processing the request. - 'ignore'take no action. - OR a custom error handler function with the signature 'function(request, reply, source, error)` where: - requestthe request object. - replythe continuation reply interface. - sourcethe source of the invalid field (e.g. 'path', 'query', 'payload'). - errorthe error object prepared for the client response (including the validation function error under error.data). */ - failAction?: string | hapi.IRouteFailFunction; - /** options to pass to Joi.Useful to set global options such as stripUnknown or abortEarly (the complete list is available here: https://github.com/hapijs/joi#validatevalue-schema-options-callback ).Defaults to no options. */ - options?: any; -} -export function validate(config: IValidateConfig): (target: any, key: any, descriptor: any) => any; +export function config(config: hapi.RouteAdditionalConfigurationOptions): (target: any, key: any, descriptor: any) => any; + +export function validate(config: hapi.RouteValidationConfigurationObject): (target: any, key: any, descriptor: any) => any; interface ICacheConfig { privacy?: string; expiresIn?: number; diff --git a/types/hapi/index.d.ts b/types/hapi/index.d.ts index 6fdef94dec..3567e50c34 100644 --- a/types/hapi/index.d.ts +++ b/types/hapi/index.d.ts @@ -1,2489 +1,2599 @@ -// Type definitions for hapi 16.0 -// Project: http://github.com/spumko/hapi -// Definitions by: Jason Swearingen +// Type definitions for hapi 16.1 +// Project: https://github.com/hapijs/hapi +// Definitions by: Jason Swearingen , AJP // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 -// Note/Disclaimer: This .d.ts was created against hapi v8.x but has been incrementally upgraded to 13.x. Some newer features/changes may be missing. YMMV. +/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + WARNING: BACKWARDS INCOMPATIBLE + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + * + * + * Removal of IPromise replaced with Promise + * Removal of IReplyStrict<> + * Removal of IReply replaced with different interfaces like: + * ReplyWithContinue + * ReplyNoContinue, etc. + * Renaming of all interfaces to remove preceding I in preparation of dtslint + */ /// -import http = require("http"); -import stream = require("stream"); import Events = require("events"); +import stream = require("stream"); +import http = require("http"); +import https = require("https"); import url = require("url"); +import zlib = require("zlib"); +import domain = require("domain"); -export interface IDictionary { - [key: string]: T; -} +import * as Boom from 'boom'; +import { + ValidationOptions as JoiValidationOptions, + Schema as JoiValidationObject, +} from 'joi'; -export interface IHeaderOptions { - append?: boolean; - separator?: string; - override?: boolean; - duplicate?: boolean; -} +import * as Catbox from 'catbox'; +import {MimosOptions} from 'mimos'; +import Podium = require('podium'); +import * as Shot from 'shot'; -/** Boom Module for errors. https://github.com/hapijs/boom - * boom provides a set of utilities for returning HTTP errors. Each utility returns a Boom error response object (instance of Error) which includes the following properties: */ -export interface IBoom extends Error { - /** if true, indicates this is a Boom object instance. */ - isBoom: boolean; - /** convenience bool indicating status code >= 500. */ - isServer: boolean; - /** the error message. */ - message: string; - /** the formatted response.Can be directly manipulated after object construction to return a custom error response.Allowed root keys: */ - output: { - /** the HTTP status code (typically 4xx or 5xx). */ - statusCode: number; - /** an object containing any HTTP headers where each key is a header name and value is the header content. */ - headers: IDictionary; - /** the formatted object used as the response payload (stringified).Can be directly manipulated but any changes will be lost if reformat() is called.Any content allowed and by default includes the following content: */ - payload: { - /** the HTTP status code, derived from error.output.statusCode. */ - statusCode: number; - /** the HTTP status message (e.g. 'Bad Request', 'Internal Server Error') derived from statusCode. */ - error: string; - /** the error message derived from error.message. */ - message: string; - }; - }; - /** reformat()rebuilds error.output using the other object properties. */ - reformat(): void; -} - -/** cache functionality via the "CatBox" module. */ -export interface ICatBoxCacheOptions { - /** a prototype function or catbox engine object. */ - engine: any; - /** 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 the default cache. If every cache includes a name, a default memory cache is provisions as well. */ - name?: string; - /** if true, allows multiple cache users to share the same segment (e.g. multiple methods using the same cache storage container). Default to false. */ - shared?: boolean; -} - -/** policy configuration for the "CatBox" module and server method options. */ -export interface ICatBoxCachePolicyOptions { - /** the cache name configured in server.cache. Defaults to the default cache. */ - cache?: string; - /** 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 server method, defaults to '#name' where 'name' is the server method name. Required when called outside of a plugin. */ - segment?: string; - /** if true, allows multiple cache provisions to share the same segment. Default to false. */ - shared?: boolean; - /** relative expiration expressed in the number of milliseconds since the item was saved in the cache. Cannot be used together with expiresAt. */ - expiresIn?: number; - /** 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. */ - expiresAt?: number; - /** 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 function(id, next) where: - id - the id string or object provided to the get() method. - next - the method called when the new item is returned with the signature function(err, value, ttl) where: - err - an error condition. - value - the new value generated. - ttl - the cache ttl value in milliseconds. Set to 0 to skip storing in the cache. Defaults to the cache global policy. */ - generateFunc?: Function; - /** 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. */ - staleIn?: number; - /** number of milliseconds to wait before checking if an item is stale. */ - staleTimeout?: number; - /** 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 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. */ - generateTimeout?: number; - /** if true, an error or timeout in the generateFunc causes the stale value to be evicted from the cache. Defaults to true */ - dropOnError?: boolean; - /** 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. */ - generateOnReadError?: boolean; - /** if false, an upstream cache write error when calling cache.get() will be passed back with the generated value when calling. Defaults to true. */ - generateIgnoreWriteError?: boolean; - /** 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 concurrent generateFunc calls beyond staleTimeout). */ - pendingGenerateTimeout?: number; -} - -/** Any connections configuration server defaults can be included to override and customize the individual connection. */ -export interface IServerConnectionOptions extends IConnectionConfigurationServerDefaults { - /** - the public hostname or IP address. Used only to set server.info.host and server.info.uri. If not configured, defaults to the operating system hostname and if not available, to 'localhost'.*/ - host?: string; - /** - sets the host name or IP address the connection will listen on.If not configured, defaults to host if present, otherwise to all available network interfaces (i.e. '0.0.0.0').Set to 127.0.0.1 or localhost to restrict connection to only those coming from the same machine.*/ - address?: string; - /** - the TCP port the connection will listen to.Defaults to an ephemeral port (0) which uses an available port when the server is started (and assigned to server.info.port).If port is a string containing a '/' character, it is used as a UNIX domain socket path and if it starts with '\.\pipe' as a Windows named pipe.*/ - port?: string | number; - /** - the full public URI without the path (e.g. 'http://example.com:8080').If present, used as the connection info.uri otherwise constructed from the connection settings.*/ - uri?: string; - /** - optional node.js HTTP (or HTTPS) http.Server object or any compatible object.If the listener needs to be manually started, set autoListen to false.If the listener uses TLS, set tls to true.*/ - listener?: any; - /** - indicates that the connection.listener will be started manually outside the framework.Cannot be specified with a port setting.Defaults to true.*/ - autoListen?: boolean; - /** caching headers configuration: */ - cache?: { - /** - an array of HTTP response status codes (e.g. 200) which are allowed to include a valid caching directive.Defaults to [200]. */ - statuses: number[]; - }; - /** - a string or string array of labels used to server.select() specific connections matching the specified labels.Defaults to an empty array [](no labels).*/ - labels?: string | string[]; - /** - used to create an HTTPS connection.The tls object is passed unchanged as options to the node.js HTTPS server as described in the node.js HTTPS documentation.Set to true when passing a listener object that has been configured to use TLS directly. */ - tls?: boolean | { key?: string; cert?: string; pfx?: string; } | Object; -} - -export interface IConnectionConfigurationServerDefaults { - /** application-specific connection configuration which can be accessed via connection.settings.app. Provides a safe place to store application configuration without potential conflicts with the framework internals. Should not be used to configure plugins which should use plugins[name]. Note the difference between connection.settings.app which is used to store configuration values and connection.app which is meant for storing run-time state. */ - app?: any; - /** if false, response content encoding is disabled. Defaults to true */ - compression?: boolean; - /** connection load limits configuration where: */ - load?: { - /** maximum V8 heap size over which incoming requests are rejected with an HTTP Server Timeout (503) response. Defaults to 0 (no limit). */ - maxHeapUsedBytes: number; - /** maximum process RSS size over which incoming requests are rejected with an HTTP Server Timeout (503) response. Defaults to 0 (no limit). */ - maxRssBytes: number; - /** maximum event loop delay duration in milliseconds over which incoming requests are rejected with an HTTP Server Timeout (503) response. Defaults to 0 (no limit). */ - maxEventLoopDelay: number; - }; - /** plugin-specific configuration which can later be accessed via connection.settings.plugins. Provides a place to store and pass connection-specific plugin configuration. plugins is an object where each key is a plugin name and the value is the configuration. Note the difference between connection.settings.plugins which is used to store configuration values and connection.plugins which is meant for storing run-time state. */ - plugins?: any; - /** controls how incoming request URIs are matched against the routing table: */ - router?: { - /** determines whether the paths '/example' and '/EXAMPLE' are considered different resources. Defaults to true. */ - isCaseSensitive: boolean; - /** removes trailing slashes on incoming paths. Defaults to false. */ - stripTrailingSlash: boolean; - }; - /** a route options object used to set the default configuration for every route. */ - routes?: IRouteAdditionalConfigurationOptions; - state?: IServerState; -} - -/** Note that the options object is deeply cloned and cannot contain any values that are unsafe to perform deep copy on.*/ -export interface IServerOptions { - /** application-specific configuration which can later be accessed via server.settings.app. Note the difference between server.settings.app which is used to store static configuration values and server.app which is meant for storing run-time state. Defaults to {}. */ - app?: any; - /** sets up server-side caching. Every server includes a default cache for storing application state. By default, a simple memory-based cache is created which has limited capacity and capabilities. hapi uses catbox for its cache which includes support for common storage solutions (e.g. Redis, MongoDB, Memcached, and Riak). Caching is only utilized if methods and plugins explicitly store their state in the cache. The server cache configuration only defines the storage container itself. cache can be assigned: - a prototype function (usually obtained by calling require() on a catbox strategy such as require('catbox-redis')). - a configuration object with the following options: - enginea prototype function or catbox engine object. - namean 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 the default cache. If every cache includes a name, a default memory cache is provisions as well. - sharedif true, allows multiple cache users to share the same segment (e.g. multiple methods using the same cache storage container). Default to false. - other options passed to the catbox strategy used. - an array of the above object for configuring multiple cache instances, each with a unique name. When an array of objects is provided, multiple cache connections are established and each array item (except one) must include a name. */ - cache?: string | ICatBoxCacheOptions | ICatBoxCacheOptions[] | any; - /** sets the default connections configuration which can be overridden by each connection where: */ - connections?: IConnectionConfigurationServerDefaults; - /** determines which logged events are sent to the console (this should only be used for development and does not affect which events are actually logged internally and recorded). Set to false to disable all console logging, or to an object*/ - debug?: boolean | { - /** - a string array of server log tags to be displayed via console.error() when the events are logged via server.log() as well as internally generated server logs. For example, to display all errors, set the option to ['error']. To turn off all console debug messages set it to false. Defaults to uncaught errors thrown in external code (these errors are handled automatically and result in an Internal Server Error response) or runtime errors due to developer error. */ - log: string[]; - /** - a string array of request log tags to be displayed via console.error() when the events are logged via request.log() as well as internally generated request logs. For example, to display all errors, set the option to ['error']. To turn off all console debug messages set it to false. Defaults to uncaught errors thrown in external code (these errors are handled automatically and result in an Internal Server Error response) or runtime errors due to developer error.*/ - request: string[]; - }; - /** file system related settings*/ - files?: { - /** sets the maximum number of file etag hash values stored in the etags cache. Defaults to 10000.*/ - etagsCacheMaxSize?: number; - }; - /** process load monitoring*/ - load?: { - /** the frequency of sampling in milliseconds. Defaults to 0 (no sampling).*/ - sampleInterval?: number; - }; - - /** options passed to the mimos module (https://github.com/hapijs/mimos) when generating the mime database used by the server and accessed via server.mime.*/ - mime?: any; - /** if true, does not load the inert (file and directory support), h2o2 (proxy support), and vision (views support) plugins automatically. The plugins can be loaded manually after construction. Defaults to false (plugins loaded). */ - minimal?: boolean; - /** plugin-specific configuration which can later be accessed via server.settings.plugins. plugins is an object where each key is a plugin name and the value is the configuration. Note the difference between server.settings.plugins which is used to store static configuration values and server.plugins which is meant for storing run-time state. Defaults to {}.*/ - plugins?: IDictionary; -} - -export interface IServerViewCompile { - (template: string, options: any): void; - (template: string, options: any, callback: (err: any, compiled: (context: any, options: any, callback: (err: any, rendered: boolean) => void) => void) => void): void; -} - -export interface IServerViewsAdditionalOptions { - /** path - the root file path used to resolve and load the templates identified when calling reply.view().Defaults to current working directory.*/ - path?: string; - /**partialsPath - the root file path where partials are located.Partials are small segments of template code that can be nested and reused throughout other templates.Defaults to no partials support (empty path). - */ - partialsPath?: string; - /**helpersPath - the directory path where helpers are located.Helpers are functions used within templates to perform transformations and other data manipulations using the template context or other inputs.Each '.js' file in the helpers directory is loaded and the file name is used as the helper name.The files must export a single method with the signature function(context) and return a string.Sub - folders are not supported and are ignored.Defaults to no helpers support (empty path).Note that jade does not support loading helpers this way.*/ - helpersPath?: string; - /**relativeTo - a base path used as prefix for path and partialsPath.No default.*/ - relativeTo?: string; - - /**layout - if set to true or a layout filename, layout support is enabled.A layout is a single template file used as the parent template for other view templates in the same engine.If true, the layout template name must be 'layout.ext' where 'ext' is the engine's extension. Otherwise, the provided filename is suffixed with the engine's extension and loaded.Disable layout when using Jade as it will handle including any layout files independently.Defaults to false.*/ - layout?: boolean | string; - /**layoutPath - the root file path where layout templates are located (using the relativeTo prefix if present). Defaults to path.*/ - layoutPath?: string; - /**layoutKeyword - the key used by the template engine to denote where primary template content should go.Defaults to 'content'.*/ - layoutKeywork?: string; - /**encoding - the text encoding used by the templates when reading the files and outputting the result.Defaults to 'utf8'.*/ - encoding?: string; - /**isCached - if set to false, templates will not be cached (thus will be read from file on every use).Defaults to true.*/ - isCached?: boolean; - /**allowAbsolutePaths - if set to true, allows absolute template paths passed to reply.view().Defaults to false.*/ - allowAbsolutePaths?: boolean; - /**allowInsecureAccess - if set to true, allows template paths passed to reply.view() to contain '../'.Defaults to false.*/ - allowInsecureAccess?: boolean; - /**compileOptions - options object passed to the engine's compile function. Defaults to empty options {}.*/ - compileOptions?: any; - /**runtimeOptions - options object passed to the returned function from the compile operation.Defaults to empty options {}.*/ - runtimeOptions?: any; - /**contentType - the content type of the engine results.Defaults to 'text/html'.*/ - contentType?: string; - /**compileMode - specify whether the engine compile() method is 'sync' or 'async'.Defaults to 'sync'.*/ - compileMode?: string; - /**context - a global context used with all templates.The global context option can be either an object or a function that takes no arguments and returns a context object.When rendering views, the global context will be merged with any context object specified on the handler or using reply.view().When multiple context objects are used, values from the global context always have lowest precedence.*/ - context?: any; -} - -export interface IServerViewsEnginesOptions extends IServerViewsAdditionalOptions { - /**- the npm module used for rendering the templates.The module object must contain: "module", the rendering function. The required function signature depends on the compileMode settings. - * If the compileMode is 'sync', the signature is compile(template, options), the return value is a function with signature function(context, options), and the method is allowed to throw errors.If the compileMode is 'async', the signature is compile(template, options, callback) where callback has the signature function(err, compiled) where compiled is a function with signature function(context, options, callback) and callback has the signature function(err, rendered).*/ - module: { - compile?(template: any, options: any): (context: any, options: any) => void; - compile?(template: any, options: any, callback: (err: any, compiled: (context: any, options: any, callback: (err: any, rendered: any) => void) => void) => void): void; - }; -} - -/**Initializes the server views manager - var Hapi = require('hapi'); - var server = new Hapi.Server(); - - server.views({ -engines: { -html: require('handlebars'), -jade: require('jade') -}, -path: '/static/templates' -}); - When server.views() is called within a plugin, the views manager is only available to plugins methods. - */ -export interface IServerViewsConfiguration extends IServerViewsAdditionalOptions { - /** - required object where each key is a file extension (e.g. 'html', 'hbr'), mapped to the npm module used for rendering the templates.Alternatively, the extension can be mapped to an object with the following options:*/ - engines: IDictionary | IServerViewsEnginesOptions; - /** defines the default filename extension to append to template names when multiple engines are configured and not explicit extension is provided for a given template. No default value.*/ - defaultExtension?: string; -} - -export interface IReplyMethods { - /** Returns control back to the framework without setting a response. If called in the handler, the response defaults to an empty payload with status code 200. - * The data argument is only used for passing back authentication data and is ignored elsewhere. */ - continue(credentialData?: any): void; - - /** Transmits a file from the file system. The 'Content-Type' header defaults to the matching mime type based on filename extension. The response flow control rules do not apply. */ - file(/** the file path. */ - path: string, - /** optional settings: */ - options?: { - /** - an optional filename to specify if sending a 'Content-Disposition' header, defaults to the basename of path*/ - filename?: string; - /** specifies whether to include the 'Content-Disposition' header with the response. Available values: - false - header is not included. This is the default value. - 'attachment' - 'inline'*/ - mode?: boolean | string; - /** if true, looks for the same filename with the '.gz' suffix for a pre-compressed version of the file to serve if the request supports content encoding. Defaults to false. */ - lookupCompressed: boolean; - }): void; - /** Concludes the handler activity by returning control over to the router with a templatized view response. - the response flow control rules apply. */ - view(/** the template filename and path, relative to the templates path configured via the server views manager. */ - template: string, - /** optional object used by the template to render context-specific result. Defaults to no context {}. */ - context?: {}, - /** optional object used to override the server's views manager configuration for this response. Cannot override isCached, partialsPath, or helpersPath which are only loaded at initialization. */ - options?: any): Response; - /** Sets a header on the response */ - header(name: string, value: string, options?: IHeaderOptions): Response; - - /** Concludes the handler activity by returning control over to the router and informing the router that a response has already been sent back directly via request.raw.res and that no further response action is needed - The response flow control rules do not apply. */ - close(options?: { - /** if false, the router will not call request.raw.res.end()) to ensure the response was ended. Defaults to true. */ - end?: boolean; - }): void; - /** Proxies the request to an upstream endpoint. - the response flow control rules do not apply. */ - - proxy(/** an object including the same keys and restrictions defined by the route proxy handler options. */ - options: IProxyHandlerConfig): void; - /** Redirects the client to the specified uri. Same as calling reply().redirect(uri). - he response flow control rules apply. */ - redirect(uri: string): ResponseRedirect; - - /** Replies with the specified response */ - response(result: any): Response; - - /** Sets a cookie on the response */ - state(name: string, value: any, options?: any): void; - - /** Clears a cookie on the response */ - unstate(name: string, options?: any): void; -} - -/** Concludes the handler activity by setting a response and returning control over to the framework where: - erran optional error response. - result an optional response payload. - Since an request can only have one response regardless if it is an error or success, the reply() method can only result in a single response value. This means that passing both an err and result will only use the err. There is no requirement for either err or result to be (or not) an Error object. The framework will simply use the first argument if present, otherwise the second. The method supports two arguments to be compatible with the common callback pattern of error first. - FLOW CONTROL: - When calling reply(), the framework waits until process.nextTick() to continue processing the request and transmit the response. This enables making changes to the returned response object before the response is sent. This means the framework will resume as soon as the handler method exits. To suspend this behavior, the returned response object supports the following methods: hold(), send() */ -export interface IReply extends IReplyMethods { - ( - err: Error, - result?: string | number | boolean | Buffer | stream.Stream | Promise | T, - /** Note that when used to return both an error and credentials in the authentication methods, reply() must be called with three arguments function(err, null, data) where data is the additional authentication information. */ - credentialData?: any): IBoom; - /** Note that if result is a Stream with a statusCode property, that status code will be used as the default response code. */ - (result: string | number | boolean | Buffer | stream.Stream | Promise | T): Response; -} - -/** Concludes the handler activity by setting a response and returning control over to the framework where: - erran optional error response. - result an optional response payload. - Since an request can only have one response regardless if it is an error or success, the reply() method can only result in a single response value. This means that passing both an err and result will only use the err. There is no requirement for either err or result to be (or not) an Error object. The framework will simply use the first argument if present, otherwise the second. The method supports two arguments to be compatible with the common callback pattern of error first. - FLOW CONTROL: - When calling reply(), the framework waits until process.nextTick() to continue processing the request and transmit the response. This enables making changes to the returned response object before the response is sent. This means the framework will resume as soon as the handler method exits. To suspend this behavior, the returned response object supports the following methods: hold(), send() */ -export interface IStrictReply extends IReplyMethods { - (err: Error, - result?: Promise | T, - /** Note that when used to return both an error and credentials in the authentication methods, reply() must be called with three arguments function(err, null, data) where data is the additional authentication information. */ - credentialData?: any): IBoom; - /** Note that if result is a Stream with a statusCode property, that status code will be used as the default response code. */ - (result: Promise | T): Response; -} - -export interface ISessionHandler { - (request: Request, reply: IReply): void; -} - -export interface IStrictSessionHandler { - (request: Request, reply: IStrictReply): void; -} - -export interface IRequestHandler { - (request: Request): T; -} - -export interface IFailAction { - (source: string, error: any, next: () => void): void; -} -/** generates a reverse proxy handler */ -export interface IProxyHandlerConfig { - /** the upstream service host to proxy requests to. The same path on the client request will be used as the path on the host.*/ - host?: string; - /** the upstream service port. */ - port?: number; - /** The protocol to use when making a request to the proxied host: - 'http' - 'https'*/ - protocol?: string; - /** an absolute URI used instead of the incoming host, port, protocol, path, and query. Cannot be used with host, port, protocol, or mapUri.*/ - uri?: string; - /** if true, forwards the headers sent from the client to the upstream service being proxied to, headers sent from the upstream service will also be forwarded to the client. Defaults to false.*/ - passThrough?: boolean; - /** localStatePassThrough - if false, any locally defined state is removed from incoming requests before being passed upstream. This is a security feature to prevent local state (e.g. authentication cookies) from leaking upstream to other servers along with the cookies intended for those servers. This value can be overridden on a per state basis via the server.state() passThrough option. Defaults to false.*/ - localStatePassThrough?: boolean; - /**acceptEncoding - if false, does not pass-through the 'Accept-Encoding' HTTP header which is useful when using an onResponse post-processing to avoid receiving an encoded response (e.g. gzipped). Can only be used together with passThrough. Defaults to true (passing header).*/ - acceptEncoding?: boolean; - /** rejectUnauthorized - sets the rejectUnauthorized property on the https agent making the request. This value is only used when the proxied server uses TLS/SSL. When set it will override the node.js rejectUnauthorized property. If false then ssl errors will be ignored. When true the server certificate is verified and an 500 response will be sent when verification fails. This shouldn't be used alongside the agent setting as the agent will be used instead. Defaults to the https agent default value of true.*/ - rejectUnauthorized?: boolean; - /**if true, sets the 'X-Forwarded-For', 'X-Forwarded-Port', 'X-Forwarded-Proto' headers when making a request to the proxied upstream endpoint. Defaults to false.*/ - xforward?: boolean; - /** the maximum number of HTTP redirections allowed, to be followed automatically by the handler. Set to false or 0 to disable all redirections (the response will contain the redirection received from the upstream service). If redirections are enabled, no redirections (301, 302, 307, 308) will be passed along to the client, and reaching the maximum allowed redirections will return an error response. Defaults to false.*/ - redirects?: boolean | number; - /**number of milliseconds before aborting the upstream request. Defaults to 180000 (3 minutes).*/ - timeout?: number; - /** a function used to map the request URI to the proxied URI. Cannot be used together with host, port, protocol, or uri. The function signature is function(request, callback) where: - request - is the incoming request object. - callback - is function(err, uri, headers) where: - err - internal error condition. - uri - the absolute proxy URI. - headers - optional object where each key is an HTTP request header and the value is the header content.*/ - mapUri?(request: Request, callback: (err: any, uri: string, headers?: { [key: string]: string }) => void): void; - /** a custom function for processing the response from the upstream service before sending to the client. Useful for custom error handling of responses from the proxied endpoint or other payload manipulation. Function signature is function(err, res, request, reply, settings, ttl) where: - err - internal or upstream error returned from attempting to contact the upstream proxy. - res - the node response object received from the upstream service. res is a readable stream (use the wreck module read method to easily convert it to a Buffer or string). - request - is the incoming request object. - reply - the reply interface function. - settings - the proxy handler configuration. - ttl - the upstream TTL in milliseconds if proxy.ttl it set to 'upstream' and the upstream response included a valid 'Cache-Control' header with 'max-age'.*/ - onResponse?( - err: any, - res: http.ServerResponse, - req: Request, - reply: IReply, - settings: IProxyHandlerConfig, - ttl: number): void; - /** if set to 'upstream', applies the upstream response caching policy to the response using the response.ttl() method (or passed as an argument to the onResponse method if provided).*/ - ttl?: number; - /** - a node http(s) agent to be used for connections to upstream server. see https://nodejs.org/api/http.html#http_class_http_agent */ - agent?: http.Agent; - /** sets the maximum number of sockets available per outgoing proxy host connection. false means use the wreck module default value (Infinity). Does not affect non-proxy outgoing client connections. Defaults to Infinity.*/ - maxSockets?: boolean | number; -} -/** TODO: fill in joi definition */ -export interface IJoi { } - -/** a validation function using the signature function(value, options, next) */ -export interface IValidationFunction { - ( - /** the object containing the path parameters. */ - value: any, - /** the server validation options. */ - options: any, - /** the callback function called when validation is completed. */ - next: (err: any, value: any) => void): void; -} -/** a custom error handler function with the signature 'function(request, reply, source, error)` */ -export interface IRouteFailFunction { - /** a custom error handler function with the signature 'function(request, reply, source, error)` */ - (/** - the [request object]. */ - request: Request, - /** the continuation reply interface. */ - reply: IReply, - /** the source of the invalid field (e.g. 'path', 'query', 'payload'). */ - source: string, - /** the error object prepared for the client response (including the validation function error under error.data). */ - error: any): void; -} - -/** Each route can be customize to change the default behavior of the request lifecycle using the following options: */ -export interface IRouteAdditionalConfigurationOptions { - /** application specific configuration.Should not be used by plugins which should use plugins[name] instead. */ - app?: any; - /** authentication configuration.Value can be: false to disable authentication if a default strategy is set. - a string with the name of an authentication strategy registered with server.auth.strategy(). - an object */ - auth?: boolean | string | - { - /** the authentication mode.Defaults to 'required' if a server authentication strategy is configured, otherwise defaults to no authentication.Available values: - 'required'authentication is required. - 'optional'authentication is optional (must be valid if present). - 'try'same as 'optional' but allows for invalid authentication. */ - mode?: string; - /** a string array of strategy names in order they should be attempted.If only one strategy is used, strategy can be used instead with the single string value.Defaults to the default authentication strategy which is available only when a single strategy is configured. */ - strategies?: string | string[]; - /** if set, the payload (in requests other than 'GET' and 'HEAD') is authenticated after it is processed.Requires a strategy with payload authentication support (e.g.Hawk).Cannot be set to a value other than 'required' when the scheme sets the options.payload to true.Available values: - falseno payload authentication.This is the default value. - 'required'payload authentication required.This is the default value when the scheme sets options.payload to true. - 'optional'payload authentication performed only when the client includes payload authentication information (e.g.hash attribute in Hawk). */ - payload?: string; - /** the application scope required to access the route.Value can be a scope string or an array of scope strings.The authenticated credentials object scope property must contain at least one of the scopes defined to access the route.Set to false to remove scope requirements.Defaults to no scope required. */ - scope?: string | string[] | boolean; - /** the required authenticated entity type.If set, must match the entity value of the authentication credentials.Available values: - anythe authentication can be on behalf of a user or application.This is the default value. - userthe authentication must be on behalf of a user. - appthe authentication must be on behalf of an application. */ - entity?: string; - /** - * an object or array of objects specifying the route access rules. Each rule is evaluated against an incoming - * request and access is granted if at least one rule matches. Each rule object must include at least one of: - */ - access?: IRouteAdditionalConfigurationAuthAccess | IRouteAdditionalConfigurationAuthAccess[]; - }; - /** an object passed back to the provided handler (via this) when called. */ - bind?: any; - /** if the route method is 'GET', the route can be configured to include caching directives in the response using the following options */ - cache?: { - /** mines the privacy flag included in clientside caching using the 'Cache-Control' header.Values are: - fault'no privacy flag.This is the default setting. - 'public'mark the response as suitable for public caching. - 'private'mark the response as suitable only for private caching. */ - privacy: string; - /** relative expiration expressed in the number of milliseconds since the item was saved in the cache.Cannot be used together with expiresAt. */ - expiresIn: number; - /** time of day expressed in 24h notation using the 'HH:MM' format, at which point all cache records for the route expire.Cannot be used together with expiresIn. */ - expiresAt: string; - }; - /** the Cross- Origin Resource Sharing protocol allows browsers to make cross- origin API calls.CORS is required by web applications running inside a browser which are loaded from a different domain than the API server.CORS headers are disabled by default. To enable, set cors to true, or to an object with the following options: */ - cors?: { - /** a strings array of allowed origin servers ('Access-Control-Allow-Origin').The array can contain any combination of fully qualified origins along with origin strings containing a wildcard '' character, or a single `''origin string. Defaults to any origin['*']`. */ - origin?: string[]; - /** if true, matches the value of the incoming 'Origin' header to the list of origin values ('*' matches anything) and if a match is found, uses that as the value of the 'Access-Control-Allow-Origin' response header.When false, the origin config is returned as- is.Defaults to true. */ - matchOrigin?: boolean; - /** if false, prevents the connection from returning the full list of non- wildcard origin values if the incoming origin header does not match any of the values.Has no impact if matchOrigin is set to false.Defaults to true. */ - isOriginExposed?: boolean; - /** number of seconds the browser should cache the CORS response ('Access-Control-Max-Age').The greater the value, the longer it will take before the browser checks for changes in policy.Defaults to 86400 (one day). */ - maxAge?: number; - /** a strings array of allowed headers ('Access-Control-Allow-Headers').Defaults to ['Authorization', 'Content-Type', 'If-None-Match']. */ - headers?: string[]; - /** a strings array of additional headers to headers.Use this to keep the default headers in place. */ - additionalHeaders?: string[]; - /** a strings array of allowed HTTP methods ('Access-Control-Allow-Methods').Defaults to ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'OPTIONS']. */ - methods?: string[]; - /** a strings array of additional methods to methods.Use this to keep the default methods in place. */ - additionalMethods?: string[]; - /** a strings array of exposed headers ('Access-Control-Expose-Headers').Defaults to ['WWW-Authenticate', 'Server-Authorization']. */ - exposedHeaders?: string[]; - /** a strings array of additional headers to exposedHeaders.Use this to keep the default headers in place. */ - additionalExposedHeaders?: string[]; - /** if true, allows user credentials to be sent ('Access-Control-Allow-Credentials').Defaults to false. */ - credentials?: boolean; - /** if false, preserves existing CORS headers set manually before the response is sent.Defaults to true. */ - override?: boolean; - }; - /** defines the behavior for serving static resources using the built-in route handlers for files and directories: */ - files?: {/** determines the folder relative paths are resolved against when using the file and directory handlers. */ - relativeTo: string; - }; - - /** an alternative location for the route handler option. */ - handler?: ISessionHandler | IStrictSessionHandler | string | IRouteHandlerConfig; - /** an optional unique identifier used to look up the route using server.lookup(). */ - id?: number; - /** optional arguments passed to JSON.stringify() when converting an object or error response to a string payload.Supports the following: */ - json?: { - /** the replacer function or array.Defaults to no action. */ - replacer?: Function | string[]; - /** number of spaces to indent nested object keys.Defaults to no indentation. */ - space?: number | string; - /** string suffix added after conversion to JSON string.Defaults to no suffix. */ - suffix?: string; - }; - /** enables JSONP support by setting the value to the query parameter name containing the function name used to wrap the response payload.For example, if the value is 'callback', a request comes in with 'callback=me', and the JSON response is '{ "a":"b" }', the payload will be 'me({ "a":"b" });'.Does not work with stream responses. */ - jsonp?: string; - /** determines how the request payload is processed: */ - payload?: { - /** the type of payload representation requested. The value must be one of: - 'data'the incoming payload is read fully into memory.If parse is true, the payload is parsed (JSON, formdecoded, multipart) based on the 'Content- Type' header.If parse is false, the raw Buffer is returned.This is the default value except when a proxy handler is used. - 'stream'the incoming payload is made available via a Stream.Readable interface.If the payload is 'multipart/form-data' and parse is true, fields values are presented as text while files are provided as streams.File streams from a 'multipart/form-data' upload will also have a property hapi containing filename and headers properties. - 'file'the incoming payload in written to temporary file in the directory specified by the server's payload.uploads settings. If the payload is 'multipart/ formdata' and parse is true, fields values are presented as text while files are saved. Note that it is the sole responsibility of the application to clean up the files generated by the framework. This can be done by keeping track of which files are used (e.g. using the request.app object), and listening to the server 'response' event to perform any needed cleaup. */ - output?: string; - /** can be true, false, or gunzip; determines if the incoming payload is processed or presented raw. true and gunzip includes gunzipping when the appropriate 'Content-Encoding' is specified on the received request. If parsing is enabled and the 'Content-Type' is known (for the whole payload as well as parts), the payload is converted into an object when possible. If the format is unknown, a Bad Request (400) error response is sent. Defaults to true, except when a proxy handler is used. The supported mime types are: - 'application/json' - 'application/x-www-form-urlencoded' - 'application/octet-stream' - 'text/ *' - 'multipart/form-data' */ - parse?: string | boolean; - /** a string or an array of strings with the allowed mime types for the endpoint.Defaults to any of the supported mime types listed above.Note that allowing other mime types not listed will not enable them to be parsed, and that if parsing mode is 'parse', the request will result in an error response. */ - allow?: string | string[]; - /** a mime type string overriding the 'Content-Type' header value received.Defaults to no override. */ - override?: string; - /** limits the size of incoming payloads to the specified byte count.Allowing very large payloads may cause the server to run out of memory.Defaults to 1048576 (1MB). */ - maxBytes?: number; - /** payload reception timeout in milliseconds.Sets the maximum time allowed for the client to transmit the request payload (body) before giving up and responding with a Request Timeout (408) error response.Set to false to disable.Defaults to 10000 (10 seconds). */ - timeout?: number; - /** the directory used for writing file uploads.Defaults to os.tmpDir(). */ - uploads?: string; - /** determines how to handle payload parsing errors. Allowed values are: - 'error'return a Bad Request (400) error response. This is the default value. - 'log'report the error but continue processing the request. - 'ignore'take no action and continue processing the request. */ - failAction?: string; - }; - /** pluginspecific configuration.plugins is an object where each key is a plugin name and the value is the plugin configuration. */ - plugins?: IDictionary; - /** an array with [route prerequisites] methods which are executed in serial or in parallel before the handler is called. */ - pre?: any[]; - /** validation rules for the outgoing response payload (response body).Can only validate object response: */ - response?: { - /** the default HTTP status code when the payload is empty. Value can be 200 or 204. - Note that a 200 status code is converted to a 204 only at the time or response transmission - (the response status code will remain 200 throughout the request lifecycle unless manually set). Defaults to 200. */ - emptyStatusCode?: number; - /** the default response object validation rules (for all non-error responses) expressed as one of: - true - any payload allowed (no validation performed). This is the default. - false - no payload allowed. - a Joi validation object. - a validation function using the signature function(value, options, next) where: - value - the object containing the response object. - options - the server validation options. - next(err) - the callback function called when validation is completed. */ - schema?: boolean | any; - /** HTTP status- codespecific validation rules.The status key is set to an object where each key is a 3 digit HTTP status code and the value has the same definition as schema.If a response status code is not present in the status object, the schema definition is used, expect for errors which are not validated by default. */ - status?: { [statusCode: number]: boolean | any }; - /** the percent of responses validated (0100).Set to 0 to disable all validation.Defaults to 100 (all responses). */ - sample?: number; - /** defines what to do when a response fails validation.Options are: - errorreturn an Internal Server Error (500) error response.This is the default value. - loglog the error but send the response. */ - failAction?: string; - /** if true, applies the validation rule changes to the response.Defaults to false. */ - modify?: boolean; - /** options to pass to Joi.Useful to set global options such as stripUnknown or abortEarly (the complete list is available here: https://github.com/hapijs/joi#validatevalue-schema-options-callback ).Defaults to no options. */ - options?: any; - }; - /** sets common security headers (disabled by default).To enable set security to true or to an object with the following options */ - security?: boolean | { - /** controls the 'Strict-Transport-Security' header.If set to true the header will be set to max- age=15768000, if specified as a number the maxAge parameter will be set to that number.Defaults to true.You may also specify an object with the following fields: */ - hsts?: boolean | number | { - /** the max- age portion of the header, as a number.Default is 15768000. */ - maxAge?: number; - /** a boolean specifying whether to add the includeSubdomains flag to the header. */ - includeSubdomains?: boolean; - /** a boolean specifying whether to add the 'preload' flag (used to submit domains inclusion in Chrome's HTTP Strict Transport Security (HSTS) preload list) to the header. */ - preload?: boolean; - }; - /** controls the 'X-Frame-Options' header.When set to true the header will be set to DENY, you may also specify a string value of 'deny' or 'sameorigin'.To use the 'allow-from' rule, you must set this to an object with the following fields: */ - xframe?: { - /** either 'deny', 'sameorigin', or 'allow-from' */ - rule: string; - /** when rule is 'allow-from' this is used to form the rest of the header, otherwise this field is ignored.If rule is 'allow-from' but source is unset, the rule will be automatically changed to 'sameorigin'. */ - source: string; - }; - /** boolean that controls the 'X-XSS-PROTECTION' header for IE.Defaults to true which sets the header to equal '1; mode=block'.NOTE: This setting can create a security vulnerability in versions of IE below 8, as well as unpatched versions of IE8.See here and here for more information.If you actively support old versions of IE, it may be wise to explicitly set this flag to false. */ - xss?: boolean; - /** boolean controlling the 'X-Download-Options' header for IE, preventing downloads from executing in your context.Defaults to true setting the header to 'noopen'. */ - noOpen?: boolean; - /** boolean controlling the 'X-Content-Type-Options' header.Defaults to true setting the header to its only and default option, 'nosniff'. */ - noSniff?: boolean; - }; - /** HTTP state management (cookies) allows the server to store information on the client which is sent back to the server with every request (as defined in RFC 6265).state supports the following options: */ - state?: { - /** determines if incoming 'Cookie' headers are parsed and stored in the request.state object.Defaults to true. */ - parse: boolean; - /** determines how to handle cookie parsing errors.Allowed values are: - 'error'return a Bad Request (400) error response.This is the default value. - 'log'report the error but continue processing the request. - 'ignore'take no action. */ - failAction: string; - }; - /** request input validation rules for various request components.When using a Joi validation object, the values of the other inputs (i.e.headers, query, params, payload, and auth) are made available under the validation context (accessible in rules as Joi.ref('$query.key')).Note that validation is performed in order(i.e.headers, params, query, payload) and if type casting is used (converting a string to number), the value of inputs not yet validated will reflect the raw, unvalidated and unmodified values.The validate object supports: */ - validate?: { - /** validation rules for incoming request headers.Values allowed: - * trueany headers allowed (no validation performed).This is the default. - falseno headers allowed (this will cause all valid HTTP requests to fail). - a Joi validation object. - a validation function using the signature function(value, options, next) where: - valuethe object containing the request headers. - optionsthe server validation options. - next(err, value)the callback function called when validation is completed. - */ - headers?: boolean | IJoi | IValidationFunction; - - /** validation rules for incoming request path parameters, after matching the path against the route and extracting any parameters then stored in request.params.Values allowed: - trueany path parameters allowed (no validation performed).This is the default. - falseno path variables allowed. - a Joi validation object. - a validation function using the signature function(value, options, next) where: - valuethe object containing the path parameters. - optionsthe server validation options. - next(err, value)the callback function called when validation is completed. */ - params?: boolean | IJoi | IValidationFunction; - /** validation rules for an incoming request URI query component (the key- value part of the URI between '?' and '#').The query is parsed into its individual key- value pairs (using the qs module) and stored in request.query prior to validation.Values allowed: - trueany query parameters allowed (no validation performed).This is the default. - falseno query parameters allowed. - a Joi validation object. - a validation function using the signature function(value, options, next) where: - valuethe object containing the query parameters. - optionsthe server validation options. - next(err, value)the callback function called when validation is completed. */ - query?: boolean | IJoi | IValidationFunction; - /** validation rules for an incoming request payload (request body).Values allowed: - trueany payload allowed (no validation performed).This is the default. - falseno payload allowed. - a Joi validation object. - a validation function using the signature function(value, options, next) where: - valuethe object containing the payload object. - optionsthe server validation options. - next(err, value)the callback function called when validation is completed. */ - payload?: boolean | IJoi | IValidationFunction; - /** an optional object with error fields copied into every validation error response. */ - errorFields?: any; - /** determines how to handle invalid requests.Allowed values are: - 'error'return a Bad Request (400) error response.This is the default value. - 'log'log the error but continue processing the request. - 'ignore'take no action. - OR a custom error handler function with the signature 'function(request, reply, source, error)` where: - requestthe request object. - replythe continuation reply interface. - sourcethe source of the invalid field (e.g. 'path', 'query', 'payload'). - errorthe error object prepared for the client response (including the validation function error under error.data). */ - failAction?: string | IRouteFailFunction; - /** options to pass to Joi.Useful to set global options such as stripUnknown or abortEarly (the complete list is available here: https://github.com/hapijs/joi#validatevalue-schema-options-callback ).Defaults to no options. */ - options?: any; - }; - /** define timeouts for processing durations: */ - timeout?: { - /** response timeout in milliseconds.Sets the maximum time allowed for the server to respond to an incoming client request before giving up and responding with a Service Unavailable (503) error response.Disabled by default (false). */ - server: boolean | number; - /** by default, node sockets automatically timeout after 2 minutes.Use this option to override this behavior.Defaults to undefined which leaves the node default unchanged.Set to false to disable socket timeouts. */ - socket: boolean | number; - }; - - /** ONLY WHEN ADDING NEW ROUTES (not when setting defaults). - *route description used for generating documentation (string). - */ - description?: string; - /** ONLY WHEN ADDING NEW ROUTES (not when setting defaults). - *route notes used for generating documentation (string or array of strings). - */ - notes?: string | string[]; - /** ONLY WHEN ADDING NEW ROUTES (not when setting defaults). - *route tags used for generating documentation (array of strings). - */ - tags?: string[]; - - /** Enable logging of routes - */ - log?: boolean; +export interface Dictionary { + [key: string]: T; } /** - * specifying the route access rules. Each rule is evaluated against an incoming request and access is granted if at least one rule matches + * Server + * The Server object is the main application container. The server manages all incoming connections along with all the facilities provided by the framework. A server can contain more than one connection (e.g. listen to port 80 and 8080). + * [See docs](https://hapijs.com/api/16.1.1#server) + * [See docs](https://hapijs.com/api/16.1.1#server-properties) + * [See docs](https://hapijs.com/api/16.1.1#server-events) */ -export interface IRouteAdditionalConfigurationAuthAccess { - /** - * the application scope required to access the route. Value can be a scope string or an array of scope strings. - * The authenticated credentials object scope property must contain at least one of the scopes defined to access the route. - * If a scope string begins with a + character, that scope is required. If a scope string begins with a ! character, - * that scope is forbidden. For example, the scope ['!a', '+b', 'c', 'd'] means the incoming request credentials' - * scope must not include 'a', must include 'b', and must include on of 'c' or 'd'. You may also access properties - * on the request object (query and params} to populate a dynamic scope by using {} characters around the property name, - * such as 'user-{params.id}'. Defaults to false (no scope requirements). - */ - scope?: string | string[] | boolean; - /** the required authenticated entity type. If set, must match the entity value of the authentication credentials. Available values: - * any - the authentication can be on behalf of a user or application. This is the default value. - * user - the authentication must be on behalf of a user which is identified by the presence of a user attribute in the credentials object returned by the authentication strategy. - * app - the authentication must be on behalf of an application which is identified by the lack of presence of a user attribute in the credentials object returned by the authentication strategy. - */ - entity?: string; +export class Server extends Podium { + /** + * Creates a new Server object + */ + constructor(options?: ServerOptions); + + /** + * Provides a safe place to store server-specific run-time application data without potential conflicts with the framework internals. The data can be accessed whenever the server is accessible. Initialized with an empty object. + * [See docs](https://hapijs.com/api/16.1.1#serverapp) + */ + app?: any; + /** + * An array containing the server's connections. When the server object is returned from server.select(), the connections array only includes the connections matching the selection criteria. + * [See docs](https://hapijs.com/api/16.1.1#serverconnections) + */ + connections: ServerConnection[]; + /** + * When the server contains exactly one connection, info is an object containing information about the sole connection + * When the server contains more than one connection, each server.connections array member provides its own connection.info. + * [See docs](https://hapijs.com/api/16.1.1#serverinfo) + */ + info: ServerConnectionInfo | null; + /** + * An object containing the process load metrics (when load.sampleInterval is enabled): + * [See docs](https://hapijs.com/api/16.1.1#serverload) + */ + load: { + /** event loop delay milliseconds. */ + eventLoopDelay: number; + /** V8 heap usage. */ + heapUsed: number; + /** RSS memory usage. */ + rss: number; + }; + /** + * When the server contains exactly one connection, listener is the node HTTP server object of the sole connection. + * When the server contains more than one connection, each server.connections array member provides its own connection.listener. + * [See docs](https://hapijs.com/api/16.1.1#serverlistener) + */ + listener: ServerListener | null; + /** + * An object providing access to the server methods cs://hapijs.com/api/16.1.1#servermethodname-method-options} where each server method name is an object property. + * [See docs](https://hapijs.com/api/16.1.1#servermethods) + */ + methods: Dictionary; + /** + * Provides access to the server MIME database used for setting content-type information. The object must not be modified directly but only through the mime server setting. + * [See docs](https://hapijs.com/api/16.1.1#servermime) + */ + readonly mime: {path(path: string): {type: string}}; + /** + * An object containing the values exposed by each plugin registered where each key is a plugin name and the values are the exposed properties by each plugin using server.expose(). Plugins may set the value of the server.plugins[name] object directly or via the server.expose() method. + * [See docs](https://hapijs.com/api/16.1.1#serverplugins) + */ + plugins: PluginsStates; + /** + * The realm object contains server-wide or plugin-specific state that can be shared across various methods. For example, when calling server.bind(), the active realm settings.bind property is set which is then used by routes and extensions added at the same level (server root or plugin). Realms are a limited version of a sandbox where plugins can maintain state used by the framework when adding routes, extensions, and other properties. + * [See docs](https://hapijs.com/api/16.1.1#serverrealm) + */ + readonly realm: ServerRealm; + /** + * When the server contains exactly one connection, registrations is an object where each key is a registered plugin name + * When the server contains more than one connection, each server.connections array member provides its own connection.registrations. + * TODO check and offer PR to update Hapi docs: Assuming readonly. + * [See docs](https://hapijs.com/api/16.1.1#serverregistrations) + */ + readonly registrations: ServerRegisteredPlugins; + /** + * The root server object containing all the connections and the root server methods (e.g. start(), stop(), connection()). + * TODO, check and offer PR to update Hapi docs: Marked as optional as presumably root server does not reference itself. + * [See docs](https://hapijs.com/api/16.1.1#serverroot) + */ + root?: Server; + /** + * The server configuration object after defaults applied. + * [See docs](https://hapijs.com/api/16.1.1#serversettings) + */ + settings: ServerOptions; + /** + * The hapi module version number. + * [See docs](https://hapijs.com/api/16.1.1#serverversion) + */ + version: string; + + /** + * [See docs](https://hapijs.com/api/16.1.1#serverauthapi) + * [See docs](https://hapijs.com/api/16.1.1#serverauthdefaultoptions) + * [See docs](https://hapijs.com/api/16.1.1#serverauthschemename-scheme) + * [See docs](https://hapijs.com/api/16.1.1#serverauthstrategyname-scheme-mode-options) + * [See docs](https://hapijs.com/api/16.1.1#serverauthteststrategy-request-next) + */ + auth: ServerAuth; + + /** + * Sets a global context used as the default bind object when adding a route or an extension + * When setting context inside a plugin, the context is applied only to methods set up by the plugin. Note that the context applies only to routes and extensions added after it has been set. Ignored if the method being bound is an arrow function. + * @param context the object used to bind this in handler and extension methods. + * [See docs](https://hapijs.com/api/16.1.1#serverbindcontext) + */ + bind(context: any): void; + /** + * [See docs](https://hapijs.com/api/16.1.1#servercacheoptions) + * [See docs](https://hapijs.com/api/16.1.1#servercacheprovisionoptions-callback) + */ + cache: ServerCacheMethod; + /** + * Adds an incoming server connection + * Returns a server object with the new connections selected. + * Must be called before any other server method that modifies connections is called for it to apply to the new connection (e.g. server.state()) + * Note that the options object is deeply cloned (with the exception of listener which is shallowly copied) and cannot contain any values that are unsafe to perform deep copy on. + * + * [See docs](https://hapijs.com/api/16.1.1#serverconnectionoptions) for various advantage topics covering usage and caveats around use of the function in plugin register(), connectionless plugins calling connection(), etc. + * @param connection a connection configuration object or array of objects + */ + connection(options?: ServerConnectionOptions[]): Server; + connection(options?: ServerConnectionOptions): Server; + // connection: (options: ServerConnectionOptions[] | ServerConnectionOptions) => Server; + /** + * Registers a custom content decoding compressor to extend the built-in support for 'gzip' and 'deflate' + * [See docs](https://hapijs.com/api/16.1.1#serverdecoderencoding-decoder) + * @param encoding the decoder name string. + * @param decoder a function using the signature function(options) where options are the encoding specific options configured in the route payload.compression configuration option, and the return value is an object compatible with the output of node's zlib.createGunzip(). + */ + decoder(encoding: string, decoder: ((options: CompressionDecoderSettings) => zlib.Gunzip)): void; + /** + * Extends various framework interfaces with custom methods + * Note that decorations apply to the entire server and all its connections regardless of current selection. + * [See docs](https://hapijs.com/api/16.1.1#serverdecoratetype-property-method-options) + * + * NOTE: it's not possible to type the result of this action. + * It's advised that in a custom definition file, you extend the ReplyNoContinue + * and ReplyWithContinue functions. See Inert `.file` for an example. + * Or if it is not part of a library / plugin then you use a namespace within + * your code to type the request, server and or reply. See + * [tests/server/decorate.ts](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/hapi/tests/server/decorate.ts) + * for examples. + * @param type the interface being decorated. Supported types: + * * 'request' - adds methods to the Request object. + * * 'reply' - adds methods to the reply interface. + * * 'server' - adds methods to the Server object. + * @param property the object decoration key name. + * @param method the extension function or other value. + * @param options if the type is 'request', supports the following optional settings: + * * apply - if true, the method function is invoked using the signature function(request) where request is the current request object and the returned value is assigned as the decoration. + */ + decorate(type: 'request' | 'reply' | 'server', property: string, method: Function): void; + decorate(type: 'request', property: string, method: Function, options?: {apply: false}): void; + decorate(type: 'request', property: string, method: (request: Request) => Function, options: {apply: true}): void; + /** + * The server.decorate('server', ...) method can modify this prototype/interface. + * Have disabled these typings as there is a better alternative, see example in: tests/server/decorate.ts + * [And discussion here](https://github.com/DefinitelyTyped/DefinitelyTyped/issues/14517#issuecomment-298891630) + */ + // [index: string]: any; + /** + * Used within a plugin to declare a required dependency on other plugins + * The after method is identical to setting a server extension point on 'onPreStart'. Connectionless plugins (those with attributes.connections set to false) can only depend on other connectionless plugins (server initialization will fail even of the dependency is loaded but is not connectionless). + * Dependencies can also be set via the register attributes property (does not support setting after). + * [See docs](https://hapijs.com/api/16.1.1#serverdependencydependencies-after) + * @param dependencies a single string or array of plugin name strings which must be registered in order for this plugin to operate. Plugins listed must be registered before the server is initialized or started. Does not provide version dependency which should be implemented using npm peer dependencies. + * @param after an optional function called after all the specified dependencies have been registered and before the server starts. The function is only called if the server is initialized or started. If a circular dependency is detected, an exception is thrown (e.g. two plugins each has an after function to be called after the other). The function signature is function(server, next) + */ + dependency(dependencies: string | string[], after?: AfterDependencyLoadCallback): void; + /** + * Emits a custom application event update to all the subscribed listeners + * Note that events must be registered before they can be emitted or subscribed to by calling server.event(events). This is done to detect event name misspelling and invalid event activities. + * [See docs](https://hapijs.com/api/16.1.1#serveremitcriteria-data-callback) + * @param criteria the event update criteria which if an object can have the following optional keys (unless noted otherwise): + * * name - the event name string (required). + * * channel - the channel name string. + * * tags - a tag string or array of tag strings. + * @param data the value emitted to the subscribers. If data is a function, the function signature is function() and it called once to generate (return value) the actual data emitted to the listeners. If no listeners match the event, the data function is not invoked. + * @param callback an optional callback method invoked when all subscribers have been notified using the signature function(). The callback is called only after all the listeners have been notified, including any event updates emitted earlier (the order of event updates are guaranteed to be in the order they were emitted). + */ + emit(criteria: string | {name: string, channel?: string, tags?: string | string[]}, data: any, callback?: () => void): void; + /** + * Registers a custom content encoding compressor to extend the built-in support for 'gzip' and 'deflate' + * [See docs](https://hapijs.com/api/16.1.1#serverencoderencoding-encoder) + * @param encoding the encoder name string. + * @param encoder a function using the signature function(options) where options are the encoding specific options configured in the route compression configuration option, and the return value is an object compatible with the output of node's zlib.createGzip(). + */ + encoder(encoding: string, encoder: ((options: CompressionEncoderSettings) => zlib.Gzip)): void; + /** + * Register custom application events + * [See docs](https://hapijs.com/api/16.1.1#servereventevents) + * @param events see ApplicationEvent + */ + event(events: ApplicationEvent[]): void; + event(events: ApplicationEvent): void; + /** + * Used within a plugin to expose a property via server.plugins[name] + * [See docs](https://hapijs.com/api/16.1.1#serverexposekey-value) + * @param key the key assigned (server.plugins[name][key]). + * @param value the value assigned. + */ + expose(key: string, value: any): void; + /** + * Merges an object into to the existing content of server.plugins[name] + * Note that all properties of obj are deeply cloned into server.plugins[name], so you should avoid using this method for exposing large objects that may be expensive to clone or singleton objects such as database client objects. Instead favor the server.expose(key, value) form, which only copies a reference to value. + * [See docs](https://hapijs.com/api/16.1.1#serverexposeobj) + * @param obj the object merged into the exposed properties container. + */ + expose(obj: Object): void; + /** + * Registers an extension function in one of the available extension points + * [See docs](https://hapijs.com/api/16.1.1#serverextevents) + * @param events see @ServerExtConfigurationObject + */ + ext(events: ServerExtConfigurationObject[]): void; + ext(events: ServerExtConfigurationObject): void; + /** + * Registers a single extension event using the same properties as used in server.ext(events), but passed as arguments. + * [See docs](https://hapijs.com/api/16.1.1#serverextevent-method-options) + * @param event the extension point event name. + * @param method a function or an array of functions to be executed at a specified point during request processing. + * @param options + */ + ext(event: ServerExtPoints, method: ServerExtMethod[], options?: ServerExtOptions): void; + ext(event: ServerExtPoints, method: ServerExtMethod, options?: ServerExtOptions): void; + /** + * Registers a new handler type to be used in routes + * The method function can have a defaults object or function property. If the property is set to an object, that object is used as the default route config for routes using this handler. If the property is set to a function, the function uses the signature function(method) and returns the route default configuration. + * [See docs](https://hapijs.com/api/16.1.1#serverhandlername-method) + * @param name string name for the handler being registered. Cannot override any previously registered type. + * @param method the function used to generate the route handler using the signature function(route, options) where: + * * route - the route public interface object. + * * options - the configuration object provided in the handler config. + */ + handler(name: string, method: MakeRouteHandler): void; + /** + * Initializes the server (starts the caches, finalizes plugin registration) but does not start listening on the connection ports + * Note that if the method fails and the callback includes an error, the server is considered to be in an undefined state and should be shut down. In most cases it would be impossible to fully recover as the various plugins, caches, and other event listeners will get confused by repeated attempts to start the server or make assumptions about the healthy state of the environment. It is recommended to assert that no error has been returned after calling initialize() to abort the process when the server fails to start properly. If you must try to resume after an error, call server.stop() first to reset the server state. + * [See docs](https://hapijs.com/api/16.1.1#serverinitializecallback) + * @param callback the callback method when server initialization is completed or failed with the signature function(err) + */ + initialize(callback: (err: Error) => void): void; + initialize(): Promise; + /** + * When the server contains exactly one connection, injects a request into the sole connection simulating an incoming HTTP request without making an actual socket connection. Injection is useful for testing purposes as well as for invoking routing logic internally without the overhead or limitations of the network stack. Utilizes the shot module for performing injections, with some additional options and response properties + * If no callback is provided, a Promise object is returned. + * When the server contains more than one connection, each server.connections array member provides its own connection.inject(). + * [See docs](https://hapijs.com/api/16.1.1#serverinjectoptions-callback) + * @param options can be assigned a string with the requested URI, or an object + * @param callback the callback function with signature function(res) + */ + inject(options: string | InjectedRequestOptions, callback: (res: InjectedResponseObject) => void): void; + inject(options: string | InjectedRequestOptions, ): Promise; + /** + * Logs server events that cannot be associated with a specific request. When called the server emits a 'log' event which can be used by other listeners or plugins to record the information or output to the console. + * [See docs](https://hapijs.com/api/16.1.1#serverlogtags-data-timestamp) + * @param tags a string or an array of strings (e.g. ['error', 'database', 'read']) used to identify the event. Tags are used instead of log levels and provide a much more expressive mechanism for describing and filtering events. Any logs generated by the server internally include the 'hapi' tag along with event-specific information. + * @param data an optional message string or object with the application data being logged. If data is a function, the function signature is function() and it called once to generate (return value) the actual data emitted to the listeners. If no listeners match the event, the data function is not invoked. + * @param timestamp an optional timestamp expressed in milliseconds. Defaults to Date.now() (now). + */ + log(tags: string | string[], data?: string | Object | Function, timestamp?: number): void; + /** + * When the server contains exactly one connection, looks up a route configuration. + * When the server contains more than one connection, each server.connections array member provides its own connection.lookup() method. + * [See docs](https://hapijs.com/api/16.1.1#serverlookupid) + * @param id the route identifier as set in the route options. + * @return the route public interface object if found, otherwise null. + */ + lookup(id: string): RoutePublicInterface | null; + /** + * When the server contains exactly one connection, looks up a route configuration + * When the server contains more than one connection, each server.connections array member provides its own connection.match() method. + * [See docs](https://hapijs.com/api/16.1.1#servermatchmethod-path-host) + * @param method the HTTP method (e.g. 'GET', 'POST'). TODO check if it allows HEAD + * @param path the requested path (must begin with '/'). + * @param host optional hostname (to match against routes with vhost). + * @return the route public interface object if found, otherwise null. + */ + match(method: HTTP_METHODS, path: string, host?: string): RoutePublicInterface | null; + /** + * Registers a server method. Server methods are functions registered with the server and used throughout the application as a common utility. Their advantage is in the ability to configure them to use the built-in cache and share across multiple request handlers without having to create a common module. + * [See docs](https://hapijs.com/api/16.1.1#servermethodname-method-options) + * @param name a unique method name used to invoke the method via server.methods[name]. Supports using nested names such as utils.users.get which will automatically create the missing path under server.methods and can be accessed for the previous example via server.methods.utils.users.get. When configured with caching enabled, server.methods[name].cache will be an object see ServerMethodNameCacheObject + * @param method the method function + * @param options optional configuration + */ + method(name: string, method: ServerMethod, options?: ServerMethodOptions): void; + /** + * Registers a server method function as described in server.method() using a configuration object + * [See docs](https://hapijs.com/api/16.1.1#servermethodmethods) + */ + method(methods: ServerMethodConfigurationObject[]): void; + method(methods: ServerMethodConfigurationObject): void; + /** + * Subscribe a handler to an event + * [See docs](https://hapijs.com/api/16.1.1#serveroncriteria-listener) + * @param criteria the subscription criteria which can be an event name string which can be any of the built-in server events or a custom application event registered with server.event(events). + * Or an see ServerEventCriteria. + * If 'start' - emitted when the server is started using server.start(). + * If 'stop' - emitted when the server is stopped using server.stop(). + * @param listener + */ + on(criteria: 'start' | 'stop' | string | ServerEventCriteria, listener: Function): void; + /** + * The 'log' event includes the event object and a tags object (where each tag is a key with the value true) + * [See docs](https://hapijs.com/api/16.1.1#server-events) + */ + on(criteria: 'log', listener: (event: ServerEventObject, tags: Podium.Tags) => void): void; + /** + * The 'request' and 'request-internal' events include the request object, the event object, and a tags object (where each tag is a key with the value true) + * [See docs](https://hapijs.com/api/16.1.1#server-events) + * TODO submit issue to TypeScript. Using 'request' | 'request-internal' removes the type + * interference when using code like: `server.on('request', (request, event, tags) => {...}` + * Same for 'response' | 'tail'. + */ + on(criteria: 'request', listener: (request: Request, event: ServerEventObject, tags: Podium.Tags) => void): void; + on(criteria: 'request-internal', listener: (request: Request, event: ServerEventObject, tags: Podium.Tags) => void): void; + /** + * The 'request-error' event includes the request object and the causing error err object + * [See docs](https://hapijs.com/api/16.1.1#server-events) + */ + on(criteria: 'request-error', listener: (request: Request, err: Error) => void): void; + /** + * The 'response' and 'tail' events include the request object + * [See docs](https://hapijs.com/api/16.1.1#server-events) + * See 'request' and 'request-internal' + */ + on(criteria: 'response', listener: (request: Request) => void): void; + on(criteria: 'tail', listener: (request: Request) => void): void; + /** + * The 'route' event includes the route public interface, the connection, and the server object used to add the route (e.g. the result of a plugin select operation) + * [See docs](https://hapijs.com/api/16.1.1#server-events) + */ + on(criteria: 'route', listener: (route: RoutePublicInterface, connection: ServerConnection, server: Server) => void): void; + /** + * Same as calling server.on() with the count option set to 1. + * TODO type this to copy the server.on specific types for 'route', 'tail', etc. + * [See docs](https://hapijs.com/api/16.1.1#serveroncecriteria-listener) + * @param criteria + * @param listener + */ + once(criteria: string | ServerEventCriteria, listener: Function): void; + /** + * Sets the path prefix used to locate static resources (files and view templates) when relative paths are used + * Note that setting a path within a plugin only applies to resources accessed by plugin methods. If no path is set, the connection files.relativeTo configuration is used. The path only applies to routes added after it has been set. + * [See docs](https://hapijs.com/api/16.1.1#serverpathrelativeto) + * @param relativeTo the path prefix added to any relative file path starting with '.'. + */ + path(relativeTo: string): void; + /** + * Registers a plugin + * If no callback is provided, a Promise object is returned. + * Note that plugin registration are recorded on each of the available connections. When plugins express a dependency on other plugins, both have to be loaded into the same connections for the dependency requirement to be fulfilled. It is recommended that plugin registration happen after all the server connections are created via server.connection(). + * [See docs](https://hapijs.com/api/16.1.1#serverregisterplugins-options-callback) + * @param plugins + * @param options + * @param callback with signature function(err) where err an error returned from the registration function. Note that exceptions thrown by the registration function are not handled by the framework. + * A note on typings. Common use case is: + * register(Plugin, (err) => {// do more stuff}) + * so these typings save passing empty `options` object or having to + * explicity type the Error in the Callback e.g.: + * register(Plugin, {}, (err) => {// do more stuff}) or + * register(Plugin, (err: Error) => {// do more stuff}) + */ + register(plugins: Array<(PluginFunction | PluginRegistrationObject)>, callback: (err: Error | null) => void): void; + register(plugins: Array<(PluginFunction | PluginRegistrationObject)>): Promise; + register(plugins: PluginFunction | PluginRegistrationObject, callback: (err: Error | null) => void): void; + register(plugins: PluginFunction | PluginRegistrationObject): Promise; + register(plugins: Array<(PluginFunction | PluginRegistrationObject)>, options: PluginRegistrationOptions, callback: (err: Error | null) => void): void; + register(plugins: Array<(PluginFunction | PluginRegistrationObject)>, options: PluginRegistrationOptions): Promise; + register(plugins: PluginFunction | PluginRegistrationObject, options: PluginRegistrationOptions, callback: (err: Error | null) => void): void; + register(plugins: PluginFunction | PluginRegistrationObject, options: PluginRegistrationOptions): Promise; + /** + * Adds a connection route + * [See docs](https://hapijs.com/api/16.1.1#serverrouteoptions) + * @param options a route configuration object [See docs](https://hapijs.com/api/16.1.1#route-configuration) or an array of configuration objects. + */ + route(options: RouteConfiguration[]): void; + route(options: RouteConfiguration): void; + /** + * Selects a subset of the server's connections + * Returns a server object with connections set to the requested subset. Selecting again on a selection operates as a logic AND statement between the individual selections. + * [See docs](https://hapijs.com/api/16.1.1#serverselectlabels) + * @param labels a single string or array of strings of labels used as a logical OR statement to select all the connections with matching labels in their configuration. + */ + select(labels: string | string[]): Server; + /** + * Starts the server connections by listening for incoming requests on the configured port of each listener (unless the connection was configured with autoListen set to false) + * If no callback is provided, a Promise object is returned. + * Note that if the method fails and the callback includes an error, the server is considered to be in an undefined state and should be shut down. In most cases it would be impossible to fully recover as the various plugins, caches, and other event listeners will get confused by repeated attempts to start the server or make assumptions about the healthy state of the environment. It is recommended to assert that no error has been returned after calling start() to abort the process when the server fails to start properly. If you must try to resume after a start error, call server.stop() first to reset the server state. + * If a started server is started again, the second call to start() will only start new connections added after the initial start() was called. No events will be emitted and no extension points invoked. + * [See docs](https://hapijs.com/api/16.1.1#serverstartcallback) + * @param callback the callback method when server startup is completed or failed with the signature function(err) where: + * * err - any startup error condition. + */ + start(callback: (err?: Error) => void): void; + start(): Promise; + /** + * HTTP state management [See docs](https://tools.ietf.org/html/rfc6265) uses client cookies to persist a state across multiple requests. Registers a cookie definitions + * [See docs](https://hapijs.com/api/16.1.1#serverstatename-options) + * @param name the cookie name string. + * @param options optional cookie settings + */ + state(name: string, options?: ServerStateCookieConfiguationObject): void; + /** + * Stops the server's connections by refusing to accept any new connections or requests (existing connections will continue until closed or timeout) + * If no callback is provided, a Promise object is returned. + * [See docs](https://hapijs.com/api/16.1.1#serverstopoptions-callback) + * @param options options object with: + * * timeout - overrides the timeout in millisecond before forcefully terminating a connection. Defaults to 5000 (5 seconds). + * @param callback optional callback method which is called once all the connections have ended and it is safe to exit the process with signature function(err) where: + * * err - any termination error condition. + */ + stop(options: {timeout: number} | null, callback: (err?: Error) => void): void; + stop(options?: {timeout: number}): Promise; + /** + * Returns a copy of the routing table + * Note that if the server has not been started and multiple connections use port 0, the table items will override each other and will produce an incomplete result. + * When calling connection.table() directly on each connection, the return value is the same as the array table item value of an individual connection + * [See docs](https://hapijs.com/api/16.1.1#servertablehost) + * @param host optional host to filter routes matching a specific virtual host. Defaults to all virtual hosts. + */ + table(host?: string): RoutingTableEntry[]; } -/** server.realm http://hapijs.com/api#serverrealm - The realm object contains server-wide or plugin-specific state that can be shared across various methods. For example, when calling server.bind(), - the active realm settings.bind property is set which is then used by routes and extensions added at the same level (server root or plugin). - Realms are a limited version of a sandbox where plugins can maintain state used by the framework when adding routes, extensions, and other properties. - The server.realm object should be considered read-only and must not be changed directly except for the plugins property can be directly manipulated by the plugins (each setting its own under plugins[name]). - exports.register = function (server, options, next) { -console.log(server.realm.modifiers.route.prefix); -return next(); -}; +/** + * Server Options + * Note that the options object is deeply cloned and cannot contain any values that are unsafe to perform deep copy on. + * [See docs](https://hapijs.com/api/16.1.1#new-serveroptions) */ -export interface IServerRealm { - /** when the server object is provided as an argument to the plugin register() method, modifiers provides the registration preferences passed the server.register() method */ - modifiers: { - /** routes preferences: */ - route: { - /** - the route path prefix used by any calls to server.route() from the server. */ - prefix: string; - /** the route virtual host settings used by any calls to server.route() from the server. */ - vhost: string; - }; - }; - /** the active plugin name (empty string if at the server root). */ - plugin: string; - /** plugin-specific state to be shared only among activities sharing the same active state. plugins is an object where each key is a plugin name and the value is the plugin state. */ - plugins: IDictionary; - /** settings overrides */ - settings: { - files: { - relativeTo: any; - }; - bind: any; - }; -} -/** server.state(name, [options]) http://hapijs.com/api#serverstatename-options - HTTP state management uses client cookies to persist a state across multiple requests. Registers a cookie definitions where:*/ -export interface IServerState { - /** - the cookie name string. */ - name: string; - - /** - are the optional cookie settings: */ - options: { - /** - time - to - live in milliseconds.Defaults to null (session time- life - cookies are deleted when the browser is closed).*/ - ttl: number; - /** - sets the 'Secure' flag.Defaults to false.*/ - isSecure: boolean; - /** - sets the 'HttpOnly' flag.Defaults to false.*/ - isHttpOnly: boolean - /** - the path scope.Defaults to null (no path).*/ - path: any; - /** - the domain scope.Defaults to null (no domain). */ - domain: any; - /** if present and the cookie was not received from the client or explicitly set by the route handler, the cookie is automatically added to the response with the provided value. The value can be a function with signature function(request, next) where: - request - the request object. - next - the continuation function using the function(err, value) signature.*/ - autoValue(request: Request, next: (err: any, value: any) => void): void; - /** - encoding performs on the provided value before serialization. Options are: - 'none' - no encoding. When used, the cookie value must be a string. This is the default value. - 'base64' - string value is encoded using Base64. - 'base64json' - object value is JSON-stringified than encoded using Base64. - 'form' - object value is encoded using the x-www-form-urlencoded method. - 'iron' - Encrypts and sign the value using iron.*/ - encoding: string; - /** - an object used to calculate an HMAC for cookie integrity validation.This does not provide privacy, only a mean to verify that the cookie value was generated by the server.Redundant when 'iron' encoding is used.Options are:*/ - sign: { - /** - algorithm options.Defaults to require('iron').defaults.integrity.*/ - integrity: any; - /** - password used for HMAC key generation.*/ - password: string; - }; - /** - password used for 'iron' encoding.*/ - password: string; - /** - options for 'iron' encoding.Defaults to require('iron').defaults.*/ - iron: any; - /** - if false, errors are ignored and treated as missing cookies.*/ - ignoreErrors: boolean; - /** - if true, automatically instruct the client to remove invalid cookies.Defaults to false.*/ - clearInvalid: boolean; - /** - if false, allows any cookie value including values in violation of RFC 6265. Defaults to true.*/ - strictHeader: boolean; - /** - overrides the default proxy localStatePassThrough setting.*/ - passThrough: any; - }; +export interface ServerOptions { + /** app - application-specific configuration which can later be accessed via server.settings.app. Note the difference between server.settings.app which is used to store static configuration values and server.app which is meant for storing run-time state. Defaults to {}. */ + app?: any; + /** + * cache - sets up server-side caching. Every server includes a default cache for storing application state. By default, a simple memory-based cache is created which has limited capacity and capabilities. hapi uses catbox for its cache which includes support for common storage solutions (e.g. Redis, MongoDB, Memcached, Riak, among others). Caching is only utilized if methods and plugins explicitly store their state in the cache. The server cache configuration only defines the storage container itself. cache can be assigned: + * * a prototype function (usually obtained by calling require() on a catbox strategy such as require('catbox-redis')). A new catbox client will be created internally using this function. + * * a CatboxServerOptionsCacheConfiguration configuration object + * * an array of the above object for configuring multiple cache instances, each with a unique name. When an array of objects is provided, multiple cache connections are established and each array item (except one) must include a name. + */ + cache?: Catbox.EnginePrototype | CatboxServerOptionsCacheConfiguration | CatboxServerOptionsCacheConfiguration[]; + /** sets the default connections configuration which can be overridden by each connection */ + connections?: ConnectionConfigurationServerDefaults; + /** determines which logged events are sent to the console (this should only be used for development and does not affect which events are actually logged internally and recorded). Set to false to disable all console logging, or to an object with: */ + debug?: false | { + /** a string array of server log tags to be displayed via console.error() when the events are logged via server.log() as well as internally generated server logs. For example, to display all errors, set the option to ['error']. To turn off all console debug messages set it to false. Defaults to uncaught errors thrown in external code (these errors are handled automatically and result in an Internal Server Error response) or runtime errors due to developer error. */ + log?: string[] | false; + /** a string array of request log tags to be displayed via console.error() when the events are logged via request.log() as well as internally generated request logs. For example, to display all errors, set the option to ['error']. To turn off all console debug messages set it to false. Defaults to uncaught errors thrown in external code (these errors are handled automatically and result in an Internal Server Error response) or runtime errors due to developer error. */ + request?: string[] | false; + }; + /** process load monitoring */ + load?: { + /** the frequency of sampling in milliseconds. Defaults to 0 (no sampling). */ + sampleInterval?: number; + }; + /** options passed to the mimos module (https://github.com/hapijs/mimos) when generating the mime database used by the server and accessed via server.mime. */ + mime?: MimosOptions; + /** plugin-specific configuration which can later be accessed via server.settings.plugins. plugins is an object where each key is a plugin name and the value is the configuration. Note the difference between server.settings.plugins which is used to store static configuration values and server.plugins which is meant for storing run-time state. Defaults to {}. */ + plugins?: Object; + /** if false, will not use node domains to protect against exceptions thrown in handlers and other external code. Defaults to true. */ + useDomains?: boolean; } -export interface IFileHandlerConfig { - /** a path string or function as described above.*/ - path: string; - /** an optional filename to specify if sending a 'Content-Disposition' header, defaults to the basename of path*/ - filename?: string; - /**- specifies whether to include the 'Content-Disposition' header with the response. Available values: - false - header is not included. This is the default value. - 'attachment' - 'inline'*/ - mode?: boolean | string; - /** if true, looks for the same filename with the '.gz' suffix for a pre-compressed version of the file to serve if the request supports content encoding. Defaults to false.*/ - lookupCompressed: boolean; +/** + * The server event object + * [See docs](https://hapijs.com/api/16.1.1#server-events) + */ +export interface ServerEventObject { + /** the event timestamp. */ + timestamp: number; + /** if the event relates to a request, the request id. */ + request: string; + /** if the event relates to a server, the server.info.uri. */ + server: string; + /** an array of tags (e.g. ['error', 'http']). */ + tags: string[]; + /** optional event-specific information. */ + data: any; + /** true if the event was generated internally by the framework. */ + internal: boolean; } -/**http://hapijs.com/api#route-handler - Built-in handlers - - The framework comes with a few built-in handler types available by setting the route handler config to an object containing one of these keys.*/ -export interface IRouteHandlerConfig { - /** generates a static file endpoint for serving a single file. file can be set to: - a relative or absolute file path string (relative paths are resolved based on the route files configuration). - a function with the signature function(request) which returns the relative or absolute file path. - an object with the following options */ - file?: string | IRequestHandler | IFileHandlerConfig; - /** directory - generates a directory endpoint for serving static content from a directory. Routes using the directory handler must include a path parameter at the end of the path string (e.g. /path/to/somewhere/{param} where the parameter name does not matter). The path parameter can use any of the parameter options (e.g. {param} for one level files only, {param?} for one level files or the directory root, {param*} for any level, or {param*3} for a specific level). If additional path parameters are present, they are ignored for the purpose of selecting the file system resource. The directory handler is an object with the following options: - path - (required) the directory root path (relative paths are resolved based on the route files configuration). Value can be: - a single path string used as the prefix for any resources requested by appending the request path parameter to the provided string. - an array of path strings. Each path will be attempted in order until a match is found (by following the same process as the single path string). - a function with the signature function(request) which returns the path string or an array of path strings. If the function returns an error, the error is passed back to the client in the response. - index - optional boolean|string|string[], determines if an index file will be served if found in the folder when requesting a directory. The given string or strings specify the name(s) of the index file to look for. If true, looks for 'index.html'. Any falsy value disables index file lookup. Defaults to true. - listing - optional boolean, determines if directory listing is generated when a directory is requested without an index document. Defaults to false. - showHidden - optional boolean, determines if hidden files will be shown and served. Defaults to false. - redirectToSlash - optional boolean, determines if requests for a directory without a trailing slash are redirected to the same path with the missing slash. Useful for ensuring relative links inside the response are resolved correctly. Disabled when the server config router.stripTrailingSlash is true.Defaults to false. - lookupCompressed - optional boolean, instructs the file processor to look for the same filename with the '.gz' suffix for a pre-compressed version of the file to serve if the request supports content encoding. Defaults to false. - defaultExtension - optional string, appended to file requests if the requested file is not found. Defaults to no extension.*/ - directory?: { - path: string | string[] | IRequestHandler | IRequestHandler; - index?: boolean | string | string[]; - listing?: boolean; - showHidden?: boolean; - redirectToSlash?: boolean; - lookupCompressed?: boolean; - defaultExtension?: string; - }; - proxy?: IProxyHandlerConfig; - view?: string | { - template: string; - context: { - payload: any; - params: any; - query: any; - pre: any; - } - }; - config?: { - handler: any; - bind: any; - app: any; - plugins: { - [name: string]: any; - }; - pre: Array<() => void>; - validate: { - headers: any; - params: any; - query: any; - payload: any; - errorFields?: any; - failAction?: string | IFailAction; - }; - payload: { - output: { - data: any; - stream: any; - file: any; - }; - parse?: any; - allow?: string | string[]; - override?: string; - maxBytes?: number; - uploads?: number; - failAction?: string; - }; - response: { - schema: any; - sample: number; - failAction: string; - }; - cache: { - privacy: string; - expiresIn: number; - expiresAt: number; - }; - auth: string | boolean | { - mode: string; - strategies: string[]; - payload?: boolean | string; - tos?: boolean | string; - scope?: string | string[]; - entity: string; - }; - cors?: boolean; - jsonp?: string; - description?: string; - notes?: string | string[]; - tags?: string[]; - }; -} -/** Route configuration - The route configuration object*/ -export interface IRouteConfiguration { - /** - (required) the absolute path used to match incoming requests (must begin with '/'). Incoming requests are compared to the configured paths based on the connection router configuration option.The path can include named parameters enclosed in {} which will be matched against literal values in the request as described in Path parameters.*/ - path: string; - /** - (required) the HTTP method.Typically one of 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', or 'OPTIONS'.Any HTTP method is allowed, except for 'HEAD'.Use '*' to match against any HTTP method (only when an exact match was not found, and any match with a specific method will be given a higher priority over a wildcard match). - * Can be assigned an array of methods which has the same result as adding the same route with different methods manually.*/ - method: string | string[]; - /** - an optional domain string or an array of domain strings for limiting the route to only requests with a matching host header field.Matching is done against the hostname part of the header only (excluding the port).Defaults to all hosts.*/ - vhost?: string; - /** - (required) the function called to generate the response after successful authentication and validation.The handler function is described in Route handler.If set to a string, the value is parsed the same way a prerequisite server method string shortcut is processed.Alternatively, handler can be assigned an object with a single key using the name of a registered handler type and value with the options passed to the registered handler.*/ - handler?: ISessionHandler | IStrictSessionHandler | string | IRouteHandlerConfig; - /** - additional route options.*/ - config?: IRouteAdditionalConfigurationOptions; -} -/** Route public interface When route information is returned or made available as a property. http://hapijs.com/api#route-public-interface */ -export interface IRoute { - /** the route HTTP method. */ - method: string; - /** the route path. */ - path: string; - /** the route vhost option if configured. */ - vhost?: string | string[]; - /** the [active realm] associated with the route.*/ - realm: IServerRealm; - /** the [route options] object with all defaults applied. */ - settings: IRouteAdditionalConfigurationOptions; +/** + * [See docs](https://hapijs.com/api/16.1.1#serveroncriteria-listener) + */ +export interface ServerEventCriteria { + /** the event name string (required). */ + name: string; + /** if true, the listener method receives an additional callback argument which must be called when the method completes. No other event will be emitted until the callback methods is called. The method signature is function(). If block is set to a positive integer, the value is used to set a timeout after which any pending events will be emitted, ignoring the eventual call to callback. Defaults to false (non blocking). */ + block?: boolean; + /** 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 channels. If channels are specified, event updates without any channel designation will not be included in the subscription. Defaults to no channels filter. */ + channels?: string | string[]; + /** if true, the data object passed to server.emit() is cloned before it is passed to the listener method. Defaults to the event registration option (which defaults to false). */ + clone?: boolean; + /** 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.once(). Defaults to no limit. */ + count?: number; + /** + * the event tags (if present) to subscribe to + * If the object is given: + * * tags - a tag string or array of tag strings. + * * all - if true, all tags must be present for the event update to match the subscription. Defaults to false (at least one matching tag). + */ + filter?: string | string[] | {tags: string | string[], all?: boolean}; + /** if true, and the data object passed to server.emit() is an array, the listener method is called with each array element passed as a separate argument. This should only be used when the emitted data structure is known and predictable. Defaults to the event registration option (which defaults to false). */ + spread?: boolean; + /** if true and the criteria object passed to server.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 the arguments list at the end (but before the callback argument if block is set). Defaults to the event registration option (which defaults to false). */ + tags?: boolean; } -export interface IServerAuthScheme { - /** authenticate(request, reply) - required function called on each incoming request configured with the authentication scheme where: - request - the request object. - reply - the reply interface the authentication method must call when done authenticating the request where: - reply(err, response, result) - is called if authentication failed where: - err - any authentication error. - response - any authentication response action such as redirection. Ignored if err is present, otherwise required. - result - an object containing: - credentials - the authenticated credentials. - artifacts - optional authentication artifacts. - reply.continue(result) - is called if authentication succeeded where: - result - same object as result above. - When the scheme authenticate() method implementation calls reply() with an error condition, the specifics of the error affect whether additional authentication strategies will be attempted if configured for the route. - .If the err returned by the reply() method includes a message, no additional strategies will be attempted. - If the err does not include a message but does include a scheme name (e.g. Boom.unauthorized(null, 'Custom')), additional strategies will be attempted in order of preference. - var server = new Hapi.Server(); - server.connection({ port: 80 }); - var scheme = function (server, options) { - return { - authenticate: function (request, reply) { - var req = request.raw.req; - var authorization = req.headers.authorization; - if (!authorization) { - return reply(Boom.unauthorized(null, 'Custom')); - } - return reply(null, { credentials: { user: 'john' } }); - } - }; - }; - server.auth.scheme('custom', scheme);*/ - authenticate(request: Request, reply: IReply): void; - authenticate(request: Request, reply: IStrictReply): void; - /** payload(request, reply) - optional function called to authenticate the request payload where: - request - the request object. - reply(err, response) - is called if authentication failed where: - err - any authentication error. - response - any authentication response action such as redirection. Ignored if err is present, otherwise required. - reply.continue() - is called if payload authentication succeeded. - When the scheme payload() method returns an error with a message, it means payload validation failed due to bad payload. If the error has no message but includes a scheme name (e.g. Boom.unauthorized(null, 'Custom')), authentication may still be successful if the route auth.payload configuration is set to 'optional'.*/ - payload?(request: Request, reply: IReply): void; - payload?(request: Request, reply: IStrictReply): void; - /** response(request, reply) - optional function called to decorate the response with authentication headers before the response headers or payload is written where: - request - the request object. - reply(err, response) - is called if an error occurred where: - err - any authentication error. - response - any authentication response to send instead of the current response. Ignored if err is present, otherwise required. - reply.continue() - is called if the operation succeeded.*/ - response?(request: Request, reply: IReply): void; - response?(request: Request, reply: IStrictReply): void; - /** an optional object */ - options?: { - /** if true, requires payload validation as part of the scheme and forbids routes from disabling payload auth validation. Defaults to false.*/ - payload: boolean; - }; +/** + * Server methods, user configured + * Related to [See docs](https://hapijs.com/api/16.1.1#servermethods) + * Related to [See docs](https://hapijs.com/api/16.1.1#servermethodname-method-options) + */ +export interface ServerMethod { + /** Not possible to improve this typing due to this unresolvable issue: https://github.com/Microsoft/TypeScript/issues/15190 */ + (...args: (any | ServerMethodNext)[]): void; + /** the method must return a value (result, Error, or a promise) or throw an Error. */ + (...args: any[]): any | Error | Promise; + /** When configured with caching enabled, server.methods[name].cache will be an object see ServerMethodNameCacheObject */ + cache?: ServerMethodNameCacheObject; } -/**the response object where: - statusCode - the HTTP status code. - headers - an object containing the headers set. - payload - the response payload string. - rawPayload - the raw response payload buffer. - raw - an object with the injection request and response objects: - req - the simulated node request object. - res - the simulated node response object. - result - the raw handler response (e.g. when not a stream or a view) before it is serialized for transmission. If not available, the value is set to payload. Useful for inspection and reuse of the internal objects returned (instead of parsing the response string). - request - the request object.*/ -export interface IServerInjectResponse { - statusCode: number; - headers: IDictionary; - payload: string; - rawPayload: Buffer; - raw: { - req: http.IncomingMessage; - res: http.ServerResponse - }; - result: any; - request: Request; +/** + * Related to [See docs](https://hapijs.com/api/16.1.1#servermethodname-method-options) + * @param err error response if the method failed. + * @param result the return value. + * @param ttl 0 if result is valid but cannot be cached. Defaults to cache policy. + */ +export interface ServerMethodNext { + (err: Error | null, result: any, ttl?: number): void; } -export interface IServerInject { - (options: string | IServerInjectOptions, callback: (res: IServerInjectResponse) => void): void; - (options: string | IServerInjectOptions): Promise; +/** For context [See docs](https://hapijs.com/api/16.1.1#servermethodname-method-options) */ +export interface ServerMethodNameCacheObject { + /** + * function that can be used to clear the cache for a given key. + * @param ...args any number of string, number or boolean. If other types then generateKey function must be specified. + * @param callback last argument is a callback. + * Not possible to improve this typing due to this unresolvable issue: https://github.com/Microsoft/TypeScript/issues/15190 + */ + drop(...args: (any | Function)[]): void; + /** an object with cache statistics, see stats documentation for catbox. */ + stats: Catbox.CacheStatisticsObject; } -export interface IServerInjectOptions { - /** the request HTTP method (e.g. 'POST'). Defaults to 'GET'.*/ - method: string; - /** the request URL. If the URI includes an authority (e.g. 'example.com:8080'), it is used to automatically set an HTTP 'Host' header, unless one was specified in headers.*/ - url: string; - /** an object with optional request headers where each key is the header name and the value is the header content. Defaults to no additions to the default Shot headers.*/ - headers?: IDictionary; - /** n optional string, buffer or object containing the request payload. In case of an object it will be converted to a string for you. Defaults to no payload. Note that payload processing defaults to 'application/json' if no 'Content-Type' header provided.*/ - payload?: string | {} | Buffer; - /** an optional credentials object containing authentication information. The credentials are used to bypass the default authentication strategies, and are validated directly as if they were received via an authentication scheme. Defaults to no credentials.*/ - credentials?: any; - /** an optional artifacts object containing authentication artifact information. The artifacts are used to bypass the default authentication strategies, and are validated directly as if they were received via an authentication scheme. Ignored if set without credentials. Defaults to no artifacts.*/ - artifacts?: any; - /** sets the initial value of request.app*/ - app?: any; - /** sets the initial value of request.plugins*/ - plugins?: any; - /** allows access to routes with config.isInternal set to true. Defaults to false.*/ - allowInternals?: boolean; - /** sets the remote address for the incoming connection.*/ - remoteAddress?: boolean; - /**object with options used to simulate client request stream conditions for testing: - error - if true, emits an 'error' event after payload transmission (if any). Defaults to false. - close - if true, emits a 'close' event after payload transmission (if any). Defaults to false. - end - if false, does not end the stream. Defaults to true.*/ - simulate?: { - error: boolean; - close: boolean; - end: boolean; - }; +/** For context [See docs](https://hapijs.com/api/16.1.1#servermethodname-method-options) */ +export interface ServerMethodOptions { + /** a context object passed back to the method function (via this) when called. Defaults to active context (set via server.bind() when the method is registered. Ignored if the method is an arrow function. */ + bind?: any; + /** the same cache configuration used in server.cache(). The generateTimeout option is required. */ + cache?: CatboxServerCacheConfiguration; + /** + * if false, expects the method to be a synchronous function. Note that using a synchronous function with caching will convert the method interface to require a callback as an additional argument with the signature function(err, result, cached, report) since the cache interface cannot return values synchronously. Defaults to true. + * TODO: understand and type "an additional argument with the signature function(err, result, cached, report)" if appropriate. + */ + callback?: boolean; + /** a function used to generate a unique key (for caching) from the arguments passed to the method function (the callback argument is not passed as input). The server will automatically generate a unique key if the function's arguments are all of types 'string', 'number', or 'boolean'. However if the method uses other types of arguments, a key generation function must be provided which takes the same arguments as the function and returns a unique string (or null if no key can be generated). */ + generateKey?(args: any[]): string | null; } -/** host - optional host to filter routes matching a specific virtual host. Defaults to all virtual hosts. - The return value is an array where each item is an object containing: - info - the connection.info the connection the table was generated for. - labels - the connection labels. - table - an array of routes where each route contains: - settings - the route config with defaults applied. - method - the HTTP method in lower case. - path - the route path.*/ -export interface IConnectionTable { - info: any; - labels: any; - table: IRoute[]; +/** For context [See docs](https://hapijs.com/api/16.1.1#servermethodmethods) */ +export interface ServerMethodConfigurationObject { + name: string; + method: ServerMethod; + options: ServerMethodOptions; } -export interface ICookieSettings { - /** - time - to - live in milliseconds.Defaults to null (session time- life - cookies are deleted when the browser is closed).*/ - ttl?: number; - /** - sets the 'Secure' flag.Defaults to false.*/ - isSecure?: boolean; - /** - sets the 'HttpOnly' flag.Defaults to false.*/ - isHttpOnly?: boolean; - /** - the path scope.Defaults to null (no path).*/ - path?: string; - /** - the domain scope.Defaults to null (no domain).*/ - domain?: any; - /** - if present and the cookie was not received from the client or explicitly set by the route handler, the cookie is automatically added to the response with the provided value.The value can be a function with signature function(request, next) where: - request - the request object. - next - the continuation function using the function(err, value) signature.*/ - autoValue?(request: Request, next: (err: any, value: any) => void): void; - /** - encoding performs on the provided value before serialization.Options are: - 'none' - no encoding.When used, the cookie value must be a string.This is the default value. - 'base64' - string value is encoded using Base64. - 'base64json' - object value is JSON- stringified than encoded using Base64. - 'form' - object value is encoded using the x- www - form - urlencoded method. */ - encoding?: string; - /** - an object used to calculate an HMAC for cookie integrity validation.This does not provide privacy, only a mean to verify that the cookie value was generated by the server.Redundant when 'iron' encoding is used.Options are: - integrity - algorithm options.Defaults to require('iron').defaults.integrity. - password - password used for HMAC key generation. */ - sign?: { integrity: any; password: string; }; - password?: string; - iron?: any; - ignoreErrors?: boolean; - clearInvalid?: boolean; - strictHeader?: boolean; - passThrough?: any; +/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Caching with Catbox + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */ +// TODO: move to separate file http://stackoverflow.com/questions/43276921 + +/** + * TODO confirm this is the same as CatboxServerCacheConfiguration + * + * ** + * Server instantiation options configuration for Catbox cache + * TODO: check it extends Catbox.PolicyOptions and this is what "other options passed to the catbox strategy used." means. + * For context [See docs](https://hapijs.com/api/16.1.1#new-serveroptions) under: options > cache > a configuration object + * ** + * export interface CatboxServerOptionsCacheConfiguration extends Catbox.IPolicyOptions { + * // a prototype function or catbox engine object. + * engine: Catbox.EnginePrototypeOrObject; + * // 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 the default cache. If every cache includes a name, a default memory cache is provisioned as well. + * name?: string; + * // if true, allows multiple cache users to share the same segment (e.g. multiple methods using the same cache storage container). Default to false. + * shared?: boolean; + * } + */ +export type CatboxServerOptionsCacheConfiguration = CatboxServerCacheConfiguration; + +/** + * Server cache method configuration for Catbox cache + * Used for "Provisions a cache segment within the server cache facility" + * For context [See docs](https://hapijs.com/api/16.1.1#servercacheoptions) + * Also used in [See docs](https://hapijs.com/api/16.1.1#servermethodname-method-options) > options.cache + */ +export interface CatboxServerCacheConfiguration extends Catbox.PolicyOptions { + /** the cache name configured in server.cache. Defaults to the default cache. */ + cache?: string; + /** 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 server method, defaults to '#name' where 'name' is the server method name. Required when called outside of a plugin. */ + segment?: string; + /** if true, allows multiple cache provisions to share the same segment. Default to false. */ + shared?: boolean; + /** + * a prototype function or catbox engine object. + * [See docs](https://hapijs.com/api/16.1.1#servercacheprovisionoptions-callback) example code includes use of `engine` option. But server.cache.provision of `options` says "same as the server cache configuration options.". + * TODO confirm once PR to hapi docs accepted / rejected. + */ + engine?: Catbox.EnginePrototypeOrObject; + /** + * 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 the default cache. If every cache includes a name, a default memory cache is provisioned as well. + * [See docs](https://hapijs.com/api/16.1.1#servercacheprovisionoptions-callback) example code includes use of `name` option. But server.cache.provision of `options` says "same as the server cache configuration options.". + */ + name?: string; } -/** method - the method function with the signature is one of: - function(arg1, arg2, ..., argn, next) where: - arg1, arg2, etc. - the method function arguments. - next - the function called when the method is done with the signature function(err, result, ttl) where: - err - error response if the method failed. - result - the return value. - ttl - 0 if result is valid but cannot be cached. Defaults to cache policy. - function(arg1, arg2, ..., argn) where: - arg1, arg2, etc. - the method function arguments. - the callback option is set to false. - the method must returns a value (result, Error, or a promise) or throw an Error.*/ -export interface IServerMethod { - // (): void; - // (next: (err: any, result: any, ttl: number) => void): void; - // (arg1: any): void; - // (arg1: any, arg2: any, next: (err: any, result: any, ttl: number) => void): void; - // (arg1: any, arg2: any): void; - (...args: any[]): void; +/** + * Additional notes + * payload - In case of an object it will be converted to a string for you. Defaults to no payload. Note that payload processing defaults to 'application/json' if no 'Content-Type' header provided. + * [See docs](https://hapijs.com/api/16.1.1#serverinjectoptions-callback) + */ +export interface InjectedRequestOptions extends Shot.RequestOptions { + /** an optional credentials object containing authentication information. The credentials are used to bypass the default authentication strategies, and are validated directly as if they were received via an authentication scheme. Defaults to no credentials. */ + credentials?: any; + /** an optional artifacts object containing authentication artifact information. The artifacts are used to bypass the default authentication strategies, and are validated directly as if they were received via an authentication scheme. Ignored if set without credentials. Defaults to no artifacts. */ + artifacts?: any; + /** sets the initial value of request.app. */ + app?: any; + /** sets the initial value of request.plugins. */ + plugins?: PluginsStates; + /** allows access to routes with config.isInternal set to true. Defaults to false. */ + allowInternals?: boolean; } -/** options - optional configuration: - bind - a context object passed back to the method function (via this) when called. Defaults to active context (set via server.bind() when the method is registered. - cache - the same cache configuration used in server.cache(). - callback - if false, expects the method to be a synchronous function. Note that using a synchronous function with caching will convert the method interface to require a callback as an additional argument with the signature function(err, result, cached, report) since the cache interface cannot return values synchronously. Defaults to true. - generateKey - a function used to generate a unique key (for caching) from the arguments passed to the method function (the callback argument is not passed as input). The server will automatically generate a unique key if the function's arguments are all of types 'string', 'number', or 'boolean'. However if the method uses other types of arguments, a key generation function must be provided which takes the same arguments as the function and returns a unique string (or null if no key can be generated).*/ -export interface IServerMethodOptions { - bind?: any; - cache?: ICatBoxCachePolicyOptions; - callback?: boolean; - generateKey?(args: any[]): string; + +/** + * the response object from server.inject + * [See docs](https://hapijs.com/api/16.1.1#serverinjectoptions-callback) + */ +export interface InjectedResponseObject extends Shot.ResponseObject { + /** the raw handler response (e.g. when not a stream or a view) before it is serialized for transmission. If not available, the value is set to payload. Useful for inspection and reuse of the internal objects returned (instead of parsing the response string). */ + result: Shot.ResponseObject | string; + /** the request object. */ + request: InjectedRequestOptions; } -/** Request object - The request object is created internally for each incoming request. It is different from the node.js request object received from the HTTP server callback (which is available in request.raw.req). The request object methods and properties change throughout the request lifecycle. - Request events +/** + * For context [See docs](https://hapijs.com/api/16.1.1#new-serveroptions) under: options > connections + */ +export interface ConnectionConfigurationServerDefaults { + /** application-specific connection configuration which can be accessed via connection.settings.app. Provides a safe place to store application configuration without potential conflicts with the framework internals. Should not be used to configure plugins which should use plugins[name]. Note the difference between connection.settings.app which is used to store configuration values and connection.app which is meant for storing run-time state. */ + app?: any; + /** if false, response content encoding is disabled. Defaults to true */ + compression?: boolean; + /** connection load limits configuration where: */ + load?: { + /** maximum V8 heap size over which incoming requests are rejected with an HTTP Server Timeout (503) response. Defaults to 0 (no limit). */ + maxHeapUsedBytes: number; + /** maximum process RSS size over which incoming requests are rejected with an HTTP Server Timeout (503) response. Defaults to 0 (no limit). */ + maxRssBytes: number; + /** maximum event loop delay duration in milliseconds over which incoming requests are rejected with an HTTP Server Timeout (503) response. Defaults to 0 (no limit). */ + maxEventLoopDelay: number; + }; + /** plugin-specific configuration which can later be accessed via connection.settings.plugins. Provides a place to store and pass connection-specific plugin configuration. plugins is an object where each key is a plugin name and the value is the configuration. Note the difference between connection.settings.plugins which is used to store configuration values and connection.plugins which is meant for storing run-time state. */ + plugins?: any; + /** controls how incoming request URIs are matched against the routing table: */ + router?: { + /** determines whether the paths '/example' and '/EXAMPLE' are considered different resources. Defaults to true. */ + isCaseSensitive?: boolean; + /** removes trailing slashes on incoming paths. Defaults to false. */ + stripTrailingSlash?: boolean; + }; + /** a route options object used to set the default configuration for every route. */ + routes?: RouteAdditionalConfigurationOptions; + /** sets the default configuration for every state (cookie) set explicitly via server.state() or implicitly (without definition) using the [state configuration object](https://hapijs.com/api/16.1.1#serverstatename-options). */ + state?: ServerStateCookieConfiguationObject; +} - The request object supports the following events: +/** + * a connection configuration object or array of objects with the following optional keys. + * Any connections configuration server defaults can be included to override and customize the individual connection. + * [See docs](https://hapijs.com/api/16.1.1#serverconnectionoptions) + */ +export interface ServerConnectionOptions extends ConnectionConfigurationServerDefaults { + /** host - the public hostname or IP address. Used only to set server.info.host and server.info.uri. If not configured, defaults to the operating system hostname and if not available, to 'localhost'. */ + host?: string; + /** address - sets the host name or IP address the connection will listen on. If not configured, defaults to host if present, otherwise to all available network interfaces (i.e. '0.0.0.0'). Set to 127.0.0.1 or localhost to restrict connection to only those coming from the same machine. */ + address?: string; + /** port - the TCP port the connection will listen to. Defaults to an ephemeral port (0) which uses an available port when the server is started (and assigned to server.info.port). If port is a string containing a '/' character, it is used as a UNIX domain socket path and if it starts with '\.\pipe' as a Windows named pipe. */ + port?: string | number; + /** uri - the full public URI without the path (e.g. 'http://example.com:8080'). If present, used as the connection info.uri otherwise constructed from the connection settings. */ + uri?: string; + /** listener - optional node.js HTTP (or HTTPS) http.Server object or any compatible object. If the listener needs to be manually started, set autoListen to false. If the listener uses TLS, set tls to true. */ + listener?: http.Server; + /** autoListen - indicates that the connection.listener will be started manually outside the framework. Cannot be specified with a port setting. Defaults to true. */ + autoListen?: boolean; + /** labels - a string or string array of labels used to server.select() specific connections matching the specified labels. Defaults to an empty array [] (no labels). */ + labels?: string | string[]; + /** tls - used to create an HTTPS connection. The tls object is passed unchanged as options to the node.js HTTPS server as described in the node.js HTTPS [documentation](https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener}) . Set to true when passing a listener object that has been configured to use TLS directly. */ + tls?: true | https.RequestOptions; +} - 'peek' - emitted for each chunk of payload data read from the client connection. The event method signature is function(chunk, encoding). - 'finish' - emitted when the request payload finished reading. The event method signature is function (). - 'disconnect' - emitted when a request errors or aborts unexpectedly. - var Crypto = require('crypto'); - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); +/** + * For context see RouteAdditionalConfigurationOptions > compression + * For context [See docs](https://hapijs.com/api/16.1.1#serverencoderencoding-encoder) + */ +export type CompressionEncoderSettings = any; - server.ext('onRequest', function (request, reply) { +/** + * For context see RouteAdditionalConfigurationOptions > payload > compression + * For context [See docs](https://hapijs.com/api/16.1.1#serverdecoderencoding-decoder) + */ +export type CompressionDecoderSettings = any; -var hash = Crypto.createHash('sha1'); -request.on('peek', function (chunk) { +/** + * an optional function called after all the specified dependencies have been registered and before the server starts. The function is only called if the server is initialized or started. If a circular dependency is detected, an exception is thrown (e.g. two plugins each has an after function to be called after the other). The function signature is function(server, next) where: + * [See docs](https://hapijs.com/api/16.1.1#serverdependencydependencies-after) + * Also see Server.dependency + * @param server the server the dependency() method was called on. + * @param next the callback function the method must call to return control over to the application and complete the registration process. The function signature is function(err) where: + * * err - internal error condition, which is returned back via the server.initialize() or server.start() callback. + */ +export interface AfterDependencyLoadCallback { + (server: Server, next: (err?: Error) => void): void; +} -hash.update(chunk); +/** For context see RouteAdditionalConfigurationOptions > auth */ +export interface AuthOptions { + /** + * the authentication mode. Defaults to 'required' if a server authentication strategy is configured, otherwise defaults to no authentication. Available values: + * * 'required' - authentication is required. + * * 'optional' - authentication is optional (must be valid if present). + * * 'try' - same as 'optional' but allows for invalid authentication. + */ + mode?: 'required' | 'optional' | 'try'; + /** a string array of strategy names in order they should be attempted. If only one strategy is used, strategy can be used instead with the single string value. Defaults to the default authentication strategy which is available only when a single strategy is configured. */ + strategies?: string[]; + strategy?: string; + /** + * if set, the payload (in requests other than 'GET' and 'HEAD') is authenticated after it is processed. Requires a strategy with payload authentication support (e.g. Hawk). Cannot be set to a value other than 'required' when the scheme sets the options.payload to true. Available values: + * * false - no payload authentication. This is the default value. + * * 'required' - payload authentication required. This is the default value when the scheme sets options.payload to true. + * * 'optional' - payload authentication performed only when the client includes payload authentication information (e.g. hash attribute in Hawk). + */ + payload?: false | 'required' | 'optional'; + /** specifying the route access rules. */ + access?: RouteAuthAccessConfiguationObject | RouteAuthAccessConfiguationObject[]; + /** (undocumented) Convenience way of setting access.scope, will over write all values in `access` */ + scope?: false | string | string[]; + /** (undocumented) Convenience way of setting access.entity, will over write all values in `access` */ + entity?: 'any' | 'user' | 'app'; +} + +/** + * Each rule is evaluated against an incoming request and access is granted if at least one rule matches. Each rule object must include at least one of: + * For context see RouteAdditionalConfigurationOptions > auth > an object > access + */ +export interface RouteAuthAccessConfiguationObject { + /** the application scope required to access the route. Value can be a scope string or an array of scope strings. The authenticated credentials object scope property must contain at least one of the scopes defined to access the route. If a scope string begins with a + character, that scope is required. If a scope string begins with a ! character, that scope is forbidden. For example, the scope ['!a', '+b', 'c', 'd'] means the incoming request credentials' scope must not include 'a', must include 'b', and must include one of 'c' or 'd'. You may also access properties on the request object (query and params) to populate a dynamic scope by using {} characters around the property name, such as 'user-{params.id}'. Defaults to false (no scope requirements). */ + scope?: false | string | string[]; + /** + * the required authenticated entity type. If set, must match the entity value of the authentication credentials. Available values: + * * any - the authentication can be on behalf of a user or application. This is the default value. + * * user - the authentication must be on behalf of a user which is identified by the presence of a user attribute in the credentials object returned by the authentication strategy. + * * app - the authentication must be on behalf of an application which is identified by the lack of presence of a user attribute in the credentials object returned by the authentication strategy. + */ + entity?: 'any' | 'user' | 'app'; +} + +/** + * For context see RouteAdditionalConfigurationOptions > cache + */ +export type RouteCacheOptions = { + /** + * determines the privacy flag included in client-side caching using the 'Cache-Control' header. Values are: + * * 'default' - no privacy flag. This is the default setting. + * * 'public' - mark the response as suitable for public caching. + * * 'private' - mark the response as suitable only for private caching. + */ + privacy?: 'default' | 'public' | 'private'; + /** an array of HTTP response status codes (e.g. 200) which are allowed to include a valid caching directive. Defaults to [200]. */ + statuses?: number[]; + /** a string with the value of the 'Cache-Control' header when caching is disabled. Defaults to 'no-cache'. */ + otherwise?: string; +} & ({ + /** relative expiration expressed in the number of milliseconds since the item was saved in the cache. Cannot be used together with expiresAt. */ + expiresIn?: number; + /** time of day expressed in 24h notation using the 'HH:MM' format, at which point all cache records for the route expire. Cannot be used together with expiresIn. */ + expiresAt?: undefined; +} | { + /** relative expiration expressed in the number of milliseconds since the item was saved in the cache. Cannot be used together with expiresAt. */ + expiresIn?: undefined; + /** time of day expressed in 24h notation using the 'HH:MM' format, at which point all cache records for the route expire. Cannot be used together with expiresIn. */ + expiresAt?: string; +} | { + /** relative expiration expressed in the number of milliseconds since the item was saved in the cache. Cannot be used together with expiresAt. */ + expiresIn?: undefined; + /** time of day expressed in 24h notation using the 'HH:MM' format, at which point all cache records for the route expire. Cannot be used together with expiresIn. */ + expiresAt?: undefined; }); -request.once('finish', function () { - -console.log(hash.digest('hex')); -}); - -request.once('disconnect', function () { - -console.error('request aborted'); -}); - -return reply.continue(); -});*/ -export class Request extends Events.EventEmitter { - /** application-specific state. Provides a safe place to store application data without potential conflicts with the framework. Should not be used by plugins which should use plugins[name].*/ - app: any; - /** authentication information*/ - auth: { - /** true is the request has been successfully authenticated, otherwise false.*/ - isAuthenticated: boolean; - /** the credential object received during the authentication process. The presence of an object does not mean successful authentication. can be set in the validate function's callback.*/ - credentials: any; - /** an artifact object received from the authentication strategy and used in authentication-related actions.*/ - artifacts: any; - /** the route authentication mode.*/ - mode: any; - /** the authentication error is failed and mode set to 'try'.*/ - error: any; - }; - /** the connection used by this request*/ - connection: ServerConnection; - /** the node domain object used to protect against exceptions thrown in extensions, handlers and route prerequisites. Can be used to manually bind callback functions otherwise bound to other domains.*/ - domain: any; - /** the raw request headers (references request.raw.headers).*/ - headers: IDictionary; - /** a unique request identifier (using the format '{now}:{connection.info.id}:{5 digits counter}').*/ - id: string; - /** request information */ - info: { - /** the request preferred encoding. */ - acceptEncoding: string; - /** if CORS is enabled for the route, contains the following: */ - cors: { - isOriginMatch: boolean; /** true if the request 'Origin' header matches the configured CORS restrictions. Set to false if no 'Origin' header is found or if it does not match. Note that this is only available after the 'onRequest' extension point as CORS is configured per-route and no routing decisions are made at that point in the request lifecycle. */ - }; - /** content of the HTTP 'Host' header (e.g. 'example.com:8080'). */ - host: string; - /** the hostname part of the 'Host' header (e.g. 'example.com').*/ - hostname: string; - /** request reception timestamp. */ - received: number; - /** content of the HTTP 'Referrer' (or 'Referer') header. */ - referrer: string; - /** remote client IP address. */ - remoteAddress: string; - /** remote client port. */ - remotePort: number; - /** request response timestamp (0 is not responded yet). */ - responded: number; - }; - /** the request method in lower case (e.g. 'get', 'post'). */ - method: string; - /** the parsed content-type header. Only available when payload parsing enabled and no payload error occurred. */ - mime: string; - /** an object containing the values of params, query, and payload before any validation modifications made. Only set when input validation is performed.*/ - orig: { - params: any; - query: any; - payload: any; - }; - /** an object where each key is a path parameter name with matching value as described in Path parameters.*/ - params: IDictionary; - /** an array containing all the path params values in the order they appeared in the path.*/ - paramsArray: string[]; - /** the request URI's path component. */ - path: string; - /** the request payload based on the route payload.output and payload.parse settings.*/ - payload: stream.Readable | Buffer | any; - /** plugin-specific state. Provides a place to store and pass request-level plugin data. The plugins is an object where each key is a plugin name and the value is the state.*/ - plugins: any; - /** an object where each key is the name assigned by a route prerequisites function. The values are the raw values provided to the continuation function as argument. For the wrapped response object, use responses.*/ - pre: IDictionary; - /** the response object when set. The object can be modified but must not be assigned another object. To replace the response with another from within an extension point, use reply(response) to override with a different response. Contains null when no response has been set (e.g. when a request terminates prematurely when the client disconnects).*/ - response: Response; - /**preResponses - same as pre but represented as the response object created by the pre method.*/ - preResponses: any; - /**an object containing the query parameters.*/ - query: any; - /** an object containing the Node HTTP server objects. Direct interaction with these raw objects is not recommended.*/ - raw: { - req: http.IncomingMessage; - res: http.ServerResponse; - }; - /** the route public interface.*/ - route: IRoute; - /** the server object. */ - server: Server; - /** an object containing parsed HTTP state information (cookies) where each key is the cookie name and value is the matching cookie content after processing using any registered cookie definition. */ - state: any; - /** complex object contining details on the url */ - url: { - /** null when i tested */ - auth: any; - /** null when i tested */ - hash: any; - /** null when i tested */ - host: any; - /** null when i tested */ - hostname: any; - href: string; - path: string; - /** path without search*/ - pathname: string; - /** null when i tested */ - port: any; - /** null when i tested */ - protocol: any; - /** querystring parameters*/ - query: IDictionary; - /** querystring parameters as a string*/ - search: string; - /** null when i tested */ - slashes: any; - }; - - /** request.setUrl(url) - - Available only in 'onRequest' extension methods. - - Changes the request URI before the router begins processing the request where: - - url - the new request path value. - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - - server.ext('onRequest', function (request, reply) { - - // Change all requests to '/test' - request.setUrl('/test'); - return reply.continue(); - });*/ - setUrl(url: string | url.Url): void; - /** request.setMethod(method) - - Available only in 'onRequest' extension methods. - - Changes the request method before the router begins processing the request where: - - method - is the request HTTP method (e.g. 'GET'). - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - - server.ext('onRequest', function (request, reply) { - - // Change all requests to 'GET' - request.setMethod('GET'); - return reply.continue(); - });*/ - setMethod(method: string): void; - - /** request.log(tags, [data, [timestamp]]) - - Always available. - - Logs request-specific events. When called, the server emits a 'request' event which can be used by other listeners or plugins. The arguments are: - - data - an optional message string or object with the application data being logged. - timestamp - an optional timestamp expressed in milliseconds. Defaults to Date.now() (now). - Any logs generated by the server internally will be emitted only on the 'request-internal' channel and will include the event.internal flag set to true. - - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - - server.on('request', function (request, event, tags) { - - if (tags.error) { - console.log(event); - } - }); - - var handler = function (request, reply) { - - request.log(['test', 'error'], 'Test event'); - return reply(); - }; - */ - log(/** a string or an array of strings (e.g. ['error', 'database', 'read']) used to identify the event. Tags are used instead of log levels and provide a much more expressive mechanism for describing and filtering events.*/ - tags: string | string[], - /** an optional message string or object with the application data being logged.*/ - data?: any, - /** an optional timestamp expressed in milliseconds. Defaults to Date.now() (now).*/ - timestamp?: number): void; - - /** request.getLog([tags], [internal]) - - Always available. - - Returns an array containing the events matching any of the tags specified (logical OR) - request.getLog(); - request.getLog('error'); - request.getLog(['error', 'auth']); - request.getLog(['error'], true); - request.getLog(false);*/ - - getLog(/** is a single tag string or array of tag strings. If no tags specified, returns all events.*/ - tags?: string, - /** filters the events to only those with a matching event.internal value. If true, only internal logs are included. If false, only user event are included. Defaults to all events (undefined).*/ - internal?: boolean): string[]; - - /** request.tail([name]) - - Available until immediately after the 'response' event is emitted. - - Adds a request tail which has to complete before the request lifecycle is complete where: - - name - an optional tail name used for logging purposes. - Returns a tail function which must be called when the tail activity is completed. - - Tails are actions performed throughout the request lifecycle, but which may end after a response is sent back to the client. For example, a request may trigger a database update which should not delay sending back a response. However, it is still desirable to associate the activity with the request when logging it (or an error associated with it). - - When all tails completed, the server emits a 'tail' event. - - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - - var get = function (request, reply) { - - var dbTail = request.tail('write to database'); - - db.save('key', 'value', function () { - - dbTail(); - }); - - return reply('Success!'); - }; - - server.route({ method: 'GET', path: '/', handler: get }); - - server.on('tail', function (request) { - - console.log('Request completed including db activity'); - });*/ - tail(/** an optional tail name used for logging purposes.*/ - name?: string): Function; -} -/** Response events - - The response object supports the following events: - - 'peek' - emitted for each chunk of data written back to the client connection. The event method signature is function(chunk, encoding). - 'finish' - emitted when the response finished writing but before the client response connection is ended. The event method signature is function (). - var Crypto = require('crypto'); - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - - server.ext('onPreResponse', function (request, reply) { - -var response = request.response; -if (response.isBoom) { -return reply(); +/** + * For context see RouteAdditionalConfigurationOptions > cors + */ +export interface CorsConfigurationObject { + /** a strings array of allowed origin servers ('Access-Control-Allow-Origin'). The array can contain any combination of fully qualified origins along with origin strings containing a wildcard '*' character, or a single '*' origin string. Defaults to any origin ['*']. */ + origin?: string[] | '*'; + /** number of seconds the browser should cache the CORS response ('Access-Control-Max-Age'). The greater the value, the longer it will take before the browser checks for changes in policy. Defaults to 86400 (one day). */ + maxAge?: number; + /** a strings array of allowed headers ('Access-Control-Allow-Headers'). Defaults to ['Accept', 'Authorization', 'Content-Type', 'If-None-Match'] */ + headers?: string[]; + /** a strings array of additional headers to headers. Use this to keep the default headers in place. */ + additionalHeaders?: string[]; + /** a strings array of exposed headers ('Access-Control-Expose-Headers'). Defaults to ['WWW-Authenticate', 'Server-Authorization']. */ + exposedHeaders?: string[]; + /** a strings array of additional headers to exposedHeaders. Use this to keep the default headers in place. */ + additionalExposedHeaders?: string[]; + /** if true, allows user credentials to be sent ('Access-Control-Allow-Credentials'). Defaults to false. */ + credentials?: boolean; } -var hash = Crypto.createHash('sha1'); -response.on('peek', function (chunk) { - -hash.update(chunk); -}); - -response.once('finish', function () { - -console.log(hash.digest('hex')); -}); - -return reply.continue(); -});*/ -export class Response extends Events.EventEmitter { - isBoom: boolean; - /** the HTTP response status code. Defaults to 200 (except for errors).*/ - statusCode: number; - /** an object containing the response headers where each key is a header field name. Note that this is an incomplete list of headers to be included with the response. Additional headers will be added once the response is prepare for transmission.*/ - headers: IDictionary; - /** the value provided using the reply interface.*/ - source: any; - /** a string indicating the type of source with available values: - 'plain' - a plain response such as string, number, null, or simple object (e.g. not a Stream, Buffer, or view). - 'buffer' - a Buffer. - 'view' - a view generated with reply.view(). - 'file' - a file generated with reply.file() of via the directory handler. - 'stream' - a Stream. - 'promise' - a Promise object. */ - variety: string; - /** application-specific state. Provides a safe place to store application data without potential conflicts with the framework. Should not be used by plugins which should use plugins[name].*/ - app: any; - /** plugin-specific state. Provides a place to store and pass request-level plugin data. The plugins is an object where each key is a plugin name and the value is the state. */ - plugins: any; - /** settings - response handling flags: - charset - the 'Content-Type' HTTP header 'charset' property. Defaults to 'utf-8'. - encoding - the string encoding scheme used to serial data into the HTTP payload when source is a string or marshals into a string. Defaults to 'utf8'. - passThrough - if true and source is a Stream, copies the statusCode and headers of the stream to the outbound response. Defaults to true. - stringify - options used for source value requiring stringification. Defaults to no replacer and no space padding. - ttl - if set, overrides the route cache expiration milliseconds value set in the route config. Defaults to no override. - varyEtag - if true, a suffix will be automatically added to the 'ETag' header at transmission time (separated by a '-' character) when the HTTP 'Vary' header is present.*/ - settings: { - charset: string; - encoding: string; - passThrough: boolean; - stringify: any; - ttl: number; - varyEtag: boolean; - }; - - /** sets the HTTP 'Content-Length' header (to avoid chunked transfer encoding) where: - length - the header value. Must match the actual payload size.*/ - bytes(length: number): Response; - - /** sets the 'Content-Type' HTTP header 'charset' property where: charset - the charset property value.*/ - charset(charset: string): Response; - - /** sets the HTTP status code where: - statusCode - the HTTP status code.*/ - code(statusCode: number): Response; - - /** sets the HTTP status code to Created (201) and the HTTP 'Location' header where: uri - an absolute or relative URI used as the 'Location' header value.*/ - created(uri: string): Response; - - /** encoding(encoding) - sets the string encoding scheme used to serial data into the HTTP payload where: encoding - the encoding property value (see node Buffer encoding).*/ - encoding(encoding: string): Response; - - /** etag(tag, options) - sets the representation entity tag where: - tag - the entity tag string without the double-quote. - options - optional settings where: - weak - if true, the tag will be prefixed with the 'W/' weak signifier. Weak tags will fail to match identical tags for the purpose of determining 304 response status. Defaults to false. - vary - if true and content encoding is set or applied to the response (e.g 'gzip' or 'deflate'), the encoding name will be automatically added to the tag at transmission time (separated by a '-' character). Ignored when weak is true. Defaults to true.*/ - etag(tag: string, options: { - weak: boolean; vary: boolean; - }): Response; - - /**header(name, value, options) - sets an HTTP header where: - name - the header name. - value - the header value. - options - optional settings where: - append - if true, the value is appended to any existing header value using separator. Defaults to false. - separator - string used as separator when appending to an exiting value. Defaults to ','. - override - if false, the header value is not set if an existing value present. Defaults to true.*/ - header(name: string, value: string, options?: IHeaderOptions): Response; - - /** hold() - puts the response on hold until response.send() is called. Available only after reply() is called and until response.hold() is invoked once. */ - hold(): Response; - - /** location(uri) - sets the HTTP 'Location' header where: - uri - an absolute or relative URI used as the 'Location' header value.*/ - location(uri: string): Response; - - /** redirect(uri) - sets an HTTP redirection response (302) and decorates the response with additional methods listed below, where: - uri - an absolute or relative URI used to redirect the client to another resource. */ - redirect(uri: string): Response; - - /** replacer(method) - sets the JSON.stringify() replacer argument where: - method - the replacer function or array. Defaults to none.*/ - replacer(method: Function | Function[]): Response; - - /** spaces(count) - sets the JSON.stringify() space argument where: - count - the number of spaces to indent nested object keys. Defaults to no indentation. */ - spaces(count: number): Response; - - /**state(name, value, [options]) - sets an HTTP cookie where: - name - the cookie name. - value - the cookie value. If no encoding is defined, must be a string. - options - optional configuration. If the state was previously registered with the server using server.state(), the specified keys in options override those same keys in the server definition (but not others).*/ - state(name: string, value: string, options?: any): Response; - - /** send() - resume the response which will be transmitted in the next tick. Available only after response.hold() is called and until response.send() is invoked once. */ - send(): void; - - /** sets a string suffix when the response is process via JSON.stringify().*/ - suffix(suffix: string): void; - - /** overrides the default route cache expiration rule for this response instance where: - msec - the time-to-live value in milliseconds.*/ - ttl(msec: number): void; - - /** type(mimeType) - sets the HTTP 'Content-Type' header where: - mimeType - is the mime type. Should only be used to override the built-in default for each response type. */ - type(mimeType: string): Response; - - /** clears the HTTP cookie by setting an expired value where: - name - the cookie name. - options - optional configuration for expiring cookie. If the state was previously registered with the server using server.state(), the specified keys in options override those same keys in the server definition (but not others).*/ - unstate(name: string, options?: { [key: string]: string }): Response; - - /** adds the provided header to the list of inputs affected the response generation via the HTTP 'Vary' header where: - header - the HTTP request header name.*/ - vary(header: string): void; +/** + * An object describing the extension function used whilst registering the extension function in one of the available extension points + * [See docs](https://hapijs.com/api/16.1.1#serverextevents) + * For context see RouteAdditionalConfigurationOptions > ext + */ +export interface ServerExtConfigurationObject { + /** the extension point event name. */ + type: ServerExtPoints; + /** + * a function or an array of functions to be executed at a specified point during request processing. The required extension function signature is see ServerExtFunction or see ServerExtRequestHandler + */ + method: ServerExtMethod | ServerExtMethod[]; + options?: ServerExtOptions; } -/** When using the redirect() method, the response object provides these additional methods */ -export class ResponseRedirect extends Response { - /** sets the status code to 302 or 307 (based on the rewritable() setting) where: - isTemporary - if false, sets status to permanent. Defaults to true.*/ - temporary(isTemporary: boolean): void; - /** sets the status code to 301 or 308 (based on the rewritable() setting) where: - isPermanent - if true, sets status to temporary. Defaults to false. */ - permanent(isPermanent: boolean): void; +/** + * [See docs](https://hapijs.com/api/16.1.1#serverextevents) > events > method + */ +export type ServerExtMethod = ServerExtFunction | ServerExtRequestHandler; - /** sets the status code to 301/302 for rewritable (allows changing the request method from 'POST' to 'GET') or 307/308 for non-rewritable (does not allow changing the request method from 'POST' to 'GET'). Exact code based on the temporary() or permanent() setting. Arguments: - isRewritable - if false, sets to non-rewritable. Defaults to true. - Permanent Temporary - Rewritable 301 302(1) - Non-rewritable 308(2) 307 - Notes: 1. Default value. 2. Proposed code, not supported by all clients. */ - rewritable(isRewritable: boolean): void; +/** + * [See docs](https://hapijs.com/api/16.1.1#serverextevents) > events > options + */ +export interface ServerExtOptions { + /** before - a string or array of strings of plugin names this method must execute before (on the same event). Otherwise, extension methods are executed in the order added. */ + before: string | string[]; + /** after - a string or array of strings of plugin names this method must execute after (on the same event). Otherwise, extension methods are executed in the order added. */ + after: string | string[]; + /** bind - a context object passed back to the provided method (via this) when called. Ignored if the method is an arrow function. */ + bind: any; + /** sandbox - if set to 'plugin' when adding a request extension points the extension is only added to routes defined by the current plugin. Not allowed when configuring route-level extensions, or when adding server extensions. Defaults to 'connection' which applies to any route added to the connection the extension is added to. */ + sandbox?: 'connection' | 'plugin'; } -/** info about a server connection */ -export interface IServerConnectionInfo { - /** - a unique connection identifier (using the format '{hostname}:{pid}:{now base36}').*/ - id: string; - /** - the connection creation timestamp.*/ - created: number; - /** - the connection start timestamp (0 when stopped).*/ - started: number; - /** the connection port based on the following rules: - the configured port value before the server has been started. - the actual port assigned when no port is configured or set to 0 after the server has been started.*/ - port: number; - /** - the host name the connection was configured to. Defaults to the operating system hostname when available, otherwise 'localhost'.*/ - host: string; - /** - the active IP address the connection was bound to after starting.Set to undefined until the server has been started or when using a non TCP port (e.g. UNIX domain socket).*/ - address: string; - /** - the protocol used: - 'http' - HTTP. - 'https' - HTTPS. - 'socket' - UNIX domain socket or Windows named pipe.*/ - protocol: string; - /** a string representing the connection (e.g. 'http://example.com:8080' or 'socket:/unix/domain/socket/path'). Contains the uri setting if provided, otherwise constructed from the available settings. If no port is available or set to 0, the uri will not include a port component.*/ - uri: string; +/** + * [See docs](https://hapijs.com/api/16.1.1#request-lifecycle) + * The available extension points include the request extension points as well as the following server extension points: + * * 'onPreStart' - called before the connection listeners are started. + * * 'onPostStart' - called after the connection listeners are started. + * * 'onPreStop' - called before the connection listeners are stopped. + * * 'onPostStop' - called after the connection listeners are stopped. + * [See docs](https://hapijs.com/api/16.1.1#serverextevents) > events > type + */ +export type ServerExtPoints = 'onRequest' | 'onPreResponse' | 'onPreAuth' | 'onPostAuth' | 'onPreHandler' | 'onPostHandler' | 'onPreResponse' | 'onPreStart' | 'onPostStart' | 'onPreStop' | 'onPostStop'; + +/** + * Server extension function registered an one of the server extension points + * [See docs](https://hapijs.com/api/16.1.1#serverextevents) + * For context see ServerExtConfigurationObject + * @param server - the server object. + * @param next - the continuation method with signature function(err). + * @param this - the object provided via options.bind or the current active context set with server.bind(). + */ +export interface ServerExtFunction { + (server: Server, next: ContinuationFunction): void; +} + +/** + * For context see RouteAdditionalConfigurationOptions > payload + */ +export interface RoutePayloadConfigurationObject { + /** + * the type of payload representation requested. The value must be one of: + * * 'data' - the incoming payload is read fully into memory. If parse is true, the payload is parsed (JSON, form-decoded, multipart) based on the 'Content-Type' header. If parse is false, the raw Buffer is returned. This is the default value except when a proxy handler is used. + * * 'stream' - the incoming payload is made available via a Stream.Readable interface. If the payload is 'multipart/form-data' and parse is true, fields values are presented as text while files are provided as streams. File streams from a 'multipart/form-data' upload will also have a property hapi containing filename and headers properties. + * * 'file' - the incoming payload is written to temporary file in the directory specified by the server's payload.uploads settings. If the payload is 'multipart/form-data' and parse is true, fields values are presented as text while files are saved. Note that it is the sole responsibility of the application to clean up the files generated by the framework. This can be done by keeping track of which files are used (e.g. using the request.app object), and listening to the server 'response' event to perform any needed cleanup. + */ + output?: PayLoadOutputOption; + /** + * can be true, false, or gunzip; determines if the incoming payload is processed or presented raw. true and gunzip includes gunzipping when the appropriate 'Content-Encoding' is specified on the received request. If parsing is enabled and the 'Content-Type' is known (for the whole payload as well as parts), the payload is converted into an object when possible. If the format is unknown, a Bad Request (400) error response is sent. Defaults to true, except when a proxy handler is used. The supported mime types are: + * * 'application/json' + * * 'application/x-www-form-urlencoded' + * * 'application/octet-stream' + * * 'text/*' + * * 'multipart/form-data' + */ + parse?: 'gunzip' | boolean; + /** + * overrides payload processing for multipart requests. Value can be one of: + * * false - disables multipart processing. + * * object with the following required options: + * * output - same as the payload.output option with an additional value option: + * * annotated - wraps each multipart part in an object with the following keys: // TODO type this? + * * headers - the part headers. + * * filename - the part file name. + * * payload - the processed part payload. + */ + multipart?: false | { + output: PayLoadOutputOption | 'annotated'; + }; + /** a string or an array of strings with the allowed mime types for the endpoint. Defaults to any of the supported mime types listed above. Note that allowing other mime types not listed will not enable them to be parsed, and that if parsing mode is 'parse', the request will result in an error response. */ + allow?: string | string[]; + /** a mime type string overriding the 'Content-Type' header value received. Defaults to no override. */ + override?: string; + /** limits the size of incoming payloads to the specified byte count. Allowing very large payloads may cause the server to run out of memory. Defaults to 1048576 (1MB). */ + maxBytes?: number; + /** payload reception timeout in milliseconds. Sets the maximum time allowed for the client to transmit the request payload (body) before giving up and responding with a Request Timeout (408) error response. Set to false to disable. Defaults to 10000 (10 seconds). */ + timeout?: number; + /** the directory used for writing file uploads. Defaults to os.tmpdir(). */ + uploads?: string; + /** + * determines how to handle payload parsing errors. Allowed values are: + * * 'error' - return a Bad Request (400) error response. This is the default value. + * * 'log' - report the error but continue processing the request. + * * 'ignore' - take no action and continue processing the request. + */ + failAction?: 'error' | 'log' | 'ignore'; + /** the default 'Content-Type' HTTP header value is not present. Defaults to 'application/json'. */ + defaultContentType?: string; + /** an object where each key is a content-encoding name and each value is an object with the desired decoder settings. Note that encoder settings are set in the root option compression. */ + compression: Dictionary; +} + +export type PayLoadOutputOption = 'data' | 'stream' | 'file'; + +/** + * events must be one of: + * * an event name string. + * * an event options object see ApplicationEventOptionsObject + * * a podium [See docs](https://github.com/hapijs/podium) emitter object. + * For context [See docs](https://hapijs.com/api/16.1.1#servereventevents) > events parameter + */ +export type ApplicationEvent = string | ApplicationEventOptionsObject | Podium; + +/** + * an event options object + * For context see ApplicationEvent + * For context [See docs](https://hapijs.com/api/16.1.1#servereventevents) > events parameter + */ +export interface ApplicationEventOptionsObject { + /** the event name string (required). */ + name: string; + /** a string or array of strings specifying the event channels available. Defaults to no channel restrictions (event updates can specify a channel or not). */ + channels?: string | string[]; + /** if true, the data object passed to server.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). */ + clone?: boolean; + /** if true, the data object passed to server.emit() must be an array and the listener method is called with each array element passed as a separate argument (unless an override specified 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). */ + spread?: boolean; + /** if true and the criteria object passed to server.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 the arguments list at the end (but before the callback argument if block is set). A configuration override can be set by each listener. Defaults to false. */ + tags?: boolean; + /** 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 configuration is used. Defaults to false (a duplicate registration will throw an error). */ + shared?: boolean; +} + +/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Route + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */ +// TODO: move to separate file http://stackoverflow.com/questions/43276921 + +/** + * Route configuration + * The route configuration object + * + * [See docs](https://hapijs.com/api/16.1.1#route-configuration) + * + * TODO typings check that the following refers to RouteAdditionalConfigurationOptions "Note that the options object is deeply cloned (with the exception of bind which is shallowly copied) and cannot contain any values that are unsafe to perform deep copy on." + */ +export interface RouteConfiguration { + /** the absolute path used to match incoming requests (must begin with '/'). Incoming requests are compared to the configured paths based on the connection router configuration option. The path can include named parameters enclosed in {} which will be matched against literal values in the request as described in Path parameters. */ + path: string; + /** the HTTP method. Typically one of 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', or 'OPTIONS'. Any HTTP method is allowed, except for 'HEAD'. Use '*' to match against any HTTP method (only when an exact match was not found, and any match with a specific method will be given a higher priority over a wildcard match). Can be assigned an array of methods which has the same result as adding the same route with different methods manually. */ + method: HTTP_METHODS_PARTIAL | '*' | (HTTP_METHODS_PARTIAL | '*')[]; + /** an optional domain string or an array of domain strings for limiting the route to only requests with a matching host header field. Matching is done against the hostname part of the header only (excluding the port). Defaults to all hosts. */ + vhost?: string; + /** the function called to generate the response after successful authentication and validation. The handler function is described in Route handler. If set to a string, the value is parsed the same way a prerequisite server method string shortcut is processed. Alternatively, handler can be assigned an object with a single key using the name of a registered handler type and value with the options passed to the registered handler. */ + handler?: string | RouteHandler | RouteHandlerPlugins; + /** additional route options. The config value can be an object or a function that returns an object using the signature function(server) where server is the server the route is being added to and this is bound to the current realm's bind option. */ + config?: RouteAdditionalConfigurationOptions | ((server: Server) => RouteAdditionalConfigurationOptions); +} + +/** + * Route options + * Each route can be customize to change the default behavior of the request lifecycle using the following options: + * [See docs](https://hapijs.com/api/16.1.1#route-options) + */ +export interface RouteAdditionalConfigurationOptions { + /** application specific configuration.Should not be used by plugins which should use plugins[name] instead. */ + app?: any; + /** + * Authentication configuration. Value can be: + * * false to disable authentication if a default strategy is set. + * * a string with the name of an authentication strategy registered with server.auth.strategy(). + * * an object + */ + auth?: false | string | AuthOptions; + /** an object passed back to the provided handler (via this) when called. Ignored if the method is an arrow function. */ + bind?: any; + /** + * Route cache options + * if the route method is 'GET', the route can be configured to include caching directives in the response. The default Cache-Control: no-cache header can be disabled by setting cache to false. Caching can be customized using an object + * TODO check: the default is to have 'Cache-Control: no-cache', but on first reading is a contridiction as you can disabled cache and disabled no-cache by setting RouteCacheOptions to false? + */ + cache?: boolean | RouteCacheOptions; + /** an object where each key is a content-encoding name and each value is an object with the desired encoder settings. Note that decoder settings are set in payload.compression. */ + compression?: Dictionary; + /** the Cross-Origin Resource Sharing protocol allows browsers to make cross-origin API calls. CORS is required by web applications running inside a browser which are loaded from a different domain than the API server. CORS headers are disabled by default (false). To enable, set cors to true, or to an object */ + cors?: boolean | CorsConfigurationObject; + /** defined a route-level request extension points by setting the option to an object with a key for each of the desired extension points ('onRequest' is not allowed), and the value is the same as the [server.ext(events)](https://hapijs.com/api/16.1.1#serverextevents) event argument. */ + ext?: Dictionary; + /** defines the behavior for accessing files: */ + files?: { + /** determines the folder relative paths are resolved against. */ + relativeTo: string; + }; + /** an alternative location for the route.handler option. */ + handler?: string | RouteHandler; + /** an optional unique identifier used to look up the route using server.lookup(). Cannot be assigned to routes with an array of methods. */ + id?: string; + /** if true, the route cannot be accessed through the HTTP connection but only through the server.inject() interface with the allowInternals option set to true. Used for internal routes that should not be accessible to the outside world. Defaults to false. */ + isInternal?: boolean; + /** optional arguments passed to JSON.stringify() when converting an object or error response to a string payload. Supports the following: */ + json?: Json.StringifyArguments & { + /** string suffix added after conversion to JSON string. Defaults to no suffix. */ + suffix?: string; + }; + /** enables JSONP support by setting the value to the query parameter name containing the function name used to wrap the response payload. For example, if the value is 'callback', a request comes in with 'callback=me', and the JSON response is '{ "a":"b" }', the payload will be 'me({ "a":"b" });'. Does not work with stream responses. Headers content-type and x-content-type-options are set to text/javascript and nosniff respectively, and will override those headers even if explicitly set by response.type() */ + jsonp?: string; + /** if true, request level logging is enabled (accessible via request.getLog()). */ + log?: boolean; + /** + * determines how the request payload is processed + * [See docs](https://hapijs.com/api/16.1.1#route-options) + */ + payload?: RoutePayloadConfigurationObject; + /** plugin-specific configuration. plugins is an object where each key is a plugin name and the value is the plugin configuration. */ + plugins?: Object; + /** an array with [route prerequisites](https://hapijs.com/api/16.1.1#route-prerequisites) methods which are executed in serial or in parallel before the handler is called. */ + pre?: RoutePrerequisitesArray; + /** processing rules for the outgoing response */ + response?: RouteResponseConfigurationObject; + /** sets common security headers (disabled by default). To enable set security to true or to an object with the following options: See RouteSecurityConfigurationObject */ + security?: boolean | RouteSecurityConfigurationObject; + /** HTTP state management (cookies) allows the server to store information on the client which is sent back to the server with every request (as defined in RFC 6265). state supports the following options: */ + state?: { + /** determines if incoming 'Cookie' headers are parsed and stored in the request.state object. Defaults to true. */ + parse?: boolean; + /** + * determines how to handle cookie parsing errors. Allowed values are: + * * 'error' - return a Bad Request (400) error response. This is the default value. + * * 'log' - report the error but continue processing the request. + * * 'ignore' - take no action. + */ + failAction: 'error' | 'log' | 'ignore'; + }; + /** request input validation rules for various request components. When using a Joi validation object, the values of the other inputs (i.e. headers, query, params, payload, and auth) are made available under the validation context (accessible in rules as Joi.ref('$query.key')). Note that validation is performed in order (i.e. headers, params, query, payload) and if type casting is used (converting a string to number), the value of inputs not yet validated will reflect the raw, unvalidated and unmodified values. If the validation rules for headers, params, query, and payload are defined at both the routes defaults level and an individual route, the individual route settings override the routes defaults (the rules are not merged). The validate object supports: */ + validate?: RouteValidationConfigurationObject; + /** define timeouts for processing durations: */ + timeout?: { + /** response timeout in milliseconds. Sets the maximum time allowed for the server to respond to an incoming client request before giving up and responding with a Service Unavailable (503) error response. Disabled by default (false). */ + server?: boolean | number; + /** by default, node sockets automatically timeout after 2 minutes. Use this option to override this behavior. Defaults to undefined which leaves the node default unchanged. Set to false to disable socket timeouts. */ + socket?: boolean | number; + }; + + /** + * TODO decide on moving these to an extended interface of RouteAdditionalConfigurationOptions + */ + /** + * ONLY WHEN ADDING NEW ROUTES (not when setting defaults). + * route description used for generating documentation + */ + description?: string; + /** + * ONLY WHEN ADDING NEW ROUTES (not when setting defaults). + * route notes used for generating documentation + */ + notes?: string | string[]; + /** + * ONLY WHEN ADDING NEW ROUTES (not when setting defaults). + * route tags used for generating documentation + */ + tags?: string[]; +} + +/** + * Route public interface + * When route information is returned or made available as a property, it is an object with the following: + * [See docs](https://hapijs.com/api/16.1.1#route-public-interface) + */ +export interface RoutePublicInterface { + /** the route HTTP method. */ + method: string; + /** the route path. */ + path: string; + /** the route vhost option if configured. */ + vhost?: string | string[]; + /** the [active realm] [See docs](https://hapijs.com/api/16.1.1#serverrealm) associated with the route.*/ + realm: ServerRealm; + /** the [route options] [See docs](https://hapijs.com/api/16.1.1#route-options) object with all defaults applied. */ + settings: RouteAdditionalConfigurationOptions; + /** the route internal normalized string representing the normalized path. */ + fingerprint: string; + /** route authentication utilities: */ + auth: { + /** authenticates the passed request argument against the route's authentication access configuration. Returns true if the request would have passed the route's access requirements. Note that the route's authentication mode and strategies are ignored. The only match is made between the request.auth.credentials scope and entity information and the route access configuration. Also, if the route uses dynamic scopes, the scopes are constructed against the request.query and request.params which may or may not match between the route and the request's route. If this method is called using a request that has not been authenticated (yet or at all), it will return false if the route requires any authentication. */ + access(request: Request): boolean; + }; +} + +export type RouteHandlerConfig = any; + +/** + * For context [See docs](https://hapijs.com/api/16.1.1#serverhandlername-method) + * For source [See docs](https://github.com/hapijs/hapi/blob/v16.1.1/lib/handler.js#L103) + * For source [See docs](https://github.com/hapijs/hapi/blob/v16.1.1/lib/route.js#L56-L60) + * TODO check the type of `RouteHandlerConfig` is correct for `defaults`. + */ +export interface MakeRouteHandler { + (route: RoutePublicInterface, options: RouteHandlerConfig): RouteHandler; + defaults?: RouteHandlerConfig | ((method: HTTP_METHODS_PARTIAL_lowercase) => RouteHandlerConfig); +} + +/** + * [See docs](https://hapijs.com/api/16.1.1#servertablehost) > return value + */ +export interface RoutingTableEntry { + /** the connection.info the connection the table was generated for. */ + info: ServerConnectionInfo; + /** the connection labels. */ + labels: string[]; + /** an array of routes where each route contains: */ + table: Route[]; +} + +/** + * [See docs](https://hapijs.com/api/16.1.1#servertablehost) > return value + */ +export interface Route { + /** + * the route config with defaults applied. + * TODO check type of RouteConfiguration here is correct + */ + settings: RouteConfiguration; + /** + * the HTTP method in lower case. + * TODO, check if it can contain 'head' or not. + */ + method: HTTP_METHODS_PARTIAL_lowercase; + /** the route path. */ + path: string; +} + +/** + * Route Prerequisites + * It is often necessary to perform prerequisite actions before the handler is called (e.g. load required reference data from a database). The route pre option allows defining such pre-handler methods. The methods are called in order. If the pre array contains another array, those methods are called in parallel. pre can be assigned a mixed array of: + * * arrays containing the elements listed below, which are executed in parallel. + * * objects see RoutePrerequisiteObjects + * * functions - same as including an object with a single method key. + * * strings - special short-hand notation for registered server methods using the format 'name(args)' (e.g. 'user(params.id)') where: + * * 'name' - the method name. The name is also used as the default value of assign. + * * 'args' - the method arguments (excluding next) where each argument is a property of the request object + * [See docs](https://hapijs.com/api/16.1.1#route-prerequisites) + * For context see RouteAdditionalConfigurationOptions > pre + * + * TODO follow up on "server methods" in "special short-hand notation for registered server methods" at https://hapijs.com/api/16.1.1#servermethodname-method-options + * TODO follow up on "request object" in "each argument is a property of the request object" at https://hapijs.com/api/16.1.1#request-object + */ +export type RoutePrerequisitesArray = RoutePrerequisitesPart[] | (RoutePrerequisitesPart[] | RoutePrerequisitesPart)[]; +export type RoutePrerequisitesPart = RoutePrerequisiteObjects | RoutePrerequisiteRequestHandler | string; + +/** + * see RoutePrerequisites > objects + */ +export interface RoutePrerequisiteObjects { + /** the function to call (or short-hand method string as described below [see RoutePrerequisitesArray]). the function signature is identical to a route handler as described in Route handler. */ + method: RoutePrerequisiteRequestHandler | string; + /** key name to assign the result of the function to within request.pre. */ + assign: string; + /* + * determines how to handle errors returned by the method. Allowed values are: + * * 'error' - returns the error response back to the client. This is the default value. + * * 'log' - logs the error but continues processing the request. If assign is used, the error will be assigned. + * * 'ignore' - takes no special action. If assign is used, the error will be assigned. + */ + failAction?: 'error' | 'log' | 'ignore'; +} + +/** + * For context see RouteAdditionalConfigurationOptions > response + */ +export interface RouteResponseConfigurationObject { + /** the default HTTP status code when the payload is empty. Value can be 200 or 204. Note that a 200 status code is converted to a 204 only at the time or response transmission (the response status code will remain 200 throughout the request lifecycle unless manually set). Defaults to 200. */ + emptyStatusCode?: number; + /** + * defines what to do when a response fails payload validation. Options are: + * * 'error' - return an Internal Server Error (500) error response. This is the default value. + * * 'log' - log the error but send the response. + * * a custom error handler function with the signature function(request, reply, source, error) where: + * * 'request' - the request object. + * * 'reply' - the continuation reply interface. + * * 'error' - the error returned from the validation schema. + * TODO update type of source once PR to hapi is concluded. + */ + failAction?: 'error' | 'log' | ((request: Request, reply: ReplyWithContinue, source: string, error: Boom.BoomError) => void); + /** if true, applies the validation rule changes to the response payload. Defaults to false. */ + modify?: boolean; + /** options to pass to Joi. Useful to set global options such as stripUnknown or abortEarly (the complete list is available [here](https://github.com/hapijs/joi/blob/master/API.md#validatevalue-schema-options-callback) ). Defaults to no options. */ + options?: JoiValidationOptions; + /** if false, payload range support is disabled. Defaults to true. */ + ranges?: boolean; + /** the percent of response payloads validated (0 - 100). Set to 0 to disable all validation. Defaults to 100 (all response payloads). */ + sample?: number; + /** the default response payload validation rules (for all non-error responses) */ + schema?: RouteResponseConfigurationScheme; + /** HTTP status-code-specific payload validation rules. The status key is set to an object where each key is a 3 digit HTTP status code and the value has the same definition as schema. If a response status code is not present in the status object, the schema definition is used, except for errors which are not validated by default. */ + status?: Dictionary; +} + +/** + * the default response payload validation rules (for all non-error responses) expressed as one of: + * * true - any payload allowed (no validation performed). This is the default. + * * false - no payload allowed. + * * a Joi validation object. This will receive the request's headers, params, query, payload, and auth credentials and isAuthenticated flags as context. + * * a validation function using the signature function(value, options, next) where: + * * value - the object containing the response object. + * * options - the server validation options, merged with an object containing the request's headers, params, payload, and auth credentials object and isAuthenticated flag. + * * next(err) - the callback function called when validation is completed. + * TODO check JoiValidationObject is correct for "a Joi validation object" + * + * For context see RouteAdditionalConfigurationOptions > response > schema + * and + * For context see RouteAdditionalConfigurationOptions > response > status + */ +export type RouteResponseConfigurationScheme = boolean | JoiValidationObject | ValidationFunctionForRouteReponse; + +/** + * see RouteResponseConfigurationScheme + * + * TODO check `value: Response` is correct as it says "**the object containing** the response object." not just "the response object". + * TODO check `options: JoiValidationOptions` is correct + * Also see ValidationFunctionForRouteValidate + */ +export interface ValidationFunctionForRouteReponse { + (value: Response, options: JoiValidationOptions, next: ContinuationFunction): void; +} + +/** + * For context see RouteAdditionalConfigurationOptions > security + */ +export interface RouteSecurityConfigurationObject { + /** controls the 'Strict-Transport-Security' header. If set to true the header will be set to max-age=15768000, if specified as a number the maxAge parameter will be set to that number. Defaults to true. You may also specify an object with the following fields: */ + hsts?: boolean | number | { + /** the max-age portion of the header, as a number. Default is 15768000. */ + maxAge?: number; + /** a boolean specifying whether to add the includeSubDomains flag to the header. */ + includeSubdomains?: boolean; + /** a boolean specifying whether to add the 'preload' flag (used to submit domains inclusion in Chrome's HTTP Strict Transport Security (HSTS) preload list) to the header. */ + preload?: boolean; + }; + /** controls the 'X-Frame-Options' header. When set to true the header will be set to DENY, you may also specify a string value of 'deny' or 'sameorigin'. Defaults to true. To use the 'allow-from' rule, you must set this to an object with the following fields: */ + xframe?: true | 'deny' | 'sameorigin' | { + /** may also be 'deny' or 'sameorigin' but set directly as a string for xframe */ + rule: 'allow-from'; + /** when rule is 'allow-from' this is used to form the rest of the header, otherwise this field is ignored. If rule is 'allow-from' but source is unset, the rule will be automatically changed to 'sameorigin'. */ + source: string; + }; + /** boolean that controls the 'X-XSS-PROTECTION' header for IE. Defaults to true which sets the header to equal '1; mode=block'. NOTE: This setting can create a security vulnerability in versions of IE below 8, as well as unpatched versions of IE8. See [here](https://hackademix.net/2009/11/21/ies-xss-filter-creates-xss-vulnerabilities/) and [here](https://technet.microsoft.com/library/security/ms10-002) for more information. If you actively support old versions of IE, it may be wise to explicitly set this flag to false. [Kept typing non optional to force this security related documentation to be read.] */ + xss: boolean; + /** boolean controlling the 'X-Download-Options' header for IE, preventing downloads from executing in your context. Defaults to true setting the header to 'noopen'. */ + noOpen?: boolean; + /** boolean controlling the 'X-Content-Type-Options' header. Defaults to true setting the header to its only and default option, 'nosniff' */ + noSniff?: boolean; +} + +/** + * request input validation rules for various request components. When using a Joi validation object, the values of the other inputs (i.e. headers, query, params, payload, and auth) are made available under the validation context (accessible in rules as Joi.ref('$query.key')). Note that validation is performed in order (i.e. headers, params, query, payload) and if type casting is used (converting a string to number), the value of inputs not yet validated will reflect the raw, unvalidated and unmodified values. If the validation rules for headers, params, query, and payload are defined at both the routes defaults level and an individual route, the individual route settings override the routes defaults (the rules are not merged). The validate object supports: + * For context see RouteAdditionalConfigurationOptions > validate + * TODO check JoiValidationObject is correct for "a Joi validation object" + */ +export interface RouteValidationConfigurationObject { + /** + * validation rules for incoming request headers (note that all header field names must be in lowercase to match the headers normalized by node). Values allowed: + * * true - any headers allowed (no validation performed). This is the default. + * * false - no headers allowed (this will cause all valid HTTP requests to fail). + * * a Joi validation object. + * * a validation function using the signature function(value, options, next) where: + * * value - the object containing the request headers. + * * options - the server validation options. + * * next(err, value) - the callback function called when validation is completed. + */ + headers?: boolean | JoiValidationObject | ValidationFunctionForRouteValidate; + /** + * validation rules for incoming request path parameters, after matching the path against the route and extracting any parameters then stored in request.params. Values allowed: + * Same as `headers`, see above. + */ + params?: boolean | JoiValidationObject | ValidationFunctionForRouteValidate; + /** + * validation rules for an incoming request URI query component (the key-value part of the URI between '?' and '#'). The query is parsed into its individual key-value pairs and stored in request.query prior to validation. Values allowed: + * Same as `headers`, see above. + */ + query?: boolean | JoiValidationObject | ValidationFunctionForRouteValidate; + /** + * validation rules for an incoming request payload (request body). Values allowed: + * Same as `headers`, see above, with the addition that: + * * a Joi validation object. Note that empty payloads are represented by a null value. If a validation schema is provided and empty payload are supported, it must be explicitly defined by setting the payload value to a joi schema with null allowed (e.g. Joi.object({ /* keys here * / }).allow(null)). + */ + payload?: boolean | JoiValidationObject | ValidationFunctionForRouteValidate; + /** an optional object with error fields copied into every validation error response. */ + errorFields?: any; + /** + * determines how to handle invalid requests. Allowed values are: + * * 'error' - return a Bad Request (400) error response. This is the default value. + * * 'log' - log the error but continue processing the request. + * * 'ignore' - take no action. + * * a custom error handler function with the signature function(request, reply, source, error) see RouteFailFunction + */ + failAction?: 'error' | 'log' | 'ignore' | RouteFailFunction; + /** options to pass to Joi. Useful to set global options such as stripUnknown or abortEarly (the complete list is [available here](https://github.com/hapijs/joi/blob/master/API.md#validatevalue-schema-options-callback)). Defaults to no options. */ + options?: JoiValidationOptions; +} + +/** + * a validation function using the signature function(value, options, next) where: + * For context see RouteAdditionalConfigurationOptions > validate (IRouteValidationConfigurationObject) + * + * TODO check `value: Response` is correct as it says "**the object containing** the response object." not just "the response object". + * TODO check `options: JoiValidationOptions` is correct + * TODO type of the returned value? + * Also see ValidationFunctionForRouteReponse + * @param value - the object containing the request headers. + * @param options - the server validation options. + * @param next(err, value) - the callback function called when validation is completed. + */ +export interface ValidationFunctionForRouteValidate { + (value: Response, options: JoiValidationOptions, next: ContinuationValueFunction): void; +} + +/** + * a custom error handler function with the signature 'function(request, reply, source, error)` + * @param request - the request object. + * @param reply - the continuation reply interface. + * @param source - the source of the invalid field (e.g. 'headers', 'params', 'query', 'payload'). + * @param error - the error object prepared for the client response (including the validation function error under error.data). + */ +export interface RouteFailFunction { + (request: Request, reply: ReplyWithContinue, source: string, error: any): void; +} + +/** + * optional cookie settings + * [See docs](https://hapijs.com/api/16.1.1#serverstatename-options) + * Related to see ConnectionConfigurationServerDefaults + */ +export interface ServerStateCookieConfiguationObject { + /** time-to-live in milliseconds. Defaults to null (session time-life - cookies are deleted when the browser is closed). */ + ttl?: number | null; + /** sets the 'Secure' flag. Defaults to true. */ + isSecure?: boolean; + /** sets the 'HttpOnly' flag. Defaults to true. */ + isHttpOnly?: boolean; + /** + * sets the 'SameSite' flag where the value must be one of: + * * false - no flag. + * * 'Strict' - sets the value to 'Strict' (this is the default value). + * * 'Lax' - sets the value to 'Lax'. + */ + isSameSite?: false | 'Strict' | 'Lax'; + /** the path scope. Defaults to null (no path). */ + path?: string | null; + /** the domain scope. Defaults to null (no domain). */ + domain?: string | null; + /** + * if present and the cookie was not received from the client or explicitly set by the route handler, the cookie is automatically added to the response with the provided value. The value can be a function with signature function(request, next) where: + * * request - the request object. + * * next - the continuation function using the function(err, value) signature. + */ + autoValue?(request: Request, next: ContinuationValueFunction): void; + /** + * encoding performs on the provided value before serialization. Options are: + * * 'none' - no encoding. When used, the cookie value must be a string. This is the default value. + * * 'base64' - string value is encoded using Base64. + * * 'base64json' - object value is JSON-stringified then encoded using Base64. + * * 'form' - object value is encoded using the x-www-form-urlencoded method. + * * 'iron' - Encrypts and sign the value using iron. + */ + encoding?: 'none' | 'base64' | 'base64json' | 'form' | 'iron'; + /** + * an object used to calculate an HMAC for cookie integrity validation. This does not provide privacy, only a mean to verify that the cookie value was generated by the server. Redundant when 'iron' encoding is used. Options are: + * * integrity - algorithm options. Defaults to require('iron').defaults.integrity. + * * password - password used for HMAC key generation (must be at least 32 characters long). + */ + sign?: { + integrity?: any; // TODO make iron definitions and getting typing from iron + password: string; + }; + /** password used for 'iron' encoding (must be at least 32 characters long). */ + password?: string; + /** options for 'iron' encoding. Defaults to require('iron').defaults. */ + iron?: any; // TODO make iron definitions and getting typing from iron + /** if true, errors are ignored and treated as missing cookies. */ + ignoreErrors?: boolean; + /** if true, automatically instruct the client to remove invalid cookies. Defaults to false. */ + clearInvalid?: boolean; + /** if false, allows any cookie value including values in violation of RFC 6265. Defaults to true. */ + strictHeader?: boolean; + /** used by proxy plugins (e.g. h2o2). */ + passThrough?: any; +} + +/** + * [See docs](https://hapijs.com/api/16.1.1#serverconnections) + */ +export interface ServerConnection { + /** settings - the connection configuration object passed to server.connection() after applying the server defaults. */ + settings: ServerConnectionOptions; + /** server - the connection's Server object. */ + server: Server; + /** type - set to 'tcp' is the connection is listening on a TCP port, otherwise to 'socket'(a UNIX domain socket or a Windows named pipe). */ + type: 'tcp' | 'socket'; + /** + * registrations + * Described [See docs](https://hapijs.com/api/16.1.1#serverregistrations) "When the server contains more than one connection, each server.connections array member provides its own connection.registrations." + */ + registrations: ServerRegisteredPlugins; + /** states - TODO contribute docs to hapi if they want, and then update type here */ + states: any; + /** auth - TODO contribute docs to hapi if they want, and then update type here */ + auth: any; + /** + * plugins + * TODO contribute docs to hapi if they want. Assuming similar to `registrations`, `listener`, `info`, etc + */ + plugins: PluginsStates; + /** + * app + * TODO contribute docs to hapi if they want. Assuming similar to `registrations`, `listener`, `info`, etc + */ + app: any; + /** Described in server.listener [See docs](https://hapijs.com/api/16.1.1#serverlistener) */ + listener: ServerListener; + /** Described in server.info [See docs](https://hapijs.com/api/16.1.1#serverinfo) */ + info: ServerConnectionInfo; + /** Described in server.inject [See docs](https://hapijs.com/api/16.1.1#serverinjectoptions-callback) */ + inject(options: string | InjectedRequestOptions, callback: (res: InjectedResponseObject) => void): void; + inject(options: string | InjectedRequestOptions, ): Promise; + /** Described in server.table [See docs](https://hapijs.com/api/16.1.1#servertablehost) */ + table(host?: string): RoutingTableEntry; + /** Described in server.table [See docs](https://hapijs.com/api/16.1.1#serverlookupid) */ + lookup(id: string): RoutePublicInterface | null; + /** Described in server.table [See docs](https://hapijs.com/api/16.1.1#servermatchmethod-path-host) */ + match(method: HTTP_METHODS, path: string, host?: string): RoutePublicInterface | null; +} + +/** + * [See docs](https://hapijs.com/api/16.1.1#serverinfo) + */ +export interface ServerConnectionInfo { + /** a unique connection identifier (using the format '{hostname}:{pid}:{now base36}'). */ + id: string; + /** the connection creation timestamp. */ + created: number; + /** the connection start timestamp (0 when stopped). */ + started: number; + /** + * the connection port based on the following rules: + * * the configured port value before the server has been started. + * * the actual port assigned when no port is configured or set to 0 after the server has been started. + * TODO check this type. What happens when socket is a UNIX domain socket or Windows named pipe? + */ + port: number | string; + /** the host name the connection was configured to. Defaults to the operating system hostname when available, otherwise 'localhost'. */ + host: string; + /** the active IP address the connection was bound to after starting. Set to undefined until the server has been started or when using a non TCP port (e.g. UNIX domain socket). */ + address: undefined | string; + /** the protocol used. 'socket' when UNIX domain socket or Windows named pipe. */ + protocol: 'http' | 'https' | 'socket'; + /** a string representing the connection (e.g. 'http://example.com:8080' or 'socket:/unix/domain/socket/path'). Contains the uri setting if provided, otherwise constructed from the available settings. If no port is available or set to 0, the uri will not include a port component. */ + uri: string; +} + +/** + * [See docs](https://hapijs.com/api/16.1.1#serverlistener) + */ +export type ServerListener = http.Server; + +/** + * server.realm + * The realm object contains server-wide or plugin-specific state that can be shared across various methods. For example, when calling server.bind(), the active realm settings.bind property is set which is then used by routes and extensions added at the same level (server root or plugin). Realms are a limited version of a sandbox where plugins can maintain state used by the framework when adding routes, extensions, and other properties. + * [See docs](https://hapijs.com/api/16.1.1#serverrealm) + */ +export interface ServerRealm { + /** when the server object is provided as an argument to the plugin register() method, modifiers provides the registration preferences passed the server.register() method and includes: */ + modifiers: { + /** routes preferences: */ + route: { + /** the route path prefix used by any calls to server.route() from the server. Note that if a prefix is used and the route path is set to '/', the resulting path will not include the trailing slash. */ + prefix: string; + /** the route virtual host settings used by any calls to server.route() from the server. */ + vhost: string; + } + }; + /** the active plugin name (empty string if at the server root). */ + plugin: string; + /** the plugin options object passed at registration. */ + pluginOptions: any; // OptionsPassedToPlugin; + /** plugin-specific state to be shared only among activities sharing the same active state. plugins is an object where each key is a plugin name and the value is the plugin state. */ + plugins: PluginsStates; + /** settings overrides (from RouteAdditionalConfigurationOptions) */ + settings: { + files: { + relativeTo: string; + }; + bind: any; + }; +} + +/** + * [See docs](https://hapijs.com/api/16.1.1#serverregistrations) + */ +export interface ServerRegisteredPlugins { + [pluginName: string]: ServerRegisteredPlugin; +} + +/** + * [See docs](https://hapijs.com/api/16.1.1#serverregistrations) + */ +export interface ServerRegisteredPlugin { + /** the plugin version. */ + version: string; + /** the plugin name. */ + name: string; + /** + * options used to register the plugin. + * TODO update with outcome of: https://github.com/hapijs/hapi/pull/3479 + */ + options: any; // OptionsPassedToPlugin; + /** plugin registration attributes. */ + attributes: PluginAttributes; +} + +export interface ServerAuth { + /** + * server.auth.api + * An object where each key is a strategy name and the value is the exposed strategy API. Available on when the authentication scheme exposes an API by returning an api key in the object returned from its implementation function. + * When the server contains more than one connection, each server.connections array member provides its own connection.auth.api object. + * [See docs](https://hapijs.com/api/16.1.1#serverauthapi) + */ + api: Dictionary; + /** + * server.auth.default + * Sets a default strategy which is applied to every route + * The default does not apply when the route config specifies auth as false, or has an authentication strategy configured (contains the strategy or strategies authentication settings). Otherwise, the route authentication config is applied to the defaults. + * Note that if the route has authentication config, the default only applies at the time of adding the route, not at runtime. This means that calling default() after adding a route with some authentication config will have no impact on the routes added prior. However, the default will apply to routes added before default() is called if those routes lack any authentication config. + * The default auth strategy configuration can be accessed via connection.auth.settings.default. To obtain the active authentication configuration of a route, use connection.auth.lookup(request.route). + * [See docs](https://hapijs.com/api/16.1.1#serverauthdefaultoptions) + */ + default(options: string | AuthOptions): void; + /** + * server.auth.scheme + * Registers an authentication scheme + * [See docs](https://hapijs.com/api/16.1.1#serverauthschemename-scheme) + * @param name the scheme name. + * @param scheme the method implementing the scheme with signature function(server, options) see ServerAuthScheme + */ + scheme(name: string, scheme: ServerAuthScheme): void; + /** + * Registers an authentication strategy + * [See docs](https://hapijs.com/api/16.1.1#serverauthstrategyname-scheme-mode-options) + * @param name the strategy name. + * @param scheme the scheme name (must be previously registered using server.auth.scheme()). + * @param mode if set to true (which is the same as 'required') or to a valid authentication mode ('required', 'optional', 'try'), the scheme is automatically assigned as the default strategy for any route without an auth config. Can only be assigned to a single server strategy. Defaults to false (no default settings). + * @param options scheme options based on the scheme requirements. + */ + strategy(name: string, scheme: string, options?: any): void; + strategy(name: string, scheme: string, mode: boolean | 'required' | 'optional' | 'try', options?: any): void; + /** + * Tests a request against an authentication strategy + * Note that the test() method does not take into account the route authentication configuration. It also does not perform payload authentication. It is limited to the basic strategy authentication execution. It does not include verifying scope, entity, or other route properties. + * [See docs](https://hapijs.com/api/16.1.1#serverauthteststrategy-request-next) + * @param strategy - the strategy name registered with server.auth.strategy(). + * @param request - the request object. + * @param next - the callback function with signature function(err, credentials) where: + * * err - the error if authentication failed. + * * credentials - the authentication credentials object if authentication was successful. + */ + test(strategy: string, request: Request, next: (err: Error | null, credentials: AuthenticatedCredentials) => void): void; +} + +export type Strategy = any; +export type SchemeSettings = any; + +/** + * the method implementing the scheme with signature function(server, options) where: + * [See docs](https://hapijs.com/api/16.1.1#serverauthschemename-scheme) + * @param server a reference to the server object the scheme is added to. + * @param options optional scheme settings used to instantiate a strategy. + */ +export interface ServerAuthScheme { + (server: Server, options: SchemeSettings): SchemeMethodResult; +} + +/** + * The scheme method must return an object with the following + * [See docs](https://hapijs.com/api/16.1.1#serverauthschemename-scheme) + */ +export interface SchemeMethodResult { + /** optional object which is exposed via the server.auth.api object. */ + api?: Strategy; + /** + * required function called on each incoming request configured with the authentication scheme + * When the scheme authenticate() method implementation calls reply() with an error condition, the specifics of the error affect whether additional authentication strategies will be attempted (if configured for the route). If the err passed to the reply() method includes a message, no additional strategies will be attempted. If the err does not include a message but does include the scheme name (e.g. Boom.unauthorized(null, 'Custom')), additional strategies will be attempted in the order of preference (defined in the route configuration). If authentication fails the scheme names will be present in the 'WWW-Authenticate' header. + * @param request the request object. + * @param reply the reply interface the authentication method must call when done authenticating the request + */ + authenticate(request: Request, reply: ReplySchemeAuth): void; + /** + * optional function called to authenticate the request payload + * When the scheme payload() method returns an error with a message, it means payload validation failed due to bad payload. If the error has no message but includes a scheme name (e.g. Boom.unauthorized(null, 'Custom')), authentication may still be successful if the route auth.payload configuration is set to 'optional'. + * @param request the request object. + * @param reply is called if authentication failed + */ + payload?(request: Request, reply: ReplySchemeAuthOfPayload): void; + /** + * optional function called to decorate the response with authentication headers before the response headers or payload is written where: + * @param request the request object. + * @param reply is called if an error occured + */ + response?(request: Request, reply: ReplySchemeAuthDecorateResponse): void; + /** an optional object with the following keys: */ + options?: { + /** if true, requires payload validation as part of the scheme and forbids routes from disabling payload auth validation. Defaults to false. */ + payload?: boolean; + }; +} + +export interface ServerCacheMethod { + /** + * Provisions a cache segment within the server cache facility + * [See docs](https://hapijs.com/api/16.1.1#servercacheoptions) + */ + (options: CatboxServerCacheConfiguration): Catbox.Policy; + /** + * Provisions a server cache as described in server.cache + * If no callback is provided, a Promise object is returned. + * 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. + * [See docs](https://hapijs.com/api/16.1.1#servercacheprovisionoptions-callback) + * @param options same as the server cache configuration options. + * @param callback the callback method when cache provisioning is completed or failed with the signature function(err) where: + * * err - any cache startup error condition. + */ + provision(options: CatboxServerCacheConfiguration): Promise; + provision(options: CatboxServerCacheConfiguration, callback: (err?: Error) => void): void; +} + +/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Request + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */ +// TODO: move to separate file http://stackoverflow.com/questions/43276921 + +/** + * Request object + * The request object is created internally for each incoming request. It is different from the node.js request object received from the HTTP server callback (which is available in request.raw.req). The request object methods and properties change throughout the request lifecycle. + * [See docs](https://hapijs.com/api/16.1.1#request-object) + * [See docs](https://hapijs.com/api/16.1.1#request-properties) + */ +export class Request extends Podium { + /** application-specific state. Provides a safe place to store application data without potential conflicts with the framework. Should not be used by plugins which should use plugins[name]. */ + app: any; + /** authentication information */ + auth: { + /** true if the request has been successfully authenticated, otherwise false. */ + isAuthenticated: boolean; + /** the credential object received during the authentication process. The presence of an object does not mean successful authentication. */ + credentials: any; + /** an artifact object received from the authentication strategy and used in authentication-related actions. */ + artifacts: any; + /** the route authentication mode. */ + mode: string; + /** the authentication error is failed and mode set to 'try'. */ + error: Error; + }; + /** the connection the request was received by. */ + connection: ServerConnection; + /** the node domain object used to protect against exceptions thrown in extensions, handlers and route prerequisites. Can be used to manually bind callback functions otherwise bound to other domains. Set to null when the server useDomains options is false. */ + domain: domain.Domain | null; + /** the raw request headers (references request.raw.headers). */ + headers: Dictionary; + /** a unique request identifier (using the format '{now}:{connection.info.id}:{5 digits counter}').*/ + id: string; + /** request information */ + info: { + /** the request preferred encoding. */ + acceptEncoding: string; + /** if CORS is enabled for the route, contains the following: */ + cors: { + /** + * true if the request 'Origin' header matches the configured CORS restrictions. Set to false if no 'Origin' header is found or if it does not match. Note that this is only available after the 'onRequest' extension point as CORS is configured per-route and no routing decisions are made at that point in the request lifecycle. + * Note: marking as optional as "... this is only available after ..." + */ + isOriginMatch?: boolean; + }; + /** content of the HTTP 'Host' header (e.g. 'example.com:8080'). */ + host: string; + /** the hostname part of the 'Host' header (e.g. 'example.com'). */ + hostname: string; + /** request reception timestamp. */ + received: number; + /** content of the HTTP 'Referrer' (or 'Referer') header. */ + referrer: string; + /** remote client IP address. */ + remoteAddress: string; + /** + * remote client port. + * Set to string in casethey're requesting from a UNIX domain socket. + * TODO, what type does Hapi return, should this be number | string? + */ + remotePort: string; + /** request response timestamp (0 is not responded yet). */ + responded: number; + }; + /** the request method in lower case (e.g. 'get', 'post'). */ + method: string; + /** the parsed content-type header. Only available when payload parsing enabled and no payload error occurred. */ + mime: string; + /** an object containing the values of params, query, and payload before any validation modifications made. Only set when input validation is performed. */ + orig: { + params: any; + query: any; + payload: any; + }; + /** an object where each key is a path parameter name with matching value as described in Path parameters [See docs](https://hapijs.com/api/16.1.1#path-parameters). */ + params: Dictionary; + /** an array containing all the path params values in the order they appeared in the path. */ + paramsArray: string[]; + /** the request URI's pathname [See docs](https://nodejs.org/api/url.html#url_urlobject_pathname) component. */ + path: string; + /** + * the request payload based on the route payload.output and payload.parse settings. + * TODO check this typing and add references / links. + */ + payload: stream.Readable | Buffer | any; + /** plugin-specific state. Provides a place to store and pass request-level plugin data. The plugins is an object where each key is a plugin name and the value is the state. */ + plugins: PluginsStates; + /** an object where each key is the name assigned by a route prerequisites function. The values are the raw values provided to the continuation function as argument. For the wrapped response object, use responses. */ + pre: Object; + /** the response object when set. The object can be modified but must not be assigned another object. To replace the response with another from within an extension point, use reply(response) to override with a different response. Contains null when no response has been set (e.g. when a request terminates prematurely when the client disconnects). */ + response: Response | null; + /** same as pre but represented as the response object created by the pre method. */ + preResponses: Object; + /** + * an object containing the query parameters. + * TODO update with outcome of: https://github.com/hapijs/hapi/pull/3479 + */ + query: Object; + /** an object containing the Node HTTP server objects. **Direct interaction with these raw objects is not recommended.** */ + raw: { + req: http.IncomingMessage; // Or http.ClientRequest http://www.apetuts.com/tutorial/node-js-http-client-request-class/ ? + res: http.ServerResponse; + }; + /** + * the route public interface. + * Optional due to "request.route is not yet populated at this point." [See docs](https://hapijs.com/api/16.1.1#request-lifecycle) + */ + route?: RoutePublicInterface; + /** the server object. */ + server: Server; + /** an object containing parsed HTTP state information (cookies) where each key is the cookie name and value is the matching cookie content after processing using any registered cookie definition. */ + state: Dictionary; + /** the parsed request URI */ + url: url.Url; + + /** + * request.setUrl(url, [stripTrailingSlash]) + * Available only in 'onRequest' extension methods. + * Changes the request URI before the router begins processing the request + * [See docs](https://hapijs.com/api/16.1.1#requestseturlurl-striptrailingslash) + * @param url the new request URI. If url is a string, it is parsed with node's URL parse() method. url can also be set to an object compatible with node's URL parse() method output. + * @param stripTrailingSlash if true, strip the trailing slash from the path. Defaults to false. + */ + setUrl(url: string | url.Url, stripTrailingSlash?: boolean): void; + /** + * request.setMethod(method) + * Available only in 'onRequest' extension methods. + * Changes the request method before the router begins processing the request + * [See docs](https://hapijs.com/api/16.1.1#requestsetmethodmethod) + * @param method is the request HTTP method (e.g. 'GET'). + */ + setMethod(method: HTTP_METHODS): void; + /** + * request.generateResponse(source, [options]) + * Always available. + * Returns a response which you can pass into the reply interface where: + * [See docs](https://hapijs.com/api/16.1.1#requestgenerateresponsesource-options) + * @param source the object to set as the source of the reply interface. TODO, submit a PR to clarify this doc, from the source code it's clear that "the object to set" refers to something of type `ReplyValue` i.e. that can be null, string, number, object, Stream, Promise, or Buffer. + * @param options options for the method, optional. Not documented yet, perhaps not very important. + */ + generateResponse(source?: ReplyValue, options?: {marshal?: any; prepare?: any; close?: any; variety?: any}): Response; + /** + * request.log(tags, [data, [timestamp]]) + * Always available. + * Logs request-specific events. When called, the server emits a 'request' event which can be used by other listeners or plugins. + * Any logs generated by the server internally will be emitted only on the 'request-internal' channel and will include the event.internal flag set to true. + * [See docs](https://hapijs.com/api/16.1.1#requestlogtags-data-timestamp) + * @param tags a string or an array of strings (e.g. ['error', 'database', 'read']) used to identify the event. Tags are used instead of log levels and provide a much more expressive mechanism for describing and filtering events. + * @param data an optional message string or object with the application data being logged. If data is a function, the function signature is function() and it called once to generate (return value) the actual data emitted to the listeners. + * @param timestamp an optional timestamp expressed in milliseconds. Defaults to Date.now() (now). + */ + log(tags: string | string[], data?: string | Object | (() => string | Object), timestamp?: number): void; + /** + * request.getLog([tags], [internal]) + * Always available. + * Returns an array containing the events matching any of the tags specified (logical OR) + * Note that this methods requires the route log configuration set to true. + * [See docs](https://hapijs.com/api/16.1.1#requestgetlogtags-internal) + * @param tags is a single tag string or array of tag strings. If no tags specified, returns all events. + * @param internal filters the events to only those with a matching event.internal value. If true, only internal logs are included. If false, only user event are included. Defaults to all events (undefined). + */ + getLog(tags?: string | string[], internal?: boolean): string[]; + getLog(internal?: boolean): string[]; + /** + * request.tail([name]) + * Available until immediately after the 'response' event is emitted. + * Adds a request tail which has to complete before the request lifecycle is complete. + * Returns a tail function which must be called when the tail activity is completed. + * Tails are actions performed throughout the request lifecycle, but which may end after a response is sent back to the client. For example, a request may trigger a database update which should not delay sending back a response. However, it is still desirable to associate the activity with the request when logging it (or an error associated with it). + * When all tails completed, the server emits a 'tail' event. + * [See docs](https://hapijs.com/api/16.1.1#requesttailname) + * @param name an optional tail name used for logging purposes. + */ + tail(name?: string): (() => void); + /** + * The server.decorate('request', ...) method can modify this prototype/interface. + * Have disabled these typings as there is a better alternative, see example in: tests/server/decorate.ts + * [And discussion here](https://github.com/DefinitelyTyped/DefinitelyTyped/issues/14517#issuecomment-298891630) + */ + // [index: string]: any; +} + +export type HTTP_METHODS_PARTIAL_lowercase = 'get' | 'post' | 'put' | 'patch' | 'delete' | 'options'; +export type HTTP_METHODS_PARTIAL = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | HTTP_METHODS_PARTIAL_lowercase; +export type HTTP_METHODS = 'HEAD' | 'head' | HTTP_METHODS_PARTIAL; + +/** + * Request events + * The request object supports the following events: + * * 'peek' - emitted for each chunk of payload data read from the client connection. The event method signature is function(chunk, encoding). + * * 'finish' - emitted when the request payload finished reading. The event method signature is function (). + * * 'disconnect' - emitted when a request errors or aborts unexpectedly. + * [See docs](https://hapijs.com/api/16.1.1#request-events) + */ +export type RequestEventTypes = 'peek' | 'finish' | 'disconnect'; + +/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Handler functions + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */ +// TODO: move to separate file http://stackoverflow.com/questions/43276921 + +/** + * Extending RouteConfiguration.handler + * + * The hapi documentation allows for the RouteConfiguration.handler type to have + * `{[pluginName: string]: pluginOptions}` + * "handler can be assigned an object with a single key using the name of a registered handler type and value with the options passed to the registered handler" + * This can be provided by extending the hapi module definition as follows, + * see h2o2 for example: + * + * declare module 'hapi' { + * interface RouteHandlerPlugins { + * proxy?: ... + */ +export interface RouteHandlerPlugins { } /** - * undocumented. The connection object constructed after calling server.connection(); - * can be accessed via server.connections; or request.connection; + * The route handler function uses the signature function(request, reply) (NOTE: do not use a fat arrow style function for route handlers as they do not allow context binding and will cause problems when used in conjunction with server.bind) where: + * * request - is the incoming request object (this is not the node.js request object). + * * reply - the reply interface the handler must call to set a response and return control back to the framework. + * [See docs](https://hapijs.com/api/16.1.1#route-handler) + * Same function signature used by request extension point used in server.ext(event), see ServerExtConfigurationObject.method */ -export class ServerConnection extends Events.EventEmitter { - domain: any; - _events: { route: Function, domain: Function, _events: Function, _eventsCount: Function, _maxListeners: Function }; - _eventsCount: number; - settings: IServerConnectionOptions; - server: Server; - /** ex: "tcp" */ - type: string; - _started: boolean; - /** dictionary of sockets */ - _connections: { [ip_port: string]: any }; - _onConnection: Function; - registrations: any; - _extensions: any; - _requestCounter: { value: number; min: number; max: number }; - _load: any; - states: { - settings: any; cookies: any; names: any[] - }; - auth: { connection: ServerConnection; _schemes: any; _strategies: any; settings: any; api: any; }; - _router: any; - MSPluginsCollection: any; - applicationCache: any; - addEventListener: any; - info: IServerConnectionInfo; +export interface RouteHandler { + (request: Request, reply: ReplyNoContinue): void; + // (request: Request, reply: StrictReply): void; } -export type RequestExtPoints = "onRequest" | "onPreResponse" | "onPreAuth" | "onPostAuth" | "onPreHandler" | "onPostHandler" | "onPreResponse"; -export type ServerExtPoints = "onPreStart" | "onPostStart" | "onPreStop" | "onPostStop"; +/** + * "the function to call, the function signature is identical to a route handler as described in Route handler." + * [See docs](https://hapijs.com/api/16.1.1#route-prerequisites) Route prerequisites + */ +export type RoutePrerequisiteRequestHandler = RouteHandler; -/** Server http://hapijs.com/api#server - rver object is the main application container. The server manages all incoming connections along with all the facilities provided by the framework. A server can contain more than one connection (e.g. listen to port 80 and 8080). - Server events - The server object inherits from Events.EventEmitter and emits the following events: - 'log' - events logged with server.log() and server events generated internally by the framework. - 'start' - emitted when the server is started using server.start(). - 'stop' - emitted when the server is stopped using server.stop(). - 'request' - events generated by request.log(). Does not include any internally generated events. - 'request-internal' - request events generated internally by the framework (multiple events per request). - 'request-error' - emitted whenever an Internal Server Error (500) error response is sent. Single event per request. - 'response' - emitted after the response is sent back to the client (or when the client connection closed and no response sent, in which case request.response is null). Single event per request. - 'tail' - emitted when a request finished processing, including any registered tails. Single event per request. - Note that the server object should not be used to emit application events as its internal implementation is designed to fan events out to the various plugin selections and not for application events. - MORE EVENTS HERE: http://hapijs.com/api#server-events*/ -export class Server extends Events.EventEmitter { - constructor(options?: IServerOptions); - - /** Provides a safe place to store server-specific run-time application data without potential conflicts with the framework internals. The data can be accessed whenever the server is accessible. Initialized with an empty object. - var Hapi = require('hapi'); - server = new Hapi.Server(); - server.app.key = 'value'; - var handler = function (request, reply) { - return reply(request.server.app.key); - }; */ - app: any; - /** An array containing the server's connections. When the server object is returned from server.select(), the connections array only includes the connections matching the selection criteria. - var server = new Hapi.Server(); - server.connection({ port: 80, labels: 'a' }); - server.connection({ port: 8080, labels: 'b' }); - // server.connections.length === 2 - var a = server.select('a'); - // a.connections.length === 1*/ - connections: ServerConnection[]; - /** When the server contains exactly one connection, info is an object containing information about the sole connection. - * When the server contains more than one connection, each server.connections array member provides its own connection.info. - var server = new Hapi.Server(); - server.connection({ port: 80 }); - // server.info.port === 80 - server.connection({ port: 8080 }); - // server.info === null - // server.connections[1].info.port === 8080 - */ - info: IServerConnectionInfo; - /** An object containing the process load metrics (when load.sampleInterval is enabled): - rss - RSS memory usage. - var Hapi = require('hapi'); - var server = new Hapi.Server({ load: { sampleInterval: 1000 } }); - console.log(server.load.rss);*/ - load: { - /** - event loop delay milliseconds.*/ - eventLoopDelay: number; - /** - V8 heap usage.*/ - heapUsed: number; - }; - /** When the server contains exactly one connection, listener is the node HTTP server object of the sole connection. - When the server contains more than one connection, each server.connections array member provides its own connection.listener. - var Hapi = require('hapi'); - var SocketIO = require('socket.io'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - var io = SocketIO.listen(server.listener); - io.sockets.on('connection', function(socket) { - socket.emit({ msg: 'welcome' }); - });*/ - listener: http.Server; - - /** server.methods - An object providing access to the server methods where each server method name is an object property. - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.method('add', function (a, b, next) { - return next(null, a + b); - }); - server.methods.add(1, 2, function (err, result) { - // result === 3 - });*/ - methods: IDictionary; - - /** server.mime - Provides access to the server MIME database used for setting content-type information. The object must not be modified directly but only through the mime server setting. - var Hapi = require('hapi'); - var options = { - mime: { - override: { - 'node/module': { - source: 'steve', - compressible: false, - extensions: ['node', 'module', 'npm'], - type: 'node/module' - } - } - } - }; - var server = new Hapi.Server(options); - // server.mime.path('code.js').type === 'application/javascript' - // server.mime.path('file.npm').type === 'node/module'*/ - mime: any; - /**server.plugins - An object containing the values exposed by each plugin registered where each key is a plugin name and the values are the exposed properties by each plugin using server.expose(). Plugins may set the value of the server.plugins[name] object directly or via the server.expose() method. - exports.register = function (server, options, next) { - server.expose('key', 'value'); - // server.plugins.example.key === 'value' - return next(); - }; - exports.register.attributes = { - name: 'example' - };*/ - plugins: IDictionary; - /** server.realm - The realm object contains server-wide or plugin-specific state that can be shared across various methods. For example, when calling server.bind(), the active realm settings.bind property is set which is then used by routes and extensions added at the same level (server root or plugin). Realms are a limited version of a sandbox where plugins can maintain state used by the framework when adding routes, extensions, and other properties. - modifiers - when the server object is provided as an argument to the plugin register() method, modifiers provides the registration preferences passed the server.register() method and includes: - route - routes preferences: - prefix - the route path prefix used by any calls to server.route() from the server. - vhost - the route virtual host settings used by any calls to server.route() from the server. - plugin - the active plugin name (empty string if at the server root). - plugins - plugin-specific state to be shared only among activities sharing the same active state. plugins is an object where each key is a plugin name and the value is the plugin state. - settings - settings overrides: - files.relativeTo - bind - The server.realm object should be considered read-only and must not be changed directly except for the plugins property can be directly manipulated by the plugins (each setting its own under plugins[name]). - exports.register = function (server, options, next) { - console.log(server.realm.modifiers.route.prefix); - return next(); - };*/ - realm: IServerRealm; - - /** server.root - The root server object containing all the connections and the root server methods (e.g. start(), stop(), connection()).*/ - root: Server; - /** server.settings - The server configuration object after defaults applied. - var Hapi = require('hapi'); - var server = new Hapi.Server({ - app: { - key: 'value' - } - }); - // server.settings.app === { key: 'value' }*/ - settings: IServerOptions; - - /** server.version - The hapi module version number. - var Hapi = require('hapi'); - var server = new Hapi.Server(); - // server.version === '8.0.0'*/ - version: string; - - /** server.after(method, [dependencies]) - Adds a method to be called after all the plugin dependencies have been registered and before the server starts (only called if the server is started) where: - after - the method with signature function(plugin, next) where: - server - server object the after() method was called on. - next - the callback function the method must call to return control over to the application and complete the registration process. The function signature is function(err) where: - err - internal error which is returned back via the server.start() callback. - dependencies - a string or array of string with the plugin names to call this method after their after() methods. There is no requirement for the other plugins to be registered. Setting dependencies only arranges the after methods in the specified order. - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - server.after(function () { - // Perform some pre-start logic - }); - server.start(function (err) { - // After method already executed - }); - server.auth.default(options)*/ - after(method: (plugin: any, next: (err: any) => void) => void, dependencies: string | string[]): void; - - auth: { - /** server.auth.api - An object where each key is a strategy name and the value is the exposed strategy API. Available on when the authentication scheme exposes an API by returning an api key in the object returned from its implementation function. - When the server contains more than one connection, each server.connections array member provides its own connection.auth.api object. - const server = new Hapi.Server(); - server.connection({ port: 80 }); - const scheme = function (server, options) { - return { - api: { - settings: { - x: 5 - } - }, - authenticate: function (request, reply) { - const req = request.raw.req; - const authorization = req.headers.authorization; - if (!authorization) { - return reply(Boom.unauthorized(null, 'Custom')); - } - return reply.continue({ credentials: { user: 'john' } }); - } - }; - }; - server.auth.scheme('custom', scheme); - server.auth.strategy('default', 'custom'); - console.log(server.auth.api.default.settings.x); // 5 - */ - api: { - [index: string]: any; - } - /** server.auth.default(options) - Sets a default strategy which is applied to every route where: - options - a string with the default strategy name or an object with a specified strategy or strategies using the same format as the route auth handler options. - The default does not apply when the route config specifies auth as false, or has an authentication strategy configured. Otherwise, the route authentication config is applied to the defaults. Note that the default only applies at time of route configuration, not at runtime. Calling default() after adding a route will have no impact on routes added prior. - The default auth strategy configuration can be accessed via connection.auth.settings.default. - var server = new Hapi.Server(); - server.connection({ port: 80 }); - server.auth.scheme('custom', scheme); - server.auth.strategy('default', 'custom'); - server.auth.default('default'); - server.route({ - method: 'GET', - path: '/', - handler: function (request, reply) { - return reply(request.auth.credentials.user); - } - });*/ - default(options: string): void; - default(options: { strategy: string }): void; - default(options: { strategies: string[] }): void; - /** server.auth.scheme(name, scheme) - Registers an authentication scheme where: - name - the scheme name. - scheme - the method implementing the scheme with signature function(server, options) where: - server - a reference to the server object the scheme is added to. - options - optional scheme settings used to instantiate a strategy.*/ - scheme( - name: string, - /** - * When the scheme authenticate() method implementation calls reply() with an error condition, the specifics of the error affect whether additional authentication strategies will be attempted if configured for the route. If the err returned by the reply() method includes a message, no additional strategies will be attempted. If the err does not include a message but does include a scheme name (e.g. Boom.unauthorized(null, 'Custom')), additional strategies will be attempted in order of preference. - * n the scheme payload() method returns an error with a message, it means payload validation failed due to bad payload. If the error has no message but includes a scheme name (e.g. Boom.unauthorized(null, 'Custom')), authentication may still be successful if the route auth.payload configuration is set to 'optional'. - * server = new Hapi.Server(); - * server.connection({ port: 80 }); - * scheme = function (server, options) { - * urn { - * authenticate: function (request, reply) { - * req = request.raw.req; - * var authorization = req.headers.authorization; - * if (!authorization) { - * return reply(Boom.unauthorized(null, 'Custom')); - * } - * urn reply(null, { credentials: { user: 'john' } }); - * } - * }; - * }; - */ - scheme: (server: Server, options: any) => IServerAuthScheme): void; - - /** - * server.auth.strategy(name, scheme, [mode], [options]) - * Registers an authentication strategy where: - * name - the strategy name. - * scheme - the scheme name (must be previously registered using server.auth.scheme()). - * mode - if true, the scheme is automatically assigned as a required strategy to any route without an auth config. Can only be assigned to a single server strategy. Value must be true (which is the same as 'required') or a valid authentication mode ('required', 'optional', 'try'). Defaults to false. - * options - scheme options based on the scheme requirements. - * var server = new Hapi.Server(); - * server.connection({ port: 80 }); - * server.auth.scheme('custom', scheme); - * server.auth.strategy('default', 'custom'); - * server.route({ - * method: 'GET', - * path: '/', - * config: { - * auth: 'default', - * handler: function (request, reply) { - * return reply(request.auth.credentials.user); - * } - * } - * }); - */ - strategy(name: string, scheme: string, mode?: boolean | string, options?: any): void; - strategy(name: string, scheme: string, mode?: boolean | string): void; - strategy(name: string, scheme: string, options?: any): void; - - /** server.auth.test(strategy, request, next) - Tests a request against an authentication strategy where: - strategy - the strategy name registered with server.auth.strategy(). - request - the request object. - next - the callback function with signature function(err, credentials) where: - err - the error if authentication failed. - credentials - the authentication credentials object if authentication was successful. - Note that the test() method does not take into account the route authentication configuration. It also does not perform payload authentication. It is limited to the basic strategy authentication execution. It does not include verifying scope, entity, or other route properties. - var server = new Hapi.Server(); - server.connection({ port: 80 }); - server.auth.scheme('custom', scheme); - server.auth.strategy('default', 'custom'); - server.route({ - method: 'GET', - path: '/', - handler: function (request, reply) { - request.server.auth.test('default', request, function (err, credentials) { - if (err) { - return reply({ status: false }); - } - return reply({ status: true, user: credentials.name }); - }); - } - });*/ - test(strategy: string, request: Request, next: (err: any, credentials: any) => void): void; - }; - - /** server.bind(context) - Sets a global context used as the default bind object when adding a route or an extension where: - context - the object used to bind this in handler and extension methods. - When setting context inside a plugin, the context is applied only to methods set up by the plugin. Note that the context applies only to routes and extensions added after it has been set. - var handler = function (request, reply) { - return reply(this.message); - }; - exports.register = function (server, options, next) { - var bind = { - message: 'hello' - }; - server.bind(bind); - server.route({ method: 'GET', path: '/', handler: handler }); - return next(); - };*/ - bind(context: any): void; - - /** server.cache(options) - Provisions a cache segment within the server cache facility where: - options - catbox policy configuration where: - expiresIn - relative expiration expressed in the number of milliseconds since the item was saved in the cache. Cannot be used together with expiresAt. - 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. - 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 function(id, next) where: - id - the id string or object provided to the get() method. - next - the method called when the new item is returned with the signature function(err, value, ttl) where: - err - an error condition. - value - the new value generated. - ttl - the cache ttl value in milliseconds. Set to 0 to skip storing in the cache. Defaults to the cache global policy. - 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. - staleTimeout - number of milliseconds to wait before checking if an item is stale. - 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 is stored in the cache for future requests. - cache - the cache name configured in 'server.cache`. Defaults to the default cache. - 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. Required when called outside of a plugin. - shared - if true, allows multiple cache provisions to share the same segment. Default to false. - var server = new Hapi.Server(); - server.connection({ port: 80 }); - var cache = server.cache({ segment: 'countries', expiresIn: 60 * 60 * 1000 }); - cache.set('norway', { capital: 'oslo' }, null, function (err) { - cache.get('norway', function (err, value, cached, log) { - // value === { capital: 'oslo' }; - }); - });*/ - cache(options: ICatBoxCachePolicyOptions): void; - - /** server.connection([options]) - Adds an incoming server connection - Returns a server object with the new connection selected. - Must be called before any other server method that modifies connections is called for it to apply to the new connection (e.g. server.state()). - Note that the options object is deeply cloned (with the exception of listener which is shallowly copied) and cannot contain any values that are unsafe to perform deep copy on. - var Hapi = require('hapi'); - var server = new Hapi.Server(); - var web = server.connection({ port: 8000, host: 'example.com', labels: ['web'] }); - var admin = server.connection({ port: 8001, host: 'example.com', labels: ['admin'] }); - // server.connections.length === 2 - // web.connections.length === 1 - // admin.connections.length === 1 */ - connection(options: IServerConnectionOptions): Server; - - /** server.decorate(type, property, method, [options]) - Extends various framework interfaces with custom methods where: - type - the interface being decorated. Supported types: - 'reply' - adds methods to the reply interface. - 'server' - adds methods to the Server object. - property - the object decoration key name. - method - the extension function. - options - if the type is 'request', supports the following optional settings: - 'apply' - if true, the method function is invoked using the signature function(request) where request is the current request object and the returned value is assigned as the decoration. - Note that decorations apply to the entire server and all its connections regardless of current selection. - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - server.decorate('reply', 'success', function () { - return this.response({ status: 'ok' }); - }); - server.route({ - method: 'GET', - path: '/', - handler: function (request, reply) { - return reply.success(); - } - });*/ - decorate(type: string, property: string, method: Function, options?: { apply: boolean }): void; - - /** server.dependency(dependencies, [after]) - Used within a plugin to declares a required dependency on other plugins where: - dependencies - a single string or array of plugin name strings which must be registered in order for this plugin to operate. Plugins listed must be registered before the server is started. Does not provide version dependency which should be implemented using npm peer dependencies. - after - an optional function called after all the specified dependencies have been registered and before the server starts. The function is only called if the server is started. If a circular dependency is detected, an exception is thrown (e.g. two plugins each has an after function to be called after the other). The function signature is function(server, next) where: - server - the server the dependency() method was called on. - next - the callback function the method must call to return control over to the application and complete the registration process. The function signature is function(err) where: - err - internal error condition, which is returned back via the server.start() callback. - exports.register = function (server, options, next) { - server.dependency('yar', after); - return next(); - }; - var after = function (server, next) { - // Additional plugin registration logic - return next(); - };*/ - dependency(dependencies: string | string[], after?: (server: Server, next: (err: any) => void) => void): void; - - /** server.expose(key, value) - Used within a plugin to expose a property via server.plugins[name] where: - key - the key assigned (server.plugins[name][key]). - value - the value assigned. - exports.register = function (server, options, next) { - server.expose('util', function () { console.log('something'); }); - return next(); - };*/ - expose(key: string, value: any): void; - - /** server.expose(obj) - Merges a deep copy of an object into to the existing content of server.plugins[name] where: - obj - the object merged into the exposed properties container. - exports.register = function (server, options, next) { - server.expose({ util: function () { console.log('something'); } }); - return next(); - };*/ - expose(obj: any): void; - - /** server.ext(event, method, [options]) - Registers an extension function in one of the available extension points where: - event - the event name. - method - a function or an array of functions to be executed at a specified point during request processing. The required extension function signature is function(request, reply) where: - request - the request object. NOTE: Access the Response via request.response - reply - the reply interface which is used to return control back to the framework. To continue normal execution of the request lifecycle, reply.continue() must be called. To abort processing and return a response to the client, call reply(value) where value is an error or any other valid response. - this - the object provided via options.bind or the current active context set with server.bind(). - options - an optional object with the following: - before - a string or array of strings of plugin names this method must execute before (on the same event). Otherwise, extension methods are executed in the order added. - after - a string or array of strings of plugin names this method must execute after (on the same event). Otherwise, extension methods are executed in the order added. - bind - a context object passed back to the provided method (via this) when called. - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - server.ext('onRequest', function (request, reply) { - // Change all requests to '/test' - request.setUrl('/test'); - return reply.continue(); - }); - var handler = function (request, reply) { - return reply({ status: 'ok' }); - }; - server.route({ method: 'GET', path: '/test', handler: handler }); - server.start(); - // All requests will get routed to '/test'*/ - ext(event: RequestExtPoints, method: (request: Request, reply: IReply, bind?: any) => void, options?: { before: string | string[]; after: string | string[]; bind?: any }): void; - ext(event: RequestExtPoints, method: (request: Request, reply: IStrictReply, bind?: any) => void, options?: { before: string | string[]; after: string | string[]; bind?: any }): void; - ext(event: ServerExtPoints, method: (server: Server, next: (err?: any) => void, bind?: any) => void, options?: { before: string | string[]; after: string | string[]; bind?: any }): void; - - /** server.handler(name, method) - Registers a new handler type to be used in routes where: - name - string name for the handler being registered. Cannot override the built-in handler types (directory, file, proxy, and view) or any previously registered type. - method - the function used to generate the route handler using the signature function(route, options) where: - route - the route public interface object. - options - the configuration object provided in the handler config. - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ host: 'localhost', port: 8000 }); - // Defines new handler for routes on this server - server.handler('test', function (route, options) { - return function (request, reply) { - return reply('new handler: ' + options.msg); - } - }); - server.route({ - method: 'GET', - path: '/', - handler: { test: { msg: 'test' } } - }); - server.start(); - The method function can have a defaults object or function property. If the property is set to an object, that object is used as the default route config for routes using this handler. If the property is set to a function, the function uses the signature function(method) and returns the route default configuration. - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ host: 'localhost', port: 8000 }); - var handler = function (route, options) { - return function (request, reply) { - return reply('new handler: ' + options.msg); - } - }; - // Change the default payload processing for this handler - handler.defaults = { - payload: { - output: 'stream', - parse: false - } - }; - server.handler('test', handler);*/ - handler(name: string, method: (route: IRoute, options: THandlerConfig) => ISessionHandler): void; - - /** server.initialize([callback]) - Initializes the server (starts the caches, finalizes plugin registration) but does not start listening - on the connection ports, where: - - `callback` - the callback method when server initialization is completed or failed with the signature - `function(err)` where: - - `err` - any initialization error condition. - - If no `callback` is provided, a `Promise` object is returned. - - Note that if the method fails and the callback includes an error, the server is considered to be in - an undefined state and should be shut down. In most cases it would be impossible to fully recover as - the various plugins, caches, and other event listeners will get confused by repeated attempts to - start the server or make assumptions about the healthy state of the environment. It is recommended - to assert that no error has been returned after calling `initialize()` to abort the process when the - server fails to start properly. If you must try to resume after an error, call `server.stop()` - first to reset the server state. - */ - initialize(callback?: (error: any) => void): Promise; - - /** When the server contains exactly one connection, injects a request into the sole connection simulating an incoming HTTP request without making an actual socket connection. - Injection is useful for testing purposes as well as for invoking routing logic internally without the overhead or limitations of the network stack. - Utilizes the [shot module | https://github.com/hapijs/shot ] for performing injections, with some additional options and response properties - * When the server contains more than one connection, each server.connections array member provides its own connection.inject(). - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - var handler = function (request, reply) { - return reply('Success!'); - }; - server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { - console.log(res.result); - }); - */ - inject: IServerInject; - - /** server.log(tags, [data, [timestamp]]) - Logs server events that cannot be associated with a specific request. When called the server emits a 'log' event which can be used by other listeners or plugins to record the information or output to the console. The arguments are: - tags - a string or an array of strings (e.g. ['error', 'database', 'read']) used to identify the event. Tags are used instead of log levels and provide a much more expressive mechanism for describing and filtering events. Any logs generated by the server internally include the 'hapi' tag along with event-specific information. - data - an optional message string or object with the application data being logged. - timestamp - an optional timestamp expressed in milliseconds. Defaults to Date.now() (now). - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - server.on('log', function (event, tags) { - if (tags.error) { - console.log(event); - } - }); - server.log(['test', 'error'], 'Test event');*/ - log(tags: string | string[], data?: string | any, timestamp?: number): void; - - /**server.lookup(id) - When the server contains exactly one connection, looks up a route configuration where: - id - the route identifier as set in the route options. - returns the route public interface object if found, otherwise null. - var server = new Hapi.Server(); - server.connection(); - server.route({ - method: 'GET', - path: '/', - config: { - handler: function (request, reply) { return reply(); }, - id: 'root' - } - }); - var route = server.lookup('root'); - When the server contains more than one connection, each server.connections array member provides its own connection.lookup() method.*/ - lookup(id: string): IRoute; - - /** server.match(method, path, [host]) - When the server contains exactly one connection, looks up a route configuration where: - method - the HTTP method (e.g. 'GET', 'POST'). - path - the requested path (must begin with '/'). - host - optional hostname (to match against routes with vhost). - returns the route public interface object if found, otherwise null. - var server = new Hapi.Server(); - server.connection(); - server.route({ - method: 'GET', - path: '/', - config: { - handler: function (request, reply) { return reply(); }, - id: 'root' - } - }); - var route = server.match('get', '/'); - When the server contains more than one connection, each server.connections array member provides its own connection.match() method.*/ - match(method: string, path: string, host?: string): IRoute; - - /** server.method(name, method, [options]) - Registers a server method. Server methods are functions registered with the server and used throughout the application as a common utility. Their advantage is in the ability to configure them to use the built-in cache and share across multiple request handlers without having to create a common module. - Methods are registered via server.method(name, method, [options]) - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - // Simple arguments - var add = function (a, b, next) { - return next(null, a + b); - }; - server.method('sum', add, { cache: { expiresIn: 2000 } }); - server.methods.sum(4, 5, function (err, result) { - console.log(result); - }); - // Object argument - var addArray = function (array, next) { - var sum = 0; - array.forEach(function (item) { - sum += item; - }); - return next(null, sum); - }; - server.method('sumObj', addArray, { - cache: { expiresIn: 2000 }, - generateKey: function (array) { - return array.join(','); - } - }); - server.methods.sumObj([5, 6], function (err, result) { - console.log(result); - }); - // Synchronous method with cache - var addSync = function (a, b) { - return a + b; - }; - server.method('sumSync', addSync, { cache: { expiresIn: 2000 }, callback: false }); - server.methods.sumSync(4, 5, function (err, result) { - console.log(result); - }); */ - method(/** a unique method name used to invoke the method via server.methods[name]. When configured with caching enabled, server.methods[name].cache.drop(arg1, arg2, ..., argn, callback) can be used to clear the cache for a given key. Supports using nested names such as utils.users.get which will automatically create the missing path under server.methods and can be accessed for the previous example via server.methods.utils.users.get.*/ - name: string, - method: IServerMethod, - options?: IServerMethodOptions): void; - - /**server.method(methods) - Registers a server method function as described in server.method() using a configuration object where: - methods - an object or an array of objects where each one contains: - name - the method name. - method - the method function. - options - optional settings. - var add = function (a, b, next) { - next(null, a + b); - }; - server.method({ - name: 'sum', - method: add, - options: { - cache: { - expiresIn: 2000 - } - } - });*/ - method(methods: { - name: string; method: IServerMethod; options?: IServerMethodOptions - } | Array<{ - name: string; method: IServerMethod; options?: IServerMethodOptions - }>): void; - - /**server.path(relativeTo) - Sets the path prefix used to locate static resources (files and view templates) when relative paths are used where: - relativeTo - the path prefix added to any relative file path starting with '.'. - Note that setting a path within a plugin only applies to resources accessed by plugin methods. If no path is set, the connection files.relativeTo configuration is used. The path only applies to routes added after it has been set. - exports.register = function (server, options, next) { - server.path(__dirname + '../static'); - server.route({ path: '/file', method: 'GET', handler: { file: './test.html' } }); - next(); - };*/ - path(relativeTo: string): void; - - /** - * server.register(plugins, [options], callback) - * Registers a plugin where: - * plugins - an object or array of objects where each one is either: - * a plugin registration function. - * an object with the following: - * register - the plugin registration function. - * options - optional options passed to the registration function when called. - * options - optional registration options (different from the options passed to the registration function): - * select - a string or array of string labels used to pre-select connections for plugin registration. - * routes - modifiers applied to each route added by the plugin: - * prefix - string added as prefix to any route path (must begin with '/'). If a plugin registers a child plugin the prefix is passed on to the child or is added in front of the child-specific prefix. - * vhost - virtual host string (or array of strings) applied to every route. The outer-most vhost overrides the any nested configuration. - * callback - the callback function with signature function(err) where: - * err - an error returned from the registration function. Note that exceptions thrown by the registration function are not handled by the framework. - * - * If no callback is provided, a Promise object is returned. - */ - register( - plugins: any | any[], - options: { - select?: string | string[]; - routes: { - prefix: string; vhost?: string | string[] - }; - }, - callback: (err: any) => void): void; - register( - plugins: any | any[], - options: { - select?: string | string[]; - routes: { - prefix: string; vhost?: string | string[] - }; - }): Promise; - - register(plugins: any | any[], callback: (err: any) => void): void; - register(plugins: any | any[]): Promise; - - /**server.render(template, context, [options], callback) - Utilizes the server views manager to render a template where: - template - the template filename and path, relative to the views manager templates path (path or relativeTo). - context - optional object used by the template to render context-specific result. Defaults to no context ({}). - options - optional object used to override the views manager configuration. - callback - the callback function with signature function (err, rendered, config) where: - err - the rendering error if any. - rendered - the result view string. - config - the configuration used to render the template. - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - server.views({ - engines: { html: require('handlebars') }, - path: __dirname + '/templates' - }); - var context = { - title: 'Views Example', - message: 'Hello, World' - }; - server.render('hello', context, function (err, rendered, config) { - console.log(rendered); - });*/ - render(template: string, context: any, options: any, callback: (err: any, rendered: any, config: any) => void): void; - - /** server.route(options) - Adds a connection route where: - options - a route configuration object or an array of configuration objects. - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - server.route({ method: 'GET', path: '/', handler: function (request, reply) { return reply('ok'); } }); - server.route([ - { method: 'GET', path: '/1', handler: function (request, reply) { return reply('ok'); } }, - { method: 'GET', path: '/2', handler: function (request, reply) { return reply('ok'); } } - ]);*/ - route(options: IRouteConfiguration): void; - route(options: IRouteConfiguration[]): void; - - /**server.select(labels) - Selects a subset of the server's connections where: - labels - a single string or array of strings of labels used as a logical OR statement to select all the connections with matching labels in their configuration. - Returns a server object with connections set to the requested subset. Selecting again on a selection operates as a logic AND statement between the individual selections. - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80, labels: ['a'] }); - server.connection({ port: 8080, labels: ['b'] }); - server.connection({ port: 8081, labels: ['c'] }); - server.connection({ port: 8082, labels: ['c','d'] }); - var a = server.select('a'); // The server with port 80 - var ab = server.select(['a','b']); // A list of servers containing the server with port 80 and the server with port 8080 - var c = server.select('c'); // A list of servers containing the server with port 8081 and the server with port 8082 */ - select(labels: string | string[]): Server | Server[]; - - /** server.start([callback]) - Starts the server connections by listening for incoming requests on the configured port of each listener (unless the connection was configured with autoListen set to false), where: - callback - optional callback when server startup is completed or failed with the signature function(err) where: - err - any startup error condition. - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - server.start(function (err) { - console.log('Server started at: ' + server.info.uri); - });*/ - start(callback?: (err: any) => void): Promise; - - /** server.state(name, [options]) - HTTP state management uses client cookies to persist a state across multiple requests. Registers a cookie definitions - State defaults can be modified via the server connections.routes.state configuration option. - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - // Set cookie definition - server.state('session', { - ttl: 24 * 60 * 60 * 1000, // One day - isSecure: true, - path: '/', - encoding: 'base64json' - }); - // Set state in route handler - var handler = function (request, reply) { - var session = request.state.session; - if (!session) { - session = { user: 'joe' }; - } - session.last = Date.now(); - return reply('Success').state('session', session); - }; - Registered cookies are automatically parsed when received. Parsing rules depends on the route state.parse configuration. If an incoming registered cookie fails parsing, it is not included in request.state, regardless of the state.failAction setting. When state.failAction is set to 'log' and an invalid cookie value is received, the server will emit a 'request-internal' event. To capture these errors subscribe to the 'request-internal' events and filter on 'error' and 'state' tags: - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - server.on('request-internal', function (request, event, tags) { - if (tags.error && tags.state) { - console.error(event); - } - }); */ - state(name: string, options?: ICookieSettings): void; - - /** server.stop([options], [callback]) - Stops the server's connections by refusing to accept any new connections or requests (existing connections will continue until closed or timeout), where: - options - optional object with: - timeout - overrides the timeout in millisecond before forcefully terminating a connection. Defaults to 5000 (5 seconds). - callback - optional callback method with signature function() which is called once all the connections have ended and it is safe to exit the process. - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80 }); - server.stop({ timeout: 60 * 1000 }, function () { - console.log('Server stopped'); - });*/ - stop(options?: { timeout: number }, callback?: () => void): Promise; - - /**server.table([host]) - Returns a copy of the routing table where: - host - optional host to filter routes matching a specific virtual host. Defaults to all virtual hosts. - The return value is an array where each item is an object containing: - info - the connection.info the connection the table was generated for. - labels - the connection labels. - table - an array of routes where each route contains: - settings - the route config with defaults applied. - method - the HTTP method in lower case. - path - the route path. - Note that if the server has not been started and multiple connections use port 0, the table items will override each other and will produce an incomplete result. - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80, host: 'example.com' }); - server.route({ method: 'GET', path: '/example', handler: function (request, reply) { return reply(); } }); - var table = server.table(); - When calling connection.table() directly on each connection, the return value is the same as the array table item value of an individual connection: - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.connection({ port: 80, host: 'example.com' }); - server.route({ method: 'GET', path: '/example', handler: function (request, reply) { return reply(); } }); - var table = server.connections[0].table(); - //[ - // { - // method: 'get', - // path: '/example', - // settings: { ... } - // } - //] - */ - table(host?: any): IConnectionTable; - - /**server.views(options) - Initializes the server views manager - var Hapi = require('hapi'); - var server = new Hapi.Server(); - server.views({ - engines: { - html: require('handlebars'), - jade: require('jade') - }, - path: '/static/templates' - }); - When server.views() is called within a plugin, the views manager is only available to plugins methods.*/ - views(options: IServerViewsConfiguration): void; +/** + * request extension points: function(request, reply) where + * this - the object provided via options.bind or the current active context set with server.bind(). + * [See docs](https://hapijs.com/api/16.1.1#serverextevents) + * @param request the request object. + * @param reply the reply interface which is used to return control back to the framework. To continue normal execution of the request lifecycle, reply.continue() must be called. If the extension type is 'onPostHandler' or 'onPreResponse', a single argument passed to reply.continue() will override the current set response (including all headers) but will not stop the request lifecycle execution. To abort processing and return a response to the client, call reply(value) where value is an error or any other valid response. + */ +export interface ServerExtRequestHandler { + (request: Request, reply: ReplyWithContinue): void; +} + +/** + * Used by various extensions to handle a request and + * synchronously return a result of some form. + * + * Left in for backwards compatibility of typings but according to the + * [DefinitelyTyped Readme under common mistakes](https://github.com/DefinitelyTyped/DefinitelyTyped#common-mistakes) + * it talks about not using generic types unless the type was used in typing one + * or more of the function arguments. Using it to type the return was suggested + * to be the same as a type assertion. + */ +export interface RequestHandler { + (request: Request): T; +} + +/** + * Used by server extension points + * err can be BoomError or Error that will be wrapped as a BoomError + * For source [See docs](https://github.com/hapijs/hapi/blob/v16.1.1/lib/reply.js#L109-L118) + * For source [See docs](https://github.com/hapijs/hapi/blob/v16.1.1/lib/response.js#L60-L65) + */ +export interface ContinuationFunction { + (err: Boom.BoomError): void; +} +/** + * For source [See docs](https://github.com/hapijs/hapi/blob/v16.1.1/lib/response.js#L60-L65) + * TODO Can value be typed with a useful generic? + */ +export interface ContinuationValueFunction { + (err: Boom.BoomError, value: any): void; +} + +/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Reply functions + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */ +// TODO: move to separate file http://stackoverflow.com/questions/43276921 + +/** + * Typings listed explicitly here [See docs](https://hapijs.com/api/16.1.1#replyerr-result) + * Typings also described in part here [See docs](https://hapijs.com/api/16.1.1#response-object) + */ +export type ReplyValue = _ReplyValue | Promise<_ReplyValue>; +export type _ReplyValue = null | undefined | string | number | boolean | Buffer | Error | stream.Stream | Object; // | array; + +/** + * Reply interface + * reply([err], [result]) + * Concludes the handler activity by setting a response and returning control over to the framework + * When reply() is called with an error or result response, that value is used as the response sent to the client. When reply() is called within a prerequisite, the value is saved for future use and is not used as the response. In all other places except for the handler, calling reply() will be considered an error and will abort the request lifecycle, jumping directly to the 'onPreResponse' event. + * To return control to the framework within an extension or other places other than the handler, without setting a response, the method reply.continue() must be called. Except when used within an authentication strategy, or in an 'onPostHandler' or 'onPreResponse' extension, the reply.continue() must not be passed any argument or an exception is thrown. + * [See docs](https://hapijs.com/api/16.1.1#reply-interface) + * [See docs](https://hapijs.com/api/16.1.1#replyerr-result) + * + * NOTE: modules should extend this interface to expose reply.Nnn methods + */ +export interface Base_Reply { + (err?: ReplyValue): Response; + (err: null, result?: ReplyValue): Response; + /** the active realm associated with the route. */ + realm: ServerRealm; + /** the request object */ + request: Request; + + /** + * reply.entity(options) + * Sets the response 'ETag' and 'Last-Modified' headers and checks for any conditional request headers to decide if the response is going to qualify for an HTTP 304 (Not Modified). If the entity values match the request conditions, reply.entity() returns control back to the framework with a 304 response. Otherwise, it sets the provided entity headers and returns null. + * Returns a response object if the reply is unmodified or null if the response has changed. If null is returned, the developer must call reply() to continue execution. If the response is not null, the developer must not call reply(). + * [See docs](https://hapijs.com/api/16.1.1#replyentityoptions) + * @param options a required configuration object with: + * * etag - the ETag string. Required if modified is not present. Defaults to no header. + * * modified - the Last-Modified header value. Required if etag is not present. Defaults to no header. + * * vary - same as the response.etag() option. Defaults to true. + */ + entity(options: {etag?: string, modified?: string, vary?: boolean}): Response | null; + /** + * reply.close([options]) + * Concludes the handler activity by returning control over to the router and informing the router that a response has already been sent back directly via request.raw.res and that no further response action is needed. Supports the following optional options: + * The response flow control rules do not apply. + * [See docs](https://hapijs.com/api/16.1.1#replycloseoptions) + * @param options options object: + * * end - if false, the router will not call request.raw.res.end()) to ensure the response was ended. Defaults to true. + */ + close(options?: {end?: boolean}): void; + /** + * reply.redirect(uri) + * Redirects the client to the specified uri. Same as calling reply().redirect(uri). + * The response flow control rules apply. + * Sets an HTTP redirection response (302) and decorates the response with additional methods for + * changing to a permanent or non-rewritable redirect is also available see response object redirect for more information. + * [See docs](https://hapijs.com/api/16.1.1#replyredirecturi) + * @param uri an absolute or relative URI used to redirect the client to another resource. + */ + redirect(uri: string): ResponseRedirect; + /** + * reply.response(result) + * Shorthand for calling `reply(null, result)`, replies with the response set to `result`. + * [See docs](https://hapijs.com/api/16.1.1#replyresponseresult) + * TODO likely to change. Await approval of pull request to Hapi docs. + */ + response(result: ReplyValue): Response; + /** + * Sets a cookie on the response + * [See docs](https://hapijs.com/api/16.1.1#reply) + * TODO likely to change. Await approval of pull request to Hapi docs. + */ + state(name: string, value: any, options?: any): void; + /** + * Clears a cookie on the response + * [See docs](https://hapijs.com/api/16.1.1#reply) + * TODO likely to change. Await approval of pull request to Hapi docs. + */ + unstate(name: string, options?: any): void; + /** + * The server.decorate('reply', ...) method can modify this prototype/interface. + * Have disabled these typings as there is a better alternative, see example in: tests/server/decorate.ts + * [And discussion here](https://github.com/DefinitelyTyped/DefinitelyTyped/issues/14517#issuecomment-298891630) + */ + // [index: string]: any; +} +/** + * reply.continue([result]) + * Returns control back to the framework without ending the request lifecycle + * [See docs](https://hapijs.com/api/16.1.1#replycontinueresult) + * [See docs](https://hapijs.com/api/16.1.1#replyerr-result) "With the exception of the handler function, all other methods provide the reply.continue() method which instructs the framework to continue processing the request without setting a response." + * @param result if called in the handler, prerequisites, or extension points other than the 'onPreHandler' and 'onPreResponse', the result argument is not allowed and will throw an exception if present. If called within an authentication strategy, it sets the authenticated credentials. If called by the 'onPreHandler' or 'onPreResponse' extensions, the result argument overrides the current response including all headers, and returns control back to the framework to continue processing any remaining extensions. + */ +export interface Continue_Reply { + continue(result?: ReplyValue): Response | undefined; +} + +/** + * [See docs](https://hapijs.com/api/16.1.1#serverauthschemename-scheme) > authenticate. + * Also [See docs](https://hapijs.com/api/16.1.1#replyerr-result). + * TODO check it has Base_Reply methods and properties + */ +export interface ReplySchemeAuth extends Base_Reply { + /** + * This function is called if authentication failed. + * TODO, check type the `response` parameter. In https://hapijs.com/api/16.1.1#replyerr-result it is referred to as "null" but this seems to be for a third scenario where it is "used to return both an error and credentials in the authentication methods" then "reply() must be called with three arguments function(err, null, data)" + * @param err any authentication error. + * @param response any authentication response action such as redirection. Ignored if err is present, otherwise required. + * @param result an object containing: + * * credentials the authenticated credentials. + * * artifacts optional authentication artifacts. + */ + (err: Error | null, response: AnyAuthenticationResponseAction | null, result: AuthenticationResult): void; + /** + * is called if authentication succeeded + * @param result same object as result above. + */ + continue(result: AuthenticationResult): void; +} +/** + * Typing as any as it's not yet clear what type this argument takes. + * "any authentication response action such as redirection" is it equivalent to + * `ReplyValue` ? + * [See docs](https://hapijs.com/api/16.1.1#serverauthschemename-scheme) + * TODO research hapi source and type this. + */ +export type AnyAuthenticationResponseAction = any; +/** [See docs](https://hapijs.com/api/16.1.1#serverauthschemename-scheme) */ +export interface AuthenticationResult { + credentials?: AuthenticatedCredentials; + artefacts?: any; +} +export interface AuthenticatedCredentials { + // Disabled to allow typing within a project + // [index: string]: any; +} + +/** + * [See docs](https://hapijs.com/api/16.1.1#serverauthschemename-scheme) > payload + * TODO check it has Base_Reply methods and properties + */ +export interface ReplySchemeAuthOfPayload extends Base_Reply { + /** + * function called to authenticate the request payload where: + * @param err any authentication error. + * @param response any authentication response action such as redirection. Ignored if err is present, otherwise required. + */ + (err: Error | null, response: AnyAuthenticationResponseAction): void; + /** is called if payload authentication succeeded */ + continue(): void; +} + +/** + * [See docs](https://hapijs.com/api/16.1.1#serverauthschemename-scheme) > response + * TODO check it has Base_Reply methods and properties + */ +export interface ReplySchemeAuthDecorateResponse extends Base_Reply { + /** + * is called if an error occurred + * @param err any authentication error. + * @param response any authentication response to send instead of the current response. Ignored if err is present, otherwise required. + */ + (err?: Error, response?: ReplyValue): void; + /** is called if the operation succeeded. */ + continue(): void; +} + +export interface ReplyWithContinue extends Continue_Reply, Base_Reply {} + +export interface ReplyNoContinue extends Base_Reply {} + +// TODO assess use and usefulness of StrictReply + +// Concludes the handler activity by setting a response and returning control over to the framework where: +// erran optional error response. +// result an optional response payload. +// Since an request can only have one response regardless if it is an error or success, the reply() method can only result in a single response value. This means that passing both an err and result will only use the err. There is no requirement for either err or result to be (or not) an Error object. The framework will simply use the first argument if present, otherwise the second. The method supports two arguments to be compatible with the common callback pattern of error first. +// FLOW CONTROL: +// When calling reply(), the framework waits until process.nextTick() to continue processing the request and transmit the response. This enables making changes to the returned response object before the response is sent. This means the framework will resume as soon as the handler method exits. To suspend this behavior, the returned response object supports the following methods: hold(), send() +/** + * + */ +// export interface Reply { // extends ReplyMethods { +// (err: Error, +// result?: string | number | boolean | Buffer | stream.Stream | Promise | T, +// /** Note that when used to return both an error and credentials in the authentication methods, reply() must be called with three arguments function(err, null, data) where data is the additional authentication information. */ +// credentialData?: any): BoomError; +// /** Note that if result is a Stream with a statusCode property, that status code will be used as the default response code. */ +// (result: string | number | boolean | Buffer | stream.Stream | Promise | T): Response; +// } + +/** Concludes the handler activity by setting a response and returning control over to the framework where: + erran optional error response. + result an optional response payload. + Since an request can only have one response regardless if it is an error or success, the reply() method can only result in a single response value. This means that passing both an err and result will only use the err. There is no requirement for either err or result to be (or not) an Error object. The framework will simply use the first argument if present, otherwise the second. The method supports two arguments to be compatible with the common callback pattern of error first. + FLOW CONTROL: + When calling reply(), the framework waits until process.nextTick() to continue processing the request and transmit the response. This enables making changes to the returned response object before the response is sent. This means the framework will resume as soon as the handler method exits. To suspend this behavior, the returned response object supports the following methods: hold(), send() */ +// export interface StrictReply extends ReplyMethods { +// (err: Error, +// result?: Promise | T, +// /** Note that when used to return both an error and credentials in the authentication methods, reply() must be called with three arguments function(err, null, data) where data is the additional authentication information. */ +// credentialData?: any): BoomError; +// /** Note that if result is a Stream with a statusCode property, that status code will be used as the default response code. */ +// (result: Promise | T): Response; +// } + +/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Response + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */ +// TODO: move to separate file http://stackoverflow.com/questions/43276921 + +/** + * Response object + * [See docs](https://hapijs.com/api/16.1.1#response-object) + * + * TODO, check extending from Podium is correct. Extending because of "The response object supports the following events" [See docs](https://hapijs.com/api/16.1.1#response-events) + * * 'peek' - emitted for each chunk of data written back to the client connection. The event method signature is function(chunk, encoding). + * * 'finish' - emitted when the response finished writing but before the client response connection is ended. The event method signature is function (). + */ +export interface Response extends Podium { + /** the HTTP response status code. Defaults to 200 (except for errors). */ + statusCode: number; + /** an object containing the response headers where each key is a header field name. Note that this is an incomplete list of headers to be included with the response. Additional headers will be added once the response is prepared for transmission. */ + headers: Dictionary; + /** the value provided using the reply interface. */ + source: ReplyValue; + /** + * a string indicating the type of source with available values: + * * 'plain' - a plain response such as string, number, null, or simple object (e.g. not a Stream, Buffer, or view). + * * 'buffer' - a Buffer. + * * 'stream' - a Stream. + * * 'promise' - a Promise object. + */ + variety: 'plain' | 'buffer' | 'stream' | 'promise'; + /** application-specific state. Provides a safe place to store application data without potential conflicts with the framework. Should not be used by plugins which should use plugins[name]. */ + app: any; + /** plugin-specific state. Provides a place to store and pass request-level plugin data. The plugins is an object where each key is a plugin name and the value is the state. */ + plugins: PluginsStates; + /** response handling flags: */ + settings: { + /** the 'Content-Type' HTTP header 'charset' property. Defaults to 'utf-8'. */ + charset: string; + /** the string encoding scheme used to serial data into the HTTP payload when source is a string or marshals into a string. Defaults to 'utf8'. */ + encoding: string; + /** if true and source is a Stream, copies the statusCode and headers of the stream to the outbound response. Defaults to true. */ + passThrough: boolean; + /** options used for source value requiring stringification. Defaults to no replacer and no space padding. */ + stringify: Json.StringifyArguments; + /** if set, overrides the route cache expiration milliseconds value set in the route config. Defaults to no override. */ + ttl: number | null; + /** if true, a suffix will be automatically added to the 'ETag' header at transmission time (separated by a '-' character) when the HTTP 'Vary' header is present. */ + varyEtag: boolean; + }; + + /** + * The following attribute is present in one or more of the examples + * TODO update once Hapi docs describes explicitly + */ + isBoom?: boolean; + /** + * The following attribute is present in one or more of the examples + * TODO update once Hapi docs describes explicitly + */ + isMissing?: boolean; + /** + * The following attribute is present in one or more of the examples + * TODO update once Hapi docs describes explicitly + */ + output?: Boom.Output; + + /** + * sets the HTTP 'Content-Length' header (to avoid chunked transfer encoding) + * @param length the header value. Must match the actual payload size. + */ + bytes(length: number): Response; + /** + * sets the 'Content-Type' HTTP header 'charset' property + * @param charset the charset property value. + */ + charset(charset: string): Response; + /** + * sets the HTTP status code + * @param statusCode the HTTP status code (e.g. 200). + */ + code(statusCode: number): Response; + /** + * sets the HTTP status message + * @param httpMessage the HTTP status message (e.g. 'Ok' for status code 200). + */ + message(httpMessage: string): Response; + /** + * sets the HTTP status code to Created (201) and the HTTP 'Location' header + * @param uri an absolute or relative URI used as the 'Location' header value. + */ + created(uri: string): Response; + /** + * sets the string encoding scheme used to serial data into the HTTP payload + * @param encoding the encoding property value (see node Buffer encoding [See docs](https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings)). + * * 'ascii' - for 7-bit ASCII data only. This encoding is fast and will strip the high bit if set. + * * 'utf8' - Multibyte encoded Unicode characters. Many web pages and other document formats use UTF-8. + * * 'utf16le' - 2 or 4 bytes, little-endian encoded Unicode characters. Surrogate pairs (U+10000 to U+10FFFF) are supported. + * * 'ucs2' - Alias of 'utf16le'. + * * 'base64' - Base64 encoding. When creating a Buffer from a string, this encoding will also correctly accept "URL and Filename Safe Alphabet" as specified in RFC4648, Section 5. + * * 'latin1' - A way of encoding the Buffer into a one-byte encoded string (as defined by the IANA in RFC1345, page 63, to be the Latin-1 supplement block and C0/C1 control codes). + * * 'binary' - Alias for 'latin1'. + * * 'hex' - Encode each byte as two hexadecimal characters. + */ + encoding(encoding: 'ascii' | 'utf8' | 'utf16le' | 'ucs2' | 'base64' | 'latin1' | 'binary' | 'hex'): Response; + /** + * sets the representation entity tag + * @param tag the entity tag string without the double-quote. + * @param options options object + * * weak - if true, the tag will be prefixed with the 'W/' weak signifier. Weak tags will fail to match identical tags for the purpose of determining 304 response status. Defaults to false. + * * vary - if true and content encoding is set or applied to the response (e.g 'gzip' or 'deflate'), the encoding name will be automatically added to the tag at transmission time (separated by a '-' character). Ignored when weak is true. Defaults to true. + */ + etag(tag: string, options?: {weak: boolean, vary: boolean}): Response; + /** + * sets an HTTP header + * @param name the header name. + * @param value the header value. + */ + header(name: string, value: string, options?: ResponseHeaderOptionsObject): Response; + /** + * sets the HTTP 'Location' header + * @param uri an absolute or relative URI used as the 'Location' header value. + */ + location(uri: string): Response; + /** + * sets an HTTP redirection response (302) and decorates the response with additional methods listed below, + * @param uri an absolute or relative URI used to redirect the client to another resource. + */ + redirect(uri: string): Response; + /** + * sets the JSON.stringify() replacer argument + * @param method the replacer function or array. Defaults to none. + */ + replacer(method: Json.StringifyReplacer): Response; + /** + * sets the JSON.stringify() space argument + * @param count the number of spaces to indent nested object keys. Defaults to no indentation. + */ + spaces(count: Json.StringifySpace): Response; + /** + * sets an HTTP cookie + * @param name the cookie name. + * @param value the cookie value. If no encoding is defined, must be a string. + * @param options optional configuration. If the state was previously registered with the server using server.state(), the specified keys in options override those same keys in the server definition (but not others). + */ + state(name: string, value: string | Object | any[], options?: ServerStateCookieConfiguationObject): Response; + /** + * sets a string suffix when the response is process via JSON.stringify(). + */ + suffix(suffix: string): Response; + /** + * overrides the default route cache expiration rule for this response instance + * @param msec the time-to-live value in milliseconds. + */ + ttl(msec: number): Response; + /** + * sets the HTTP 'Content-Type' header + * @param mimeType is the mime type. Should only be used to override the built-in default for each response type. + */ + type(mimeType: string): Response; + /** + * clears the HTTP cookie by setting an expired value + * @param name the cookie name. + * @param options optional configuration for expiring cookie. If the state was previously registered with the server using server.state(), the specified keys in options override those same keys in the server definition (but not others). + */ + unstate(name: string, options?: ServerStateCookieConfiguationObject): Response; + /** + * adds the provided header to the list of inputs affected the response generation via the HTTP 'Vary' header + * @param header the HTTP request header name. + */ + vary(header: string): Response; + + /** + * Flow control - hold() + * When calling reply(), the framework waits until process.nextTick() to continue processing the request and transmit the response. This enables making changes to the returned response object before the response is sent. This means the framework will resume as soon as the handler method exits. To suspend this behavior, the returned response object supports the following methods: + * puts the response on hold until response.send() is called. Available only after reply() is called and until response.hold() is invoked once. + * [See docs](https://hapijs.com/api/16.1.1#flow-control) + */ + hold(): Response; + /** + * Flow control - send() + * When calling reply(), the framework waits until process.nextTick() to continue processing the request and transmit the response. This enables making changes to the returned response object before the response is sent. This means the framework will resume as soon as the handler method exits. To suspend this behavior, the returned response object supports the following methods: + * immediately resume the response. Available only after response.hold() is called and until response.send() is invoked once. + * [See docs](https://hapijs.com/api/16.1.1#flow-control) + */ + send(): Response; + + /** + * Mentioned here: "Note that prerequisites do not follow the same rules of the normal reply interface. In all other cases, calling reply() with or without a value will use the result as the response sent back to the client. In a prerequisite method, calling reply() will assign the returned value to the provided assign key. If the returned value is an error, the failAction setting determines the behavior. To force the return value as the response and skip any other prerequisites and the handler, use the reply().takeover() method." + * TODO prepare documentation PR and submit to hapi. + * [See docs](https://hapijs.com/api/16.1.1#route-prerequisites) + */ + takeover(): Response; +} + +/** + * Response Object Redirect Methods + * When using the redirect() method, the response object provides these additional methods: + * [See docs](https://hapijs.com/api/16.1.1#response-object-redirect-methods) + */ +export interface ResponseRedirect extends Response { + /** + * temporary + * sets the status code to 302 or 307 (based on the rewritable() setting) where: + * [See docs](https://hapijs.com/api/16.1.1#response-object-redirect-methods) + * @param isTemporary if false, sets status to permanent. Defaults to true. + */ + temporary(isTemporary: boolean): Response; + /** + * permanent + * sets the status code to 301 or 308 (based on the rewritable() setting) where: + * [See docs](https://hapijs.com/api/16.1.1#response-object-redirect-methods) + * @param isPermanent if false, sets status to temporary. Defaults to true. + */ + permanent(isPermanent: boolean): Response; + /** + * rewritable + * sets the status code to 301/302 for rewritable (allows changing the request method from 'POST' to 'GET') or 307/308 for non-rewritable (does not allow changing the request method from 'POST' to 'GET'). Exact code based on the temporary() or permanent() setting. Arguments: + * [See docs](https://hapijs.com/api/16.1.1#response-object-redirect-methods) + * @param isRewritable if false, sets to non-rewritable. Defaults to true. + */ + rewritable(isRewritable: boolean): Response; +} + +/** + * [See docs](https://hapijs.com/api/16.1.1#response-object) under "response object provides the following methods" > header > options + */ +export interface ResponseHeaderOptionsObject { + /** if true, the value is appended to any existing header value using separator. Defaults to false. */ + append?: boolean; + /** string used as separator when appending to an existing value. Defaults to ','. */ + separator?: string; + /** if false, the header value is not set if an existing value present. Defaults to true. */ + override?: boolean; + /** if false, the header value is not modified if the provided value is already included. Does not apply when append is false or if the name is 'set-cookie'. Defaults to true. */ + duplicate?: boolean; +} + +/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Plugins and register + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */ +// TODO: move to separate file http://stackoverflow.com/questions/43276921 + +/** + * Plugins + * Plugins provide a way to organize the application code by splitting the server logic into smaller components. Each plugin can manipulate the server and its connections through the standard server interface, but with the added ability to sandbox certain properties. + * [See docs](https://hapijs.com/api/16.1.1#plugins) + * @param server the server object the plugin is being registered to. + * @param options an options object passed to the plugin during registration. + * @param next a callback method the function must call to return control back to the framework to complete the registration process with signature function(err) + */ +export interface PluginFunction { + (server: Server, options: OptionsPassedToPlugin, next: (err?: Error) => void): void; + /** + * Note attributes is NOT optional but this type is easier to use. + */ + attributes?: PluginAttributes; +} + +/** + * see Plugin + * [See docs](https://hapijs.com/api/16.1.1#plugins) + */ +export interface PluginAttributes { + /** + * required plugin name string. The name is used as a unique key. Published plugins should use the same name as the name field in the 'package.json' file. Names must be unique within each application. + * NOTE: marked as optional as `pkg` can be used instead. + */ + name?: string; + /** optional plugin version. The version is only used informatively to enable other plugins to find out the versions loaded. The version should be the same as the one specified in the plugin's 'package.json' file. */ + version?: string; + /** Alternatively, the name and version can be included via the pkg attribute containing the 'package.json' file for the module which already has the name and version included */ + pkg?: any; + /** if true, allows the plugin to be registered multiple times with the same server. Defaults to false. */ + multiple?: boolean; + /** optional string or array of string indicating a plugin dependency. Same as setting dependencies via server.dependency(). */ + dependencies?: string | string[]; + /** if false, does not allow the plugin to call server APIs that modify the connections such as adding a route or configuring state. This flag allows the plugin to be registered before connections are added and to pass dependency requirements. When set to 'conditional', the mode is based on the presence of selected connections (if the server has connections, it is the same as true, but if no connections are available, it is the same as false). Defaults to true. */ + connections?: boolean | 'conditional'; + /** if true, will only register the plugin once per connection (or once per server for a connectionless plugin). If set, overrides the once option passed to server.register(). Defaults to undefined (registration will be based on the server.register() option once). */ + once?: boolean; +} + +/** + * Plugins State + * Related [See docs](https://hapijs.com/api/16.1.1#serverplugins) + * Related [See docs](https://hapijs.com/api/16.1.1#serverrealm) + */ +export interface PluginsStates { + [pluginName: string]: any; +} + +/** + * once, select, routes - optional plugin-specific registration options as defined see PluginRegistrationOptions + * [See docs](https://hapijs.com/api/16.1.1#serverregisterplugins-options-callback) + */ +export interface PluginRegistrationObject extends PluginRegistrationOptions { + /** the plugin registration function. */ + register: PluginFunction; + /** optional options passed to the registration function when called. */ + options?: OptionsPassedToPlugin; +} + +/** + * registration options (different from the options passed to the registration function): + * * once - if true, the registration is skipped for any connection already registered with. Cannot be used with plugin options. If the plugin does not have a connections attribute set to false and the registration selection is empty, registration will be skipped as no connections are available to register once. Defaults to false. + * * routes - modifiers applied to each route added by the plugin: + * * prefix - string added as prefix to any route path (must begin with '/'). If a plugin registers a child plugin the prefix is passed on to the child or is added in front of the child-specific prefix. + * * vhost - virtual host string (or array of strings) applied to every route. The outer-most vhost overrides the any nested configuration. + * * select - a string or array of string labels used to pre-select connections for plugin registration. + * [See docs](https://hapijs.com/api/16.1.1#serverregisterplugins-options-callback) + */ +export interface PluginRegistrationOptions { + once?: boolean; + routes?: {prefix?: string, vhost?: string | string[]}; + select?: string | string[]; +} + +/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + JSON + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */ +// This was in a seperate file and perhaps should move to some of the lib typings? +// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/16065#issuecomment-299443673 +// +// json/json-tests.ts +// +// import * as JSON from './index'; +// +// var a: JSON.StringifyReplacer = function(key, value) { +// if (key === "do not include") { +// return undefined; +// } +// return value; +// }; +// + +export namespace Json { + /** + * @see {@link https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter} + */ + export type StringifyReplacer = ((key: string, value: any) => any) | (string | number)[] | undefined; + + /** + * Any value greater than 10 is truncated. + */ + export type StringifySpace = number | string; + + export interface StringifyArguments { + /** the replacer function or array. Defaults to no action. */ + replacer?: StringifyReplacer; + /** number of spaces to indent nested object keys. Defaults to no indentation. */ + space?: StringifySpace; + } } diff --git a/types/hapi/test/connection/table.ts b/types/hapi/test/connection/table.ts new file mode 100644 index 0000000000..dee4741b07 --- /dev/null +++ b/types/hapi/test/connection/table.ts @@ -0,0 +1,20 @@ + +// From https://hapijs.com/api/16.1.1#servertablehost + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80, host: 'example.com' }); +server.route({ method: 'GET', path: '/example', handler: function (request, reply) { return reply(); } }); + +const connection = server.connections[0]; +const table = connection.table(); + +/* + [ + { + method: 'get', + path: '/example', + settings: { ... } + } + ] +*/ diff --git a/types/hapi/test/getting-started/01-creating-a-server.ts b/types/hapi/test/getting-started/01-creating-a-server.ts new file mode 100644 index 0000000000..819ab5108e --- /dev/null +++ b/types/hapi/test/getting-started/01-creating-a-server.ts @@ -0,0 +1,16 @@ +// From https://hapijs.com/tutorials/getting-started#creating-a-server + +'use strict'; + +import Hapi = require('hapi'); + +const server = new Hapi.Server(); +server.connection({ port: 3000, host: 'localhost' }); + +server.start((err) => { + + if (err) { + throw err; + } + console.log(`Server running at: ${server.info!.uri}`); +}); diff --git a/types/hapi/test/getting-started/02-adding-routes.ts b/types/hapi/test/getting-started/02-adding-routes.ts new file mode 100644 index 0000000000..db4805366c --- /dev/null +++ b/types/hapi/test/getting-started/02-adding-routes.ts @@ -0,0 +1,32 @@ +// from https://hapijs.com/tutorials/getting-started#adding-routes + +'use strict'; + +import Hapi = require('hapi'); + +const server = new Hapi.Server(); +server.connection({ port: 3000, host: 'localhost' }); + +server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + reply('Hello, world!'); + } +}); + +server.route({ + method: 'GET', + path: '/{name}', + handler: function (request, reply) { + reply('Hello, ' + encodeURIComponent(request.params.name) + '!'); + } +}); + +server.start((err) => { + + if (err) { + throw err; + } + console.log(`Server running at: ${server.info!.uri}`); +}); diff --git a/types/hapi/test/getting-started/03-serving-static-content.ts b/types/hapi/test/getting-started/03-serving-static-content.ts new file mode 100644 index 0000000000..06200f2b07 --- /dev/null +++ b/types/hapi/test/getting-started/03-serving-static-content.ts @@ -0,0 +1,23 @@ +'use strict'; + +import Hapi = require('hapi'); +import Inert = require('inert'); + +const server = new Hapi.Server(); + +// from https://hapijs.com/tutorials/getting-started#creating-static-pages-and-content + +server.register(Inert, (err) => { + + if (err) { + throw err; + } + + server.route({ + method: 'GET', + path: '/hello', + handler: function (request, reply) { + reply.file('./public/hello.html'); + } + }); +}); \ No newline at end of file diff --git a/types/hapi/test/getting-started/04-using-plugins.ts b/types/hapi/test/getting-started/04-using-plugins.ts new file mode 100644 index 0000000000..28d1a1b576 --- /dev/null +++ b/types/hapi/test/getting-started/04-using-plugins.ts @@ -0,0 +1,56 @@ +// from https://hapijs.com/tutorials/getting-started#using-plugins + +'use strict'; + +import Hapi = require('hapi'); +const Good = require('good'); + +const server = new Hapi.Server(); +server.connection({ port: 3000, host: 'localhost' }); + +server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + reply('Hello, world!'); + } +}); + +server.route({ + method: 'GET', + path: '/{name}', + handler: function (request, reply) { + reply('Hello, ' + encodeURIComponent(request.params.name) + '!'); + } +}); + +server.register({ + register: Good, + options: { + reporters: { + console: [{ + module: 'good-squeeze', + name: 'Squeeze', + args: [{ + response: '*', + log: '*' + }] + }, { + module: 'good-console' + }, 'stdout'] + } + } +}, (err) => { + + if (err) { + throw err; // something bad happened loading the plugin + } + + server.start((err) => { + + if (err) { + throw err; + } + server.log('info', 'Server running at: ' + server.info!.uri); + }); +}); diff --git a/types/hapi/test/path/catch-all.ts b/types/hapi/test/path/catch-all.ts new file mode 100644 index 0000000000..c9002392b1 --- /dev/null +++ b/types/hapi/test/path/catch-all.ts @@ -0,0 +1,15 @@ + +// From https://hapijs.com/api/16.1.1#catch-all-route + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +server.route({ + method: '*', + path: '/{p*}', + handler: function (request, reply) { + + return reply('The page was not found').code(404); + } +}); diff --git a/types/hapi/test/path/parameters.ts b/types/hapi/test/path/parameters.ts new file mode 100644 index 0000000000..62bbfcc638 --- /dev/null +++ b/types/hapi/test/path/parameters.ts @@ -0,0 +1,33 @@ + +// From https://hapijs.com/api/16.1.1#path-parameters + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +const getAlbum: Hapi.RouteHandler = function (request, reply) { + + return reply('You asked for ' + + (request.params.song ? request.params.song + ' from ' : '') + + request.params.album); +}; + +server.route({ + path: '/{album}/{song?}', + method: 'GET', + handler: getAlbum +}); + +// Example 2 + +const getPerson: Hapi.RouteHandler = function (request, reply) { + + const nameParts = request.params.name.split('/'); + return reply({ first: nameParts[0], last: nameParts[1] }); +}; + +server.route({ + path: '/person/{name*2}', // Matches '/person/john/doe' + method: 'GET', + handler: getPerson +}); diff --git a/types/hapi/test/plugins/options.ts b/types/hapi/test/plugins/options.ts new file mode 100644 index 0000000000..cd192f1996 --- /dev/null +++ b/types/hapi/test/plugins/options.ts @@ -0,0 +1,36 @@ + +// From https://hapijs.com/api/16.1.1#serverinfo + +import * as Hapi from 'hapi'; + +// added in addition to code from docs +interface PluginOptions { + quantity: number; +} + +// modified from docs +var registerFunction: Hapi.PluginFunction = function(server, options, next) { + + server.route({ + method: 'GET', + path: '/test', + handler: function (request, reply) { + + // modified from docs + return reply(`ok ${options.quantity}`); + } + }); + + return next(); +}; + +var attributes: Hapi.PluginAttributes = { + name: 'test', + version: '1.0.0' +}; + +var attributes: Hapi.PluginAttributes = { + pkg: {} // require('./package.json'), +}; + +registerFunction.attributes = attributes; diff --git a/types/hapi/test/reply/continue.ts b/types/hapi/test/reply/continue.ts new file mode 100644 index 0000000000..de6994fbd0 --- /dev/null +++ b/types/hapi/test/reply/continue.ts @@ -0,0 +1,15 @@ + +// From https://hapijs.com/api/16.1.1#replycontinueresult + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +const onRequest: Hapi.ServerExtRequestHandler = function (request, reply) { + + // Change all requests to '/test' + request.setUrl('/test'); + return reply.continue(); +}; + +server.ext('onRequest', onRequest); diff --git a/types/hapi/test/reply/entity.ts b/types/hapi/test/reply/entity.ts new file mode 100644 index 0000000000..cfcce6de25 --- /dev/null +++ b/types/hapi/test/reply/entity.ts @@ -0,0 +1,24 @@ + +// From https://hapijs.com/api/16.1.1#replyentityoptions + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +server.route({ + method: 'GET', + path: '/', + config: { + cache: { expiresIn: 5000 }, + handler: function (request, reply) { + + const response = reply.entity({ etag: 'abc' }); + if (response) { + response.header('X', 'y'); + return; + } + + return reply('ok'); + } + } +}); diff --git a/types/hapi/test/reply/redirect.ts b/types/hapi/test/reply/redirect.ts new file mode 100644 index 0000000000..5baf892ac1 --- /dev/null +++ b/types/hapi/test/reply/redirect.ts @@ -0,0 +1,8 @@ + +// From https://hapijs.com/api/16.1.1#replyredirecturi + +import * as Hapi from 'hapi'; +const handler: Hapi.RouteHandler = function (request, reply) { + + return reply.redirect('http://example.com'); +}; diff --git a/types/hapi/test/reply/reply.ts b/types/hapi/test/reply/reply.ts new file mode 100644 index 0000000000..31f3bff9b4 --- /dev/null +++ b/types/hapi/test/reply/reply.ts @@ -0,0 +1,22 @@ + +// From https://hapijs.com/api/16.1.1#replyerr-result + +import * as Hapi from 'hapi'; + +// verbose notation + +const handler: Hapi.RouteHandler = function (request, reply) { + + const response = reply('success'); + response.type('text/plain'); + response.header('X-Custom', 'some-value'); +}; + +// Chained notation + +const handler2: Hapi.RouteHandler = function (request, reply) { + + return reply('success') + .type('text/plain') + .header('X-Custom', 'some-value'); +}; \ No newline at end of file diff --git a/types/hapi/test/reply/state_cookie.ts b/types/hapi/test/reply/state_cookie.ts new file mode 100644 index 0000000000..bf94219311 --- /dev/null +++ b/types/hapi/test/reply/state_cookie.ts @@ -0,0 +1,33 @@ + +// from https://hapijs.com/tutorials/cookies?lang=en_US + +import * as Hapi from 'hapi'; + +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +server.state('data', { + ttl: null, + isSecure: true, + isHttpOnly: true, + encoding: 'base64json', + clearInvalid: false, // remove invalid cookies + strictHeader: true // don't allow violations of RFC 6265 +}); + +server.route({ + method: 'GET', + path: '/say-hello', + config: { + state: { + parse: true, // parse and store in request.state + failAction: 'error' // may also be 'ignore' or 'log' + } + }, + handler: function(request, reply) { + // TODO test this + reply('Hello').state('data', { firstVisit: false }); + } +}) + + diff --git a/types/hapi/test/request/event-types.ts b/types/hapi/test/request/event-types.ts new file mode 100644 index 0000000000..0946f0a0ed --- /dev/null +++ b/types/hapi/test/request/event-types.ts @@ -0,0 +1,30 @@ + +// From https://hapijs.com/api/16.1.1#requestsetmethodmethod + +import * as Hapi from 'hapi'; +const Crypto = require('crypto'); +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +const onRequest: Hapi.ServerExtRequestHandler = function (request, reply) { + + const hash = Crypto.createHash('sha1'); + request.on('peek', (chunk) => { + + hash.update(chunk); + }); + + request.once('finish', () => { + + console.log(hash.digest('hex')); + }); + + request.once('disconnect', () => { + + console.error('request aborted'); + }); + + return reply.continue(); +}; + +server.ext('onRequest', onRequest); diff --git a/types/hapi/test/request/generate-response.ts b/types/hapi/test/request/generate-response.ts new file mode 100644 index 0000000000..c30c3a7be5 --- /dev/null +++ b/types/hapi/test/request/generate-response.ts @@ -0,0 +1,21 @@ + +// From https://hapijs.com/api/16.1.1#requestgenerateresponsesource-options + +import * as Hapi from 'hapi'; + +// Added in addition to code from docs +function promiseMethod() { + return Promise.resolve(true); +} + +const handler: Hapi.ServerExtRequestHandler = function (request, reply) { + + const result = promiseMethod().then((thing: boolean) => { + + if (!thing) { + return request.generateResponse().code(214); + } + return thing; + }); + return reply(result); +}; diff --git a/types/hapi/test/request/get-log.ts b/types/hapi/test/request/get-log.ts new file mode 100644 index 0000000000..289bdd3282 --- /dev/null +++ b/types/hapi/test/request/get-log.ts @@ -0,0 +1,12 @@ + +// From https://hapijs.com/api/16.1.1#requestgetlogtags-internal + +import * as Hapi from 'hapi'; + +var request: Hapi.Request = {}; + +request.getLog(); +request.getLog('error'); +request.getLog(['error', 'auth']); +request.getLog(['error'], true); +request.getLog(false); diff --git a/types/hapi/test/request/log.ts b/types/hapi/test/request/log.ts new file mode 100644 index 0000000000..1c2da1f3b0 --- /dev/null +++ b/types/hapi/test/request/log.ts @@ -0,0 +1,19 @@ + +// From https://hapijs.com/api/16.1.1#requestlogtags-data-timestamp + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80, routes: { log: true, security: false } }); + +server.on('request', (request, event, tags) => { + + if (tags.error) { + console.log(event); + } +}); + +const handler: Hapi.RouteHandler = function (request, reply) { + + request.log(['test', 'error'], 'Test event'); + return reply(); +}; diff --git a/types/hapi/test/request/set-method.ts b/types/hapi/test/request/set-method.ts new file mode 100644 index 0000000000..7269e6255f --- /dev/null +++ b/types/hapi/test/request/set-method.ts @@ -0,0 +1,15 @@ + +// From https://hapijs.com/api/16.1.1#requestsetmethodmethod + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +const onRequest: Hapi.ServerExtRequestHandler = function (request, reply) { + + // Change all requests to 'GET' + request.setMethod('GET'); + return reply.continue(); +}; + +server.ext('onRequest', onRequest); diff --git a/types/hapi/test/request/set-url.ts b/types/hapi/test/request/set-url.ts new file mode 100644 index 0000000000..4add3c5a17 --- /dev/null +++ b/types/hapi/test/request/set-url.ts @@ -0,0 +1,32 @@ + +// From https://hapijs.com/api/16.1.1#requestseturlurl-striptrailingslash + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +var onRequest: Hapi.ServerExtRequestHandler = function (request, reply) { + + // Change all requests to '/test' + request.setUrl('/test'); + return reply.continue(); +}; + +server.ext('onRequest', onRequest); + +// Example 2 + +const Url = require('url'); +const Qs = require('qs'); + +onRequest = function (request, reply) { + + const uri = request.raw.req.url; + const parsed = Url.parse(uri, false); + parsed.query = Qs.parse(parsed.query); + request.setUrl(parsed); + + return reply.continue(); +}; + +server.ext('onRequest', onRequest); diff --git a/types/hapi/test/request/tail.ts b/types/hapi/test/request/tail.ts new file mode 100644 index 0000000000..9c68ccb8f2 --- /dev/null +++ b/types/hapi/test/request/tail.ts @@ -0,0 +1,26 @@ + +// From https://hapijs.com/api/16.1.1#requestsetmethodmethod + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +const get: Hapi.RouteHandler = function (request, reply) { + + const dbTail = request.tail('write to database'); + + var db: any; + db.save('key', 'value', () => { + + dbTail(); + }); + + return reply('Success!'); +}; + +server.route({ method: 'GET', path: '/', handler: get }); + +server.on('tail', (request) => { + + console.log('Request completed including db activity'); +}); diff --git a/types/hapi/test/response/error-representation.ts b/types/hapi/test/response/error-representation.ts new file mode 100644 index 0000000000..f0fb62eb30 --- /dev/null +++ b/types/hapi/test/response/error-representation.ts @@ -0,0 +1,33 @@ + +// From https://hapijs.com/api/16.1.1#error-transformation + +import * as Hapi from 'hapi'; +import Vision from 'vision'; +const server = new Hapi.Server(); +server.register(Vision, {}, (err) => { + server.views({ + engines: { + html: require('handlebars') + } + }); +}); +server.connection({ port: 80 }); + +const preResponse: Hapi.ServerExtRequestHandler = function (request, reply) { + + const response = request.response!; + if (!response.isBoom) { + return reply.continue(); + } + + // Replace error with friendly HTML + + const error = response; + const ctx = { + message: (error.output!.statusCode === 404 ? 'page not found' : 'something went wrong') + }; + + return reply.view('error', ctx); +}; + +server.ext('onPreResponse', preResponse); diff --git a/types/hapi/test/response/error.ts b/types/hapi/test/response/error.ts new file mode 100644 index 0000000000..0ef503df12 --- /dev/null +++ b/types/hapi/test/response/error.ts @@ -0,0 +1,25 @@ + +// From https://hapijs.com/api/16.1.1#error-response + +import * as Hapi from 'hapi'; +const Boom = require('boom'); + +const server = new Hapi.Server(); + +server.route({ + method: 'GET', + path: '/badRequest', + handler: function (request, reply) { + + return reply(Boom.badRequest('Unsupported parameter')); + } +}); + +server.route({ + method: 'GET', + path: '/internal', + handler: function (request, reply) { + + return reply(new Error('unexpect error')); + } +}); diff --git a/types/hapi/test/response/events.ts b/types/hapi/test/response/events.ts new file mode 100644 index 0000000000..2d1658087b --- /dev/null +++ b/types/hapi/test/response/events.ts @@ -0,0 +1,30 @@ + +// From https://hapijs.com/api/16.1.1#response-events + +import * as Hapi from 'hapi'; +const Crypto = require('crypto'); +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +const preResponse: Hapi.ServerExtRequestHandler = function (request, reply) { + + const response = request.response!; + if (response.isBoom) { + return reply(); + } + + const hash = Crypto.createHash('sha1'); + response.on('peek', (chunk) => { + + hash.update(chunk); + }); + + response.once('finish', () => { + + console.log(hash.digest('hex')); + }); + + return reply.continue(); +}; + +server.ext('onPreResponse', preResponse); diff --git a/types/hapi/test/response/flow-control.ts b/types/hapi/test/response/flow-control.ts new file mode 100644 index 0000000000..bc18a36aff --- /dev/null +++ b/types/hapi/test/response/flow-control.ts @@ -0,0 +1,14 @@ + +// From https://hapijs.com/api/16.1.1#flow-control + +import * as Hapi from 'hapi'; + +const handler = function (request: Hapi.Request, reply: Hapi.ReplyWithContinue) { + + const response = reply('success').hold(); + + setTimeout(() => { + + response.send(); + }, 1000); +}; diff --git a/types/hapi/test/route/additional-options.ts b/types/hapi/test/route/additional-options.ts new file mode 100644 index 0000000000..99e818d08a --- /dev/null +++ b/types/hapi/test/route/additional-options.ts @@ -0,0 +1,39 @@ +'use strict'; + +import * as Hapi from 'hapi'; + +var authConfig: Hapi.RouteAdditionalConfigurationOptions = { + app: {} +}; + +// Handler in config +const user: Hapi.RouteAdditionalConfigurationOptions = { + cache: { expiresIn: 5000, statuses: [200, 201] }, + handler: function (request, reply) { + + return reply({ name: 'John' }); + } +}; + +// Add in addition to examples in docs + +var cache: Hapi.RouteCacheOptions = { + privacy: 'default', + expiresIn: 5000, +}; + +cache = { + privacy: 'default', + expiresAt: '22:44', +}; + +cache = { + privacy: 'default', +}; + +/* should error (as does!) +var cache: Hapi.RouteCacheOptions = { + expiresIn: 5000, + expiresAt: '22:44', +}; +*/ diff --git a/types/hapi/test/route/auth.ts b/types/hapi/test/route/auth.ts new file mode 100644 index 0000000000..e5239322da --- /dev/null +++ b/types/hapi/test/route/auth.ts @@ -0,0 +1,25 @@ +'use strict'; + +import * as Hapi from 'hapi'; + +var routeMoreConfig: Hapi.RouteAdditionalConfigurationOptions = { + auth: false, +} +routeMoreConfig = { + auth: 'some_strategy', +} +routeMoreConfig = { + auth: { + mode: 'required', + strategies: ['strat1', 'strat2'], + strategy: 'should not be given when strategies given', + payload: false, + access: { + scope: false, + entity: 'any', + }, + // Will over write values in access + scope: false, + entity: 'any', + } +} diff --git a/types/hapi/test/route/config.ts b/types/hapi/test/route/config.ts new file mode 100644 index 0000000000..16457395f8 --- /dev/null +++ b/types/hapi/test/route/config.ts @@ -0,0 +1,46 @@ +'use strict'; + +import * as Hapi from 'hapi'; + +// different methods +var routeConfig: Hapi.RouteConfiguration = { + path: '/signin', + method: 'PUT', + vhost: 'site.coms', +}; +var routeConfig: Hapi.RouteConfiguration = { + path: '/signin', + method: '*' +}; +var routeConfig: Hapi.RouteConfiguration = { + path: '/signin', + method: ['OPTIONS', '*'] +}; + +// different handlers +var routeConfig: Hapi.RouteConfiguration = { + path: '/signin', + method: 'PUT', + handler: 'some registered handler' +}; +var routeConfig: Hapi.RouteConfiguration = { + path: '/signin', + method: 'PUT', + handler: function (request, reply) { + return reply('ok'); + } +}; + +const server = new Hapi.Server(); +server.route(routeConfig); + +// Handler in config +const user: Hapi.RouteAdditionalConfigurationOptions = { + cache: { expiresIn: 5000 }, + handler: function (request, reply) { + + return reply({ name: 'John' }); + } +}; + +server.route({method: 'GET', path: '/user', config: user }); diff --git a/types/hapi/test/route/handler.ts b/types/hapi/test/route/handler.ts new file mode 100644 index 0000000000..c66d17018e --- /dev/null +++ b/types/hapi/test/route/handler.ts @@ -0,0 +1,10 @@ +'use strict'; + +import * as Hapi from 'hapi'; + +var handler: Hapi.RouteHandler = function(request, reply) { + reply('success'); +} +var strictHandler: Hapi.RouteHandler = function(request, reply) { + reply(123); +} diff --git a/types/hapi/test/route/prerequisites.ts b/types/hapi/test/route/prerequisites.ts new file mode 100644 index 0000000000..c4ca7650fa --- /dev/null +++ b/types/hapi/test/route/prerequisites.ts @@ -0,0 +1,47 @@ + +// From https://hapijs.com/api/16.1.1#route-prerequisites + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +const pre1: Hapi.RoutePrerequisiteRequestHandler = function (request, reply) { + + return reply('Hello'); +}; + +const pre2: Hapi.RoutePrerequisiteRequestHandler = function (request, reply) { + + return reply('World'); +}; + +const pre3: Hapi.RoutePrerequisiteRequestHandler = function (request, reply) { + const pre = request.pre as Pre1; + return reply(pre.m1 + ' ' + pre.m2); +}; + +server.route({ + method: 'GET', + path: '/', + config: { + pre: [ + [ + // m1 and m2 executed in parallel + { method: pre1, assign: 'm1' }, + { method: pre2, assign: 'm2' } + ], + { method: pre3, assign: 'm3' }, + ], + handler: function (request, reply) { + const pre = request.pre as Pre2; + return reply(pre.m3 + '\n'); + } + } +}); +interface Pre1 { + m1: string; + m2: string; +} +interface Pre2 extends Pre1 { + m3: string; +} diff --git a/types/hapi/test/route/public-interface.ts b/types/hapi/test/route/public-interface.ts new file mode 100644 index 0000000000..e61fb8275f --- /dev/null +++ b/types/hapi/test/route/public-interface.ts @@ -0,0 +1,17 @@ +'use strict'; + +import * as Hapi from 'hapi'; + +var route = {}; + +var a: string = route.method; +var a: string = route.path; +if (typeof(route.vhost) == 'string') { + var a: string = route.vhost; +} else { + var b: string[] = route.vhost!; +} +var c: Hapi.ServerRealm = route.realm; +var d: Hapi.RouteAdditionalConfigurationOptions = route.settings; +var a: string = route.fingerprint; +var e: boolean = route.auth.access( {}); diff --git a/types/hapi/test/server/app.ts b/types/hapi/test/server/app.ts new file mode 100644 index 0000000000..dac8921c20 --- /dev/null +++ b/types/hapi/test/server/app.ts @@ -0,0 +1,11 @@ + +// From https://hapijs.com/api/16.1.1#serverapp + +import * as Hapi from 'hapi'; +var server = new Hapi.Server(); +server.app.key = 'value'; + +const handler: Hapi.RouteHandler = function (request, reply) { + + return reply(request.server.app.key); +}; diff --git a/types/hapi/test/server/auth.ts b/types/hapi/test/server/auth.ts new file mode 100644 index 0000000000..3f28732d42 --- /dev/null +++ b/types/hapi/test/server/auth.ts @@ -0,0 +1,101 @@ + +// From https://hapijs.com/api/16.1.1#serverauthapi + +import * as Hapi from 'hapi'; +import * as Boom from 'boom'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +var scheme: Hapi.ServerAuthScheme = function (server, options) { + + return { + api: { + settings: { + x: 5 + } + }, + authenticate: function (request, reply) { + + const req = request.raw.req; + const authorization = req.headers.authorization; + if (!authorization) { + return reply(Boom.unauthorized(null, 'Custom')); + } + + return reply.continue({ credentials: { user: 'john' } }); + } + }; +}; + +server.auth.scheme('custom', scheme); +server.auth.strategy('default', 'custom'); + +console.log(server.auth.api.default.settings.x); // 5 + +// Default + +server.auth.default('default'); + +server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply(request.auth.credentials.user); + } +}); + +// scheme + +scheme = function (server, options) { + + return { + authenticate: function (request, reply) { + + const req = request.raw.req; + const authorization = req.headers.authorization; + if (!authorization) { + return reply(Boom.unauthorized(null, 'Custom')); + } + + return reply.continue({ credentials: { user: 'john' } }); + } + }; +}; + +server.auth.scheme('custom', scheme); + +// strategy + +server.auth.scheme('custom', scheme); +server.auth.strategy('default', 'custom'); + +server.route({ + method: 'GET', + path: '/', + config: { + auth: 'default', + handler: function (request, reply) { + + return reply(request.auth.credentials.user); + } + } +}); + +// test + +server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + request.server.auth.test('default', request, (err, credentials) => { + + if (err) { + return reply({ status: false }); + } + + return reply({ status: true, user: (credentials as any).name }); + }); + } +}); diff --git a/types/hapi/test/server/bind.ts b/types/hapi/test/server/bind.ts new file mode 100644 index 0000000000..2741e16c82 --- /dev/null +++ b/types/hapi/test/server/bind.ts @@ -0,0 +1,24 @@ + +// From https://hapijs.com/api/16.1.1#serverbindcontext + +import * as Hapi from 'hapi'; + +interface HandlerThis { + message: string; +} + +const handler: Hapi.RouteHandler = function (this: HandlerThis, request, reply) { + + return reply(this.message); +}; + +var register: Hapi.PluginFunction<{}> = function (server, options, next) { + + const bind: HandlerThis = { + message: 'hello' + }; + + server.bind(bind); + server.route({ method: 'GET', path: '/', handler: handler }); + return next(); +}; diff --git a/types/hapi/test/server/cache.ts b/types/hapi/test/server/cache.ts new file mode 100644 index 0000000000..4cb9eaa4b7 --- /dev/null +++ b/types/hapi/test/server/cache.ts @@ -0,0 +1,33 @@ + +// From https://hapijs.com/api/16.1.1#servercacheoptions + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +const cache = server.cache({ segment: 'countries', expiresIn: 60 * 60 * 1000 }); +cache.set('norway', { capital: 'oslo' }, null, (err) => { + + cache.get('norway', (err, value, cached, log) => { + + // value === { capital: 'oslo' }; + }); +}); + +// provision +// From https://hapijs.com/api/16.1.1#servercacheprovisionoptions-callback + +server.initialize((err) => { + + server.cache.provision({ engine: require('catbox-memory'), name: 'countries' }, (err) => { + + const cache = server.cache({ cache: 'countries', expiresIn: 60 * 60 * 1000 }); + cache.set('norway', { capital: 'oslo' }, null, (err) => { + + cache.get('norway', (err, value, cached, log) => { + + // value === { capital: 'oslo' }; + }); + }); + }); +}); diff --git a/types/hapi/test/server/connection-options.ts b/types/hapi/test/server/connection-options.ts new file mode 100644 index 0000000000..3a526307db --- /dev/null +++ b/types/hapi/test/server/connection-options.ts @@ -0,0 +1,39 @@ + +// From https://hapijs.com/api/16.1.1#serverconnectionoptions + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); + +const web = server.connection({ port: 8000, host: 'example.com', labels: ['web'] }); +const admin = server.connection({ port: 8001, host: 'example.com', labels: ['admin'] }); + +server.connections.length === 2; +web.connections.length === 1; +admin.connections.length === 1; + +// example 2 + +var registerFunction: Hapi.PluginFunction<{}> = function (srv, options, next) { + + // Use the 'srv' argument to add a new connection + + const server = srv.connection(); + + // Use the 'server' return value to manage the new connection + + server.route({ + path: '/', + method: 'GET', + handler: function (request, reply) { + + return reply('hello'); + } + }); + + return next(); +}; + +registerFunction.attributes = { + name: 'example', + connections: false +}; diff --git a/types/hapi/test/server/connections.ts b/types/hapi/test/server/connections.ts new file mode 100644 index 0000000000..fb2f9f303e --- /dev/null +++ b/types/hapi/test/server/connections.ts @@ -0,0 +1,13 @@ + +// From https://hapijs.com/api/16.1.1#serverconnections + +import * as Hapi from 'hapi'; +var server = new Hapi.Server(); +server.connection({ port: 80, labels: 'a' }); +server.connection({ port: 8080, labels: 'b' }); + +// server.connections.length === 2 + +const a = server.select('a'); + +// a.connections.length === 1 diff --git a/types/hapi/test/server/decoder.ts b/types/hapi/test/server/decoder.ts new file mode 100644 index 0000000000..edd52485ea --- /dev/null +++ b/types/hapi/test/server/decoder.ts @@ -0,0 +1,9 @@ + +// From https://hapijs.com/api/16.1.1#serverdecoderencoding-decoder + +import * as Hapi from 'hapi'; +import * as Zlib from 'zlib'; +const server = new Hapi.Server(); +server.connection({ port: 80, routes: { payload: { compression: { special: { chunkSize: 16 * 1024 } } } } }); + +server.decoder('special', (options) => Zlib.createGunzip(options)); diff --git a/types/hapi/test/server/decorate.ts b/types/hapi/test/server/decorate.ts new file mode 100644 index 0000000000..540e29c951 --- /dev/null +++ b/types/hapi/test/server/decorate.ts @@ -0,0 +1,68 @@ + +// From https://hapijs.com/api/16.1.1#serverdecoratetype-property-method-options + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +const success = function (this: Hapi.ReplyNoContinue) { + + return this.response({ status: 'ok' }); +}; + +server.decorate('reply', 'success', success); + +declare module 'hapi' { + interface Base_Reply { + success: () => Response; + } +} + +server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply.success(); + } +}); + +// custom typing code for decorating request + +server.decorate('request', 'some_request_method', (request) => { + return function() { + // Do some sort of processing; + return request.id; + } +}, {apply: true}); + +declare module 'hapi' { + interface Request { + some_request_method(): void; + } +} + +server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + request.some_request_method(); + return reply(); + } +}); + +// custom typing code for decorating server + +server.decorate('server', 'some_server_method', (server: Hapi.Server) => { + return function(arg1: number){ + return "some text"; + } +}); + +declare module 'hapi' { + interface Server { + some_server_method(arg1: number): string; + } +} + +server.some_server_method(1); diff --git a/types/hapi/test/server/dependency.ts b/types/hapi/test/server/dependency.ts new file mode 100644 index 0000000000..1d63397cf2 --- /dev/null +++ b/types/hapi/test/server/dependency.ts @@ -0,0 +1,24 @@ + +// From https://hapijs.com/api/16.1.1#serverdependencydependencies-after + +import * as Hapi from 'hapi'; + +const after: Hapi.AfterDependencyLoadCallback = function (server, next) { + + // Additional plugin registration logic + return next(); +}; + +var registerFunction: Hapi.PluginFunction<{}> = function (server, options, next) { + + server.dependency('yar', after); + return next(); +}; + +// Example 2, via attributes + +registerFunction.attributes = { + name: 'test', + version: '1.0.0', + dependencies: 'yar' +}; diff --git a/types/hapi/test/server/emit.ts b/types/hapi/test/server/emit.ts new file mode 100644 index 0000000000..d248f7d4ab --- /dev/null +++ b/types/hapi/test/server/emit.ts @@ -0,0 +1,12 @@ + +// From https://hapijs.com/api/16.1.1#serveremitcriteria-data-callback + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +server.event('test'); +server.on('test', (update: Update) => console.log(update)); +type Update = string; +var toSend: Update = 'hello'; +server.emit('test', toSend); diff --git a/types/hapi/test/server/encoder.ts b/types/hapi/test/server/encoder.ts new file mode 100644 index 0000000000..f35102b776 --- /dev/null +++ b/types/hapi/test/server/encoder.ts @@ -0,0 +1,9 @@ + +// From https://hapijs.com/api/16.1.1#serverencoderencoding-encoder + +import * as Hapi from 'hapi'; +import * as Zlib from 'zlib'; +const server = new Hapi.Server(); +server.connection({ port: 80, routes: { compression: { special: { chunkSize: 16 * 1024 } } } }); + +server.encoder('special', (options) => Zlib.createGzip(options)); diff --git a/types/hapi/test/server/event.ts b/types/hapi/test/server/event.ts new file mode 100644 index 0000000000..f0c2215f12 --- /dev/null +++ b/types/hapi/test/server/event.ts @@ -0,0 +1,12 @@ + +// From https://hapijs.com/api/16.1.1#servereventevents + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +server.event('test'); +server.on('test', (update: Update) => console.log(update)); +type Update = string; +var toSend: Update = 'hello'; +server.emit('test', toSend); diff --git a/types/hapi/test/server/expose.ts b/types/hapi/test/server/expose.ts new file mode 100644 index 0000000000..effbc1ae49 --- /dev/null +++ b/types/hapi/test/server/expose.ts @@ -0,0 +1,17 @@ + +// From https://hapijs.com/api/16.1.1#serverexposekey-value + +import * as Hapi from 'hapi'; +var register: Hapi.PluginFunction<{}> = function (server, options, next) { + + server.expose('util', function () { console.log('something'); }); + return next(); +}; + +// example of `expose()` merging object + +register = function (server, options, next) { + + server.expose({ util: function () { console.log('something'); } }); + return next(); +}; diff --git a/types/hapi/test/server/ext.ts b/types/hapi/test/server/ext.ts new file mode 100644 index 0000000000..410c913c41 --- /dev/null +++ b/types/hapi/test/server/ext.ts @@ -0,0 +1,46 @@ + +// From https://hapijs.com/api/16.1.1#serverextevents + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +server.ext({ + type: 'onRequest', + method: function (request, reply) { + + // Change all requests to '/test' + request.setUrl('/test'); + return reply.continue(); + } +}); + +var handler: Hapi.RouteHandler = function (request, reply) { + + return reply({ status: 'ok' }); +}; + +server.route({ method: 'GET', path: '/test', handler: handler }); +server.start((err) => { }); + +// All requests will get routed to '/test' + + +// Example 2 + +server.ext('onRequest', function (request, reply) { + + // Change all requests to '/test' + request.setUrl('/test'); + return reply.continue(); +}); + +handler = function (request, reply) { + + return reply({ status: 'ok' }); +}; + +server.route({ method: 'GET', path: '/test', handler: handler }); +server.start((err) => { }); + +// All requests will get routed to '/test' diff --git a/types/hapi/test/server/handler.ts b/types/hapi/test/server/handler.ts new file mode 100644 index 0000000000..713fe7f114 --- /dev/null +++ b/types/hapi/test/server/handler.ts @@ -0,0 +1,55 @@ + +// From https://hapijs.com/api/16.1.1#serverhandlername-method + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ host: 'localhost', port: 8000 }); + +// Defines new handler for routes on this server +var handler: Hapi.MakeRouteHandler = function (route, options) { + + return function (request, reply) { + + return reply('new handler: ' + options.msg); + } +}; + +server.handler('test', handler); + +interface TestPluginConfig { + msg: string; +} + +declare module 'hapi' { + interface RouteHandlerPlugins { + test?: TestPluginConfig; + } +} + +server.route({ + method: 'GET', + path: '/', + handler: { test: { msg: 'test' } } +}); + +server.start(function (err) { }); + +// example 2 + +handler = function (route, options: TestPluginConfig) { + + return function (request, reply) { + + return reply('new handler: ' + options.msg); + } +}; + +// Change the default payload processing for this handler +handler.defaults = { + payload: { + output: 'stream', + parse: false + } +}; + +server.handler('test', handler); diff --git a/types/hapi/test/server/info.ts b/types/hapi/test/server/info.ts new file mode 100644 index 0000000000..eaf1b39747 --- /dev/null +++ b/types/hapi/test/server/info.ts @@ -0,0 +1,16 @@ + +// From https://hapijs.com/api/16.1.1#serverinfo + +import assert = require('assert'); +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +var options: Hapi.ServerConnectionOptions = { port: 80 }; +server.connection(options); + +if(server.info) assert(server.info.port === 80); + +options = { port: 8080 }; +server.connection(options); + +assert(server.info === null); +assert(server.connections[1].info.port === 8080); diff --git a/types/hapi/test/server/initialize.ts b/types/hapi/test/server/initialize.ts new file mode 100644 index 0000000000..7ef4b51527 --- /dev/null +++ b/types/hapi/test/server/initialize.ts @@ -0,0 +1,12 @@ + +// From https://hapijs.com/api/16.1.1#serverinitializecallback + +import * as Hapi from 'hapi'; +const Hoek = require('hoek'); +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +server.initialize((err) => { + + Hoek.assert(!err, err); +}); diff --git a/types/hapi/test/server/inject.ts b/types/hapi/test/server/inject.ts new file mode 100644 index 0000000000..130af7489b --- /dev/null +++ b/types/hapi/test/server/inject.ts @@ -0,0 +1,18 @@ + +// From https://hapijs.com/api/16.1.1#serverinjectoptions-callback + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +const handler: Hapi.RouteHandler = function (request, reply) { + + return reply('Success!'); +}; + +server.route({ method: 'GET', path: '/', handler: handler }); + +server.inject('/', (res) => { + + console.log(res.result); +}); diff --git a/types/hapi/test/server/listener.ts b/types/hapi/test/server/listener.ts new file mode 100644 index 0000000000..d7bb6fc5ea --- /dev/null +++ b/types/hapi/test/server/listener.ts @@ -0,0 +1,19 @@ + +// From https://hapijs.com/api/16.1.1#serverlistener + +import * as Hapi from 'hapi'; +import SocketIO = require('socket.io'); + +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +// https://socket.io/docs/server-api/#server-attach-httpserver-options +const io = SocketIO.listen(server.listener); +io.sockets.on('connection', (socket) => { + + // TODO, FIXME by removing , probably `socket` type given here is + // a wrapper around socket.io. Much less likely is either + // a type error in the TypeScript definitions of socket.io or + // an error in the Hapi docs. + socket.emit( { msg: 'welcome' }); +}); diff --git a/types/hapi/test/server/load.ts b/types/hapi/test/server/load.ts new file mode 100644 index 0000000000..ee204c864f --- /dev/null +++ b/types/hapi/test/server/load.ts @@ -0,0 +1,7 @@ + +// From https://hapijs.com/api/16.1.1#serverload + +import * as Hapi from 'hapi'; +const server = new Hapi.Server({ load: { sampleInterval: 1000 } }); + +var d: number = server.load.rss; diff --git a/types/hapi/test/server/log.ts b/types/hapi/test/server/log.ts new file mode 100644 index 0000000000..4507358d60 --- /dev/null +++ b/types/hapi/test/server/log.ts @@ -0,0 +1,15 @@ + +// From https://hapijs.com/api/16.1.1#serverlogtags-data-timestamp + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +server.on('log', (event, tags) => { + + if (tags.error) { + console.log(event); + } +}); + +server.log(['test', 'error'], 'Test event'); diff --git a/types/hapi/test/server/lookup.ts b/types/hapi/test/server/lookup.ts new file mode 100644 index 0000000000..89ce852700 --- /dev/null +++ b/types/hapi/test/server/lookup.ts @@ -0,0 +1,19 @@ + +// From https://hapijs.com/api/16.1.1#serverlookupid + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection(); +server.route({ + method: 'GET', + path: '/', + config: { + handler: function (request, reply) { + + return reply(); + }, + id: 'root' + } +}); + +const route = server.lookup('root'); diff --git a/types/hapi/test/server/match.ts b/types/hapi/test/server/match.ts new file mode 100644 index 0000000000..f322a31494 --- /dev/null +++ b/types/hapi/test/server/match.ts @@ -0,0 +1,19 @@ + +// From https://hapijs.com/api/16.1.1#servermatchmethod-path-host + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection(); +server.route({ + method: 'GET', + path: '/', + config: { + handler: function (request, reply) { + + return reply(); + }, + id: 'root' + } +}); + +const route = server.match('get', '/'); diff --git a/types/hapi/test/server/method.ts b/types/hapi/test/server/method.ts new file mode 100644 index 0000000000..a67022d250 --- /dev/null +++ b/types/hapi/test/server/method.ts @@ -0,0 +1,78 @@ + +// From https://hapijs.com/api/16.1.1#servermethodname-method-options + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +// Simple arguments + +const add = function (a: number, b: number, next: Hapi.ServerMethodNext) { + + return next(null, a + b); +}; + +server.method('sum', add, { cache: { expiresIn: 2000, generateTimeout: 100 } }); + +var next: Hapi.ServerMethodNext = (err, result) => { + + console.log(result); +}; +server.methods.sum(4, 5, next); + +// Object argument + +const addArray = function (array: number[], next: Hapi.ServerMethodNext) { + + let sum = 0; + array.forEach((item) => { + + sum += item; + }); + + return next(null, sum); +}; + +server.method('sumObj', addArray, { + cache: { expiresIn: 2000, generateTimeout: 100 }, + generateKey: function (array) { + + return array.join(','); + } +}); + +var next: Hapi.ServerMethodNext = (err, result) => { + + console.log(result); +}; +server.methods.sumObj([5, 6], next); + +// Synchronous method with cache + +const addSync = function (a: number, b: number) { + + return a + b; +}; + +server.method('sumSync', addSync, { cache: { expiresIn: 2000, generateTimeout: 100 }, callback: false }); + +var next: Hapi.ServerMethodNext = (err, result) => { + + console.log(result); +}; +server.methods.sumSync(4, 5, next); + +// server.method(methods) + +var config = { + name: 'sum', + method: add, + options: { + cache: { + expiresIn: 2000, + generateTimeout: 100 + } + } +}; +server.method(config); +server.method([config]); diff --git a/types/hapi/test/server/methods.ts b/types/hapi/test/server/methods.ts new file mode 100644 index 0000000000..1e2060c3e8 --- /dev/null +++ b/types/hapi/test/server/methods.ts @@ -0,0 +1,17 @@ + +// From https://hapijs.com/api/16.1.1#servermethods + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); + +const add = function (a: number, b: number, next: (err: Error | null, result: number) => void) { + + return next(null, a + b); +}; + +server.method('add', add); + +server.methods.add(1, 2, (err: Error | null, result: number) => { + + // result === 3 +}); diff --git a/types/hapi/test/server/mime.ts b/types/hapi/test/server/mime.ts new file mode 100644 index 0000000000..e2dd25529b --- /dev/null +++ b/types/hapi/test/server/mime.ts @@ -0,0 +1,21 @@ + +// From https://hapijs.com/api/16.1.1#servermime + +import * as Hapi from 'hapi'; + +const options: Hapi.ServerOptions = { + mime: { + override: { + 'node/module': { + source: 'steve', + compressible: false, + extensions: ['node', 'module', 'npm'], + type: 'node/module' + } + } + } +}; + +const server = new Hapi.Server(options); +server.mime.path('code.js').type === 'application/javascript'; +server.mime.path('file.npm').type === 'node/module'; diff --git a/types/hapi/test/server/new.ts b/types/hapi/test/server/new.ts new file mode 100644 index 0000000000..24ac654ecd --- /dev/null +++ b/types/hapi/test/server/new.ts @@ -0,0 +1,65 @@ +'use strict'; + +import * as Hapi from 'hapi'; + +new Hapi.Server(); +new Hapi.Server({ + app: {some: 'values'}, + cache: require('catbox-redis'), +}); + +const server = new Hapi.Server({ + cache: require('catbox-redis'), + load: { + sampleInterval: 1000 + } +}); + +// Specific cache configuration options +new Hapi.Server({ + cache: { + engine: require('catbox-redis'), + // name: 'optionally omitted when only a single cache used', + } +}); +new Hapi.Server({ + cache: [{ + engine: require('catbox-redis'), + name: 'unique 1', + }, + { + engine: require('catbox-redis'), + name: 'unique 2', + shared: true, + otherOptions: 'will be passed to the catbox strategy', + }] +}); +new Hapi.Server({ + cache: [{ + engine: require('catbox-redis'), + }, + // Does not correctly error but will be caught by hapi at runtime + { + engine: require('catbox-redis'), + }] +}); + +new Hapi.Server({ + connections: { + app: {}, + compression: false, + load: { + maxHeapUsedBytes: 10, + maxRssBytes: 10, + maxEventLoopDelay: 10, + }, + plugins: { + 'some-plugin-name': {options: 'here'} + }, + router: { + isCaseSensitive: false, + stripTrailingSlash: true, + }, + routes: {} + } +}) diff --git a/types/hapi/test/server/on.ts b/types/hapi/test/server/on.ts new file mode 100644 index 0000000000..08802fda96 --- /dev/null +++ b/types/hapi/test/server/on.ts @@ -0,0 +1,43 @@ + +// From https://hapijs.com/api/16.1.1#serveroncriteria-listener +// From https://hapijs.com/api/16.1.1#server-events + +import * as Hapi from 'hapi'; + +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +server.event('test'); +server.on('test', (update: Update) => console.log(update)); +type Update = string; +var toSend: Update = 'hello'; +server.emit('test', toSend); + +server.on('log', (event, tags) => { + + if (tags.error) { + console.log('Server error: ' + (event.data || 'unspecified')); + } +}); + +server.on('request', (request, event, tags) => { + + if (tags.received) { + console.log('New request: ' + request.id); + } +}); + +server.on('request-error', (request, err) => { + + console.log('Error response (500) sent for request: ' + request.id + ' because: ' + err.message); +}); + +server.on('response', (request) => { + + console.log('Response sent for request: ' + request.id); +}); + +server.on('route', (route, connection, server) => { + + console.log('New route added: ' + route.path); +}); diff --git a/types/hapi/test/server/once.ts b/types/hapi/test/server/once.ts new file mode 100644 index 0000000000..55fdb08ec2 --- /dev/null +++ b/types/hapi/test/server/once.ts @@ -0,0 +1,14 @@ + +// From https://hapijs.com/api/16.1.1#serveroncecriteria-listener + +import * as Hapi from 'hapi'; + +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +server.event('test'); +server.once('test', (update: Update) => console.log(update)); +type Update = string; +var toSend: Update = 'hello'; +server.emit('test', toSend); +server.emit('test', toSend); // Ignored diff --git a/types/hapi/test/server/path.ts b/types/hapi/test/server/path.ts new file mode 100644 index 0000000000..20479a795f --- /dev/null +++ b/types/hapi/test/server/path.ts @@ -0,0 +1,13 @@ + +// From https://hapijs.com/api/16.1.1#serverpathrelativeto + +import * as Hapi from 'hapi'; + +var register: Hapi.PluginFunction<{}> = function (server, options, next) { + + // Assuming the Inert plugin was registered previously + + server.path(__dirname + '../static'); + server.route({ path: '/file', method: 'GET', handler: { file: './test.html' } }); + next(); +}; diff --git a/types/hapi/test/server/plugins.ts b/types/hapi/test/server/plugins.ts new file mode 100644 index 0000000000..2ce13ffb2f --- /dev/null +++ b/types/hapi/test/server/plugins.ts @@ -0,0 +1,15 @@ + +// From https://hapijs.com/api/16.1.1#serverplugins + +import * as Hapi from 'hapi'; + +var registerFunction: Hapi.PluginFunction<{}> = function(server, options, next) { + + server.expose('key', 'value'); + server.plugins.example.key === 'value'; + return next(); +}; + +registerFunction.attributes = { + name: 'example' +}; diff --git a/types/hapi/test/server/realm.ts b/types/hapi/test/server/realm.ts new file mode 100644 index 0000000000..31f764b7ef --- /dev/null +++ b/types/hapi/test/server/realm.ts @@ -0,0 +1,10 @@ + +// From https://hapijs.com/api/16.1.1#serverrealm + +import * as Hapi from 'hapi'; + +var registerFunction: Hapi.PluginFunction<{}> = function(server, options, next) { + + console.log(server.realm.modifiers.route.prefix); + return next(); +}; diff --git a/types/hapi/test/server/register.ts b/types/hapi/test/server/register.ts new file mode 100644 index 0000000000..c04e0b16bc --- /dev/null +++ b/types/hapi/test/server/register.ts @@ -0,0 +1,18 @@ + +// From https://hapijs.com/api/16.1.1#serverregisterplugins-options-callback + +import * as Hapi from 'hapi'; + +const server = new Hapi.Server(); + +server.register({ + register: require('plugin_name'), + options: { + message: 'hello' + } +}, (err) => { + + if (err) { + console.log('Failed loading plugin'); + } +}); diff --git a/types/hapi/test/server/route.ts b/types/hapi/test/server/route.ts new file mode 100644 index 0000000000..0402fd3e89 --- /dev/null +++ b/types/hapi/test/server/route.ts @@ -0,0 +1,12 @@ + +// From https://hapijs.com/api/16.1.1#serverrouteoptions + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +server.route({ method: 'GET', path: '/', handler: function (request, reply) { return reply('ok'); } }); +server.route([ + { method: 'GET', path: '/1', handler: function (request, reply) { return reply('ok'); } }, + { method: 'GET', path: '/2', handler: function (request, reply) { return reply('ok'); } } +]); diff --git a/types/hapi/test/server/select.ts b/types/hapi/test/server/select.ts new file mode 100644 index 0000000000..8a25f45abd --- /dev/null +++ b/types/hapi/test/server/select.ts @@ -0,0 +1,11 @@ + +// From https://hapijs.com/api/16.1.1#serverselectlabels + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80, labels: ['a', 'b'] }); +server.connection({ port: 8080, labels: ['a', 'c'] }); +server.connection({ port: 8081, labels: ['b', 'c'] }); + +const a = server.select('a'); // 80, 8080 +const ac = a.select('c'); // 8080 diff --git a/types/hapi/test/server/settings.ts b/types/hapi/test/server/settings.ts new file mode 100644 index 0000000000..c2ec9ce3ed --- /dev/null +++ b/types/hapi/test/server/settings.ts @@ -0,0 +1,11 @@ + +// From https://hapijs.com/api/16.1.1#serversettings + +import * as Hapi from 'hapi'; +const server = new Hapi.Server({ + app: { + key: 'value' + } +}); + +server.settings.app === { key: 'value' }; diff --git a/types/hapi/test/server/start.ts b/types/hapi/test/server/start.ts new file mode 100644 index 0000000000..a15bb79740 --- /dev/null +++ b/types/hapi/test/server/start.ts @@ -0,0 +1,13 @@ + +// From https://hapijs.com/api/16.1.1#serverstartcallback + +import * as Hapi from 'hapi'; +import * as Hoek from 'hoek'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +server.start((err) => { + + Hoek.assert(!err, err!); + console.log('Server started at: ' + server.info!.uri); +}); diff --git a/types/hapi/test/server/state.ts b/types/hapi/test/server/state.ts new file mode 100644 index 0000000000..2035343a17 --- /dev/null +++ b/types/hapi/test/server/state.ts @@ -0,0 +1,38 @@ + +// From https://hapijs.com/api/16.1.1#serverstatename-options + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +// Set cookie definition + +server.state('session', { + ttl: 24 * 60 * 60 * 1000, // One day + isSecure: true, + path: '/', + encoding: 'base64json' +}); + +// Set state in route handler + +const handler: Hapi.RouteHandler = function (request, reply) { + + let session = request.state.session; + if (!session) { + session = { user: 'joe' }; + } + + session.last = Date.now(); + + return reply('Success').state('session', session); +}; + +// Example 2 + +server.on('request-internal', (request, event, tags) => { + + if (tags.error && tags.state) { + console.error(event); + } +}); diff --git a/types/hapi/test/server/stop.ts b/types/hapi/test/server/stop.ts new file mode 100644 index 0000000000..ddfd07aa8f --- /dev/null +++ b/types/hapi/test/server/stop.ts @@ -0,0 +1,11 @@ + +// From https://hapijs.com/api/16.1.1#serverstopoptions-callback + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +server.stop({ timeout: 60 * 1000 }, (err) => { + + console.log('Server stopped'); +}); diff --git a/types/hapi/test/server/table.ts b/types/hapi/test/server/table.ts new file mode 100644 index 0000000000..abe0707c76 --- /dev/null +++ b/types/hapi/test/server/table.ts @@ -0,0 +1,9 @@ + +// From https://hapijs.com/api/16.1.1#servertablehost + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.connection({ port: 80, host: 'example.com' }); +server.route({ method: 'GET', path: '/example', handler: function (request, reply) { return reply(); } }); + +const table = server.table(); diff --git a/types/hapi/test/server/version.ts b/types/hapi/test/server/version.ts new file mode 100644 index 0000000000..064e87e3cd --- /dev/null +++ b/types/hapi/test/server/version.ts @@ -0,0 +1,6 @@ + +// From http://hapijs.com/api#serversettings + +import * as Hapi from 'hapi'; +const server = new Hapi.Server(); +server.version === '8.0.0' diff --git a/types/hapi/tsconfig.json b/types/hapi/tsconfig.json index c7385884c3..f8d5b055e2 100644 --- a/types/hapi/tsconfig.json +++ b/types/hapi/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" @@ -17,6 +17,76 @@ }, "files": [ "index.d.ts", - "hapi-tests.ts" + "test/connection/table.ts", + "test/getting-started/01-creating-a-server.ts", + "test/getting-started/02-adding-routes.ts", + "test/getting-started/03-serving-static-content.ts", + "test/getting-started/04-using-plugins.ts", + "test/path/catch-all.ts", + "test/path/parameters.ts", + "test/plugins/options.ts", + "test/reply/continue.ts", + "test/reply/entity.ts", + "test/reply/redirect.ts", + "test/reply/reply.ts", + "test/reply/state_cookie.ts", + "test/request/event-types.ts", + "test/request/generate-response.ts", + "test/request/get-log.ts", + "test/request/log.ts", + "test/request/set-method.ts", + "test/request/set-url.ts", + "test/request/tail.ts", + "test/response/error-representation.ts", + "test/response/error.ts", + "test/response/events.ts", + "test/response/flow-control.ts", + "test/route/additional-options.ts", + "test/route/auth.ts", + "test/route/config.ts", + "test/route/handler.ts", + "test/route/prerequisites.ts", + "test/route/public-interface.ts", + "test/server/app.ts", + "test/server/auth.ts", + "test/server/bind.ts", + "test/server/cache.ts", + "test/server/connection-options.ts", + "test/server/connections.ts", + "test/server/decoder.ts", + "test/server/decorate.ts", + "test/server/dependency.ts", + "test/server/emit.ts", + "test/server/encoder.ts", + "test/server/event.ts", + "test/server/expose.ts", + "test/server/ext.ts", + "test/server/handler.ts", + "test/server/info.ts", + "test/server/initialize.ts", + "test/server/inject.ts", + "test/server/listener.ts", + "test/server/load.ts", + "test/server/log.ts", + "test/server/lookup.ts", + "test/server/match.ts", + "test/server/method.ts", + "test/server/methods.ts", + "test/server/mime.ts", + "test/server/new.ts", + "test/server/on.ts", + "test/server/once.ts", + "test/server/path.ts", + "test/server/plugins.ts", + "test/server/realm.ts", + "test/server/register.ts", + "test/server/route.ts", + "test/server/select.ts", + "test/server/settings.ts", + "test/server/start.ts", + "test/server/state.ts", + "test/server/stop.ts", + "test/server/table.ts", + "test/server/version.ts" ] } \ No newline at end of file diff --git a/types/hapi/tslint.json b/types/hapi/tslint.json deleted file mode 100644 index 56c322f9d7..0000000000 --- a/types/hapi/tslint.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "extends": "dtslint/dt.json", - "rules": { - "indent": [true, "tabs"], - - // All TODOs - "ban-types": false, - "callable-types": false, - "interface-name": false, - "jsdoc-format": false, - "max-line-length": false, - "no-empty-interface": false, - "unified-signatures": false - } -} diff --git a/types/hapi/v12/hapi-tests.ts b/types/hapi/v12/hapi-tests.ts new file mode 100644 index 0000000000..32d1aff704 --- /dev/null +++ b/types/hapi/v12/hapi-tests.ts @@ -0,0 +1,114 @@ +// Copied directly from 918608a5e007d925f4f60a275881057f07c12619 which seems to +// be the last author to work on v12.0 + + +import * as Hapi from 'hapi'; + +// Create a server with a host and port +var server = new Hapi.Server(); +server.connection({ + host: "localhost", + port: 8000 +}); + +// Add plugins +var plugin: any = { + register: function (plugin: Object, options: Object, next: Function) { + next(); + } +}; + +plugin.register.attributes = { + name: "test", + version: "1.0.0" +}; + +// optional options parameter +server.register({}, function (err) {}); + +// optional options.routes.vhost parameter +server.register({}, { select: 'api', routes: { prefix: '/prefix' } }, function (err) {}); + +//server.pack.register(plugin, (err: Object) => { +// if (err) { throw err; } +//}); + +//server.pack.register([plugin], (err: Object) => { +// if (err) { throw err; } +//}); + +// Add server method +var add = function (a: number, b: number, next: (err: any, result?: any, ttl?: number) => void) { + next(null, a + b); +}; + +server.method("sum", add);//, { cache: { expiresIn: 2000 } }); + +server.methods["sum"](4, 5, (err: any, result: any) => { + console.log(result); +}); + +var addArray = function (array: Array, next: (err: any, result?: any, ttl?: number) => void) { + var sum: number = 0; + array.forEach((item: number) => { + sum += item; + }); + next(null, sum); +}; + +server.method("sumObj", addArray, { + //cache: { expiresIn: 2000 }, + generateKey: (array: Array) => { + return array.join(','); + } +}); + +server.methods["sumObj"]([5, 6], (err: any, result: any) => { + console.log(result); +}); + +// Add the route +server.route({ + method: 'GET', + path: '/hello', + handler: function (request: Hapi.Request, reply: Function) { + reply('hello world'); + } +}); + +server.route([{ + method: 'GET', + path: '/hello2', + handler: function (request: Hapi.Request, reply: Function) { + reply('hello world2'); + } +}]); + +// config.validate parameters should be optional +server.route([{ + method: 'GET', + path: '/hello2', + handler: function(request: Hapi.Request, reply: Function) { + reply('hello world2'); + }, + config: { + validate: { + } + } +}]); + +// Should be able to chain reply options +server.route([{ + method: 'GET', + path: '/chained-notation', + handler: function(request: Hapi.Request, reply: Hapi.IReply) { + reply('chained-notation') + .bytes(16) + .code(200) + .type('text/plain') + .header('X-Custom', 'some-value'); + } +}]); + +// Start the server +server.start(); diff --git a/types/hapi/v12/index.d.ts b/types/hapi/v12/index.d.ts new file mode 100644 index 0000000000..13c9e7a28d --- /dev/null +++ b/types/hapi/v12/index.d.ts @@ -0,0 +1,2331 @@ +// Type definitions for hapi 12.0.1 +// Project: http://github.com/spumko/hapi +// Definitions by: Jason Swearingen +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +// Note/Disclaimer: +// Copied directly from 918608a5e007d925f4f60a275881057f07c12619 which seems to +// be the last author to work on v12.0 + + +/// + +declare module "hapi" { + import http = require("http"); + import stream = require("stream"); + import Events = require("events"); + + interface IDictionary { + [key: string]: T; + } + + interface IThenable { + then(onFulfilled?: (value: R) => U | IThenable, onRejected?: (error: any) => U | IThenable): IThenable; + then(onFulfilled?: (value: R) => U | IThenable, onRejected?: (error: any) => void): IThenable; + } + + interface IPromise extends IThenable { + then(onFulfilled?: (value: R) => U | IThenable, onRejected?: (error: any) => U | IThenable): IPromise; + then(onFulfilled?: (value: R) => U | IThenable, onRejected?: (error: any) => void): IPromise; + catch(onRejected?: (error: any) => U | IThenable): IPromise; + } + + /** Boom Module for errors. https://github.com/hapijs/boom + * boom provides a set of utilities for returning HTTP errors. Each utility returns a Boom error response object (instance of Error) which includes the following properties: */ + export interface IBoom extends Error { + /** if true, indicates this is a Boom object instance. */ + isBoom: boolean; + /** convenience bool indicating status code >= 500. */ + isServer: boolean; + /** the error message. */ + message: string; + /** the formatted response.Can be directly manipulated after object construction to return a custom error response.Allowed root keys: */ + output: { + /** the HTTP status code (typically 4xx or 5xx). */ + statusCode: number; + /** an object containing any HTTP headers where each key is a header name and value is the header content. */ + headers: IDictionary; + /** the formatted object used as the response payload (stringified).Can be directly manipulated but any changes will be lost if reformat() is called.Any content allowed and by default includes the following content: */ + payload: { + /** the HTTP status code, derived from error.output.statusCode. */ + statusCode: number; + /** the HTTP status message (e.g. 'Bad Request', 'Internal Server Error') derived from statusCode. */ + error: string; + /** the error message derived from error.message. */ + message: string; + }; + }; + /** reformat()rebuilds error.output using the other object properties. */ + reformat(): void; + + } + + /** cache functionality via the "CatBox" module. */ + export interface ICatBoxCacheOptions { + /** a prototype function or catbox engine object. */ + engine: any; + /** 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 the default cache. If every cache includes a name, a default memory cache is provisions as well. */ + name?: string; + /** if true, allows multiple cache users to share the same segment (e.g. multiple methods using the same cache storage container). Default to false. */ + shared?: boolean; + } + + /** Any connections configuration server defaults can be included to override and customize the individual connection. */ + export interface IServerConnectionOptions extends IConnectionConfigurationServerDefaults { + /** - the public hostname or IP address. Used only to set server.info.host and server.info.uri. If not configured, defaults to the operating system hostname and if not available, to 'localhost'.*/ + host?: string; + /** - sets the host name or IP address the connection will listen on.If not configured, defaults to host if present, otherwise to all available network interfaces (i.e. '0.0.0.0').Set to 127.0.0.1 or localhost to restrict connection to only those coming from the same machine.*/ + address?: string; + /** - the TCP port the connection will listen to.Defaults to an ephemeral port (0) which uses an available port when the server is started (and assigned to server.info.port).If port is a string containing a '/' character, it is used as a UNIX domain socket path and if it starts with '\.\pipe' as a Windows named pipe.*/ + port?: string | number; + /** - the full public URI without the path (e.g. 'http://example.com:8080').If present, used as the connection info.uri otherwise constructed from the connection settings.*/ + uri?: string; + /** - optional node.js HTTP (or HTTPS) http.Server object or any compatible object.If the listener needs to be manually started, set autoListen to false.If the listener uses TLS, set tls to true.*/ + listener?: any; + /** - indicates that the connection.listener will be started manually outside the framework.Cannot be specified with a port setting.Defaults to true.*/ + autoListen?: boolean; + /** caching headers configuration: */ + cache?: { + /** - an array of HTTP response status codes (e.g. 200) which are allowed to include a valid caching directive.Defaults to [200]. */ + statuses: number[]; + }; + /** - a string or string array of labels used to server.select() specific connections matching the specified labels.Defaults to an empty array [](no labels).*/ + labels?: string | string[]; + /** - used to create an HTTPS connection.The tls object is passed unchanged as options to the node.js HTTPS server as described in the node.js HTTPS documentation.Set to true when passing a listener object that has been configured to use TLS directly. */ + tls?: boolean | { key?: string; cert?: string; pfx?: string; } | Object; + + } + + export interface IConnectionConfigurationServerDefaults { + /** application-specific connection configuration which can be accessed via connection.settings.app. Provides a safe place to store application configuration without potential conflicts with the framework internals. Should not be used to configure plugins which should use plugins[name]. Note the difference between connection.settings.app which is used to store configuration values and connection.app which is meant for storing run-time state. */ + app?: any; + /** connection load limits configuration where: */ + load?: { + /** maximum V8 heap size over which incoming requests are rejected with an HTTP Server Timeout (503) response. Defaults to 0 (no limit). */ + maxHeapUsedBytes: number; + /** maximum process RSS size over which incoming requests are rejected with an HTTP Server Timeout (503) response. Defaults to 0 (no limit). */ + maxRssBytes: number; + /** maximum event loop delay duration in milliseconds over which incoming requests are rejected with an HTTP Server Timeout (503) response. Defaults to 0 (no limit). */ + maxEventLoopDelay: number; + }; + /** plugin-specific configuration which can later be accessed via connection.settings.plugins. Provides a place to store and pass connection-specific plugin configuration. plugins is an object where each key is a plugin name and the value is the configuration. Note the difference between connection.settings.plugins which is used to store configuration values and connection.plugins which is meant for storing run-time state. */ + plugins?: any; + /** controls how incoming request URIs are matched against the routing table: */ + router?: { + /** determines whether the paths '/example' and '/EXAMPLE' are considered different resources. Defaults to true. */ + isCaseSensitive: boolean; + /** removes trailing slashes on incoming paths. Defaults to false. */ + stripTrailingSlash: boolean; + }; + /** a route options object used to set the default configuration for every route. */ + routes?: IRouteAdditionalConfigurationOptions; + state?: IServerState; + } + + /** Note that the options object is deeply cloned and cannot contain any values that are unsafe to perform deep copy on.*/ + export interface IServerOptions { + /** application-specific configuration which can later be accessed via server.settings.app. Note the difference between server.settings.app which is used to store static configuration values and server.app which is meant for storing run-time state. Defaults to {}. */ + app?: any; + /** sets up server-side caching. Every server includes a default cache for storing application state. By default, a simple memory-based cache is created which has limited capacity and capabilities. hapi uses catbox for its cache which includes support for common storage solutions (e.g. Redis, MongoDB, Memcached, and Riak). Caching is only utilized if methods and plugins explicitly store their state in the cache. The server cache configuration only defines the storage container itself. cache can be assigned: + a prototype function (usually obtained by calling require() on a catbox strategy such as require('catbox-redis')). + a configuration object with the following options: + enginea prototype function or catbox engine object. + namean 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 the default cache. If every cache includes a name, a default memory cache is provisions as well. + sharedif true, allows multiple cache users to share the same segment (e.g. multiple methods using the same cache storage container). Default to false. + other options passed to the catbox strategy used. + an array of the above object for configuring multiple cache instances, each with a unique name. When an array of objects is provided, multiple cache connections are established and each array item (except one) must include a name. */ + cache?: string | ICatBoxCacheOptions | Array | any; + /** sets the default connections configuration which can be overridden by each connection where: */ + connections?: IConnectionConfigurationServerDefaults; + /** determines which logged events are sent to the console (this should only be used for development and does not affect which events are actually logged internally and recorded). Set to false to disable all console logging, or to an object*/ + debug?: boolean | { + /** - a string array of server log tags to be displayed via console.error() when the events are logged via server.log() as well as internally generated server logs. For example, to display all errors, set the option to ['error']. To turn off all console debug messages set it to false. Defaults to uncaught errors thrown in external code (these errors are handled automatically and result in an Internal Server Error response) or runtime errors due to developer error. */ + log: string[]; + /** - a string array of request log tags to be displayed via console.error() when the events are logged via request.log() as well as internally generated request logs. For example, to display all errors, set the option to ['error']. To turn off all console debug messages set it to false. Defaults to uncaught errors thrown in external code (these errors are handled automatically and result in an Internal Server Error response) or runtime errors due to developer error.*/ + request: string[]; + }; + /** file system related settings*/ + files?: { + /** sets the maximum number of file etag hash values stored in the etags cache. Defaults to 10000.*/ + etagsCacheMaxSize?: number; + }; + /** process load monitoring*/ + load?: { + /** the frequency of sampling in milliseconds. Defaults to 0 (no sampling).*/ + sampleInterval?: number; + }; + + /** options passed to the mimos module (https://github.com/hapijs/mimos) when generating the mime database used by the server and accessed via server.mime.*/ + mime?: any; + /** if true, does not load the inert (file and directory support), h2o2 (proxy support), and vision (views support) plugins automatically. The plugins can be loaded manually after construction. Defaults to false (plugins loaded). */ + minimal?: boolean; + /** plugin-specific configuration which can later be accessed via server.settings.plugins. plugins is an object where each key is a plugin name and the value is the configuration. Note the difference between server.settings.plugins which is used to store static configuration values and server.plugins which is meant for storing run-time state. Defaults to {}.*/ + plugins?: IDictionary; + + } + + export interface IServerViewCompile { + (template: string, options: any): void; + (template: string, options: any, callback: (err: any, compiled: (context: any, options: any, callback: (err: any, rendered: boolean) => void) => void) => void): void; + } + + export interface IServerViewsAdditionalOptions { + /** path - the root file path used to resolve and load the templates identified when calling reply.view().Defaults to current working directory.*/ + path?: string; + /**partialsPath - the root file path where partials are located.Partials are small segments of template code that can be nested and reused throughout other templates.Defaults to no partials support (empty path). + */ + partialsPath?: string; + /**helpersPath - the directory path where helpers are located.Helpers are functions used within templates to perform transformations and other data manipulations using the template context or other inputs.Each '.js' file in the helpers directory is loaded and the file name is used as the helper name.The files must export a single method with the signature function(context) and return a string.Sub - folders are not supported and are ignored.Defaults to no helpers support (empty path).Note that jade does not support loading helpers this way.*/ + helpersPath?: string; + /**relativeTo - a base path used as prefix for path and partialsPath.No default.*/ + relativeTo?: string; + + /**layout - if set to true or a layout filename, layout support is enabled.A layout is a single template file used as the parent template for other view templates in the same engine.If true, the layout template name must be 'layout.ext' where 'ext' is the engine's extension. Otherwise, the provided filename is suffixed with the engine's extension and loaded.Disable layout when using Jade as it will handle including any layout files independently.Defaults to false.*/ + layout?: boolean; + /**layoutPath - the root file path where layout templates are located (using the relativeTo prefix if present). Defaults to path.*/ + layoutPath?: string; + /**layoutKeyword - the key used by the template engine to denote where primary template content should go.Defaults to 'content'.*/ + layoutKeywork?: string; + /**encoding - the text encoding used by the templates when reading the files and outputting the result.Defaults to 'utf8'.*/ + encoding?: string; + /**isCached - if set to false, templates will not be cached (thus will be read from file on every use).Defaults to true.*/ + isCached?: boolean; + /**allowAbsolutePaths - if set to true, allows absolute template paths passed to reply.view().Defaults to false.*/ + allowAbsolutePaths?: boolean; + /**allowInsecureAccess - if set to true, allows template paths passed to reply.view() to contain '../'.Defaults to false.*/ + allowInsecureAccess?: boolean; + /**compileOptions - options object passed to the engine's compile function. Defaults to empty options {}.*/ + compileOptions?: any; + /**runtimeOptions - options object passed to the returned function from the compile operation.Defaults to empty options {}.*/ + runtimeOptions?: any; + /**contentType - the content type of the engine results.Defaults to 'text/html'.*/ + contentType?: string; + /**compileMode - specify whether the engine compile() method is 'sync' or 'async'.Defaults to 'sync'.*/ + compileMode?: string; + /**context - a global context used with all templates.The global context option can be either an object or a function that takes no arguments and returns a context object.When rendering views, the global context will be merged with any context object specified on the handler or using reply.view().When multiple context objects are used, values from the global context always have lowest precedence.*/ + context?: any; + } + + export interface IServerViewsEnginesOptions extends IServerViewsAdditionalOptions { + /**- the npm module used for rendering the templates.The module object must contain: "module", the rendering function. The required function signature depends on the compileMode settings. + * If the compileMode is 'sync', the signature is compile(template, options), the return value is a function with signature function(context, options), and the method is allowed to throw errors.If the compileMode is 'async', the signature is compile(template, options, callback) where callback has the signature function(err, compiled) where compiled is a function with signature function(context, options, callback) and callback has the signature function(err, rendered).*/ + module: { + compile?(template: any, options: any): (context: any, options: any) => void; + compile?(template: any, options: any, callback: (err: any, compiled: (context: any, options: any, callback: (err: any, rendered: any) => void) => void) => void): void; + }; + } + + /**Initializes the server views manager + var Hapi = require('hapi'); + var server = new Hapi.Server(); + + server.views({ + engines: { + html: require('handlebars'), + jade: require('jade') + }, + path: '/static/templates' + }); + When server.views() is called within a plugin, the views manager is only available to plugins methods. + */ + export interface IServerViewsConfiguration extends IServerViewsAdditionalOptions { + /** - required object where each key is a file extension (e.g. 'html', 'hbr'), mapped to the npm module used for rendering the templates.Alternatively, the extension can be mapped to an object with the following options:*/ + engines: IDictionary | IServerViewsEnginesOptions; + /** defines the default filename extension to append to template names when multiple engines are configured and not explicit extension is provided for a given template. No default value.*/ + defaultExtension?: string; + } + + /** Concludes the handler activity by setting a response and returning control over to the framework where: + erran optional error response. + resultan optional response payload. + Since an request can only have one response regardless if it is an error or success, the reply() method can only result in a single response value. This means that passing both an err and result will only use the err. There is no requirement for either err or result to be (or not) an Error object. The framework will simply use the first argument if present, otherwise the second. The method supports two arguments to be compatible with the common callback pattern of error first. + FLOW CONTROL: + When calling reply(), the framework waits until process.nextTick() to continue processing the request and transmit the response. This enables making changes to the returned response object before the response is sent. This means the framework will resume as soon as the handler method exits. To suspend this behavior, the returned response object supports the following methods: hold(), send() */ + export interface IReply { + (err: Error, + result?: string | number | boolean | Buffer | stream.Stream | IPromise | T, + /** Note that when used to return both an error and credentials in the authentication methods, reply() must be called with three arguments function(err, null, data) where data is the additional authentication information. */ + credentialData?: any + ): IBoom; + /** Note that if result is a Stream with a statusCode property, that status code will be used as the default response code. */ + (result: string | number | boolean | Buffer | stream.Stream | IPromise | T): Response; + + /** Returns control back to the framework without setting a response. If called in the handler, the response defaults to an empty payload with status code 200. + * The data argument is only used for passing back authentication data and is ignored elsewhere. */ + continue(credentialData?: any): void; + + /** Transmits a file from the file system. The 'Content-Type' header defaults to the matching mime type based on filename extension. The response flow control rules do not apply. */ + file( + /** the file path. */ + path: string, + /** optional settings: */ + options?: { + /** - an optional filename to specify if sending a 'Content-Disposition' header, defaults to the basename of path*/ + filename?: string; + /** specifies whether to include the 'Content-Disposition' header with the response. Available values: + false - header is not included. This is the default value. + 'attachment' + 'inline'*/ + mode?: boolean | string; + /** if true, looks for the same filename with the '.gz' suffix for a pre-compressed version of the file to serve if the request supports content encoding. Defaults to false. */ + lookupCompressed: boolean; + }): void; + /** Concludes the handler activity by returning control over to the router with a templatized view response. + the response flow control rules apply. */ + + view( + /** the template filename and path, relative to the templates path configured via the server views manager. */ + template: string, + /** optional object used by the template to render context-specific result. Defaults to no context {}. */ + context?: {}, + /** optional object used to override the server's views manager configuration for this response. Cannot override isCached, partialsPath, or helpersPath which are only loaded at initialization. */ + options?: any): Response; + /** Concludes the handler activity by returning control over to the router and informing the router that a response has already been sent back directly via request.raw.res and that no further response action is needed + The response flow control rules do not apply. */ + close(options?: { + /** if false, the router will not call request.raw.res.end()) to ensure the response was ended. Defaults to true. */ + end?: boolean; + }): void; + /** Proxies the request to an upstream endpoint. + the response flow control rules do not apply. */ + + proxy(/** an object including the same keys and restrictions defined by the route proxy handler options. */ + options: IProxyHandlerConfig): void; + /** Redirects the client to the specified uri. Same as calling reply().redirect(uri). + he response flow control rules apply. */ + redirect(uri: string): ResponseRedirect; + } + + export interface ISessionHandler { + (request: Request, reply: IReply): void; + } + export interface IRequestHandler { + (request: Request): T; + } + + + export interface IFailAction { + (source: string, error: any, next: () => void): void + } + /** generates a reverse proxy handler */ + export interface IProxyHandlerConfig { + /** the upstream service host to proxy requests to. The same path on the client request will be used as the path on the host.*/ + host?: string; + /** the upstream service port. */ + port?: number; + /** The protocol to use when making a request to the proxied host: + 'http' + 'https'*/ + protocol?: string; + /** an absolute URI used instead of the incoming host, port, protocol, path, and query. Cannot be used with host, port, protocol, or mapUri.*/ + uri?: string; + /** if true, forwards the headers sent from the client to the upstream service being proxied to, headers sent from the upstream service will also be forwarded to the client. Defaults to false.*/ + passThrough?: boolean; + /** localStatePassThrough - if false, any locally defined state is removed from incoming requests before being passed upstream. This is a security feature to prevent local state (e.g. authentication cookies) from leaking upstream to other servers along with the cookies intended for those servers. This value can be overridden on a per state basis via the server.state() passThrough option. Defaults to false.*/ + localStatePassThrough?: boolean; + /**acceptEncoding - if false, does not pass-through the 'Accept-Encoding' HTTP header which is useful when using an onResponse post-processing to avoid receiving an encoded response (e.g. gzipped). Can only be used together with passThrough. Defaults to true (passing header).*/ + acceptEncoding?: boolean; + /** rejectUnauthorized - sets the rejectUnauthorized property on the https agent making the request. This value is only used when the proxied server uses TLS/SSL. When set it will override the node.js rejectUnauthorized property. If false then ssl errors will be ignored. When true the server certificate is verified and an 500 response will be sent when verification fails. This shouldn't be used alongside the agent setting as the agent will be used instead. Defaults to the https agent default value of true.*/ + rejectUnauthorized?: boolean; + /**if true, sets the 'X-Forwarded-For', 'X-Forwarded-Port', 'X-Forwarded-Proto' headers when making a request to the proxied upstream endpoint. Defaults to false.*/ + xforward?: boolean; + /** the maximum number of HTTP redirections allowed, to be followed automatically by the handler. Set to false or 0 to disable all redirections (the response will contain the redirection received from the upstream service). If redirections are enabled, no redirections (301, 302, 307, 308) will be passed along to the client, and reaching the maximum allowed redirections will return an error response. Defaults to false.*/ + redirects?: boolean | number; + /**number of milliseconds before aborting the upstream request. Defaults to 180000 (3 minutes).*/ + timeout?: number; + /** a function used to map the request URI to the proxied URI. Cannot be used together with host, port, protocol, or uri. The function signature is function(request, callback) where: + request - is the incoming request object. + callback - is function(err, uri, headers) where: + err - internal error condition. + uri - the absolute proxy URI. + headers - optional object where each key is an HTTP request header and the value is the header content.*/ + mapUri?: (request: Request, callback: (err: any, uri: string, headers?: { [key: string]: string }) => void) => void; + /** a custom function for processing the response from the upstream service before sending to the client. Useful for custom error handling of responses from the proxied endpoint or other payload manipulation. Function signature is function(err, res, request, reply, settings, ttl) where: - err - internal or upstream error returned from attempting to contact the upstream proxy. - res - the node response object received from the upstream service. res is a readable stream (use the wreck module read method to easily convert it to a Buffer or string). - request - is the incoming request object. - reply - the reply interface function. - settings - the proxy handler configuration. - ttl - the upstream TTL in milliseconds if proxy.ttl it set to 'upstream' and the upstream response included a valid 'Cache-Control' header with 'max-age'.*/ + onResponse?: ( + err: any, + res: http.ServerResponse, + req: Request, + reply: IReply, + settings: IProxyHandlerConfig, + ttl: number + ) => void; + /** if set to 'upstream', applies the upstream response caching policy to the response using the response.ttl() method (or passed as an argument to the onResponse method if provided).*/ + ttl?: number; + /** - a node http(s) agent to be used for connections to upstream server. see https://nodejs.org/api/http.html#http_class_http_agent */ + agent?: http.Agent; + /** sets the maximum number of sockets available per outgoing proxy host connection. false means use the wreck module default value (Infinity). Does not affect non-proxy outgoing client connections. Defaults to Infinity.*/ + maxSockets?: boolean | number; + } + /** TODO: fill in joi definition */ + export interface IJoi { + + } + /** a validation function using the signature function(value, options, next) */ + export interface IValidationFunction { + + (/** the object containing the path parameters. */ + value: any, + /** the server validation options. */ + options: any, + /** the callback function called when validation is completed. */ + next: (err: any, value: any) => void): void; + } + /** a custom error handler function with the signature 'function(request, reply, source, error)` */ + export interface IRouteFailFunction { + /** a custom error handler function with the signature 'function(request, reply, source, error)` */ + ( + /** - the [request object]. */ + request: Request, + /** the continuation reply interface. */ + reply: IReply, + /** the source of the invalid field (e.g. 'path', 'query', 'payload'). */ + source: string, + /** the error object prepared for the client response (including the validation function error under error.data). */ + error: any): void; + } + + /** Each route can be customize to change the default behavior of the request lifecycle using the following options: */ + export interface IRouteAdditionalConfigurationOptions { + /** application specific configuration.Should not be used by plugins which should use plugins[name] instead. */ + app?: any; + /** authentication configuration.Value can be: false to disable authentication if a default strategy is set. + a string with the name of an authentication strategy registered with server.auth.strategy(). + an object */ + auth?: boolean | string | + { + /** the authentication mode.Defaults to 'required' if a server authentication strategy is configured, otherwise defaults to no authentication.Available values: + 'required'authentication is required. + 'optional'authentication is optional (must be valid if present). + 'try'same as 'optional' but allows for invalid authentication. */ + mode?: string; + /** a string array of strategy names in order they should be attempted.If only one strategy is used, strategy can be used instead with the single string value.Defaults to the default authentication strategy which is available only when a single strategy is configured. */ + strategies?: string | Array; + /** if set, the payload (in requests other than 'GET' and 'HEAD') is authenticated after it is processed.Requires a strategy with payload authentication support (e.g.Hawk).Cannot be set to a value other than 'required' when the scheme sets the options.payload to true.Available values: + falseno payload authentication.This is the default value. + 'required'payload authentication required.This is the default value when the scheme sets options.payload to true. + 'optional'payload authentication performed only when the client includes payload authentication information (e.g.hash attribute in Hawk). */ + payload?: string; + /** the application scope required to access the route.Value can be a scope string or an array of scope strings.The authenticated credentials object scope property must contain at least one of the scopes defined to access the route.Set to false to remove scope requirements.Defaults to no scope required. */ + scope?: string | Array | boolean; + /** the required authenticated entity type.If set, must match the entity value of the authentication credentials.Available values: + anythe authentication can be on behalf of a user or application.This is the default value. + userthe authentication must be on behalf of a user. + appthe authentication must be on behalf of an application. */ + entity?: string; + /** + * an object or array of objects specifying the route access rules. Each rule is evaluated against an incoming + * request and access is granted if at least one rule matches. Each rule object must include at least one of: + */ + access?: IRouteAdditionalConfigurationAuthAccess | IRouteAdditionalConfigurationAuthAccess[]; + }; + /** an object passed back to the provided handler (via this) when called. */ + bind?: any; + /** if the route method is 'GET', the route can be configured to include caching directives in the response using the following options */ + cache?: { + /** mines the privacy flag included in clientside caching using the 'Cache-Control' header.Values are: + fault'no privacy flag.This is the default setting. + 'public'mark the response as suitable for public caching. + 'private'mark the response as suitable only for private caching. */ + privacy: string; + /** relative expiration expressed in the number of milliseconds since the item was saved in the cache.Cannot be used together with expiresAt. */ + expiresIn: number; + /** time of day expressed in 24h notation using the 'HH:MM' format, at which point all cache records for the route expire.Cannot be used together with expiresIn. */ + expiresAt: string; + }; + /** the Cross- Origin Resource Sharing protocol allows browsers to make cross- origin API calls.CORS is required by web applications running inside a browser which are loaded from a different domain than the API server.CORS headers are disabled by default. To enable, set cors to true, or to an object with the following options: */ + cors?: { + /** a strings array of allowed origin servers ('Access-Control-Allow-Origin').The array can contain any combination of fully qualified origins along with origin strings containing a wildcard '' character, or a single `''origin string. Defaults to any origin['*']`. */ + origin?: Array; + /** if true, matches the value of the incoming 'Origin' header to the list of origin values ('*' matches anything) and if a match is found, uses that as the value of the 'Access-Control-Allow-Origin' response header.When false, the origin config is returned as- is.Defaults to true. */ + matchOrigin?: boolean; + /** if false, prevents the connection from returning the full list of non- wildcard origin values if the incoming origin header does not match any of the values.Has no impact if matchOrigin is set to false.Defaults to true. */ + isOriginExposed?: boolean; + /** number of seconds the browser should cache the CORS response ('Access-Control-Max-Age').The greater the value, the longer it will take before the browser checks for changes in policy.Defaults to 86400 (one day). */ + maxAge?: number; + /** a strings array of allowed headers ('Access-Control-Allow-Headers').Defaults to ['Authorization', 'Content-Type', 'If-None-Match']. */ + headers?: string[]; + /** a strings array of additional headers to headers.Use this to keep the default headers in place. */ + additionalHeaders?: string[]; + /** a strings array of allowed HTTP methods ('Access-Control-Allow-Methods').Defaults to ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'OPTIONS']. */ + methods?: string[]; + /** a strings array of additional methods to methods.Use this to keep the default methods in place. */ + additionalMethods?: string[]; + /** a strings array of exposed headers ('Access-Control-Expose-Headers').Defaults to ['WWW-Authenticate', 'Server-Authorization']. */ + exposedHeaders?: string[]; + /** a strings array of additional headers to exposedHeaders.Use this to keep the default headers in place. */ + additionalExposedHeaders?: string[]; + /** if true, allows user credentials to be sent ('Access-Control-Allow-Credentials').Defaults to false. */ + credentials?: boolean; + /** if false, preserves existing CORS headers set manually before the response is sent.Defaults to true. */ + override?: boolean; + }; + /** defines the behavior for serving static resources using the built-in route handlers for files and directories: */ + files?: {/** determines the folder relative paths are resolved against when using the file and directory handlers. */ + relativeTo: string; + }; + + /** an alternative location for the route handler option. */ + handler?: ISessionHandler | string | IRouteHandlerConfig; + /** an optional unique identifier used to look up the route using server.lookup(). */ + id?: number; + /** optional arguments passed to JSON.stringify() when converting an object or error response to a string payload.Supports the following: */ + json?: { + /** the replacer function or array.Defaults to no action. */ + replacer?: Function | string[]; + /** number of spaces to indent nested object keys.Defaults to no indentation. */ + space?: number | string; + /** string suffix added after conversion to JSON string.Defaults to no suffix. */ + suffix?: string; + }; + /** enables JSONP support by setting the value to the query parameter name containing the function name used to wrap the response payload.For example, if the value is 'callback', a request comes in with 'callback=me', and the JSON response is '{ "a":"b" }', the payload will be 'me({ "a":"b" });'.Does not work with stream responses. */ + jsonp?: string; + /** determines how the request payload is processed: */ + payload?: { + /** the type of payload representation requested. The value must be one of: + 'data'the incoming payload is read fully into memory.If parse is true, the payload is parsed (JSON, formdecoded, multipart) based on the 'Content- Type' header.If parse is false, the raw Buffer is returned.This is the default value except when a proxy handler is used. + 'stream'the incoming payload is made available via a Stream.Readable interface.If the payload is 'multipart/form-data' and parse is true, fields values are presented as text while files are provided as streams.File streams from a 'multipart/form-data' upload will also have a property hapi containing filename and headers properties. + 'file'the incoming payload in written to temporary file in the directory specified by the server's payload.uploads settings. If the payload is 'multipart/ formdata' and parse is true, fields values are presented as text while files are saved. Note that it is the sole responsibility of the application to clean up the files generated by the framework. This can be done by keeping track of which files are used (e.g. using the request.app object), and listening to the server 'response' event to perform any needed cleaup. */ + output?: string; + /** can be true, false, or gunzip; determines if the incoming payload is processed or presented raw. true and gunzip includes gunzipping when the appropriate 'Content-Encoding' is specified on the received request. If parsing is enabled and the 'Content-Type' is known (for the whole payload as well as parts), the payload is converted into an object when possible. If the format is unknown, a Bad Request (400) error response is sent. Defaults to true, except when a proxy handler is used. The supported mime types are: + 'application/json' + 'application/x-www-form-urlencoded' + 'application/octet-stream' + 'text/ *' + 'multipart/form-data' */ + parse?: string | boolean; + /** a string or an array of strings with the allowed mime types for the endpoint.Defaults to any of the supported mime types listed above.Note that allowing other mime types not listed will not enable them to be parsed, and that if parsing mode is 'parse', the request will result in an error response. */ + allow?: string | string[]; + /** a mime type string overriding the 'Content-Type' header value received.Defaults to no override. */ + override?: string; + /** limits the size of incoming payloads to the specified byte count.Allowing very large payloads may cause the server to run out of memory.Defaults to 1048576 (1MB). */ + maxBytes?: number; + /** payload reception timeout in milliseconds.Sets the maximum time allowed for the client to transmit the request payload (body) before giving up and responding with a Request Timeout (408) error response.Set to false to disable.Defaults to 10000 (10 seconds). */ + timeout?: number; + /** the directory used for writing file uploads.Defaults to os.tmpDir(). */ + uploads?: string; + /** determines how to handle payload parsing errors. Allowed values are: + 'error'return a Bad Request (400) error response. This is the default value. + 'log'report the error but continue processing the request. + 'ignore'take no action and continue processing the request. */ + failAction?: string; + }; + /** pluginspecific configuration.plugins is an object where each key is a plugin name and the value is the plugin configuration. */ + plugins?: IDictionary; + /** an array with [route prerequisites] methods which are executed in serial or in parallel before the handler is called. */ + pre?: any[]; + /** validation rules for the outgoing response payload (response body).Can only validate object response: */ + response?: { + /** the default HTTP status code when the payload is empty. Value can be 200 or 204. + Note that a 200 status code is converted to a 204 only at the time or response transmission + (the response status code will remain 200 throughout the request lifecycle unless manually set). Defaults to 200. */ + emptyStatusCode?: number; + /** the default response object validation rules (for all non-error responses) expressed as one of: + true - any payload allowed (no validation performed). This is the default. + false - no payload allowed. + a Joi validation object. + a validation function using the signature function(value, options, next) where: + value - the object containing the response object. + options - the server validation options. + next(err) - the callback function called when validation is completed. */ + schema?: boolean | any; + /** HTTP status- codespecific validation rules.The status key is set to an object where each key is a 3 digit HTTP status code and the value has the same definition as schema.If a response status code is not present in the status object, the schema definition is used, expect for errors which are not validated by default. */ + status?: { [statusCode: number] : boolean | any }; + /** the percent of responses validated (0100).Set to 0 to disable all validation.Defaults to 100 (all responses). */ + sample?: number; + /** defines what to do when a response fails validation.Options are: + errorreturn an Internal Server Error (500) error response.This is the default value. + loglog the error but send the response. */ + failAction?: string; + /** if true, applies the validation rule changes to the response.Defaults to false. */ + modify?: boolean; + /** options to pass to Joi.Useful to set global options such as stripUnknown or abortEarly (the complete list is available here: https://github.com/hapijs/joi#validatevalue-schema-options-callback ).Defaults to no options. */ + options?: any; + }; + /** sets common security headers (disabled by default).To enable set security to true or to an object with the following options */ + security?: boolean | { + /** controls the 'Strict-Transport-Security' header.If set to true the header will be set to max- age=15768000, if specified as a number the maxAge parameter will be set to that number.Defaults to true.You may also specify an object with the following fields: */ + hsts?: boolean | number | { + /** the max- age portion of the header, as a number.Default is 15768000. */ + maxAge?: number; + /** a boolean specifying whether to add the includeSubdomains flag to the header. */ + includeSubdomains?: boolean; + /** a boolean specifying whether to add the 'preload' flag (used to submit domains inclusion in Chrome's HTTP Strict Transport Security (HSTS) preload list) to the header. */ + preload?: boolean; + }; + /** controls the 'X-Frame-Options' header.When set to true the header will be set to DENY, you may also specify a string value of 'deny' or 'sameorigin'.To use the 'allow-from' rule, you must set this to an object with the following fields: */ + xframe?: { + /** either 'deny', 'sameorigin', or 'allow-from' */ + rule: string; + /** when rule is 'allow-from' this is used to form the rest of the header, otherwise this field is ignored.If rule is 'allow-from' but source is unset, the rule will be automatically changed to 'sameorigin'. */ + source: string; + }; + /** boolean that controls the 'X-XSS-PROTECTION' header for IE.Defaults to true which sets the header to equal '1; mode=block'.NOTE: This setting can create a security vulnerability in versions of IE below 8, as well as unpatched versions of IE8.See here and here for more information.If you actively support old versions of IE, it may be wise to explicitly set this flag to false. */ + xss?: boolean; + /** boolean controlling the 'X-Download-Options' header for IE, preventing downloads from executing in your context.Defaults to true setting the header to 'noopen'. */ + noOpen?: boolean; + /** boolean controlling the 'X-Content-Type-Options' header.Defaults to true setting the header to its only and default option, 'nosniff'. */ + noSniff?: boolean; + }; + /** HTTP state management (cookies) allows the server to store information on the client which is sent back to the server with every request (as defined in RFC 6265).state supports the following options: */ + state?: { + /** determines if incoming 'Cookie' headers are parsed and stored in the request.state object.Defaults to true. */ + parse: boolean; + /** determines how to handle cookie parsing errors.Allowed values are: + 'error'return a Bad Request (400) error response.This is the default value. + 'log'report the error but continue processing the request. + 'ignore'take no action. */ + failAction: string; + }; + /** request input validation rules for various request components.When using a Joi validation object, the values of the other inputs (i.e.headers, query, params, payload, and auth) are made available under the validation context (accessible in rules as Joi.ref('$query.key')).Note that validation is performed in order(i.e.headers, params, query, payload) and if type casting is used (converting a string to number), the value of inputs not yet validated will reflect the raw, unvalidated and unmodified values.The validate object supports: */ + validate?: { + /** validation rules for incoming request headers.Values allowed: + * trueany headers allowed (no validation performed).This is the default. + falseno headers allowed (this will cause all valid HTTP requests to fail). + a Joi validation object. + a validation function using the signature function(value, options, next) where: + valuethe object containing the request headers. + optionsthe server validation options. + next(err, value)the callback function called when validation is completed. + */ + headers?: boolean | IJoi | IValidationFunction; + + + /** validation rules for incoming request path parameters, after matching the path against the route and extracting any parameters then stored in request.params.Values allowed: + trueany path parameters allowed (no validation performed).This is the default. + falseno path variables allowed. + a Joi validation object. + a validation function using the signature function(value, options, next) where: + valuethe object containing the path parameters. + optionsthe server validation options. + next(err, value)the callback function called when validation is completed. */ + params?: boolean | IJoi | IValidationFunction; + /** validation rules for an incoming request URI query component (the key- value part of the URI between '?' and '#').The query is parsed into its individual key- value pairs (using the qs module) and stored in request.query prior to validation.Values allowed: + trueany query parameters allowed (no validation performed).This is the default. + falseno query parameters allowed. + a Joi validation object. + a validation function using the signature function(value, options, next) where: + valuethe object containing the query parameters. + optionsthe server validation options. + next(err, value)the callback function called when validation is completed. */ + query?: boolean | IJoi | IValidationFunction; + /** validation rules for an incoming request payload (request body).Values allowed: + trueany payload allowed (no validation performed).This is the default. + falseno payload allowed. + a Joi validation object. + a validation function using the signature function(value, options, next) where: + valuethe object containing the payload object. + optionsthe server validation options. + next(err, value)the callback function called when validation is completed. */ + payload?: boolean | IJoi | IValidationFunction; + /** an optional object with error fields copied into every validation error response. */ + errorFields?: any; + /** determines how to handle invalid requests.Allowed values are: + 'error'return a Bad Request (400) error response.This is the default value. + 'log'log the error but continue processing the request. + 'ignore'take no action. + OR a custom error handler function with the signature 'function(request, reply, source, error)` where: + requestthe request object. + replythe continuation reply interface. + sourcethe source of the invalid field (e.g. 'path', 'query', 'payload'). + errorthe error object prepared for the client response (including the validation function error under error.data). */ + failAction?: string | IRouteFailFunction; + /** options to pass to Joi.Useful to set global options such as stripUnknown or abortEarly (the complete list is available here: https://github.com/hapijs/joi#validatevalue-schema-options-callback ).Defaults to no options. */ + options?: any; + }; + /** define timeouts for processing durations: */ + timeout?: { + /** response timeout in milliseconds.Sets the maximum time allowed for the server to respond to an incoming client request before giving up and responding with a Service Unavailable (503) error response.Disabled by default (false). */ + server: boolean | number; + /** by default, node sockets automatically timeout after 2 minutes.Use this option to override this behavior.Defaults to undefined which leaves the node default unchanged.Set to false to disable socket timeouts. */ + socket: boolean | number; + }; + + /** ONLY WHEN ADDING NEW ROUTES (not when setting defaults). + *route description used for generating documentation (string). + */ + description?: string; + /** ONLY WHEN ADDING NEW ROUTES (not when setting defaults). + *route notes used for generating documentation (string or array of strings). + */ + notes?: string | string[]; + /** ONLY WHEN ADDING NEW ROUTES (not when setting defaults). + *route tags used for generating documentation (array of strings). + */ + tags?: string[] + } + + /** + * specifying the route access rules. Each rule is evaluated against an incoming request and access is granted if at least one rule matches + */ + export interface IRouteAdditionalConfigurationAuthAccess { + /** + * the application scope required to access the route. Value can be a scope string or an array of scope strings. + * The authenticated credentials object scope property must contain at least one of the scopes defined to access the route. + * If a scope string begins with a + character, that scope is required. If a scope string begins with a ! character, + * that scope is forbidden. For example, the scope ['!a', '+b', 'c', 'd'] means the incoming request credentials' + * scope must not include 'a', must include 'b', and must include on of 'c' or 'd'. You may also access properties + * on the request object (query and params} to populate a dynamic scope by using {} characters around the property name, + * such as 'user-{params.id}'. Defaults to false (no scope requirements). + */ + scope?: string | Array | boolean; + /** the required authenticated entity type. If set, must match the entity value of the authentication credentials. Available values: + * any - the authentication can be on behalf of a user or application. This is the default value. + * user - the authentication must be on behalf of a user which is identified by the presence of a user attribute in the credentials object returned by the authentication strategy. + * app - the authentication must be on behalf of an application which is identified by the lack of presence of a user attribute in the credentials object returned by the authentication strategy. + */ + entity?: string; + } + + /** server.realm http://hapijs.com/api#serverrealm + The realm object contains server-wide or plugin-specific state that can be shared across various methods. For example, when calling server.bind(), + the active realm settings.bind property is set which is then used by routes and extensions added at the same level (server root or plugin). + Realms are a limited version of a sandbox where plugins can maintain state used by the framework when adding routes, extensions, and other properties. + The server.realm object should be considered read-only and must not be changed directly except for the plugins property can be directly manipulated by the plugins (each setting its own under plugins[name]). + exports.register = function (server, options, next) { + console.log(server.realm.modifiers.route.prefix); + return next(); + }; + */ + export interface IServerRealm { + /** when the server object is provided as an argument to the plugin register() method, modifiers provides the registration preferences passed the server.register() method */ + modifiers: { + /** routes preferences: */ + route: { + /** - the route path prefix used by any calls to server.route() from the server. */ + prefix: string; + /** the route virtual host settings used by any calls to server.route() from the server. */ + vhost: string; + }; + + }; + /** the active plugin name (empty string if at the server root). */ + plugin: string; + /** plugin-specific state to be shared only among activities sharing the same active state. plugins is an object where each key is a plugin name and the value is the plugin state. */ + plugins: IDictionary; + /** settings overrides */ + settings: { + files: { + relativeTo: any; + }; + bind: any; + } + } + /** server.state(name, [options]) http://hapijs.com/api#serverstatename-options + HTTP state management uses client cookies to persist a state across multiple requests. Registers a cookie definitions where:*/ + export interface IServerState { +/** - the cookie name string. */name: string; + +/** - are the optional cookie settings: */options: { +/** - time - to - live in milliseconds.Defaults to null (session time- life - cookies are deleted when the browser is closed).*/ttl: number; +/** - sets the 'Secure' flag.Defaults to false.*/isSecure: boolean; +/** - sets the 'HttpOnly' flag.Defaults to false.*/isHttpOnly: boolean +/** - the path scope.Defaults to null (no path).*/path: any; +/** - the domain scope.Defaults to null (no domain). */domain: any; + /** if present and the cookie was not received from the client or explicitly set by the route handler, the cookie is automatically added to the response with the provided value. The value can be a function with signature function(request, next) where: + request - the request object. + next - the continuation function using the function(err, value) signature.*/ + autoValue: (request: Request, next: (err: any, value: any) => void) => void; + /** - encoding performs on the provided value before serialization. Options are: + 'none' - no encoding. When used, the cookie value must be a string. This is the default value. + 'base64' - string value is encoded using Base64. + 'base64json' - object value is JSON-stringified than encoded using Base64. + 'form' - object value is encoded using the x-www-form-urlencoded method. + 'iron' - Encrypts and sign the value using iron.*/ + encoding: string; +/** - an object used to calculate an HMAC for cookie integrity validation.This does not provide privacy, only a mean to verify that the cookie value was generated by the server.Redundant when 'iron' encoding is used.Options are:*/sign: { +/** - algorithm options.Defaults to require('iron').defaults.integrity.*/integrity: any; +/** - password used for HMAC key generation.*/password: string; + }; +/** - password used for 'iron' encoding.*/password: string; +/** - options for 'iron' encoding.Defaults to require('iron').defaults.*/iron: any; +/** - if false, errors are ignored and treated as missing cookies.*/ignoreErrors: boolean; +/** - if true, automatically instruct the client to remove invalid cookies.Defaults to false.*/clearInvalid: boolean; +/** - if false, allows any cookie value including values in violation of RFC 6265. Defaults to true.*/strictHeader: boolean; +/** - overrides the default proxy localStatePassThrough setting.*/passThrough: any; + }; + } + + export interface IFileHandlerConfig { + /** a path string or function as described above.*/ + path: string; + /** an optional filename to specify if sending a 'Content-Disposition' header, defaults to the basename of path*/ + filename?: string; + /**- specifies whether to include the 'Content-Disposition' header with the response. Available values: + false - header is not included. This is the default value. + 'attachment' + 'inline'*/ + mode?: boolean | string; + /** if true, looks for the same filename with the '.gz' suffix for a pre-compressed version of the file to serve if the request supports content encoding. Defaults to false.*/ + lookupCompressed: boolean; + } + + /**http://hapijs.com/api#route-handler + Built-in handlers + + The framework comes with a few built-in handler types available by setting the route handler config to an object containing one of these keys.*/ + export interface IRouteHandlerConfig { + /** generates a static file endpoint for serving a single file. file can be set to: + a relative or absolute file path string (relative paths are resolved based on the route files configuration). + a function with the signature function(request) which returns the relative or absolute file path. + an object with the following options */ + file?: string | IRequestHandler | IFileHandlerConfig; + /** directory - generates a directory endpoint for serving static content from a directory. Routes using the directory handler must include a path parameter at the end of the path string (e.g. /path/to/somewhere/{param} where the parameter name does not matter). The path parameter can use any of the parameter options (e.g. {param} for one level files only, {param?} for one level files or the directory root, {param*} for any level, or {param*3} for a specific level). If additional path parameters are present, they are ignored for the purpose of selecting the file system resource. The directory handler is an object with the following options: + path - (required) the directory root path (relative paths are resolved based on the route files configuration). Value can be: + a single path string used as the prefix for any resources requested by appending the request path parameter to the provided string. + an array of path strings. Each path will be attempted in order until a match is found (by following the same process as the single path string). + a function with the signature function(request) which returns the path string or an array of path strings. If the function returns an error, the error is passed back to the client in the response. + index - optional boolean|string|string[], determines if an index file will be served if found in the folder when requesting a directory. The given string or strings specify the name(s) of the index file to look for. If true, looks for 'index.html'. Any falsy value disables index file lookup. Defaults to true. + listing - optional boolean, determines if directory listing is generated when a directory is requested without an index document. Defaults to false. + showHidden - optional boolean, determines if hidden files will be shown and served. Defaults to false. + redirectToSlash - optional boolean, determines if requests for a directory without a trailing slash are redirected to the same path with the missing slash. Useful for ensuring relative links inside the response are resolved correctly. Disabled when the server config router.stripTrailingSlash is true.Defaults to false. + lookupCompressed - optional boolean, instructs the file processor to look for the same filename with the '.gz' suffix for a pre-compressed version of the file to serve if the request supports content encoding. Defaults to false. + defaultExtension - optional string, appended to file requests if the requested file is not found. Defaults to no extension.*/ + directory?: { + path: string | Array | IRequestHandler | IRequestHandler>; + index?: boolean | string | string[]; + listing?: boolean; + showHidden?: boolean; + redirectToSlash?: boolean; + lookupCompressed?: boolean; + defaultExtension?: string; + }; + proxy?: IProxyHandlerConfig; + view?: string | { + template: string; + context: { + payload: any; + params: any; + query: any; + pre: any; + } + }; + config?: { + handler: any; + bind: any; + app: any; + plugins: { + [name: string]: any; + }; + pre: Array<() => void>; + validate: { + headers: any; + params: any; + query: any; + payload: any; + errorFields?: any; + failAction?: string | IFailAction; + }; + payload: { + output: { + data: any; + stream: any; + file: any; + }; + parse?: any; + allow?: string | Array; + override?: string; + maxBytes?: number; + uploads?: number; + failAction?: string; + }; + response: { + schema: any; + sample: number; + failAction: string; + }; + cache: { + privacy: string; + expiresIn: number; + expiresAt: number; + }; + auth: string | boolean | { + mode: string; + strategies: Array; + payload?: boolean | string; + tos?: boolean | string; + scope?: string | Array; + entity: string; + }; + cors?: boolean; + jsonp?: string; + description?: string; + notes?: string | Array; + tags?: Array; + }; + } + /** Route configuration + The route configuration object*/ + export interface IRouteConfiguration { + /** - (required) the absolute path used to match incoming requests (must begin with '/'). Incoming requests are compared to the configured paths based on the connection router configuration option.The path can include named parameters enclosed in {} which will be matched against literal values in the request as described in Path parameters.*/ + path: string; + /** - (required) the HTTP method.Typically one of 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', or 'OPTIONS'.Any HTTP method is allowed, except for 'HEAD'.Use '*' to match against any HTTP method (only when an exact match was not found, and any match with a specific method will be given a higher priority over a wildcard match). + * Can be assigned an array of methods which has the same result as adding the same route with different methods manually.*/ + method: string | string[]; + /** - an optional domain string or an array of domain strings for limiting the route to only requests with a matching host header field.Matching is done against the hostname part of the header only (excluding the port).Defaults to all hosts.*/ + vhost?: string; + /** - (required) the function called to generate the response after successful authentication and validation.The handler function is described in Route handler.If set to a string, the value is parsed the same way a prerequisite server method string shortcut is processed.Alternatively, handler can be assigned an object with a single key using the name of a registered handler type and value with the options passed to the registered handler.*/ + handler: ISessionHandler | string | IRouteHandlerConfig; + /** - additional route options.*/ + config?: IRouteAdditionalConfigurationOptions; + } + /** Route public interface When route information is returned or made available as a property. http://hapijs.com/api#route-public-interface */ + export interface IRoute { + + + /** the route HTTP method. */ + method: string; + /** the route path. */ + path: string; + /** the route vhost option if configured. */ + vhost?: string | Array; + /** the [active realm] associated with the route.*/ + realm: IServerRealm; + /** the [route options] object with all defaults applied. */ + settings: IRouteAdditionalConfigurationOptions; + } + + export interface IServerAuthScheme { + /** authenticate(request, reply) - required function called on each incoming request configured with the authentication scheme where: + request - the request object. + reply - the reply interface the authentication method must call when done authenticating the request where: + reply(err, response, result) - is called if authentication failed where: + err - any authentication error. + response - any authentication response action such as redirection. Ignored if err is present, otherwise required. + result - an object containing: + credentials - the authenticated credentials. + artifacts - optional authentication artifacts. + reply.continue(result) - is called if authentication succeeded where: + result - same object as result above. + When the scheme authenticate() method implementation calls reply() with an error condition, the specifics of the error affect whether additional authentication strategies will be attempted if configured for the route. + .If the err returned by the reply() method includes a message, no additional strategies will be attempted. + If the err does not include a message but does include a scheme name (e.g. Boom.unauthorized(null, 'Custom')), additional strategies will be attempted in order of preference. + var server = new Hapi.Server(); + server.connection({ port: 80 }); + var scheme = function (server, options) { + return { + authenticate: function (request, reply) { + var req = request.raw.req; + var authorization = req.headers.authorization; + if (!authorization) { + return reply(Boom.unauthorized(null, 'Custom')); + } + return reply(null, { credentials: { user: 'john' } }); + } + }; + }; + server.auth.scheme('custom', scheme);*/ + authenticate(request: Request, reply: IReply): void; + /** payload(request, reply) - optional function called to authenticate the request payload where: + request - the request object. + reply(err, response) - is called if authentication failed where: + err - any authentication error. + response - any authentication response action such as redirection. Ignored if err is present, otherwise required. + reply.continue() - is called if payload authentication succeeded. + When the scheme payload() method returns an error with a message, it means payload validation failed due to bad payload. If the error has no message but includes a scheme name (e.g. Boom.unauthorized(null, 'Custom')), authentication may still be successful if the route auth.payload configuration is set to 'optional'.*/ + payload?(request: Request, reply: IReply): void; + /** response(request, reply) - optional function called to decorate the response with authentication headers before the response headers or payload is written where: + request - the request object. + reply(err, response) - is called if an error occurred where: + err - any authentication error. + response - any authentication response to send instead of the current response. Ignored if err is present, otherwise required. + reply.continue() - is called if the operation succeeded.*/ + response?(request: Request, reply: IReply): void; + /** an optional object */ + options?: { + /** if true, requires payload validation as part of the scheme and forbids routes from disabling payload auth validation. Defaults to false.*/ + payload: boolean; + } + } + + + + export interface IServerInject { + (options: string | { + /** the request HTTP method (e.g. 'POST'). Defaults to 'GET'.*/ + method: string; + /** the request URL. If the URI includes an authority (e.g. 'example.com:8080'), it is used to automatically set an HTTP 'Host' header, unless one was specified in headers.*/ + url: string; + /** an object with optional request headers where each key is the header name and the value is the header content. Defaults to no additions to the default Shot headers.*/ + headers?: IDictionary; + /** n optional string, buffer or object containing the request payload. In case of an object it will be converted to a string for you. Defaults to no payload. Note that payload processing defaults to 'application/json' if no 'Content-Type' header provided.*/ + payload?: string | {} | Buffer; + /** an optional credentials object containing authentication information. The credentials are used to bypass the default authentication strategies, and are validated directly as if they were received via an authentication scheme. Defaults to no credentials.*/ + credentials?: any; + /** an optional artifacts object containing authentication artifact information. The artifacts are used to bypass the default authentication strategies, and are validated directly as if they were received via an authentication scheme. Ignored if set without credentials. Defaults to no artifacts.*/ + artifacts?: any; + /** sets the initial value of request.app*/ + app?: any; + /** sets the initial value of request.plugins*/ + plugins?: any; + /** allows access to routes with config.isInternal set to true. Defaults to false.*/ + allowInternals?: boolean; + /** sets the remote address for the incoming connection.*/ + remoteAddress?: boolean; + /**object with options used to simulate client request stream conditions for testing: + error - if true, emits an 'error' event after payload transmission (if any). Defaults to false. + close - if true, emits a 'close' event after payload transmission (if any). Defaults to false. + end - if false, does not end the stream. Defaults to true.*/ + simulate?: { + error: boolean; + close: boolean; + end: boolean; + }; + }, + callback: ( + /**the response object where: + statusCode - the HTTP status code. + headers - an object containing the headers set. + payload - the response payload string. + rawPayload - the raw response payload buffer. + raw - an object with the injection request and response objects: + req - the simulated node request object. + res - the simulated node response object. + result - the raw handler response (e.g. when not a stream or a view) before it is serialized for transmission. If not available, the value is set to payload. Useful for inspection and reuse of the internal objects returned (instead of parsing the response string). + request - the request object.*/ + res: { statusCode: number; headers: IDictionary; payload: string; rawPayload: Buffer; raw: { req: http.ClientRequest; res: http.ServerResponse }; result: string; request: Request }) => void + ): void; + + } + + + /** host - optional host to filter routes matching a specific virtual host. Defaults to all virtual hosts. + The return value is an array where each item is an object containing: + info - the connection.info the connection the table was generated for. + labels - the connection labels. + table - an array of routes where each route contains: + settings - the route config with defaults applied. + method - the HTTP method in lower case. + path - the route path.*/ + export interface IConnectionTable { + info: any; + labels: any; + table: IRoute[]; + } + + export interface ICookieSettings { + /** - time - to - live in milliseconds.Defaults to null (session time- life - cookies are deleted when the browser is closed).*/ + ttl?: number; + /** - sets the 'Secure' flag.Defaults to false.*/ + isSecure?: boolean; + /** - sets the 'HttpOnly' flag.Defaults to false.*/ + isHttpOnly?: boolean; + /** - the path scope.Defaults to null (no path).*/ + path?: string; + /** - the domain scope.Defaults to null (no domain).*/ + domain?: any; + /** - if present and the cookie was not received from the client or explicitly set by the route handler, the cookie is automatically added to the response with the provided value.The value can be a function with signature function(request, next) where: + request - the request object. + next - the continuation function using the function(err, value) signature.*/ + autoValue?: (request: Request, next: (err: any, value: any) => void) => void; + /** - encoding performs on the provided value before serialization.Options are: + 'none' - no encoding.When used, the cookie value must be a string.This is the default value. + 'base64' - string value is encoded using Base64. + 'base64json' - object value is JSON- stringified than encoded using Base64. + 'form' - object value is encoded using the x- www - form - urlencoded method. */ + encoding?: string; + /** - an object used to calculate an HMAC for cookie integrity validation.This does not provide privacy, only a mean to verify that the cookie value was generated by the server.Redundant when 'iron' encoding is used.Options are: + integrity - algorithm options.Defaults to require('iron').defaults.integrity. + password - password used for HMAC key generation. */ + sign?: { integrity: any; password: string; } + password?: string; + iron?: any; + ignoreErrors?: boolean; + clearInvalid?: boolean; + strictHeader?: boolean; + passThrough?: any; + } + + /** method - the method function with the signature is one of: + function(arg1, arg2, ..., argn, next) where: + arg1, arg2, etc. - the method function arguments. + next - the function called when the method is done with the signature function(err, result, ttl) where: + err - error response if the method failed. + result - the return value. + ttl - 0 if result is valid but cannot be cached. Defaults to cache policy. + function(arg1, arg2, ..., argn) where: + arg1, arg2, etc. - the method function arguments. + the callback option is set to false. + the method must returns a value (result, Error, or a promise) or throw an Error.*/ + export interface IServerMethod { + //(): void; + //(next: (err: any, result: any, ttl: number) => void): void; + //(arg1: any): void; + //(arg1: any, arg2: any, next: (err: any, result: any, ttl: number) => void): void; + //(arg1: any, arg2: any): void; + (...args: any[]): void; + + } + /** options - optional configuration: + bind - a context object passed back to the method function (via this) when called. Defaults to active context (set via server.bind() when the method is registered. + cache - the same cache configuration used in server.cache(). + callback - if false, expects the method to be a synchronous function. Note that using a synchronous function with caching will convert the method interface to require a callback as an additional argument with the signature function(err, result, cached, report) since the cache interface cannot return values synchronously. Defaults to true. + generateKey - a function used to generate a unique key (for caching) from the arguments passed to the method function (the callback argument is not passed as input). The server will automatically generate a unique key if the function's arguments are all of types 'string', 'number', or 'boolean'. However if the method uses other types of arguments, a key generation function must be provided which takes the same arguments as the function and returns a unique string (or null if no key can be generated).*/ + export interface IServerMethodOptions { + bind?: any; + cache?: ICatBoxCacheOptions; + callback?: boolean; + generateKey?(args: any[]): string; + } + /** Request object + + The request object is created internally for each incoming request. It is different from the node.js request object received from the HTTP server callback (which is available in request.raw.req). The request object methods and properties change throughout the request lifecycle. + Request events + + The request object supports the following events: + + 'peek' - emitted for each chunk of payload data read from the client connection. The event method signature is function(chunk, encoding). + 'finish' - emitted when the request payload finished reading. The event method signature is function (). + 'disconnect' - emitted when a request errors or aborts unexpectedly. + var Crypto = require('crypto'); + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + + server.ext('onRequest', function (request, reply) { + + var hash = Crypto.createHash('sha1'); + request.on('peek', function (chunk) { + + hash.update(chunk); + }); + + request.once('finish', function () { + + console.log(hash.digest('hex')); + }); + + request.once('disconnect', function () { + + console.error('request aborted'); + }); + + return reply.continue(); + });*/ + export class Request extends Events.EventEmitter { + /** application-specific state. Provides a safe place to store application data without potential conflicts with the framework. Should not be used by plugins which should use plugins[name].*/ + app: any; + /** authentication information*/ + auth: { + /** true is the request has been successfully authenticated, otherwise false.*/ + isAuthenticated: boolean; + /** the credential object received during the authentication process. The presence of an object does not mean successful authentication. can be set in the validate function's callback.*/ + credentials: any; + /** an artifact object received from the authentication strategy and used in authentication-related actions.*/ + artifacts: any; + /** the route authentication mode.*/ + mode: any; + /** the authentication error is failed and mode set to 'try'.*/ + error: any; + /** an object used by the ['cookie' authentication scheme] https://github.com/hapijs/hapi-auth-cookie */ + session: any + }; + /** the connection used by this request*/ + connection: ServerConnection; + /** the node domain object used to protect against exceptions thrown in extensions, handlers and route prerequisites. Can be used to manually bind callback functions otherwise bound to other domains.*/ + domain: any; + /** the raw request headers (references request.raw.headers).*/ + headers: IDictionary; + /** a unique request identifier (using the format '{now}:{connection.info.id}:{5 digits counter}').*/ + id: number; + /** request information */ + info: { + /** request reception timestamp. */ + received: number; + /** request response timestamp (0 is not responded yet). */ + responded: number; + /** remote client IP address. */ + + remoteAddress: string; + /** remote client port. */ + remotePort: number; + /** content of the HTTP 'Referrer' (or 'Referer') header. */ + referrer: string; + /** content of the HTTP 'Host' header (e.g. 'example.com:8080'). */ + host: string; + /** the hostname part of the 'Host' header (e.g. 'example.com').*/ + hostname: string; + }; + /** the request method in lower case (e.g. 'get', 'post'). */ + method: string; + /** the parsed content-type header. Only available when payload parsing enabled and no payload error occurred. */ + mime: string; + /** an object containing the values of params, query, and payload before any validation modifications made. Only set when input validation is performed.*/ + orig: { + params: any; + query: any; + payload: any; + }; + /** an object where each key is a path parameter name with matching value as described in Path parameters.*/ + params: IDictionary; + /** an array containing all the path params values in the order they appeared in the path.*/ + paramsArray: string[]; + /** the request URI's path component. */ + path: string; + /** the request payload based on the route payload.output and payload.parse settings.*/ + payload: stream.Readable | Buffer | any; + /** plugin-specific state. Provides a place to store and pass request-level plugin data. The plugins is an object where each key is a plugin name and the value is the state.*/ + plugins: any; + /** an object where each key is the name assigned by a route prerequisites function. The values are the raw values provided to the continuation function as argument. For the wrapped response object, use responses.*/ + pre: IDictionary; + /** the response object when set. The object can be modified but must not be assigned another object. To replace the response with another from within an extension point, use reply(response) to override with a different response. Contains null when no response has been set (e.g. when a request terminates prematurely when the client disconnects).*/ + response: Response; + /**preResponses - same as pre but represented as the response object created by the pre method.*/ + preResponses: any; + /**an object containing the query parameters.*/ + query: any; + /** an object containing the Node HTTP server objects. Direct interaction with these raw objects is not recommended.*/ + raw: { + req: http.ClientRequest; + res: http.ServerResponse; + }; + /** the route public interface.*/ + route: IRoute; + /** the server object. */ + server: Server; + /** Special key reserved for plugins implementing session support. Plugins utilizing this key must check for null value to ensure there is no conflict with another similar server. */ + session: any; + /** an object containing parsed HTTP state information (cookies) where each key is the cookie name and value is the matching cookie content after processing using any registered cookie definition. */ + state: any; + /** complex object contining details on the url */ + url: { + /** null when i tested */ + auth: any; + /** null when i tested */ + hash: any; + /** null when i tested */ + host: any; + /** null when i tested */ + hostname: any; + href: string; + path: string; + /** path without search*/ + pathname: string; + /** null when i tested */ + port: any; + /** null when i tested */ + protocol: any; + /** querystring parameters*/ + query: IDictionary; + /** querystring parameters as a string*/ + search: string; + /** null when i tested */ + slashes: any; + }; + /** request.setUrl(url) + + Available only in 'onRequest' extension methods. + + Changes the request URI before the router begins processing the request where: + + url - the new request path value. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + + server.ext('onRequest', function (request, reply) { + + // Change all requests to '/test' + request.setUrl('/test'); + return reply.continue(); + });*/ + setUrl(url: string): void; + /** request.setMethod(method) + + Available only in 'onRequest' extension methods. + + Changes the request method before the router begins processing the request where: + + method - is the request HTTP method (e.g. 'GET'). + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + + server.ext('onRequest', function (request, reply) { + + // Change all requests to 'GET' + request.setMethod('GET'); + return reply.continue(); + });*/ + setMethod(method: string): void; + /** request.log(tags, [data, [timestamp]]) + + Always available. + + Logs request-specific events. When called, the server emits a 'request' event which can be used by other listeners or plugins. The arguments are: + + data - an optional message string or object with the application data being logged. + timestamp - an optional timestamp expressed in milliseconds. Defaults to Date.now() (now). + Any logs generated by the server internally will be emitted only on the 'request-internal' channel and will include the event.internal flag set to true. + + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + + server.on('request', function (request, event, tags) { + + if (tags.error) { + console.log(event); + } + }); + + var handler = function (request, reply) { + + request.log(['test', 'error'], 'Test event'); + return reply(); + }; + */ + log( + /** a string or an array of strings (e.g. ['error', 'database', 'read']) used to identify the event. Tags are used instead of log levels and provide a much more expressive mechanism for describing and filtering events.*/ + tags: string | string[], + /** an optional message string or object with the application data being logged.*/ + data?: string, + /** an optional timestamp expressed in milliseconds. Defaults to Date.now() (now).*/ + timestamp?: number): void; + /** request.getLog([tags], [internal]) + + Always available. + + Returns an array containing the events matching any of the tags specified (logical OR) + request.getLog(); + request.getLog('error'); + request.getLog(['error', 'auth']); + request.getLog(['error'], true); + request.getLog(false);*/ + + getLog( + /** is a single tag string or array of tag strings. If no tags specified, returns all events.*/ + tags?: string, + /** filters the events to only those with a matching event.internal value. If true, only internal logs are included. If false, only user event are included. Defaults to all events (undefined).*/ + internal?: boolean): string[]; + + /** request.tail([name]) + + Available until immediately after the 'response' event is emitted. + + Adds a request tail which has to complete before the request lifecycle is complete where: + + name - an optional tail name used for logging purposes. + Returns a tail function which must be called when the tail activity is completed. + + Tails are actions performed throughout the request lifecycle, but which may end after a response is sent back to the client. For example, a request may trigger a database update which should not delay sending back a response. However, it is still desirable to associate the activity with the request when logging it (or an error associated with it). + + When all tails completed, the server emits a 'tail' event. + + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + + var get = function (request, reply) { + + var dbTail = request.tail('write to database'); + + db.save('key', 'value', function () { + + dbTail(); + }); + + return reply('Success!'); + }; + + server.route({ method: 'GET', path: '/', handler: get }); + + server.on('tail', function (request) { + + console.log('Request completed including db activity'); + });*/ + tail( + /** an optional tail name used for logging purposes.*/ + name?: string): Function; + } + /** Response events + + The response object supports the following events: + + 'peek' - emitted for each chunk of data written back to the client connection. The event method signature is function(chunk, encoding). + 'finish' - emitted when the response finished writing but before the client response connection is ended. The event method signature is function (). + var Crypto = require('crypto'); + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + + server.ext('onPreResponse', function (request, reply) { + + var response = request.response; + if (response.isBoom) { + return reply(); + } + + var hash = Crypto.createHash('sha1'); + response.on('peek', function (chunk) { + + hash.update(chunk); + }); + + response.once('finish', function () { + + console.log(hash.digest('hex')); + }); + + return reply.continue(); + });*/ + export class Response extends Events.EventEmitter { + isBoom: boolean; + /** the HTTP response status code. Defaults to 200 (except for errors).*/ + statusCode: number; + /** an object containing the response headers where each key is a header field name. Note that this is an incomplete list of headers to be included with the response. Additional headers will be added once the response is prepare for transmission.*/ + headers: IDictionary; + /** the value provided using the reply interface.*/ + source: any; + /** a string indicating the type of source with available values: + 'plain' - a plain response such as string, number, null, or simple object (e.g. not a Stream, Buffer, or view). + 'buffer' - a Buffer. + 'view' - a view generated with reply.view(). + 'file' - a file generated with reply.file() of via the directory handler. + 'stream' - a Stream. + 'promise' - a Promise object. */ + variety: string; + /** application-specific state. Provides a safe place to store application data without potential conflicts with the framework. Should not be used by plugins which should use plugins[name].*/ + app: any; + /** plugin-specific state. Provides a place to store and pass request-level plugin data. The plugins is an object where each key is a plugin name and the value is the state. */ + plugins: any; + /** settings - response handling flags: + charset - the 'Content-Type' HTTP header 'charset' property. Defaults to 'utf-8'. + encoding - the string encoding scheme used to serial data into the HTTP payload when source is a string or marshals into a string. Defaults to 'utf8'. + passThrough - if true and source is a Stream, copies the statusCode and headers of the stream to the outbound response. Defaults to true. + stringify - options used for source value requiring stringification. Defaults to no replacer and no space padding. + ttl - if set, overrides the route cache expiration milliseconds value set in the route config. Defaults to no override. + varyEtag - if true, a suffix will be automatically added to the 'ETag' header at transmission time (separated by a '-' character) when the HTTP 'Vary' header is present.*/ + settings: { + charset: string; + encoding: string; + passThrough: boolean; + stringify: any; + ttl: number; + varyEtag: boolean; + } + + /** sets the HTTP 'Content-Length' header (to avoid chunked transfer encoding) where: + length - the header value. Must match the actual payload size.*/ + bytes(length: number): Response; + /** sets the 'Content-Type' HTTP header 'charset' property where: charset - the charset property value.*/ + charset(charset: string): Response; + + /** sets the HTTP status code where: + statusCode - the HTTP status code.*/ + code(statusCode: number): Response; + /** sets the HTTP status code to Created (201) and the HTTP 'Location' header where: uri - an absolute or relative URI used as the 'Location' header value.*/ + created(uri: string): Response; + + + /** encoding(encoding) - sets the string encoding scheme used to serial data into the HTTP payload where: encoding - the encoding property value (see node Buffer encoding).*/ + encoding(encoding: string): Response; + + /** etag(tag, options) - sets the representation entity tag where: + tag - the entity tag string without the double-quote. + options - optional settings where: + weak - if true, the tag will be prefixed with the 'W/' weak signifier. Weak tags will fail to match identical tags for the purpose of determining 304 response status. Defaults to false. + vary - if true and content encoding is set or applied to the response (e.g 'gzip' or 'deflate'), the encoding name will be automatically added to the tag at transmission time (separated by a '-' character). Ignored when weak is true. Defaults to true.*/ + etag(tag: string, options: { + weak: boolean; vary: boolean; + }): Response; + + /**header(name, value, options) - sets an HTTP header where: + name - the header name. + value - the header value. + options - optional settings where: + append - if true, the value is appended to any existing header value using separator. Defaults to false. + separator - string used as separator when appending to an exiting value. Defaults to ','. + override - if false, the header value is not set if an existing value present. Defaults to true.*/ + header(name: string, value: string, options?: { + append: boolean; + separator: string; + override: boolean; + }): Response; + + /** location(uri) - sets the HTTP 'Location' header where: + uri - an absolute or relative URI used as the 'Location' header value.*/ + location(uri: string): Response; + + /** redirect(uri) - sets an HTTP redirection response (302) and decorates the response with additional methods listed below, where: + uri - an absolute or relative URI used to redirect the client to another resource. */ + redirect(uri: string): Response; + + /** replacer(method) - sets the JSON.stringify() replacer argument where: + method - the replacer function or array. Defaults to none.*/ + replacer(method: Function | Array): Response; + + /** spaces(count) - sets the JSON.stringify() space argument where: + count - the number of spaces to indent nested object keys. Defaults to no indentation. */ + spaces(count: number): Response; + /**state(name, value, [options]) - sets an HTTP cookie where: + name - the cookie name. + value - the cookie value. If no encoding is defined, must be a string. + options - optional configuration. If the state was previously registered with the server using server.state(), the specified keys in options override those same keys in the server definition (but not others).*/ + state(name: string, value: string, options?: any): Response; + + /** sets a string suffix when the response is process via JSON.stringify().*/ + suffix(suffix: string): void; + /** overrides the default route cache expiration rule for this response instance where: +msec - the time-to-live value in milliseconds.*/ + ttl(msec: number): void; + /** type(mimeType) - sets the HTTP 'Content-Type' header where: + mimeType - is the mime type. Should only be used to override the built-in default for each response type. */ + type(mimeType: string): Response; + /** clears the HTTP cookie by setting an expired value where: +name - the cookie name. +options - optional configuration for expiring cookie. If the state was previously registered with the server using server.state(), the specified keys in options override those same keys in the server definition (but not others).*/ + unstate(name: string, options?: { [key: string]: string }): void; + /** adds the provided header to the list of inputs affected the response generation via the HTTP 'Vary' header where: +header - the HTTP request header name.*/ + vary(header: string): void; + } + /** When using the redirect() method, the response object provides these additional methods */ + export class ResponseRedirect extends Response { + /** sets the status code to 302 or 307 (based on the rewritable() setting) where: +isTemporary - if false, sets status to permanent. Defaults to true.*/ + temporary(isTemporary: boolean): void; + /** sets the status code to 301 or 308 (based on the rewritable() setting) where: +isPermanent - if true, sets status to temporary. Defaults to false. */ + permanent(isPermanent: boolean): void; + /** sets the status code to 301/302 for rewritable (allows changing the request method from 'POST' to 'GET') or 307/308 for non-rewritable (does not allow changing the request method from 'POST' to 'GET'). Exact code based on the temporary() or permanent() setting. Arguments: +isRewritable - if false, sets to non-rewritable. Defaults to true. +Permanent Temporary +Rewritable 301 302(1) +Non-rewritable 308(2) 307 +Notes: 1. Default value. 2. Proposed code, not supported by all clients. */ + rewritable(isRewritable: boolean): void; + } + /** info about a server connection */ + export interface IServerConnectionInfo { + /** - a unique connection identifier (using the format '{hostname}:{pid}:{now base36}').*/ + id: string; + /** - the connection creation timestamp.*/ + created: number; + /** - the connection start timestamp (0 when stopped).*/ + started: number; + /** the connection port based on the following rules: + the configured port value before the server has been started. + the actual port assigned when no port is configured or set to 0 after the server has been started.*/ + port: number; + + /** - the host name the connection was configured to. Defaults to the operating system hostname when available, otherwise 'localhost'.*/ + host: string; + /** - the active IP address the connection was bound to after starting.Set to undefined until the server has been started or when using a non TCP port (e.g. UNIX domain socket).*/ + address: string; + /** - the protocol used: + 'http' - HTTP. + 'https' - HTTPS. + 'socket' - UNIX domain socket or Windows named pipe.*/ + protocol: string; + /** a string representing the connection (e.g. 'http://example.com:8080' or 'socket:/unix/domain/socket/path'). Contains the uri setting if provided, otherwise constructed from the available settings. If no port is available or set to 0, the uri will not include a port component.*/ + uri: string; + } + /** + * undocumented. The connection object constructed after calling server.connection(); + * can be accessed via server.connections; or request.connection; + */ + export class ServerConnection extends Events.EventEmitter { + domain: any; + _events: { route: Function, domain: Function, _events: Function, _eventsCount: Function, _maxListeners: Function }; + _eventsCount: number; + settings: IServerConnectionOptions; + server: Server; + /** ex: "tcp" */ + type: string; + _started: boolean; + /** dictionary of sockets */ + _connections: { [ip_port: string]: any }; + _onConnection: Function; + registrations: any; + _extensions: any; + _requestCounter: { value: number; min: number; max: number }; + _load: any; + states: { + settings: any; cookies: any; names: any[] + }; + auth: { connection: ServerConnection; _schemes: any; _strategies: any; settings: any; }; + _router: any; + MSPluginsCollection: any; + applicationCache: any; + addEventListener: any; + info: IServerConnectionInfo; + } + + /** Server http://hapijs.com/api#server + rver object is the main application container. The server manages all incoming connections along with all the facilities provided by the framework. A server can contain more than one connection (e.g. listen to port 80 and 8080). + Server events + The server object inherits from Events.EventEmitter and emits the following events: + 'log' - events logged with server.log() and server events generated internally by the framework. + 'start' - emitted when the server is started using server.start(). + 'stop' - emitted when the server is stopped using server.stop(). + 'request' - events generated by request.log(). Does not include any internally generated events. + 'request-internal' - request events generated internally by the framework (multiple events per request). + 'request-error' - emitted whenever an Internal Server Error (500) error response is sent. Single event per request. + 'response' - emitted after the response is sent back to the client (or when the client connection closed and no response sent, in which case request.response is null). Single event per request. + 'tail' - emitted when a request finished processing, including any registered tails. Single event per request. + Note that the server object should not be used to emit application events as its internal implementation is designed to fan events out to the various plugin selections and not for application events. + MORE EVENTS HERE: http://hapijs.com/api#server-events*/ + export class Server extends Events.EventEmitter { + + constructor(options?: IServerOptions); + /** Provides a safe place to store server-specific run-time application data without potential conflicts with the framework internals. The data can be accessed whenever the server is accessible. Initialized with an empty object. + var Hapi = require('hapi'); + server = new Hapi.Server(); + server.app.key = 'value'; + var handler = function (request, reply) { + return reply(request.server.app.key); + }; */ + app: any; + /** An array containing the server's connections. When the server object is returned from server.select(), the connections array only includes the connections matching the selection criteria. + var server = new Hapi.Server(); + server.connection({ port: 80, labels: 'a' }); + server.connection({ port: 8080, labels: 'b' }); + // server.connections.length === 2 + var a = server.select('a'); + // a.connections.length === 1*/ + connections: Array; + /** When the server contains exactly one connection, info is an object containing information about the sole connection. + * When the server contains more than one connection, each server.connections array member provides its own connection.info. + var server = new Hapi.Server(); + server.connection({ port: 80 }); + // server.info.port === 80 + server.connection({ port: 8080 }); + // server.info === null + // server.connections[1].info.port === 8080 + */ + info: IServerConnectionInfo; + /** An object containing the process load metrics (when load.sampleInterval is enabled): + rss - RSS memory usage. + var Hapi = require('hapi'); + var server = new Hapi.Server({ load: { sampleInterval: 1000 } }); + console.log(server.load.rss);*/ + load: { + /** - event loop delay milliseconds.*/ + eventLoopDelay: number; + /** - V8 heap usage.*/ + heapUsed: number; + }; + /** When the server contains exactly one connection, listener is the node HTTP server object of the sole connection. + When the server contains more than one connection, each server.connections array member provides its own connection.listener. + var Hapi = require('hapi'); + var SocketIO = require('socket.io'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + var io = SocketIO.listen(server.listener); + io.sockets.on('connection', function(socket) { + socket.emit({ msg: 'welcome' }); + });*/ + listener: http.Server; + + /** server.methods + An object providing access to the server methods where each server method name is an object property. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.method('add', function (a, b, next) { + return next(null, a + b); + }); + server.methods.add(1, 2, function (err, result) { + // result === 3 + });*/ + methods: IDictionary; + + /** server.mime + Provides access to the server MIME database used for setting content-type information. The object must not be modified directly but only through the mime server setting. + var Hapi = require('hapi'); + var options = { + mime: { + override: { + 'node/module': { + source: 'steve', + compressible: false, + extensions: ['node', 'module', 'npm'], + type: 'node/module' + } + } + } + }; + var server = new Hapi.Server(options); + // server.mime.path('code.js').type === 'application/javascript' + // server.mime.path('file.npm').type === 'node/module'*/ + mime: any; + /**server.plugins + An object containing the values exposed by each plugin registered where each key is a plugin name and the values are the exposed properties by each plugin using server.expose(). Plugins may set the value of the server.plugins[name] object directly or via the server.expose() method. + exports.register = function (server, options, next) { + server.expose('key', 'value'); + // server.plugins.example.key === 'value' + return next(); + }; + exports.register.attributes = { + name: 'example' + };*/ + plugins: IDictionary; + /** server.realm + The realm object contains server-wide or plugin-specific state that can be shared across various methods. For example, when calling server.bind(), the active realm settings.bind property is set which is then used by routes and extensions added at the same level (server root or plugin). Realms are a limited version of a sandbox where plugins can maintain state used by the framework when adding routes, extensions, and other properties. + modifiers - when the server object is provided as an argument to the plugin register() method, modifiers provides the registration preferences passed the server.register() method and includes: + route - routes preferences: + prefix - the route path prefix used by any calls to server.route() from the server. + vhost - the route virtual host settings used by any calls to server.route() from the server. + plugin - the active plugin name (empty string if at the server root). + plugins - plugin-specific state to be shared only among activities sharing the same active state. plugins is an object where each key is a plugin name and the value is the plugin state. + settings - settings overrides: + files.relativeTo + bind + The server.realm object should be considered read-only and must not be changed directly except for the plugins property can be directly manipulated by the plugins (each setting its own under plugins[name]). + exports.register = function (server, options, next) { + console.log(server.realm.modifiers.route.prefix); + return next(); + };*/ + realm: IServerRealm; + + /** server.root + The root server object containing all the connections and the root server methods (e.g. start(), stop(), connection()).*/ + root: Server; + /** server.settings + The server configuration object after defaults applied. + var Hapi = require('hapi'); + var server = new Hapi.Server({ + app: { + key: 'value' + } + }); + // server.settings.app === { key: 'value' }*/ + settings: IServerOptions; + + /** server.version + The hapi module version number. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + // server.version === '8.0.0'*/ + version: string; + + /** server.after(method, [dependencies]) + Adds a method to be called after all the plugin dependencies have been registered and before the server starts (only called if the server is started) where: + after - the method with signature function(plugin, next) where: + server - server object the after() method was called on. + next - the callback function the method must call to return control over to the application and complete the registration process. The function signature is function(err) where: + err - internal error which is returned back via the server.start() callback. + dependencies - a string or array of string with the plugin names to call this method after their after() methods. There is no requirement for the other plugins to be registered. Setting dependencies only arranges the after methods in the specified order. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.after(function () { + // Perform some pre-start logic + }); + server.start(function (err) { + // After method already executed + }); + server.auth.default(options)*/ + after(method: (plugin: any, next: (err: any) => void) => void, dependencies: string | string[]): void; + + auth: { + /** server.auth.default(options) + Sets a default strategy which is applied to every route where: + options - a string with the default strategy name or an object with a specified strategy or strategies using the same format as the route auth handler options. + The default does not apply when the route config specifies auth as false, or has an authentication strategy configured. Otherwise, the route authentication config is applied to the defaults. Note that the default only applies at time of route configuration, not at runtime. Calling default() after adding a route will have no impact on routes added prior. + The default auth strategy configuration can be accessed via connection.auth.settings.default. + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.auth.scheme('custom', scheme); + server.auth.strategy('default', 'custom'); + server.auth.default('default'); + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + return reply(request.auth.credentials.user); + } + });*/ + default(options: string): void; + /** server.auth.scheme(name, scheme) + Registers an authentication scheme where: + name - the scheme name. + scheme - the method implementing the scheme with signature function(server, options) where: + server - a reference to the server object the scheme is added to. + options - optional scheme settings used to instantiate a strategy.*/ + scheme(name: string, + /** When the scheme authenticate() method implementation calls reply() with an error condition, the specifics of the error affect whether additional authentication strategies will be attempted if configured for the route. If the err returned by the reply() method includes a message, no additional strategies will be attempted. If the err does not include a message but does include a scheme name (e.g. Boom.unauthorized(null, 'Custom')), additional strategies will be attempted in order of preference. + n the scheme payload() method returns an error with a message, it means payload validation failed due to bad payload. If the error has no message but includes a scheme name (e.g. Boom.unauthorized(null, 'Custom')), authentication may still be successful if the route auth.payload configuration is set to 'optional'. + server = new Hapi.Server(); + server.connection({ port: 80 }); + scheme = function (server, options) { + urn { + authenticate: function (request, reply) { + req = request.raw.req; + var authorization = req.headers.authorization; + if (!authorization) { + return reply(Boom.unauthorized(null, 'Custom')); + } + urn reply(null, { credentials: { user: 'john' } }); + } + }; + }; + */ + scheme: (server: Server, options: any) => IServerAuthScheme): void; + + /** server.auth.strategy(name, scheme, [mode], [options]) + Registers an authentication strategy where: + name - the strategy name. + scheme - the scheme name (must be previously registered using server.auth.scheme()). + mode - if true, the scheme is automatically assigned as a required strategy to any route without an auth config. Can only be assigned to a single server strategy. Value must be true (which is the same as 'required') or a valid authentication mode ('required', 'optional', 'try'). Defaults to false. + options - scheme options based on the scheme requirements. + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.auth.scheme('custom', scheme); + server.auth.strategy('default', 'custom'); + server.route({ + method: 'GET', + path: '/', + config: { + auth: 'default', + handler: function (request, reply) { + return reply(request.auth.credentials.user); + } + } + });*/ + strategy(name: string, scheme: any, mode?: boolean | string, options?: any): void; + + /** server.auth.test(strategy, request, next) + Tests a request against an authentication strategy where: + strategy - the strategy name registered with server.auth.strategy(). + request - the request object. + next - the callback function with signature function(err, credentials) where: + err - the error if authentication failed. + credentials - the authentication credentials object if authentication was successful. + Note that the test() method does not take into account the route authentication configuration. It also does not perform payload authentication. It is limited to the basic strategy authentication execution. It does not include verifying scope, entity, or other route properties. + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.auth.scheme('custom', scheme); + server.auth.strategy('default', 'custom'); + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + request.server.auth.test('default', request, function (err, credentials) { + if (err) { + return reply({ status: false }); + } + return reply({ status: true, user: credentials.name }); + }); + } + });*/ + test(strategy: string, request: Request, next: (err: any, credentials: any) => void): void; + }; + /** server.bind(context) + Sets a global context used as the default bind object when adding a route or an extension where: + context - the object used to bind this in handler and extension methods. + When setting context inside a plugin, the context is applied only to methods set up by the plugin. Note that the context applies only to routes and extensions added after it has been set. + var handler = function (request, reply) { + return reply(this.message); + }; + exports.register = function (server, options, next) { + var bind = { + message: 'hello' + }; + server.bind(bind); + server.route({ method: 'GET', path: '/', handler: handler }); + return next(); + };*/ + bind(context: any): void; + + + /** server.cache(options) + Provisions a cache segment within the server cache facility where: + options - catbox policy configuration where: + expiresIn - relative expiration expressed in the number of milliseconds since the item was saved in the cache. Cannot be used together with expiresAt. + 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. + 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 function(id, next) where: - id - the id string or object provided to the get() method. - next - the method called when the new item is returned with the signature function(err, value, ttl) where: - err - an error condition. - value - the new value generated. - ttl - the cache ttl value in milliseconds. Set to 0 to skip storing in the cache. Defaults to the cache global policy. + 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. + staleTimeout - number of milliseconds to wait before checking if an item is stale. + 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 is stored in the cache for future requests. + cache - the cache name configured in 'server.cache`. Defaults to the default cache. + 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. Required when called outside of a plugin. + shared - if true, allows multiple cache provisions to share the same segment. Default to false. + var server = new Hapi.Server(); + server.connection({ port: 80 }); + var cache = server.cache({ segment: 'countries', expiresIn: 60 * 60 * 1000 }); + cache.set('norway', { capital: 'oslo' }, null, function (err) { + cache.get('norway', function (err, value, cached, log) { + // value === { capital: 'oslo' }; + }); + });*/ + cache(options: ICatBoxCacheOptions): void; + + /** server.connection([options]) + Adds an incoming server connection + Returns a server object with the new connection selected. + Must be called before any other server method that modifies connections is called for it to apply to the new connection (e.g. server.state()). + Note that the options object is deeply cloned (with the exception of listener which is shallowly copied) and cannot contain any values that are unsafe to perform deep copy on. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + var web = server.connection({ port: 8000, host: 'example.com', labels: ['web'] }); + var admin = server.connection({ port: 8001, host: 'example.com', labels: ['admin'] }); + // server.connections.length === 2 + // web.connections.length === 1 + // admin.connections.length === 1 */ + connection(options: IServerConnectionOptions): Server; + /** server.decorate(type, property, method) + Extends various framework interfaces with custom methods where: + type - the interface being decorated. Supported types: + 'reply' - adds methods to the reply interface. + 'server' - adds methods to the Server object. + property - the object decoration key name. + method - the extension function. + Note that decorations apply to the entire server and all its connections regardless of current selection. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.decorate('reply', 'success', function () { + return this.response({ status: 'ok' }); + }); + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + return reply.success(); + } + });*/ + decorate(type: string, property: string, method: Function): void; + + /** server.dependency(dependencies, [after]) + Used within a plugin to declares a required dependency on other plugins where: + dependencies - a single string or array of plugin name strings which must be registered in order for this plugin to operate. Plugins listed must be registered before the server is started. Does not provide version dependency which should be implemented using npm peer dependencies. + after - an optional function called after all the specified dependencies have been registered and before the server starts. The function is only called if the server is started. If a circular dependency is detected, an exception is thrown (e.g. two plugins each has an after function to be called after the other). The function signature is function(server, next) where: + server - the server the dependency() method was called on. + next - the callback function the method must call to return control over to the application and complete the registration process. The function signature is function(err) where: + err - internal error condition, which is returned back via the server.start() callback. + exports.register = function (server, options, next) { + server.dependency('yar', after); + return next(); + }; + var after = function (server, next) { + // Additional plugin registration logic + return next(); + };*/ + dependency(dependencies: string | string[], after?: (server: Server, next: (err: any) => void) => void): void; + + + /** server.expose(key, value) + Used within a plugin to expose a property via server.plugins[name] where: + key - the key assigned (server.plugins[name][key]). + value - the value assigned. + exports.register = function (server, options, next) { + server.expose('util', function () { console.log('something'); }); + return next(); + };*/ + expose(key: string, value: any): void; + + /** server.expose(obj) + Merges a deep copy of an object into to the existing content of server.plugins[name] where: + obj - the object merged into the exposed properties container. + exports.register = function (server, options, next) { + server.expose({ util: function () { console.log('something'); } }); + return next(); + };*/ + expose(obj: any): void; + + /** server.ext(event, method, [options]) + Registers an extension function in one of the available extension points where: + event - the event name. + method - a function or an array of functions to be executed at a specified point during request processing. The required extension function signature is function(request, reply) where: + request - the request object. NOTE: Access the Response via request.response + reply - the reply interface which is used to return control back to the framework. To continue normal execution of the request lifecycle, reply.continue() must be called. To abort processing and return a response to the client, call reply(value) where value is an error or any other valid response. + this - the object provided via options.bind or the current active context set with server.bind(). + options - an optional object with the following: + before - a string or array of strings of plugin names this method must execute before (on the same event). Otherwise, extension methods are executed in the order added. + after - a string or array of strings of plugin names this method must execute after (on the same event). Otherwise, extension methods are executed in the order added. + bind - a context object passed back to the provided method (via this) when called. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.ext('onRequest', function (request, reply) { + // Change all requests to '/test' + request.setUrl('/test'); + return reply.continue(); + }); + var handler = function (request, reply) { + return reply({ status: 'ok' }); + }; + server.route({ method: 'GET', path: '/test', handler: handler }); + server.start(); + // All requests will get routed to '/test'*/ + ext(event: string, method: (request: Request, reply: IReply, bind?: any) => void, options?: { before: string | string[]; after: string | string[]; bind?: any }): void; + + /** server.handler(name, method) + Registers a new handler type to be used in routes where: + name - string name for the handler being registered. Cannot override the built-in handler types (directory, file, proxy, and view) or any previously registered type. + method - the function used to generate the route handler using the signature function(route, options) where: + route - the route public interface object. + options - the configuration object provided in the handler config. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ host: 'localhost', port: 8000 }); + // Defines new handler for routes on this server + server.handler('test', function (route, options) { + return function (request, reply) { + return reply('new handler: ' + options.msg); + } + }); + server.route({ + method: 'GET', + path: '/', + handler: { test: { msg: 'test' } } + }); + server.start(); + The method function can have a defaults object or function property. If the property is set to an object, that object is used as the default route config for routes using this handler. If the property is set to a function, the function uses the signature function(method) and returns the route default configuration. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ host: 'localhost', port: 8000 }); + var handler = function (route, options) { + return function (request, reply) { + return reply('new handler: ' + options.msg); + } + }; + // Change the default payload processing for this handler + handler.defaults = { + payload: { + output: 'stream', + parse: false + } + }; + server.handler('test', handler);*/ + handler(name: string, method: (route: IRoute, options: THandlerConfig) => ISessionHandler): void; + /** When the server contains exactly one connection, injects a request into the sole connection simulating an incoming HTTP request without making an actual socket connection. + Injection is useful for testing purposes as well as for invoking routing logic internally without the overhead or limitations of the network stack. + Utilizes the [shot module | https://github.com/hapijs/shot ] for performing injections, with some additional options and response properties + * When the server contains more than one connection, each server.connections array member provides its own connection.inject(). + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + var handler = function (request, reply) { + return reply('Success!'); + }; + server.route({ method: 'GET', path: '/', handler: handler }); + server.inject('/', function (res) { + console.log(res.result); + }); + */ + inject: IServerInject; + + /** server.log(tags, [data, [timestamp]]) + Logs server events that cannot be associated with a specific request. When called the server emits a 'log' event which can be used by other listeners or plugins to record the information or output to the console. The arguments are: + tags - a string or an array of strings (e.g. ['error', 'database', 'read']) used to identify the event. Tags are used instead of log levels and provide a much more expressive mechanism for describing and filtering events. Any logs generated by the server internally include the 'hapi' tag along with event-specific information. + data - an optional message string or object with the application data being logged. + timestamp - an optional timestamp expressed in milliseconds. Defaults to Date.now() (now). + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.on('log', function (event, tags) { + if (tags.error) { + console.log(event); + } + }); + server.log(['test', 'error'], 'Test event');*/ + log(tags: string | string[], data?: string | any, timestamp?: number): void; + /**server.lookup(id) + When the server contains exactly one connection, looks up a route configuration where: + id - the route identifier as set in the route options. + returns the route public interface object if found, otherwise null. + var server = new Hapi.Server(); + server.connection(); + server.route({ + method: 'GET', + path: '/', + config: { + handler: function (request, reply) { return reply(); }, + id: 'root' + } + }); + var route = server.lookup('root'); + When the server contains more than one connection, each server.connections array member provides its own connection.lookup() method.*/ + lookup(id: string): IRoute; + /** server.match(method, path, [host]) + When the server contains exactly one connection, looks up a route configuration where: + method - the HTTP method (e.g. 'GET', 'POST'). + path - the requested path (must begin with '/'). + host - optional hostname (to match against routes with vhost). + returns the route public interface object if found, otherwise null. + var server = new Hapi.Server(); + server.connection(); + server.route({ + method: 'GET', + path: '/', + config: { + handler: function (request, reply) { return reply(); }, + id: 'root' + } + }); + var route = server.match('get', '/'); + When the server contains more than one connection, each server.connections array member provides its own connection.match() method.*/ + match(method: string, path: string, host?: string): IRoute; + + + + + /** server.method(name, method, [options]) + Registers a server method. Server methods are functions registered with the server and used throughout the application as a common utility. Their advantage is in the ability to configure them to use the built-in cache and share across multiple request handlers without having to create a common module. + Methods are registered via server.method(name, method, [options]) + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + // Simple arguments + var add = function (a, b, next) { + return next(null, a + b); + }; + server.method('sum', add, { cache: { expiresIn: 2000 } }); + server.methods.sum(4, 5, function (err, result) { + console.log(result); + }); + // Object argument + var addArray = function (array, next) { + var sum = 0; + array.forEach(function (item) { + sum += item; + }); + return next(null, sum); + }; + server.method('sumObj', addArray, { + cache: { expiresIn: 2000 }, + generateKey: function (array) { + return array.join(','); + } + }); + server.methods.sumObj([5, 6], function (err, result) { + console.log(result); + }); + // Synchronous method with cache + var addSync = function (a, b) { + return a + b; + }; + server.method('sumSync', addSync, { cache: { expiresIn: 2000 }, callback: false }); + server.methods.sumSync(4, 5, function (err, result) { + console.log(result); + }); */ + method( + /** a unique method name used to invoke the method via server.methods[name]. When configured with caching enabled, server.methods[name].cache.drop(arg1, arg2, ..., argn, callback) can be used to clear the cache for a given key. Supports using nested names such as utils.users.get which will automatically create the missing path under server.methods and can be accessed for the previous example via server.methods.utils.users.get.*/ + name: string, + method: IServerMethod, + options?: IServerMethodOptions): void; + + + /**server.method(methods) + Registers a server method function as described in server.method() using a configuration object where: + methods - an object or an array of objects where each one contains: + name - the method name. + method - the method function. + options - optional settings. + var add = function (a, b, next) { + next(null, a + b); + }; + server.method({ + name: 'sum', + method: add, + options: { + cache: { + expiresIn: 2000 + } + } + });*/ + method(methods: { + name: string; method: IServerMethod; options?: IServerMethodOptions + } | Array<{ + name: string; method: IServerMethod; options?: IServerMethodOptions + }>): void; + /**server.path(relativeTo) + Sets the path prefix used to locate static resources (files and view templates) when relative paths are used where: + relativeTo - the path prefix added to any relative file path starting with '.'. + Note that setting a path within a plugin only applies to resources accessed by plugin methods. If no path is set, the connection files.relativeTo configuration is used. The path only applies to routes added after it has been set. + exports.register = function (server, options, next) { + server.path(__dirname + '../static'); + server.route({ path: '/file', method: 'GET', handler: { file: './test.html' } }); + next(); + };*/ + path(relativeTo: string): void; + /**server.register(plugins, [options], callback) + Registers a plugin where: + plugins - an object or array of objects where each one is either: + a plugin registration function. + an object with the following: + register - the plugin registration function. + options - optional options passed to the registration function when called. + options - optional registration options (different from the options passed to the registration function): + select - a string or array of string labels used to pre-select connections for plugin registration. + routes - modifiers applied to each route added by the plugin: + prefix - string added as prefix to any route path (must begin with '/'). If a plugin registers a child plugin the prefix is passed on to the child or is added in front of the child-specific prefix. + vhost - virtual host string (or array of strings) applied to every route. The outer-most vhost overrides the any nested configuration. + callback - the callback function with signature function(err) where: + err - an error returned from the registration function. Note that exceptions thrown by the registration function are not handled by the framework. + server.register({ + register: require('plugin_name'), + options: { + message: 'hello' + } + }, function (err) { + if (err) { + console.log('Failed loading plugin'); + } + });*/ + register(plugins: any | any[], options: { + select: string | string[]; + routes: { + prefix: string; vhost?: string | string[] + }; + } + , callback: (err: any) => void): void; + + register(plugins: any | any[], callback: (err: any) => void): void; + + /**server.render(template, context, [options], callback) + Utilizes the server views manager to render a template where: + template - the template filename and path, relative to the views manager templates path (path or relativeTo). + context - optional object used by the template to render context-specific result. Defaults to no context ({}). + options - optional object used to override the views manager configuration. + callback - the callback function with signature function (err, rendered, config) where: + err - the rendering error if any. + rendered - the result view string. + config - the configuration used to render the template. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.views({ + engines: { html: require('handlebars') }, + path: __dirname + '/templates' + }); + var context = { + title: 'Views Example', + message: 'Hello, World' + }; + server.render('hello', context, function (err, rendered, config) { + console.log(rendered); + });*/ + render(template: string, context: any, options: any, callback: (err: any, rendered: any, config: any) => void): void; + /** server.route(options) + Adds a connection route where: + options - a route configuration object or an array of configuration objects. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.route({ method: 'GET', path: '/', handler: function (request, reply) { return reply('ok'); } }); + server.route([ + { method: 'GET', path: '/1', handler: function (request, reply) { return reply('ok'); } }, + { method: 'GET', path: '/2', handler: function (request, reply) { return reply('ok'); } } + ]);*/ + route(options: IRouteConfiguration): void; + route(options: IRouteConfiguration[]): void; + /**server.select(labels) + Selects a subset of the server's connections where: + labels - a single string or array of strings of labels used as a logical OR statement to select all the connections with matching labels in their configuration. + Returns a server object with connections set to the requested subset. Selecting again on a selection operates as a logic AND statement between the individual selections. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80, labels: ['a'] }); + server.connection({ port: 8080, labels: ['b'] }); + server.connection({ port: 8081, labels: ['c'] }); + server.connection({ port: 8082, labels: ['c','d'] }); + var a = server.select('a'); // The server with port 80 + var ab = server.select(['a','b']); // A list of servers containing the server with port 80 and the server with port 8080 + var c = server.select('c'); // A list of servers containing the server with port 8081 and the server with port 8082 */ + select(labels: string | string[]): Server | Server[]; + /** server.start([callback]) + Starts the server connections by listening for incoming requests on the configured port of each listener (unless the connection was configured with autoListen set to false), where: + callback - optional callback when server startup is completed or failed with the signature function(err) where: + err - any startup error condition. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.start(function (err) { + console.log('Server started at: ' + server.info.uri); + });*/ + start(callback?: (err: any) => void): void; + /** server.state(name, [options]) + HTTP state management uses client cookies to persist a state across multiple requests. Registers a cookie definitions + State defaults can be modified via the server connections.routes.state configuration option. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + // Set cookie definition + server.state('session', { + ttl: 24 * 60 * 60 * 1000, // One day + isSecure: true, + path: '/', + encoding: 'base64json' + }); + // Set state in route handler + var handler = function (request, reply) { + var session = request.state.session; + if (!session) { + session = { user: 'joe' }; + } + session.last = Date.now(); + return reply('Success').state('session', session); + }; + Registered cookies are automatically parsed when received. Parsing rules depends on the route state.parse configuration. If an incoming registered cookie fails parsing, it is not included in request.state, regardless of the state.failAction setting. When state.failAction is set to 'log' and an invalid cookie value is received, the server will emit a 'request-internal' event. To capture these errors subscribe to the 'request-internal' events and filter on 'error' and 'state' tags: + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.on('request-internal', function (request, event, tags) { + if (tags.error && tags.state) { + console.error(event); + } + }); */ + state(name: string, options?: ICookieSettings): void; + + /** server.stop([options], [callback]) + Stops the server's connections by refusing to accept any new connections or requests (existing connections will continue until closed or timeout), where: + options - optional object with: + timeout - overrides the timeout in millisecond before forcefully terminating a connection. Defaults to 5000 (5 seconds). + callback - optional callback method with signature function() which is called once all the connections have ended and it is safe to exit the process. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.stop({ timeout: 60 * 1000 }, function () { + console.log('Server stopped'); + });*/ + stop(options?: { timeout: number }, callback?: () => void): void; + /**server.table([host]) + Returns a copy of the routing table where: + host - optional host to filter routes matching a specific virtual host. Defaults to all virtual hosts. + The return value is an array where each item is an object containing: + info - the connection.info the connection the table was generated for. + labels - the connection labels. + table - an array of routes where each route contains: + settings - the route config with defaults applied. + method - the HTTP method in lower case. + path - the route path. + Note that if the server has not been started and multiple connections use port 0, the table items will override each other and will produce an incomplete result. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80, host: 'example.com' }); + server.route({ method: 'GET', path: '/example', handler: function (request, reply) { return reply(); } }); + var table = server.table(); + When calling connection.table() directly on each connection, the return value is the same as the array table item value of an individual connection: + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80, host: 'example.com' }); + server.route({ method: 'GET', path: '/example', handler: function (request, reply) { return reply(); } }); + var table = server.connections[0].table(); + //[ + // { + // method: 'get', + // path: '/example', + // settings: { ... } + // } + //] + */ + table(host?: any): IConnectionTable; + + /**server.views(options) + Initializes the server views manager + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.views({ + engines: { + html: require('handlebars'), + jade: require('jade') + }, + path: '/static/templates' + }); + When server.views() is called within a plugin, the views manager is only available to plugins methods.*/ + views(options: IServerViewsConfiguration): void; + + } +} diff --git a/types/hapi/v12/tsconfig.json b/types/hapi/v12/tsconfig.json new file mode 100644 index 0000000000..4659b7940a --- /dev/null +++ b/types/hapi/v12/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "types": [], + "paths": { + "hapi": [ + "hapi/v12" + ] + }, + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "hapi-tests.ts" + ] +} \ No newline at end of file diff --git a/types/hapi/hapi-tests.ts b/types/hapi/v15/hapi-tests.ts similarity index 100% rename from types/hapi/hapi-tests.ts rename to types/hapi/v15/hapi-tests.ts diff --git a/types/hapi/v15/index.d.ts b/types/hapi/v15/index.d.ts new file mode 100644 index 0000000000..86b993bfb5 --- /dev/null +++ b/types/hapi/v15/index.d.ts @@ -0,0 +1,2497 @@ +// Type definitions for hapi 15.0 +// Project: http://github.com/spumko/hapi +// Definitions by: Jason Swearingen +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +// Note/Disclaimer: This .d.ts was created against hapi v8.x but has been incrementally upgraded to 13.x. Some newer features/changes may be missing. YMMV. + +/// + +import http = require("http"); +import stream = require("stream"); +import Events = require("events"); +import url = require("url"); + +export interface IDictionary { + [key: string]: T; +} + +export interface IHeaderOptions { + append?: boolean; + separator?: string; + override?: boolean; + duplicate?: boolean; +} + +/** Boom Module for errors. https://github.com/hapijs/boom + * boom provides a set of utilities for returning HTTP errors. Each utility returns a Boom error response object (instance of Error) which includes the following properties: */ +export interface IBoom extends Error { + /** if true, indicates this is a Boom object instance. */ + isBoom: boolean; + /** convenience bool indicating status code >= 500. */ + isServer: boolean; + /** the error message. */ + message: string; + /** the formatted response.Can be directly manipulated after object construction to return a custom error response.Allowed root keys: */ + output: { + /** the HTTP status code (typically 4xx or 5xx). */ + statusCode: number; + /** an object containing any HTTP headers where each key is a header name and value is the header content. */ + headers: IDictionary; + /** the formatted object used as the response payload (stringified).Can be directly manipulated but any changes will be lost if reformat() is called.Any content allowed and by default includes the following content: */ + payload: { + /** the HTTP status code, derived from error.output.statusCode. */ + statusCode: number; + /** the HTTP status message (e.g. 'Bad Request', 'Internal Server Error') derived from statusCode. */ + error: string; + /** the error message derived from error.message. */ + message: string; + }; + }; + /** reformat()rebuilds error.output using the other object properties. */ + reformat(): void; +} + +/** cache functionality via the "CatBox" module. */ +export interface ICatBoxCacheOptions { + /** a prototype function or catbox engine object. */ + engine: any; + /** 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 the default cache. If every cache includes a name, a default memory cache is provisions as well. */ + name?: string; + /** if true, allows multiple cache users to share the same segment (e.g. multiple methods using the same cache storage container). Default to false. */ + shared?: boolean; +} + +/** policy configuration for the "CatBox" module and server method options. */ +export interface ICatBoxCachePolicyOptions { + /** the cache name configured in server.cache. Defaults to the default cache. */ + cache?: string; + /** 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 server method, defaults to '#name' where 'name' is the server method name. Required when called outside of a plugin. */ + segment?: string; + /** if true, allows multiple cache provisions to share the same segment. Default to false. */ + shared?: boolean; + /** relative expiration expressed in the number of milliseconds since the item was saved in the cache. Cannot be used together with expiresAt. */ + expiresIn?: number; + /** 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. */ + expiresAt?: number; + /** 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 function(id, next) where: - id - the id string or object provided to the get() method. - next - the method called when the new item is returned with the signature function(err, value, ttl) where: - err - an error condition. - value - the new value generated. - ttl - the cache ttl value in milliseconds. Set to 0 to skip storing in the cache. Defaults to the cache global policy. */ + generateFunc?: Function; + /** 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. */ + staleIn?: number; + /** number of milliseconds to wait before checking if an item is stale. */ + staleTimeout?: number; + /** 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 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. */ + generateTimeout?: number; + /** if true, an error or timeout in the generateFunc causes the stale value to be evicted from the cache. Defaults to true */ + dropOnError?: boolean; + /** 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. */ + generateOnReadError?: boolean; + /** if false, an upstream cache write error when calling cache.get() will be passed back with the generated value when calling. Defaults to true. */ + generateIgnoreWriteError?: boolean; + /** 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 concurrent generateFunc calls beyond staleTimeout). */ + pendingGenerateTimeout?: number; +} + +/** Any connections configuration server defaults can be included to override and customize the individual connection. */ +export interface IServerConnectionOptions extends IConnectionConfigurationServerDefaults { + /** - the public hostname or IP address. Used only to set server.info.host and server.info.uri. If not configured, defaults to the operating system hostname and if not available, to 'localhost'.*/ + host?: string; + /** - sets the host name or IP address the connection will listen on.If not configured, defaults to host if present, otherwise to all available network interfaces (i.e. '0.0.0.0').Set to 127.0.0.1 or localhost to restrict connection to only those coming from the same machine.*/ + address?: string; + /** - the TCP port the connection will listen to.Defaults to an ephemeral port (0) which uses an available port when the server is started (and assigned to server.info.port).If port is a string containing a '/' character, it is used as a UNIX domain socket path and if it starts with '\.\pipe' as a Windows named pipe.*/ + port?: string | number; + /** - the full public URI without the path (e.g. 'http://example.com:8080').If present, used as the connection info.uri otherwise constructed from the connection settings.*/ + uri?: string; + /** - optional node.js HTTP (or HTTPS) http.Server object or any compatible object.If the listener needs to be manually started, set autoListen to false.If the listener uses TLS, set tls to true.*/ + listener?: any; + /** - indicates that the connection.listener will be started manually outside the framework.Cannot be specified with a port setting.Defaults to true.*/ + autoListen?: boolean; + /** caching headers configuration: */ + cache?: { + /** - an array of HTTP response status codes (e.g. 200) which are allowed to include a valid caching directive.Defaults to [200]. */ + statuses: number[]; + }; + /** - a string or string array of labels used to server.select() specific connections matching the specified labels.Defaults to an empty array [](no labels).*/ + labels?: string | string[]; + /** - used to create an HTTPS connection.The tls object is passed unchanged as options to the node.js HTTPS server as described in the node.js HTTPS documentation.Set to true when passing a listener object that has been configured to use TLS directly. */ + tls?: boolean | { key?: string; cert?: string; pfx?: string; } | Object; +} + +export interface IConnectionConfigurationServerDefaults { + /** application-specific connection configuration which can be accessed via connection.settings.app. Provides a safe place to store application configuration without potential conflicts with the framework internals. Should not be used to configure plugins which should use plugins[name]. Note the difference between connection.settings.app which is used to store configuration values and connection.app which is meant for storing run-time state. */ + app?: any; + /** if false, response content encoding is disabled. Defaults to true */ + compression?: boolean; + /** connection load limits configuration where: */ + load?: { + /** maximum V8 heap size over which incoming requests are rejected with an HTTP Server Timeout (503) response. Defaults to 0 (no limit). */ + maxHeapUsedBytes: number; + /** maximum process RSS size over which incoming requests are rejected with an HTTP Server Timeout (503) response. Defaults to 0 (no limit). */ + maxRssBytes: number; + /** maximum event loop delay duration in milliseconds over which incoming requests are rejected with an HTTP Server Timeout (503) response. Defaults to 0 (no limit). */ + maxEventLoopDelay: number; + }; + /** plugin-specific configuration which can later be accessed via connection.settings.plugins. Provides a place to store and pass connection-specific plugin configuration. plugins is an object where each key is a plugin name and the value is the configuration. Note the difference between connection.settings.plugins which is used to store configuration values and connection.plugins which is meant for storing run-time state. */ + plugins?: any; + /** controls how incoming request URIs are matched against the routing table: */ + router?: { + /** determines whether the paths '/example' and '/EXAMPLE' are considered different resources. Defaults to true. */ + isCaseSensitive: boolean; + /** removes trailing slashes on incoming paths. Defaults to false. */ + stripTrailingSlash: boolean; + }; + /** a route options object used to set the default configuration for every route. */ + routes?: IRouteAdditionalConfigurationOptions; + state?: IServerState; +} + +/** Note that the options object is deeply cloned and cannot contain any values that are unsafe to perform deep copy on.*/ +export interface IServerOptions { + /** application-specific configuration which can later be accessed via server.settings.app. Note the difference between server.settings.app which is used to store static configuration values and server.app which is meant for storing run-time state. Defaults to {}. */ + app?: any; + /** sets up server-side caching. Every server includes a default cache for storing application state. By default, a simple memory-based cache is created which has limited capacity and capabilities. hapi uses catbox for its cache which includes support for common storage solutions (e.g. Redis, MongoDB, Memcached, and Riak). Caching is only utilized if methods and plugins explicitly store their state in the cache. The server cache configuration only defines the storage container itself. cache can be assigned: + a prototype function (usually obtained by calling require() on a catbox strategy such as require('catbox-redis')). + a configuration object with the following options: + enginea prototype function or catbox engine object. + namean 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 the default cache. If every cache includes a name, a default memory cache is provisions as well. + sharedif true, allows multiple cache users to share the same segment (e.g. multiple methods using the same cache storage container). Default to false. + other options passed to the catbox strategy used. + an array of the above object for configuring multiple cache instances, each with a unique name. When an array of objects is provided, multiple cache connections are established and each array item (except one) must include a name. */ + cache?: string | ICatBoxCacheOptions | ICatBoxCacheOptions[] | any; + /** sets the default connections configuration which can be overridden by each connection where: */ + connections?: IConnectionConfigurationServerDefaults; + /** determines which logged events are sent to the console (this should only be used for development and does not affect which events are actually logged internally and recorded). Set to false to disable all console logging, or to an object*/ + debug?: boolean | { + /** - a string array of server log tags to be displayed via console.error() when the events are logged via server.log() as well as internally generated server logs. For example, to display all errors, set the option to ['error']. To turn off all console debug messages set it to false. Defaults to uncaught errors thrown in external code (these errors are handled automatically and result in an Internal Server Error response) or runtime errors due to developer error. */ + log: string[]; + /** - a string array of request log tags to be displayed via console.error() when the events are logged via request.log() as well as internally generated request logs. For example, to display all errors, set the option to ['error']. To turn off all console debug messages set it to false. Defaults to uncaught errors thrown in external code (these errors are handled automatically and result in an Internal Server Error response) or runtime errors due to developer error.*/ + request: string[]; + }; + /** file system related settings*/ + files?: { + /** sets the maximum number of file etag hash values stored in the etags cache. Defaults to 10000.*/ + etagsCacheMaxSize?: number; + }; + /** process load monitoring*/ + load?: { + /** the frequency of sampling in milliseconds. Defaults to 0 (no sampling).*/ + sampleInterval?: number; + }; + + /** options passed to the mimos module (https://github.com/hapijs/mimos) when generating the mime database used by the server and accessed via server.mime.*/ + mime?: any; + /** if true, does not load the inert (file and directory support), h2o2 (proxy support), and vision (views support) plugins automatically. The plugins can be loaded manually after construction. Defaults to false (plugins loaded). */ + minimal?: boolean; + /** plugin-specific configuration which can later be accessed via server.settings.plugins. plugins is an object where each key is a plugin name and the value is the configuration. Note the difference between server.settings.plugins which is used to store static configuration values and server.plugins which is meant for storing run-time state. Defaults to {}.*/ + plugins?: IDictionary; +} + +export interface IServerViewCompile { + (template: string, options: any): void; + (template: string, options: any, callback: (err: any, compiled: (context: any, options: any, callback: (err: any, rendered: boolean) => void) => void) => void): void; +} + +export interface IServerViewsAdditionalOptions { + /** path - the root file path used to resolve and load the templates identified when calling reply.view().Defaults to current working directory.*/ + path?: string; + /**partialsPath - the root file path where partials are located.Partials are small segments of template code that can be nested and reused throughout other templates.Defaults to no partials support (empty path). + */ + partialsPath?: string; + /**helpersPath - the directory path where helpers are located.Helpers are functions used within templates to perform transformations and other data manipulations using the template context or other inputs.Each '.js' file in the helpers directory is loaded and the file name is used as the helper name.The files must export a single method with the signature function(context) and return a string.Sub - folders are not supported and are ignored.Defaults to no helpers support (empty path).Note that jade does not support loading helpers this way.*/ + helpersPath?: string; + /**relativeTo - a base path used as prefix for path and partialsPath.No default.*/ + relativeTo?: string; + + /**layout - if set to true or a layout filename, layout support is enabled.A layout is a single template file used as the parent template for other view templates in the same engine.If true, the layout template name must be 'layout.ext' where 'ext' is the engine's extension. Otherwise, the provided filename is suffixed with the engine's extension and loaded.Disable layout when using Jade as it will handle including any layout files independently.Defaults to false.*/ + layout?: boolean | string; + /**layoutPath - the root file path where layout templates are located (using the relativeTo prefix if present). Defaults to path.*/ + layoutPath?: string; + /**layoutKeyword - the key used by the template engine to denote where primary template content should go.Defaults to 'content'.*/ + layoutKeywork?: string; + /**encoding - the text encoding used by the templates when reading the files and outputting the result.Defaults to 'utf8'.*/ + encoding?: string; + /**isCached - if set to false, templates will not be cached (thus will be read from file on every use).Defaults to true.*/ + isCached?: boolean; + /**allowAbsolutePaths - if set to true, allows absolute template paths passed to reply.view().Defaults to false.*/ + allowAbsolutePaths?: boolean; + /**allowInsecureAccess - if set to true, allows template paths passed to reply.view() to contain '../'.Defaults to false.*/ + allowInsecureAccess?: boolean; + /**compileOptions - options object passed to the engine's compile function. Defaults to empty options {}.*/ + compileOptions?: any; + /**runtimeOptions - options object passed to the returned function from the compile operation.Defaults to empty options {}.*/ + runtimeOptions?: any; + /**contentType - the content type of the engine results.Defaults to 'text/html'.*/ + contentType?: string; + /**compileMode - specify whether the engine compile() method is 'sync' or 'async'.Defaults to 'sync'.*/ + compileMode?: string; + /**context - a global context used with all templates.The global context option can be either an object or a function that takes no arguments and returns a context object.When rendering views, the global context will be merged with any context object specified on the handler or using reply.view().When multiple context objects are used, values from the global context always have lowest precedence.*/ + context?: any; +} + +export interface IServerViewsEnginesOptions extends IServerViewsAdditionalOptions { + /**- the npm module used for rendering the templates.The module object must contain: "module", the rendering function. The required function signature depends on the compileMode settings. + * If the compileMode is 'sync', the signature is compile(template, options), the return value is a function with signature function(context, options), and the method is allowed to throw errors.If the compileMode is 'async', the signature is compile(template, options, callback) where callback has the signature function(err, compiled) where compiled is a function with signature function(context, options, callback) and callback has the signature function(err, rendered).*/ + module: { + compile?(template: any, options: any): (context: any, options: any) => void; + compile?(template: any, options: any, callback: (err: any, compiled: (context: any, options: any, callback: (err: any, rendered: any) => void) => void) => void): void; + }; +} + +/**Initializes the server views manager + var Hapi = require('hapi'); + var server = new Hapi.Server(); + + server.views({ +engines: { +html: require('handlebars'), +jade: require('jade') +}, +path: '/static/templates' +}); + When server.views() is called within a plugin, the views manager is only available to plugins methods. + */ +export interface IServerViewsConfiguration extends IServerViewsAdditionalOptions { + /** - required object where each key is a file extension (e.g. 'html', 'hbr'), mapped to the npm module used for rendering the templates.Alternatively, the extension can be mapped to an object with the following options:*/ + engines: IDictionary | IServerViewsEnginesOptions; + /** defines the default filename extension to append to template names when multiple engines are configured and not explicit extension is provided for a given template. No default value.*/ + defaultExtension?: string; +} + +export interface IReplyMethods { + /** Returns control back to the framework without setting a response. If called in the handler, the response defaults to an empty payload with status code 200. + * The data argument is only used for passing back authentication data and is ignored elsewhere. */ + continue(credentialData?: any): void; + + /** Transmits a file from the file system. The 'Content-Type' header defaults to the matching mime type based on filename extension. The response flow control rules do not apply. */ + file(/** the file path. */ + path: string, + /** optional settings: */ + options?: { + /** - an optional filename to specify if sending a 'Content-Disposition' header, defaults to the basename of path*/ + filename?: string; + /** specifies whether to include the 'Content-Disposition' header with the response. Available values: + false - header is not included. This is the default value. + 'attachment' + 'inline'*/ + mode?: boolean | string; + /** if true, looks for the same filename with the '.gz' suffix for a pre-compressed version of the file to serve if the request supports content encoding. Defaults to false. */ + lookupCompressed: boolean; + }): void; + /** Concludes the handler activity by returning control over to the router with a templatized view response. + the response flow control rules apply. */ + view(/** the template filename and path, relative to the templates path configured via the server views manager. */ + template: string, + /** optional object used by the template to render context-specific result. Defaults to no context {}. */ + context?: {}, + /** optional object used to override the server's views manager configuration for this response. Cannot override isCached, partialsPath, or helpersPath which are only loaded at initialization. */ + options?: any): Response; + /** Sets a header on the response */ + header(name: string, value: string, options?: IHeaderOptions): Response; + + /** Concludes the handler activity by returning control over to the router and informing the router that a response has already been sent back directly via request.raw.res and that no further response action is needed + The response flow control rules do not apply. */ + close(options?: { + /** if false, the router will not call request.raw.res.end()) to ensure the response was ended. Defaults to true. */ + end?: boolean; + }): void; + /** Proxies the request to an upstream endpoint. + the response flow control rules do not apply. */ + + proxy(/** an object including the same keys and restrictions defined by the route proxy handler options. */ + options: IProxyHandlerConfig): void; + /** Redirects the client to the specified uri. Same as calling reply().redirect(uri). + he response flow control rules apply. */ + redirect(uri: string): ResponseRedirect; + + /** Replies with the specified response */ + response(result: any): Response; + + /** Sets a cookie on the response */ + state(name: string, value: any, options?: any): void; + + /** Clears a cookie on the response */ + unstate(name: string, options?: any): void; +} + +/** Concludes the handler activity by setting a response and returning control over to the framework where: + erran optional error response. + result an optional response payload. + Since an request can only have one response regardless if it is an error or success, the reply() method can only result in a single response value. This means that passing both an err and result will only use the err. There is no requirement for either err or result to be (or not) an Error object. The framework will simply use the first argument if present, otherwise the second. The method supports two arguments to be compatible with the common callback pattern of error first. + FLOW CONTROL: + When calling reply(), the framework waits until process.nextTick() to continue processing the request and transmit the response. This enables making changes to the returned response object before the response is sent. This means the framework will resume as soon as the handler method exits. To suspend this behavior, the returned response object supports the following methods: hold(), send() */ +export interface IReply extends IReplyMethods { + ( + err: Error, + result?: string | number | boolean | Buffer | stream.Stream | Promise | T, + /** Note that when used to return both an error and credentials in the authentication methods, reply() must be called with three arguments function(err, null, data) where data is the additional authentication information. */ + credentialData?: any): IBoom; + /** Note that if result is a Stream with a statusCode property, that status code will be used as the default response code. */ + (result: string | number | boolean | Buffer | stream.Stream | Promise | T): Response; +} + +/** Concludes the handler activity by setting a response and returning control over to the framework where: + erran optional error response. + result an optional response payload. + Since an request can only have one response regardless if it is an error or success, the reply() method can only result in a single response value. This means that passing both an err and result will only use the err. There is no requirement for either err or result to be (or not) an Error object. The framework will simply use the first argument if present, otherwise the second. The method supports two arguments to be compatible with the common callback pattern of error first. + FLOW CONTROL: + When calling reply(), the framework waits until process.nextTick() to continue processing the request and transmit the response. This enables making changes to the returned response object before the response is sent. This means the framework will resume as soon as the handler method exits. To suspend this behavior, the returned response object supports the following methods: hold(), send() */ +export interface IStrictReply extends IReplyMethods { + (err: Error, + result?: Promise | T, + /** Note that when used to return both an error and credentials in the authentication methods, reply() must be called with three arguments function(err, null, data) where data is the additional authentication information. */ + credentialData?: any): IBoom; + /** Note that if result is a Stream with a statusCode property, that status code will be used as the default response code. */ + (result: Promise | T): Response; +} + +export interface ISessionHandler { + (request: Request, reply: IReply): void; +} + +export interface IStrictSessionHandler { + (request: Request, reply: IStrictReply): void; +} + +export interface IRequestHandler { + (request: Request): T; +} + +export interface IFailAction { + (source: string, error: any, next: () => void): void; +} +/** generates a reverse proxy handler */ +export interface IProxyHandlerConfig { + /** the upstream service host to proxy requests to. The same path on the client request will be used as the path on the host.*/ + host?: string; + /** the upstream service port. */ + port?: number; + /** The protocol to use when making a request to the proxied host: + 'http' + 'https'*/ + protocol?: string; + /** an absolute URI used instead of the incoming host, port, protocol, path, and query. Cannot be used with host, port, protocol, or mapUri.*/ + uri?: string; + /** if true, forwards the headers sent from the client to the upstream service being proxied to, headers sent from the upstream service will also be forwarded to the client. Defaults to false.*/ + passThrough?: boolean; + /** localStatePassThrough - if false, any locally defined state is removed from incoming requests before being passed upstream. This is a security feature to prevent local state (e.g. authentication cookies) from leaking upstream to other servers along with the cookies intended for those servers. This value can be overridden on a per state basis via the server.state() passThrough option. Defaults to false.*/ + localStatePassThrough?: boolean; + /**acceptEncoding - if false, does not pass-through the 'Accept-Encoding' HTTP header which is useful when using an onResponse post-processing to avoid receiving an encoded response (e.g. gzipped). Can only be used together with passThrough. Defaults to true (passing header).*/ + acceptEncoding?: boolean; + /** rejectUnauthorized - sets the rejectUnauthorized property on the https agent making the request. This value is only used when the proxied server uses TLS/SSL. When set it will override the node.js rejectUnauthorized property. If false then ssl errors will be ignored. When true the server certificate is verified and an 500 response will be sent when verification fails. This shouldn't be used alongside the agent setting as the agent will be used instead. Defaults to the https agent default value of true.*/ + rejectUnauthorized?: boolean; + /**if true, sets the 'X-Forwarded-For', 'X-Forwarded-Port', 'X-Forwarded-Proto' headers when making a request to the proxied upstream endpoint. Defaults to false.*/ + xforward?: boolean; + /** the maximum number of HTTP redirections allowed, to be followed automatically by the handler. Set to false or 0 to disable all redirections (the response will contain the redirection received from the upstream service). If redirections are enabled, no redirections (301, 302, 307, 308) will be passed along to the client, and reaching the maximum allowed redirections will return an error response. Defaults to false.*/ + redirects?: boolean | number; + /**number of milliseconds before aborting the upstream request. Defaults to 180000 (3 minutes).*/ + timeout?: number; + /** a function used to map the request URI to the proxied URI. Cannot be used together with host, port, protocol, or uri. The function signature is function(request, callback) where: + request - is the incoming request object. + callback - is function(err, uri, headers) where: + err - internal error condition. + uri - the absolute proxy URI. + headers - optional object where each key is an HTTP request header and the value is the header content.*/ + mapUri?(request: Request, callback: (err: any, uri: string, headers?: { [key: string]: string }) => void): void; + /** a custom function for processing the response from the upstream service before sending to the client. Useful for custom error handling of responses from the proxied endpoint or other payload manipulation. Function signature is function(err, res, request, reply, settings, ttl) where: - err - internal or upstream error returned from attempting to contact the upstream proxy. - res - the node response object received from the upstream service. res is a readable stream (use the wreck module read method to easily convert it to a Buffer or string). - request - is the incoming request object. - reply - the reply interface function. - settings - the proxy handler configuration. - ttl - the upstream TTL in milliseconds if proxy.ttl it set to 'upstream' and the upstream response included a valid 'Cache-Control' header with 'max-age'.*/ + onResponse?( + err: any, + res: http.ServerResponse, + req: Request, + reply: IReply, + settings: IProxyHandlerConfig, + ttl: number): void; + /** if set to 'upstream', applies the upstream response caching policy to the response using the response.ttl() method (or passed as an argument to the onResponse method if provided).*/ + ttl?: number; + /** - a node http(s) agent to be used for connections to upstream server. see https://nodejs.org/api/http.html#http_class_http_agent */ + agent?: http.Agent; + /** sets the maximum number of sockets available per outgoing proxy host connection. false means use the wreck module default value (Infinity). Does not affect non-proxy outgoing client connections. Defaults to Infinity.*/ + maxSockets?: boolean | number; +} +/** TODO: fill in joi definition */ +export interface IJoi { } + +/** a validation function using the signature function(value, options, next) */ +export interface IValidationFunction { + ( + /** the object containing the path parameters. */ + value: any, + /** the server validation options. */ + options: any, + /** the callback function called when validation is completed. */ + next: (err: any, value: any) => void): void; +} +/** a custom error handler function with the signature 'function(request, reply, source, error)` */ +export interface IRouteFailFunction { + /** a custom error handler function with the signature 'function(request, reply, source, error)` */ + (/** - the [request object]. */ + request: Request, + /** the continuation reply interface. */ + reply: IReply, + /** the source of the invalid field (e.g. 'path', 'query', 'payload'). */ + source: string, + /** the error object prepared for the client response (including the validation function error under error.data). */ + error: any): void; +} + +/** Each route can be customize to change the default behavior of the request lifecycle using the following options: */ +export interface IRouteAdditionalConfigurationOptions { + /** application specific configuration.Should not be used by plugins which should use plugins[name] instead. */ + app?: any; + /** authentication configuration.Value can be: false to disable authentication if a default strategy is set. + a string with the name of an authentication strategy registered with server.auth.strategy(). + an object */ + auth?: boolean | string | + { + /** the authentication mode.Defaults to 'required' if a server authentication strategy is configured, otherwise defaults to no authentication.Available values: + 'required'authentication is required. + 'optional'authentication is optional (must be valid if present). + 'try'same as 'optional' but allows for invalid authentication. */ + mode?: string; + /** a string array of strategy names in order they should be attempted.If only one strategy is used, strategy can be used instead with the single string value.Defaults to the default authentication strategy which is available only when a single strategy is configured. */ + strategies?: string | string[]; + /** if set, the payload (in requests other than 'GET' and 'HEAD') is authenticated after it is processed.Requires a strategy with payload authentication support (e.g.Hawk).Cannot be set to a value other than 'required' when the scheme sets the options.payload to true.Available values: + falseno payload authentication.This is the default value. + 'required'payload authentication required.This is the default value when the scheme sets options.payload to true. + 'optional'payload authentication performed only when the client includes payload authentication information (e.g.hash attribute in Hawk). */ + payload?: string; + /** the application scope required to access the route.Value can be a scope string or an array of scope strings.The authenticated credentials object scope property must contain at least one of the scopes defined to access the route.Set to false to remove scope requirements.Defaults to no scope required. */ + scope?: string | string[] | boolean; + /** the required authenticated entity type.If set, must match the entity value of the authentication credentials.Available values: + anythe authentication can be on behalf of a user or application.This is the default value. + userthe authentication must be on behalf of a user. + appthe authentication must be on behalf of an application. */ + entity?: string; + /** + * an object or array of objects specifying the route access rules. Each rule is evaluated against an incoming + * request and access is granted if at least one rule matches. Each rule object must include at least one of: + */ + access?: IRouteAdditionalConfigurationAuthAccess | IRouteAdditionalConfigurationAuthAccess[]; + }; + /** an object passed back to the provided handler (via this) when called. */ + bind?: any; + /** if the route method is 'GET', the route can be configured to include caching directives in the response using the following options */ + cache?: IRouteAdditionalConfigurationCache; + /** the Cross- Origin Resource Sharing protocol allows browsers to make cross- origin API calls.CORS is required by web applications running inside a browser which are loaded from a different domain than the API server.CORS headers are disabled by default. To enable, set cors to true, or to an object with the following options: */ + cors?: { + /** a strings array of allowed origin servers ('Access-Control-Allow-Origin').The array can contain any combination of fully qualified origins along with origin strings containing a wildcard '' character, or a single `''origin string. Defaults to any origin['*']`. */ + origin?: string[]; + /** if true, matches the value of the incoming 'Origin' header to the list of origin values ('*' matches anything) and if a match is found, uses that as the value of the 'Access-Control-Allow-Origin' response header.When false, the origin config is returned as- is.Defaults to true. */ + matchOrigin?: boolean; + /** if false, prevents the connection from returning the full list of non- wildcard origin values if the incoming origin header does not match any of the values.Has no impact if matchOrigin is set to false.Defaults to true. */ + isOriginExposed?: boolean; + /** number of seconds the browser should cache the CORS response ('Access-Control-Max-Age').The greater the value, the longer it will take before the browser checks for changes in policy.Defaults to 86400 (one day). */ + maxAge?: number; + /** a strings array of allowed headers ('Access-Control-Allow-Headers').Defaults to ['Authorization', 'Content-Type', 'If-None-Match']. */ + headers?: string[]; + /** a strings array of additional headers to headers.Use this to keep the default headers in place. */ + additionalHeaders?: string[]; + /** a strings array of allowed HTTP methods ('Access-Control-Allow-Methods').Defaults to ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'OPTIONS']. */ + methods?: string[]; + /** a strings array of additional methods to methods.Use this to keep the default methods in place. */ + additionalMethods?: string[]; + /** a strings array of exposed headers ('Access-Control-Expose-Headers').Defaults to ['WWW-Authenticate', 'Server-Authorization']. */ + exposedHeaders?: string[]; + /** a strings array of additional headers to exposedHeaders.Use this to keep the default headers in place. */ + additionalExposedHeaders?: string[]; + /** if true, allows user credentials to be sent ('Access-Control-Allow-Credentials').Defaults to false. */ + credentials?: boolean; + /** if false, preserves existing CORS headers set manually before the response is sent.Defaults to true. */ + override?: boolean; + }; + /** defines the behavior for serving static resources using the built-in route handlers for files and directories: */ + files?: {/** determines the folder relative paths are resolved against when using the file and directory handlers. */ + relativeTo: string; + }; + + /** an alternative location for the route handler option. */ + handler?: ISessionHandler | IStrictSessionHandler | string | IRouteHandlerConfig; + /** an optional unique identifier used to look up the route using server.lookup(). */ + id?: number; + /** optional arguments passed to JSON.stringify() when converting an object or error response to a string payload.Supports the following: */ + json?: { + /** the replacer function or array.Defaults to no action. */ + replacer?: Function | string[]; + /** number of spaces to indent nested object keys.Defaults to no indentation. */ + space?: number | string; + /** string suffix added after conversion to JSON string.Defaults to no suffix. */ + suffix?: string; + }; + /** enables JSONP support by setting the value to the query parameter name containing the function name used to wrap the response payload.For example, if the value is 'callback', a request comes in with 'callback=me', and the JSON response is '{ "a":"b" }', the payload will be 'me({ "a":"b" });'.Does not work with stream responses. */ + jsonp?: string; + /** determines how the request payload is processed: */ + payload?: { + /** the type of payload representation requested. The value must be one of: + 'data'the incoming payload is read fully into memory.If parse is true, the payload is parsed (JSON, formdecoded, multipart) based on the 'Content- Type' header.If parse is false, the raw Buffer is returned.This is the default value except when a proxy handler is used. + 'stream'the incoming payload is made available via a Stream.Readable interface.If the payload is 'multipart/form-data' and parse is true, fields values are presented as text while files are provided as streams.File streams from a 'multipart/form-data' upload will also have a property hapi containing filename and headers properties. + 'file'the incoming payload in written to temporary file in the directory specified by the server's payload.uploads settings. If the payload is 'multipart/ formdata' and parse is true, fields values are presented as text while files are saved. Note that it is the sole responsibility of the application to clean up the files generated by the framework. This can be done by keeping track of which files are used (e.g. using the request.app object), and listening to the server 'response' event to perform any needed cleaup. */ + output?: string; + /** can be true, false, or gunzip; determines if the incoming payload is processed or presented raw. true and gunzip includes gunzipping when the appropriate 'Content-Encoding' is specified on the received request. If parsing is enabled and the 'Content-Type' is known (for the whole payload as well as parts), the payload is converted into an object when possible. If the format is unknown, a Bad Request (400) error response is sent. Defaults to true, except when a proxy handler is used. The supported mime types are: + 'application/json' + 'application/x-www-form-urlencoded' + 'application/octet-stream' + 'text/ *' + 'multipart/form-data' */ + parse?: string | boolean; + /** a string or an array of strings with the allowed mime types for the endpoint.Defaults to any of the supported mime types listed above.Note that allowing other mime types not listed will not enable them to be parsed, and that if parsing mode is 'parse', the request will result in an error response. */ + allow?: string | string[]; + /** a mime type string overriding the 'Content-Type' header value received.Defaults to no override. */ + override?: string; + /** limits the size of incoming payloads to the specified byte count.Allowing very large payloads may cause the server to run out of memory.Defaults to 1048576 (1MB). */ + maxBytes?: number; + /** payload reception timeout in milliseconds.Sets the maximum time allowed for the client to transmit the request payload (body) before giving up and responding with a Request Timeout (408) error response.Set to false to disable.Defaults to 10000 (10 seconds). */ + timeout?: number; + /** the directory used for writing file uploads.Defaults to os.tmpDir(). */ + uploads?: string; + /** determines how to handle payload parsing errors. Allowed values are: + 'error'return a Bad Request (400) error response. This is the default value. + 'log'report the error but continue processing the request. + 'ignore'take no action and continue processing the request. */ + failAction?: string; + }; + /** pluginspecific configuration.plugins is an object where each key is a plugin name and the value is the plugin configuration. */ + plugins?: IDictionary; + /** an array with [route prerequisites] methods which are executed in serial or in parallel before the handler is called. */ + pre?: any[]; + /** validation rules for the outgoing response payload (response body).Can only validate object response: */ + response?: { + /** the default HTTP status code when the payload is empty. Value can be 200 or 204. + Note that a 200 status code is converted to a 204 only at the time or response transmission + (the response status code will remain 200 throughout the request lifecycle unless manually set). Defaults to 200. */ + emptyStatusCode?: number; + /** the default response object validation rules (for all non-error responses) expressed as one of: + true - any payload allowed (no validation performed). This is the default. + false - no payload allowed. + a Joi validation object. + a validation function using the signature function(value, options, next) where: + value - the object containing the response object. + options - the server validation options. + next(err) - the callback function called when validation is completed. */ + schema?: boolean | any; + /** HTTP status- codespecific validation rules.The status key is set to an object where each key is a 3 digit HTTP status code and the value has the same definition as schema.If a response status code is not present in the status object, the schema definition is used, expect for errors which are not validated by default. */ + status?: { [statusCode: number]: boolean | any }; + /** the percent of responses validated (0100).Set to 0 to disable all validation.Defaults to 100 (all responses). */ + sample?: number; + /** defines what to do when a response fails validation.Options are: + errorreturn an Internal Server Error (500) error response.This is the default value. + loglog the error but send the response. */ + failAction?: string; + /** if true, applies the validation rule changes to the response.Defaults to false. */ + modify?: boolean; + /** options to pass to Joi.Useful to set global options such as stripUnknown or abortEarly (the complete list is available here: https://github.com/hapijs/joi#validatevalue-schema-options-callback ).Defaults to no options. */ + options?: any; + }; + /** sets common security headers (disabled by default).To enable set security to true or to an object with the following options */ + security?: boolean | { + /** controls the 'Strict-Transport-Security' header.If set to true the header will be set to max- age=15768000, if specified as a number the maxAge parameter will be set to that number.Defaults to true.You may also specify an object with the following fields: */ + hsts?: boolean | number | { + /** the max- age portion of the header, as a number.Default is 15768000. */ + maxAge?: number; + /** a boolean specifying whether to add the includeSubdomains flag to the header. */ + includeSubdomains?: boolean; + /** a boolean specifying whether to add the 'preload' flag (used to submit domains inclusion in Chrome's HTTP Strict Transport Security (HSTS) preload list) to the header. */ + preload?: boolean; + }; + /** controls the 'X-Frame-Options' header.When set to true the header will be set to DENY, you may also specify a string value of 'deny' or 'sameorigin'.To use the 'allow-from' rule, you must set this to an object with the following fields: */ + xframe?: { + /** either 'deny', 'sameorigin', or 'allow-from' */ + rule: string; + /** when rule is 'allow-from' this is used to form the rest of the header, otherwise this field is ignored.If rule is 'allow-from' but source is unset, the rule will be automatically changed to 'sameorigin'. */ + source: string; + }; + /** boolean that controls the 'X-XSS-PROTECTION' header for IE.Defaults to true which sets the header to equal '1; mode=block'.NOTE: This setting can create a security vulnerability in versions of IE below 8, as well as unpatched versions of IE8.See here and here for more information.If you actively support old versions of IE, it may be wise to explicitly set this flag to false. */ + xss?: boolean; + /** boolean controlling the 'X-Download-Options' header for IE, preventing downloads from executing in your context.Defaults to true setting the header to 'noopen'. */ + noOpen?: boolean; + /** boolean controlling the 'X-Content-Type-Options' header.Defaults to true setting the header to its only and default option, 'nosniff'. */ + noSniff?: boolean; + }; + /** HTTP state management (cookies) allows the server to store information on the client which is sent back to the server with every request (as defined in RFC 6265).state supports the following options: */ + state?: { + /** determines if incoming 'Cookie' headers are parsed and stored in the request.state object.Defaults to true. */ + parse: boolean; + /** determines how to handle cookie parsing errors.Allowed values are: + 'error'return a Bad Request (400) error response.This is the default value. + 'log'report the error but continue processing the request. + 'ignore'take no action. */ + failAction: string; + }; + /** request input validation rules for various request components.When using a Joi validation object, the values of the other inputs (i.e.headers, query, params, payload, and auth) are made available under the validation context (accessible in rules as Joi.ref('$query.key')).Note that validation is performed in order(i.e.headers, params, query, payload) and if type casting is used (converting a string to number), the value of inputs not yet validated will reflect the raw, unvalidated and unmodified values.The validate object supports: */ + validate?: { + /** validation rules for incoming request headers.Values allowed: + * trueany headers allowed (no validation performed).This is the default. + falseno headers allowed (this will cause all valid HTTP requests to fail). + a Joi validation object. + a validation function using the signature function(value, options, next) where: + valuethe object containing the request headers. + optionsthe server validation options. + next(err, value)the callback function called when validation is completed. + */ + headers?: boolean | IJoi | IValidationFunction; + + /** validation rules for incoming request path parameters, after matching the path against the route and extracting any parameters then stored in request.params.Values allowed: + trueany path parameters allowed (no validation performed).This is the default. + falseno path variables allowed. + a Joi validation object. + a validation function using the signature function(value, options, next) where: + valuethe object containing the path parameters. + optionsthe server validation options. + next(err, value)the callback function called when validation is completed. */ + params?: boolean | IJoi | IValidationFunction; + /** validation rules for an incoming request URI query component (the key- value part of the URI between '?' and '#').The query is parsed into its individual key- value pairs (using the qs module) and stored in request.query prior to validation.Values allowed: + trueany query parameters allowed (no validation performed).This is the default. + falseno query parameters allowed. + a Joi validation object. + a validation function using the signature function(value, options, next) where: + valuethe object containing the query parameters. + optionsthe server validation options. + next(err, value)the callback function called when validation is completed. */ + query?: boolean | IJoi | IValidationFunction; + /** validation rules for an incoming request payload (request body).Values allowed: + trueany payload allowed (no validation performed).This is the default. + falseno payload allowed. + a Joi validation object. + a validation function using the signature function(value, options, next) where: + valuethe object containing the payload object. + optionsthe server validation options. + next(err, value)the callback function called when validation is completed. */ + payload?: boolean | IJoi | IValidationFunction; + /** an optional object with error fields copied into every validation error response. */ + errorFields?: any; + /** determines how to handle invalid requests.Allowed values are: + 'error'return a Bad Request (400) error response.This is the default value. + 'log'log the error but continue processing the request. + 'ignore'take no action. + OR a custom error handler function with the signature 'function(request, reply, source, error)` where: + requestthe request object. + replythe continuation reply interface. + sourcethe source of the invalid field (e.g. 'path', 'query', 'payload'). + errorthe error object prepared for the client response (including the validation function error under error.data). */ + failAction?: string | IRouteFailFunction; + /** options to pass to Joi.Useful to set global options such as stripUnknown or abortEarly (the complete list is available here: https://github.com/hapijs/joi#validatevalue-schema-options-callback ).Defaults to no options. */ + options?: any; + }; + /** define timeouts for processing durations: */ + timeout?: { + /** response timeout in milliseconds.Sets the maximum time allowed for the server to respond to an incoming client request before giving up and responding with a Service Unavailable (503) error response.Disabled by default (false). */ + server: boolean | number; + /** by default, node sockets automatically timeout after 2 minutes.Use this option to override this behavior.Defaults to undefined which leaves the node default unchanged.Set to false to disable socket timeouts. */ + socket: boolean | number; + }; + + /** ONLY WHEN ADDING NEW ROUTES (not when setting defaults). + *route description used for generating documentation (string). + */ + description?: string; + /** ONLY WHEN ADDING NEW ROUTES (not when setting defaults). + *route notes used for generating documentation (string or array of strings). + */ + notes?: string | string[]; + /** ONLY WHEN ADDING NEW ROUTES (not when setting defaults). + *route tags used for generating documentation (array of strings). + */ + tags?: string[]; + + /** Enable logging of routes + */ + log?: boolean; +} + +/** + * specifying the route access rules. Each rule is evaluated against an incoming request and access is granted if at least one rule matches + */ +export interface IRouteAdditionalConfigurationAuthAccess { + /** + * the application scope required to access the route. Value can be a scope string or an array of scope strings. + * The authenticated credentials object scope property must contain at least one of the scopes defined to access the route. + * If a scope string begins with a + character, that scope is required. If a scope string begins with a ! character, + * that scope is forbidden. For example, the scope ['!a', '+b', 'c', 'd'] means the incoming request credentials' + * scope must not include 'a', must include 'b', and must include on of 'c' or 'd'. You may also access properties + * on the request object (query and params} to populate a dynamic scope by using {} characters around the property name, + * such as 'user-{params.id}'. Defaults to false (no scope requirements). + */ + scope?: string | string[] | boolean; + /** the required authenticated entity type. If set, must match the entity value of the authentication credentials. Available values: + * any - the authentication can be on behalf of a user or application. This is the default value. + * user - the authentication must be on behalf of a user which is identified by the presence of a user attribute in the credentials object returned by the authentication strategy. + * app - the authentication must be on behalf of an application which is identified by the lack of presence of a user attribute in the credentials object returned by the authentication strategy. + */ + entity?: string; +} + +export type IRouteAdditionalConfigurationCache = { + /** determines the privacy flag included in clientside caching using the 'Cache-Control' header. Values are: + 'Default': no privacy flag.This is the default setting. + 'public': mark the response as suitable for public caching. + 'private': mark the response as suitable only for private caching. */ + privacy?: 'default' | 'public' | 'private'; + /** an array of HTTP response status codes (e.g. 200) which are allowed to include a valid caching directive. Defaults to [200]. */ + statuses?: number[]; + /** a string with the value of the 'Cache-Control' header when caching is disabled. Defaults to 'no-cache'. */ + otherwise?: string; +} & ({ + /** relative expiration expressed in the number of milliseconds since the item was saved in the cache. Cannot be used together with expiresAt. */ + expiresIn: number; +} | { + /** time of day expressed in 24h notation using the 'HH:MM' format, at which point all cache records for the route expire. Cannot be used together with expiresIn. */ + expiresAt: string; +} | {}); + +/** server.realm http://hapijs.com/api#serverrealm + The realm object contains server-wide or plugin-specific state that can be shared across various methods. For example, when calling server.bind(), + the active realm settings.bind property is set which is then used by routes and extensions added at the same level (server root or plugin). + Realms are a limited version of a sandbox where plugins can maintain state used by the framework when adding routes, extensions, and other properties. + The server.realm object should be considered read-only and must not be changed directly except for the plugins property can be directly manipulated by the plugins (each setting its own under plugins[name]). + exports.register = function (server, options, next) { +console.log(server.realm.modifiers.route.prefix); +return next(); +}; + */ +export interface IServerRealm { + /** when the server object is provided as an argument to the plugin register() method, modifiers provides the registration preferences passed the server.register() method */ + modifiers: { + /** routes preferences: */ + route: { + /** - the route path prefix used by any calls to server.route() from the server. */ + prefix: string; + /** the route virtual host settings used by any calls to server.route() from the server. */ + vhost: string; + }; + }; + /** the active plugin name (empty string if at the server root). */ + plugin: string; + /** plugin-specific state to be shared only among activities sharing the same active state. plugins is an object where each key is a plugin name and the value is the plugin state. */ + plugins: IDictionary; + /** settings overrides */ + settings: { + files: { + relativeTo: any; + }; + bind: any; + }; +} +/** server.state(name, [options]) http://hapijs.com/api#serverstatename-options + HTTP state management uses client cookies to persist a state across multiple requests. Registers a cookie definitions where:*/ +export interface IServerState { + /** - the cookie name string. */ + name: string; + + /** - are the optional cookie settings: */ + options: { + /** - time - to - live in milliseconds.Defaults to null (session time- life - cookies are deleted when the browser is closed).*/ + ttl: number; + /** - sets the 'Secure' flag.Defaults to false.*/ + isSecure: boolean; + /** - sets the 'HttpOnly' flag.Defaults to false.*/ + isHttpOnly: boolean + /** - the path scope.Defaults to null (no path).*/ + path: any; + /** - the domain scope.Defaults to null (no domain). */ + domain: any; + /** if present and the cookie was not received from the client or explicitly set by the route handler, the cookie is automatically added to the response with the provided value. The value can be a function with signature function(request, next) where: + request - the request object. + next - the continuation function using the function(err, value) signature.*/ + autoValue(request: Request, next: (err: any, value: any) => void): void; + /** - encoding performs on the provided value before serialization. Options are: + 'none' - no encoding. When used, the cookie value must be a string. This is the default value. + 'base64' - string value is encoded using Base64. + 'base64json' - object value is JSON-stringified than encoded using Base64. + 'form' - object value is encoded using the x-www-form-urlencoded method. + 'iron' - Encrypts and sign the value using iron.*/ + encoding: string; + /** - an object used to calculate an HMAC for cookie integrity validation.This does not provide privacy, only a mean to verify that the cookie value was generated by the server.Redundant when 'iron' encoding is used.Options are:*/ + sign: { + /** - algorithm options.Defaults to require('iron').defaults.integrity.*/ + integrity: any; + /** - password used for HMAC key generation.*/ + password: string; + }; + /** - password used for 'iron' encoding.*/ + password: string; + /** - options for 'iron' encoding.Defaults to require('iron').defaults.*/ + iron: any; + /** - if false, errors are ignored and treated as missing cookies.*/ + ignoreErrors: boolean; + /** - if true, automatically instruct the client to remove invalid cookies.Defaults to false.*/ + clearInvalid: boolean; + /** - if false, allows any cookie value including values in violation of RFC 6265. Defaults to true.*/ + strictHeader: boolean; + /** - overrides the default proxy localStatePassThrough setting.*/ + passThrough: any; + }; +} + +export interface IFileHandlerConfig { + /** a path string or function as described above.*/ + path: string; + /** an optional filename to specify if sending a 'Content-Disposition' header, defaults to the basename of path*/ + filename?: string; + /**- specifies whether to include the 'Content-Disposition' header with the response. Available values: + false - header is not included. This is the default value. + 'attachment' + 'inline'*/ + mode?: boolean | string; + /** if true, looks for the same filename with the '.gz' suffix for a pre-compressed version of the file to serve if the request supports content encoding. Defaults to false.*/ + lookupCompressed: boolean; +} + +/**http://hapijs.com/api#route-handler + Built-in handlers + + The framework comes with a few built-in handler types available by setting the route handler config to an object containing one of these keys.*/ +export interface IRouteHandlerConfig { + /** generates a static file endpoint for serving a single file. file can be set to: + a relative or absolute file path string (relative paths are resolved based on the route files configuration). + a function with the signature function(request) which returns the relative or absolute file path. + an object with the following options */ + file?: string | IRequestHandler | IFileHandlerConfig; + /** directory - generates a directory endpoint for serving static content from a directory. Routes using the directory handler must include a path parameter at the end of the path string (e.g. /path/to/somewhere/{param} where the parameter name does not matter). The path parameter can use any of the parameter options (e.g. {param} for one level files only, {param?} for one level files or the directory root, {param*} for any level, or {param*3} for a specific level). If additional path parameters are present, they are ignored for the purpose of selecting the file system resource. The directory handler is an object with the following options: + path - (required) the directory root path (relative paths are resolved based on the route files configuration). Value can be: + a single path string used as the prefix for any resources requested by appending the request path parameter to the provided string. + an array of path strings. Each path will be attempted in order until a match is found (by following the same process as the single path string). + a function with the signature function(request) which returns the path string or an array of path strings. If the function returns an error, the error is passed back to the client in the response. + index - optional boolean|string|string[], determines if an index file will be served if found in the folder when requesting a directory. The given string or strings specify the name(s) of the index file to look for. If true, looks for 'index.html'. Any falsy value disables index file lookup. Defaults to true. + listing - optional boolean, determines if directory listing is generated when a directory is requested without an index document. Defaults to false. + showHidden - optional boolean, determines if hidden files will be shown and served. Defaults to false. + redirectToSlash - optional boolean, determines if requests for a directory without a trailing slash are redirected to the same path with the missing slash. Useful for ensuring relative links inside the response are resolved correctly. Disabled when the server config router.stripTrailingSlash is true.Defaults to false. + lookupCompressed - optional boolean, instructs the file processor to look for the same filename with the '.gz' suffix for a pre-compressed version of the file to serve if the request supports content encoding. Defaults to false. + defaultExtension - optional string, appended to file requests if the requested file is not found. Defaults to no extension.*/ + directory?: { + path: string | string[] | IRequestHandler | IRequestHandler; + index?: boolean | string | string[]; + listing?: boolean; + showHidden?: boolean; + redirectToSlash?: boolean; + lookupCompressed?: boolean; + defaultExtension?: string; + }; + proxy?: IProxyHandlerConfig; + view?: string | { + template: string; + context: { + payload: any; + params: any; + query: any; + pre: any; + } + }; + config?: { + handler: any; + bind: any; + app: any; + plugins: { + [name: string]: any; + }; + pre: Array<() => void>; + validate: { + headers: any; + params: any; + query: any; + payload: any; + errorFields?: any; + failAction?: string | IFailAction; + }; + payload: { + output: { + data: any; + stream: any; + file: any; + }; + parse?: any; + allow?: string | string[]; + override?: string; + maxBytes?: number; + uploads?: number; + failAction?: string; + }; + response: { + schema: any; + sample: number; + failAction: string; + }; + cache: { + privacy: string; + expiresIn: number; + expiresAt: number; + }; + auth: string | boolean | { + mode: string; + strategies: string[]; + payload?: boolean | string; + tos?: boolean | string; + scope?: string | string[]; + entity: string; + }; + cors?: boolean; + jsonp?: string; + description?: string; + notes?: string | string[]; + tags?: string[]; + }; +} +/** Route configuration + The route configuration object*/ +export interface IRouteConfiguration { + /** - (required) the absolute path used to match incoming requests (must begin with '/'). Incoming requests are compared to the configured paths based on the connection router configuration option.The path can include named parameters enclosed in {} which will be matched against literal values in the request as described in Path parameters.*/ + path: string; + /** - (required) the HTTP method.Typically one of 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', or 'OPTIONS'.Any HTTP method is allowed, except for 'HEAD'.Use '*' to match against any HTTP method (only when an exact match was not found, and any match with a specific method will be given a higher priority over a wildcard match). + * Can be assigned an array of methods which has the same result as adding the same route with different methods manually.*/ + method: string | string[]; + /** - an optional domain string or an array of domain strings for limiting the route to only requests with a matching host header field.Matching is done against the hostname part of the header only (excluding the port).Defaults to all hosts.*/ + vhost?: string; + /** - (required) the function called to generate the response after successful authentication and validation.The handler function is described in Route handler.If set to a string, the value is parsed the same way a prerequisite server method string shortcut is processed.Alternatively, handler can be assigned an object with a single key using the name of a registered handler type and value with the options passed to the registered handler.*/ + handler?: ISessionHandler | IStrictSessionHandler | string | IRouteHandlerConfig; + /** - additional route options.*/ + config?: IRouteAdditionalConfigurationOptions; +} +/** Route public interface When route information is returned or made available as a property. http://hapijs.com/api#route-public-interface */ +export interface IRoute { + /** the route HTTP method. */ + method: string; + /** the route path. */ + path: string; + /** the route vhost option if configured. */ + vhost?: string | string[]; + /** the [active realm] associated with the route.*/ + realm: IServerRealm; + /** the [route options] object with all defaults applied. */ + settings: IRouteAdditionalConfigurationOptions; +} + +export interface IServerAuthScheme { + /** authenticate(request, reply) - required function called on each incoming request configured with the authentication scheme where: + request - the request object. + reply - the reply interface the authentication method must call when done authenticating the request where: + reply(err, response, result) - is called if authentication failed where: + err - any authentication error. + response - any authentication response action such as redirection. Ignored if err is present, otherwise required. + result - an object containing: + credentials - the authenticated credentials. + artifacts - optional authentication artifacts. + reply.continue(result) - is called if authentication succeeded where: + result - same object as result above. + When the scheme authenticate() method implementation calls reply() with an error condition, the specifics of the error affect whether additional authentication strategies will be attempted if configured for the route. + .If the err returned by the reply() method includes a message, no additional strategies will be attempted. + If the err does not include a message but does include a scheme name (e.g. Boom.unauthorized(null, 'Custom')), additional strategies will be attempted in order of preference. + var server = new Hapi.Server(); + server.connection({ port: 80 }); + var scheme = function (server, options) { + return { + authenticate: function (request, reply) { + var req = request.raw.req; + var authorization = req.headers.authorization; + if (!authorization) { + return reply(Boom.unauthorized(null, 'Custom')); + } + return reply(null, { credentials: { user: 'john' } }); + } + }; + }; + server.auth.scheme('custom', scheme);*/ + authenticate(request: Request, reply: IReply): void; + authenticate(request: Request, reply: IStrictReply): void; + /** payload(request, reply) - optional function called to authenticate the request payload where: + request - the request object. + reply(err, response) - is called if authentication failed where: + err - any authentication error. + response - any authentication response action such as redirection. Ignored if err is present, otherwise required. + reply.continue() - is called if payload authentication succeeded. + When the scheme payload() method returns an error with a message, it means payload validation failed due to bad payload. If the error has no message but includes a scheme name (e.g. Boom.unauthorized(null, 'Custom')), authentication may still be successful if the route auth.payload configuration is set to 'optional'.*/ + payload?(request: Request, reply: IReply): void; + payload?(request: Request, reply: IStrictReply): void; + /** response(request, reply) - optional function called to decorate the response with authentication headers before the response headers or payload is written where: + request - the request object. + reply(err, response) - is called if an error occurred where: + err - any authentication error. + response - any authentication response to send instead of the current response. Ignored if err is present, otherwise required. + reply.continue() - is called if the operation succeeded.*/ + response?(request: Request, reply: IReply): void; + response?(request: Request, reply: IStrictReply): void; + /** an optional object */ + options?: { + /** if true, requires payload validation as part of the scheme and forbids routes from disabling payload auth validation. Defaults to false.*/ + payload: boolean; + }; +} + +/**the response object where: + statusCode - the HTTP status code. + headers - an object containing the headers set. + payload - the response payload string. + rawPayload - the raw response payload buffer. + raw - an object with the injection request and response objects: + req - the simulated node request object. + res - the simulated node response object. + result - the raw handler response (e.g. when not a stream or a view) before it is serialized for transmission. If not available, the value is set to payload. Useful for inspection and reuse of the internal objects returned (instead of parsing the response string). + request - the request object.*/ +export interface IServerInjectResponse { + statusCode: number; + headers: IDictionary; + payload: string; + rawPayload: Buffer; + raw: { + req: http.IncomingMessage; + res: http.ServerResponse + }; + result: any; + request: Request; +} + +export interface IServerInject { + (options: string | IServerInjectOptions, callback: (res: IServerInjectResponse) => void): void; + (options: string | IServerInjectOptions): Promise; +} + +export interface IServerInjectOptions { + /** the request HTTP method (e.g. 'POST'). Defaults to 'GET'.*/ + method: string; + /** the request URL. If the URI includes an authority (e.g. 'example.com:8080'), it is used to automatically set an HTTP 'Host' header, unless one was specified in headers.*/ + url: string; + /** an object with optional request headers where each key is the header name and the value is the header content. Defaults to no additions to the default Shot headers.*/ + headers?: IDictionary; + /** n optional string, buffer or object containing the request payload. In case of an object it will be converted to a string for you. Defaults to no payload. Note that payload processing defaults to 'application/json' if no 'Content-Type' header provided.*/ + payload?: string | {} | Buffer; + /** an optional credentials object containing authentication information. The credentials are used to bypass the default authentication strategies, and are validated directly as if they were received via an authentication scheme. Defaults to no credentials.*/ + credentials?: any; + /** an optional artifacts object containing authentication artifact information. The artifacts are used to bypass the default authentication strategies, and are validated directly as if they were received via an authentication scheme. Ignored if set without credentials. Defaults to no artifacts.*/ + artifacts?: any; + /** sets the initial value of request.app*/ + app?: any; + /** sets the initial value of request.plugins*/ + plugins?: any; + /** allows access to routes with config.isInternal set to true. Defaults to false.*/ + allowInternals?: boolean; + /** sets the remote address for the incoming connection.*/ + remoteAddress?: boolean; + /**object with options used to simulate client request stream conditions for testing: + error - if true, emits an 'error' event after payload transmission (if any). Defaults to false. + close - if true, emits a 'close' event after payload transmission (if any). Defaults to false. + end - if false, does not end the stream. Defaults to true.*/ + simulate?: { + error: boolean; + close: boolean; + end: boolean; + }; +} + +/** host - optional host to filter routes matching a specific virtual host. Defaults to all virtual hosts. + The return value is an array where each item is an object containing: + info - the connection.info the connection the table was generated for. + labels - the connection labels. + table - an array of routes where each route contains: + settings - the route config with defaults applied. + method - the HTTP method in lower case. + path - the route path.*/ +export interface IConnectionTable { + info: any; + labels: any; + table: IRoute[]; +} + +export interface ICookieSettings { + /** - time - to - live in milliseconds.Defaults to null (session time- life - cookies are deleted when the browser is closed).*/ + ttl?: number; + /** - sets the 'Secure' flag.Defaults to false.*/ + isSecure?: boolean; + /** - sets the 'HttpOnly' flag.Defaults to false.*/ + isHttpOnly?: boolean; + /** - the path scope.Defaults to null (no path).*/ + path?: string; + /** - the domain scope.Defaults to null (no domain).*/ + domain?: any; + /** - if present and the cookie was not received from the client or explicitly set by the route handler, the cookie is automatically added to the response with the provided value.The value can be a function with signature function(request, next) where: + request - the request object. + next - the continuation function using the function(err, value) signature.*/ + autoValue?(request: Request, next: (err: any, value: any) => void): void; + /** - encoding performs on the provided value before serialization.Options are: + 'none' - no encoding.When used, the cookie value must be a string.This is the default value. + 'base64' - string value is encoded using Base64. + 'base64json' - object value is JSON- stringified than encoded using Base64. + 'form' - object value is encoded using the x- www - form - urlencoded method. */ + encoding?: string; + /** - an object used to calculate an HMAC for cookie integrity validation.This does not provide privacy, only a mean to verify that the cookie value was generated by the server.Redundant when 'iron' encoding is used.Options are: + integrity - algorithm options.Defaults to require('iron').defaults.integrity. + password - password used for HMAC key generation. */ + sign?: { integrity: any; password: string; }; + password?: string; + iron?: any; + ignoreErrors?: boolean; + clearInvalid?: boolean; + strictHeader?: boolean; + passThrough?: any; +} + +/** method - the method function with the signature is one of: + function(arg1, arg2, ..., argn, next) where: + arg1, arg2, etc. - the method function arguments. + next - the function called when the method is done with the signature function(err, result, ttl) where: + err - error response if the method failed. + result - the return value. + ttl - 0 if result is valid but cannot be cached. Defaults to cache policy. + function(arg1, arg2, ..., argn) where: + arg1, arg2, etc. - the method function arguments. + the callback option is set to false. + the method must returns a value (result, Error, or a promise) or throw an Error.*/ +export interface IServerMethod { + // (): void; + // (next: (err: any, result: any, ttl: number) => void): void; + // (arg1: any): void; + // (arg1: any, arg2: any, next: (err: any, result: any, ttl: number) => void): void; + // (arg1: any, arg2: any): void; + (...args: any[]): void; +} +/** options - optional configuration: + bind - a context object passed back to the method function (via this) when called. Defaults to active context (set via server.bind() when the method is registered. + cache - the same cache configuration used in server.cache(). + callback - if false, expects the method to be a synchronous function. Note that using a synchronous function with caching will convert the method interface to require a callback as an additional argument with the signature function(err, result, cached, report) since the cache interface cannot return values synchronously. Defaults to true. + generateKey - a function used to generate a unique key (for caching) from the arguments passed to the method function (the callback argument is not passed as input). The server will automatically generate a unique key if the function's arguments are all of types 'string', 'number', or 'boolean'. However if the method uses other types of arguments, a key generation function must be provided which takes the same arguments as the function and returns a unique string (or null if no key can be generated).*/ +export interface IServerMethodOptions { + bind?: any; + cache?: ICatBoxCachePolicyOptions; + callback?: boolean; + generateKey?(args: any[]): string; +} +/** Request object + + The request object is created internally for each incoming request. It is different from the node.js request object received from the HTTP server callback (which is available in request.raw.req). The request object methods and properties change throughout the request lifecycle. + Request events + + The request object supports the following events: + + 'peek' - emitted for each chunk of payload data read from the client connection. The event method signature is function(chunk, encoding). + 'finish' - emitted when the request payload finished reading. The event method signature is function (). + 'disconnect' - emitted when a request errors or aborts unexpectedly. + var Crypto = require('crypto'); + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + + server.ext('onRequest', function (request, reply) { + +var hash = Crypto.createHash('sha1'); +request.on('peek', function (chunk) { + +hash.update(chunk); +}); + +request.once('finish', function () { + +console.log(hash.digest('hex')); +}); + +request.once('disconnect', function () { + +console.error('request aborted'); +}); + +return reply.continue(); +});*/ +export class Request extends Events.EventEmitter { + /** application-specific state. Provides a safe place to store application data without potential conflicts with the framework. Should not be used by plugins which should use plugins[name].*/ + app: any; + /** authentication information*/ + auth: { + /** true is the request has been successfully authenticated, otherwise false.*/ + isAuthenticated: boolean; + /** the credential object received during the authentication process. The presence of an object does not mean successful authentication. can be set in the validate function's callback.*/ + credentials: any; + /** an artifact object received from the authentication strategy and used in authentication-related actions.*/ + artifacts: any; + /** the route authentication mode.*/ + mode: any; + /** the authentication error is failed and mode set to 'try'.*/ + error: any; + }; + /** the connection used by this request*/ + connection: ServerConnection; + /** the node domain object used to protect against exceptions thrown in extensions, handlers and route prerequisites. Can be used to manually bind callback functions otherwise bound to other domains.*/ + domain: any; + /** the raw request headers (references request.raw.headers).*/ + headers: IDictionary; + /** a unique request identifier (using the format '{now}:{connection.info.id}:{5 digits counter}').*/ + id: string; + /** request information */ + info: { + /** the request preferred encoding. */ + acceptEncoding: string; + /** if CORS is enabled for the route, contains the following: */ + cors: { + isOriginMatch: boolean; /** true if the request 'Origin' header matches the configured CORS restrictions. Set to false if no 'Origin' header is found or if it does not match. Note that this is only available after the 'onRequest' extension point as CORS is configured per-route and no routing decisions are made at that point in the request lifecycle. */ + }; + /** content of the HTTP 'Host' header (e.g. 'example.com:8080'). */ + host: string; + /** the hostname part of the 'Host' header (e.g. 'example.com').*/ + hostname: string; + /** request reception timestamp. */ + received: number; + /** content of the HTTP 'Referrer' (or 'Referer') header. */ + referrer: string; + /** remote client IP address. */ + remoteAddress: string; + /** remote client port. */ + remotePort: number; + /** request response timestamp (0 is not responded yet). */ + responded: number; + }; + /** the request method in lower case (e.g. 'get', 'post'). */ + method: string; + /** the parsed content-type header. Only available when payload parsing enabled and no payload error occurred. */ + mime: string; + /** an object containing the values of params, query, and payload before any validation modifications made. Only set when input validation is performed.*/ + orig: { + params: any; + query: any; + payload: any; + }; + /** an object where each key is a path parameter name with matching value as described in Path parameters.*/ + params: IDictionary; + /** an array containing all the path params values in the order they appeared in the path.*/ + paramsArray: string[]; + /** the request URI's path component. */ + path: string; + /** the request payload based on the route payload.output and payload.parse settings.*/ + payload: stream.Readable | Buffer | any; + /** plugin-specific state. Provides a place to store and pass request-level plugin data. The plugins is an object where each key is a plugin name and the value is the state.*/ + plugins: any; + /** an object where each key is the name assigned by a route prerequisites function. The values are the raw values provided to the continuation function as argument. For the wrapped response object, use responses.*/ + pre: IDictionary; + /** the response object when set. The object can be modified but must not be assigned another object. To replace the response with another from within an extension point, use reply(response) to override with a different response. Contains null when no response has been set (e.g. when a request terminates prematurely when the client disconnects).*/ + response: Response; + /**preResponses - same as pre but represented as the response object created by the pre method.*/ + preResponses: any; + /**an object containing the query parameters.*/ + query: any; + /** an object containing the Node HTTP server objects. Direct interaction with these raw objects is not recommended.*/ + raw: { + req: http.IncomingMessage; + res: http.ServerResponse; + }; + /** the route public interface.*/ + route: IRoute; + /** the server object. */ + server: Server; + /** an object containing parsed HTTP state information (cookies) where each key is the cookie name and value is the matching cookie content after processing using any registered cookie definition. */ + state: any; + /** complex object contining details on the url */ + url: { + /** null when i tested */ + auth: any; + /** null when i tested */ + hash: any; + /** null when i tested */ + host: any; + /** null when i tested */ + hostname: any; + href: string; + path: string; + /** path without search*/ + pathname: string; + /** null when i tested */ + port: any; + /** null when i tested */ + protocol: any; + /** querystring parameters*/ + query: IDictionary; + /** querystring parameters as a string*/ + search: string; + /** null when i tested */ + slashes: any; + }; + + /** request.setUrl(url) + + Available only in 'onRequest' extension methods. + + Changes the request URI before the router begins processing the request where: + + url - the new request path value. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + + server.ext('onRequest', function (request, reply) { + + // Change all requests to '/test' + request.setUrl('/test'); + return reply.continue(); + });*/ + setUrl(url: string | url.Url): void; + /** request.setMethod(method) + + Available only in 'onRequest' extension methods. + + Changes the request method before the router begins processing the request where: + + method - is the request HTTP method (e.g. 'GET'). + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + + server.ext('onRequest', function (request, reply) { + + // Change all requests to 'GET' + request.setMethod('GET'); + return reply.continue(); + });*/ + setMethod(method: string): void; + + /** request.log(tags, [data, [timestamp]]) + + Always available. + + Logs request-specific events. When called, the server emits a 'request' event which can be used by other listeners or plugins. The arguments are: + + data - an optional message string or object with the application data being logged. + timestamp - an optional timestamp expressed in milliseconds. Defaults to Date.now() (now). + Any logs generated by the server internally will be emitted only on the 'request-internal' channel and will include the event.internal flag set to true. + + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + + server.on('request', function (request, event, tags) { + + if (tags.error) { + console.log(event); + } + }); + + var handler = function (request, reply) { + + request.log(['test', 'error'], 'Test event'); + return reply(); + }; + */ + log(/** a string or an array of strings (e.g. ['error', 'database', 'read']) used to identify the event. Tags are used instead of log levels and provide a much more expressive mechanism for describing and filtering events.*/ + tags: string | string[], + /** an optional message string or object with the application data being logged.*/ + data?: any, + /** an optional timestamp expressed in milliseconds. Defaults to Date.now() (now).*/ + timestamp?: number): void; + + /** request.getLog([tags], [internal]) + + Always available. + + Returns an array containing the events matching any of the tags specified (logical OR) + request.getLog(); + request.getLog('error'); + request.getLog(['error', 'auth']); + request.getLog(['error'], true); + request.getLog(false);*/ + + getLog(/** is a single tag string or array of tag strings. If no tags specified, returns all events.*/ + tags?: string, + /** filters the events to only those with a matching event.internal value. If true, only internal logs are included. If false, only user event are included. Defaults to all events (undefined).*/ + internal?: boolean): string[]; + + /** request.tail([name]) + + Available until immediately after the 'response' event is emitted. + + Adds a request tail which has to complete before the request lifecycle is complete where: + + name - an optional tail name used for logging purposes. + Returns a tail function which must be called when the tail activity is completed. + + Tails are actions performed throughout the request lifecycle, but which may end after a response is sent back to the client. For example, a request may trigger a database update which should not delay sending back a response. However, it is still desirable to associate the activity with the request when logging it (or an error associated with it). + + When all tails completed, the server emits a 'tail' event. + + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + + var get = function (request, reply) { + + var dbTail = request.tail('write to database'); + + db.save('key', 'value', function () { + + dbTail(); + }); + + return reply('Success!'); + }; + + server.route({ method: 'GET', path: '/', handler: get }); + + server.on('tail', function (request) { + + console.log('Request completed including db activity'); + });*/ + tail(/** an optional tail name used for logging purposes.*/ + name?: string): Function; +} +/** Response events + + The response object supports the following events: + + 'peek' - emitted for each chunk of data written back to the client connection. The event method signature is function(chunk, encoding). + 'finish' - emitted when the response finished writing but before the client response connection is ended. The event method signature is function (). + var Crypto = require('crypto'); + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + + server.ext('onPreResponse', function (request, reply) { + +var response = request.response; +if (response.isBoom) { +return reply(); +} + +var hash = Crypto.createHash('sha1'); +response.on('peek', function (chunk) { + +hash.update(chunk); +}); + +response.once('finish', function () { + +console.log(hash.digest('hex')); +}); + +return reply.continue(); +});*/ +export class Response extends Events.EventEmitter { + isBoom: boolean; + /** the HTTP response status code. Defaults to 200 (except for errors).*/ + statusCode: number; + /** an object containing the response headers where each key is a header field name. Note that this is an incomplete list of headers to be included with the response. Additional headers will be added once the response is prepare for transmission.*/ + headers: IDictionary; + /** the value provided using the reply interface.*/ + source: any; + /** a string indicating the type of source with available values: + 'plain' - a plain response such as string, number, null, or simple object (e.g. not a Stream, Buffer, or view). + 'buffer' - a Buffer. + 'view' - a view generated with reply.view(). + 'file' - a file generated with reply.file() of via the directory handler. + 'stream' - a Stream. + 'promise' - a Promise object. */ + variety: string; + /** application-specific state. Provides a safe place to store application data without potential conflicts with the framework. Should not be used by plugins which should use plugins[name].*/ + app: any; + /** plugin-specific state. Provides a place to store and pass request-level plugin data. The plugins is an object where each key is a plugin name and the value is the state. */ + plugins: any; + /** settings - response handling flags: + charset - the 'Content-Type' HTTP header 'charset' property. Defaults to 'utf-8'. + encoding - the string encoding scheme used to serial data into the HTTP payload when source is a string or marshals into a string. Defaults to 'utf8'. + passThrough - if true and source is a Stream, copies the statusCode and headers of the stream to the outbound response. Defaults to true. + stringify - options used for source value requiring stringification. Defaults to no replacer and no space padding. + ttl - if set, overrides the route cache expiration milliseconds value set in the route config. Defaults to no override. + varyEtag - if true, a suffix will be automatically added to the 'ETag' header at transmission time (separated by a '-' character) when the HTTP 'Vary' header is present.*/ + settings: { + charset: string; + encoding: string; + passThrough: boolean; + stringify: any; + ttl: number; + varyEtag: boolean; + }; + + /** sets the HTTP 'Content-Length' header (to avoid chunked transfer encoding) where: + length - the header value. Must match the actual payload size.*/ + bytes(length: number): Response; + + /** sets the 'Content-Type' HTTP header 'charset' property where: charset - the charset property value.*/ + charset(charset: string): Response; + + /** sets the HTTP status code where: + statusCode - the HTTP status code.*/ + code(statusCode: number): Response; + + /** sets the HTTP status code to Created (201) and the HTTP 'Location' header where: uri - an absolute or relative URI used as the 'Location' header value.*/ + created(uri: string): Response; + + /** encoding(encoding) - sets the string encoding scheme used to serial data into the HTTP payload where: encoding - the encoding property value (see node Buffer encoding).*/ + encoding(encoding: string): Response; + + /** etag(tag, options) - sets the representation entity tag where: + tag - the entity tag string without the double-quote. + options - optional settings where: + weak - if true, the tag will be prefixed with the 'W/' weak signifier. Weak tags will fail to match identical tags for the purpose of determining 304 response status. Defaults to false. + vary - if true and content encoding is set or applied to the response (e.g 'gzip' or 'deflate'), the encoding name will be automatically added to the tag at transmission time (separated by a '-' character). Ignored when weak is true. Defaults to true.*/ + etag(tag: string, options: { + weak: boolean; vary: boolean; + }): Response; + + /**header(name, value, options) - sets an HTTP header where: + name - the header name. + value - the header value. + options - optional settings where: + append - if true, the value is appended to any existing header value using separator. Defaults to false. + separator - string used as separator when appending to an exiting value. Defaults to ','. + override - if false, the header value is not set if an existing value present. Defaults to true.*/ + header(name: string, value: string, options?: IHeaderOptions): Response; + + /** hold() - puts the response on hold until response.send() is called. Available only after reply() is called and until response.hold() is invoked once. */ + hold(): Response; + + /** location(uri) - sets the HTTP 'Location' header where: + uri - an absolute or relative URI used as the 'Location' header value.*/ + location(uri: string): Response; + + /** redirect(uri) - sets an HTTP redirection response (302) and decorates the response with additional methods listed below, where: + uri - an absolute or relative URI used to redirect the client to another resource. */ + redirect(uri: string): Response; + + /** replacer(method) - sets the JSON.stringify() replacer argument where: + method - the replacer function or array. Defaults to none.*/ + replacer(method: Function | Function[]): Response; + + /** spaces(count) - sets the JSON.stringify() space argument where: + count - the number of spaces to indent nested object keys. Defaults to no indentation. */ + spaces(count: number): Response; + + /**state(name, value, [options]) - sets an HTTP cookie where: + name - the cookie name. + value - the cookie value. If no encoding is defined, must be a string. + options - optional configuration. If the state was previously registered with the server using server.state(), the specified keys in options override those same keys in the server definition (but not others).*/ + state(name: string, value: string, options?: any): Response; + + /** send() - resume the response which will be transmitted in the next tick. Available only after response.hold() is called and until response.send() is invoked once. */ + send(): void; + + /** sets a string suffix when the response is process via JSON.stringify().*/ + suffix(suffix: string): void; + + /** overrides the default route cache expiration rule for this response instance where: + msec - the time-to-live value in milliseconds.*/ + ttl(msec: number): void; + + /** type(mimeType) - sets the HTTP 'Content-Type' header where: + mimeType - is the mime type. Should only be used to override the built-in default for each response type. */ + type(mimeType: string): Response; + + /** clears the HTTP cookie by setting an expired value where: + name - the cookie name. + options - optional configuration for expiring cookie. If the state was previously registered with the server using server.state(), the specified keys in options override those same keys in the server definition (but not others).*/ + unstate(name: string, options?: { [key: string]: string }): Response; + + /** adds the provided header to the list of inputs affected the response generation via the HTTP 'Vary' header where: + header - the HTTP request header name.*/ + vary(header: string): void; +} +/** When using the redirect() method, the response object provides these additional methods */ +export class ResponseRedirect extends Response { + /** sets the status code to 302 or 307 (based on the rewritable() setting) where: + isTemporary - if false, sets status to permanent. Defaults to true.*/ + temporary(isTemporary: boolean): void; + + /** sets the status code to 301 or 308 (based on the rewritable() setting) where: + isPermanent - if true, sets status to temporary. Defaults to false. */ + permanent(isPermanent: boolean): void; + + /** sets the status code to 301/302 for rewritable (allows changing the request method from 'POST' to 'GET') or 307/308 for non-rewritable (does not allow changing the request method from 'POST' to 'GET'). Exact code based on the temporary() or permanent() setting. Arguments: + isRewritable - if false, sets to non-rewritable. Defaults to true. + Permanent Temporary + Rewritable 301 302(1) + Non-rewritable 308(2) 307 + Notes: 1. Default value. 2. Proposed code, not supported by all clients. */ + rewritable(isRewritable: boolean): void; +} +/** info about a server connection */ +export interface IServerConnectionInfo { + /** - a unique connection identifier (using the format '{hostname}:{pid}:{now base36}').*/ + id: string; + /** - the connection creation timestamp.*/ + created: number; + /** - the connection start timestamp (0 when stopped).*/ + started: number; + /** the connection port based on the following rules: + the configured port value before the server has been started. + the actual port assigned when no port is configured or set to 0 after the server has been started.*/ + port: number; + + /** - the host name the connection was configured to. Defaults to the operating system hostname when available, otherwise 'localhost'.*/ + host: string; + /** - the active IP address the connection was bound to after starting.Set to undefined until the server has been started or when using a non TCP port (e.g. UNIX domain socket).*/ + address: string; + /** - the protocol used: + 'http' - HTTP. + 'https' - HTTPS. + 'socket' - UNIX domain socket or Windows named pipe.*/ + protocol: string; + /** a string representing the connection (e.g. 'http://example.com:8080' or 'socket:/unix/domain/socket/path'). Contains the uri setting if provided, otherwise constructed from the available settings. If no port is available or set to 0, the uri will not include a port component.*/ + uri: string; +} +/** + * undocumented. The connection object constructed after calling server.connection(); + * can be accessed via server.connections; or request.connection; + */ +export class ServerConnection extends Events.EventEmitter { + domain: any; + _events: { route: Function, domain: Function, _events: Function, _eventsCount: Function, _maxListeners: Function }; + _eventsCount: number; + settings: IServerConnectionOptions; + server: Server; + /** ex: "tcp" */ + type: string; + _started: boolean; + /** dictionary of sockets */ + _connections: { [ip_port: string]: any }; + _onConnection: Function; + registrations: any; + _extensions: any; + _requestCounter: { value: number; min: number; max: number }; + _load: any; + states: { + settings: any; cookies: any; names: any[] + }; + auth: { connection: ServerConnection; _schemes: any; _strategies: any; settings: any; api: any; }; + _router: any; + MSPluginsCollection: any; + applicationCache: any; + addEventListener: any; + info: IServerConnectionInfo; +} + +export type RequestExtPoints = "onRequest" | "onPreResponse" | "onPreAuth" | "onPostAuth" | "onPreHandler" | "onPostHandler" | "onPreResponse"; +export type ServerExtPoints = "onPreStart" | "onPostStart" | "onPreStop" | "onPostStop"; + +/** Server http://hapijs.com/api#server + rver object is the main application container. The server manages all incoming connections along with all the facilities provided by the framework. A server can contain more than one connection (e.g. listen to port 80 and 8080). + Server events + The server object inherits from Events.EventEmitter and emits the following events: + 'log' - events logged with server.log() and server events generated internally by the framework. + 'start' - emitted when the server is started using server.start(). + 'stop' - emitted when the server is stopped using server.stop(). + 'request' - events generated by request.log(). Does not include any internally generated events. + 'request-internal' - request events generated internally by the framework (multiple events per request). + 'request-error' - emitted whenever an Internal Server Error (500) error response is sent. Single event per request. + 'response' - emitted after the response is sent back to the client (or when the client connection closed and no response sent, in which case request.response is null). Single event per request. + 'tail' - emitted when a request finished processing, including any registered tails. Single event per request. + Note that the server object should not be used to emit application events as its internal implementation is designed to fan events out to the various plugin selections and not for application events. + MORE EVENTS HERE: http://hapijs.com/api#server-events*/ +export class Server extends Events.EventEmitter { + constructor(options?: IServerOptions); + + /** Provides a safe place to store server-specific run-time application data without potential conflicts with the framework internals. The data can be accessed whenever the server is accessible. Initialized with an empty object. + var Hapi = require('hapi'); + server = new Hapi.Server(); + server.app.key = 'value'; + var handler = function (request, reply) { + return reply(request.server.app.key); + }; */ + app: any; + /** An array containing the server's connections. When the server object is returned from server.select(), the connections array only includes the connections matching the selection criteria. + var server = new Hapi.Server(); + server.connection({ port: 80, labels: 'a' }); + server.connection({ port: 8080, labels: 'b' }); + // server.connections.length === 2 + var a = server.select('a'); + // a.connections.length === 1*/ + connections: ServerConnection[]; + /** When the server contains exactly one connection, info is an object containing information about the sole connection. + * When the server contains more than one connection, each server.connections array member provides its own connection.info. + var server = new Hapi.Server(); + server.connection({ port: 80 }); + // server.info.port === 80 + server.connection({ port: 8080 }); + // server.info === null + // server.connections[1].info.port === 8080 + */ + info: IServerConnectionInfo; + /** An object containing the process load metrics (when load.sampleInterval is enabled): + rss - RSS memory usage. + var Hapi = require('hapi'); + var server = new Hapi.Server({ load: { sampleInterval: 1000 } }); + console.log(server.load.rss);*/ + load: { + /** - event loop delay milliseconds.*/ + eventLoopDelay: number; + /** - V8 heap usage.*/ + heapUsed: number; + }; + /** When the server contains exactly one connection, listener is the node HTTP server object of the sole connection. + When the server contains more than one connection, each server.connections array member provides its own connection.listener. + var Hapi = require('hapi'); + var SocketIO = require('socket.io'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + var io = SocketIO.listen(server.listener); + io.sockets.on('connection', function(socket) { + socket.emit({ msg: 'welcome' }); + });*/ + listener: http.Server; + + /** server.methods + An object providing access to the server methods where each server method name is an object property. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.method('add', function (a, b, next) { + return next(null, a + b); + }); + server.methods.add(1, 2, function (err, result) { + // result === 3 + });*/ + methods: IDictionary; + + /** server.mime + Provides access to the server MIME database used for setting content-type information. The object must not be modified directly but only through the mime server setting. + var Hapi = require('hapi'); + var options = { + mime: { + override: { + 'node/module': { + source: 'steve', + compressible: false, + extensions: ['node', 'module', 'npm'], + type: 'node/module' + } + } + } + }; + var server = new Hapi.Server(options); + // server.mime.path('code.js').type === 'application/javascript' + // server.mime.path('file.npm').type === 'node/module'*/ + mime: any; + /**server.plugins + An object containing the values exposed by each plugin registered where each key is a plugin name and the values are the exposed properties by each plugin using server.expose(). Plugins may set the value of the server.plugins[name] object directly or via the server.expose() method. + exports.register = function (server, options, next) { + server.expose('key', 'value'); + // server.plugins.example.key === 'value' + return next(); + }; + exports.register.attributes = { + name: 'example' + };*/ + plugins: IDictionary; + /** server.realm + The realm object contains server-wide or plugin-specific state that can be shared across various methods. For example, when calling server.bind(), the active realm settings.bind property is set which is then used by routes and extensions added at the same level (server root or plugin). Realms are a limited version of a sandbox where plugins can maintain state used by the framework when adding routes, extensions, and other properties. + modifiers - when the server object is provided as an argument to the plugin register() method, modifiers provides the registration preferences passed the server.register() method and includes: + route - routes preferences: + prefix - the route path prefix used by any calls to server.route() from the server. + vhost - the route virtual host settings used by any calls to server.route() from the server. + plugin - the active plugin name (empty string if at the server root). + plugins - plugin-specific state to be shared only among activities sharing the same active state. plugins is an object where each key is a plugin name and the value is the plugin state. + settings - settings overrides: + files.relativeTo + bind + The server.realm object should be considered read-only and must not be changed directly except for the plugins property can be directly manipulated by the plugins (each setting its own under plugins[name]). + exports.register = function (server, options, next) { + console.log(server.realm.modifiers.route.prefix); + return next(); + };*/ + realm: IServerRealm; + + /** server.root + The root server object containing all the connections and the root server methods (e.g. start(), stop(), connection()).*/ + root: Server; + /** server.settings + The server configuration object after defaults applied. + var Hapi = require('hapi'); + var server = new Hapi.Server({ + app: { + key: 'value' + } + }); + // server.settings.app === { key: 'value' }*/ + settings: IServerOptions; + + /** server.version + The hapi module version number. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + // server.version === '8.0.0'*/ + version: string; + + /** server.after(method, [dependencies]) + Adds a method to be called after all the plugin dependencies have been registered and before the server starts (only called if the server is started) where: + after - the method with signature function(plugin, next) where: + server - server object the after() method was called on. + next - the callback function the method must call to return control over to the application and complete the registration process. The function signature is function(err) where: + err - internal error which is returned back via the server.start() callback. + dependencies - a string or array of string with the plugin names to call this method after their after() methods. There is no requirement for the other plugins to be registered. Setting dependencies only arranges the after methods in the specified order. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.after(function () { + // Perform some pre-start logic + }); + server.start(function (err) { + // After method already executed + }); + server.auth.default(options)*/ + after(method: (plugin: any, next: (err: any) => void) => void, dependencies: string | string[]): void; + + auth: { + /** server.auth.api + An object where each key is a strategy name and the value is the exposed strategy API. Available on when the authentication scheme exposes an API by returning an api key in the object returned from its implementation function. + When the server contains more than one connection, each server.connections array member provides its own connection.auth.api object. + const server = new Hapi.Server(); + server.connection({ port: 80 }); + const scheme = function (server, options) { + return { + api: { + settings: { + x: 5 + } + }, + authenticate: function (request, reply) { + const req = request.raw.req; + const authorization = req.headers.authorization; + if (!authorization) { + return reply(Boom.unauthorized(null, 'Custom')); + } + return reply.continue({ credentials: { user: 'john' } }); + } + }; + }; + server.auth.scheme('custom', scheme); + server.auth.strategy('default', 'custom'); + console.log(server.auth.api.default.settings.x); // 5 + */ + api: { + [index: string]: any; + } + /** server.auth.default(options) + Sets a default strategy which is applied to every route where: + options - a string with the default strategy name or an object with a specified strategy or strategies using the same format as the route auth handler options. + The default does not apply when the route config specifies auth as false, or has an authentication strategy configured. Otherwise, the route authentication config is applied to the defaults. Note that the default only applies at time of route configuration, not at runtime. Calling default() after adding a route will have no impact on routes added prior. + The default auth strategy configuration can be accessed via connection.auth.settings.default. + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.auth.scheme('custom', scheme); + server.auth.strategy('default', 'custom'); + server.auth.default('default'); + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + return reply(request.auth.credentials.user); + } + });*/ + default(options: string): void; + default(options: { strategy: string }): void; + default(options: { strategies: string[] }): void; + /** server.auth.scheme(name, scheme) + Registers an authentication scheme where: + name - the scheme name. + scheme - the method implementing the scheme with signature function(server, options) where: + server - a reference to the server object the scheme is added to. + options - optional scheme settings used to instantiate a strategy.*/ + scheme( + name: string, + /** + * When the scheme authenticate() method implementation calls reply() with an error condition, the specifics of the error affect whether additional authentication strategies will be attempted if configured for the route. If the err returned by the reply() method includes a message, no additional strategies will be attempted. If the err does not include a message but does include a scheme name (e.g. Boom.unauthorized(null, 'Custom')), additional strategies will be attempted in order of preference. + * n the scheme payload() method returns an error with a message, it means payload validation failed due to bad payload. If the error has no message but includes a scheme name (e.g. Boom.unauthorized(null, 'Custom')), authentication may still be successful if the route auth.payload configuration is set to 'optional'. + * server = new Hapi.Server(); + * server.connection({ port: 80 }); + * scheme = function (server, options) { + * urn { + * authenticate: function (request, reply) { + * req = request.raw.req; + * var authorization = req.headers.authorization; + * if (!authorization) { + * return reply(Boom.unauthorized(null, 'Custom')); + * } + * urn reply(null, { credentials: { user: 'john' } }); + * } + * }; + * }; + */ + scheme: (server: Server, options: any) => IServerAuthScheme): void; + + /** + * server.auth.strategy(name, scheme, [mode], [options]) + * Registers an authentication strategy where: + * name - the strategy name. + * scheme - the scheme name (must be previously registered using server.auth.scheme()). + * mode - if true, the scheme is automatically assigned as a required strategy to any route without an auth config. Can only be assigned to a single server strategy. Value must be true (which is the same as 'required') or a valid authentication mode ('required', 'optional', 'try'). Defaults to false. + * options - scheme options based on the scheme requirements. + * var server = new Hapi.Server(); + * server.connection({ port: 80 }); + * server.auth.scheme('custom', scheme); + * server.auth.strategy('default', 'custom'); + * server.route({ + * method: 'GET', + * path: '/', + * config: { + * auth: 'default', + * handler: function (request, reply) { + * return reply(request.auth.credentials.user); + * } + * } + * }); + */ + strategy(name: string, scheme: string, mode?: boolean | string, options?: any): void; + strategy(name: string, scheme: string, mode?: boolean | string): void; + strategy(name: string, scheme: string, options?: any): void; + + /** server.auth.test(strategy, request, next) + Tests a request against an authentication strategy where: + strategy - the strategy name registered with server.auth.strategy(). + request - the request object. + next - the callback function with signature function(err, credentials) where: + err - the error if authentication failed. + credentials - the authentication credentials object if authentication was successful. + Note that the test() method does not take into account the route authentication configuration. It also does not perform payload authentication. It is limited to the basic strategy authentication execution. It does not include verifying scope, entity, or other route properties. + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.auth.scheme('custom', scheme); + server.auth.strategy('default', 'custom'); + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + request.server.auth.test('default', request, function (err, credentials) { + if (err) { + return reply({ status: false }); + } + return reply({ status: true, user: credentials.name }); + }); + } + });*/ + test(strategy: string, request: Request, next: (err: any, credentials: any) => void): void; + }; + + /** server.bind(context) + Sets a global context used as the default bind object when adding a route or an extension where: + context - the object used to bind this in handler and extension methods. + When setting context inside a plugin, the context is applied only to methods set up by the plugin. Note that the context applies only to routes and extensions added after it has been set. + var handler = function (request, reply) { + return reply(this.message); + }; + exports.register = function (server, options, next) { + var bind = { + message: 'hello' + }; + server.bind(bind); + server.route({ method: 'GET', path: '/', handler: handler }); + return next(); + };*/ + bind(context: any): void; + + /** server.cache(options) + Provisions a cache segment within the server cache facility where: + options - catbox policy configuration where: + expiresIn - relative expiration expressed in the number of milliseconds since the item was saved in the cache. Cannot be used together with expiresAt. + 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. + 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 function(id, next) where: - id - the id string or object provided to the get() method. - next - the method called when the new item is returned with the signature function(err, value, ttl) where: - err - an error condition. - value - the new value generated. - ttl - the cache ttl value in milliseconds. Set to 0 to skip storing in the cache. Defaults to the cache global policy. + 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. + staleTimeout - number of milliseconds to wait before checking if an item is stale. + 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 is stored in the cache for future requests. + cache - the cache name configured in 'server.cache`. Defaults to the default cache. + 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. Required when called outside of a plugin. + shared - if true, allows multiple cache provisions to share the same segment. Default to false. + var server = new Hapi.Server(); + server.connection({ port: 80 }); + var cache = server.cache({ segment: 'countries', expiresIn: 60 * 60 * 1000 }); + cache.set('norway', { capital: 'oslo' }, null, function (err) { + cache.get('norway', function (err, value, cached, log) { + // value === { capital: 'oslo' }; + }); + });*/ + cache(options: ICatBoxCachePolicyOptions): void; + + /** server.connection([options]) + Adds an incoming server connection + Returns a server object with the new connection selected. + Must be called before any other server method that modifies connections is called for it to apply to the new connection (e.g. server.state()). + Note that the options object is deeply cloned (with the exception of listener which is shallowly copied) and cannot contain any values that are unsafe to perform deep copy on. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + var web = server.connection({ port: 8000, host: 'example.com', labels: ['web'] }); + var admin = server.connection({ port: 8001, host: 'example.com', labels: ['admin'] }); + // server.connections.length === 2 + // web.connections.length === 1 + // admin.connections.length === 1 */ + connection(options: IServerConnectionOptions): Server; + + /** server.decorate(type, property, method, [options]) + Extends various framework interfaces with custom methods where: + type - the interface being decorated. Supported types: + 'reply' - adds methods to the reply interface. + 'server' - adds methods to the Server object. + property - the object decoration key name. + method - the extension function. + options - if the type is 'request', supports the following optional settings: + 'apply' - if true, the method function is invoked using the signature function(request) where request is the current request object and the returned value is assigned as the decoration. + Note that decorations apply to the entire server and all its connections regardless of current selection. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.decorate('reply', 'success', function () { + return this.response({ status: 'ok' }); + }); + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + return reply.success(); + } + });*/ + decorate(type: string, property: string, method: Function, options?: { apply: boolean }): void; + + /** server.dependency(dependencies, [after]) + Used within a plugin to declares a required dependency on other plugins where: + dependencies - a single string or array of plugin name strings which must be registered in order for this plugin to operate. Plugins listed must be registered before the server is started. Does not provide version dependency which should be implemented using npm peer dependencies. + after - an optional function called after all the specified dependencies have been registered and before the server starts. The function is only called if the server is started. If a circular dependency is detected, an exception is thrown (e.g. two plugins each has an after function to be called after the other). The function signature is function(server, next) where: + server - the server the dependency() method was called on. + next - the callback function the method must call to return control over to the application and complete the registration process. The function signature is function(err) where: + err - internal error condition, which is returned back via the server.start() callback. + exports.register = function (server, options, next) { + server.dependency('yar', after); + return next(); + }; + var after = function (server, next) { + // Additional plugin registration logic + return next(); + };*/ + dependency(dependencies: string | string[], after?: (server: Server, next: (err: any) => void) => void): void; + + /** server.expose(key, value) + Used within a plugin to expose a property via server.plugins[name] where: + key - the key assigned (server.plugins[name][key]). + value - the value assigned. + exports.register = function (server, options, next) { + server.expose('util', function () { console.log('something'); }); + return next(); + };*/ + expose(key: string, value: any): void; + + /** server.expose(obj) + Merges a deep copy of an object into to the existing content of server.plugins[name] where: + obj - the object merged into the exposed properties container. + exports.register = function (server, options, next) { + server.expose({ util: function () { console.log('something'); } }); + return next(); + };*/ + expose(obj: any): void; + + /** server.ext(event, method, [options]) + Registers an extension function in one of the available extension points where: + event - the event name. + method - a function or an array of functions to be executed at a specified point during request processing. The required extension function signature is function(request, reply) where: + request - the request object. NOTE: Access the Response via request.response + reply - the reply interface which is used to return control back to the framework. To continue normal execution of the request lifecycle, reply.continue() must be called. To abort processing and return a response to the client, call reply(value) where value is an error or any other valid response. + this - the object provided via options.bind or the current active context set with server.bind(). + options - an optional object with the following: + before - a string or array of strings of plugin names this method must execute before (on the same event). Otherwise, extension methods are executed in the order added. + after - a string or array of strings of plugin names this method must execute after (on the same event). Otherwise, extension methods are executed in the order added. + bind - a context object passed back to the provided method (via this) when called. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.ext('onRequest', function (request, reply) { + // Change all requests to '/test' + request.setUrl('/test'); + return reply.continue(); + }); + var handler = function (request, reply) { + return reply({ status: 'ok' }); + }; + server.route({ method: 'GET', path: '/test', handler: handler }); + server.start(); + // All requests will get routed to '/test'*/ + ext(event: RequestExtPoints, method: (request: Request, reply: IReply, bind?: any) => void, options?: { before: string | string[]; after: string | string[]; bind?: any }): void; + ext(event: RequestExtPoints, method: (request: Request, reply: IStrictReply, bind?: any) => void, options?: { before: string | string[]; after: string | string[]; bind?: any }): void; + ext(event: ServerExtPoints, method: (server: Server, next: (err?: any) => void, bind?: any) => void, options?: { before: string | string[]; after: string | string[]; bind?: any }): void; + + /** server.handler(name, method) + Registers a new handler type to be used in routes where: + name - string name for the handler being registered. Cannot override the built-in handler types (directory, file, proxy, and view) or any previously registered type. + method - the function used to generate the route handler using the signature function(route, options) where: + route - the route public interface object. + options - the configuration object provided in the handler config. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ host: 'localhost', port: 8000 }); + // Defines new handler for routes on this server + server.handler('test', function (route, options) { + return function (request, reply) { + return reply('new handler: ' + options.msg); + } + }); + server.route({ + method: 'GET', + path: '/', + handler: { test: { msg: 'test' } } + }); + server.start(); + The method function can have a defaults object or function property. If the property is set to an object, that object is used as the default route config for routes using this handler. If the property is set to a function, the function uses the signature function(method) and returns the route default configuration. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ host: 'localhost', port: 8000 }); + var handler = function (route, options) { + return function (request, reply) { + return reply('new handler: ' + options.msg); + } + }; + // Change the default payload processing for this handler + handler.defaults = { + payload: { + output: 'stream', + parse: false + } + }; + server.handler('test', handler);*/ + handler(name: string, method: (route: IRoute, options: THandlerConfig) => ISessionHandler): void; + + /** server.initialize([callback]) + Initializes the server (starts the caches, finalizes plugin registration) but does not start listening + on the connection ports, where: + - `callback` - the callback method when server initialization is completed or failed with the signature + `function(err)` where: + - `err` - any initialization error condition. + + If no `callback` is provided, a `Promise` object is returned. + + Note that if the method fails and the callback includes an error, the server is considered to be in + an undefined state and should be shut down. In most cases it would be impossible to fully recover as + the various plugins, caches, and other event listeners will get confused by repeated attempts to + start the server or make assumptions about the healthy state of the environment. It is recommended + to assert that no error has been returned after calling `initialize()` to abort the process when the + server fails to start properly. If you must try to resume after an error, call `server.stop()` + first to reset the server state. + */ + initialize(callback?: (error: any) => void): Promise; + + /** When the server contains exactly one connection, injects a request into the sole connection simulating an incoming HTTP request without making an actual socket connection. + Injection is useful for testing purposes as well as for invoking routing logic internally without the overhead or limitations of the network stack. + Utilizes the [shot module | https://github.com/hapijs/shot ] for performing injections, with some additional options and response properties + * When the server contains more than one connection, each server.connections array member provides its own connection.inject(). + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + var handler = function (request, reply) { + return reply('Success!'); + }; + server.route({ method: 'GET', path: '/', handler: handler }); + server.inject('/', function (res) { + console.log(res.result); + }); + */ + inject: IServerInject; + + /** server.log(tags, [data, [timestamp]]) + Logs server events that cannot be associated with a specific request. When called the server emits a 'log' event which can be used by other listeners or plugins to record the information or output to the console. The arguments are: + tags - a string or an array of strings (e.g. ['error', 'database', 'read']) used to identify the event. Tags are used instead of log levels and provide a much more expressive mechanism for describing and filtering events. Any logs generated by the server internally include the 'hapi' tag along with event-specific information. + data - an optional message string or object with the application data being logged. + timestamp - an optional timestamp expressed in milliseconds. Defaults to Date.now() (now). + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.on('log', function (event, tags) { + if (tags.error) { + console.log(event); + } + }); + server.log(['test', 'error'], 'Test event');*/ + log(tags: string | string[], data?: string | any, timestamp?: number): void; + + /**server.lookup(id) + When the server contains exactly one connection, looks up a route configuration where: + id - the route identifier as set in the route options. + returns the route public interface object if found, otherwise null. + var server = new Hapi.Server(); + server.connection(); + server.route({ + method: 'GET', + path: '/', + config: { + handler: function (request, reply) { return reply(); }, + id: 'root' + } + }); + var route = server.lookup('root'); + When the server contains more than one connection, each server.connections array member provides its own connection.lookup() method.*/ + lookup(id: string): IRoute; + + /** server.match(method, path, [host]) + When the server contains exactly one connection, looks up a route configuration where: + method - the HTTP method (e.g. 'GET', 'POST'). + path - the requested path (must begin with '/'). + host - optional hostname (to match against routes with vhost). + returns the route public interface object if found, otherwise null. + var server = new Hapi.Server(); + server.connection(); + server.route({ + method: 'GET', + path: '/', + config: { + handler: function (request, reply) { return reply(); }, + id: 'root' + } + }); + var route = server.match('get', '/'); + When the server contains more than one connection, each server.connections array member provides its own connection.match() method.*/ + match(method: string, path: string, host?: string): IRoute; + + /** server.method(name, method, [options]) + Registers a server method. Server methods are functions registered with the server and used throughout the application as a common utility. Their advantage is in the ability to configure them to use the built-in cache and share across multiple request handlers without having to create a common module. + Methods are registered via server.method(name, method, [options]) + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + // Simple arguments + var add = function (a, b, next) { + return next(null, a + b); + }; + server.method('sum', add, { cache: { expiresIn: 2000 } }); + server.methods.sum(4, 5, function (err, result) { + console.log(result); + }); + // Object argument + var addArray = function (array, next) { + var sum = 0; + array.forEach(function (item) { + sum += item; + }); + return next(null, sum); + }; + server.method('sumObj', addArray, { + cache: { expiresIn: 2000 }, + generateKey: function (array) { + return array.join(','); + } + }); + server.methods.sumObj([5, 6], function (err, result) { + console.log(result); + }); + // Synchronous method with cache + var addSync = function (a, b) { + return a + b; + }; + server.method('sumSync', addSync, { cache: { expiresIn: 2000 }, callback: false }); + server.methods.sumSync(4, 5, function (err, result) { + console.log(result); + }); */ + method(/** a unique method name used to invoke the method via server.methods[name]. When configured with caching enabled, server.methods[name].cache.drop(arg1, arg2, ..., argn, callback) can be used to clear the cache for a given key. Supports using nested names such as utils.users.get which will automatically create the missing path under server.methods and can be accessed for the previous example via server.methods.utils.users.get.*/ + name: string, + method: IServerMethod, + options?: IServerMethodOptions): void; + + /**server.method(methods) + Registers a server method function as described in server.method() using a configuration object where: + methods - an object or an array of objects where each one contains: + name - the method name. + method - the method function. + options - optional settings. + var add = function (a, b, next) { + next(null, a + b); + }; + server.method({ + name: 'sum', + method: add, + options: { + cache: { + expiresIn: 2000 + } + } + });*/ + method(methods: { + name: string; method: IServerMethod; options?: IServerMethodOptions + } | Array<{ + name: string; method: IServerMethod; options?: IServerMethodOptions + }>): void; + + /**server.path(relativeTo) + Sets the path prefix used to locate static resources (files and view templates) when relative paths are used where: + relativeTo - the path prefix added to any relative file path starting with '.'. + Note that setting a path within a plugin only applies to resources accessed by plugin methods. If no path is set, the connection files.relativeTo configuration is used. The path only applies to routes added after it has been set. + exports.register = function (server, options, next) { + server.path(__dirname + '../static'); + server.route({ path: '/file', method: 'GET', handler: { file: './test.html' } }); + next(); + };*/ + path(relativeTo: string): void; + + /** + * server.register(plugins, [options], callback) + * Registers a plugin where: + * plugins - an object or array of objects where each one is either: + * a plugin registration function. + * an object with the following: + * register - the plugin registration function. + * options - optional options passed to the registration function when called. + * options - optional registration options (different from the options passed to the registration function): + * select - a string or array of string labels used to pre-select connections for plugin registration. + * routes - modifiers applied to each route added by the plugin: + * prefix - string added as prefix to any route path (must begin with '/'). If a plugin registers a child plugin the prefix is passed on to the child or is added in front of the child-specific prefix. + * vhost - virtual host string (or array of strings) applied to every route. The outer-most vhost overrides the any nested configuration. + * callback - the callback function with signature function(err) where: + * err - an error returned from the registration function. Note that exceptions thrown by the registration function are not handled by the framework. + * + * If no callback is provided, a Promise object is returned. + */ + register( + plugins: any | any[], + options: { + select?: string | string[]; + routes: { + prefix: string; vhost?: string | string[] + }; + }, + callback: (err: any) => void): void; + register( + plugins: any | any[], + options: { + select?: string | string[]; + routes: { + prefix: string; vhost?: string | string[] + }; + }): Promise; + + register(plugins: any | any[], callback: (err: any) => void): void; + register(plugins: any | any[]): Promise; + + /**server.render(template, context, [options], callback) + Utilizes the server views manager to render a template where: + template - the template filename and path, relative to the views manager templates path (path or relativeTo). + context - optional object used by the template to render context-specific result. Defaults to no context ({}). + options - optional object used to override the views manager configuration. + callback - the callback function with signature function (err, rendered, config) where: + err - the rendering error if any. + rendered - the result view string. + config - the configuration used to render the template. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.views({ + engines: { html: require('handlebars') }, + path: __dirname + '/templates' + }); + var context = { + title: 'Views Example', + message: 'Hello, World' + }; + server.render('hello', context, function (err, rendered, config) { + console.log(rendered); + });*/ + render(template: string, context: any, options: any, callback: (err: any, rendered: any, config: any) => void): void; + + /** server.route(options) + Adds a connection route where: + options - a route configuration object or an array of configuration objects. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.route({ method: 'GET', path: '/', handler: function (request, reply) { return reply('ok'); } }); + server.route([ + { method: 'GET', path: '/1', handler: function (request, reply) { return reply('ok'); } }, + { method: 'GET', path: '/2', handler: function (request, reply) { return reply('ok'); } } + ]);*/ + route(options: IRouteConfiguration): void; + route(options: IRouteConfiguration[]): void; + + /**server.select(labels) + Selects a subset of the server's connections where: + labels - a single string or array of strings of labels used as a logical OR statement to select all the connections with matching labels in their configuration. + Returns a server object with connections set to the requested subset. Selecting again on a selection operates as a logic AND statement between the individual selections. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80, labels: ['a'] }); + server.connection({ port: 8080, labels: ['b'] }); + server.connection({ port: 8081, labels: ['c'] }); + server.connection({ port: 8082, labels: ['c','d'] }); + var a = server.select('a'); // The server with port 80 + var ab = server.select(['a','b']); // A list of servers containing the server with port 80 and the server with port 8080 + var c = server.select('c'); // A list of servers containing the server with port 8081 and the server with port 8082 */ + select(labels: string | string[]): Server | Server[]; + + /** server.start([callback]) + Starts the server connections by listening for incoming requests on the configured port of each listener (unless the connection was configured with autoListen set to false), where: + callback - optional callback when server startup is completed or failed with the signature function(err) where: + err - any startup error condition. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.start(function (err) { + console.log('Server started at: ' + server.info.uri); + });*/ + start(callback?: (err: any) => void): Promise; + + /** server.state(name, [options]) + HTTP state management uses client cookies to persist a state across multiple requests. Registers a cookie definitions + State defaults can be modified via the server connections.routes.state configuration option. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + // Set cookie definition + server.state('session', { + ttl: 24 * 60 * 60 * 1000, // One day + isSecure: true, + path: '/', + encoding: 'base64json' + }); + // Set state in route handler + var handler = function (request, reply) { + var session = request.state.session; + if (!session) { + session = { user: 'joe' }; + } + session.last = Date.now(); + return reply('Success').state('session', session); + }; + Registered cookies are automatically parsed when received. Parsing rules depends on the route state.parse configuration. If an incoming registered cookie fails parsing, it is not included in request.state, regardless of the state.failAction setting. When state.failAction is set to 'log' and an invalid cookie value is received, the server will emit a 'request-internal' event. To capture these errors subscribe to the 'request-internal' events and filter on 'error' and 'state' tags: + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.on('request-internal', function (request, event, tags) { + if (tags.error && tags.state) { + console.error(event); + } + }); */ + state(name: string, options?: ICookieSettings): void; + + /** server.stop([options], [callback]) + Stops the server's connections by refusing to accept any new connections or requests (existing connections will continue until closed or timeout), where: + options - optional object with: + timeout - overrides the timeout in millisecond before forcefully terminating a connection. Defaults to 5000 (5 seconds). + callback - optional callback method with signature function() which is called once all the connections have ended and it is safe to exit the process. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80 }); + server.stop({ timeout: 60 * 1000 }, function () { + console.log('Server stopped'); + });*/ + stop(options?: { timeout: number }, callback?: () => void): Promise; + + /**server.table([host]) + Returns a copy of the routing table where: + host - optional host to filter routes matching a specific virtual host. Defaults to all virtual hosts. + The return value is an array where each item is an object containing: + info - the connection.info the connection the table was generated for. + labels - the connection labels. + table - an array of routes where each route contains: + settings - the route config with defaults applied. + method - the HTTP method in lower case. + path - the route path. + Note that if the server has not been started and multiple connections use port 0, the table items will override each other and will produce an incomplete result. + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80, host: 'example.com' }); + server.route({ method: 'GET', path: '/example', handler: function (request, reply) { return reply(); } }); + var table = server.table(); + When calling connection.table() directly on each connection, the return value is the same as the array table item value of an individual connection: + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.connection({ port: 80, host: 'example.com' }); + server.route({ method: 'GET', path: '/example', handler: function (request, reply) { return reply(); } }); + var table = server.connections[0].table(); + //[ + // { + // method: 'get', + // path: '/example', + // settings: { ... } + // } + //] + */ + table(host?: any): IConnectionTable; + + /**server.views(options) + Initializes the server views manager + var Hapi = require('hapi'); + var server = new Hapi.Server(); + server.views({ + engines: { + html: require('handlebars'), + jade: require('jade') + }, + path: '/static/templates' + }); + When server.views() is called within a plugin, the views manager is only available to plugins methods.*/ + views(options: IServerViewsConfiguration): void; +} diff --git a/types/hapi/v15/tsconfig.json b/types/hapi/v15/tsconfig.json new file mode 100644 index 0000000000..4a54b80605 --- /dev/null +++ b/types/hapi/v15/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "types": [], + "paths": { + "hapi": [ + "hapi/v15" + ] + }, + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "hapi-tests.ts" + ] +} \ No newline at end of file diff --git a/types/inert/index.d.ts b/types/inert/index.d.ts index cb9b8e8edb..a54841eac7 100644 --- a/types/inert/index.d.ts +++ b/types/inert/index.d.ts @@ -5,10 +5,8 @@ import * as hapi from 'hapi'; -declare module 'hapi' { - interface IFileHandler { - /** path - a path string or function as described above (required). */ - path: string | IRequestHandler; +declare namespace inert { + export interface ReplyFileHandlerOptions { /** confine - serve file relative to this directory and returns 403 Forbidden if the path resolves outside the confine directory. Defaults to true which uses the relativeTo route option as the confine. Set to false to disable this security feature. */ confine?: boolean; /** filename - an optional filename to specify if sending a 'Content-Disposition' header, defaults to the basename of path */ @@ -37,13 +35,18 @@ declare module 'hapi' { end?: number; } - interface IDirectoryHandler { + export interface FileHandlerRouteObject extends ReplyFileHandlerOptions { + /** path - a path string or function as described above (required). */ + path: string | hapi.RequestHandler; + } + + export interface DirectoryHandlerRouteObject { /** path - (required) the directory root path (relative paths are resolved based on the route files configuration). Value can be: * * a single path string used as the prefix for any resources requested by appending the request path parameter to the provided string. * * an array of path strings. Each path will be attempted in order until a match is found (by following the same process as the single path string). * * a function with the signature function(request) which returns the path string or an array of path strings. If the function returns an error, the error is passed back to the client in the response. */ - path: string | string[] | IRequestHandler; + path: string | string[] | hapi.RequestHandler; /** index - optional boolean|string|string[], determines if an index file will be served if found in the folder when requesting a directory. The given string or strings specify the name(s) of the index file to look for. If true, looks for 'index.html'. Any falsy value disables index file lookup. Defaults to true. */ index?: boolean | string | string[]; /** listing - optional boolean, determines if directory listing is generated when a directory is requested without an index document. Defaults to false. */ @@ -65,23 +68,48 @@ declare module 'hapi' { defaultExtension?: string; } - interface IRouteConfiguration { + /** + * inert accepts the following registration options + * @see {@link https://github.com/hapijs/inert#registration-options} + */ + interface OptionalRegistrationOptions { + /** + * sets the maximum number of file etag hash values stored in the etags cache. Defaults to 10000. + */ + etagsCacheMaxSize?: number; + } +} + +declare module 'hapi' { + interface RouteHandlerPlugins { /** * The file handler * * Generates a static file endpoint for serving a single file. file can be set to: * * a relative or absolute file path string (relative paths are resolved based on the route files configuration). * * a function with the signature function(request) which returns the relative or absolute file path. - * * an object with one or more of the following options: + * * an object with one or more of the following options @see IFileHandler * @see {@link https://github.com/hapijs/inert#the-file-handler} */ - file?: string | IRequestHandler | IFileHandler; + file?: string | RequestHandler | inert.FileHandlerRouteObject; /** * The directory handler * * Generates a directory endpoint for serving static content from a directory. Routes using the directory handler must include a path parameter at the end of the path string (e.g. /path/to/somewhere/{param} where the parameter name does not matter). The path parameter can use any of the parameter options (e.g. {param} for one level files only, {param?} for one level files or the directory root, {param*} for any level, or {param*3} for a specific level). If additional path parameters are present, they are ignored for the purpose of selecting the file system resource. The directory handler is an object with the following options: * @see {@link https://github.com/hapijs/inert#the-directory-handler} */ - directory?: IDirectoryHandler; + directory?: inert.DirectoryHandlerRouteObject; + } + + interface Base_Reply { + /** + * Transmits a file from the file system. The 'Content-Type' header defaults to the matching mime type based on filename extension. + * @see {@link https://github.com/hapijs/inert#replyfilepath-options} + */ + file: (path: string, options?: inert.ReplyFileHandlerOptions) => Response; } } + +declare var inert: hapi.PluginFunction; + +export = inert; diff --git a/types/inert/inert-tests.ts b/types/inert/inert-tests.ts index 38830694cd..82cd1f1295 100644 --- a/types/inert/inert-tests.ts +++ b/types/inert/inert-tests.ts @@ -1,36 +1,132 @@ -import * as HapiES6 from 'hapi'; -import * as InertES6 from 'inert'; +// Copied from: https://github.com/hapijs/inert#examples -const server = new HapiES6.Server({}); -server.register(InertES6, () => {}); +/// + +import Path = require('path'); +import Hapi = require('hapi'); +import Inert = require('inert'); + +const server = new Hapi.Server({ + connections: { + routes: { + files: { + relativeTo: Path.join(__dirname, 'public') + } + } + } +}); +server.connection({ port: 3000 }); + +server.register(Inert, () => {}); + +// added in addition to code from docs +const options: Inert.OptionalRegistrationOptions = {etagsCacheMaxSize: 400}; +server.register({ + register: Inert, + options, +}, (err) => {}); + +// added in addition to code from docs +server.register({ + register: Inert, + once: true, +}, (err) => {}); + +server.route({ + method: 'GET', + path: '/{param*}', + handler: { + directory: { + path: '.', + redirectToSlash: true, + index: true + } + } +}); + +server.start((err) => { + + if (err) { + throw err; + } + + console.log('Server running at:', server.info!.uri); +}); + +// https://github.com/hapijs/inert#serving-a-single-file + +server.route({ + method: 'GET', + path: '/{path*}', + handler: { + file: 'page.html' + } +}); + +// https://github.com/hapijs/inert#customized-file-response + +server.route({ + method: 'GET', + path: '/file', + handler: function (request, reply) { + + let path = 'plain.txt'; + if (request.headers['x-magic'] === 'sekret') { + path = 'awesome.png'; + } + + return reply.file(path).vary('x-magic'); + } +}); + +const handler: Hapi.ServerExtRequestHandler = function (request, reply) { + + const response = request.response!; + if (response.isBoom && + response.output!.statusCode === 404) { + + return reply.file('404.html').code(404); + } + + return reply.continue(); +} + +server.ext('onPostHandler', handler); + +// additional code added in addition to doc example code + +var file: Inert.FileHandlerRouteObject = { + path: '', + confine: true, +}; +var directory: Inert.DirectoryHandlerRouteObject = { + path: '', + listing: true +}; + +file = { + path: '', + confine: true, +}; server.route({ path: '', method: 'GET', - file: { - path: '', - confine: true, + handler: { + file, + directory: { + path: function(){ + if(Math.random() > 0.5) { + return ''; + } + else if(Math.random() > 0) { + return ['']; + } + return new Error(''); + }, + BAD_listing: true, // TODO change typings to make this error + }, }, - directory: { - path: '', - listing: true - } + config: { files: { relativeTo: __dirname } } }) -var fileHandler: HapiES6.IFileHandler = { - path: '', - confine: true, -} - -var directoryHandler: HapiES6.IDirectoryHandler = { - path: function(){ - if(Math.random() > 0.5) { - return ''; - } - else if(Math.random() > 0) { - return ['']; - } - return new Error(''); - }, - listing: true, -} diff --git a/types/mime-db/index.d.ts b/types/mime-db/index.d.ts new file mode 100644 index 0000000000..cf46a0c593 --- /dev/null +++ b/types/mime-db/index.d.ts @@ -0,0 +1,21 @@ +// Type definitions for mime-db 1.27 +// Project: https://github.com/jshttp/mime-db +// Definitions by: AJP +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +/** + * Data Structure + * If unknown, every property could be undefined. + * @see {@link https://github.com/jshttp/mime-db#data-structure} + */ +export interface DataStructure { + /** where the mime type is defined. If not set, it's probably a custom media type. */ + source?: string; + /** known extensions associated with this mime type. */ + extensions?: string[]; + /** whether a file of this type can be gzipped. */ + compressible?: boolean; + /** the default charset associated with this type, if any. */ + charset?: string; +} diff --git a/types/mime-db/tsconfig.json b/types/mime-db/tsconfig.json new file mode 100644 index 0000000000..3bc13b0278 --- /dev/null +++ b/types/mime-db/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts" + ] +} \ No newline at end of file diff --git a/types/mimos/index.d.ts b/types/mimos/index.d.ts new file mode 100644 index 0000000000..9d000b0ce9 --- /dev/null +++ b/types/mimos/index.d.ts @@ -0,0 +1,27 @@ +// Type definitions for mimos 3.0 +// Project: https://github.com/hapijs/mimos +// Definitions by: AJP +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +import {DataStructure as MimeDbDataStructure} from 'mime-db'; + +/** + * + * @see {@link https://github.com/hapijs/mimos#new-mimosoptions} + */ +export interface MimosOptions { + /** + * an object hash that is merged into the built in mime information specified here {@link https://github.com/jshttp/mime-db}. Each key value pair represents a single mime object. Each override value should follow this schema: + * * the key is the lower-cased correct mime-type. (Ex. "application/javascript"). + * * the value should an object @see MimosOptionsValue + */ + override: {[index: string]: MimosOptionsValue}; +} + +export interface MimosOptionsValue extends MimeDbDataStructure { + /** specify the type value of result objects, defaults to key. See the example below for more clarification. */ + type?: string; + /** method with signature function(mime) when this mime type is found in the database, this function will run. This allows you make customizations to mime based on developer criteria. */ + predicate?: (mime: MimosOptionsValue) => MimosOptionsValue; +} diff --git a/types/mimos/tsconfig.json b/types/mimos/tsconfig.json new file mode 100644 index 0000000000..3bc13b0278 --- /dev/null +++ b/types/mimos/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts" + ] +} \ No newline at end of file diff --git a/types/nes/client.d.ts b/types/nes/client.d.ts new file mode 100644 index 0000000000..8314894683 --- /dev/null +++ b/types/nes/client.d.ts @@ -0,0 +1,54 @@ +// Type definitions for nes 6.4.2 +// Project: https://github.com/hapijs/nes +// Definitions by: Ivo Stratev +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare class Client { + constructor(url: string, options?: Client.ClientOptions); + onError: (err: any) => void; + onConnect: () => void; + onDisconnect: () => void; + onUpdate: (message: any) => void; + connect(options: Client.ClientConnectOptions, callback: (err?: any) => void): void; + connect(callback: (err?: any) => void): void; + disconnect(): void; + id: any; // can be `null | number` but also the "socket" value from websocket message data. + request(options: string | Client.ClientRequestOptions, callback: (err: any, payload: any, statusCode?: number, headers?: Object) => void): void; + message(message: any, callback: (err: any, message: any) => void): void; + subscribe(path: string, handler: Client.Handler, callback: (err?: any) => void): void; + unsubscribe(path: string, handler: Client.Handler, callback: (err?: any) => void): void; + subscriptions(): string[]; + overrideReconnectionAuth(auth: any): void; +} + +declare namespace Client { + interface Handler { + (message: any, flags: Client.ClientSubscribeFlags): void; + } + + interface ClientOptions { + ws?: any; + timeout?: number | boolean; + } + + interface ClientConnectOptions { + auth?: any; + delay?: number; + maxDelay?: number; + retries?: number; + timeout?: number; + } + + interface ClientRequestOptions { + path: string; + method?: string; + headers?: Object; + payload?: any; + } + + interface ClientSubscribeFlags { + revoked?: boolean; + } +} + +export = Client; diff --git a/types/nes/index.d.ts b/types/nes/index.d.ts index 6ebc9ee1c0..87e428bc29 100644 --- a/types/nes/index.d.ts +++ b/types/nes/index.d.ts @@ -3,38 +3,58 @@ // Definitions by: Ivo Stratev // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -/// +/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + WARNING: BACKWARDS INCOMPATIBLE + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + * + * + * Removal of Nes.Server. No longer need to cast to Nes.Server as Hapi.Server + * has been modified directly. + * Removal of Nes.Request. Same reason as Nes.Server + * Move Nes.Socket from class to just an interface as it is not mentioned + * publically in docs. Perhaps this should be included though? Add + * failing test demonstrating use if so. + */ -declare module 'nes' { - import Hapi = require('hapi'); +import * as Hapi from 'hapi'; +import NesClient = require('nes/client'); - export interface SocketAuthObject { +declare module 'hapi' { + interface Server { + broadcast(message: any, options?: nes.ServerBroadcastOptions): void; + subscription(path: string, options?: nes.ServerSubscriptionOptions): void; + publish(path: string, message: any, options?: nes.ServerPublishOptions): void; + eachSocket(each: (socket: nes.Socket) => void, options?: nes.ServerEachSocketOptions): void; + } + + interface Request { + socket: nes.Socket; + } +} + +declare module nes { + interface SocketAuthObject { isAuthenticated: boolean; credentials: any; artifacts: any; } - export class Socket { - id: any; - app: Object; - auth: SocketAuthObject; - disconect(callback?: () => void): void; - send(message: any, callback?: (err?: any) => void): void; - publish(path: string, message: any, callback?: (err?: any) => void): void; - revoke(path: string, message: any, callback?: (err?: any) => void): void; - } - - export interface ServerBroadcastOptions { + interface ServerBroadcastOptions { user: any } - export interface ServerSubscriptionOptionsFilterOptions { + interface ServerSubscriptionOptionsFilterOptions { socket: Socket; credentials?: any; params?: any; } - export interface ServerSubscriptionOptionsAuthOptions { + interface ServerSubscriptionOptionsAuthOptions { mode?: 'required' | 'optional'; scope?: string | string[]; entity?: 'user' | 'app' | 'any'; @@ -49,82 +69,66 @@ declare module 'nes' { export type ServerOnUnSubscribeWithoutParams = (socket: Socket, path: string, next: () => void) => void; export type ServerOnUnSubscribe = ServerOnUnSubscribeWithParams | ServerOnUnSubscribeWithoutParams; - export interface ServerSubscriptionOptions { - filter?: (path: string, message: any, options: ServerSubscriptionOptionsFilterOptions, next: (isMatch: boolean, override: any) => void) => void; + interface ServerSubscriptionOptions { + filter?: (path: string, message: any, options: ServerSubscriptionOptionsFilterOptions, next: (isMatch: boolean, override?: any) => void) => void; auth?: boolean | ServerSubscriptionOptionsAuthOptions; onSubscribe?: ServerOnSubscribe; onUnsubscribe?: ServerOnUnSubscribe; } - export interface ServerPublishOptions { + interface ServerPublishOptions { internal?: any; user?: any; } - export interface ServerEachSocketOptions { + interface ServerEachSocketOptions { subscription?: string; user?: any; } - export class Server extends Hapi.Server { - broadcast(message: any, options?: ServerBroadcastOptions): void; - subscription(path: string, options?: ServerSubscriptionOptions): void; - publish(path: string, message: any, options?: ServerPublishOptions): void; - eachSocket(each: (socket: Socket) => void, options?: ServerEachSocketOptions): void; + interface Socket { + id: string; + app: Object; + auth: nes.SocketAuthObject; + disconect(callback?: () => void): void; + send(message: any, callback?: (err?: any) => void): void; + publish(path: string, message: any, callback?: (err?: any) => void): void; + revoke(path: string, message: any, callback?: (err?: any) => void): void; } - export class Request extends Hapi.Request { - socket: Socket; - } + /** + * TODO (if possible) use a drier, more robust way of doing this that + * allows for: + * * the export to have be of type Hapi.PluginFunction whilst + * * also exposing the Client type + * * exporting the NesClient as the Client class without having to + * duplicate the constructor definition + * * and all the type exports from the NesClient namespace (Handler, + * ClientOptions, ClientConnectOptions, ClientRequestOptions, + * ClientSubscribeFlags) + */ - export interface ClientOptions { - ws?: any; - timeout?: number | boolean; - } + interface Client extends NesClient {} - export interface ClientConnectOptions { - auth?: any; - delay?: number; - maxDelay?: number; - retries?: number; - timeout?: number; - } + interface Handler extends NesClient.Handler {} - export interface ClientRequestOptions { - path: string; - method?: string; - headers?: Object; - payload?: any; - } + interface ClientOptions extends NesClient.ClientOptions {} - export interface ClientSubscribeFlags { - revoked?: boolean; - } + interface ClientConnectOptions extends NesClient.ClientConnectOptions {} - export class Client { - constructor(url: string, options?: ClientOptions); - onError: (err: any) => void; - onConnect: () => void; - onDisconnect: () => void; - onUpdate: (message: any) => void; - connect(options: ClientConnectOptions, callback: (err?: any) => void): void; - connect(callback: (err?: any) => void): void; - disconnect(): void; - id: any; - request(options: string | ClientRequestOptions, callback: (err: any, payload: any, statusCode?: number, headers?: Object) => void): void; - message(message: any, callback: (err: any, message: any) => void): void; - subscribe(path: string, handler: (message: any, flags: ClientSubscribeFlags) => void, callback: (err?: any) => void): void; - unsubscribe(path: string, handler: (message: any, flags: ClientSubscribeFlags) => void, callback: (err?: any) => void): void; - subscriptions(): string[]; - overrideReconnectionAuth(auth: any): void; - } + interface ClientRequestOptions extends NesClient.ClientRequestOptions {} + + interface ClientSubscribeFlags extends NesClient.ClientSubscribeFlags {} } -declare module 'nes/client' { - export { - Client, - ClientConnectOptions, - ClientRequestOptions, - ClientSubscribeFlags - } from 'nes'; +interface NesClassExports { + Client: { + new(url: string, options?: NesClient.ClientOptions): NesClient; + }; } + +interface NesAllExports extends NesClassExports, Hapi.PluginFunction<{}> {} + +declare var nes: NesAllExports; + +export = nes; diff --git a/types/nes/test/broadcast-client.ts b/types/nes/test/broadcast-client.ts new file mode 100644 index 0000000000..4c94b8c575 --- /dev/null +++ b/types/nes/test/broadcast-client.ts @@ -0,0 +1,25 @@ +// from https://github.com/hapijs/nes#broadcast + +import Nes = require('nes'); + +var client = new Nes.Client('ws://localhost'); +client.connect(function (err) { + + client.onUpdate = function (update) { + + // update -> 'welcome!' + }; +}); + +// Added in addition to nes doc example code + +import NesClient = require('nes/client'); + +var client = new NesClient('ws://localhost'); +client.connect(function (err) { + + client.onUpdate = function (update) { + + // update -> 'welcome!' + }; +}); diff --git a/types/nes/test/broadcast-server.ts b/types/nes/test/broadcast-server.ts new file mode 100644 index 0000000000..dcfcb3400e --- /dev/null +++ b/types/nes/test/broadcast-server.ts @@ -0,0 +1,15 @@ +// from https://github.com/hapijs/nes#broadcast + +import Hapi = require('hapi'); +import Nes = require('nes'); + +var server = new Hapi.Server(); +server.connection(); + +server.register(Nes, function (err) { + + server.start(function (err) { + + server.broadcast('welcome!'); + }); +}); diff --git a/types/nes/test/client-require.ts b/types/nes/test/client-require.ts new file mode 100644 index 0000000000..f5a07ea1d3 --- /dev/null +++ b/types/nes/test/client-require.ts @@ -0,0 +1,12 @@ +// from https://github.com/hapijs/nes/#browser-client +// When you require('nes') it loads the full module and adds a lot of extra code +// that is not needed for the browser. The browser will only need the nes client. +// If you are using CommonJS you can load the client with require('nes/client'). + +import Client = require('nes/client'); + +var options: Client.ClientConnectOptions = { + delay: 3 +} + +var client: Client = new Client('ws://localhost', options); diff --git a/types/nes/nes-tests.ts b/types/nes/test/nes-tests.ts similarity index 72% rename from types/nes/nes-tests.ts rename to types/nes/test/nes-tests.ts index effe4e6c94..90afe68f3e 100644 --- a/types/nes/nes-tests.ts +++ b/types/nes/test/nes-tests.ts @@ -1,22 +1,24 @@ import Hapi = require('hapi'); import Nes = require('nes'); -let server: Hapi.Server = new Hapi.Server(); +var server: Hapi.Server = new Hapi.Server(); server.connection({port: 8080}); -server.register(Nes, (regErr: any) => { +server.register(Nes, (regErr) => { if(regErr) { console.log('register err'); console.log(regErr); } else { - let wsServer: Nes.Server = server as Nes.Server; + // No longer need to cast to Nes.Server as Hapi.Server has been modified directly. + // let wsServer: Nes.Server = server as Nes.Server; + let wsServer: Hapi.Server = server; wsServer.subscription('/item/{id}'); wsServer.route( { method: 'GET', path: '/test', config: { - handler: (request: Hapi.Request, reply: Hapi.IReply) => { - reply({test: 'passes'}); + handler: (request, reply) => { + reply({test: 'passes ' + request.socket.id}); } } }); @@ -34,14 +36,16 @@ server.register(Nes, (regErr: any) => { } }); -let wsClient: Nes.Client = new Nes.Client('ws://localhost:8080'); +let options: Nes.ClientConnectOptions = {delay: 3}; + +let wsClient: Nes.Client = new Nes.Client('ws://localhost:8080', options); wsClient.connect((err: any) => { if(err) { console.log('start err'); console.log(err); } else { wsClient.subscribe('/item/5', (update) => { - wsClient.request('/test', (reqErr: any, payload: any, statusCode: number) => { + wsClient.request('/test', (reqErr, payload, statusCode) => { if(reqErr) { console.log('request err'); console.log(reqErr); diff --git a/types/nes/test/route-authentication-client.ts b/types/nes/test/route-authentication-client.ts new file mode 100644 index 0000000000..fe57eb8d56 --- /dev/null +++ b/types/nes/test/route-authentication-client.ts @@ -0,0 +1,25 @@ +// from https://github.com/hapijs/nes#route-authentication + +import Nes = require('nes'); + +var client = new Nes.Client('ws://localhost'); +client.connect({ auth: { headers: { authorization: 'Basic am9objpzZWNyZXQ=' } } }, function (err) { + + client.request('hello', function (err, payload) { // Can also request '/h' + + // payload -> 'Hello John Doe' + }); +}); + +// Added in addition to nes doc example code + +import NesClient = require('nes/client'); + +var client = new NesClient('ws://localhost'); +client.connect({ auth: { headers: { authorization: 'Basic am9objpzZWNyZXQ=' } } }, function (err) { + + client.request('hello', function (err, payload) { // Can also request '/h' + + // payload -> 'Hello John Doe' + }); +}); diff --git a/types/nes/test/route-authentication-server.ts b/types/nes/test/route-authentication-server.ts new file mode 100644 index 0000000000..5b4634d77d --- /dev/null +++ b/types/nes/test/route-authentication-server.ts @@ -0,0 +1,61 @@ +// from https://github.com/hapijs/nes#route-authentication + +import Hapi = require('hapi'); +import Basic = require('hapi-auth-basic'); +import Bcrypt = require('bcrypt'); +import Nes = require('nes'); + +var server = new Hapi.Server(); +server.connection(); + +server.register([Basic, Nes], function (err) { + + // Set up HTTP Basic authentication + + interface User { + username: string; + password: string; + name: string; + id: string; + } + + var users: {[index: string]: User} = { + john: { + username: 'john', + password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm', // 'secret' + name: 'John Doe', + id: '2133d32a' + } + }; + + var validate: Basic.ValidateFunc = function (request, username, password, callback) { + + var user = users[username]; + if (!user) { + return callback(null, false); + } + + Bcrypt.compare(password, user.password, function (err, isValid) { + + callback(err, isValid, { id: user.id, name: user.name }); + }); + }; + + server.auth.strategy('simple', 'basic', 'required', { validateFunc: validate }); + + // Configure route with authentication + + server.route({ + method: 'GET', + path: '/h', + config: { + id: 'hello', + handler: function (request, reply) { + + return reply('Hello ' + request.auth.credentials.name); + } + } + }); + + server.start(function (err) { /* ... */ }); +}); diff --git a/types/nes/test/route-invocation-client.ts b/types/nes/test/route-invocation-client.ts new file mode 100644 index 0000000000..7b144e3a9c --- /dev/null +++ b/types/nes/test/route-invocation-client.ts @@ -0,0 +1,25 @@ +// from https://github.com/hapijs/nes#route-invocation + +import Nes = require('nes'); + +var client = new Nes.Client('ws://localhost'); +client.connect(function (err) { + + client.request('hello', function (err, payload) { // Can also request '/h' + + // payload -> 'world!' + }); +}); + +// Added in addition to nes doc example code + +import NesClient = require('nes/client'); + +var client = new NesClient('ws://localhost'); +client.connect(function (err) { + + client.request('hello', function (err, payload) { // Can also request '/h' + + // payload -> 'world!' + }); +}); diff --git a/types/nes/test/route-invocation-server.ts b/types/nes/test/route-invocation-server.ts new file mode 100644 index 0000000000..c32780baa9 --- /dev/null +++ b/types/nes/test/route-invocation-server.ts @@ -0,0 +1,24 @@ +// from https://github.com/hapijs/nes#route-invocation + +import Hapi = require('hapi'); +import Nes = require('nes'); + +var server = new Hapi.Server(); +server.connection(); + +server.register(Nes, function (err) { + + server.route({ + method: 'GET', + path: '/h', + config: { + id: 'hello', + handler: function (request, reply) { + + return reply('world!'); + } + } + }); + + server.start(function (err) { /* ... */ }); +}); diff --git a/types/nes/test/subscription-filter-client.ts b/types/nes/test/subscription-filter-client.ts new file mode 100644 index 0000000000..3f8d3ffb8b --- /dev/null +++ b/types/nes/test/subscription-filter-client.ts @@ -0,0 +1,37 @@ +// from https://github.com/hapijs/nes#subscription-filter + +import Nes = require('nes'); + +var client = new Nes.Client('ws://localhost'); + +// Authenticate as 'john' + +client.connect({ auth: { headers: { authorization: 'Basic am9objpzZWNyZXQ=' } } }, function (err) { + + var handler: Nes.Handler = function (err, update) { + + // First publish is not received (filtered due to updater key) + // update -> { id: 6, status: 'initial', updater: 'steve' } + }; + + client.subscribe('/items', handler, function (err) { }); +}); + +// Added in addition to nes doc example code + +import NesClient = require('nes/client'); + +var client = new NesClient('ws://localhost'); + +// Authenticate as 'john' + +client.connect({ auth: { headers: { authorization: 'Basic am9objpzZWNyZXQ=' } } }, function (err) { + + var handler: NesClient.Handler = function (err, update) { + + // First publish is not received (filtered due to updater key) + // update -> { id: 6, status: 'initial', updater: 'steve' } + }; + + client.subscribe('/items', handler, function (err) { }); +}); diff --git a/types/nes/test/subscription-filter-server.ts b/types/nes/test/subscription-filter-server.ts new file mode 100644 index 0000000000..be2dda8d7f --- /dev/null +++ b/types/nes/test/subscription-filter-server.ts @@ -0,0 +1,60 @@ +// from https://github.com/hapijs/nes#subscription-filter + +import Hapi = require('hapi'); +import Basic = require('hapi-auth-basic'); +import Bcrypt = require('bcrypt'); +import Nes = require('nes'); + +var server = new Hapi.Server(); +server.connection(); + +server.register([Basic, Nes], function (err) { + + // Set up HTTP Basic authentication + + interface User { + username: string; + password: string; + name: string; + id: string; + } + + var users: {[index: string]: User} = { + john: { + username: 'john', + password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm', // 'secret' + name: 'John Doe', + id: '2133d32a' + } + }; + + var validate: Basic.ValidateFunc = function (request, username, password, callback) { + + var user = users[username]; + if (!user) { + return callback(null, false); + } + + Bcrypt.compare(password, user.password, function (err, isValid) { + + callback(err, isValid, { id: user.id, name: user.name, username: user.username }); + }); + }; + + server.auth.strategy('simple', 'basic', 'required', { validateFunc: validate }); + + // Set up subscription + + server.subscription('/items', { + filter: function (path, message, options, next) { + + return next(message.updater !== options.credentials.username); + } + }); + + server.start(function (err) { + + server.publish('/items', { id: 5, status: 'complete', updater: 'john' }); + server.publish('/items', { id: 6, status: 'initial', updater: 'steve' }); + }); +}); diff --git a/types/nes/test/subscriptions-client.ts b/types/nes/test/subscriptions-client.ts new file mode 100644 index 0000000000..a9ac55fc6d --- /dev/null +++ b/types/nes/test/subscriptions-client.ts @@ -0,0 +1,31 @@ +// from https://github.com/hapijs/nes#subscriptions + +import Nes = require('nes'); + +var client = new Nes.Client('ws://localhost'); +client.connect(function (err) { + + var handler: Nes.Handler = function (update, flags) { + + // update -> { id: 5, status: 'complete' } + // Second publish is not received (doesn't match) + }; + + client.subscribe('/item/5', handler, function (err) { }); +}); + +// Added in addition to nes doc example code + +import NesClient = require('nes/client'); + +var client = new NesClient('ws://localhost'); +client.connect(function (err) { + + var handler: NesClient.Handler = function (update, flags) { + + // update -> { id: 5, status: 'complete' } + // Second publish is not received (doesn't match) + }; + + client.subscribe('/item/5', handler, function (err) { }); +}); diff --git a/types/nes/test/subscriptions-server.ts b/types/nes/test/subscriptions-server.ts new file mode 100644 index 0000000000..23212f21ea --- /dev/null +++ b/types/nes/test/subscriptions-server.ts @@ -0,0 +1,18 @@ +// from https://github.com/hapijs/nes#subscriptions + +import Hapi = require('hapi'); +import Nes = require('nes'); + +var server = new Hapi.Server(); +server.connection(); + +server.register(Nes, function (err) { + + server.subscription('/item/{id}'); + + server.start(function (err) { + + server.publish('/item/5', { id: 5, status: 'complete' }); + server.publish('/item/6', { id: 6, status: 'initial' }); + }); +}); diff --git a/types/nes/tsconfig.json b/types/nes/tsconfig.json index 74c7bfef19..cb28129b71 100644 --- a/types/nes/tsconfig.json +++ b/types/nes/tsconfig.json @@ -17,6 +17,18 @@ }, "files": [ "index.d.ts", - "nes-tests.ts" + "client.d.ts", + "test/client-require.ts", + "test/nes-tests.ts", + "test/broadcast-client.ts", + "test/broadcast-server.ts", + "test/route-authentication-client.ts", + "test/route-authentication-server.ts", + "test/route-invocation-client.ts", + "test/route-invocation-server.ts", + "test/subscription-filter-client.ts", + "test/subscription-filter-server.ts", + "test/subscriptions-client.ts", + "test/subscriptions-server.ts" ] } \ No newline at end of file diff --git a/types/podium/index.d.ts b/types/podium/index.d.ts new file mode 100644 index 0000000000..cf89d57f82 --- /dev/null +++ b/types/podium/index.d.ts @@ -0,0 +1,186 @@ +// Type definitions for podium 1.0 +// Project: https://github.com/hapijs/podium +// Definitions by: AJP +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +/** + * Podium + * Node (semi) compatible event emitter with extra features. + * podium is an event emitter with support for tags, filters, channels, event update cloning, arguments spreading, and other features useful when building large scale applications. While node's native EventEmitter is strictly focused on maximum performance, it lacks many features that do not belong in the core implementation. podium is not restricted by node's performance requirement as it is designed for application layer needs where it's overhead is largely insignificant as implementing these features will have similar cost on top of the native emitter. + * @see {@link https://github.com/hapijs/podium} + */ +interface Podium { + /** + * Creates a new podium emitter + * @param events if present, the value is passed to podium.registerEvent(). + */ + new(events?: Podium.Events[]): Podium; + new(events?: Podium.Events): Podium; + + /** + * podium.registerEvent(events) + * Register the specified events and their optional configuration. Events must be registered before they can be emitted or subscribed to. This is done to detect event name mispelling and invalid event activities. + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumregistereventevents} + */ + registerEvent(events: Podium.Events[]): void; + registerEvent(events: Podium.Events): void; + + /** + * podium.registerPodium(podiums) + * Registers another emitter as an event source for the current emitter (any event update emitted by the source emitter is passed to any subscriber of the current emitter) + * Note that any events registered with a source emitter are automatically added to the current emitter. If the events are already registered, they are left as-is. + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumregisterpodiumpodiums} + */ + registerPodium(podiums: Podium[]): void; + registerPodium(podiums: Podium): void; + + /** + * podium.emit(criteria, data, [callback]) + * Emits an event update to all the subscribed listeners + * @param criteria the event update criteria + * @param data the value emitted to the subscribers. + * @param callback an optional callback method invoked when all subscribers have been notified using the signature function() + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumemitcriteria-data-callback} + */ + emit(criteria: string | {name: string, channel?: string, tags?: string | string[]}, data: any, callback?: (() => void)): void; + + /** + * podium.on(criteria, listener) + * Subscribe a handler to an event + * @param criteria the subscription criteria + * @param listener the handler method set to receive event updates. The function signature depends on the block, spread, and tags options. + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} + */ + on(criteria: string | Podium.Criteria, listener: Podium.Listener): void; + + /** + * podium.addListener(criteria, listener) + * Same as podium.on() + * @param criteria the subscription criteria + * @param listener the handler method set to receive event updates. The function signature depends on the block, spread, and tags options. + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} + */ + addListener(criteria: string | Podium.Criteria, listener: Podium.Listener): void; + + /** + * podium.once(criteria, listener) + * Same as podium.on() with the count option set to 1. + * @param criteria the subscription criteria + * @param listener the handler method set to receive event updates. The function signature depends on the block, spread, and tags options. + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} + */ + once(criteria: string | Podium.Criteria, listener: Podium.Listener): void; + + /** + * podium.removeListener(name, listener) + * Removes all listeners subscribed to a given event name matching the provided listener method where: + * @param name the event name string. + * @param listener the function reference provided when subscribed. + * Returns a reference to the current emitter. + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumremovelistenername-listener} + */ + removeListener(name: string, listener: Podium.Listener): Podium; + + /** + * podium.removeAllListeners(name) + * Removes all listeners subscribed to a given event name + * @param name the event name string. + * Returns a reference to the current emitter. + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumremovealllistenersname} + */ + removeAllListeners(name: string): Podium; + + /** + * podium.hasListeners(name) + * Returns whether an event has any listeners subscribed + * @param name the event name string. + * Returns true if the event name has any listeners, otherwise false. + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumhaslistenersname} + */ + hasListeners(name: string): boolean; +} + +declare namespace Podium { + /** + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumregistereventevents} + */ + export type Events = string | EventOptionsObject | Podium; + + /** + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumregistereventevents} + */ + export interface EventOptionsObject { + /** the event name string (required). */ + name: string; + /** a string or array of strings specifying the event channels available. Defaults to no channel restrictions (event updates can specify a channel or not). */ + channels?: string | string[]; + /** if true, the data object passed to podium.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). */ + clone?: boolean; + /** if true, the data object passed to podium.emit() must be an array and the listener method is called with each array element passed as a separate argument (unless an override specified 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). */ + spread?: boolean; + /** if true and the criteria object passed to podium.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 the arguments list at the end (but before the callback argument if block is set). A configuration override can be set by each listener. Defaults to false. */ + tags?: boolean; + /** 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 configuration is used. Defaults to false (a duplicate registration will throw an error). */ + shared?: boolean; + } + + /** + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumaddlistenercriteria-listener} + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncecriteria-listener} + */ + export interface CriteriaObject { + /** the event name string (required). */ + name: string; + /** if true, the listener method receives an additional callback argument which must be called when the method completes. No other event will be emitted until the callback methods is called. The method signature is function(). If block is set to a positive integer, the value is used to set a timeout after which any pending events will be emitted, ignoring the eventual call to callback. Defaults to false (non blocking). */ + block?: boolean | number; + /** 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 channels. If channels are specified, event updates without any channel designation will not be included in the subscription. Defaults to no channels filter. */ + channels?: string | string[]; + /** if true, the data object passed to podium.emit() is cloned before it is passed to the listener method. Defaults to the event registration option (which defaults to false). */ + clone?: boolean; + /** 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 podium.once(). Defaults to no limit. */ + count?: number; + /** + * the event tags (if present) to subscribe to which can be one of: + * * a tag string. + * * an array of tag strings. + * * an object with the following: + */ + filter?: string | string[] | CriteriaFilterOptionsObject; + /** if true, and the data object passed to podium.emit() is an array, the listener method is called with each array element passed as a separate argument. This should only be used when the emitted data structure is known and predictable. Defaults to the event registration option (which defaults to false). */ + spread?: boolean; + /** if true and the criteria object passed to podium.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 the arguments list at the end (but before the callback argument if block is set). Defaults to the event registration option (which defaults to false). */ + tags?: boolean; + /** the handler method set to receive event updates. The function signature depends on the block, spread, and tags options. */ + listener?: Listener; + } + + /** + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} + */ + export interface CriteriaFilterOptionsObject { + /** a tag string or array of tag strings. */ + tags?: string | string[]; + /** if true, all tags must be present for the event update to match the subscription. Defaults to false (at least one matching tag). */ + all?: boolean; + } + + /** + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} + */ + export type Criteria = string | CriteriaObject; + + /** + * @see {@link https://github.com/hapijs/podium/blob/master/API.md#podiumoncriteria-listener} + */ + export interface Listener { + (data: any, tags?: Tags, callback?: () => void): void; + } + + export type Tags = {[tag: string]: boolean}; +} + +declare var Podium: Podium; + +export = Podium; diff --git a/types/podium/podium-tests.ts b/types/podium/podium-tests.ts new file mode 100644 index 0000000000..1717d2af1a --- /dev/null +++ b/types/podium/podium-tests.ts @@ -0,0 +1,33 @@ +/// + +import Podium = require('podium'); +const podiumObject = new Podium(); // new emitter + +const podiumObject2 = new Podium('event1');// creates new event and calls registerEvent() + +podiumObject.registerEvent('event1'); + +//with optional parameters +podiumObject.registerEvent({ + name: 'event1', + shared: true +}); + +podiumObject.registerEvent('event1'); + +podiumObject.on('event1',function(update){ // Way 1 + console.log('inside autonomous listener without name! data:', update); +}); + +const listener1 = function() { // normal function object + console.log('listener1 called'); +} +podiumObject.on('event1',listener1); // Way 2 + +// podium.addListener(criteria, listener) Same as podium.on(). + +podiumObject.addListener('event1',listener1); + +// podium.once(criteria, listener) Same as calling podium.on() with the count option set to 1. Whenever we call emit(), listener1 will get fired but also get removed, so that it won't get fired on call to emit(). + +podiumObject.once('event1',listener1); diff --git a/types/podium/tsconfig.json b/types/podium/tsconfig.json new file mode 100644 index 0000000000..267d05537f --- /dev/null +++ b/types/podium/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "podium-tests.ts" + ] +} \ No newline at end of file diff --git a/types/shot/index.d.ts b/types/shot/index.d.ts new file mode 100644 index 0000000000..323fb5137f --- /dev/null +++ b/types/shot/index.d.ts @@ -0,0 +1,115 @@ +// Type definitions for shot 3.4 +// Project: https://github.com/hapijs/shot +// Definitions by: AJP +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +/// + +import stream = require("stream"); +import http = require("http"); + +interface ShotAPI { + /** + * Injects a fake request into an HTTP server. + * @param dispatchFunc listener function. The same as you would pass to Http.createServer when making a node HTTP server. @see IListener + * @param options request options object @see RequestOptions + * @param callback the callback function @see Callback + * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback} + */ + inject(dispatchFunc: Shot.Listener, options: Shot.RequestOptions, callback: (res: Shot.ResponseObject) => void): void; + + /** + * Checks if given object obj is a Shot Request object. + * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotisinjectionobj} + */ + isInjection(obj: any): boolean; +} + +declare namespace Shot { + /** + * listener function. The same as you would pass to Http.createServer when making a node HTTP server. Has the signature function (req, res) where: + * * req - a simulated request object. Inherits from Stream.Readable. + * * res - a simulated response object. Inherits from node's Http.ServerResponse. + * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback} + */ + export interface Listener { + (req: SimulatedRequestObject, res: SimulatedResponseObject): void; + } + + /** + * a simulated request object. Inherits from Stream.Readable. + * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback} + */ + interface SimulatedRequestObject extends stream.Readable {} + + /** + * a simulated response object. Inherits from node's Http.ServerResponse. + * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback} + */ + interface SimulatedResponseObject extends http.ServerResponse {} + + /** + * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback} + */ + export interface RequestOptions { + /** a string specifying the request URL. */ + url: string; + /** a string specifying the HTTP request method, defaulting to 'GET'. */ + method?: string; + /** a string specifying the HTTP HOST header value to be used if no header is provided, and the url does not include an authority component. Defaults to 'localhost'. */ + authority?: string; + /** an optional object containing request headers. */ + headers?: Headers; + /** an optional string specifying the client remote address. Defaults to '127.0.0.1'. */ + remoteAddress?: string; + /** an optional request payload. Can be a string, Buffer, Stream or object. */ + payload?: string | Buffer | stream.Stream | {[key: string]: any}; + /** an object containing flags to simulate various conditions: */ + simulate?: { + /** indicates whether the request will fire an end event. Defaults to undefined, meaning an end event will fire. */ + end?: boolean; + /** indicates whether the request payload will be split into chunks. Defaults to `undefined`, meaning payload will not be chunked. */ + split?: boolean; + /** whether the request will emit an error event. Defaults to undefined, meaning no error event will be emitted. If set to true, the emitted error will have a message of 'Simulated'. */ + error?: boolean; + /** whether the request will emit a close event. Defaults to undefined, meaning no close event will be emitted. */ + close?: boolean; + } + /** Optional flag to validate this options object. Defaults to true. */ + validate?: boolean; + } + + interface Headers { + [header: string]: string; + } + + /** + * @see {@link https://github.com/hapijs/shot/blob/master/API.md#shotinjectdispatchfunc-options-callback} + */ + export interface ResponseObject { + /** an object containing the raw request and response objects where: */ + raw: { + /** the simulated request object. */ + req: SimulatedRequestObject; + /** the simulated response object. */ + res: SimulatedResponseObject; + }; + /** an object containing the response headers. */ + headers: Headers; + /** the HTTP status code. */ + statusCode: number; + /** the HTTP status message. */ + statusMessage: string; + /** the payload as a UTF-8 encoded string. */ + payload: string; + /** the raw payload as a Buffer. */ + rawPayload: Buffer; + /** an object containing the response trailers. */ + trailers: {[index: string]: any}; + } +} + +declare var Shot: ShotAPI; + +export = Shot; diff --git a/types/shot/shot-tests.ts b/types/shot/shot-tests.ts new file mode 100644 index 0000000000..1c6749915b --- /dev/null +++ b/types/shot/shot-tests.ts @@ -0,0 +1,29 @@ +// From: https://github.com/hapijs/shot#example + +// Load modules + +import Http = require('http'); +import Shot = require('shot'); + +// Declare internals + +const internals: any = {}; + +internals.main = function () { + + const dispatch = function (req: Http.IncomingMessage, res: Http.ServerResponse) { + + const reply = 'Hello World'; + res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Length': reply.length }); + res.end(reply); + }; + + const server = Http.createServer(dispatch); + + Shot.inject(dispatch, { method: 'get', url: '/' }, (res) => { + + console.log(res.payload); + }); +}; + +internals.main(); diff --git a/types/shot/tsconfig.json b/types/shot/tsconfig.json new file mode 100644 index 0000000000..eb876b2143 --- /dev/null +++ b/types/shot/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "shot-tests.ts" + ] +} \ No newline at end of file diff --git a/types/vision/index.d.ts b/types/vision/index.d.ts new file mode 100644 index 0000000000..2b0164cc0f --- /dev/null +++ b/types/vision/index.d.ts @@ -0,0 +1,212 @@ +// Type definitions for vision 4.1 +// Project: https://github.com/hapijs/vision +// Definitions by: Jason Swearingen , AJP +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +import * as Hapi from 'hapi'; + +declare module 'hapi' { + interface Server { + /** + * Initializes the server views manager + * @see {@link https://github.com/hapijs/vision/blob/master/API.md#serverviewsoptions} + */ + views(options: ServerViewsConfiguration): ViewManager; + /** + * Utilizes the server views manager to render a template + * @see {@link https://github.com/hapijs/vision/blob/master/API.md#serverrendertemplate-context-options-callback} + */ + render: RenderMethod; + } + + interface Request { + /** + * request.render() works the same way as server.render() but is for use inside of request handlers. server.render() does not work inside request handlers when called via request.server.render() if the view manager was created by a plugin. This is because the request.server object does not have access to the plugin realm where the view manager was configured. request.render() gets its realm from the route that the request was bound to. + * Note that this will not work in onRequest extensions added by the plugin because the route isn't yet set at this point in the request lifecycle and the request.render() method will produce the same limited results server.render() can. + * @see {@link https://github.com/hapijs/vision/blob/master/API.md#requestrendertemplate-context-options-callback} + */ + render: RenderMethod; + } + + interface RouteHandlerPlugins { + /** + * The view handler can be used with routes registered in the same realm as the view manager. The handler takes an options parameter that can be either a string or an object. When the options parameter is a string, it should be the filename and path of the template relative to the templates path configured via the views manager. When the options parameter is an object, it may have the following keys: + * The rendering context contains the `params`, `payload`, `query`, and `pre` values from the request by default (these can be overriden by values explicitly set via the options). + * @see {@link https://github.com/hapijs/vision/blob/master/API.md#the-view-handler} + */ + view?: string | { + /** the template filename and path, relative to the templates path configured via the server views manager. */ + template: string; + /** optional object used by the template to render context-specific result. Defaults to no context {}. */ + context?: Object; + /** + * optional object used to override the server's views manager configuration for this response. Cannot override isCached, partialsPath, or helpersPath which are only loaded at initialization. + * TODO check if it can have `defaultExtension`. + */ + options?: ViewHandlerOrReplyOptions; + } + } + + interface Base_Reply { + /** + * Concludes the handler activity by returning control over to the router with a templatized view response + * Returns a response object. The generated response will have the variety property set to view. + * The response flow control rules apply. + * @param template the template filename and path, relative to the templates path configured via the server views manager. + * @param context optional object used by the template to render context-specific result. Defaults to no context {}. + * @param options optional object used to override the server's views manager configuration for this response. Cannot override isCached, partialsPath, or helpersPath which are only loaded at initialization. + * @see {@link https://github.com/hapijs/vision/blob/master/API.md#replyviewtemplate-context-options} + */ + view(templatePath: string, context?: any, options?: ViewHandlerOrReplyOptions): Response; + } +} + +/** + * Options for initialising server views manager + * @see {@link https://github.com/hapijs/vision/blob/master/API.md#serverviewsoptions} + */ +export interface ServerViewsConfiguration extends ServerViewsAdditionalOptions { + /** required object where each key is a file extension (e.g. 'html', 'hbr'), mapped to the npm module used for rendering the templates. Alternatively, the extension can be mapped to an object */ + engines: {[fileExtension: string]: NpmModule} | ServerViewsEnginesOptions; + /** defines the default filename extension to append to template names when multiple engines are configured and no explicit extension is provided for a given template. No default value. */ + defaultExtension?: string; +} + +/** + * Includes `module` and any of the views options listed below (@see ServerViewsAdditionalOptions) (except defaultExtension) to override the defaults for a specific engine. + */ +export interface ServerViewsEnginesOptions extends ServerViewsAdditionalOptions { + /** + * The npm module used for rendering the templates. The module object must contain the compile() function + * @see {@link https://github.com/hapijs/vision/blob/master/API.md#serverviewsoptions} > options > engines > module + */ + module: NpmModule +} +export interface ServerViewsAdditionalOptions extends ViewHandlerOrReplyOptions { + /** the root file path, or array of file paths, where partials are located. Partials are small segments of template code that can be nested and reused throughout other templates. Defaults to no partials support (empty path). */ + partialsPath?: string | string[]; + /** the directory path, or array of directory paths, where helpers are located. Helpers are functions used within templates to perform transformations and other data manipulations using the template context or other inputs. Each '.js' file in the helpers directory is loaded and the file name is used as the helper name. The files must export a single method with the signature function(context) and return a string. Sub-folders are not supported and are ignored. Defaults to no helpers support (empty path). Note that jade does not support loading helpers this way. */ + helpersPath?: string | string[]; + /** if set to false, templates will not be cached (thus will be read from file on every use). Defaults to true. */ + isCached?: boolean; +} + +/** + * + * @see {@link https://github.com/hapijs/vision/blob/master/API.md#the-view-handler} > options for the list of attributes it can not have (isCached, partialsPath, helpersPath) + */ +export interface ViewHandlerOrReplyOptions { + /** the root file path, or array of file paths, used to resolve and load the templates identified when calling reply.view(). Defaults to current working directory. */ + path?: string | string[]; + /** + * a base path used as prefix for path and partialsPath. No default. + * TODO update if PR for updating docs is accepted. + */ + relativeTo?: string; + /** if set to true or a layout filename, layout support is enabled. A layout is a single template file used as the parent template for other view templates in the same engine. If true, the layout template name must be 'layout.ext' where 'ext' is the engine's extension. Otherwise, the provided filename is suffixed with the engine's extension and loaded. Disable layout when using Jade as it will handle including any layout files independently. Defaults to false. */ + layout?: boolean; + /** the root file path, or array of file paths, where layout templates are located (using the relativeTo prefix if present). Defaults to path. */ + layoutPath?: string | string[]; + /** the key used by the template engine to denote where primary template content should go. Defaults to 'content'. */ + layoutKeyword?: string; + /** the text encoding used by the templates when reading the files and outputting the result. Defaults to 'utf8'. */ + encoding?: string; + /** if set to true, allows absolute template paths passed to reply.view(). Defaults to false. */ + allowAbsolutePaths?: boolean; + /** if set to true, allows template paths passed to reply.view() to contain '../'. Defaults to false. */ + allowInsecureAccess?: boolean; + /** options object passed to the engine's compile function. Defaults to empty options {}. */ + compileOptions?: CompileOptions; + /** options object passed to the returned function from the compile operation. Defaults to empty options {}. */ + runtimeOptions?: RuntimeOptions; + /** the content type of the engine results. Defaults to 'text/html'. */ + contentType?: string; + /** specify whether the engine compile() method is 'sync' or 'async'. Defaults to 'sync'. */ + compileMode?: 'sync' | 'async'; + /** a global context used with all templates. The global context option can be either an object or a function that takes the request as its only argument and returns a context object. The request object is only provided when using the view handler or reply.view(). When using server.render() or request.render(), the request argument will be null. When rendering views, the global context will be merged with any context object specified on the handler or using reply.view(). When multiple context objects are used, values from the global context always have lowest precedence. */ + context?: Object | ((request: Hapi.Request) => Object); +} + +/** + * Options passed to module when compiling template. + * Cast your options to this interface or extend it with: + * declare module 'hapi' { + * interface CompileOptions { + * noEscape: boolean; + * } + * } + */ +export interface CompileOptions {} +export interface RuntimeOptions {} + +/** + * the rendering function. The required function signature depends on the compileMode settings (see below). + * If compileMode is 'sync', the signature is compile(template, options), the return value is a function with signature function(context, options) (the compiled sync template), and the method is allowed to throw errors. + * If compileMode is 'async', the signature is compile(template, options, next) where next has the signature function(err, compiled), compiled is a function with signature function(context, options, callback) (the compiled async template) and callback has the signature function(err, rendered). + */ +export interface ServerViewCompile { + (template: string, options: any): (context: any, options: any) => void; + (template: string, options: any, next: (err: Error | null, compiled: (context: any, options: any, callback: (err: null | Error, rendered: string | null) => void) => void) => void): void; +} + +/** + * The npm module used for rendering the templates. The module object must contain the compile() function + * @see {@link https://github.com/hapijs/vision/blob/master/API.md#serverviewsoptions} > options > engines > module + */ +interface NpmModule { + /** the rendering function. The required function signature depends on the compileMode settings */ + compile: ServerViewCompile + /** initializes additional engine state. The config object is the engine configuration object allowing updates to be made. This is useful for engines like Nunjucks that rely on additional state for rendering. next has the signature function(err). */ + prepare(config: EngineConfigurationObject, next: (err?: Error) => void): void; + /** registers a partial for use during template rendering. The name is the partial path that templates should use to reference the partial and src is the uncompiled template string for the partial. */ + registerPartial(name: string, src: string): void; + /** registers a helper for use during template rendering. The name is the name that templates should use to reference the helper and helper is the function that will be invoked when the helper is called. */ + registerHelper(name: string, helper: Function): void; +} +export interface EngineConfigurationObject {} + +/** + * Renders a template + * @param template - the template filename and path, relative to the views manager templates path (path or relativeTo). + * @param context - optional object used by the template to render context-specific result. Defaults to no context ({}). + * @param options - optional object used to override the views manager configuration. + * @param callback - the callback function with signature function (err, rendered, config) where: + * * err - the rendering error if any. + * * rendered - the result view string or undefined if error. + * * config - the configuration used to render the template or undefined if error. + * If no `callback` is provided, a `Promise` object is returned. The returned promise is resolved with only the rendered content and not the configuration object. + * @see {@link https://github.com/hapijs/vision/blob/master/API.md#serverrendertemplate-context-options-callback} + * @see {@link https://github.com/hapijs/vision/blob/master/API.md#managerrendertemplate-context-options-callback} + */ +interface RenderMethod { + (template: string, context?: any, options?: ServerViewsAdditionalOptions, callback?: RenderCallBack): void; + (template: string, context?: any, options?: ServerViewsAdditionalOptions): Promise; +} +export interface RenderCallBack { + (err: null | Error, rendered?: string, options?: ServerViewsAdditionalOptions): void; +} + +/** + * View Manager + * @see {@link https://github.com/hapijs/vision/blob/master/API.md#view-manager} + */ +export interface ViewManager { + /** + * Registers a helper, on all configured engines that have a registerHelper() method, for use during template rendering. Engines without a registerHelper() method will be skipped. The name is the name that templates should use to reference the helper and helper is the function that will be invoked when the helper is called. + * @param name + * @param helper + * @see {@link https://github.com/hapijs/vision/blob/master/API.md#managerregisterhelpername-helper} + */ + registerHelper(name: string, helper: Function): void; + /** + * Renders a template. This is typically not needed and it is usually more convenient to use server.render(). + * @see {@link https://github.com/hapijs/vision/blob/master/API.md#managerrendertemplate-context-options-callback} + */ + render: RenderMethod; +} + +// Has no plugin registration options +declare var visionPlugin: Hapi.PluginFunction<{}>; + +export default visionPlugin; diff --git a/types/vision/tsconfig.json b/types/vision/tsconfig.json new file mode 100644 index 0000000000..9d1c80a15c --- /dev/null +++ b/types/vision/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "vision-tests.ts" + ] +} \ No newline at end of file diff --git a/types/vision/vision-tests.ts b/types/vision/vision-tests.ts new file mode 100644 index 0000000000..74f8f432b0 --- /dev/null +++ b/types/vision/vision-tests.ts @@ -0,0 +1,128 @@ + +/// + +import * as Hapi from 'hapi'; +import Vision from 'vision'; +const server = new Hapi.Server(); +server.connection({ port: 80 }); + +// https://github.com/hapijs/vision/blob/master/API.md#serverrendertemplate-context-options-callback + +server.register(Vision, (err) => { + + if (err) { + throw err; + } + + server.views({ + engines: { html: require('handlebars') }, + path: __dirname + '/templates' + }); + + const context = { + title: 'Views Example', + message: 'Hello, World' + }; + + server.render('hello', context, {}, (err, rendered, config) => { + + console.log(rendered); + }); + + // https://github.com/hapijs/vision/blob/master/API.md#requestrendertemplate-context-options-callback + + server.views({ + engines: { html: require('handlebars') }, + path: __dirname + '/templates' + }); + + server.route({ + method: 'GET', + path: '/view', + handler: function (request, reply) { + + request.render('test', { message: 'hello' }, {}, (err, rendered, config) => { + + return reply(rendered); + }); + } + }); + + // https://github.com/hapijs/vision/blob/master/API.md#the-view-handler + + server.views({ + engines: { html: require('handlebars') }, + path: __dirname + '/templates' + }); + + server.route({ + method: 'GET', + path: '/', + handler: { + view: { + template: 'hello', + context: { + title: 'Views Example', + message: 'Hello, World' + } + } + } + }); + + // https://github.com/hapijs/vision/blob/master/API.md#replyviewtemplate-context-options + + server.views({ + engines: { html: require('handlebars') }, + path: __dirname + '/templates' + }); + + const handler: Hapi.RouteHandler = function (request, reply) { + + const context = { + title: 'Views Example', + message: 'Hello, World' + }; + + return reply.view('hello', context); + }; + + server.route({ method: 'GET', path: '/', handler: handler }); + +}); + +// Extending CompileOptions or RuntimeOptions interfaces + +server.register(Vision, (err) => { + + if (err) { + throw err; + } + + server.views({ + engines: { html: require('handlebars') }, + path: __dirname + '/templates' + }); + + var opts: Hapi.CompileOptions = { + noEscape: true + }; + + server.route({ + method: 'GET', + path: '/temp1', + handler: { + view: { + template: 'temp1', + options: { + compileOptions: opts + } + } + } + }); +}); + +declare module 'hapi' { + interface CompileOptions { + noEscape: boolean; + } +}