@catbee/utils 0.0.7 → 0.0.8-next.0

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 (285) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +26 -1
  3. package/build/index.cjs +6888 -0
  4. package/build/index.d.ts +5242 -0
  5. package/build/index.mjs +6615 -0
  6. package/package.json +26 -99
  7. package/build/esm/config.d.ts +0 -122
  8. package/build/esm/config.js +0 -133
  9. package/build/esm/config.js.map +0 -1
  10. package/build/esm/index.d.ts +0 -27
  11. package/build/esm/index.js +0 -50
  12. package/build/esm/index.js.map +0 -1
  13. package/build/esm/servers/server.builder.d.ts +0 -508
  14. package/build/esm/servers/server.builder.js +0 -702
  15. package/build/esm/servers/server.builder.js.map +0 -1
  16. package/build/esm/servers/server.d.ts +0 -256
  17. package/build/esm/servers/server.js +0 -1201
  18. package/build/esm/servers/server.js.map +0 -1
  19. package/build/esm/types/api-response.d.ts +0 -152
  20. package/build/esm/types/api-response.js +0 -34
  21. package/build/esm/types/api-response.js.map +0 -1
  22. package/build/esm/types/index.d.ts +0 -125
  23. package/build/esm/types/index.js +0 -25
  24. package/build/esm/types/index.js.map +0 -1
  25. package/build/esm/types/server.d.ts +0 -268
  26. package/build/esm/types/server.js +0 -25
  27. package/build/esm/types/server.js.map +0 -1
  28. package/build/esm/utils/array.utils.d.ts +0 -168
  29. package/build/esm/utils/array.utils.js +0 -382
  30. package/build/esm/utils/array.utils.js.map +0 -1
  31. package/build/esm/utils/async.utils.d.ts +0 -265
  32. package/build/esm/utils/async.utils.js +0 -896
  33. package/build/esm/utils/async.utils.js.map +0 -1
  34. package/build/esm/utils/cache.utils.d.ts +0 -153
  35. package/build/esm/utils/cache.utils.js +0 -499
  36. package/build/esm/utils/cache.utils.js.map +0 -1
  37. package/build/esm/utils/context-store.utils.d.ts +0 -189
  38. package/build/esm/utils/context-store.utils.js +0 -307
  39. package/build/esm/utils/context-store.utils.js.map +0 -1
  40. package/build/esm/utils/crypto.utils.d.ts +0 -160
  41. package/build/esm/utils/crypto.utils.js +0 -375
  42. package/build/esm/utils/crypto.utils.js.map +0 -1
  43. package/build/esm/utils/date.utils.d.ts +0 -159
  44. package/build/esm/utils/date.utils.js +0 -389
  45. package/build/esm/utils/date.utils.js.map +0 -1
  46. package/build/esm/utils/decorators.utils.d.ts +0 -315
  47. package/build/esm/utils/decorators.utils.js +0 -666
  48. package/build/esm/utils/decorators.utils.js.map +0 -1
  49. package/build/esm/utils/dir.utils.d.ts +0 -196
  50. package/build/esm/utils/dir.utils.js +0 -998
  51. package/build/esm/utils/dir.utils.js.map +0 -1
  52. package/build/esm/utils/env.utils.d.ts +0 -377
  53. package/build/esm/utils/env.utils.js +0 -873
  54. package/build/esm/utils/env.utils.js.map +0 -1
  55. package/build/esm/utils/exception.utils.d.ts +0 -230
  56. package/build/esm/utils/exception.utils.js +0 -559
  57. package/build/esm/utils/exception.utils.js.map +0 -1
  58. package/build/esm/utils/fs.utils.d.ts +0 -164
  59. package/build/esm/utils/fs.utils.js +0 -580
  60. package/build/esm/utils/fs.utils.js.map +0 -1
  61. package/build/esm/utils/http-status-codes.d.ts +0 -266
  62. package/build/esm/utils/http-status-codes.js +0 -295
  63. package/build/esm/utils/http-status-codes.js.map +0 -1
  64. package/build/esm/utils/id.utils.d.ts +0 -36
  65. package/build/esm/utils/id.utils.js +0 -87
  66. package/build/esm/utils/id.utils.js.map +0 -1
  67. package/build/esm/utils/logger.utils.d.ts +0 -160
  68. package/build/esm/utils/logger.utils.js +0 -341
  69. package/build/esm/utils/logger.utils.js.map +0 -1
  70. package/build/esm/utils/middleware.utils.d.ts +0 -83
  71. package/build/esm/utils/middleware.utils.js +0 -206
  72. package/build/esm/utils/middleware.utils.js.map +0 -1
  73. package/build/esm/utils/obj.utils.d.ts +0 -124
  74. package/build/esm/utils/obj.utils.js +0 -519
  75. package/build/esm/utils/obj.utils.js.map +0 -1
  76. package/build/esm/utils/performance.utils.d.ts +0 -136
  77. package/build/esm/utils/performance.utils.js +0 -376
  78. package/build/esm/utils/performance.utils.js.map +0 -1
  79. package/build/esm/utils/request.utils.d.ts +0 -86
  80. package/build/esm/utils/request.utils.js +0 -223
  81. package/build/esm/utils/request.utils.js.map +0 -1
  82. package/build/esm/utils/response.utils.d.ts +0 -163
  83. package/build/esm/utils/response.utils.js +0 -284
  84. package/build/esm/utils/response.utils.js.map +0 -1
  85. package/build/esm/utils/stream.utils.d.ts +0 -88
  86. package/build/esm/utils/stream.utils.js +0 -284
  87. package/build/esm/utils/stream.utils.js.map +0 -1
  88. package/build/esm/utils/string.utils.d.ts +0 -93
  89. package/build/esm/utils/string.utils.js +0 -169
  90. package/build/esm/utils/string.utils.js.map +0 -1
  91. package/build/esm/utils/type.utils.d.ts +0 -90
  92. package/build/esm/utils/type.utils.js +0 -190
  93. package/build/esm/utils/type.utils.js.map +0 -1
  94. package/build/esm/utils/url.utils.d.ts +0 -141
  95. package/build/esm/utils/url.utils.js +0 -381
  96. package/build/esm/utils/url.utils.js.map +0 -1
  97. package/build/esm/utils/validate.utils.d.ts +0 -177
  98. package/build/esm/utils/validate.utils.js +0 -336
  99. package/build/esm/utils/validate.utils.js.map +0 -1
  100. package/build/esnext/config.d.ts +0 -122
  101. package/build/esnext/config.js +0 -133
  102. package/build/esnext/config.js.map +0 -1
  103. package/build/esnext/index.d.ts +0 -27
  104. package/build/esnext/index.js +0 -50
  105. package/build/esnext/index.js.map +0 -1
  106. package/build/esnext/servers/server.builder.d.ts +0 -508
  107. package/build/esnext/servers/server.builder.js +0 -657
  108. package/build/esnext/servers/server.builder.js.map +0 -1
  109. package/build/esnext/servers/server.d.ts +0 -256
  110. package/build/esnext/servers/server.js +0 -908
  111. package/build/esnext/servers/server.js.map +0 -1
  112. package/build/esnext/types/api-response.d.ts +0 -152
  113. package/build/esnext/types/api-response.js +0 -34
  114. package/build/esnext/types/api-response.js.map +0 -1
  115. package/build/esnext/types/index.d.ts +0 -125
  116. package/build/esnext/types/index.js +0 -25
  117. package/build/esnext/types/index.js.map +0 -1
  118. package/build/esnext/types/server.d.ts +0 -268
  119. package/build/esnext/types/server.js +0 -25
  120. package/build/esnext/types/server.js.map +0 -1
  121. package/build/esnext/utils/array.utils.d.ts +0 -168
  122. package/build/esnext/utils/array.utils.js +0 -345
  123. package/build/esnext/utils/array.utils.js.map +0 -1
  124. package/build/esnext/utils/async.utils.d.ts +0 -265
  125. package/build/esnext/utils/async.utils.js +0 -620
  126. package/build/esnext/utils/async.utils.js.map +0 -1
  127. package/build/esnext/utils/cache.utils.d.ts +0 -153
  128. package/build/esnext/utils/cache.utils.js +0 -293
  129. package/build/esnext/utils/cache.utils.js.map +0 -1
  130. package/build/esnext/utils/context-store.utils.d.ts +0 -189
  131. package/build/esnext/utils/context-store.utils.js +0 -289
  132. package/build/esnext/utils/context-store.utils.js.map +0 -1
  133. package/build/esnext/utils/crypto.utils.d.ts +0 -160
  134. package/build/esnext/utils/crypto.utils.js +0 -276
  135. package/build/esnext/utils/crypto.utils.js.map +0 -1
  136. package/build/esnext/utils/date.utils.d.ts +0 -159
  137. package/build/esnext/utils/date.utils.js +0 -384
  138. package/build/esnext/utils/date.utils.js.map +0 -1
  139. package/build/esnext/utils/decorators.utils.d.ts +0 -315
  140. package/build/esnext/utils/decorators.utils.js +0 -504
  141. package/build/esnext/utils/decorators.utils.js.map +0 -1
  142. package/build/esnext/utils/dir.utils.d.ts +0 -196
  143. package/build/esnext/utils/dir.utils.js +0 -477
  144. package/build/esnext/utils/dir.utils.js.map +0 -1
  145. package/build/esnext/utils/env.utils.d.ts +0 -377
  146. package/build/esnext/utils/env.utils.js +0 -785
  147. package/build/esnext/utils/env.utils.js.map +0 -1
  148. package/build/esnext/utils/exception.utils.d.ts +0 -230
  149. package/build/esnext/utils/exception.utils.js +0 -381
  150. package/build/esnext/utils/exception.utils.js.map +0 -1
  151. package/build/esnext/utils/fs.utils.d.ts +0 -164
  152. package/build/esnext/utils/fs.utils.js +0 -349
  153. package/build/esnext/utils/fs.utils.js.map +0 -1
  154. package/build/esnext/utils/http-status-codes.d.ts +0 -266
  155. package/build/esnext/utils/http-status-codes.js +0 -295
  156. package/build/esnext/utils/http-status-codes.js.map +0 -1
  157. package/build/esnext/utils/id.utils.d.ts +0 -36
  158. package/build/esnext/utils/id.utils.js +0 -84
  159. package/build/esnext/utils/id.utils.js.map +0 -1
  160. package/build/esnext/utils/logger.utils.d.ts +0 -160
  161. package/build/esnext/utils/logger.utils.js +0 -300
  162. package/build/esnext/utils/logger.utils.js.map +0 -1
  163. package/build/esnext/utils/middleware.utils.d.ts +0 -83
  164. package/build/esnext/utils/middleware.utils.js +0 -199
  165. package/build/esnext/utils/middleware.utils.js.map +0 -1
  166. package/build/esnext/utils/obj.utils.d.ts +0 -124
  167. package/build/esnext/utils/obj.utils.js +0 -413
  168. package/build/esnext/utils/obj.utils.js.map +0 -1
  169. package/build/esnext/utils/performance.utils.d.ts +0 -136
  170. package/build/esnext/utils/performance.utils.js +0 -271
  171. package/build/esnext/utils/performance.utils.js.map +0 -1
  172. package/build/esnext/utils/request.utils.d.ts +0 -86
  173. package/build/esnext/utils/request.utils.js +0 -186
  174. package/build/esnext/utils/request.utils.js.map +0 -1
  175. package/build/esnext/utils/response.utils.d.ts +0 -163
  176. package/build/esnext/utils/response.utils.js +0 -247
  177. package/build/esnext/utils/response.utils.js.map +0 -1
  178. package/build/esnext/utils/stream.utils.d.ts +0 -88
  179. package/build/esnext/utils/stream.utils.js +0 -210
  180. package/build/esnext/utils/stream.utils.js.map +0 -1
  181. package/build/esnext/utils/string.utils.d.ts +0 -93
  182. package/build/esnext/utils/string.utils.js +0 -165
  183. package/build/esnext/utils/string.utils.js.map +0 -1
  184. package/build/esnext/utils/type.utils.d.ts +0 -90
  185. package/build/esnext/utils/type.utils.js +0 -187
  186. package/build/esnext/utils/type.utils.js.map +0 -1
  187. package/build/esnext/utils/url.utils.d.ts +0 -141
  188. package/build/esnext/utils/url.utils.js +0 -304
  189. package/build/esnext/utils/url.utils.js.map +0 -1
  190. package/build/esnext/utils/validate.utils.d.ts +0 -177
  191. package/build/esnext/utils/validate.utils.js +0 -320
  192. package/build/esnext/utils/validate.utils.js.map +0 -1
  193. package/build/src/config.d.ts +0 -122
  194. package/build/src/config.js +0 -138
  195. package/build/src/config.js.map +0 -1
  196. package/build/src/index.d.ts +0 -27
  197. package/build/src/index.js +0 -69
  198. package/build/src/index.js.map +0 -1
  199. package/build/src/servers/server.builder.d.ts +0 -508
  200. package/build/src/servers/server.builder.js +0 -661
  201. package/build/src/servers/server.builder.js.map +0 -1
  202. package/build/src/servers/server.d.ts +0 -256
  203. package/build/src/servers/server.js +0 -948
  204. package/build/src/servers/server.js.map +0 -1
  205. package/build/src/types/api-response.d.ts +0 -152
  206. package/build/src/types/api-response.js +0 -37
  207. package/build/src/types/api-response.js.map +0 -1
  208. package/build/src/types/index.d.ts +0 -125
  209. package/build/src/types/index.js +0 -26
  210. package/build/src/types/index.js.map +0 -1
  211. package/build/src/types/server.d.ts +0 -268
  212. package/build/src/types/server.js +0 -26
  213. package/build/src/types/server.js.map +0 -1
  214. package/build/src/utils/array.utils.d.ts +0 -168
  215. package/build/src/utils/array.utils.js +0 -364
  216. package/build/src/utils/array.utils.js.map +0 -1
  217. package/build/src/utils/async.utils.d.ts +0 -265
  218. package/build/src/utils/async.utils.js +0 -641
  219. package/build/src/utils/async.utils.js.map +0 -1
  220. package/build/src/utils/cache.utils.d.ts +0 -153
  221. package/build/src/utils/cache.utils.js +0 -297
  222. package/build/src/utils/cache.utils.js.map +0 -1
  223. package/build/src/utils/context-store.utils.d.ts +0 -189
  224. package/build/src/utils/context-store.utils.js +0 -296
  225. package/build/src/utils/context-store.utils.js.map +0 -1
  226. package/build/src/utils/crypto.utils.d.ts +0 -160
  227. package/build/src/utils/crypto.utils.js +0 -293
  228. package/build/src/utils/crypto.utils.js.map +0 -1
  229. package/build/src/utils/date.utils.d.ts +0 -159
  230. package/build/src/utils/date.utils.js +0 -396
  231. package/build/src/utils/date.utils.js.map +0 -1
  232. package/build/src/utils/decorators.utils.d.ts +0 -315
  233. package/build/src/utils/decorators.utils.js +0 -516
  234. package/build/src/utils/decorators.utils.js.map +0 -1
  235. package/build/src/utils/dir.utils.d.ts +0 -196
  236. package/build/src/utils/dir.utils.js +0 -501
  237. package/build/src/utils/dir.utils.js.map +0 -1
  238. package/build/src/utils/env.utils.d.ts +0 -377
  239. package/build/src/utils/env.utils.js +0 -789
  240. package/build/src/utils/env.utils.js.map +0 -1
  241. package/build/src/utils/exception.utils.d.ts +0 -230
  242. package/build/src/utils/exception.utils.js +0 -407
  243. package/build/src/utils/exception.utils.js.map +0 -1
  244. package/build/src/utils/fs.utils.d.ts +0 -164
  245. package/build/src/utils/fs.utils.js +0 -372
  246. package/build/src/utils/fs.utils.js.map +0 -1
  247. package/build/src/utils/http-status-codes.d.ts +0 -266
  248. package/build/src/utils/http-status-codes.js +0 -298
  249. package/build/src/utils/http-status-codes.js.map +0 -1
  250. package/build/src/utils/id.utils.d.ts +0 -36
  251. package/build/src/utils/id.utils.js +0 -91
  252. package/build/src/utils/id.utils.js.map +0 -1
  253. package/build/src/utils/logger.utils.d.ts +0 -160
  254. package/build/src/utils/logger.utils.js +0 -346
  255. package/build/src/utils/logger.utils.js.map +0 -1
  256. package/build/src/utils/middleware.utils.d.ts +0 -83
  257. package/build/src/utils/middleware.utils.js +0 -206
  258. package/build/src/utils/middleware.utils.js.map +0 -1
  259. package/build/src/utils/obj.utils.d.ts +0 -124
  260. package/build/src/utils/obj.utils.js +0 -429
  261. package/build/src/utils/obj.utils.js.map +0 -1
  262. package/build/src/utils/performance.utils.d.ts +0 -136
  263. package/build/src/utils/performance.utils.js +0 -278
  264. package/build/src/utils/performance.utils.js.map +0 -1
  265. package/build/src/utils/request.utils.d.ts +0 -86
  266. package/build/src/utils/request.utils.js +0 -195
  267. package/build/src/utils/request.utils.js.map +0 -1
  268. package/build/src/utils/response.utils.d.ts +0 -163
  269. package/build/src/utils/response.utils.js +0 -260
  270. package/build/src/utils/response.utils.js.map +0 -1
  271. package/build/src/utils/stream.utils.d.ts +0 -88
  272. package/build/src/utils/stream.utils.js +0 -218
  273. package/build/src/utils/stream.utils.js.map +0 -1
  274. package/build/src/utils/string.utils.d.ts +0 -93
  275. package/build/src/utils/string.utils.js +0 -179
  276. package/build/src/utils/string.utils.js.map +0 -1
  277. package/build/src/utils/type.utils.d.ts +0 -90
  278. package/build/src/utils/type.utils.js +0 -196
  279. package/build/src/utils/type.utils.js.map +0 -1
  280. package/build/src/utils/url.utils.d.ts +0 -141
  281. package/build/src/utils/url.utils.js +0 -317
  282. package/build/src/utils/url.utils.js.map +0 -1
  283. package/build/src/utils/validate.utils.d.ts +0 -177
  284. package/build/src/utils/validate.utils.js +0 -345
  285. 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
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,iBAAiB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAEpC;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,IAAY;IAC3C,IAAI,CAAC;QACH,MAAM,EAAE,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;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAAU,IAAY;IACtD,IAAI,CAA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/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\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,266 +0,0 @@
1
- /**
2
- * Complete HTTP status codes enum with descriptions.
3
- * For use with API responses and error handling.
4
- */
5
- export declare enum HttpStatusCodes {
6
- /**
7
- * The server has received the request headers and the client should proceed to send the request body.
8
- */
9
- CONTINUE = 100,
10
- /**
11
- * The server is switching protocols according to the client's request.
12
- */
13
- SWITCHING_PROTOCOLS = 101,
14
- /**
15
- * Server has received the request and is still processing it.
16
- */
17
- PROCESSING = 102,
18
- /**
19
- * The server is sending preliminary headers while preparing the final response.
20
- */
21
- EARLY_HINTS = 103,
22
- /**
23
- * The request succeeded. The result meaning depends on the HTTP method used.
24
- */
25
- OK = 200,
26
- /**
27
- * The request succeeded and a new resource was created as a result.
28
- */
29
- CREATED = 201,
30
- /**
31
- * The request has been received but not yet acted upon.
32
- */
33
- ACCEPTED = 202,
34
- /**
35
- * The returned information is from a third-party source, not the origin server.
36
- */
37
- NON_AUTHORITATIVE_INFORMATION = 203,
38
- /**
39
- * The request succeeded but there's no content to send in the response.
40
- */
41
- NO_CONTENT = 204,
42
- /**
43
- * The client should reset the document view that caused this request.
44
- */
45
- RESET_CONTENT = 205,
46
- /**
47
- * The server is delivering only part of the resource due to a range header sent by the client.
48
- */
49
- PARTIAL_CONTENT = 206,
50
- /**
51
- * Conveys information about multiple resources, for complex operations.
52
- */
53
- MULTI_STATUS = 207,
54
- /**
55
- * The members of a DAV binding have already been enumerated and are not included in the response.
56
- */
57
- ALREADY_REPORTED = 208,
58
- /**
59
- * The server has fulfilled a request for the resource, and the response represents the result of one or more instance-manipulations.
60
- */
61
- IM_USED = 226,
62
- /**
63
- * The request has more than one possible response and the client should choose one.
64
- */
65
- MULTIPLE_CHOICES = 300,
66
- /**
67
- * The resource has been permanently moved to a new URL.
68
- */
69
- MOVED_PERMANENTLY = 301,
70
- /**
71
- * The resource has been temporarily moved to another URL.
72
- */
73
- MOVED_TEMPORARILY = 302,
74
- /**
75
- * The response to the request can be found under another URI using the GET method.
76
- */
77
- SEE_OTHER = 303,
78
- /**
79
- * The resource hasn't been modified since the last request.
80
- */
81
- NOT_MODIFIED = 304,
82
- /**
83
- * @deprecated
84
- * Response must be accessed through a proxy.
85
- */
86
- USE_PROXY = 305,
87
- /**
88
- * The resource is temporarily available at a different URI, using the same HTTP method.
89
- */
90
- TEMPORARY_REDIRECT = 307,
91
- /**
92
- * The resource has been permanently moved to another URL, using the same HTTP method.
93
- */
94
- PERMANENT_REDIRECT = 308,
95
- /**
96
- * The server cannot process the request due to a client error (malformed request).
97
- */
98
- BAD_REQUEST = 400,
99
- /**
100
- * Authentication is required and has failed or not been provided.
101
- */
102
- UNAUTHORIZED = 401,
103
- /**
104
- * Reserved for future use, originally intended for digital payment systems.
105
- */
106
- PAYMENT_REQUIRED = 402,
107
- /**
108
- * The client doesn't have permission to access the requested resource.
109
- */
110
- FORBIDDEN = 403,
111
- /**
112
- * The requested resource could not be found on the server.
113
- */
114
- NOT_FOUND = 404,
115
- /**
116
- * The request method is not supported for the requested resource.
117
- */
118
- METHOD_NOT_ALLOWED = 405,
119
- /**
120
- * The server can't provide a response matching the list of acceptable values in the client's headers.
121
- */
122
- NOT_ACCEPTABLE = 406,
123
- /**
124
- * Authentication by a proxy is required.
125
- */
126
- PROXY_AUTHENTICATION_REQUIRED = 407,
127
- /**
128
- * The server timed out waiting for the request.
129
- */
130
- REQUEST_TIMEOUT = 408,
131
- /**
132
- * The request conflicts with the current state of the server.
133
- */
134
- CONFLICT = 409,
135
- /**
136
- * The resource has been permanently deleted with no forwarding address.
137
- */
138
- GONE = 410,
139
- /**
140
- * The server requires a Content-Length header field.
141
- */
142
- LENGTH_REQUIRED = 411,
143
- /**
144
- * Preconditions in the client's headers failed.
145
- */
146
- PRECONDITION_FAILED = 412,
147
- /**
148
- * Request entity is larger than limits defined by server.
149
- */
150
- REQUEST_TOO_LONG = 413,
151
- /**
152
- * The request payload exceeds server size limits.
153
- * Modern equivalent of REQUEST_TOO_LONG.
154
- */
155
- PAYLOAD_TOO_LARGE = 413,
156
- /**
157
- * The URI requested is too long for the server to interpret.
158
- */
159
- REQUEST_URI_TOO_LONG = 414,
160
- /**
161
- * The server does not support the media type requested by the client.
162
- */
163
- UNSUPPORTED_MEDIA_TYPE = 415,
164
- /**
165
- * The range specified by the Range header cannot be fulfilled.
166
- */
167
- REQUESTED_RANGE_NOT_SATISFIABLE = 416,
168
- /**
169
- * The server cannot meet the expectations given in the Expect header.
170
- */
171
- EXPECTATION_FAILED = 417,
172
- /**
173
- * The server refuses to brew coffee because it is a teapot. (April Fools' joke)
174
- */
175
- IM_A_TEAPOT = 418,
176
- /**
177
- * The server cannot store the representation needed to complete the request.
178
- */
179
- INSUFFICIENT_SPACE_ON_RESOURCE = 419,
180
- /**
181
- * @deprecated
182
- * A method has failed - specific to Spring Framework.
183
- */
184
- METHOD_FAILURE = 420,
185
- /**
186
- * The server cannot produce a response for the requested URI scheme and authority.
187
- */
188
- MISDIRECTED_REQUEST = 421,
189
- /**
190
- * The request was well-formed but has semantic errors preventing processing.
191
- */
192
- UNPROCESSABLE_ENTITY = 422,
193
- /**
194
- * The requested resource is locked against access.
195
- */
196
- LOCKED = 423,
197
- /**
198
- * The request failed due to the failure of a previous request.
199
- */
200
- FAILED_DEPENDENCY = 424,
201
- /**
202
- * The client should switch to a different protocol.
203
- */
204
- UPGRADE_REQUIRED = 426,
205
- /**
206
- * The server requires conditional requests to prevent lost updates.
207
- */
208
- PRECONDITION_REQUIRED = 428,
209
- /**
210
- * The client has sent too many requests in a given time period (rate limiting).
211
- */
212
- TOO_MANY_REQUESTS = 429,
213
- /**
214
- * Request headers are too large for the server to process.
215
- */
216
- REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
217
- /**
218
- * Resource cannot be legally provided (e.g., censored by government).
219
- */
220
- UNAVAILABLE_FOR_LEGAL_REASONS = 451,
221
- /**
222
- * The server encountered an unexpected error preventing it from fulfilling the request.
223
- */
224
- INTERNAL_SERVER_ERROR = 500,
225
- /**
226
- * The server does not support the functionality required to fulfill the request.
227
- */
228
- NOT_IMPLEMENTED = 501,
229
- /**
230
- * The server, acting as a gateway, received an invalid response from an upstream server.
231
- */
232
- BAD_GATEWAY = 502,
233
- /**
234
- * The server is currently unable to handle the request due to temporary overload or maintenance.
235
- */
236
- SERVICE_UNAVAILABLE = 503,
237
- /**
238
- * The server, acting as a gateway, did not receive a timely response from an upstream server.
239
- */
240
- GATEWAY_TIMEOUT = 504,
241
- /**
242
- * The server does not support the HTTP protocol version used in the request.
243
- */
244
- HTTP_VERSION_NOT_SUPPORTED = 505,
245
- /**
246
- * Transparent content negotiation for the request resulted in circular reference.
247
- */
248
- VARIANT_ALSO_NEGOTIATES = 506,
249
- /**
250
- * The server cannot store the representation needed to complete the request.
251
- */
252
- INSUFFICIENT_STORAGE = 507,
253
- /**
254
- * The server detected an infinite loop while processing the request.
255
- */
256
- LOOP_DETECTED = 508,
257
- /**
258
- * Further extensions to the request are required for the server to fulfill it.
259
- */
260
- NOT_EXTENDED = 510,
261
- /**
262
- * The client needs to authenticate to gain network access.
263
- */
264
- NETWORK_AUTHENTICATION_REQUIRED = 511
265
- }
266
- //# sourceMappingURL=http-status-codes.d.ts.map