Import (nested) files and folders to compose a Node.js tree of values (asynchronously or not). No dependencies at all. Less than 200 lines of code.
npm install recursive-apiImport (nested) files and folders to compose a Node.js tree of values (asynchronously or not).
No dependencies, at all. Less than 200 lines of game-changer code. Most free license (WTFPL or What The Fuck Public License).
The recursive-api module is a handy and free tool to create Node.js APIs from files and folders disposition.
For example, given a file structure like this (presuposing that any *.js is a file):
```
/SuperAPI
/SuperAPI/01.utils
/SuperAPI/01.utils/customMethod.js
/SuperAPI/02.classes
/SuperAPI/02.classes/Initialization
/SuperAPI/02.classes/Initialization/prototype
/SuperAPI/02.classes/Initialization/prototype/index.js
/SuperAPI/02.classes/Initialization/prototype/initState.js
/SuperAPI/02.classes/Initialization/prototype/initAuthentication.js
/SuperAPI/02.classes/Initialization/prototype/initInternationalization.js
/SuperAPI/02.classes/Initialization/prototype/initialize.js
/SuperAPI/02.classes/Initialization/initialize.js
/SuperAPI/03.api
/SuperAPI/03.api/app
/SuperAPI/03.api/app/get/
/SuperAPI/03.api/app/get/authentication.js
/SuperAPI/03.api/app/get/authorization.js
/SuperAPI/03.api/app/get/internationalization.js
/SuperAPI/03.api/app/get/menuItems.js
/SuperAPI/03.api/app/get/statistics.js
/SuperAPI/03.api/app/get/state.js
/SuperAPI/03.api/app/post/menuItem.js
/SuperAPI/03.api/app/post/statistic.js
/SuperAPI/03.api/app/post/state.js
/SuperAPI/03.api/app/post/internationalization.js
/SuperAPI/03.noadd.initialization.js
And importing the root folder:
`js`
const output = await require("recursive-api").importNode("./SuperAPI");
We would get something like this:
``
{
utils: {
customMethod: undefined
},
api: {
get: {
authentication: undefined,
authorization: undefined,
internationalization: undefined,
menuItems: undefined,
statistics: undefined,
state: undefined,
},
post: {
menuItem: undefined,
statistic: undefined,
state: undefined,
internationalization: undefined,
}
}
}
The main point of this library is that any of these files, concerned with the API, can now:
- Extend the structure with new properties, just creating a new file or folder
- Extend the execution (asynchronously or not) at any point, transparently, just creating a new file or folder
We name the files with 3 types of data in the same name of the file:
- Order: the numbers at the begining of the file. They can be separated by dots. These numbers are to give order to the files.
- Attributes: labels that will alter the way the framework loads every file or folder.
- noadd: runs the contents, but it does not attach them to the parent.norun
- : does not run any file nor read any folder from this node.
- Identifier: name of the property of the parent in the tree that this file is representing.
As you can see, we use the names of the files and folder to:
- To put sequential order to the contents of the folder.
- To define what this file or folder is for:
- noadd represents a block of code that is not a property, but that should be executed.norun
- represents files and folders that are not related to recursive-api.noadd
- To set the name of the property that a given file represents in the parent (unless you set ).
- To save time designing
- To develop structures safer and faster and more comfortably.
- To develop better public Node.js APIs
- To save time coding
- To save some repeatitive and reusable part of code
- To save time thinking about how to expose an API
- To save time documenting
- To create better standards of development
- To intuitively allow others to understand a project by an overview
For all this reasons, it has sense to develop this module. The function of require available in Node.js is powerful, but it is not thought to import data recursively (by default), while this module is.
$ npm i -s recursive-api
Having our fancy-module located at ./fancy-module, we can simply:
`js`
require("recursive-api").importNode("./fancy-module").then(fancy => ...);
We can use also async/await:
`js`
const fancy = await require("recursive-api").importNode("./fancy-module");
When one imports a module, there are 2 moments in the execution we can play with:
- Load-time: the moment in which the module is importing nestedly all the properties recursively.Execution-time
- Here, we define all the utilities, classes, etc. our Node.js API contains.
- : the moment after the whole module has been loaded.load-time
- Here, we can call the functions we defined at .
0. Name files and folders following this pattern: [sorter].[attributes].[id]sorter
- You can provide any of: sorter, attributes, identifier.
- : numbers to set an order to items inside a folder.attributes
- : at the moment:noadd
- or norun:noadd
- : the module is imported, but not attached to the parent.norun
- : the module is not imported.id
- : the identifier property of that file in the parent..js
1. Files have to end with to be taken into account.index.js
2. The file defines the first value acquired by the object represented by any folder.index.js
3. When is not defined, a plain object {} becomes the base object of the folder.identifier
4. The files inside a folder represent:
- Properties of the parent node, when the is set in the filename.load-time
- A execution, when the identifier is not set.*.js
5. When a () file exports an object with a recursiveapi property set to true:build
- The file, when (recursively) required, will return what is found under the property of itself, and:build
- When the property holds a function, it is resolved to find out the value of the module.build
- When the property holds an async function, it is asynchronously resolved to find out the value of the module.
- You create a tree of files and folders.
- You play with the filenames using:
- the numbers at the begining, to sort them.
- the noadd and norun attributes, to avoid inclusion or recursion.load-time
- the name, to create properties (in the parent, at ).index.js
- You can play with the files to setup different structures for a given folder.{recursiveapi: true, build: undefined}` modules to load data asynchronously.
- You can play with
Okay, bitch. Do not you see that all my project are DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE? WTFPL, bitch, learn this words.