{"id":23821002,"url":"https://github.com/truesparrowsystems/base","last_synced_at":"2026-03-03T15:34:01.897Z","repository":{"id":37826475,"uuid":"495727362","full_name":"TrueSparrowSystems/base","owner":"TrueSparrowSystems","description":"Base provides advanced Promise Queue Manager, Custom Console Logger and other utilities.","archived":false,"fork":false,"pushed_at":"2023-01-30T11:50:18.000Z","size":41,"stargazers_count":14,"open_issues_count":0,"forks_count":0,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-08-31T20:24:01.240Z","etag":null,"topics":["custom-logger","promise-queue-manager"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TrueSparrowSystems.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-05-24T08:06:48.000Z","updated_at":"2023-05-03T05:15:44.000Z","dependencies_parsed_at":"2023-02-16T07:01:41.299Z","dependency_job_id":null,"html_url":"https://github.com/TrueSparrowSystems/base","commit_stats":null,"previous_names":["plg-works/base"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/TrueSparrowSystems/base","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TrueSparrowSystems%2Fbase","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TrueSparrowSystems%2Fbase/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TrueSparrowSystems%2Fbase/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TrueSparrowSystems%2Fbase/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TrueSparrowSystems","download_url":"https://codeload.github.com/TrueSparrowSystems/base/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TrueSparrowSystems%2Fbase/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273992355,"owners_count":25203726,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-07T02:00:09.463Z","response_time":67,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["custom-logger","promise-queue-manager"],"created_at":"2025-01-02T08:19:40.953Z","updated_at":"2026-03-03T15:34:01.794Z","avatar_url":"https://github.com/TrueSparrowSystems.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Base\n![npm version](https://img.shields.io/npm/v/@truesparrow/base.svg?style=flat)\nBase provides frequently used functionality like cutome logger, response helper, Custom Promise and Instance composer. \nThese are used in almost all True Sparrow repositories and hence the name `Base`.\n\n## Installation\n```shell script\nnpm install @truesparrow/base --save\n```\n\n## Logger\nLogging is a basic functionality which is needed in all the applications. Custom logger helps in standardizing the logging \nand thus help in debugging and analysis. To log different levels of information, we use different colors, which can be \nvisually distinguished easily.\n\nIn the following snippet, we try to showcase all the logger methods with documentation in comments.\n\n```js\nconst Base = require('@truesparrow/base');\nconst Logger  = Base.Logger;\n\n// Constructor's first parameter is the module name. This is logged in every line to separate logs from multiple modules.\n// Constructor's second parameter is the log level. Depending on log level, some methods will work and others will not do anything.\nconst logger  = new Logger(\"\u003cmodule name\u003e\", Logger.LOG_LEVELS.TRACE);\n\n//Log Level FATAL \nlogger.notify(\"notify called\");\n\n//Log Level ERROR\nlogger.error(\"error called\");\n\n//Log Level WARN\nlogger.warn(\"warn called\");\n\n//Log Level INFO\nlogger.info(\"info Invoked\");\nlogger.step(\"step Invoked\");\nlogger.win(\"win called\");\n\n//Log Level DEBUG\nlogger.log(\"log called\");\nlogger.debug(\"debug called\");\nlogger.dir({ l1: { l2 : { l3Val: \"val3\", l3: { l4Val: { val: \"val\"  }}} }});\n\n//Log Level TRACE\nlogger.trace(\"trace called\");\n```\n\nAll methods will be available for use irrespective of configured log level.\nLog Level only controls what needs to be logged.\n\n### Method to Log Level Map\n| Method | Enabling  |\n|        | Log Level |\n| :----- | :-------- |\n| notify | FATAL     |\n| error  | ERROR     |\n| warn   | WARN      |\n| info   | INFO      |\n| step   | INFO      |\n| win    | INFO      |\n| debug  | DEBUG     |\n| log    | DEBUG     |\n| dir    | DEBUG     |\n| trace  | TRACE     |\n\n## Response formatter\nResponse formatter helps to maintain standard format of the response given out by various libraries and services.\n\n```js\n// rootPrefix is the path to the root of this package. Give proper path if installed inside node_modules\nconst rootPrefix = '.';\n\n// paramErrorConfig is an object with error identifiers as key and value is an object with keys parameter and message.\n// parameter is the name of the parameter which had error, for example invalid value, missing value, etc.\nconst paramErrorConfig = require(rootPrefix + '/tests/mocha/lib/formatter/paramErrorConfig');\n\n// apiErrorConfig is an object with error identifiers as key and value is an object with keys http_code, code and message.\n// http_code goes as the api http code while rendering.\n// message and code goes in the error object.\nconst apiErrorConfig = require(rootPrefix + '/tests/mocha/lib/formatter/apiErrorConfig');\n\nconst Base = require('@truesparrow/base');\nconst ResponseHelper  = Base.responseHelper;\n\n// Creating an object of ResponseHelper. Parameter is an object with key moduleName.\nconst responseHelper = new ResponseHelper({\n      moduleName: '\u003cmodule name\u003e'\n  });\n    \n// for sending an error which is not parameter specific, following function can be used.\n// internal_error_identifier is used for debugging.\n// api_error_identifier is used to fetch information from the apiErrorConfig\nconst r1 = responseHelper.error({\n  internal_error_identifier: 's_vt_1', \n  api_error_identifier: 'test_1',\n  debug_options: {id: 1234},\n  error_config: {\n    param_error_config: paramErrorConfig,\n    api_error_config: apiErrorConfig   \n  }\n});\n\n// r1.toHash() gives following:\n// {\n//   success: false,\n//   err: {\n//     code: 'invalid_request',\n//     msg: 'At least one parameter is invalid or missing. See err.error_data for more details.',\n//     error_data: [],\n//     internal_id: 's_vt_1'\n//   }\n// }\n    \n// For sending parameter specific errors, following function can be used.\n// internal_error_identifier is used for debugging.\n// api_error_identifier is used to fetch information from the apiErrorConfig\n// params_error_identifiers is array of string, which are keys of paramErrorConfig\nconst r2 = responseHelper.paramValidationError({\n  internal_error_identifier:\"s_vt_2\", \n  api_error_identifier: \"test_1\", // key of apiErrorConfig\n  params_error_identifiers: [\"test_1\"], // keys of paramErrorConfig\n  debug_options: {id: 1234},\n  error_config: {\n    param_error_config: paramErrorConfig,\n    api_error_config: apiErrorConfig   \n  }\n});\n\n// r2.toHash() gives following:\n// {\n//   success: false,\n//   err: {\n//     code: 'invalid_request',\n//     msg: 'At least one parameter is invalid or missing. See err.error_data for more details.',\n//     error_data: [  { parameter: 'Username', msg: 'Invalid' } ],\n//     internal_id: 's_vt_2'\n//   }\n// }\n\n// To check if the Result object is a success or not isSuccess function can be used. It returns a boolean.\n// For example:\nr1.isSuccess(); // this will be false.\n\n// isFailure function return NOT of that returned by isSuccess function.\n// For example:\nr1.isFailure(); // this will be true.\n\n// To convert to a format which can be exposed over API, toHash function can be used.\nr1.toHash(); // Examples given above.\n```\n\n## CustomPromise QueueManager\nQueueManager provides various management options and configurations for a queue of Promises. Following is a brief \ndocumentation of the various manager options and example usage.\n```js\nconst Base = require('@truesparrow/base'),\n  logger  = new Base.Logger(\"my_module_name\");\n\nconst queueManagerOptions = {\n  // Specify the name for easy identification in logs.\n  name: \"my_module_name_promise_queue\"\n\n  // resolvePromiseOnTimeout :: set this flag to false if you need custom handling.\n  // By Default, the manager will neither resolve nor reject the Promise on time out.\n  , resolvePromiseOnTimeout: false\n  // The value to be passed to resolve when the Promise has timedout.\n  , resolvedValueOnTimeout: null\n\n  // rejectPromiseOnTimeout :: set this flag to true if you need custom handling.\n  , rejectPromiseOnTimeout : false\n\n  //  Pass timeoutInMilliSecs in options to set the timeout.\n  //  If less than or equal to zero, timeout will not be observed.\n  , timeoutInMilliSecs: 5000\n\n  //  Pass maxZombieCount in options to set the max acceptable zombie count.\n  //  When this zombie promise count reaches this limit, onMaxZombieCountReached will be triggered.\n  //  If less than or equal to zero, onMaxZombieCountReached callback will not triggered.\n  , maxZombieCount: 0\n\n  //  Pass logInfoTimeInterval in options to log queue healthcheck information.\n  //  If less than or equal to zero, healthcheck will not be logged.\n  , logInfoTimeInterval : 0\n\n\n  , onPromiseResolved: function ( resolvedValue, promiseContext ) {\n    //onPromiseResolved will be executed when the any promise is resolved.\n    //This callback method should be set by instance creator.\n    //It can be set using options parameter in constructor.\n    const oThis = this;\n\n    logger.log(oThis.name, \" :: a promise has been resolved. resolvedValue:\", resolvedValue);\n  }\n\n  , onPromiseRejected: function ( rejectReason, promiseContext ) {\n    //onPromiseRejected will be executed when the any promise is timedout.\n    //This callback method should be set by instance creator.\n    //It can be set using options parameter in constructor.\n    const oThis = this;\n\n    logger.log(oThis.name, \" :: a promise has been rejected. rejectReason: \", rejectReason);\n  }\n\n  , onPromiseTimedout: function ( promiseContext ) {\n    //onPromiseTimedout will be executed when the any promise is timedout.\n    //This callback method should be set by instance creator.\n    //It can be set using options parameter in constructor.\n    const oThis = this;\n\n    logger.log(oThis.name, \":: a promise has timed out.\", promiseContext.executorParams);\n  }\n\n  , onMaxZombieCountReached: function () {\n    //onMaxZombieCountReached will be executed when maxZombieCount \u003e= 0 \u0026\u0026 current zombie count (oThis.zombieCount) \u003e= maxZombieCount.\n    //This callback method should be set by instance creator.\n    //It can be set using options parameter in constructor.\n    const oThis = this;\n\n    logger.log(oThis.name, \":: maxZombieCount reached.\");\n\n  }\n\n  , onPromiseCompleted: function ( promiseContext ) {\n    //onPromiseCompleted will be executed when the any promise is removed from pendingPromise queue.\n    //This callback method should be set by instance creator.\n    //It can be set using options parameter in constructor.\n    const oThis = this;\n\n    logger.log(oThis.name, \":: a promise has been completed.\");\n  }  \n  , onAllPromisesCompleted: function () {\n    //onAllPromisesCompleted will be executed when the last promise in pendingPromise is resolved/rejected.\n    //This callback method should be set by instance creator.\n    //It can be set using options parameter in constructor.\n    //Ideally, you should set this inside SIGINT/SIGTERM handlers.\n\n    logger.log(\"Examples.allResolve :: onAllPromisesCompleted triggered\");\n    manager.logInfo();\n  }\n};\n\n\nconst promiseExecutor = function ( resolve, reject, params, promiseContext ) {\n  //promiseExecutor\n  setTimeout(function () {\n    resolve( params.cnt ); // Try different things here.\n  }, 1000);\n};\n\nconst manager = new Base.CustomPromise.QueueManager( promiseExecutor, queueManagerOptions);\n\n// createPromise calls the promiseExecutor\nfor( let cnt = 0; cnt \u003c 5; cnt++ ) {\n  manager.createPromise( {\"cnt\": (cnt + 1) } );\n}\n```\n\n# Running test cases\n```shell script\n./node_modules/.bin/mocha --recursive \"./tests/**/*.js\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftruesparrowsystems%2Fbase","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftruesparrowsystems%2Fbase","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftruesparrowsystems%2Fbase/lists"}