@hstech/utils
v1.2.1
Published
> This package provides some usefull variables and functions that are always used and reused in any API
Downloads
6
Readme
@hstech/utils
This package provides some usefull variables and functions that are always used and reused in any API
Install
yarn add @hstech/utils
npm i -S @hstech/utils
Usage
Logger
import { logger } from '@hstech/utils';
logger.info('Foo');
// [2020-05-06 05:20:51.685 +0000] INFO : Foo
logger.info('Foo');
// [2020-05-06 05:20:51.685 +0000] DEBUG : Foo
predefined variables
import { isProduction, currentProtocol, apiPort } from '@hstech/utils';
// Check if the variable NODE_ENV equals to production or prod
console.log(isProduction); // Boolean => true/false
// if isProduction is true, it return https otherwise http
console.log(currentProtocol); // String => 'http'/'https'
// Check those env variables API_LISTEN_PORT || VUE_APP_API_PORT || API_PORT otherwise it return 8080
console.log(apiPort); // Number => 8080
buildUrl
You need to provide at least the hostname. The protocol use the variable currentProtocol
import { buildUrl, apiPort } from '@hstech/utils';
// Create the apiUrl
const apiUrl = buildUrl({
hostname: process.env.API_HOST, // localhost
extraArgs: {
port: apiPort.toString()
}
});
console.log(apiUrl); // String => http://localhost:8080
// More complicated example
const dbUrl = buildUrl({
protocol: process.env.DB_PROTOCOL || 'mongodb',
hostname: process.env.DB_HOST, // localhost
extraArgs: {
pathname: process.env.DB_NAME, // db-name
port: process.env.DB_PORT, // 27017
username: process.env.DB_USER, // user
password: process.env.DB_PASSWORD, // password
query: JSON.parse(process.env.DB_QUERY || '{}') // {hello: 'world}
}
});
console.log(dbUrl); // String => http://user:password@localhost:27017/db-name?hello=world
Predefined url variables
import { apiUrl, webappUrl, dbUrl, s3Url } from '@hstech/utils';
// Use this env variables API_HOST, and the apiPort predefined variable
console.log(apiUrl);
// Use those env variables WEBAPP_HOST, WEBAPP_PORT
console.log(webappUrl);
// Check the second example in buildUrl
console.log(dbUrl);
// Use those env variables S3_HOST, S3_PORT
console.log(s3Url);
DB connect
import { db } from '@hstech/utils';
// it uses the predefined url dbUrl
db()
.then(() => {
console.log('You are now connected to the DB');
})
.catch((err) => {
throw err;
});