@catbee/utils 0.0.1

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 (183) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +295 -0
  3. package/build/esm/config.d.ts +22 -0
  4. package/build/esm/config.js +21 -0
  5. package/build/esm/config.js.map +1 -0
  6. package/build/esm/index.d.ts +19 -0
  7. package/build/esm/index.js +19 -0
  8. package/build/esm/index.js.map +1 -0
  9. package/build/esm/types/api-response.d.ts +44 -0
  10. package/build/esm/types/api-response.js +2 -0
  11. package/build/esm/types/api-response.js.map +1 -0
  12. package/build/esm/utils/array.utils.d.ts +105 -0
  13. package/build/esm/utils/array.utils.js +232 -0
  14. package/build/esm/utils/array.utils.js.map +1 -0
  15. package/build/esm/utils/async.utils.d.ts +119 -0
  16. package/build/esm/utils/async.utils.js +474 -0
  17. package/build/esm/utils/async.utils.js.map +1 -0
  18. package/build/esm/utils/cache.utils.d.ts +90 -0
  19. package/build/esm/utils/cache.utils.js +294 -0
  20. package/build/esm/utils/cache.utils.js.map +1 -0
  21. package/build/esm/utils/context-store.utils.d.ts +85 -0
  22. package/build/esm/utils/context-store.utils.js +99 -0
  23. package/build/esm/utils/context-store.utils.js.map +1 -0
  24. package/build/esm/utils/crypto.utils.d.ts +58 -0
  25. package/build/esm/utils/crypto.utils.js +76 -0
  26. package/build/esm/utils/crypto.utils.js.map +1 -0
  27. package/build/esm/utils/dir.utils.d.ts +75 -0
  28. package/build/esm/utils/dir.utils.js +400 -0
  29. package/build/esm/utils/dir.utils.js.map +1 -0
  30. package/build/esm/utils/env.utils.d.ts +131 -0
  31. package/build/esm/utils/env.utils.js +218 -0
  32. package/build/esm/utils/env.utils.js.map +1 -0
  33. package/build/esm/utils/exception.utils.d.ts +114 -0
  34. package/build/esm/utils/exception.utils.js +217 -0
  35. package/build/esm/utils/exception.utils.js.map +1 -0
  36. package/build/esm/utils/fs.utils.d.ts +32 -0
  37. package/build/esm/utils/fs.utils.js +140 -0
  38. package/build/esm/utils/fs.utils.js.map +1 -0
  39. package/build/esm/utils/http-status-codes.d.ts +357 -0
  40. package/build/esm/utils/http-status-codes.js +358 -0
  41. package/build/esm/utils/http-status-codes.js.map +1 -0
  42. package/build/esm/utils/id.utils.d.ts +35 -0
  43. package/build/esm/utils/id.utils.js +55 -0
  44. package/build/esm/utils/id.utils.js.map +1 -0
  45. package/build/esm/utils/logger.utils.d.ts +15 -0
  46. package/build/esm/utils/logger.utils.js +59 -0
  47. package/build/esm/utils/logger.utils.js.map +1 -0
  48. package/build/esm/utils/obj.utils.d.ts +55 -0
  49. package/build/esm/utils/obj.utils.js +121 -0
  50. package/build/esm/utils/obj.utils.js.map +1 -0
  51. package/build/esm/utils/response.utils.d.ts +48 -0
  52. package/build/esm/utils/response.utils.js +80 -0
  53. package/build/esm/utils/response.utils.js.map +1 -0
  54. package/build/esm/utils/string.utils.d.ts +37 -0
  55. package/build/esm/utils/string.utils.js +56 -0
  56. package/build/esm/utils/string.utils.js.map +1 -0
  57. package/build/esm/utils/url.utils.d.ts +24 -0
  58. package/build/esm/utils/url.utils.js +73 -0
  59. package/build/esm/utils/url.utils.js.map +1 -0
  60. package/build/esm/utils/validate.utils.d.ts +60 -0
  61. package/build/esm/utils/validate.utils.js +121 -0
  62. package/build/esm/utils/validate.utils.js.map +1 -0
  63. package/build/esnext/config.d.ts +22 -0
  64. package/build/esnext/config.js +21 -0
  65. package/build/esnext/config.js.map +1 -0
  66. package/build/esnext/index.d.ts +19 -0
  67. package/build/esnext/index.js +19 -0
  68. package/build/esnext/index.js.map +1 -0
  69. package/build/esnext/types/api-response.d.ts +44 -0
  70. package/build/esnext/types/api-response.js +2 -0
  71. package/build/esnext/types/api-response.js.map +1 -0
  72. package/build/esnext/utils/array.utils.d.ts +105 -0
  73. package/build/esnext/utils/array.utils.js +203 -0
  74. package/build/esnext/utils/array.utils.js.map +1 -0
  75. package/build/esnext/utils/async.utils.d.ts +119 -0
  76. package/build/esnext/utils/async.utils.js +291 -0
  77. package/build/esnext/utils/async.utils.js.map +1 -0
  78. package/build/esnext/utils/cache.utils.d.ts +90 -0
  79. package/build/esnext/utils/cache.utils.js +142 -0
  80. package/build/esnext/utils/cache.utils.js.map +1 -0
  81. package/build/esnext/utils/context-store.utils.d.ts +85 -0
  82. package/build/esnext/utils/context-store.utils.js +95 -0
  83. package/build/esnext/utils/context-store.utils.js.map +1 -0
  84. package/build/esnext/utils/crypto.utils.d.ts +58 -0
  85. package/build/esnext/utils/crypto.utils.js +72 -0
  86. package/build/esnext/utils/crypto.utils.js.map +1 -0
  87. package/build/esnext/utils/dir.utils.d.ts +75 -0
  88. package/build/esnext/utils/dir.utils.js +150 -0
  89. package/build/esnext/utils/dir.utils.js.map +1 -0
  90. package/build/esnext/utils/env.utils.d.ts +131 -0
  91. package/build/esnext/utils/env.utils.js +211 -0
  92. package/build/esnext/utils/env.utils.js.map +1 -0
  93. package/build/esnext/utils/exception.utils.d.ts +114 -0
  94. package/build/esnext/utils/exception.utils.js +148 -0
  95. package/build/esnext/utils/exception.utils.js.map +1 -0
  96. package/build/esnext/utils/fs.utils.d.ts +32 -0
  97. package/build/esnext/utils/fs.utils.js +62 -0
  98. package/build/esnext/utils/fs.utils.js.map +1 -0
  99. package/build/esnext/utils/http-status-codes.d.ts +357 -0
  100. package/build/esnext/utils/http-status-codes.js +358 -0
  101. package/build/esnext/utils/http-status-codes.js.map +1 -0
  102. package/build/esnext/utils/id.utils.d.ts +35 -0
  103. package/build/esnext/utils/id.utils.js +53 -0
  104. package/build/esnext/utils/id.utils.js.map +1 -0
  105. package/build/esnext/utils/logger.utils.d.ts +15 -0
  106. package/build/esnext/utils/logger.utils.js +59 -0
  107. package/build/esnext/utils/logger.utils.js.map +1 -0
  108. package/build/esnext/utils/obj.utils.d.ts +55 -0
  109. package/build/esnext/utils/obj.utils.js +94 -0
  110. package/build/esnext/utils/obj.utils.js.map +1 -0
  111. package/build/esnext/utils/response.utils.d.ts +48 -0
  112. package/build/esnext/utils/response.utils.js +58 -0
  113. package/build/esnext/utils/response.utils.js.map +1 -0
  114. package/build/esnext/utils/string.utils.d.ts +37 -0
  115. package/build/esnext/utils/string.utils.js +46 -0
  116. package/build/esnext/utils/string.utils.js.map +1 -0
  117. package/build/esnext/utils/url.utils.d.ts +24 -0
  118. package/build/esnext/utils/url.utils.js +35 -0
  119. package/build/esnext/utils/url.utils.js.map +1 -0
  120. package/build/esnext/utils/validate.utils.d.ts +60 -0
  121. package/build/esnext/utils/validate.utils.js +105 -0
  122. package/build/esnext/utils/validate.utils.js.map +1 -0
  123. package/build/src/config.d.ts +22 -0
  124. package/build/src/config.js +24 -0
  125. package/build/src/config.js.map +1 -0
  126. package/build/src/index.d.ts +19 -0
  127. package/build/src/index.js +35 -0
  128. package/build/src/index.js.map +1 -0
  129. package/build/src/types/api-response.d.ts +44 -0
  130. package/build/src/types/api-response.js +3 -0
  131. package/build/src/types/api-response.js.map +1 -0
  132. package/build/src/utils/array.utils.d.ts +105 -0
  133. package/build/src/utils/array.utils.js +216 -0
  134. package/build/src/utils/array.utils.js.map +1 -0
  135. package/build/src/utils/async.utils.d.ts +119 -0
  136. package/build/src/utils/async.utils.js +304 -0
  137. package/build/src/utils/async.utils.js.map +1 -0
  138. package/build/src/utils/cache.utils.d.ts +90 -0
  139. package/build/src/utils/cache.utils.js +146 -0
  140. package/build/src/utils/cache.utils.js.map +1 -0
  141. package/build/src/utils/context-store.utils.d.ts +85 -0
  142. package/build/src/utils/context-store.utils.js +100 -0
  143. package/build/src/utils/context-store.utils.js.map +1 -0
  144. package/build/src/utils/crypto.utils.d.ts +58 -0
  145. package/build/src/utils/crypto.utils.js +82 -0
  146. package/build/src/utils/crypto.utils.js.map +1 -0
  147. package/build/src/utils/dir.utils.d.ts +75 -0
  148. package/build/src/utils/dir.utils.js +164 -0
  149. package/build/src/utils/dir.utils.js.map +1 -0
  150. package/build/src/utils/env.utils.d.ts +131 -0
  151. package/build/src/utils/env.utils.js +215 -0
  152. package/build/src/utils/env.utils.js.map +1 -0
  153. package/build/src/utils/exception.utils.d.ts +114 -0
  154. package/build/src/utils/exception.utils.js +162 -0
  155. package/build/src/utils/exception.utils.js.map +1 -0
  156. package/build/src/utils/fs.utils.d.ts +32 -0
  157. package/build/src/utils/fs.utils.js +71 -0
  158. package/build/src/utils/fs.utils.js.map +1 -0
  159. package/build/src/utils/http-status-codes.d.ts +357 -0
  160. package/build/src/utils/http-status-codes.js +361 -0
  161. package/build/src/utils/http-status-codes.js.map +1 -0
  162. package/build/src/utils/id.utils.d.ts +35 -0
  163. package/build/src/utils/id.utils.js +60 -0
  164. package/build/src/utils/id.utils.js.map +1 -0
  165. package/build/src/utils/logger.utils.d.ts +15 -0
  166. package/build/src/utils/logger.utils.js +96 -0
  167. package/build/src/utils/logger.utils.js.map +1 -0
  168. package/build/src/utils/obj.utils.d.ts +55 -0
  169. package/build/src/utils/obj.utils.js +103 -0
  170. package/build/src/utils/obj.utils.js.map +1 -0
  171. package/build/src/utils/response.utils.d.ts +48 -0
  172. package/build/src/utils/response.utils.js +63 -0
  173. package/build/src/utils/response.utils.js.map +1 -0
  174. package/build/src/utils/string.utils.d.ts +37 -0
  175. package/build/src/utils/string.utils.js +54 -0
  176. package/build/src/utils/string.utils.js.map +1 -0
  177. package/build/src/utils/url.utils.d.ts +24 -0
  178. package/build/src/utils/url.utils.js +39 -0
  179. package/build/src/utils/url.utils.js.map +1 -0
  180. package/build/src/utils/validate.utils.d.ts +60 -0
  181. package/build/src/utils/validate.utils.js +115 -0
  182. package/build/src/utils/validate.utils.js.map +1 -0
  183. package/package.json +80 -0
