/hapi-webapi

Implements abstraction for Hapi in TypeScript that makes it similar to ASP.NET WebAPI to implement APIs on Hapi.

Primary LanguageTypeScriptMIT LicenseMIT

hapi-webapi build status

Implements an abstraction for Hapi using TypeScript, that provides a similar pattern to the ASP.NET WebApi framework for implementing Apis on Hapi.

Quickstart

To get started quickly, you can clone the 'hapi-webapi-seed' repository, which includes all files needed to get started with TypeScript and the hapi-webapi.

Getting started

Start by creating a package.json:

npm init

Install hapi-webapi and save it to your package.json dependencies:

npm install hapi-webapi --save

Create a server.ts file with the following contents:

import {WebApp, StartOptions} from 'hapi-webapi/server';
import {Startup} from "./Startup";

var options = new StartOptions();
options.port = 4600;

//To enable CORS set options.cors to true. Default value is false
options.cors = true;

WebApp.Start<Startup>(Startup, options);

Create a startup.ts file with the following contents:

import {IStartup, IAppBuilder, HttpConfiguration} from 'hapi-webapi/server';
import {UsersController} from "./Controllers/UsersController";

const pgk = require("./package.json");

export class Startup implements IStartup {
    Configuration(app: IAppBuilder) {

        var config = new HttpConfiguration();
        app.useWebApi(config);

        config.enableSwagger({ title: 'Directory API', description: pgk.description, version: pgk.version });
        config.enableSwaggerUi({ title: 'API Documentation v' + pgk.version, path: '/docs' });

        // This is different from ASP.NET WebAPI, controllers needs to manually be registered.
        app.controllers.add(UsersController);

        app.useWelcomePage();
        //app.useDirectoryBrowser('./public/', '/files/');
        //app.useStaticFiles('static');
    }
}

Create a controller.ts file with the following contents:

import {ApiController} from 'hapi-webapi/controllers';
import {RoutePrefix, Route, HttpGet, HttpDelete, HttpPut, HttpPost} from 'hapi-webapi/routing';

@RoutePrefix("users")
export class UsersController extends ApiController {
    @Route("{id}")
    @HttpGet()  // Also supports @HttpPut, @HttpPost, @HttpDelete
    getUserById(id: string) {

        return "getUserById:" + id;
    }

    @Route("search")
    @HttpPost() 
    searchUsers(id: string) {
        return this.notFound();
    }

    @Route("list")
    @HttpGet()
    list() {
        // Examples of request object values available:
        // Url object.
        console.log(this.request.requestUri);

        // HTTP version.
        console.log('HTTP Version: ' + this.request.version);

        // HTTP headers.
        console.log(this.request.headers);

        return "Hello World!";
    }
}

Launch the application by running:

npm start

And open localhost:4600 or localhost:4600/docs for Swagger UI in your browser.

Contributors

sondreb kommundsen SteffenVetrhus
sondreb kommundsen SteffenVetrhus

Change Log

View the change log to keep up-to-date on API and project changes.

License

MIT © Sondre Bjellås