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

wegweiser

v4.2.0

Published

A router for Quinn

Downloads

32

Readme

Wegweiser

Build Status

Wegweiser {m}, Wegweiser {pl}:

  • signpost; direction sign; guide sign [Am.]
  • (figurative) guide [e.g. a book]
  • a router for Quinn, powered by routington and footnote.

Install

npm install --save wegweiser

Example

import { createRouter, GET, PUT } from 'wegweiser';

const simpleHandler = GET('/my/scope')(req => {
  return respond().body('ok');
});

class PretendingItsJava {
  @PUT('/user/:username/profile')
  async updateProfile(req, { username }) {
    const data = await readJson(req);
    return respond.json({ ok: true, firstName: data.firstName });
  }
}

const objectOrInstance {
  @GET('/object/:op')
  getObject(req, { op }) { return respond.json({ op }); }
}

const router = // router is a quinn handler: request => response
  createRouter(simpleHandler, PretendingItsJava, objectOrInstance);

For decorators, you'll need babel with --stage 1.

API

createRouter(...routes)

This is also the default export.

Each argument to this function should be an annotated function, a class with annotated methods, or an object with annoated methods. Both for objects and classes the prototype chain will be scanned. Routes have to be unambiguous. If the same method and path combination is configured twice, creating the router will fail.

The result is a router; a function of form request => result.

The request is expected to have two properties:

  • method: String: An HTTP method, e.g. 'GET'.
  • url: String: A url path, e.g. '/foo?a=b'.

If one of the routes matches, the router will call the route handler with two arguments: the request (just passed through) and params. params is an object containing the path parameters.

Example
GET('/users/:id')(f);
createRouter(f)({ method: 'GET', 'url': '/users/robin' });
// Calls `f` with `(request, { id: 'robin' })`.

The return value of the router is whatever the route handler returns, no changes or assumptions are made by wegweiser itself. If no route matches, the router will return undefined.

router.resolve(req)

Can be used to retrieve the function that would be called for a given request. It returns either undefined (if no route matches the request) or an object with the following properties:

  • params: The route parameters.
  • handler: The request handler, ready to be called with req and params.

For class method routes, the handler will have two properties itself:

  • ctor: The constructor of the class.
  • key: The property key of the method to call on the instance.
Example
class MyResource {
  constructor(db) { this.db = db; }

  @GET('/my/:id')
  getById(req, {id}) { return this.db.getById(id); }
}
const router = createRouter(MyResource);

const req = { method: 'GET', url: '/my/foo' };
const {params, handler} = router.resolve(req);
const instance = someDIContainer.construct(handler.ctor);
return instance[handler.key](req, params);

Route(method: String, path: String)

A footnote annotation decorator that adds metadata to functions or methods to turn them into valid arguments for createRouter.

The return value is a decorator function that supports two different call styles:

  • Route('GET', '/')(f: Function): Annotates a function.
  • Route('GET', '/')(prototype, propertyKey, propertyDescriptor): Annotates a method.

When a method is annotated, the constructor of the class is tracked. Whenever the route is matched, a new instance of the class will be created and the method will be called on that instance. The arguments the router passes into functions and methods are the same (see `createRouter above).

GET(path: String) / POST(path) / ...

Route partially applied with an HTTP verb. Unless you need to support some very exotic HTTP verbs, you'll use these instead of Route directly.