fiberx-backend-toolkit 0.1.21 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/README.md +207 -139
  2. package/dist/code_templates/email_enqueue_code_template.js +2 -2
  3. package/dist/code_templates/sequelize_code_template.d.ts +1 -1
  4. package/dist/code_templates/sequelize_code_template.js +131 -78
  5. package/dist/config/constants.d.ts +1 -0
  6. package/dist/config/constants.js +82 -15
  7. package/dist/database/connectors/sequelize_connector.d.ts +2 -0
  8. package/dist/database/connectors/sequelize_connector.js +43 -21
  9. package/dist/database/main.d.ts +3 -2
  10. package/dist/database/main.js +4 -2
  11. package/dist/database/schema/schema_diff_util.d.ts +0 -1
  12. package/dist/database/schema/schema_diff_util.js +7 -15
  13. package/dist/database/scripts/create_schema_script.js +6 -3
  14. package/dist/database/scripts/make_migrations_script.js +17 -10
  15. package/dist/database/scripts/migration_runner_script.d.ts +3 -1
  16. package/dist/database/scripts/migration_runner_script.js +58 -23
  17. package/dist/database/scripts/seeder_runner_script.d.ts +3 -1
  18. package/dist/database/scripts/seeder_runner_script.js +49 -17
  19. package/dist/database/scripts/sequelize_model_generator_script.d.ts +2 -2
  20. package/dist/database/scripts/sequelize_model_generator_script.js +11 -5
  21. package/dist/index.d.ts +7 -0
  22. package/dist/index.js +7 -0
  23. package/dist/mailer/main.d.ts +1 -1
  24. package/dist/mailer/processors/email_delivery_processor.js +45 -13
  25. package/dist/mailer/processors/email_enqueue_processor.js +23 -7
  26. package/dist/mailer/utils/mailer_data_loader_util.d.ts +4 -4
  27. package/dist/mailer/utils/mailer_data_loader_util.js +5 -5
  28. package/dist/middle_ware/authentication_middle_ware.d.ts +3 -3
  29. package/dist/middle_ware/authentication_middle_ware.js +27 -25
  30. package/dist/middle_ware/cookie_manager_middle_ware.js +1 -1
  31. package/dist/middle_ware/main.d.ts +3 -2
  32. package/dist/middle_ware/main.js +4 -2
  33. package/dist/middle_ware/rate_limiter_middle_ware.js +0 -1
  34. package/dist/middle_ware/request_logger_middle_ware.js +2 -1
  35. package/dist/middle_ware/response_formatter_middle_ware.js +5 -4
  36. package/dist/middle_ware/secure_headers_middle_ware.js +2 -1
  37. package/dist/rbac/rbac_loader_util.js +2 -2
  38. package/dist/storage/drivers/gcs_storage_driver.js +5 -5
  39. package/dist/storage/drivers/local_storage_driver.js +2 -2
  40. package/dist/storage/main.d.ts +1 -1
  41. package/dist/storage/processors/file_upload_processor.js +4 -4
  42. package/dist/storage/utils/storage_driver_util.d.ts +1 -1
  43. package/dist/storage/utils/storage_driver_util.js +12 -10
  44. package/dist/types/express_decelaration.d.ts +1 -1
  45. package/dist/types/express_decelaration.js +0 -1
  46. package/dist/types/middle_ware_type.d.ts +14 -12
  47. package/dist/types/middle_ware_type.js +0 -2
  48. package/dist/types/schema_type.js +0 -1
  49. package/dist/types/util_type.js +0 -1
  50. package/dist/utils/content_manager_util.js +11 -9
  51. package/dist/utils/crypto_key_util.js +9 -10
  52. package/dist/utils/encryptor_decryptor_util.js +19 -9
  53. package/dist/utils/env_manager_util.d.ts +1 -0
  54. package/dist/utils/env_manager_util.js +10 -2
  55. package/dist/utils/fs_actions_util.js +1 -1
  56. package/dist/utils/input_transformer_util.js +11 -25
  57. package/dist/utils/input_validator_util.d.ts +1 -1
  58. package/dist/utils/input_validator_util.js +75 -27
  59. package/dist/utils/logger_util.d.ts +2 -2
  60. package/dist/utils/logger_util.js +23 -9
  61. package/dist/utils/main.d.ts +1 -1
  62. package/dist/utils/safe_execute_util.js +21 -11
  63. package/dist/utils/sql_formatter_util.js +1 -5
  64. package/dist/utils/totp_service_util.js +3 -7
  65. package/dist/utils/uuid_gen_util.js +6 -2
  66. package/dist/validators/file_validator_util.js +2 -2
  67. package/dist/validators/query_validator_util.js +3 -3
  68. package/package.json +68 -2
