@usetada/octopus
v2.1.0
Published
Various of useful shared libraries across TADA nodejs projects
Downloads
26
Readme
OCTOPUS
This project will act as a shared library across TADA projects. This project will not deployed independently on production environment, instead will be available and used as node_module
which can be installed via npm
command.
How to get involved?
Because this module may attached to any project, we should ensure any configuration to stay on given config, so try to not put a hard-coded configuration in here, because it may irrelevant to another project and/or module.
Before you started please read the rules below;
- Re-organize your module: When you literally create a brand new module like it's majority different and looks like nothing matched with existing, please create a new module that matched with your criteria. For example if you need to create a helper for phone number normalization and you can't found any existing module to where it store, you can create a
common
module folder on the root path to store that function.
rootDir
|
|_ modules
| |_ pigeon
| | |_ sms
| | |_ send_sms.js
| | |_ send_sms_bulk.js
| | |_ index.js
| |
| |_ common
| | |_ phone_normalizer.js
| | |_ index.js
| |
| |_ nsq
| |_ publish.js
| |_ subscribe.js
| |_ index.js
|
|_ libs
- Think before you write: Before you write and push anything to this repository, please read your code again carefully to inspect any sensitive information left on the codebase.
- Should be general: When you write a module, it has to be usable and can be repeatable used on other projects. The module that just used once in one project and has a specific case which will not available on another project should not be in here.
- Use ES7 rules: We love the code that well organized, so keep it up.
- Write a Test: If possible please write the test within module folder, you can see the sample on
common
orpigeon
module - Follow ESLint validation: Always follow the eslint rules
- Use camelCase when writing a function and variables
- Use snake_case when writing a file name and module name
How to Start Develop
This module will working as node_modules
you can start installing it by command npm i github:aksivisitama/octopus#v2.0.0
, the last fragment stand for the version of this module. Currently the best practice for doing development is using npm link, here is the step-by-step implementation;
Start linking
First you need to create linking on your local machine by using npm link
to the project.
// clone this repository
>> git clone [email protected]:aksivisitama/octopus.git
// go inside the repo
>> cd octopus
// Installing dependencies
>> npm i
// creates global link
>> npm link
// use octopus on your project & install from npm link
>> cd ~/some/path/to/your-project
>> npm link @usetada/octopus
Example Usage
Once you finished the step above, you already setup for start developing the features. After making some changes on octopus, to test or use on the main project; This is a simple example of creating pigeon module.
Create new module
Create new file or folder under modules
directory with name pigeon
. This folder will be available under octopus package as Pigeon
because module name will be converted title case including underscore (ex: pigeon_notification
will be PigeonNotification
). Also every folder or file inside the root of modules
directory will be autoloaded.
Create index.js file
Put index.js
file on root folder, this file must return object response which will be available later. So the module will look like this;
|_ modules
| |_ pigeon
|_ index.js
and the code will be similar like this;
// <base>/modules/pigeon/index.js
module.exports = config => ({
sendSMS: config => (sender, vendor, payload) => {
// maybe accessing config here
// sending SMS
}
})
Example Use on Project
Finally we can use Pigeon
module on every project like this.
// sms.js
const octopus = require('@usetada/octopus')
module.exports = (req, res) => {
const { phone, message } = req.body
const { Pigeon, Common, } = octopus({ someConfigParameter: true, })
Pigeon.sendSMS({
sender: 'TADA',
vendor: 'INFOBIP',
payload: {
phone: Common.phoneNormalizer(phone),
message: message
}
})
res.send('SMS Sent')
}
Custom Module Name
There are option to create custom name for the module created. Export property moduleName
from the index file of that module.
For example;
// <base>/modules/NSQ/index.js
module.exports = config => ({
Publish: config => (topic, message) => {
// maybe accessing config here
// publish NSQ message
}
})
module.exports.moduleName = 'NSQ' // the module name will be "NSQ"
Project Structure
- modules, directory for storing the modules. Subfolder inside this directory will be autoloaded and available under octopus module package. Any module will be converted into title case.
- libs, shared library for internal use of octopus module
Documenting the Module Function
After creating some functions on module please update or create the following documentation on API.md
. This ensure any updates or creation inside this project will easily known by others, also preventing us to re-create the same code/function in the future.
Running Test
This project using ava
library for testing, simply run by executing command below to run the test. Ava will find all of your test files where its ended with .test.js
. For more information about Ava see the documentation here
>> npm run test
> @usetada/[email protected] test /home/aditya/GCI/dev/octopus
> ava
``