@catbee/utils 0.0.8-rc.1 → 0.0.8-rc.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 (280) hide show
  1. package/build/{esnext → cjs}/config.d.ts +0 -1
  2. package/build/{src → cjs}/config.js +0 -1
  3. package/build/{esnext → cjs}/index.d.ts +0 -1
  4. package/build/{src → cjs}/index.js +0 -1
  5. package/build/{esnext → cjs}/servers/server.builder.d.ts +0 -1
  6. package/build/{src → cjs}/servers/server.builder.js +10 -13
  7. package/build/{src → cjs}/servers/server.d.ts +0 -1
  8. package/build/{src → cjs}/servers/server.js +83 -75
  9. package/build/{src → cjs}/types/api-response.d.ts +0 -1
  10. package/build/{src → cjs}/types/api-response.js +0 -1
  11. package/build/{src → cjs}/types/index.d.ts +0 -1
  12. package/build/{src → cjs}/types/index.js +0 -1
  13. package/build/{src → cjs}/types/server.d.ts +0 -1
  14. package/build/{src → cjs}/types/server.js +0 -1
  15. package/build/{src → cjs}/utils/array.utils.d.ts +0 -1
  16. package/build/{src → cjs}/utils/array.utils.js +0 -1
  17. package/build/{src → cjs}/utils/async.utils.d.ts +0 -1
  18. package/build/{src → cjs}/utils/async.utils.js +2 -3
  19. package/build/{esnext → cjs}/utils/cache.utils.d.ts +0 -1
  20. package/build/{src → cjs}/utils/cache.utils.js +7 -6
  21. package/build/{esnext → cjs}/utils/context-store.utils.d.ts +1 -2
  22. package/build/{src → cjs}/utils/context-store.utils.js +10 -9
  23. package/build/{esnext → cjs}/utils/crypto.utils.d.ts +0 -1
  24. package/build/{src → cjs}/utils/crypto.utils.js +5 -3
  25. package/build/{src → cjs}/utils/date.utils.d.ts +0 -1
  26. package/build/{src → cjs}/utils/date.utils.js +1 -2
  27. package/build/{src → cjs}/utils/decorators.utils.d.ts +30 -27
  28. package/build/{src → cjs}/utils/decorators.utils.js +169 -30
  29. package/build/{esnext → cjs}/utils/dir.utils.d.ts +0 -1
  30. package/build/{src → cjs}/utils/dir.utils.js +6 -7
  31. package/build/{esnext → cjs}/utils/env.utils.d.ts +0 -1
  32. package/build/{src → cjs}/utils/env.utils.js +5 -8
  33. package/build/{src → cjs}/utils/exception.utils.d.ts +0 -1
  34. package/build/{src → cjs}/utils/exception.utils.js +2 -1
  35. package/build/{src → cjs}/utils/fs.utils.d.ts +0 -1
  36. package/build/{src → cjs}/utils/fs.utils.js +12 -13
  37. package/build/{esnext → cjs}/utils/http-status-codes.d.ts +0 -1
  38. package/build/{src → cjs}/utils/http-status-codes.js +0 -1
  39. package/build/{esnext → cjs}/utils/id.utils.d.ts +0 -1
  40. package/build/{src → cjs}/utils/id.utils.js +0 -1
  41. package/build/{src → cjs}/utils/logger.utils.d.ts +0 -1
  42. package/build/{src → cjs}/utils/logger.utils.js +10 -6
  43. package/build/{esnext → cjs}/utils/middleware.utils.d.ts +0 -1
  44. package/build/{src → cjs}/utils/middleware.utils.js +16 -17
  45. package/build/{esnext → cjs}/utils/obj.utils.d.ts +0 -1
  46. package/build/{src → cjs}/utils/obj.utils.js +1 -2
  47. package/build/{src → cjs}/utils/performance.utils.d.ts +0 -1
  48. package/build/{src → cjs}/utils/performance.utils.js +6 -4
  49. package/build/{esnext → cjs}/utils/request.utils.d.ts +0 -1
  50. package/build/{src → cjs}/utils/request.utils.js +7 -3
  51. package/build/{esnext → cjs}/utils/response.utils.d.ts +0 -1
  52. package/build/{src → cjs}/utils/response.utils.js +42 -28
  53. package/build/{esnext → cjs}/utils/stream.utils.d.ts +0 -1
  54. package/build/{src → cjs}/utils/stream.utils.js +0 -1
  55. package/build/{esnext → cjs}/utils/string.utils.d.ts +0 -1
  56. package/build/{src → cjs}/utils/string.utils.js +0 -1
  57. package/build/{src → cjs}/utils/type.utils.d.ts +0 -1
  58. package/build/{src → cjs}/utils/type.utils.js +2 -3
  59. package/build/{src → cjs}/utils/url.utils.d.ts +0 -1
  60. package/build/{src → cjs}/utils/url.utils.js +8 -9
  61. package/build/{src → cjs}/utils/validate.utils.d.ts +0 -1
  62. package/build/{src → cjs}/utils/validate.utils.js +2 -3
  63. package/build/esm/config.d.ts +0 -1
  64. package/build/esm/config.js +6 -7
  65. package/build/esm/index.d.ts +0 -1
  66. package/build/esm/index.js +0 -1
  67. package/build/esm/servers/server.builder.d.ts +0 -1
  68. package/build/esm/servers/server.builder.js +99 -147
  69. package/build/esm/servers/server.d.ts +0 -1
  70. package/build/esm/servers/server.js +591 -876
  71. package/build/esm/types/api-response.d.ts +0 -1
  72. package/build/esm/types/api-response.js +0 -1
  73. package/build/esm/types/index.d.ts +0 -1
  74. package/build/esm/types/index.js +0 -1
  75. package/build/esm/types/server.d.ts +0 -1
  76. package/build/esm/types/server.js +0 -1
  77. package/build/esm/utils/array.utils.d.ts +0 -1
  78. package/build/esm/utils/array.utils.js +48 -86
  79. package/build/esm/utils/async.utils.d.ts +0 -1
  80. package/build/esm/utils/async.utils.js +247 -524
  81. package/build/esm/utils/cache.utils.d.ts +0 -1
  82. package/build/esm/utils/cache.utils.js +96 -301
  83. package/build/esm/utils/context-store.utils.d.ts +1 -2
  84. package/build/esm/utils/context-store.utils.js +62 -79
  85. package/build/esm/utils/crypto.utils.d.ts +0 -1
  86. package/build/esm/utils/crypto.utils.js +78 -175
  87. package/build/esm/utils/date.utils.d.ts +0 -1
  88. package/build/esm/utils/date.utils.js +47 -53
  89. package/build/esm/utils/decorators.utils.d.ts +30 -27
  90. package/build/esm/utils/decorators.utils.js +417 -461
  91. package/build/esm/utils/dir.utils.d.ts +0 -1
  92. package/build/esm/utils/dir.utils.js +264 -786
  93. package/build/esm/utils/env.utils.d.ts +0 -1
  94. package/build/esm/utils/env.utils.js +180 -271
  95. package/build/esm/utils/exception.utils.d.ts +0 -1
  96. package/build/esm/utils/exception.utils.js +112 -289
  97. package/build/esm/utils/fs.utils.d.ts +0 -1
  98. package/build/esm/utils/fs.utils.js +169 -401
  99. package/build/esm/utils/http-status-codes.d.ts +0 -1
  100. package/build/esm/utils/http-status-codes.js +0 -1
  101. package/build/esm/utils/id.utils.d.ts +0 -1
  102. package/build/esm/utils/id.utils.js +5 -9
  103. package/build/esm/utils/logger.utils.d.ts +0 -1
  104. package/build/esm/utils/logger.utils.js +34 -71
  105. package/build/esm/utils/middleware.utils.d.ts +0 -1
  106. package/build/esm/utils/middleware.utils.js +65 -155
  107. package/build/esm/utils/obj.utils.d.ts +0 -1
  108. package/build/esm/utils/obj.utils.js +69 -176
  109. package/build/esm/utils/performance.utils.d.ts +0 -1
  110. package/build/esm/utils/performance.utils.js +92 -195
  111. package/build/esm/utils/request.utils.d.ts +0 -1
  112. package/build/esm/utils/request.utils.js +41 -74
  113. package/build/esm/utils/response.utils.d.ts +0 -1
  114. package/build/esm/utils/response.utils.js +84 -107
  115. package/build/esm/utils/stream.utils.d.ts +0 -1
  116. package/build/esm/utils/stream.utils.js +38 -113
  117. package/build/esm/utils/string.utils.d.ts +0 -1
  118. package/build/esm/utils/string.utils.js +6 -11
  119. package/build/esm/utils/type.utils.d.ts +0 -1
  120. package/build/esm/utils/type.utils.js +8 -12
  121. package/build/esm/utils/url.utils.d.ts +0 -1
  122. package/build/esm/utils/url.utils.js +49 -127
  123. package/build/esm/utils/validate.utils.d.ts +0 -1
  124. package/build/esm/utils/validate.utils.js +23 -40
  125. package/package.json +288 -63
  126. package/build/esm/config.js.map +0 -1
  127. package/build/esm/index.js.map +0 -1
  128. package/build/esm/servers/server.builder.js.map +0 -1
  129. package/build/esm/servers/server.js.map +0 -1
  130. package/build/esm/types/api-response.js.map +0 -1
  131. package/build/esm/types/index.js.map +0 -1
  132. package/build/esm/types/server.js.map +0 -1
  133. package/build/esm/utils/array.utils.js.map +0 -1
  134. package/build/esm/utils/async.utils.js.map +0 -1
  135. package/build/esm/utils/cache.utils.js.map +0 -1
  136. package/build/esm/utils/context-store.utils.js.map +0 -1
  137. package/build/esm/utils/crypto.utils.js.map +0 -1
  138. package/build/esm/utils/date.utils.js.map +0 -1
  139. package/build/esm/utils/decorators.utils.js.map +0 -1
  140. package/build/esm/utils/dir.utils.js.map +0 -1
  141. package/build/esm/utils/env.utils.js.map +0 -1
  142. package/build/esm/utils/exception.utils.js.map +0 -1
  143. package/build/esm/utils/fs.utils.js.map +0 -1
  144. package/build/esm/utils/http-status-codes.js.map +0 -1
  145. package/build/esm/utils/id.utils.js.map +0 -1
  146. package/build/esm/utils/logger.utils.js.map +0 -1
  147. package/build/esm/utils/middleware.utils.js.map +0 -1
  148. package/build/esm/utils/obj.utils.js.map +0 -1
  149. package/build/esm/utils/performance.utils.js.map +0 -1
  150. package/build/esm/utils/request.utils.js.map +0 -1
  151. package/build/esm/utils/response.utils.js.map +0 -1
  152. package/build/esm/utils/stream.utils.js.map +0 -1
  153. package/build/esm/utils/string.utils.js.map +0 -1
  154. package/build/esm/utils/type.utils.js.map +0 -1
  155. package/build/esm/utils/url.utils.js.map +0 -1
  156. package/build/esm/utils/validate.utils.js.map +0 -1
  157. package/build/esnext/config.js +0 -133
  158. package/build/esnext/config.js.map +0 -1
  159. package/build/esnext/index.js +0 -50
  160. package/build/esnext/index.js.map +0 -1
  161. package/build/esnext/servers/server.builder.js +0 -657
  162. package/build/esnext/servers/server.builder.js.map +0 -1
  163. package/build/esnext/servers/server.d.ts +0 -256
  164. package/build/esnext/servers/server.js +0 -955
  165. package/build/esnext/servers/server.js.map +0 -1
  166. package/build/esnext/types/api-response.d.ts +0 -152
  167. package/build/esnext/types/api-response.js +0 -34
  168. package/build/esnext/types/api-response.js.map +0 -1
  169. package/build/esnext/types/index.d.ts +0 -125
  170. package/build/esnext/types/index.js +0 -25
  171. package/build/esnext/types/index.js.map +0 -1
  172. package/build/esnext/types/server.d.ts +0 -268
  173. package/build/esnext/types/server.js +0 -25
  174. package/build/esnext/types/server.js.map +0 -1
  175. package/build/esnext/utils/array.utils.d.ts +0 -168
  176. package/build/esnext/utils/array.utils.js +0 -345
  177. package/build/esnext/utils/array.utils.js.map +0 -1
  178. package/build/esnext/utils/async.utils.d.ts +0 -265
  179. package/build/esnext/utils/async.utils.js +0 -620
  180. package/build/esnext/utils/async.utils.js.map +0 -1
  181. package/build/esnext/utils/cache.utils.js +0 -293
  182. package/build/esnext/utils/cache.utils.js.map +0 -1
  183. package/build/esnext/utils/context-store.utils.js +0 -289
  184. package/build/esnext/utils/context-store.utils.js.map +0 -1
  185. package/build/esnext/utils/crypto.utils.js +0 -276
  186. package/build/esnext/utils/crypto.utils.js.map +0 -1
  187. package/build/esnext/utils/date.utils.d.ts +0 -159
  188. package/build/esnext/utils/date.utils.js +0 -384
  189. package/build/esnext/utils/date.utils.js.map +0 -1
  190. package/build/esnext/utils/decorators.utils.d.ts +0 -508
  191. package/build/esnext/utils/decorators.utils.js +0 -877
  192. package/build/esnext/utils/decorators.utils.js.map +0 -1
  193. package/build/esnext/utils/dir.utils.js +0 -477
  194. package/build/esnext/utils/dir.utils.js.map +0 -1
  195. package/build/esnext/utils/env.utils.js +0 -785
  196. package/build/esnext/utils/env.utils.js.map +0 -1
  197. package/build/esnext/utils/exception.utils.d.ts +0 -230
  198. package/build/esnext/utils/exception.utils.js +0 -381
  199. package/build/esnext/utils/exception.utils.js.map +0 -1
  200. package/build/esnext/utils/fs.utils.d.ts +0 -164
  201. package/build/esnext/utils/fs.utils.js +0 -349
  202. package/build/esnext/utils/fs.utils.js.map +0 -1
  203. package/build/esnext/utils/http-status-codes.js +0 -295
  204. package/build/esnext/utils/http-status-codes.js.map +0 -1
  205. package/build/esnext/utils/id.utils.js +0 -84
  206. package/build/esnext/utils/id.utils.js.map +0 -1
  207. package/build/esnext/utils/logger.utils.d.ts +0 -160
  208. package/build/esnext/utils/logger.utils.js +0 -300
  209. package/build/esnext/utils/logger.utils.js.map +0 -1
  210. package/build/esnext/utils/middleware.utils.js +0 -236
  211. package/build/esnext/utils/middleware.utils.js.map +0 -1
  212. package/build/esnext/utils/obj.utils.js +0 -413
  213. package/build/esnext/utils/obj.utils.js.map +0 -1
  214. package/build/esnext/utils/performance.utils.d.ts +0 -136
  215. package/build/esnext/utils/performance.utils.js +0 -271
  216. package/build/esnext/utils/performance.utils.js.map +0 -1
  217. package/build/esnext/utils/request.utils.js +0 -186
  218. package/build/esnext/utils/request.utils.js.map +0 -1
  219. package/build/esnext/utils/response.utils.js +0 -247
  220. package/build/esnext/utils/response.utils.js.map +0 -1
  221. package/build/esnext/utils/stream.utils.js +0 -210
  222. package/build/esnext/utils/stream.utils.js.map +0 -1
  223. package/build/esnext/utils/string.utils.js +0 -165
  224. package/build/esnext/utils/string.utils.js.map +0 -1
  225. package/build/esnext/utils/type.utils.d.ts +0 -90
  226. package/build/esnext/utils/type.utils.js +0 -187
  227. package/build/esnext/utils/type.utils.js.map +0 -1
  228. package/build/esnext/utils/url.utils.d.ts +0 -141
  229. package/build/esnext/utils/url.utils.js +0 -304
  230. package/build/esnext/utils/url.utils.js.map +0 -1
  231. package/build/esnext/utils/validate.utils.d.ts +0 -177
  232. package/build/esnext/utils/validate.utils.js +0 -320
  233. package/build/esnext/utils/validate.utils.js.map +0 -1
  234. package/build/src/config.d.ts +0 -122
  235. package/build/src/config.js.map +0 -1
  236. package/build/src/index.d.ts +0 -27
  237. package/build/src/index.js.map +0 -1
  238. package/build/src/servers/server.builder.d.ts +0 -508
  239. package/build/src/servers/server.builder.js.map +0 -1
  240. package/build/src/servers/server.js.map +0 -1
  241. package/build/src/types/api-response.js.map +0 -1
  242. package/build/src/types/index.js.map +0 -1
  243. package/build/src/types/server.js.map +0 -1
  244. package/build/src/utils/array.utils.js.map +0 -1
  245. package/build/src/utils/async.utils.js.map +0 -1
  246. package/build/src/utils/cache.utils.d.ts +0 -153
  247. package/build/src/utils/cache.utils.js.map +0 -1
  248. package/build/src/utils/context-store.utils.d.ts +0 -189
  249. package/build/src/utils/context-store.utils.js.map +0 -1
  250. package/build/src/utils/crypto.utils.d.ts +0 -160
  251. package/build/src/utils/crypto.utils.js.map +0 -1
  252. package/build/src/utils/date.utils.js.map +0 -1
  253. package/build/src/utils/decorators.utils.js.map +0 -1
  254. package/build/src/utils/dir.utils.d.ts +0 -196
  255. package/build/src/utils/dir.utils.js.map +0 -1
  256. package/build/src/utils/env.utils.d.ts +0 -377
  257. package/build/src/utils/env.utils.js.map +0 -1
  258. package/build/src/utils/exception.utils.js.map +0 -1
  259. package/build/src/utils/fs.utils.js.map +0 -1
  260. package/build/src/utils/http-status-codes.d.ts +0 -266
  261. package/build/src/utils/http-status-codes.js.map +0 -1
  262. package/build/src/utils/id.utils.d.ts +0 -36
  263. package/build/src/utils/id.utils.js.map +0 -1
  264. package/build/src/utils/logger.utils.js.map +0 -1
  265. package/build/src/utils/middleware.utils.d.ts +0 -100
  266. package/build/src/utils/middleware.utils.js.map +0 -1
  267. package/build/src/utils/obj.utils.d.ts +0 -124
  268. package/build/src/utils/obj.utils.js.map +0 -1
  269. package/build/src/utils/performance.utils.js.map +0 -1
  270. package/build/src/utils/request.utils.d.ts +0 -86
  271. package/build/src/utils/request.utils.js.map +0 -1
  272. package/build/src/utils/response.utils.d.ts +0 -163
  273. package/build/src/utils/response.utils.js.map +0 -1
  274. package/build/src/utils/stream.utils.d.ts +0 -88
  275. package/build/src/utils/stream.utils.js.map +0 -1
  276. package/build/src/utils/string.utils.d.ts +0 -93
  277. package/build/src/utils/string.utils.js.map +0 -1
  278. package/build/src/utils/type.utils.js.map +0 -1
  279. package/build/src/utils/url.utils.js.map +0 -1
  280. package/build/src/utils/validate.utils.js.map +0 -1
