0.3.0 • Published 2 years ago

hapi-filesystem-router v0.3.0

Weekly downloads
9
License
MIT
Repository
github
Last release
2 years ago

hapi-filesystem-router

Filesystem based routing for Hapi.

Package is usable but new. It makes an effort to follow semantic versioning witch means backward incompatible changes will be introduced only in major releases. But removing "^" from all your production dependencies is a good practice anyway.

It allows you to add routes based on directory structure.

This package is made for those who have relatively deeply nested routing on their sites and want stricter project structure.

Simple example

Following directories structure will add 3 routes.

import { attachRoutes } from 'hapi-filesystem-router';
// ...
const server = Hapi.server({});

await attachRoutes(server);

await server.start();
// ...

Results in

methodpath
GET/
GET/item/{id}
POST/item/{id}

More complex example

You can have multiple roots and specify different default configurations for them. As example you can use it for separating routes that require authorization/authentication.

import { attachRoutes } from 'hapi-filesystem-router';
// ...
const server = Hapi.server({});

await attachRoutes(server, 'routes/public');
await attachRoutes(server, 
  'routes/private', // path to the root of routes 
  'protected', // prefix that will be added to actual route (see results)
  {
    options: {
      auth: {
        scope: ['admin'],
        strategy: 'session',
      },
    },
  });

await server.start();
// ...

Results in

methodpathauthscope
GET/nonenone
GET/item/{id}nonenone
POST/item/{id}nonenone
GET/protected/item/{id}sessionadmin
POST/protected/item/{id}sessionadmin

Handler

File with handler should be named as any http method except for 'head' and export default hapi route object.

// routes/item/{id}/post.ts 
import Joi from 'joi';
import { FSRM } from 'hapi-filesystem-router';

const route: FSRM<{
  payload: { title: string };
  params: { id: string };
}> = {
  options: {
    validate: {
      payload: Joi.object({
        title: Joi.string().length(255),
      }),
      params: Joi.object({
        id: Joi.string(),
      }),
    },
  },
  handler: async (request, h) => {
    const { title } = request.payload;
    const { id } = request.params;
    return {
      id,
      title,
      updatedAt: new Date().toISOString(),
    };
  },
};

export default route;

Typescript

Package is written in Typescript, so types are included.

Node.js

Package is tested on Node v14+ (LTS).

Hapi

Package is tested on Hapi v20+.

0.3.0

2 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.1

4 years ago