copyit
This module allows you to declare how data will move into and out of your middleware.
Installation
npm install copyit
Usage
Refactoring Hard-coded Assumptions
Let's imagine that we're writing a piece of middleware called getUser
. This middleware fetches a User
record from the database before passing control to the next piece of middleware. Let's start with something simple:
exports { var id = reqparamsid; ;}
Most of the time, this implementation is fine. But, there are a couple of problems:
- It always reads the
id
fromreq.params.id
. - It always stores the
User
instance toreq.user
.
If your controller needs to break either of these assumptions, then it cannot use the getUser()
middleware.
We can fix this by allowing the developers to declare the dataflow behaviors when the middleware is initialized. For example:
var copyit = ;var from = copyitfrom to = copyitto; router
The from
and to
functions return new functions that get and set values, respectively. These getter and setter functions are then passed into the middleware to manage the dataflow. Therefore, the middleware can be rewritten as follows:
var copyit = ;var from = copyitfrom to = copyitto; exports { options = options || {} // Use the getter / setter functions that were provided // by the developer, or fallback to the default behaviors. var getId = reqinput || ; var setUser = reqoutput || ; return { var id = ; // Use the getter to obtain the id var user = ; ; // Use the setter to store the User ; }}
Ta-da! Now your middleware's dataflow assumptions are no longer hard-coded! You can rely on the default behaviors most of the time, but you also have the option to override them on an as-needed basis.
Simplified Declaration
Of course, it's a little inconvenient to require the developers to use the copyit
library just for minor adjustments to the behaviors. Wouldn't it be nice if they could just do something like this instead?
router
Well, today is your lucky day -- that actually is possible! We just need to modify our middleware so that it automatically coerces the developer's input into getter and setter functions:
var copyit = ;var from = copyitfrom to = copyitto; exports { options = options || {} // If the developer provided values, then coerce them into // getter / setter functions. Otherwise, return undefined. var getId = from var setUser = to; // If no behavior overrides were provided, then fallback to // the default behaviors. getId = getId || ; setUser = setUser || return { var id = ; // Use the getter to obtain the id var user = ; ; // Use the setter to store the User ; }}
Notice that we specified {prefix: "params"}
when invoking from.coerce
. The influences the way that from.coerce
will convert Strings and Arrays into getter / setter functions. In this particular case, it will append "params"
to the front of every lookup path. In other words:
// This:; // Is equivalent to this:;
What if a developer needs to obtain the user's id from req.body
instead of req.params
? Simple -- they just need to specify the full path by calling from(...)
:
;
And that, as they say, is that!