npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hapi-webapi

v0.0.5

Published

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

Downloads

10

Readme

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 |

Change Log

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

License

MIT © Sondre Bjellås