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

loader-mw

v1.0.0

Published

Middleware express pour précharger le tenant ou autre objet. Utilise req-custom et est compatible avec security-mw.

Downloads

4

Readme

Load a ressource

This middleware allows to load a resource based on the parameter id provided. This middleware is designed to be used with app.param or router.param. The request object can use custom parameters (see req-custom) to store the loaded resource and avoid collision with existing keys. If the request has not custom parameter, the resource will be added directly to the request object.

Factory parameters

The package provides a factory to create the middleware, the parameters used are:

  • accessorFn : (id, name, req) => Promise(value)|value the accessor function, gets the id (the value of the parameter), the name of the parameter and the request object. It should provide a Promise of the loaded ressource, or directly the resource. If an Error happens, it should throw an Error or reject the Promise.
  • name : The name of the key used to store the loaded value. If undefined the name of the parameter is used.
  • raiseErr :
    • if true, the field added to the request objet is directly the value, if the accessorFn fails (throw Error, or promise rejection) the error is propagated to the next middleware
    • if false (default), the field added to the request objet is an object which encapsulates the value and the Error, like this { value : value|null, err: null|Error }. The next middleware is call normally.

Use cases

First example

A typical usage is to use this middleware to retrieve the tenant, then the tenant can be used by the access control middleware or the controller to process the request.

Just the accessor function:

const tenants = require('./models/tenants');
const tenantLoader = require('pre-loader-mw');

// the resource loader
const accessorFn = (id) => tenants.get(id);
app.param('tenant', tenantLoader(accessorFn));

// Do something with the tenant for the access control
app.use('/:tenant', function(req, res, next) {
  // the key of the added field is the name of the parameter
  const err = req.tenant.err;
  if (err) {
    // Do something with the Error
    // here req.tenant.value === null
  }
  else {
    // Do something with the tenant
    // here req.tenant.err === null
    const tenant = req.tenant.value;
  }
});

Other example

With a parameter name, error raised and custom parameter middleware:

const rsrcs = require('./models/rsrc');
const rsrcLoader = require('pre-loader-mw');

// To add custom parameter to the requests
app.use(require('custom-params'));

// the resource loader
const accessorFn = (id, name, req) => rsrcs.get(id);
app.param(['image', 'video'], rsrcLoader(accessorFn, 'params', true));

// Do something with the resource ':image' or ':video'
app.get(['images/:image', 'video/:video'], function(req, res, next) {
  // The resource is here, and the 'params' name doesn't collide with the req.params
  const rsrc = req.getPrm('params');
});
app.use(function(err, req, res, next) {
  // Error will be handle here
});