@@ -1,349 +0,0 @@
1
- /*
2
- * The MIT License
3
- *
4
- * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license
5
- *
6
- * Permission is hereby granted, free of charge, to any person obtaining a copy
7
- * of this software and associated documentation files (the "Software"), to deal
8
- * in the Software without restriction, including without limitation the rights
9
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- * copies of the Software, and to permit persons to whom the Software is
11
- * furnished to do so, subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be included in all
14
- * copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- * SOFTWARE.
23
- */
24
- import fs from 'fs/promises';
25
- import { createReadStream, createWriteStream } from 'fs';
26
- import { tmpdir } from 'os';
27
- import path from 'path';
28
- import { pipeline } from 'stream/promises';
29
- import { randomUUID } from 'crypto';
30
- /**
31
- * Checks whether a file or directory exists at the given path.
32
- *
33
- * @param {string} path - The file or directory path to check.
34
- * @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.
35
- */
36
- export async function fileExists(path) {
37
- try {
38
- await fs.access(path);
39
- return true;
40
- }
41
- catch (_a) {
42
- return false;
43
- }
44
- }
45
- /**
46
- * Reads and parses a JSON file from the specified path.
47
- *
48
- * @typeParam T - The expected type of the parsed JSON object.
49
- * @param {string} path - The path to the JSON file.
50
- * @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.
51
- */
52
- export async function readJsonFile(path) {
53
- try {
54
- const data = await fs.readFile(path, 'utf-8');
55
- return JSON.parse(data);
56
- }
57
- catch (_a) {
58
- return null;
59
- }
60
- }
61
- /**
62
- * Writes a JavaScript object to a file as formatted JSON.
63
- *
64
- * @param {string} path - The destination file path.
65
- * @param {any} data - The object to serialize and write.
66
- * @param {number} [space=2] - Number of spaces for JSON formatting.
67
- * @returns {Promise<void>} Resolves when writing is complete.
68
- */
69
- export async function writeJsonFile(path, data, space = 2) {
70
- const json = JSON.stringify(data, null, space);
71
- await fs.writeFile(path, json, 'utf-8');
72
- }
73
- /**
74
- * Deletes a file if it exists.
75
- *
76
- * @param {string} path - The file path to delete.
77
- * @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.
78
- */
79
- export async function deleteFileIfExists(path) {
80
- try {
81
- await fs.unlink(path);
82
- return true;
83
- }
84
- catch (err) {
85
- if (err.code === 'ENOENT')
86
- return true;
87
- return false;
88
- }
89
- }
90
- /**
91
- * Reads a text file from the specified path.
92
- *
93
- * @param {string} path - The path to the text file.
94
- * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
95
- * @returns {Promise<string | null>} The file contents, or `null` if reading fails.
96
- */
97
- export async function readTextFile(path, encoding = 'utf-8') {
98
- try {
99
- return await fs.readFile(path, encoding);
100
- }
101
- catch (_a) {
102
- return null;
103
- }
104
- }
105
- /**
106
- * Writes text content to a file.
107
- *
108
- * @param {string} path - The destination file path.
109
- * @param {string} content - The text content to write.
110
- * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
111
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
112
- */
113
- export async function writeTextFile(path, content, encoding = 'utf-8') {
114
- try {
115
- await fs.writeFile(path, content, encoding);
116
- return true;
117
- }
118
- catch (_a) {
119
- return false;
120
- }
121
- }
122
- /**
123
- * Appends text content to a file.
124
- *
125
- * @param {string} path - The file path to append to.
126
- * @param {string} content - The text content to append.
127
- * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
128
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
129
- */
130
- export async function appendTextFile(path, content, encoding = 'utf-8') {
131
- try {
132
- await fs.appendFile(path, content, encoding);
133
- return true;
134
- }
135
- catch (_a) {
136
- return false;
137
- }
138
- }
139
- /**
140
- * Copies a file from source to destination.
141
- *
142
- * @param {string} source - Source file path.
143
- * @param {string} destination - Destination file path.
144
- * @param {boolean} [overwrite=false] - Whether to overwrite if destination exists.
145
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
146
- */
147
- export async function copyFile(source, destination, overwrite = false) {
148
- try {
149
- const flags = overwrite ? 0 : fs.constants.COPYFILE_EXCL;
150
- await fs.copyFile(source, destination, flags);
151
- return true;
152
- }
153
- catch (_a) {
154
- return false;
155
- }
156
- }
157
- /**
158
- * Renames/moves a file.
159
- *
160
- * @param {string} oldPath - Current file path.
161
- * @param {string} newPath - New file path.
162
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
163
- */
164
- export async function moveFile(oldPath, newPath) {
165
- try {
166
- await fs.rename(oldPath, newPath);
167
- return true;
168
- }
169
- catch (_a) {
170
- // If rename fails (e.g., across devices), try copy+delete
171
- try {
172
- await fs.copyFile(oldPath, newPath);
173
- await fs.unlink(oldPath);
174
- return true;
175
- }
176
- catch (_b) {
177
- return false;
178
- }
179
- }
180
- }
181
- /**
182
- * Gets file stats if the file exists.
183
- *
184
- * @param {string} path - Path to the file.
185
- * @returns {Promise<fs.Stats | null>} File stats object or null if file doesn't exist.
186
- */
187
- export async function getFileStats(path) {
188
- try {
189
- return await fs.stat(path);
190
- }
191
- catch (_a) {
192
- return null;
193
- }
194
- }
195
- /**
196
- * Creates a temporary file with optional content.
197
- *
198
- * @param {object} [options] - Options for creating the temp file.
199
- * @param {string} [options.prefix="tmp-"] - Filename prefix.
200
- * @param {string} [options.extension=""] - File extension.
201
- * @param {string} [options.dir] - Directory to create the file in (defaults to OS temp dir).
202
- * @param {string | Buffer} [options.content] - Optional content to write to the file.
203
- * @returns {Promise<string>} Path to the created temporary file.
204
- */
205
- export async function createTempFile({ prefix = 'tmp-', extension = '', dir = tmpdir(), content } = {}) {
206
- const ext = extension ? (extension.startsWith('.') ? extension : `.${extension}`) : '';
207
- const filename = `${prefix}${randomUUID()}${ext}`;
208
- const filepath = path.join(dir, filename);
209
- if (content) {
210
- await fs.writeFile(filepath, content);
211
- }
212
- else {
213
- await fs.writeFile(filepath, '');
214
- }
215
- return filepath;
216
- }
217
- /**
218
- * Streams a file from source to destination.
219
- * Useful for large files to avoid loading the entire file into memory.
220
- *
221
- * @param {string} source - Source file path.
222
- * @param {string} destination - Destination file path.
223
- * @returns {Promise<void>} Resolves when streaming completes.
224
- * @throws {Error} If streaming fails.
225
- */
226
- export async function streamFile(source, destination) {
227
- const readStream = createReadStream(source);
228
- const writeStream = createWriteStream(destination);
229
- try {
230
- await pipeline(readStream, writeStream);
231
- }
232
- catch (error) {
233
- // Ensure both streams are closed in case of error
234
- readStream.destroy();
235
- writeStream.destroy();
236
- throw error;
237
- }
238
- }
239
- /**
240
- * Reads a directory and returns file names.
241
- *
242
- * @param {string} dirPath - Path to the directory.
243
- * @param {object} [options] - Options for reading the directory.
244
- * @param {boolean} [options.fullPaths=false] - Whether to return full paths.
245
- * @param {RegExp} [options.filter] - Optional regex to filter files.
246
- * @returns {Promise<string[]>} Array of file names or paths.
247
- * @throws {Error} If directory cannot be read.
248
- */
249
- export async function readDirectory(dirPath, options = {}) {
250
- const files = await fs.readdir(dirPath);
251
- let result = files;
252
- if (options.filter) {
253
- result = result.filter(file => options.filter.test(file));
254
- }
255
- if (options.fullPaths) {
256
- result = result.map(file => path.join(dirPath, file));
257
- }
258
- return result;
259
- }
260
- /**
261
- * Creates a directory if it doesn't exist.
262
- *
263
- * @param {string} dirPath - Path to the directory.
264
- * @param {boolean} [recursive=true] - Whether to create parent directories.
265
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
266
- */
267
- export async function createDirectory(dirPath, recursive = true) {
268
- try {
269
- await fs.mkdir(dirPath, { recursive });
270
- return true;
271
- }
272
- catch (err) {
273
- // Consider it success if directory already exists
274
- return err.code === 'EEXIST';
275
- }
276
- }
277
- /**
278
- * Safely reads and parses a JSON file with error details.
279
- *
280
- * @typeParam T - The expected type of the parsed JSON object.
281
- * @param {string} path - The path to the JSON file.
282
- * @returns {Promise<{ data: T | null; error: Error | null }>} Object with data and error properties.
283
- */
284
- export async function safeReadJsonFile(path) {
285
- try {
286
- const content = await fs.readFile(path, 'utf-8');
287
- try {
288
- const data = JSON.parse(content);
289
- return { data, error: null };
290
- }
291
- catch (error) {
292
- return {
293
- data: null,
294
- error: new Error(`Invalid JSON in file ${path}: ${error.message}`)
295
- };
296
- }
297
- }
298
- catch (error) {
299
- return {
300
- data: null,
301
- error: new Error(`Failed to read file ${path}: ${error.message}`)
302
- };
303
- }
304
- }
305
- /**
306
- * Checks if a path points to a file (not a directory).
307
- *
308
- * @param {string} path - Path to check.
309
- * @returns {Promise<boolean>} True if the path is a file, false otherwise.
310
- */
311
- export async function isFile(path) {
312
- try {
313
- const stat = await fs.stat(path);
314
- return stat.isFile();
315
- }
316
- catch (_a) {
317
- return false;
318
- }
319
- }
320
- /**
321
- * Gets the size of a file in bytes.
322
- *
323
- * @param {string} path - Path to the file.
324
- * @returns {Promise<number>} Size in bytes or -1 if file doesn't exist.
325
- */
326
- export async function getFileSize(path) {
327
- try {
328
- const stat = await fs.stat(path);
329
- return stat.size;
330
- }
331
- catch (_a) {
332
- return -1;
333
- }
334
- }
335
- /**
336
- * Reads a file as a Buffer.
337
- *
338
- * @param {string} path - Path to the file.
339
- * @returns {Promise<Buffer | null>} File contents as Buffer or null if reading fails.
340
- */
341
- export async function readFileBuffer(path) {
342
- try {
343
- return await fs.readFile(path);
344
- }
345
- catch (_a) {
346
- return null;
347
- }
348
- }
349
- //# sourceMappingURL=fs.utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fs.utils.js","sourceRoot":"","sources":["../../../src/utils/fs.utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAGH,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAS,MAAM,IAAI,CAAC;AAChE,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAC5B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAi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O,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,IAAY;IAC5C,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,MAAM,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACjC,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAAC,WAAM,CAAC;QACP,OAAO,CAAC,CAAC,CAAC;IACZ,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,IAAY;IAC/C,IAAI,CAAC;QACH,OAAO,MAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC;IAAC,WAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["/*\n * The MIT License\n *\n * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { BufferEncoding } from './crypto.utils';\nimport fs from 'fs/promises';\nimport { createReadStream, createWriteStream, Stats } from 'fs';\nimport { tmpdir } from 'os';\nimport path from 'path';\nimport { pipeline } from 'stream/promises';\nimport { randomUUID } from 'crypto';\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(path: string, data: any, space = 2): 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\n/**\n * Reads a text file from the specified path.\n *\n * @param {string} path - The path to the text file.\n * @param {BufferEncoding} [encoding=\"utf-8\"] - The encoding to use.\n * @returns {Promise<string | null>} The file contents, or `null` if reading fails.\n */\nexport async function readTextFile(path: string, encoding: BufferEncoding = 'utf-8'): Promise<string | null> {\n try {\n return await fs.readFile(path, encoding);\n } catch {\n return null;\n }\n}\n\n/**\n * Writes text content to a file.\n *\n * @param {string} path - The destination file path.\n * @param {string} content - The text content to write.\n * @param {BufferEncoding} [encoding=\"utf-8\"] - The encoding to use.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function writeTextFile(\n path: string,\n content: string,\n encoding: BufferEncoding = 'utf-8'\n): Promise<boolean> {\n try {\n await fs.writeFile(path, content, encoding);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Appends text content to a file.\n *\n * @param {string} path - The file path to append to.\n * @param {string} content - The text content to append.\n * @param {BufferEncoding} [encoding=\"utf-8\"] - The encoding to use.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function appendTextFile(\n path: string,\n content: string,\n encoding: BufferEncoding = 'utf-8'\n): Promise<boolean> {\n try {\n await fs.appendFile(path, content, encoding);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Copies a file from source to destination.\n *\n * @param {string} source - Source file path.\n * @param {string} destination - Destination file path.\n * @param {boolean} [overwrite=false] - Whether to overwrite if destination exists.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function copyFile(source: string, destination: string, overwrite = false): Promise<boolean> {\n try {\n const flags = overwrite ? 0 : fs.constants.COPYFILE_EXCL;\n await fs.copyFile(source, destination, flags);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Renames/moves a file.\n *\n * @param {string} oldPath - Current file path.\n * @param {string} newPath - New file path.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function moveFile(oldPath: string, newPath: string): Promise<boolean> {\n try {\n await fs.rename(oldPath, newPath);\n return true;\n } catch {\n // If rename fails (e.g., across devices), try copy+delete\n try {\n await fs.copyFile(oldPath, newPath);\n await fs.unlink(oldPath);\n return true;\n } catch {\n return false;\n }\n }\n}\n\n/**\n * Gets file stats if the file exists.\n *\n * @param {string} path - Path to the file.\n * @returns {Promise<fs.Stats | null>} File stats object or null if file doesn't exist.\n */\nexport async function getFileStats(path: string): Promise<Stats | null> {\n try {\n return await fs.stat(path);\n } catch {\n return null;\n }\n}\n\n/**\n * Creates a temporary file with optional content.\n *\n * @param {object} [options] - Options for creating the temp file.\n * @param {string} [options.prefix=\"tmp-\"] - Filename prefix.\n * @param {string} [options.extension=\"\"] - File extension.\n * @param {string} [options.dir] - Directory to create the file in (defaults to OS temp dir).\n * @param {string | Buffer} [options.content] - Optional content to write to the file.\n * @returns {Promise<string>} Path to the created temporary file.\n */\nexport async function createTempFile({\n prefix = 'tmp-',\n extension = '',\n dir = tmpdir(),\n content\n}: {\n prefix?: string;\n extension?: string;\n dir?: string;\n content?: string | Buffer;\n} = {}): Promise<string> {\n const ext = extension ? (extension.startsWith('.') ? extension : `.${extension}`) : '';\n const filename = `${prefix}${randomUUID()}${ext}`;\n const filepath = path.join(dir, filename);\n\n if (content) {\n await fs.writeFile(filepath, content);\n } else {\n await fs.writeFile(filepath, '');\n }\n\n return filepath;\n}\n\n/**\n * Streams a file from source to destination.\n * Useful for large files to avoid loading the entire file into memory.\n *\n * @param {string} source - Source file path.\n * @param {string} destination - Destination file path.\n * @returns {Promise<void>} Resolves when streaming completes.\n * @throws {Error} If streaming fails.\n */\nexport async function streamFile(source: string, destination: string): Promise<void> {\n const readStream = createReadStream(source);\n const writeStream = createWriteStream(destination);\n\n try {\n await pipeline(readStream, writeStream);\n } catch (error) {\n // Ensure both streams are closed in case of error\n readStream.destroy();\n writeStream.destroy();\n throw error;\n }\n}\n\n/**\n * Reads a directory and returns file names.\n *\n * @param {string} dirPath - Path to the directory.\n * @param {object} [options] - Options for reading the directory.\n * @param {boolean} [options.fullPaths=false] - Whether to return full paths.\n * @param {RegExp} [options.filter] - Optional regex to filter files.\n * @returns {Promise<string[]>} Array of file names or paths.\n * @throws {Error} If directory cannot be read.\n */\nexport async function readDirectory(\n dirPath: string,\n options: { fullPaths?: boolean; filter?: RegExp } = {}\n): Promise<string[]> {\n const files = await fs.readdir(dirPath);\n\n let result = files;\n\n if (options.filter) {\n result = result.filter(file => options.filter!.test(file));\n }\n\n if (options.fullPaths) {\n result = result.map(file => path.join(dirPath, file));\n }\n\n return result;\n}\n\n/**\n * Creates a directory if it doesn't exist.\n *\n * @param {string} dirPath - Path to the directory.\n * @param {boolean} [recursive=true] - Whether to create parent directories.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function createDirectory(dirPath: string, recursive = true): Promise<boolean> {\n try {\n await fs.mkdir(dirPath, { recursive });\n return true;\n } catch (err: any) {\n // Consider it success if directory already exists\n return err.code === 'EEXIST';\n }\n}\n\n/**\n * Safely reads and parses a JSON file with error details.\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<{ data: T | null; error: Error | null }>} Object with data and error properties.\n */\nexport async function safeReadJsonFile<T = any>(path: string): Promise<{ data: T | null; error: Error | null }> {\n try {\n const content = await fs.readFile(path, 'utf-8');\n try {\n const data = JSON.parse(content) as T;\n return { data, error: null };\n } catch (error: any) {\n return {\n data: null,\n error: new Error(`Invalid JSON in file ${path}: ${error.message}`)\n };\n }\n } catch (error: any) {\n return {\n data: null,\n error: new Error(`Failed to read file ${path}: ${error.message}`)\n };\n }\n}\n\n/**\n * Checks if a path points to a file (not a directory).\n *\n * @param {string} path - Path to check.\n * @returns {Promise<boolean>} True if the path is a file, false otherwise.\n */\nexport async function isFile(path: string): Promise<boolean> {\n try {\n const stat = await fs.stat(path);\n return stat.isFile();\n } catch {\n return false;\n }\n}\n\n/**\n * Gets the size of a file in bytes.\n *\n * @param {string} path - Path to the file.\n * @returns {Promise<number>} Size in bytes or -1 if file doesn't exist.\n */\nexport async function getFileSize(path: string): Promise<number> {\n try {\n const stat = await fs.stat(path);\n return stat.size;\n } catch {\n return -1;\n }\n}\n\n/**\n * Reads a file as a Buffer.\n *\n * @param {string} path - Path to the file.\n * @returns {Promise<Buffer | null>} File contents as Buffer or null if reading fails.\n */\nexport async function readFileBuffer(path: string): Promise<Buffer | null> {\n try {\n return await fs.readFile(path);\n } catch {\n return null;\n }\n}\n"]}
@@ -1,295 +0,0 @@
1
- /*
2
- * The MIT License
3
- *
4
- * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license
5
- *
6
- * Permission is hereby granted, free of charge, to any person obtaining a copy
7
- * of this software and associated documentation files (the "Software"), to deal
8
- * in the Software without restriction, including without limitation the rights
9
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- * copies of the Software, and to permit persons to whom the Software is
11
- * furnished to do so, subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be included in all
14
- * copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- * SOFTWARE.
23
- */
24
- /**
25
- * Complete HTTP status codes enum with descriptions.
26
- * For use with API responses and error handling.
27
- */
28
- export var HttpStatusCodes;
29
- (function (HttpStatusCodes) {
30
- // 1xx Informational
31
- /**
32
- * The server has received the request headers and the client should proceed to send the request body.
33
- */
34
- HttpStatusCodes[HttpStatusCodes["CONTINUE"] = 100] = "CONTINUE";
35
- /**
36
- * The server is switching protocols according to the client's request.
37
- */
38
- HttpStatusCodes[HttpStatusCodes["SWITCHING_PROTOCOLS"] = 101] = "SWITCHING_PROTOCOLS";
39
- /**
40
- * Server has received the request and is still processing it.
41
- */
42
- HttpStatusCodes[HttpStatusCodes["PROCESSING"] = 102] = "PROCESSING";
43
- /**
44
- * The server is sending preliminary headers while preparing the final response.
45
- */
46
- HttpStatusCodes[HttpStatusCodes["EARLY_HINTS"] = 103] = "EARLY_HINTS";
47
- // 2xx Success
48
- /**
49
- * The request succeeded. The result meaning depends on the HTTP method used.
50
- */
51
- HttpStatusCodes[HttpStatusCodes["OK"] = 200] = "OK";
52
- /**
53
- * The request succeeded and a new resource was created as a result.
54
- */
55
- HttpStatusCodes[HttpStatusCodes["CREATED"] = 201] = "CREATED";
56
- /**
57
- * The request has been received but not yet acted upon.
58
- */
59
- HttpStatusCodes[HttpStatusCodes["ACCEPTED"] = 202] = "ACCEPTED";
60
- /**
61
- * The returned information is from a third-party source, not the origin server.
62
- */
63
- HttpStatusCodes[HttpStatusCodes["NON_AUTHORITATIVE_INFORMATION"] = 203] = "NON_AUTHORITATIVE_INFORMATION";
64
- /**
65
- * The request succeeded but there's no content to send in the response.
66
- */
67
- HttpStatusCodes[HttpStatusCodes["NO_CONTENT"] = 204] = "NO_CONTENT";
68
- /**
69
- * The client should reset the document view that caused this request.
70
- */
71
- HttpStatusCodes[HttpStatusCodes["RESET_CONTENT"] = 205] = "RESET_CONTENT";
72
- /**
73
- * The server is delivering only part of the resource due to a range header sent by the client.
74
- */
75
- HttpStatusCodes[HttpStatusCodes["PARTIAL_CONTENT"] = 206] = "PARTIAL_CONTENT";
76
- /**
77
- * Conveys information about multiple resources, for complex operations.
78
- */
79
- HttpStatusCodes[HttpStatusCodes["MULTI_STATUS"] = 207] = "MULTI_STATUS";
80
- /**
81
- * The members of a DAV binding have already been enumerated and are not included in the response.
82
- */
83
- HttpStatusCodes[HttpStatusCodes["ALREADY_REPORTED"] = 208] = "ALREADY_REPORTED";
84
- /**
85
- * The server has fulfilled a request for the resource, and the response represents the result of one or more instance-manipulations.
86
- */
87
- HttpStatusCodes[HttpStatusCodes["IM_USED"] = 226] = "IM_USED";
88
- // 3xx Redirection
89
- /**
90
- * The request has more than one possible response and the client should choose one.
91
- */
92
- HttpStatusCodes[HttpStatusCodes["MULTIPLE_CHOICES"] = 300] = "MULTIPLE_CHOICES";
93
- /**
94
- * The resource has been permanently moved to a new URL.
95
- */
96
- HttpStatusCodes[HttpStatusCodes["MOVED_PERMANENTLY"] = 301] = "MOVED_PERMANENTLY";
97
- /**
98
- * The resource has been temporarily moved to another URL.
99
- */
100
- HttpStatusCodes[HttpStatusCodes["MOVED_TEMPORARILY"] = 302] = "MOVED_TEMPORARILY";
101
- /**
102
- * The response to the request can be found under another URI using the GET method.
103
- */
104
- HttpStatusCodes[HttpStatusCodes["SEE_OTHER"] = 303] = "SEE_OTHER";
105
- /**
106
- * The resource hasn't been modified since the last request.
107
- */
108
- HttpStatusCodes[HttpStatusCodes["NOT_MODIFIED"] = 304] = "NOT_MODIFIED";
109
- /**
110
- * @deprecated
111
- * Response must be accessed through a proxy.
112
- */
113
- HttpStatusCodes[HttpStatusCodes["USE_PROXY"] = 305] = "USE_PROXY";
114
- /**
115
- * The resource is temporarily available at a different URI, using the same HTTP method.
116
- */
117
- HttpStatusCodes[HttpStatusCodes["TEMPORARY_REDIRECT"] = 307] = "TEMPORARY_REDIRECT";
118
- /**
119
- * The resource has been permanently moved to another URL, using the same HTTP method.
120
- */
121
- HttpStatusCodes[HttpStatusCodes["PERMANENT_REDIRECT"] = 308] = "PERMANENT_REDIRECT";
122
- // 4xx Client Error
123
- /**
124
- * The server cannot process the request due to a client error (malformed request).
125
- */
126
- HttpStatusCodes[HttpStatusCodes["BAD_REQUEST"] = 400] = "BAD_REQUEST";
127
- /**
128
- * Authentication is required and has failed or not been provided.
129
- */
130
- HttpStatusCodes[HttpStatusCodes["UNAUTHORIZED"] = 401] = "UNAUTHORIZED";
131
- /**
132
- * Reserved for future use, originally intended for digital payment systems.
133
- */
134
- HttpStatusCodes[HttpStatusCodes["PAYMENT_REQUIRED"] = 402] = "PAYMENT_REQUIRED";
135
- /**
136
- * The client doesn't have permission to access the requested resource.
137
- */
138
- HttpStatusCodes[HttpStatusCodes["FORBIDDEN"] = 403] = "FORBIDDEN";
139
- /**
140
- * The requested resource could not be found on the server.
141
- */
142
- HttpStatusCodes[HttpStatusCodes["NOT_FOUND"] = 404] = "NOT_FOUND";
143
- /**
144
- * The request method is not supported for the requested resource.
145
- */
146
- HttpStatusCodes[HttpStatusCodes["METHOD_NOT_ALLOWED"] = 405] = "METHOD_NOT_ALLOWED";
147
- /**
148
- * The server can't provide a response matching the list of acceptable values in the client's headers.
149
- */
150
- HttpStatusCodes[HttpStatusCodes["NOT_ACCEPTABLE"] = 406] = "NOT_ACCEPTABLE";
151
- /**
152
- * Authentication by a proxy is required.
153
- */
154
- HttpStatusCodes[HttpStatusCodes["PROXY_AUTHENTICATION_REQUIRED"] = 407] = "PROXY_AUTHENTICATION_REQUIRED";
155
- /**
156
- * The server timed out waiting for the request.
157
- */
158
- HttpStatusCodes[HttpStatusCodes["REQUEST_TIMEOUT"] = 408] = "REQUEST_TIMEOUT";
159
- /**
160
- * The request conflicts with the current state of the server.
161
- */
162
- HttpStatusCodes[HttpStatusCodes["CONFLICT"] = 409] = "CONFLICT";
163
- /**
164
- * The resource has been permanently deleted with no forwarding address.
165
- */
166
- HttpStatusCodes[HttpStatusCodes["GONE"] = 410] = "GONE";
167
- /**
168
- * The server requires a Content-Length header field.
169
- */
170
- HttpStatusCodes[HttpStatusCodes["LENGTH_REQUIRED"] = 411] = "LENGTH_REQUIRED";
171
- /**
172
- * Preconditions in the client's headers failed.
173
- */
174
- HttpStatusCodes[HttpStatusCodes["PRECONDITION_FAILED"] = 412] = "PRECONDITION_FAILED";
175
- /**
176
- * Request entity is larger than limits defined by server.
177
- */
178
- HttpStatusCodes[HttpStatusCodes["REQUEST_TOO_LONG"] = 413] = "REQUEST_TOO_LONG";
179
- /**
180
- * The request payload exceeds server size limits.
181
- * Modern equivalent of REQUEST_TOO_LONG.
182
- */
183
- HttpStatusCodes[HttpStatusCodes["PAYLOAD_TOO_LARGE"] = 413] = "PAYLOAD_TOO_LARGE";
184
- /**
185
- * The URI requested is too long for the server to interpret.
186
- */
187
- HttpStatusCodes[HttpStatusCodes["REQUEST_URI_TOO_LONG"] = 414] = "REQUEST_URI_TOO_LONG";
188
- /**
189
- * The server does not support the media type requested by the client.
190
- */
191
- HttpStatusCodes[HttpStatusCodes["UNSUPPORTED_MEDIA_TYPE"] = 415] = "UNSUPPORTED_MEDIA_TYPE";
192
- /**
193
- * The range specified by the Range header cannot be fulfilled.
194
- */
195
- HttpStatusCodes[HttpStatusCodes["REQUESTED_RANGE_NOT_SATISFIABLE"] = 416] = "REQUESTED_RANGE_NOT_SATISFIABLE";
196
- /**
197
- * The server cannot meet the expectations given in the Expect header.
198
- */
199
- HttpStatusCodes[HttpStatusCodes["EXPECTATION_FAILED"] = 417] = "EXPECTATION_FAILED";
200
- /**
201
- * The server refuses to brew coffee because it is a teapot. (April Fools' joke)
202
- */
203
- HttpStatusCodes[HttpStatusCodes["IM_A_TEAPOT"] = 418] = "IM_A_TEAPOT";
204
- /**
205
- * The server cannot store the representation needed to complete the request.
206
- */
207
- HttpStatusCodes[HttpStatusCodes["INSUFFICIENT_SPACE_ON_RESOURCE"] = 419] = "INSUFFICIENT_SPACE_ON_RESOURCE";
208
- /**
209
- * @deprecated
210
- * A method has failed - specific to Spring Framework.
211
- */
212
- HttpStatusCodes[HttpStatusCodes["METHOD_FAILURE"] = 420] = "METHOD_FAILURE";
213
- /**
214
- * The server cannot produce a response for the requested URI scheme and authority.
215
- */
216
- HttpStatusCodes[HttpStatusCodes["MISDIRECTED_REQUEST"] = 421] = "MISDIRECTED_REQUEST";
217
- /**
218
- * The request was well-formed but has semantic errors preventing processing.
219
- */
220
- HttpStatusCodes[HttpStatusCodes["UNPROCESSABLE_ENTITY"] = 422] = "UNPROCESSABLE_ENTITY";
221
- /**
222
- * The requested resource is locked against access.
223
- */
224
- HttpStatusCodes[HttpStatusCodes["LOCKED"] = 423] = "LOCKED";
225
- /**
226
- * The request failed due to the failure of a previous request.
227
- */
228
- HttpStatusCodes[HttpStatusCodes["FAILED_DEPENDENCY"] = 424] = "FAILED_DEPENDENCY";
229
- /**
230
- * The client should switch to a different protocol.
231
- */
232
- HttpStatusCodes[HttpStatusCodes["UPGRADE_REQUIRED"] = 426] = "UPGRADE_REQUIRED";
233
- /**
234
- * The server requires conditional requests to prevent lost updates.
235
- */
236
- HttpStatusCodes[HttpStatusCodes["PRECONDITION_REQUIRED"] = 428] = "PRECONDITION_REQUIRED";
237
- /**
238
- * The client has sent too many requests in a given time period (rate limiting).
239
- */
240
- HttpStatusCodes[HttpStatusCodes["TOO_MANY_REQUESTS"] = 429] = "TOO_MANY_REQUESTS";
241
- /**
242
- * Request headers are too large for the server to process.
243
- */
244
- HttpStatusCodes[HttpStatusCodes["REQUEST_HEADER_FIELDS_TOO_LARGE"] = 431] = "REQUEST_HEADER_FIELDS_TOO_LARGE";
245
- /**
246
- * Resource cannot be legally provided (e.g., censored by government).
247
- */
248
- HttpStatusCodes[HttpStatusCodes["UNAVAILABLE_FOR_LEGAL_REASONS"] = 451] = "UNAVAILABLE_FOR_LEGAL_REASONS";
249
- // 5xx Server Error
250
- /**
251
- * The server encountered an unexpected error preventing it from fulfilling the request.
252
- */
253
- HttpStatusCodes[HttpStatusCodes["INTERNAL_SERVER_ERROR"] = 500] = "INTERNAL_SERVER_ERROR";
254
- /**
255
- * The server does not support the functionality required to fulfill the request.
256
- */
257
- HttpStatusCodes[HttpStatusCodes["NOT_IMPLEMENTED"] = 501] = "NOT_IMPLEMENTED";
258
- /**
259
- * The server, acting as a gateway, received an invalid response from an upstream server.
260
- */
261
- HttpStatusCodes[HttpStatusCodes["BAD_GATEWAY"] = 502] = "BAD_GATEWAY";
262
- /**
263
- * The server is currently unable to handle the request due to temporary overload or maintenance.
264
- */
265
- HttpStatusCodes[HttpStatusCodes["SERVICE_UNAVAILABLE"] = 503] = "SERVICE_UNAVAILABLE";
266
- /**
267
- * The server, acting as a gateway, did not receive a timely response from an upstream server.
268
- */
269
- HttpStatusCodes[HttpStatusCodes["GATEWAY_TIMEOUT"] = 504] = "GATEWAY_TIMEOUT";
270
- /**
271
- * The server does not support the HTTP protocol version used in the request.
272
- */
273
- HttpStatusCodes[HttpStatusCodes["HTTP_VERSION_NOT_SUPPORTED"] = 505] = "HTTP_VERSION_NOT_SUPPORTED";
274
- /**
275
- * Transparent content negotiation for the request resulted in circular reference.
276
- */
277
- HttpStatusCodes[HttpStatusCodes["VARIANT_ALSO_NEGOTIATES"] = 506] = "VARIANT_ALSO_NEGOTIATES";
278
- /**
279
- * The server cannot store the representation needed to complete the request.
280
- */
281
- HttpStatusCodes[HttpStatusCodes["INSUFFICIENT_STORAGE"] = 507] = "INSUFFICIENT_STORAGE";
282
- /**
283
- * The server detected an infinite loop while processing the request.
284
- */
285
- HttpStatusCodes[HttpStatusCodes["LOOP_DETECTED"] = 508] = "LOOP_DETECTED";
286
- /**
287
- * Further extensions to the request are required for the server to fulfill it.
288
- */
289
- HttpStatusCodes[HttpStatusCodes["NOT_EXTENDED"] = 510] = "NOT_EXTENDED";
290
- /**
291
- * The client needs to authenticate to gain network access.
292
- */
293
- HttpStatusCodes[HttpStatusCodes["NETWORK_AUTHENTICATION_REQUIRED"] = 511] = "NETWORK_AUTHENTICATION_REQUIRED";
294
- })(HttpStatusCodes || (HttpStatusCodes = {}));
295
- //# sourceMappingURL=http-status-codes.js.map