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

akaya

v5.2.2

Published

Generate URIs based on named hapi routes and their parameters

Downloads

565

Readme

akaya

akaya

Generate URIs based on named hapi routes and their parameters

Travis node npm standard npm

  1. Introduction
  2. Installation
  3. Usage
  4. API
  5. Example
  6. Testing
  7. Contribution

Introduction

This hapi plugin enables to generate URIs dynamically based on the config.id of a route and passed parameters. It supports mandatory, multiple and optionals parameters as well as wildcards. Because it is not necessary to hardcode the URIs, it supersedes further adjustments in the case of refactoring.

This plugin is based on a hapi-to fork but it is about 30x faster. The modules standard and tape are used to grant a high quality implementation.

Compatibility

| Major Release | hapi.js version | node version | | --- | --- | --- | | v5 | >=18.4 @hapi/hapi | >=12 | | v4.1 | >=18.3.1 @hapi/hapi | >=8 | | v4 | >=18 hapi | >=8 | | v3 | >=17 hapi | >=8 | | v2 | >=13 hapi | >=6 |

Installation

For installation use the Node Package Manager:

$ npm install --save akaya

or clone the repository:

$ git clone https://github.com/felixheck/akaya

Usage

Change from hapi-to to akaya

If you want to change from hapi-to to akaya for performance reasons, just replace the require and use request.aka instead of request.to. Because the configuration is almost the same, the migration is seamless.

It just differs in the configuration of options.secure. The value "match" is not available in akaya. The plugin matches the current request's connections protocol automatically as default.

Additionally parts of the functionality are exposed as server method.

Import

First you have to import the module:

const akaya = require('akaya');

Create hapi server

Afterwards create your hapi server if not already done:

const hapi = require('@hapi/hapi');
const server = hapi.server({
  port: 1337,
  host: 'localhost',
});

Registration

Finally register the plugin per server.register():

(async () => {
  await server.register(akaya);
  server.start();
})();

After registering akaya, the hapi request object and the hapi server object will be decorated with the new methods request.aka() and server.aka().

API

server.aka(id, [params], [options])

Returns an relative URI to a route

  • id {string} - required routes config.id.
  • params
    • query {Object.<?string>} - Necessary query parameters, which will be stringified.
    • params {Object.<?string>} - Necessary path parameters.
  • options
    • router {call} - Set a custom Call router

request.aka(id, [params], [options])

Returns an URI to a route

  • id {string} - see above
  • params – see above
  • options
    • router {call} - Set a custom Call router
    • rel {boolean} - Whether to generate a relative URL. Default: false.
    • secure {boolean} - If true the URL will be https, if false will be http. Default: match the x-forwarded-proto header or the current request's connection protocol.
    • host {string} - Sets the host in the URL. Default: match the current request.

Example

const hapi = require('hapi');
const akaya = require('akaya');

const server = hapi.server({ port: 1337 });

server.route([{
    method: 'GET',
    path: '/',
    handler (request, h) {
        const url = request.aka('foo', {
          params: { object: 'world' },
          query: { page: '1' }
        });

        return h.redirect(url);
    }
}, {
    method: 'GET',
    path: '/multi',
    handler (request, h) {
        const url = request.aka('bar', {
          params: { multi: [42, is, sense, of life] }
        });

        return h.redirect(url);
    }
}, {
    method: 'GET',
    path: '/hello/{object}',
    config: {
        id: 'foo',
        handler (request) {
          return 'No more redirects.';
        }
    }
}, {
    method: 'GET',
    path: '/{multi*5}',
    config: {
        id: 'bar',
        handler (request) {
          return 'No more redirects.';
        }
    }
}]);

(async () => {
  try {
    await server.register(akaya);
    await server.start();
    console.log('Server started successfully');
  } catch (err) {
    console.error(err);
  }
})();

The example above make use of redirects and akaya:

The route http://localhost:1337/ will be redirected to http://localhost:1337/hello/world?page=1. And the route http://localhost:1337/multi will be redirected to http://localhost:1337/42/is/sense/of/life.

Testing

First you have to install all dependencies:

$ npm install

To execute all unit tests once, use:

$ npm test

or to run tests based on file watcher, use:

$ npm start

To get information about the test coverage, use:

$ npm run coverage

Contribution

Fork this repository and push in your ideas.

Do not forget to add corresponding tests to keep up 100% test coverage.

In case of questions or suggestions just open an issue.