diff --git a/types/h2o2/tsconfig.json b/types/h2o2/tsconfig.json index 9d5b689ee1..eb41a55289 100644 --- a/types/h2o2/tsconfig.json +++ b/types/h2o2/tsconfig.json @@ -19,6 +19,9 @@ ], "hapi": [ "hapi/v16" + ], + "inert": [ + "inert/v4" ] }, "noEmit": true, diff --git a/types/hapi-auth-jwt2/tsconfig.json b/types/hapi-auth-jwt2/tsconfig.json index 1f8cc10075..ada8a3f94d 100644 --- a/types/hapi-auth-jwt2/tsconfig.json +++ b/types/hapi-auth-jwt2/tsconfig.json @@ -19,6 +19,9 @@ ], "hapi": [ "hapi/v16" + ], + "inert": [ + "inert/v4" ] }, "noEmit": true, @@ -28,4 +31,4 @@ "index.d.ts", "hapi-auth-jwt2-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/hapi-decorators/tsconfig.json b/types/hapi-decorators/tsconfig.json index 44437f4b9e..a018c33c53 100644 --- a/types/hapi-decorators/tsconfig.json +++ b/types/hapi-decorators/tsconfig.json @@ -21,6 +21,9 @@ ], "hapi": [ "hapi/v16" + ], + "inert": [ + "inert/v4" ] }, "noEmit": true, @@ -30,4 +33,4 @@ "index.d.ts", "hapi-decorators-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/hapi/v16/tsconfig.json b/types/hapi/v16/tsconfig.json index d3d30f97dc..517d678910 100644 --- a/types/hapi/v16/tsconfig.json +++ b/types/hapi/v16/tsconfig.json @@ -19,6 +19,9 @@ ], "hapi": [ "hapi/v16" + ], + "inert": [ + "inert/v4" ] }, "noEmit": true, diff --git a/types/inert/index.d.ts b/types/inert/index.d.ts index e74d1a4ce9..d8c1d90422 100644 --- a/types/inert/index.d.ts +++ b/types/inert/index.d.ts @@ -1,16 +1,28 @@ -// Type definitions for inert 4.2 +// Type definitions for inert 5.1 // Project: https://github.com/hapijs/inert/ -// Definitions by: Steve Ognibene , AJP +// Definitions by: Steve Ognibene +// Alexander James Phillips +// Silas Rech // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.4 -import * as hapi from 'hapi'; +import { + Plugin, + Request, +} from 'hapi'; 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. */ + type RequestHandler = (request: Request) => T; + + 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 */ + /** + * filename - an optional filename to specify if sending a 'Content-Disposition' header, defaults to the basename of path + */ filename?: string; /** * mode - specifies whether to include the 'Content-Disposition' header with the response. Available values: @@ -19,9 +31,13 @@ declare namespace inert { * *'inline' */ mode?: false | 'attachment' | 'inline'; - /** lookupCompressed - if true, looks for for a pre-compressed version of the file with the same filename with an extension, depending on the accepted encoding. Defaults to false. */ + /** + * lookupCompressed - if true, looks for for a pre-compressed version of the file with the same filename with an extension, depending on the accepted encoding. Defaults to false. + */ lookupCompressed?: boolean; - /** lookupMap - an object which maps content encoding to expected file name extension. Defaults to `{ gzip: '.gz' }. */ + /** + * lookupMap - an object which maps content encoding to expected file name extension. Defaults to `{ gzip: '.gz' }. + */ lookupMap?: {[index: string]: string}; /** * etagMethod - specifies the method used to calculate the ETag header response. Available values: @@ -30,33 +46,56 @@ declare namespace inert { * * false - Disable ETag computation. */ etagMethod?: 'hash' | 'simple' | false; - /** start - offset in file to reading from, defaults to 0. */ + /** + * start - offset in file to reading from, defaults to 0. + */ start?: number; - /** end - offset in file to stop reading from. If not set, will read to end of file. */ + /** + * end - offset in file to stop reading from. If not set, will read to end of file. + */ end?: number; } - export interface FileHandlerRouteObject extends ReplyFileHandlerOptions { - /** path - a path string or function as described above (required). */ - path: string | hapi.RequestHandler; + interface FileHandlerRouteObject extends ReplyFileHandlerOptions { + /** + * path - a path string or function as described above (required). + */ + path: string | 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. + 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[] | 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. */ - 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. */ + /** + * listing - optional boolean, determines if directory listing is generated when a directory is requested without an index document. Defaults to false. + */ listing?: boolean; - /** showHidden - optional boolean, determines if hidden files will be shown and served. Defaults to false. */ + /** + * showHidden - optional boolean, determines if hidden files will be shown and served. Defaults to false. + */ showHidden?: boolean; - /** 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. */ + /** + * 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. + */ redirectToSlash?: boolean; - /** 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. */ + /** + * 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. + */ lookupCompressed?: boolean; /** * etagMethod - specifies the method used to calculate the ETag header response. Available values: @@ -65,7 +104,9 @@ declare namespace inert { * * false - Disable ETag computation. */ etagMethod?: 'hash' | 'simple' | false; - /** defaultExtension - optional string, appended to file requests if the requested file is not found. Defaults to no extension. */ + /** + * defaultExtension - optional string, appended to file requests if the requested file is not found. Defaults to no extension. + */ defaultExtension?: string; } @@ -81,8 +122,12 @@ declare namespace inert { } } +declare const inert: Plugin; + +export = inert; + declare module 'hapi' { - interface RouteHandlerPlugins { + interface RouteOptions { /** * The file handler * @@ -92,25 +137,33 @@ declare module 'hapi' { * * an object with one or more of the following options @see IFileHandler * @see {@link https://github.com/hapijs/inert#the-file-handler} */ - file?: string | RequestHandler | inert.FileHandlerRouteObject; + file?: string | inert.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: + * 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?: inert.DirectoryHandlerRouteObject; + files?: { + /** + * Set the relative path + */ + relativeTo: string; + }; } - interface Base_Reply { + interface ResponseToolkit { /** * 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; + file(path: string, options?: inert.ReplyFileHandlerOptions): ResponseObject; } } - -declare var inert: hapi.PluginFunction; - -export = inert; diff --git a/types/inert/inert-tests.ts b/types/inert/inert-tests.ts index 9062e13f6b..b772a38259 100644 --- a/types/inert/inert-tests.ts +++ b/types/inert/inert-tests.ts @@ -1,130 +1,106 @@ -// Copied from: https://github.com/hapijs/inert#examples +import { + Server, + Lifecycle, +} from 'hapi'; -import Path = require('path'); -import Hapi = require('hapi'); -import Inert = require('inert'); +import * as path from 'path'; +import * as inert from 'inert'; -const server = new Hapi.Server({ - connections: { - routes: { - files: { - relativeTo: Path.join(__dirname, 'public') +const server = new Server({ + port: 3000, + routes: { + files: { + relativeTo: path.join(__dirname, 'public') + } + } +}); + +const provision = async () => { + await server.register(inert); + + await server.register({ + plugin: inert, + options: { etagsCacheMaxSize: 400 }, + }); + + await server.register({ + plugin: inert, + once: true, + }); + + server.route({ + method: 'GET', + path: '/{param*}', + handler: { + directory: { + path: '.', + redirectToSlash: true, + index: true } } - } -}); -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 + // https://github.com/hapijs/inert#serving-a-single-file + server.route({ + method: 'GET', + path: '/{path*}', + handler: { + file: 'page.html' } - } -}); + }); -server.start((err) => { + // https://github.com/hapijs/inert#customized-file-response + server.route({ + method: 'GET', + path: '/file', + handler(request, reply) { + let path = 'plain.txt'; + if (request.headers['x-magic'] === 'sekret') { + path = 'awesome.png'; + } - if (err) { - throw err; - } + return reply.file(path).vary('x-magic'); + } + }); - 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'; + const handler: Lifecycle.Method = (request, h) => { + const response = request.response; + if (response instanceof Error && response.output.statusCode === 404) { + return h.file('404.html').code(404); } - return reply.file(path).vary('x-magic'); - } -}); + return h.continue; + }; -const handler: Hapi.ServerExtRequestHandler = function (request, reply) { + server.ext('onPostHandler', handler); - const response = request.response!; - if (response.isBoom && - response.output!.statusCode === 404) { + const file: inert.FileHandlerRouteObject = { + path: '', + confine: true, + }; - return reply.file('404.html').code(404); - } + const directory: inert.DirectoryHandlerRouteObject = { + path: '', + listing: true + }; - 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', - handler: { - file, - directory: { - path: function(){ - if(Math.random() > 0.5) { - return ''; - } - else if(Math.random() > 0) { - return ['']; - } - return new Error(''); + server.route({ + path: '', + method: 'GET', + handler: { + file, + directory: { + path() { + if (Math.random() > 0.5) { + return ''; + } else if (Math.random() > 0) { + return ['']; + } + return new Error(''); + }, + BAD_listing: true, }, - BAD_listing: true, // TODO change typings to make this error }, - }, - config: { files: { relativeTo: __dirname } } -}) - + options: { files: { relativeTo: __dirname } } + }); +}; diff --git a/types/inert/tsconfig.json b/types/inert/tsconfig.json index c96c356741..238aa1a3eb 100644 --- a/types/inert/tsconfig.json +++ b/types/inert/tsconfig.json @@ -16,9 +16,6 @@ "paths": { "boom": [ "boom/v4" - ], - "hapi": [ - "hapi/v16" ] }, "noEmit": true, @@ -28,4 +25,4 @@ "index.d.ts", "inert-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/inert/tslint.json b/types/inert/tslint.json index a41bf5d19a..3db14f85ea 100644 --- a/types/inert/tslint.json +++ b/types/inert/tslint.json @@ -1,79 +1 @@ -{ - "extends": "dtslint/dt.json", - "rules": { - "adjacent-overload-signatures": false, - "array-type": false, - "arrow-return-shorthand": false, - "ban-types": false, - "callable-types": false, - "comment-format": false, - "dt-header": false, - "eofline": false, - "export-just-namespace": false, - "import-spacing": false, - "interface-name": false, - "interface-over-type-literal": false, - "jsdoc-format": false, - "max-line-length": false, - "member-access": false, - "new-parens": false, - "no-any-union": false, - "no-boolean-literal-compare": false, - "no-conditional-assignment": false, - "no-consecutive-blank-lines": false, - "no-construct": false, - "no-declare-current-package": false, - "no-duplicate-imports": false, - "no-duplicate-variable": false, - "no-empty-interface": false, - "no-for-in-array": false, - "no-inferrable-types": false, - "no-internal-module": false, - "no-irregular-whitespace": false, - "no-mergeable-namespace": false, - "no-misused-new": false, - "no-namespace": false, - "no-object-literal-type-assertion": false, - "no-padding": false, - "no-redundant-jsdoc": false, - "no-redundant-jsdoc-2": false, - "no-redundant-undefined": false, - "no-reference-import": false, - "no-relative-import-in-test": false, - "no-self-import": false, - "no-single-declare-module": false, - "no-string-throw": false, - "no-unnecessary-callback-wrapper": false, - "no-unnecessary-class": false, - "no-unnecessary-generics": false, - "no-unnecessary-qualifier": false, - "no-unnecessary-type-assertion": false, - "no-useless-files": false, - "no-var-keyword": false, - "no-var-requires": false, - "no-void-expression": false, - "no-trailing-whitespace": false, - "object-literal-key-quotes": false, - "object-literal-shorthand": false, - "one-line": false, - "one-variable-per-declaration": false, - "only-arrow-functions": false, - "prefer-conditional-expression": false, - "prefer-const": false, - "prefer-declare-function": false, - "prefer-for-of": false, - "prefer-method-signature": false, - "prefer-template": false, - "radix": false, - "semicolon": false, - "space-before-function-paren": false, - "space-within-parens": false, - "strict-export-declare-modifiers": false, - "trim-file": false, - "triple-equals": false, - "typedef-whitespace": false, - "unified-signatures": false, - "void-return": false, - "whitespace": false - } -} +{ "extends": "dtslint/dt.json" } diff --git a/types/inert/v4/index.d.ts b/types/inert/v4/index.d.ts new file mode 100644 index 0000000000..e74d1a4ce9 --- /dev/null +++ b/types/inert/v4/index.d.ts @@ -0,0 +1,116 @@ +// Type definitions for inert 4.2 +// Project: https://github.com/hapijs/inert/ +// Definitions by: Steve Ognibene , AJP +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 + +import * as hapi from 'hapi'; + +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 */ + filename?: string; + /** + * mode - 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?: false | 'attachment' | 'inline'; + /** lookupCompressed - if true, looks for for a pre-compressed version of the file with the same filename with an extension, depending on the accepted encoding. Defaults to false. */ + lookupCompressed?: boolean; + /** lookupMap - an object which maps content encoding to expected file name extension. Defaults to `{ gzip: '.gz' }. */ + lookupMap?: {[index: string]: string}; + /** + * etagMethod - specifies the method used to calculate the ETag header response. Available values: + * * 'hash' - SHA1 sum of the file contents, suitable for distributed deployments. Default value. + * * 'simple' - Hex encoded size and modification date, suitable when files are stored on a single server. + * * false - Disable ETag computation. + */ + etagMethod?: 'hash' | 'simple' | false; + /** start - offset in file to reading from, defaults to 0. */ + start?: number; + /** end - offset in file to stop reading from. If not set, will read to end of file. */ + end?: number; + } + + 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[] | 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. */ + listing?: boolean; + /** showHidden - optional boolean, determines if hidden files will be shown and served. Defaults to false. */ + showHidden?: boolean; + /** 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. */ + redirectToSlash?: boolean; + /** 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. */ + lookupCompressed?: boolean; + /** + * etagMethod - specifies the method used to calculate the ETag header response. Available values: + * * 'hash' - SHA1 sum of the file contents, suitable for distributed deployments. Default value. + * * 'simple' - Hex encoded size and modification date, suitable when files are stored on a single server. + * * false - Disable ETag computation. + */ + etagMethod?: 'hash' | 'simple' | false; + /** defaultExtension - optional string, appended to file requests if the requested file is not found. Defaults to no extension. */ + defaultExtension?: string; + } + + /** + * 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 @see IFileHandler + * @see {@link https://github.com/hapijs/inert#the-file-handler} + */ + 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?: 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/v4/inert-tests.ts b/types/inert/v4/inert-tests.ts new file mode 100644 index 0000000000..9062e13f6b --- /dev/null +++ b/types/inert/v4/inert-tests.ts @@ -0,0 +1,130 @@ +// Copied from: https://github.com/hapijs/inert#examples + +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', + 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 + }, + }, + config: { files: { relativeTo: __dirname } } +}) + diff --git a/types/inert/v4/tsconfig.json b/types/inert/v4/tsconfig.json new file mode 100644 index 0000000000..fd468493b4 --- /dev/null +++ b/types/inert/v4/tsconfig.json @@ -0,0 +1,34 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "types": [], + "paths": { + "boom": [ + "boom/v4" + ], + "hapi": [ + "hapi/v16" + ], + "inert": [ + "inert/v4" + ] + }, + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "inert-tests.ts" + ] +} diff --git a/types/inert/v4/tslint.json b/types/inert/v4/tslint.json new file mode 100644 index 0000000000..a41bf5d19a --- /dev/null +++ b/types/inert/v4/tslint.json @@ -0,0 +1,79 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "adjacent-overload-signatures": false, + "array-type": false, + "arrow-return-shorthand": false, + "ban-types": false, + "callable-types": false, + "comment-format": false, + "dt-header": false, + "eofline": false, + "export-just-namespace": false, + "import-spacing": false, + "interface-name": false, + "interface-over-type-literal": false, + "jsdoc-format": false, + "max-line-length": false, + "member-access": false, + "new-parens": false, + "no-any-union": false, + "no-boolean-literal-compare": false, + "no-conditional-assignment": false, + "no-consecutive-blank-lines": false, + "no-construct": false, + "no-declare-current-package": false, + "no-duplicate-imports": false, + "no-duplicate-variable": false, + "no-empty-interface": false, + "no-for-in-array": false, + "no-inferrable-types": false, + "no-internal-module": false, + "no-irregular-whitespace": false, + "no-mergeable-namespace": false, + "no-misused-new": false, + "no-namespace": false, + "no-object-literal-type-assertion": false, + "no-padding": false, + "no-redundant-jsdoc": false, + "no-redundant-jsdoc-2": false, + "no-redundant-undefined": false, + "no-reference-import": false, + "no-relative-import-in-test": false, + "no-self-import": false, + "no-single-declare-module": false, + "no-string-throw": false, + "no-unnecessary-callback-wrapper": false, + "no-unnecessary-class": false, + "no-unnecessary-generics": false, + "no-unnecessary-qualifier": false, + "no-unnecessary-type-assertion": false, + "no-useless-files": false, + "no-var-keyword": false, + "no-var-requires": false, + "no-void-expression": false, + "no-trailing-whitespace": false, + "object-literal-key-quotes": false, + "object-literal-shorthand": false, + "one-line": false, + "one-variable-per-declaration": false, + "only-arrow-functions": false, + "prefer-conditional-expression": false, + "prefer-const": false, + "prefer-declare-function": false, + "prefer-for-of": false, + "prefer-method-signature": false, + "prefer-template": false, + "radix": false, + "semicolon": false, + "space-before-function-paren": false, + "space-within-parens": false, + "strict-export-declare-modifiers": false, + "trim-file": false, + "triple-equals": false, + "typedef-whitespace": false, + "unified-signatures": false, + "void-return": false, + "whitespace": false + } +} diff --git a/types/swagger-express-mw/tsconfig.json b/types/swagger-express-mw/tsconfig.json index 1d597728f7..c1db9f10a4 100644 --- a/types/swagger-express-mw/tsconfig.json +++ b/types/swagger-express-mw/tsconfig.json @@ -19,6 +19,9 @@ ], "hapi": [ "hapi/v16" + ], + "inert": [ + "inert/v4" ] }, "noEmit": true, @@ -28,4 +31,4 @@ "index.d.ts", "swagger-express-mw-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/swagger-hapi/tsconfig.json b/types/swagger-hapi/tsconfig.json index 410688bcf1..df8e735e23 100644 --- a/types/swagger-hapi/tsconfig.json +++ b/types/swagger-hapi/tsconfig.json @@ -19,6 +19,9 @@ ], "hapi": [ "hapi/v16" + ], + "inert": [ + "inert/v4" ] }, "noEmit": true, @@ -28,4 +31,4 @@ "index.d.ts", "swagger-hapi-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/swagger-node-runner/tsconfig.json b/types/swagger-node-runner/tsconfig.json index e559433107..d8bb8c8f52 100644 --- a/types/swagger-node-runner/tsconfig.json +++ b/types/swagger-node-runner/tsconfig.json @@ -19,6 +19,9 @@ ], "hapi": [ "hapi/v16" + ], + "inert": [ + "inert/v4" ] }, "noEmit": true, @@ -28,4 +31,4 @@ "index.d.ts", "swagger-node-runner-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/swagger-restify-mw/tsconfig.json b/types/swagger-restify-mw/tsconfig.json index 8c5f09a930..ec732988fc 100644 --- a/types/swagger-restify-mw/tsconfig.json +++ b/types/swagger-restify-mw/tsconfig.json @@ -19,6 +19,9 @@ ], "hapi": [ "hapi/v16" + ], + "inert": [ + "inert/v4" ] }, "noEmit": true, @@ -28,4 +31,4 @@ "index.d.ts", "swagger-restify-mw-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/swagger-sails-hook/tsconfig.json b/types/swagger-sails-hook/tsconfig.json index 508453b078..98768dd651 100644 --- a/types/swagger-sails-hook/tsconfig.json +++ b/types/swagger-sails-hook/tsconfig.json @@ -19,6 +19,9 @@ ], "hapi": [ "hapi/v16" + ], + "inert": [ + "inert/v4" ] }, "noEmit": true, @@ -28,4 +31,4 @@ "index.d.ts", "swagger-sails-hook-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/vision/v4/tsconfig.json b/types/vision/v4/tsconfig.json index bee5c542c4..2ab1f666a0 100644 --- a/types/vision/v4/tsconfig.json +++ b/types/vision/v4/tsconfig.json @@ -20,6 +20,9 @@ "hapi": [ "hapi/v16" ], + "inert": [ + "inert/v4" + ], "vision": [ "vision/v4" ]