@@ -23,7 +23,7 @@ class LoggerUtil {
23
23
  * Ensures a directory exists. If it doesn't, creates it.
24
24
  * @param directory_path - Path to the directory
25
25
  * @returns The absolute path of the ensured directory
26
- */
26
+ */
27
27
  ensureDirExists(directory_path) {
28
28
  const resolved_path = path_1.default.resolve(directory_path);
29
29
  if (!fs_1.default.existsSync(resolved_path)) {
@@ -34,7 +34,7 @@ class LoggerUtil {
34
34
  /**
35
35
  * Generate log unique log file name with timestamp and module name
36
36
  * @returns The absolute path of the module log file
37
- */
37
+ */
38
38
  getLogFilePath() {
39
39
  const timestamp = Math.floor(Date.now() / 1000);
40
40
  const file_name = `${this.module_name}-${timestamp}.log`;
@@ -57,15 +57,21 @@ class LoggerUtil {
57
57
  // Log a message with optional error data and level
58
58
  log(message = "", data_error = {}, status = "INFO") {
59
59
  const timestamp = new Date().toISOString();
60
- const error_str = data_error && typeof data_error === "object" && Object.keys(data_error).length ? this.safeStringify(data_error, 0) : "";
61
- const formatted_message = message.startsWith(`[${this.module_name}]`) ? message : `[${this.module_name}] ${message}`;
60
+ const error_str = data_error && typeof data_error === "object" && Object.keys(data_error).length
61
+ ? this.safeStringify(data_error, 0)
62
+ : "";
63
+ const formatted_message = message.startsWith(`[${this.module_name}]`)
64
+ ? message
65
+ : `[${this.module_name}] ${message}`;
62
66
  const log_line = `${timestamp} [${status}] ${formatted_message} ${error_str}\n`;
63
67
  const log_entry = { timestamp, status, message, data_error: data_error };
64
68
  if (this.store_log_locally) {
65
69
  fs_1.default.appendFileSync(this.log_file_path, log_line);
66
70
  }
67
71
  console.log(`\n${timestamp} [${status}] ${formatted_message}\n`);
68
- if (data_error && typeof data_error === "object" && Object.keys(data_error).length) {
72
+ if (data_error &&
73
+ typeof data_error === "object" &&
74
+ Object.keys(data_error).length) {
69
75
  console.log({ ...data_error });
70
76
  console.log("\n");
71
77
  console.log("========================================");
@@ -73,13 +79,21 @@ class LoggerUtil {
73
79
  return log_entry;
74
80
  }
75
81
  // Log info-level messages
76
- info(message, data = "") { return this.log(message, data, "INFO"); }
82
+ info(message, data = "") {
83
+ return this.log(message, data, "INFO");
84
+ }
77
85
  // Log error-level messages
78
- error(message, error = "") { return this.log(message, error, "ERROR"); }
86
+ error(message, error = "") {
87
+ return this.log(message, error, "ERROR");
88
+ }
79
89
  // Log alert-level messages
80
- alert(message, data = "") { return this.log(message, data, "ALERT"); }
90
+ alert(message, data = "") {
91
+ return this.log(message, data, "ALERT");
92
+ }
81
93
  // Log success-level messages
82
- success(message, data = "") { return this.log(message, data, "SUCCESS"); }
94
+ success(message, data = "") {
95
+ return this.log(message, data, "SUCCESS");
96
+ }
83
97
  // Log how long a method took to execute
84
98
  logExecutionTime(start_time, class_name, method_name) {
85
99
  const [seconds, nanoseconds] = process.hrtime(start_time);
@@ -14,4 +14,4 @@ import FsActionsUtil from "./fs_actions_util";
14
14
  import EJSRenderUtil from "./ejs_render_util";
15
15
  import PLimitUtil from "./p_limit_util";
16
16
  import CryptoKeyUtil from "./crypto_key_util";
17
- export { LoggerUtil, InputTransformerUtil, InputValidatorUtil, EnvManagerUtil, SqlFormatterUtil, ServerUtil, SafeExecuteUtil, InMemoryCacheUtil, UUIDGeneratorUtil, EncryptorDecryptorUtil, TOTPServiceUtil, ContentManagerUtil, FsActionsUtil, EJSRenderUtil, PLimitUtil, CryptoKeyUtil };
17
+ export { LoggerUtil, InputTransformerUtil, InputValidatorUtil, EnvManagerUtil, SqlFormatterUtil, ServerUtil, SafeExecuteUtil, InMemoryCacheUtil, UUIDGeneratorUtil, EncryptorDecryptorUtil, TOTPServiceUtil, ContentManagerUtil, FsActionsUtil, EJSRenderUtil, PLimitUtil, CryptoKeyUtil, };
@@ -38,13 +38,17 @@ class SafeExecuteUtil {
38
38
  }
39
39
  // Method to handle on error action
40
40
  static handleOnErrorAction(error, strategy, default_value, controller_response) {
41
- if (strategy === util_type_1.ErrorHandlingStrategyEnum.CONTROLLER_ERROR && controller_response?.errResponse) {
41
+ if (strategy === util_type_1.ErrorHandlingStrategyEnum.CONTROLLER_ERROR &&
42
+ controller_response?.errResponse) {
42
43
  return controller_response?.errResponse?.(500, "invalid_error_request");
43
44
  }
44
45
  switch (strategy) {
45
- case util_type_1.ErrorHandlingStrategyEnum.RETURN_DEFAULT: return default_value;
46
- case util_type_1.ErrorHandlingStrategyEnum.EXIT_PROCESS: process.exit(1);
47
- case util_type_1.ErrorHandlingStrategyEnum.THROW_ERROR: throw error;
46
+ case util_type_1.ErrorHandlingStrategyEnum.RETURN_DEFAULT:
47
+ return default_value;
48
+ case util_type_1.ErrorHandlingStrategyEnum.EXIT_PROCESS:
49
+ process.exit(1);
50
+ case util_type_1.ErrorHandlingStrategyEnum.THROW_ERROR:
51
+ throw error;
48
52
  }
49
53
  return;
50
54
  }
@@ -59,7 +63,11 @@ class SafeExecuteUtil {
59
63
  }
60
64
  catch (error) {
61
65
  const safely_stringified_error = SafeExecuteUtil.safeStringify(error);
62
- logger.error(`[${class_name}] ❌ Error in method ${method_name}`, { method_params, error, safely_stringified_error });
66
+ logger.error(`[${class_name}] ❌ Error in method ${method_name}`, {
67
+ method_params,
68
+ error,
69
+ safely_stringified_error,
70
+ });
63
71
  SafeExecuteUtil.handleOnErrorAction(error, strategy, default_value);
64
72
  }
65
73
  };
@@ -78,7 +86,11 @@ class SafeExecuteUtil {
78
86
  }
79
87
  catch (error) {
80
88
  const safely_stringified_error = SafeExecuteUtil.safeStringify(error);
81
- logger.error(`[${class_name}] ❌ Error in method ${method_name}`, { method_params, error, safely_stringified_error });
89
+ logger.error(`[${class_name}] ❌ Error in method ${method_name}`, {
90
+ method_params,
91
+ error,
92
+ safely_stringified_error,
93
+ });
82
94
  SafeExecuteUtil.handleOnErrorAction(error, strategy, default_value, controller_response);
83
95
  }
84
96
  finally {
@@ -86,16 +98,14 @@ class SafeExecuteUtil {
86
98
  }
87
99
  };
88
100
  }
89
- ;
90
101
  // Decorator Method to handle wrapping cclass methods
91
102
  static applySafeWrapper(target, class_name, property_key, descriptor, strategy, timed, default_value) {
92
103
  if (typeof descriptor.value !== "function") {
93
104
  return;
94
105
  }
95
- descriptor.value = timed ?
96
- SafeExecuteUtil.createTimedSafeFunction(class_name, String(property_key), descriptor.value, strategy, default_value)
97
- :
98
- SafeExecuteUtil.createSafeFunction(class_name, String(property_key), descriptor.value, strategy, default_value);
106
+ descriptor.value = timed
107
+ ? SafeExecuteUtil.createTimedSafeFunction(class_name, String(property_key), descriptor.value, strategy, default_value)
108
+ : SafeExecuteUtil.createSafeFunction(class_name, String(property_key), descriptor.value, strategy, default_value);
99
109
  }
100
110
  // Decorator method to handle controller methods
101
111
  static safeExecuteController(class_name) {
@@ -21,11 +21,7 @@ class SqlFormatterUtil {
21
21
  * - Remove extra spaces inside parentheses
22
22
  */
23
23
  static normalize(sql) {
24
- return sql
25
- .replace(/\s+/g, " ")
26
- .replace(/\(\s+/g, "(")
27
- .replace(/\s+\)/g, ")")
28
- .trim();
24
+ return sql.replace(/\s+/g, " ").replace(/\(\s+/g, "(").replace(/\s+\)/g, ")").trim();
29
25
  }
30
26
  /**
31
27
  * Colorize SQL keywords (SELECT, INSERT, UPDATE, DELETE)
@@ -20,7 +20,7 @@ class TOTPServiceUtil {
20
20
  DIGITS;
21
21
  WINDOW;
22
22
  ALGORITHM;
23
- SECRET_KEY_REGEX = (/^[A-Z2-7]+$/i);
23
+ SECRET_KEY_REGEX = /^[A-Z2-7]+$/i;
24
24
  constructor(options = {}) {
25
25
  this.STEP = options.STEP ?? constants_1.DEFAULT_TOTP_STEP;
26
26
  this.DIGITS = options.DIGITS ?? constants_1.DEFAULT_TOTP_DIGITS;
@@ -87,10 +87,7 @@ class TOTPServiceUtil {
87
87
  generateForCounter(secret, counter) {
88
88
  const buffer = Buffer.alloc(8);
89
89
  buffer.writeBigUInt64BE(BigInt(counter));
90
- const hmac = crypto_1.default
91
- .createHmac(this.ALGORITHM, secret)
92
- .update(buffer)
93
- .digest();
90
+ const hmac = crypto_1.default.createHmac(this.ALGORITHM, secret).update(buffer).digest();
94
91
  const offset = hmac[hmac.length - 1] & 0xf;
95
92
  const code = ((hmac[offset] & 0x7f) << 24) |
96
93
  ((hmac[offset + 1] & 0xff) << 16) |
@@ -153,8 +150,7 @@ class TOTPServiceUtil {
153
150
  const generated = this.generateForCounter(key, counter);
154
151
  if (input_validator_util_1.default.isSafeHashCompare(generated, token)) {
155
152
  // 🔒 Replay protection
156
- if (typeof last_used_counter === "number" &&
157
- counter <= last_used_counter) {
153
+ if (typeof last_used_counter === "number" && counter <= last_used_counter) {
158
154
  return { valid: false };
159
155
  }
160
156
  return { valid: true, counter };
@@ -4,7 +4,9 @@ const crypto_1 = require("crypto");
4
4
  const uuid_1 = require("uuid");
5
5
  class UUIDGeneratorUtil {
6
6
  // Method to Generate a standard UUID (Version 4) following RFC 4122.
7
- static generateUUIDV1() { return (0, crypto_1.randomUUID)(); }
7
+ static generateUUIDV1() {
8
+ return (0, crypto_1.randomUUID)();
9
+ }
8
10
  // Method to Generate a unique code based on the current timestamp and random characters.
9
11
  static generateUUIDV2(prefix = "") {
10
12
  const timestamp = Date.now().toString();
@@ -21,7 +23,9 @@ class UUIDGeneratorUtil {
21
23
  return `${time_stamp_part}${random_part}`.toUpperCase();
22
24
  }
23
25
  // Method to Generate a random byte string with optional prefix.
24
- static generateUUIDV4(prefix = "") { return `${prefix}_${(0, uuid_1.v4)()}`; }
26
+ static generateUUIDV4(prefix = "") {
27
+ return `${prefix}_${(0, uuid_1.v4)()}`;
28
+ }
25
29
  // Method to Generate a new file name with a unique UUID
26
30
  static generateNewFileName(file) {
27
31
  const split = file.name.split(".");
@@ -66,7 +66,7 @@ class FileValidatorUtil {
66
66
  this.logger.error("File size exceeds limit", {
67
67
  size: input.size,
68
68
  max_size,
69
- category
69
+ category,
70
70
  });
71
71
  return { v_state: false, v_msg: "file_size_to_large" };
72
72
  }
@@ -76,7 +76,7 @@ class FileValidatorUtil {
76
76
  this.logger.error("Invalid mime type", {
77
77
  mime: input.mime_type,
78
78
  allowed,
79
- category
79
+ category,
80
80
  });
81
81
  return { v_state: false, v_msg: "invalid_file_mime_type" };
82
82
  }
@@ -15,7 +15,7 @@ class QueryValidatorUtil {
15
15
  if (field.required) {
16
16
  return {
17
17
  v_state: false,
18
- v_msg: `missing_required_query_${field.key}`
18
+ v_msg: `missing_required_query_${field.key}`,
19
19
  };
20
20
  }
21
21
  result[field.key] = field.default ?? null;
@@ -80,7 +80,7 @@ class QueryValidatorUtil {
80
80
  }
81
81
  result[field.key] = {
82
82
  start_date: start.toDate(),
83
- end_date: end.toDate()
83
+ end_date: end.toDate(),
84
84
  };
85
85
  break;
86
86
  }
@@ -103,7 +103,7 @@ class QueryValidatorUtil {
103
103
  return {
104
104
  v_state: true,
105
105
  v_msg: "valid_query_params",
106
- v_data: result
106
+ v_data: result,
107
107
  };
108
108
  }
109
109
  }
package/package.json CHANGED
@@ -1,12 +1,72 @@
1
1
  {
2
2
  "name": "fiberx-backend-toolkit",
3
- "version": "0.1.21",
3
+ "version": "1.0.3",
4
4
  "description": "A TypeScript backend toolkit providing shared domain logic, infrastructure helpers, and utilities for FiberX server-side applications and services.",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "require": "./dist/index.js",
12
+ "default": "./dist/index.js"
13
+ },
14
+ "./types": {
15
+ "types": "./dist/types/main.d.ts",
16
+ "require": "./dist/types/main.js",
17
+ "default": "./dist/types/main.js"
18
+ },
19
+ "./utils": {
20
+ "types": "./dist/utils/main.d.ts",
21
+ "require": "./dist/utils/main.js",
22
+ "default": "./dist/utils/main.js"
23
+ },
24
+ "./database": {
25
+ "types": "./dist/database/main.d.ts",
26
+ "require": "./dist/database/main.js",
27
+ "default": "./dist/database/main.js"
28
+ },
29
+ "./middle-ware": {
30
+ "types": "./dist/middle_ware/main.d.ts",
31
+ "require": "./dist/middle_ware/main.js",
32
+ "default": "./dist/middle_ware/main.js"
33
+ },
34
+ "./middle_ware": {
35
+ "types": "./dist/middle_ware/main.d.ts",
36
+ "require": "./dist/middle_ware/main.js",
37
+ "default": "./dist/middle_ware/main.js"
38
+ },
39
+ "./validators": {
40
+ "types": "./dist/validators/main.d.ts",
41
+ "require": "./dist/validators/main.js",
42
+ "default": "./dist/validators/main.js"
43
+ },
44
+ "./storage": {
45
+ "types": "./dist/storage/main.d.ts",
46
+ "require": "./dist/storage/main.js",
47
+ "default": "./dist/storage/main.js"
48
+ },
49
+ "./mailer": {
50
+ "types": "./dist/mailer/main.d.ts",
51
+ "require": "./dist/mailer/main.js",
52
+ "default": "./dist/mailer/main.js"
53
+ },
54
+ "./rbac": {
55
+ "types": "./dist/rbac/main.d.ts",
56
+ "require": "./dist/rbac/main.js",
57
+ "default": "./dist/rbac/main.js"
58
+ },
59
+ "./dist/*": "./dist/*",
60
+ "./package.json": "./package.json"
61
+ },
8
62
  "scripts": {
9
63
  "build": "tsc && tsc-alias",
64
+ "typecheck": "tsc --noEmit",
65
+ "lint": "eslint . --max-warnings=0",
66
+ "format": "prettier --write .",
67
+ "format:check": "prettier --check .",
68
+ "spell:check": "cspell .",
69
+ "check": "npm run typecheck && npm run lint && npm run format:check && npm run spell:check && npm run build",
10
70
  "prepublishOnly": "npm run build"
11
71
  },
12
72
  "files": [
@@ -38,6 +98,7 @@
38
98
  "uuid": "^12.0.0"
39
99
  },
40
100
  "devDependencies": {
101
+ "@eslint/js": "^10.0.1",
41
102
  "@types/axios": "^0.9.36",
42
103
  "@types/bcrypt": "^6.0.0",
43
104
  "@types/ejs": "^3.1.5",
@@ -46,11 +107,16 @@
46
107
  "@types/jsonwebtoken": "^9.0.10",
47
108
  "@types/node": "^25.2.0",
48
109
  "@types/uuid": "^10.0.0",
110
+ "cspell": "^9.8.0",
111
+ "eslint": "^10.4.0",
112
+ "globals": "^17.6.0",
113
+ "prettier": "^3.8.3",
49
114
  "ts-node": "^10.9.2",
50
115
  "tsc-alias": "^1.8.16",
51
116
  "tsconfig-paths": "^4.2.0",
52
117
  "tsup": "^8.5.1",
53
- "typescript": "^5.8.3"
118
+ "typescript": "^5.8.3",
119
+ "typescript-eslint": "^8.59.3"
54
120
  },
55
121
  "engines": {
56
122
  "node": "20.x"