@@ -0,0 +1,215 @@
1
+ "use strict";
2
+ /* eslint-disable n/no-process-env */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.Env = exports.Environment = void 0;
5
+ /**
6
+ * Enum representing valid application environments.
7
+ */
8
+ var Environment;
9
+ (function (Environment) {
10
+ Environment["PRODUCTION"] = "production";
11
+ Environment["DEVELOPMENT"] = "development";
12
+ Environment["STAGING"] = "staging";
13
+ Environment["TESTING"] = "testing";
14
+ })(Environment || (exports.Environment = Environment = {}));
15
+ /**
16
+ * Utility class for accessing and managing environment variables.
17
+ * Provides typed getters with fallback defaults and validation.
18
+ */
19
+ class Env {
20
+ /**
21
+ * Checks if the current NODE_ENV is 'development'.
22
+ *
23
+ * @returns {boolean} `true` if NODE_ENV is 'development', else `false`.
24
+ */
25
+ static isDev() {
26
+ return (Env.get("NODE_ENV", Environment.DEVELOPMENT) === Environment.DEVELOPMENT);
27
+ }
28
+ /**
29
+ * Sets an environment variable (only affects runtime memory).
30
+ *
31
+ * @param {string} key - The environment variable key.
32
+ * @param {string} value - The value to set.
33
+ */
34
+ static set(key, value) {
35
+ process.env[key] = value;
36
+ }
37
+ /**
38
+ * Returns all environment variables as an object.
39
+ *
40
+ * @returns {object} The current `process.env` object.
41
+ */
42
+ static getAll() {
43
+ return process.env;
44
+ }
45
+ /**
46
+ * Retrieves a string environment variable with a fallback default.
47
+ *
48
+ * @param {string} key - The environment variable key.
49
+ * @param {string} [defaultValue] - Value to return if the key is missing.
50
+ * @returns {string | undefined} The env value or the fallback.
51
+ */
52
+ static get(key, defaultValue) {
53
+ var _a;
54
+ return (_a = process.env[key]) !== null && _a !== void 0 ? _a : defaultValue;
55
+ }
56
+ /**
57
+ * Retrieves a string environment variable and throws if it's missing.
58
+ *
59
+ * @param {string} key - The environment variable key.
60
+ * @returns {string} The environment variable's value.
61
+ * @throws {Error} If the variable is not defined.
62
+ */
63
+ static getRequired(key) {
64
+ const value = process.env[key];
65
+ if (value === undefined) {
66
+ throw new Error(`Required env ${key} is missing`);
67
+ }
68
+ return value;
69
+ }
70
+ /**
71
+ * Retrieves an environment variable as a number, or returns a default.
72
+ *
73
+ * @param {string} key - The environment variable key.
74
+ * @param {number} defaultValue - Fallback number if key is not present.
75
+ * @returns {number} Parsed numeric value or default.
76
+ * @throws {Error} If the value is not a valid number.
77
+ */
78
+ static getNumber(key, defaultValue) {
79
+ var _a;
80
+ const value = (_a = process.env[key]) !== null && _a !== void 0 ? _a : defaultValue;
81
+ const numberValue = Number(value);
82
+ if (isNaN(numberValue)) {
83
+ throw new Error(`Env ${key} is not a number`);
84
+ }
85
+ return numberValue;
86
+ }
87
+ /**
88
+ * Retrieves a required environment variable as a number.
89
+ *
90
+ * @param {string} key - The environment variable key.
91
+ * @returns {number} Parsed number.
92
+ * @throws {Error} If the value is missing or not a number.
93
+ */
94
+ static getNumberRequired(key) {
95
+ const value = process.env[key];
96
+ if (value === undefined) {
97
+ throw new Error(`Required env ${key} is missing`);
98
+ }
99
+ const numberValue = Number(value);
100
+ if (isNaN(numberValue)) {
101
+ throw new Error(`Required env ${key} is not a number`);
102
+ }
103
+ return numberValue;
104
+ }
105
+ /**
106
+ * Retrieves an environment variable as a boolean.
107
+ * Accepts `true`, `1`, `yes`, `on` as true; `false`, `0`, `no`, `off` as false.
108
+ *
109
+ * @param {string} key - The environment variable key.
110
+ * @param {boolean} [defaultValue=false] - Optional fallback value if key is missing.
111
+ * @returns {boolean} Parsed boolean.
112
+ * @throws {Error} If the value is not a recognized boolean string.
113
+ */
114
+ static getBoolean(key, defaultValue = false) {
115
+ var _a;
116
+ const value = ((_a = process.env[key]) !== null && _a !== void 0 ? _a : defaultValue).toString().toLowerCase();
117
+ if (["true", "1", "yes", "on"].includes(value))
118
+ return true;
119
+ if (["false", "0", "no", "off"].includes(value))
120
+ return false;
121
+ throw new Error(`Env variable ${key} is not a boolean`);
122
+ }
123
+ /**
124
+ * Retrieves a required environment variable as a boolean.
125
+ *
126
+ * @param {string} key - The environment variable key.
127
+ * @returns {boolean} Parsed boolean value.
128
+ * @throws {Error} If missing or invalid.
129
+ */
130
+ static getBooleanRequired(key) {
131
+ const value = process.env[key];
132
+ if (value === undefined) {
133
+ throw new Error(`Required env ${key} is missing`);
134
+ }
135
+ return this.getBoolean(key); // reuse logic
136
+ }
137
+ /**
138
+ * Parses a stringified JSON object from an environment variable.
139
+ *
140
+ * @typeParam T - The type to parse as (defaults to `object`).
141
+ * @param {string} key - The environment variable key.
142
+ * @param {T} defaultValue - Value to return if key is missing.
143
+ * @returns {T} Parsed object or default.
144
+ * @throws {Error} If the value is not valid JSON.
145
+ */
146
+ static getJSON(key, defaultValue) {
147
+ const v = process.env[key];
148
+ if (v !== undefined) {
149
+ try {
150
+ return JSON.parse(v);
151
+ }
152
+ catch (_a) {
153
+ throw new Error(`Env variable ${key} is not a valid JSON string`);
154
+ }
155
+ }
156
+ return defaultValue;
157
+ }
158
+ /**
159
+ * Parses a comma-separated string as an array.
160
+ *
161
+ * @typeParam T - The item type (optional, defaults to string).
162
+ * @param {string} key - The environment variable key.
163
+ * @param {T[]} [defaultValue=[]] - Array to return if value is empty or missing.
164
+ * @param {string} [splitter=','] - Delimiter to split on.
165
+ * @returns {string[] | T[]} An array of strings.
166
+ */
167
+ static getArray(key, defaultValue = [], splitter = ",") {
168
+ const value = process.env[key];
169
+ if (!value || value.trim() === "") {
170
+ return defaultValue;
171
+ }
172
+ return value
173
+ .split(splitter)
174
+ .map((item) => item.trim())
175
+ .filter((item) => item.length > 0);
176
+ }
177
+ /**
178
+ * Retrieves an enum-like environment variable value, validating against allowed values.
179
+ *
180
+ * @typeParam T - The allowed value type (string literal types).
181
+ * @param {string} key - The environment variable key.
182
+ * @param {T[]} allowedValues - Array of accepted string values.
183
+ * @param {T} [defaultValue] - Optional fallback value.
184
+ * @returns {T} The validated environment value.
185
+ * @throws {Error} If missing or invalid.
186
+ */
187
+ static getEnum(key, allowedValues, defaultValue) {
188
+ var _a;
189
+ const value = (_a = process.env[key]) !== null && _a !== void 0 ? _a : defaultValue;
190
+ if (!value || !allowedValues.includes(value)) {
191
+ throw new Error(`Env ${key} must be one of ${allowedValues.join(", ")}. Received: ${value}`);
192
+ }
193
+ return value;
194
+ }
195
+ /**
196
+ * Checks whether the specified environment variable exists.
197
+ *
198
+ * @param {string} key - The environment variable key.
199
+ * @returns {boolean} `true` if the variable is defined, otherwise `false`.
200
+ */
201
+ static has(key) {
202
+ return process.env[key] !== undefined;
203
+ }
204
+ /**
205
+ * Deletes the given environment variable (useful in tests).
206
+ *
207
+ * @param {string} key - The environment variable key to delete.
208
+ * @returns {void}
209
+ */
210
+ static delete(key) {
211
+ delete process.env[key];
212
+ }
213
+ }
214
+ exports.Env = Env;
215
+ //# sourceMappingURL=env.utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"env.utils.js","sourceRoot":"","sources":["../../../src/utils/env.utils.ts"],"names":[],"mappings":";AAAA,qCAAqC;;;AAErC;;GAEG;AACH,IAAY,WAKX;AALD,WAAY,WAAW;IACrB,wCAAyB,CAAA;IACzB,0CAA2B,CAAA;IAC3B,kCAAmB,CAAA;IACnB,kCAAmB,CAAA;AACrB,CAAC,EALW,WAAW,2BAAX,WAAW,QAKtB;AAED;;;GAGG;AACH,MAAa,GAAG;IACd;;;;OAIG;IACH,MAAM,CAAC,KAAK;QACV,OAAO,CACL,GAAG,CAAC,GAAG,CAAC,UAAU,EAAE,WAAW,CAAC,WAAW,CAAC,KAAK,WAAW,CAAC,WAAW,CACzE,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,GAAG,CAAC,GAAW,EAAE,KAAa;QACnC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IAC3B,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,MAAM;QACX,OAAO,OAAO,CAAC,GAAG,CAAC;IACrB,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,GAAG,CAAC,GAAW,EAAE,YAAqB;;QAC3C,OAAO,MAAA,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,mCAAI,YAAY,CAAC;IAC1C,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,WAAW,CAAC,GAAW;QAC5B,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAC/B,IAAI,KAAK,KAAK,SAAS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ACtB,CAAC;QACD,OAAO,KAAK;aACT,KAAK,CAAC,QAAQ,CAAC;aACf,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;aAC1B,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACvC,CAAC;IAED;;;;;;;;;OASG;IACH,MAAM,CAAC,OAAO,CACZ,GAAW,EACX,aAAkB,EAClB,YAAgB;;QAEhB,MAAM,KAAK,GAAG,MAAA,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,mCAAI,YAAY,CAAC;QAC/C,IAAI,CAAC,KAAK,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,KAAU,CAAC,EAAE,CAAC;YAClD,MAAM,IAAI,KAAK,CACb,OAAO,GAAG,mBAAmB,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,eAAe,KAAK,EAAE,CAC5E,CAAC;QACJ,CAAC;QACD,OAAO,KAAU,CAAC;IACpB,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,GAAG,CAAC,GAAW;QACpB,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,SAAS,CAAC;IACxC,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,MAAM,CAAC,GAAW;QACvB,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;CACF;AApND,kBAoNC","sourcesContent":["/* eslint-disable n/no-process-env */\n\n/**\n * Enum representing valid application environments.\n */\nexport enum Environment {\n PRODUCTION = \"production\",\n DEVELOPMENT = \"development\",\n STAGING = \"staging\",\n TESTING = \"testing\",\n}\n\n/**\n * Utility class for accessing and managing environment variables.\n * Provides typed getters with fallback defaults and validation.\n */\nexport class Env {\n /**\n * Checks if the current NODE_ENV is 'development'.\n *\n * @returns {boolean} `true` if NODE_ENV is 'development', else `false`.\n */\n static isDev(): boolean {\n return (\n Env.get(\"NODE_ENV\", Environment.DEVELOPMENT) === Environment.DEVELOPMENT\n );\n }\n\n /**\n * Sets an environment variable (only affects runtime memory).\n *\n * @param {string} key - The environment variable key.\n * @param {string} value - The value to set.\n */\n static set(key: string, value: string): void {\n process.env[key] = value;\n }\n\n /**\n * Returns all environment variables as an object.\n *\n * @returns {object} The current `process.env` object.\n */\n static getAll(): object {\n return process.env;\n }\n\n /**\n * Retrieves a string environment variable with a fallback default.\n *\n * @param {string} key - The environment variable key.\n * @param {string} [defaultValue] - Value to return if the key is missing.\n * @returns {string | undefined} The env value or the fallback.\n */\n static get(key: string, defaultValue?: string): string | undefined {\n return process.env[key] ?? defaultValue;\n }\n\n /**\n * Retrieves a string environment variable and throws if it's missing.\n *\n * @param {string} key - The environment variable key.\n * @returns {string} The environment variable's value.\n * @throws {Error} If the variable is not defined.\n */\n static getRequired(key: string): string {\n const value = process.env[key];\n if (value === undefined) {\n throw new Error(`Required env ${key} is missing`);\n }\n return value;\n }\n\n /**\n * Retrieves an environment variable as a number, or returns a default.\n *\n * @param {string} key - The environment variable key.\n * @param {number} defaultValue - Fallback number if key is not present.\n * @returns {number} Parsed numeric value or default.\n * @throws {Error} If the value is not a valid number.\n */\n static getNumber(key: string, defaultValue: number): number {\n const value = process.env[key] ?? defaultValue;\n const numberValue = Number(value);\n if (isNaN(numberValue)) {\n throw new Error(`Env ${key} is not a number`);\n }\n return numberValue;\n }\n\n /**\n * Retrieves a required environment variable as a number.\n *\n * @param {string} key - The environment variable key.\n * @returns {number} Parsed number.\n * @throws {Error} If the value is missing or not a number.\n */\n static getNumberRequired(key: string): number {\n const value = process.env[key];\n if (value === undefined) {\n throw new Error(`Required env ${key} is missing`);\n }\n const numberValue = Number(value);\n if (isNaN(numberValue)) {\n throw new Error(`Required env ${key} is not a number`);\n }\n return numberValue;\n }\n\n /**\n * Retrieves an environment variable as a boolean.\n * Accepts `true`, `1`, `yes`, `on` as true; `false`, `0`, `no`, `off` as false.\n *\n * @param {string} key - The environment variable key.\n * @param {boolean} [defaultValue=false] - Optional fallback value if key is missing.\n * @returns {boolean} Parsed boolean.\n * @throws {Error} If the value is not a recognized boolean string.\n */\n static getBoolean(key: string, defaultValue = false): boolean {\n const value = (process.env[key] ?? defaultValue).toString().toLowerCase();\n if ([\"true\", \"1\", \"yes\", \"on\"].includes(value)) return true;\n if ([\"false\", \"0\", \"no\", \"off\"].includes(value)) return false;\n throw new Error(`Env variable ${key} is not a boolean`);\n }\n\n /**\n * Retrieves a required environment variable as a boolean.\n *\n * @param {string} key - The environment variable key.\n * @returns {boolean} Parsed boolean value.\n * @throws {Error} If missing or invalid.\n */\n static getBooleanRequired(key: string): boolean {\n const value = process.env[key];\n if (value === undefined) {\n throw new Error(`Required env ${key} is missing`);\n }\n return this.getBoolean(key); // reuse logic\n }\n\n /**\n * Parses a stringified JSON object from an environment variable.\n *\n * @typeParam T - The type to parse as (defaults to `object`).\n * @param {string} key - The environment variable key.\n * @param {T} defaultValue - Value to return if key is missing.\n * @returns {T} Parsed object or default.\n * @throws {Error} If the value is not valid JSON.\n */\n static getJSON<T extends object = object>(key: string, defaultValue: T): T {\n const v = process.env[key];\n if (v !== undefined) {\n try {\n return JSON.parse(v);\n } catch {\n throw new Error(`Env variable ${key} is not a valid JSON string`);\n }\n }\n return defaultValue;\n }\n\n /**\n * Parses a comma-separated string as an array.\n *\n * @typeParam T - The item type (optional, defaults to string).\n * @param {string} key - The environment variable key.\n * @param {T[]} [defaultValue=[]] - Array to return if value is empty or missing.\n * @param {string} [splitter=','] - Delimiter to split on.\n * @returns {string[] | T[]} An array of strings.\n */\n static getArray<T = string>(\n key: string,\n defaultValue: T[] = [],\n splitter = \",\",\n ): string[] | T[] {\n const value = process.env[key];\n if (!value || value.trim() === \"\") {\n return defaultValue;\n }\n return value\n .split(splitter)\n .map((item) => item.trim())\n .filter((item) => item.length > 0);\n }\n\n /**\n * Retrieves an enum-like environment variable value, validating against allowed values.\n *\n * @typeParam T - The allowed value type (string literal types).\n * @param {string} key - The environment variable key.\n * @param {T[]} allowedValues - Array of accepted string values.\n * @param {T} [defaultValue] - Optional fallback value.\n * @returns {T} The validated environment value.\n * @throws {Error} If missing or invalid.\n */\n static getEnum<T extends string>(\n key: string,\n allowedValues: T[],\n defaultValue?: T,\n ): T {\n const value = process.env[key] ?? defaultValue;\n if (!value || !allowedValues.includes(value as T)) {\n throw new Error(\n `Env ${key} must be one of ${allowedValues.join(\", \")}. Received: ${value}`,\n );\n }\n return value as T;\n }\n\n /**\n * Checks whether the specified environment variable exists.\n *\n * @param {string} key - The environment variable key.\n * @returns {boolean} `true` if the variable is defined, otherwise `false`.\n */\n static has(key: string): boolean {\n return process.env[key] !== undefined;\n }\n\n /**\n * Deletes the given environment variable (useful in tests).\n *\n * @param {string} key - The environment variable key to delete.\n * @returns {void}\n */\n static delete(key: string): void {\n delete process.env[key];\n }\n}\n"]}
@@ -0,0 +1,114 @@
1
+ import { ErrorResponse } from "./response.utils";
2
+ /**
3
+ * Generic HTTP error class used for custom exceptions with any status code.
4
+ * Inherit this when you need to throw errors dynamically at runtime.
5
+ */
6
+ export declare class HttpError extends ErrorResponse {
7
+ /**
8
+ * Creates a new HTTP error instance.
9
+ * @param status - A valid HTTP status code (e.g., 400, 500).
10
+ * @param message - The error message to return to the client.
11
+ */
12
+ constructor(status: number, message: string);
13
+ }
14
+ /**
15
+ * Represents a 500 Internal Server Error.
16
+ * Use this for unexpected backend errors that are not caused by the client.
17
+ */
18
+ export declare class InternalServerErrorException extends ErrorResponse {
19
+ /**
20
+ * @param message - Optional custom message. Defaults to "Internal server error".
21
+ */
22
+ constructor(message?: string);
23
+ }
24
+ /**
25
+ * Represents a 401 Unauthorized Error.
26
+ * Indicates that authentication is required and has failed or not been provided.
27
+ */
28
+ export declare class UnauthorizedException extends ErrorResponse {
29
+ /**
30
+ * @param message - Optional custom message. Defaults to "Unauthorized".
31
+ */
32
+ constructor(message?: string);
33
+ }
34
+ /**
35
+ * Represents a 400 Bad Request Error.
36
+ * Indicates that the client has sent invalid data (e.g., malformed request body).
37
+ */
38
+ export declare class BadRequestException extends ErrorResponse {
39
+ /**
40
+ * @param message - Optional custom message. Defaults to "Bad request".
41
+ */
42
+ constructor(message?: string);
43
+ }
44
+ /**
45
+ * Represents a 404 Not Found Error.
46
+ * Used when a requested resource (e.g., user, file) could not be located.
47
+ */
48
+ export declare class NotFoundException extends ErrorResponse {
49
+ /**
50
+ * @param message - Optional custom message. Defaults to "Resource not found".
51
+ */
52
+ constructor(message?: string);
53
+ }
54
+ /**
55
+ * Represents a 403 Forbidden Error.
56
+ * Indicates that the client is authenticated but not allowed to access the resource.
57
+ */
58
+ export declare class ForbiddenException extends ErrorResponse {
59
+ /**
60
+ * @param message - Optional custom message. Defaults to "Forbidden".
61
+ */
62
+ constructor(message?: string);
63
+ }
64
+ /**
65
+ * Represents a 409 Conflict Error.
66
+ * Commonly used when a resource already exists or a versioning conflict occurs.
67
+ */
68
+ export declare class ConflictException extends ErrorResponse {
69
+ /**
70
+ * @param message - Optional custom message. Defaults to "Conflict".
71
+ */
72
+ constructor(message?: string);
73
+ }
74
+ /**
75
+ * Represents a 502 Bad Gateway Error.
76
+ * Used when the server receives an invalid response from an upstream server.
77
+ */
78
+ export declare class BadGatewayException extends ErrorResponse {
79
+ /**
80
+ * @param message - Optional custom message. Defaults to "Bad Gateway".
81
+ */
82
+ constructor(message?: string);
83
+ }
84
+ /**
85
+ * Represents a 429 Too Many Requests Error.
86
+ * Returned when the client has hit a rate limit.
87
+ */
88
+ export declare class TooManyRequestsException extends ErrorResponse {
89
+ /**
90
+ * @param message - Optional custom message. Defaults to "Too many requests".
91
+ */
92
+ constructor(message?: string);
93
+ }
94
+ /**
95
+ * Represents a 503 Service Unavailable Error.
96
+ * Indicates that the server is temporarily unavailable (e.g., for maintenance).
97
+ */
98
+ export declare class ServiceUnavailableException extends ErrorResponse {
99
+ /**
100
+ * @param message - Optional custom message. Defaults to "Service Unavailable".
101
+ */
102
+ constructor(message?: string);
103
+ }
104
+ /**
105
+ * Represents a 504 Gateway Timeout Error.
106
+ * Returned when the server acting as a gateway times out waiting for a response.
107
+ */
108
+ export declare class GatewayTimeoutException extends ErrorResponse {
109
+ /**
110
+ * @param message - Optional custom message. Defaults to "Gateway Timeout".
111
+ */
112
+ constructor(message?: string);
113
+ }
114
+ //# sourceMappingURL=exception.utils.d.ts.map
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GatewayTimeoutException = exports.ServiceUnavailableException = exports.TooManyRequestsException = exports.BadGatewayException = exports.ConflictException = exports.ForbiddenException = exports.NotFoundException = exports.BadRequestException = exports.UnauthorizedException = exports.InternalServerErrorException = exports.HttpError = void 0;
4
+ const http_status_codes_1 = require("./http-status-codes");
5
+ const response_utils_1 = require("./response.utils");
6
+ /**
7
+ * Generic HTTP error class used for custom exceptions with any status code.
8
+ * Inherit this when you need to throw errors dynamically at runtime.
9
+ */
10
+ class HttpError extends response_utils_1.ErrorResponse {
11
+ /**
12
+ * Creates a new HTTP error instance.
13
+ * @param status - A valid HTTP status code (e.g., 400, 500).
14
+ * @param message - The error message to return to the client.
15
+ */
16
+ constructor(status, message) {
17
+ super(message);
18
+ this.status = status;
19
+ }
20
+ }
21
+ exports.HttpError = HttpError;
22
+ /**
23
+ * Represents a 500 Internal Server Error.
24
+ * Use this for unexpected backend errors that are not caused by the client.
25
+ */
26
+ class InternalServerErrorException extends response_utils_1.ErrorResponse {
27
+ /**
28
+ * @param message - Optional custom message. Defaults to "Internal server error".
29
+ */
30
+ constructor(message = "Internal server error") {
31
+ super(message);
32
+ this.status = http_status_codes_1.HttpStatusCodes.INTERNAL_SERVER_ERROR;
33
+ }
34
+ }
35
+ exports.InternalServerErrorException = InternalServerErrorException;
36
+ /**
37
+ * Represents a 401 Unauthorized Error.
38
+ * Indicates that authentication is required and has failed or not been provided.
39
+ */
40
+ class UnauthorizedException extends response_utils_1.ErrorResponse {
41
+ /**
42
+ * @param message - Optional custom message. Defaults to "Unauthorized".
43
+ */
44
+ constructor(message = "Unauthorized") {
45
+ super(message);
46
+ this.status = http_status_codes_1.HttpStatusCodes.UNAUTHORIZED;
47
+ }
48
+ }
49
+ exports.UnauthorizedException = UnauthorizedException;
50
+ /**
51
+ * Represents a 400 Bad Request Error.
52
+ * Indicates that the client has sent invalid data (e.g., malformed request body).
53
+ */
54
+ class BadRequestException extends response_utils_1.ErrorResponse {
55
+ /**
56
+ * @param message - Optional custom message. Defaults to "Bad request".
57
+ */
58
+ constructor(message = "Bad request") {
59
+ super(message);
60
+ this.status = http_status_codes_1.HttpStatusCodes.BAD_REQUEST;
61
+ }
62
+ }
63
+ exports.BadRequestException = BadRequestException;
64
+ /**
65
+ * Represents a 404 Not Found Error.
66
+ * Used when a requested resource (e.g., user, file) could not be located.
67
+ */
68
+ class NotFoundException extends response_utils_1.ErrorResponse {
69
+ /**
70
+ * @param message - Optional custom message. Defaults to "Resource not found".
71
+ */
72
+ constructor(message = "Resource not found") {
73
+ super(message);
74
+ this.status = http_status_codes_1.HttpStatusCodes.NOT_FOUND;
75
+ }
76
+ }
77
+ exports.NotFoundException = NotFoundException;
78
+ /**
79
+ * Represents a 403 Forbidden Error.
80
+ * Indicates that the client is authenticated but not allowed to access the resource.
81
+ */
82
+ class ForbiddenException extends response_utils_1.ErrorResponse {
83
+ /**
84
+ * @param message - Optional custom message. Defaults to "Forbidden".
85
+ */
86
+ constructor(message = "Forbidden") {
87
+ super(message);
88
+ this.status = http_status_codes_1.HttpStatusCodes.FORBIDDEN;
89
+ }
90
+ }
91
+ exports.ForbiddenException = ForbiddenException;
92
+ /**
93
+ * Represents a 409 Conflict Error.
94
+ * Commonly used when a resource already exists or a versioning conflict occurs.
95
+ */
96
+ class ConflictException extends response_utils_1.ErrorResponse {
97
+ /**
98
+ * @param message - Optional custom message. Defaults to "Conflict".
99
+ */
100
+ constructor(message = "Conflict") {
101
+ super(message);
102
+ this.status = http_status_codes_1.HttpStatusCodes.CONFLICT;
103
+ }
104
+ }
105
+ exports.ConflictException = ConflictException;
106
+ /**
107
+ * Represents a 502 Bad Gateway Error.
108
+ * Used when the server receives an invalid response from an upstream server.
109
+ */
110
+ class BadGatewayException extends response_utils_1.ErrorResponse {
111
+ /**
112
+ * @param message - Optional custom message. Defaults to "Bad Gateway".
113
+ */
114
+ constructor(message = "Bad Gateway") {
115
+ super(message);
116
+ this.status = http_status_codes_1.HttpStatusCodes.BAD_GATEWAY;
117
+ }
118
+ }
119
+ exports.BadGatewayException = BadGatewayException;
120
+ /**
121
+ * Represents a 429 Too Many Requests Error.
122
+ * Returned when the client has hit a rate limit.
123
+ */
124
+ class TooManyRequestsException extends response_utils_1.ErrorResponse {
125
+ /**
126
+ * @param message - Optional custom message. Defaults to "Too many requests".
127
+ */
128
+ constructor(message = "Too many requests") {
129
+ super(message);
130
+ this.status = http_status_codes_1.HttpStatusCodes.TOO_MANY_REQUESTS;
131
+ }
132
+ }
133
+ exports.TooManyRequestsException = TooManyRequestsException;
134
+ /**
135
+ * Represents a 503 Service Unavailable Error.
136
+ * Indicates that the server is temporarily unavailable (e.g., for maintenance).
137
+ */
138
+ class ServiceUnavailableException extends response_utils_1.ErrorResponse {
139
+ /**
140
+ * @param message - Optional custom message. Defaults to "Service Unavailable".
141
+ */
142
+ constructor(message = "Service Unavailable") {
143
+ super(message);
144
+ this.status = http_status_codes_1.HttpStatusCodes.SERVICE_UNAVAILABLE;
145
+ }
146
+ }
147
+ exports.ServiceUnavailableException = ServiceUnavailableException;
148
+ /**
149
+ * Represents a 504 Gateway Timeout Error.
150
+ * Returned when the server acting as a gateway times out waiting for a response.
151
+ */
152
+ class GatewayTimeoutException extends response_utils_1.ErrorResponse {
153
+ /**
154
+ * @param message - Optional custom message. Defaults to "Gateway Timeout".
155
+ */
156
+ constructor(message = "Gateway Timeout") {
157
+ super(message);
158
+ this.status = http_status_codes_1.HttpStatusCodes.GATEWAY_TIMEOUT;
159
+ }
160
+ }
161
+ exports.GatewayTimeoutException = GatewayTimeoutException;
162
+ //# sourceMappingURL=exception.utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exception.utils.js","sourceRoot":"","sources":["../../../src/utils/exception.utils.ts"],"names":[],"mappings":";;;AAAA,2DAAsD;AACtD,qDAAiD;AAEjD;;;GAGG;AACH,MAAa,SAAU,SAAQ,8BAAa;IAC1C;;;;OAIG;IACH,YAAY,MAAc,EAAE,OAAe;QACzC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF;AAVD,8BAUC;AAED;;;GAGG;AACH,MAAa,4BAA6B,SAAQ,8BAAa;IAC7D;;OAEG;IACH,YAAY,UAAkB,uBAAuB;QACnD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,mCAAe,CAAC,qBAAqB,CAAC;IACtD,CAAC;CACF;AARD,oEAQC;AAED;;;GAGG;AACH,MAAa,qBAAsB,SAAQ,8BAAa;IACtD;;OAEG;IACH,YAAY,UAAkB,cAAc;QAC1C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,mCAAe,CAAC,YAAY,CAAC;IAC7C,CAAC;CACF;AARD,sDAQC;AAED;;;GAGG;AACH,MAAa,mBAAoB,SAAQ,8BAAa;IACpD;;OAEG;IACH,YAAY,UAAkB,aAAa;QACzC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,mCAAe,CAAC,WAAW,CAAC;IAC5C,CAAC;CACF;AARD,kDAQC;AAED;;;GAGG;AACH,MAAa,iBAAkB,SAAQ,8BAAa;IAClD;;OAEG;IACH,YAAY,UAAkB,oBAAoB;QAChD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,mCAAe,CAAC,SAAS,CAAC;IAC1C,CAAC;CACF;AARD,8CAQC;AAED;;;GAGG;AACH,MAAa,kBAAmB,SA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./http-status-codes\";\nimport { ErrorResponse } from \"./response.utils\";\n\n/**\n * Generic HTTP error class used for custom exceptions with any status code.\n * Inherit this when you need to throw errors dynamically at runtime.\n */\nexport class HttpError extends ErrorResponse {\n /**\n * Creates a new HTTP error instance.\n * @param status - A valid HTTP status code (e.g., 400, 500).\n * @param message - The error message to return to the client.\n */\n constructor(status: number, message: string) {\n super(message);\n this.status = status;\n }\n}\n\n/**\n * Represents a 500 Internal Server Error.\n * Use this for unexpected backend errors that are not caused by the client.\n */\nexport class InternalServerErrorException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Internal server error\".\n */\n constructor(message: string = \"Internal server error\") {\n super(message);\n this.status = HttpStatusCodes.INTERNAL_SERVER_ERROR;\n }\n}\n\n/**\n * Represents a 401 Unauthorized Error.\n * Indicates that authentication is required and has failed or not been provided.\n */\nexport class UnauthorizedException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Unauthorized\".\n */\n constructor(message: string = \"Unauthorized\") {\n super(message);\n this.status = HttpStatusCodes.UNAUTHORIZED;\n }\n}\n\n/**\n * Represents a 400 Bad Request Error.\n * Indicates that the client has sent invalid data (e.g., malformed request body).\n */\nexport class BadRequestException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Bad request\".\n */\n constructor(message: string = \"Bad request\") {\n super(message);\n this.status = HttpStatusCodes.BAD_REQUEST;\n }\n}\n\n/**\n * Represents a 404 Not Found Error.\n * Used when a requested resource (e.g., user, file) could not be located.\n */\nexport class NotFoundException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Resource not found\".\n */\n constructor(message: string = \"Resource not found\") {\n super(message);\n this.status = HttpStatusCodes.NOT_FOUND;\n }\n}\n\n/**\n * Represents a 403 Forbidden Error.\n * Indicates that the client is authenticated but not allowed to access the resource.\n */\nexport class ForbiddenException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Forbidden\".\n */\n constructor(message: string = \"Forbidden\") {\n super(message);\n this.status = HttpStatusCodes.FORBIDDEN;\n }\n}\n\n/**\n * Represents a 409 Conflict Error.\n * Commonly used when a resource already exists or a versioning conflict occurs.\n */\nexport class ConflictException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Conflict\".\n */\n constructor(message: string = \"Conflict\") {\n super(message);\n this.status = HttpStatusCodes.CONFLICT;\n }\n}\n\n/**\n * Represents a 502 Bad Gateway Error.\n * Used when the server receives an invalid response from an upstream server.\n */\nexport class BadGatewayException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Bad Gateway\".\n */\n constructor(message: string = \"Bad Gateway\") {\n super(message);\n this.status = HttpStatusCodes.BAD_GATEWAY;\n }\n}\n\n/**\n * Represents a 429 Too Many Requests Error.\n * Returned when the client has hit a rate limit.\n */\nexport class TooManyRequestsException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Too many requests\".\n */\n constructor(message: string = \"Too many requests\") {\n super(message);\n this.status = HttpStatusCodes.TOO_MANY_REQUESTS;\n }\n}\n\n/**\n * Represents a 503 Service Unavailable Error.\n * Indicates that the server is temporarily unavailable (e.g., for maintenance).\n */\nexport class ServiceUnavailableException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Service Unavailable\".\n */\n constructor(message: string = \"Service Unavailable\") {\n super(message);\n this.status = HttpStatusCodes.SERVICE_UNAVAILABLE;\n }\n}\n\n/**\n * Represents a 504 Gateway Timeout Error.\n * Returned when the server acting as a gateway times out waiting for a response.\n */\nexport class GatewayTimeoutException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Gateway Timeout\".\n */\n constructor(message: string = \"Gateway Timeout\") {\n super(message);\n this.status = HttpStatusCodes.GATEWAY_TIMEOUT;\n }\n}\n"]}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Checks whether a file or directory exists at the given path.
3
+ *
4
+ * @param {string} path - The file or directory path to check.
5
+ * @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.
6
+ */
7
+ export declare function fileExists(path: string): Promise<boolean>;
8
+ /**
9
+ * Reads and parses a JSON file from the specified path.
10
+ *
11
+ * @typeParam T - The expected type of the parsed JSON object.
12
+ * @param {string} path - The path to the JSON file.
13
+ * @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.
14
+ */
15
+ export declare function readJsonFile<T = any>(path: string): Promise<T | null>;
16
+ /**
17
+ * Writes a JavaScript object to a file as formatted JSON.
18
+ *
19
+ * @param {string} path - The destination file path.
20
+ * @param {any} data - The object to serialize and write.
21
+ * @param {number} [space=2] - Number of spaces for JSON formatting.
22
+ * @returns {Promise<void>} Resolves when writing is complete.
23
+ */
24
+ export declare function writeJsonFile(path: string, data: any, space?: number): Promise<void>;
25
+ /**
26
+ * Deletes a file if it exists.
27
+ *
28
+ * @param {string} path - The file path to delete.
29
+ * @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.
30
+ */
31
+ export declare function deleteFileIfExists(path: string): Promise<boolean>;
32
+ //# sourceMappingURL=fs.utils.d.ts.map
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.fileExists = fileExists;
7
+ exports.readJsonFile = readJsonFile;
8
+ exports.writeJsonFile = writeJsonFile;
9
+ exports.deleteFileIfExists = deleteFileIfExists;
10
+ const promises_1 = __importDefault(require("fs/promises"));
11
+ /**
12
+ * Checks whether a file or directory exists at the given path.
13
+ *
14
+ * @param {string} path - The file or directory path to check.
15
+ * @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.
16
+ */
17
+ async function fileExists(path) {
18
+ try {
19
+ await promises_1.default.access(path);
20
+ return true;
21
+ }
22
+ catch (_a) {
23
+ return false;
24
+ }
25
+ }
26
+ /**
27
+ * Reads and parses a JSON file from the specified path.
28
+ *
29
+ * @typeParam T - The expected type of the parsed JSON object.
30
+ * @param {string} path - The path to the JSON file.
31
+ * @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.
32
+ */
33
+ async function readJsonFile(path) {
34
+ try {
35
+ const data = await promises_1.default.readFile(path, "utf-8");
36
+ return JSON.parse(data);
37
+ }
38
+ catch (_a) {
39
+ return null;
40
+ }
41
+ }
42
+ /**
43
+ * Writes a JavaScript object to a file as formatted JSON.
44
+ *
45
+ * @param {string} path - The destination file path.
46
+ * @param {any} data - The object to serialize and write.
47
+ * @param {number} [space=2] - Number of spaces for JSON formatting.
48
+ * @returns {Promise<void>} Resolves when writing is complete.
49
+ */
50
+ async function writeJsonFile(path, data, space = 2) {
51
+ const json = JSON.stringify(data, null, space);
52
+ await promises_1.default.writeFile(path, json, "utf-8");
53
+ }
54
+ /**
55
+ * Deletes a file if it exists.
56
+ *
57
+ * @param {string} path - The file path to delete.
58
+ * @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.
59
+ */
60
+ async function deleteFileIfExists(path) {
61
+ try {
62
+ await promises_1.default.unlink(path);
63
+ return true;
64
+ }
65
+ catch (err) {
66
+ if (err.code === "ENOENT")
67
+ return true;
68
+ return false;
69
+ }
70
+ }
71
+ //# sourceMappingURL=fs.utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fs.utils.js","sourceRoot":"","sources":["../../../src/utils/fs.utils.ts"],"names":[],"mappings":";;;;;AAQA,gCAOC;AASD,oCAOC;AAUD,sCAOC;AAQD,gDAQC;AAhED,2DAA6B;AAE7B;;;;;GAKG;AACI,KAAK,UAAU,UAAU,CAAC,IAAY;IAC3C,IAAI,CAAC;QACH,MAAM,kBAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,WAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACI,KAAK,UAAU,YAAY,CAAU,IAAY;IACtD,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,MAAM,kBAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAC9C,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAM,CAAC;IAC/B,CAAC;IAAC,WAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACI,KAAK,UAAU,aAAa,CACjC,IAAY,EACZ,IAAS,EACT,KAAK,GAAG,CAAC;IAET,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;IAC/C,MAAM,kBAAE,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AAC1C,CAAC;AAED;;;;;GAKG;AACI,KAAK,UAAU,kBAAkB,CAAC,IAAY;IACnD,IAAI,CAAC;QACH,MAAM,kBAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,OAAO,GAAQ,EAAE,CAAC;QAClB,IAAI,GAAG,CAAC,IAAI,KAAK,QAAQ;YAAE,OAAO,IAAI,CAAC;QACvC,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC","sourcesContent":["import fs from \"fs/promises\";\n\n/**\n * Checks whether a file or directory exists at the given path.\n *\n * @param {string} path - The file or directory path to check.\n * @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.\n */\nexport async function fileExists(path: string): Promise<boolean> {\n try {\n await fs.access(path);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Reads and parses a JSON file from the specified path.\n *\n * @typeParam T - The expected type of the parsed JSON object.\n * @param {string} path - The path to the JSON file.\n * @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.\n */\nexport async function readJsonFile<T = any>(path: string): Promise<T | null> {\n try {\n const data = await fs.readFile(path, \"utf-8\");\n return JSON.parse(data) as T;\n } catch {\n return null;\n }\n}\n\n/**\n * Writes a JavaScript object to a file as formatted JSON.\n *\n * @param {string} path - The destination file path.\n * @param {any} data - The object to serialize and write.\n * @param {number} [space=2] - Number of spaces for JSON formatting.\n * @returns {Promise<void>} Resolves when writing is complete.\n */\nexport async function writeJsonFile(\n path: string,\n data: any,\n space = 2,\n): Promise<void> {\n const json = JSON.stringify(data, null, space);\n await fs.writeFile(path, json, \"utf-8\");\n}\n\n/**\n * Deletes a file if it exists.\n *\n * @param {string} path - The file path to delete.\n * @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.\n */\nexport async function deleteFileIfExists(path: string): Promise<boolean> {\n try {\n await fs.unlink(path);\n return true;\n } catch (err: any) {\n if (err.code === \"ENOENT\") return true;\n return false;\n }\n}\n"]}