diff --git a/types/hapi/v17/test/getting-started/02-adding-routes.ts b/types/hapi/v17/test/getting-started/02-adding-routes.ts new file mode 100644 index 0000000000..be46f20efc --- /dev/null +++ b/types/hapi/v17/test/getting-started/02-adding-routes.ts @@ -0,0 +1,41 @@ +// from https://hapijs.com/tutorials/getting-started#adding-routes +import {Request, ResponseToolkit, Server, ServerOptions, ServerRoute} from "hapi"; + +const options: ServerOptions = { + port: 8000, +}; +const server = new Server(options); + +const serverRoute: ServerRoute = { + path: '/user', + method: 'GET', + handler: function (request: Request, h: ResponseToolkit) { + console.log(request); + return 'ok: ' + request.path; + } +} + +const serverRoutes: ServerRoute[] = [ + { + path: '/test1', + method: 'GET', + handler: function (request: Request, h: ResponseToolkit) { + console.log(request); + return 'ok: ' + request.path; + } + }, + { + path: '/test2', + method: 'GET', + handler: function (request: Request, h: ResponseToolkit) { + console.log(request); + return 'ok: ' + request.path; + } + }, +]; + +server.route(serverRoute); +server.route(serverRoutes); + +server.start(); +console.log(`Server running at: ${server.info!.uri}`); diff --git a/types/hapi/v17/test/index.test.d.ts b/types/hapi/v17/test/index.test.d.ts index ce156ebbdb..89ddbd9ece 100644 --- a/types/hapi/v17/test/index.test.d.ts +++ b/types/hapi/v17/test/index.test.d.ts @@ -10,3 +10,10 @@ /** Getting Started */ export * from './getting-started/01-creating-a-server'; +export * from './getting-started/02-adding-routes'; +export * from './path/catch-all'; +export * from './server/app'; +export * from './server/info'; +export * from './server/table'; + + diff --git a/types/hapi/v17/test/path/catch-all.ts b/types/hapi/v17/test/path/catch-all.ts new file mode 100644 index 0000000000..87ddfec8be --- /dev/null +++ b/types/hapi/v17/test/path/catch-all.ts @@ -0,0 +1,15 @@ +// From https://github.com/hapijs/hapi/blob/master/API.md#catch-all-route +import {Request, ResponseToolkit, Server, ServerOptions, ServerRoute} from "hapi"; + +const options: ServerOptions = { + port: 8000, +}; +const server = new Server(options); + +const handler = function (request: Request, h: ResponseToolkit) { + return h.response('The page was not found').code(404); +}; +server.route({ method: '*', path: '/{p*}', handler }); + +server.start(); +console.log(`Server running at: ${server.info!.uri}`); diff --git a/types/hapi/v17/test/server/app.ts b/types/hapi/v17/test/server/app.ts new file mode 100644 index 0000000000..ea376560b7 --- /dev/null +++ b/types/hapi/v17/test/server/app.ts @@ -0,0 +1,22 @@ +// From https://github.com/hapijs/hapi/blob/master/API.md#-serverapp +import {Request, ResponseToolkit, Server, ServerOptions, ServerRoute} from "hapi"; + +const options: ServerOptions = { + port: 8000, +}; + +const server = new Server(options); +server.app.key = 'value2'; + +const serverRoute: ServerRoute = { + path: '/', + method: 'GET', + handler: function (request: Request, h: ResponseToolkit) { + return 'key: ' + request.server.app.key; + } +} + +server.route(serverRoute); + +server.start(); +console.log(`Server running at: ${server.info!.uri}`); diff --git a/types/hapi/v17/test/server/info.ts b/types/hapi/v17/test/server/info.ts new file mode 100644 index 0000000000..522b234c75 --- /dev/null +++ b/types/hapi/v17/test/server/info.ts @@ -0,0 +1,15 @@ +// From https://github.com/hapijs/hapi/blob/master/API.md#-serverinfo +import {Request, ResponseToolkit, Server, ServerOptions, ServerRoute} from "hapi"; +import assert = require('assert'); + +let options: ServerOptions = { + port: 8000, +}; +const server = new Server(options); + +// check the correct port +if(server.info) assert(server.info.port === 8000); +assert(server.info !== null); + +server.start(); +console.log(`Server running at: ${server.info!.uri}`); diff --git a/types/hapi/v17/test/server/table.ts b/types/hapi/v17/test/server/table.ts new file mode 100644 index 0000000000..f6dee501c7 --- /dev/null +++ b/types/hapi/v17/test/server/table.ts @@ -0,0 +1,22 @@ +// From https://github.com/hapijs/hapi/blob/master/API.md#-servertablehost +import {Request, ResponseToolkit, Server, ServerOptions, ServerRoute} from "hapi"; + +const options: ServerOptions = { + port: 8000, +}; + +const server = new Server(options); +server.app.key = 'value2'; + +server.route({ + path: '/', + method: 'GET', + handler: function (request: Request, h: ResponseToolkit) { + return h.response("Hello World"); + } +}); + +server.start(); +const table = server.table(); +console.log(table); +console.log(`Server running at: ${server.info!.uri}`);