Skip to content

junaid1460/hapiest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

80 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

[Hapiest] Beautiful code is what I seek.

NPM version

https://medium.com/@junaid1460/hapiest-7f3a184604b7 read use cases here.

npm i hapiest@latest

Requirements

hapi 17+ typescript 3 (es6)

Usage

import { Server } from "hapi";
import { Hapiest } from "hapiest";

@Hapiest.Routes({ baseUrl: "api", auth: false })
class AdminRoutes extends Hapiest.HapiestRoutes {
    @Hapiest.get({ path: "" }) // Path:  /api
    public api(args: Hapiest.HapiestParams) {
        return "base\n";
    }

    @Hapiest.get() // Path: /api/getTest
    public getTest({
        request
    }: Hapiest.HapiestParams<Hapiest.HapiestRequest<{ username: string }>>) {
        return `hey, what's up? I know type of payload ${request.payload.username}\n`;
    }

    @Hapiest.get({ path: "name" }) // Path: /api/name
    public async getit(args: Hapiest.HapiestParams) {
        return "junaid\n";
    }
}

class MyFirstHapiestModule extends Hapiest.HapiestModule {
    public routeSets = [AdminRoutes];
    public baseUrl = "dev";
    public auth = "simple"; // Make sure that you registered auth plugin
}

class ModuleWithNoAuth extends Hapiest.HapiestModule {
    public routeSets = [AdminRoutes];
    public baseUrl = "test";
    public auth: false = false;
}

const hapiServer = new Server({
    host: "0",
    port: 8000,
    routes: { cors: true }
});

async function start() {
    await hapiServer.register({
        plugin: require("hapi-auth-basic")
    });
    hapiServer.auth.strategy("simple", "basic", {
        validate: () => true
    });
    hapiServer.route(new MyFirstHapiestModule().getRoutes());
    hapiServer.route(new ModuleWithNoAuth().getRoutes());

    await hapiServer.start().then(e => {
        console.log("server started");
        const routesText = hapiServer
            .table()
            .map(route => {
                return `${route.method}: (${JSON.stringify(
                    route.settings.auth
                ) || "No auth"}) ${route.path}`;
            })
            .join("\n");
        console.log(routesText);
        // server started
        // get: ({"strategies":["simple"],"mode":"required"}) /dev/api/api
        // get: ({"strategies":["simple"],"mode":"required"}) /dev/api/getTest
        // get: ({"strategies":["simple"],"mode":"required"}) /dev/api/name
        // get: (false) /test/api/api
        // get: (false) /test/api/getTest
        // get: (false) /test/api/name
    });
}

start();

About

Neat code with decorators for Hapi 17 +.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published