1.2.1 • Published 7 years ago
@firecubez/req v1.2.1
Req
A package manager to eliminate the need for recursive dependencies, and allow for flexibility with dependencies
Installation
$ npm install @firecubez/reqUsage
const Req = require("@firecubez/req");
Req.module("hello", null, $$ => {
return {string: "Hello, world!\n"};
})
Req.module("helloname", $$ =>
$$.depend(
"hello",
"a module which provides 'Hello, world!'"
),
$$ => {
const hello = $$.get("hello", {});
return {
withname: name => hello.string + name
}
})
const main = Req.module("main", $$ =>
$$.depend(
"helloname",
"a module which puts names next to 'Hello, world!'",
{
withname: f => typeof f === "function"
}
),
$$ => {
// ...
}
)Here's where it gets interesting. For the function:
$$ => {
const helloname = $$.get("helloname", {})
helloname.withname("Req")
}
// ...
main()
// > Hello, world!
// ReqAs expected, but:
$$ => {
const helloname = $$.get("helloname", {
hello: {
string: "Hey there "
}
})
helloname.withname("Req")
}
// ...
main()
// > Hey there ReqWhat happened?
We have overridden the hello dependency of helloname in the second example with a "mock" dependency. This can be useful when creating tests.
API
Req
The Req object exposes 4 methods:
get(name): Get a module by its name using the following procedure: - Check the internal module storage for the module. - If not found, check if the module exists using Node'srequire. Returns: The found module Throws: If the module isn't found.module(name, definerFunc, runner): Create a named module and save it in the internal module storage. Parameters: -name: The module name -definerFunc: A function which takes aDefinerand calls it'sdependmethod. -runner: A function which takes aGiverand runs the module code, returning the exports Returns: The newly created moduleanonymousModule(definerFunc, runner): Same asmodule, but does not save in internal storage since no name is provideddeprecate(mod, func[, repl]): Deprecate a module Parameters: -mod: The module to deprecate -func: A function which is called when it is attempted to use the deprecated method. It should return a warning message to be printed by Req (configurable by settingReq.warnfunction) -repl: The module to use instead of this module (can be a name or the module itself)
Definer
A definer object is passed to Req.module / req.anonymousModule for modules to declare dependencies
depend(name[, desc[, interface[, def]]]): Declares a dependency. Parameters: -name: The dependency name to be referenced inGiver.get-desc: A description method for detail in error messages -interface: Asserts, atGiver.gettime, that the dependency exposes certain properties. For each property ininterface: + It must be a function + If the dependency doesn't expose a property of the same name, an error is thrown + The function is run on the dependency's property of the same name. If it returns false, an error is thrown -def: A fallback dependency to be used if a dependency of the specified name isn't found. The dependency is still accessible withGiver.getunder the same name as specified in the first argument. Returns:this(for chaining)
Giver
A giver object is passed to Req.module / req.anonymousModule for modules to use their dependencies
get(name): Get a dependency module using the following procedure: - Check if the modulegeting this module overwrote the specified name - If not, check the current module's dependencies - If not there, check the dependencies for the default module name specified inDefiner.depend- If not there, check the internal module storage for the specified and default name - If not there, check with Node'srequirefor both the specified and default name - If not there, throw an error Returns: The actual module with the specified name (NOT the object it returns). Will always be a function with__Is_Req_Module__ === trueThrows: If the dependency doesn't existget(name, impl): Get a dependency's exposed properties / return value Returns: The dependency's return value withimplcontaining overridden dependencies. Throws: If the dependency with the specified name doesn't exist orimpldoes not pass the tests described inDefiner.dependAlias for:get(name)(impl)
Module
A module is a function with the __Is_Req_Module__ property set to true.
A module exposes the following properties:
internal __Is_Req_Module__:true. Can be used to detect whether a function is a module or notinternal __Req_Deprecate__: A function that is called when this module is used. Most of the time you don't want to touch thisinternal __Req_Deprecate_Target__: The new version of this module that isn't deprecated. You also don't want to touch thismoduleName: The name of the module,nullif it is an anonymous module