@arkyn/server 1.9.1 → 1.9.5

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 (304) hide show
  1. package/LICENSE.txt +24 -0
  2. package/README.md +116 -0
  3. package/dist/api/__test__/deleteRequest.spec.d.ts +2 -0
  4. package/dist/api/__test__/deleteRequest.spec.d.ts.map +1 -0
  5. package/dist/api/__test__/deleteRequest.spec.js +84 -0
  6. package/dist/api/__test__/getRequest.spec.d.ts +2 -0
  7. package/dist/api/__test__/getRequest.spec.d.ts.map +1 -0
  8. package/dist/api/__test__/getRequest.spec.js +62 -0
  9. package/dist/api/__test__/inboxFlowRequest.spec.d.ts +2 -0
  10. package/dist/api/__test__/inboxFlowRequest.spec.d.ts.map +1 -0
  11. package/dist/api/__test__/inboxFlowRequest.spec.js +96 -0
  12. package/dist/api/__test__/makeRequest.spec.d.ts +2 -0
  13. package/dist/api/__test__/makeRequest.spec.d.ts.map +1 -0
  14. package/dist/api/__test__/makeRequest.spec.js +80 -0
  15. package/dist/api/__test__/patchRequest.spec.d.ts +2 -0
  16. package/dist/api/__test__/patchRequest.spec.d.ts.map +1 -0
  17. package/dist/api/__test__/patchRequest.spec.js +84 -0
  18. package/dist/api/__test__/postRequest.spec.d.ts +2 -0
  19. package/dist/api/__test__/postRequest.spec.d.ts.map +1 -0
  20. package/dist/api/__test__/postRequest.spec.js +84 -0
  21. package/dist/api/__test__/putRequest.spec.d.ts +2 -0
  22. package/dist/api/__test__/putRequest.spec.d.ts.map +1 -0
  23. package/dist/api/__test__/putRequest.spec.js +84 -0
  24. package/dist/api/deleteRequest.d.ts +13 -0
  25. package/dist/api/deleteRequest.d.ts.map +1 -0
  26. package/dist/api/deleteRequest.js +14 -0
  27. package/dist/api/getRequest.d.ts +12 -0
  28. package/dist/api/getRequest.d.ts.map +1 -0
  29. package/dist/api/getRequest.js +13 -0
  30. package/dist/api/inboxFlowRequest.d.ts +40 -0
  31. package/dist/api/inboxFlowRequest.d.ts.map +1 -0
  32. package/dist/api/inboxFlowRequest.js +63 -0
  33. package/dist/api/makeRequest.d.ts +38 -0
  34. package/dist/api/makeRequest.d.ts.map +1 -0
  35. package/dist/api/makeRequest.js +103 -0
  36. package/dist/api/patchRequest.d.ts +13 -0
  37. package/dist/api/patchRequest.d.ts.map +1 -0
  38. package/dist/api/patchRequest.js +14 -0
  39. package/dist/api/postRequest.d.ts +13 -0
  40. package/dist/api/postRequest.d.ts.map +1 -0
  41. package/dist/api/postRequest.js +14 -0
  42. package/dist/api/putRequest.d.ts +13 -0
  43. package/dist/api/putRequest.d.ts.map +1 -0
  44. package/dist/api/putRequest.js +14 -0
  45. package/dist/config/__test__/apiInstance.spec.d.ts +2 -0
  46. package/dist/config/__test__/apiInstance.spec.d.ts.map +1 -0
  47. package/dist/config/__test__/apiInstance.spec.js +86 -0
  48. package/dist/config/__test__/inboxFlowInstance.spec.d.ts +2 -0
  49. package/dist/config/__test__/inboxFlowInstance.spec.d.ts.map +1 -0
  50. package/dist/config/__test__/inboxFlowInstance.spec.js +48 -0
  51. package/dist/config/apiInstance.d.ts +80 -0
  52. package/dist/config/apiInstance.d.ts.map +1 -0
  53. package/dist/config/apiInstance.js +111 -0
  54. package/dist/config/inboxFlowInstance.d.ts +44 -0
  55. package/dist/config/inboxFlowInstance.d.ts.map +1 -0
  56. package/dist/config/inboxFlowInstance.js +46 -0
  57. package/dist/http/badResponses/__test__/badGateway.spec.d.ts +2 -0
  58. package/dist/http/badResponses/__test__/badGateway.spec.d.ts.map +1 -0
  59. package/dist/http/badResponses/__test__/badGateway.spec.js +42 -0
  60. package/dist/http/badResponses/__test__/badRequest.spec.d.ts +2 -0
  61. package/dist/http/badResponses/__test__/badRequest.spec.d.ts.map +1 -0
  62. package/dist/http/badResponses/__test__/badRequest.spec.js +40 -0
  63. package/dist/http/badResponses/__test__/conflict.spec.d.ts +2 -0
  64. package/dist/http/badResponses/__test__/conflict.spec.d.ts.map +1 -0
  65. package/dist/http/badResponses/__test__/conflict.spec.js +40 -0
  66. package/dist/http/badResponses/__test__/forbidden.spec.d.ts +2 -0
  67. package/dist/http/badResponses/__test__/forbidden.spec.d.ts.map +1 -0
  68. package/dist/http/badResponses/__test__/forbidden.spec.js +40 -0
  69. package/dist/http/badResponses/__test__/notFound.spec.d.ts +2 -0
  70. package/dist/http/badResponses/__test__/notFound.spec.d.ts.map +1 -0
  71. package/dist/http/badResponses/__test__/notFound.spec.js +40 -0
  72. package/dist/http/badResponses/__test__/notImplemented.spec.d.ts +2 -0
  73. package/dist/http/badResponses/__test__/notImplemented.spec.d.ts.map +1 -0
  74. package/dist/http/badResponses/__test__/notImplemented.spec.js +40 -0
  75. package/dist/http/badResponses/__test__/serverError.spec.d.ts +2 -0
  76. package/dist/http/badResponses/__test__/serverError.spec.d.ts.map +1 -0
  77. package/dist/http/badResponses/__test__/serverError.spec.js +40 -0
  78. package/dist/http/badResponses/__test__/unauthorized.spec.d.ts +2 -0
  79. package/dist/http/badResponses/__test__/unauthorized.spec.d.ts.map +1 -0
  80. package/dist/http/badResponses/__test__/unauthorized.spec.js +40 -0
  81. package/dist/http/badResponses/__test__/unprocessableEntity.spec.d.ts +2 -0
  82. package/dist/http/badResponses/__test__/unprocessableEntity.spec.d.ts.map +1 -0
  83. package/dist/http/badResponses/__test__/unprocessableEntity.spec.js +66 -0
  84. package/dist/http/badResponses/badGateway.d.ts +34 -0
  85. package/dist/http/badResponses/badGateway.d.ts.map +1 -0
  86. package/dist/http/badResponses/badGateway.js +52 -0
  87. package/dist/http/badResponses/badRequest.d.ts +34 -0
  88. package/dist/http/badResponses/badRequest.d.ts.map +1 -0
  89. package/dist/http/badResponses/badRequest.js +52 -0
  90. package/dist/http/badResponses/conflict.d.ts +34 -0
  91. package/dist/http/badResponses/conflict.d.ts.map +1 -0
  92. package/dist/http/badResponses/conflict.js +52 -0
  93. package/dist/http/badResponses/forbidden.d.ts +34 -0
  94. package/dist/http/badResponses/forbidden.d.ts.map +1 -0
  95. package/dist/http/badResponses/forbidden.js +52 -0
  96. package/dist/http/badResponses/notFound.d.ts +34 -0
  97. package/dist/http/badResponses/notFound.d.ts.map +1 -0
  98. package/dist/http/badResponses/notFound.js +52 -0
  99. package/dist/http/badResponses/notImplemented.d.ts +34 -0
  100. package/dist/http/badResponses/notImplemented.d.ts.map +1 -0
  101. package/dist/http/badResponses/notImplemented.js +52 -0
  102. package/dist/http/badResponses/serverError.d.ts +34 -0
  103. package/dist/http/badResponses/serverError.d.ts.map +1 -0
  104. package/dist/http/badResponses/serverError.js +52 -0
  105. package/dist/http/badResponses/unauthorized.d.ts +34 -0
  106. package/dist/http/badResponses/unauthorized.d.ts.map +1 -0
  107. package/dist/http/badResponses/unauthorized.js +52 -0
  108. package/dist/http/badResponses/unprocessableEntity.d.ts +43 -0
  109. package/dist/http/badResponses/unprocessableEntity.d.ts.map +1 -0
  110. package/dist/http/badResponses/unprocessableEntity.js +60 -0
  111. package/dist/http/successResponses/__test__/created.spec.d.ts +2 -0
  112. package/dist/http/successResponses/__test__/created.spec.d.ts.map +1 -0
  113. package/dist/http/successResponses/__test__/created.spec.js +57 -0
  114. package/dist/http/successResponses/__test__/found.spec.d.ts +2 -0
  115. package/dist/http/successResponses/__test__/found.spec.d.ts.map +1 -0
  116. package/dist/http/successResponses/__test__/found.spec.js +57 -0
  117. package/dist/http/successResponses/__test__/noContent.spec.d.ts +2 -0
  118. package/dist/http/successResponses/__test__/noContent.spec.d.ts.map +1 -0
  119. package/dist/http/successResponses/__test__/noContent.spec.js +40 -0
  120. package/dist/http/successResponses/__test__/success.spec.d.ts +2 -0
  121. package/dist/http/successResponses/__test__/success.spec.d.ts.map +1 -0
  122. package/dist/http/successResponses/__test__/success.spec.js +60 -0
  123. package/dist/http/successResponses/__test__/updated.spec.d.ts +2 -0
  124. package/dist/http/successResponses/__test__/updated.spec.d.ts.map +1 -0
  125. package/dist/http/successResponses/__test__/updated.spec.js +60 -0
  126. package/dist/http/successResponses/created.d.ts +36 -0
  127. package/dist/http/successResponses/created.d.ts.map +1 -0
  128. package/dist/http/successResponses/created.js +54 -0
  129. package/dist/http/successResponses/found.d.ts +39 -0
  130. package/dist/http/successResponses/found.d.ts.map +1 -0
  131. package/dist/http/successResponses/found.js +57 -0
  132. package/dist/http/successResponses/noContent.d.ts +25 -0
  133. package/dist/http/successResponses/noContent.d.ts.map +1 -0
  134. package/dist/http/successResponses/noContent.js +35 -0
  135. package/dist/http/successResponses/success.d.ts +36 -0
  136. package/dist/http/successResponses/success.d.ts.map +1 -0
  137. package/dist/http/successResponses/success.js +54 -0
  138. package/dist/http/successResponses/updated.d.ts +36 -0
  139. package/dist/http/successResponses/updated.d.ts.map +1 -0
  140. package/dist/http/successResponses/updated.js +54 -0
  141. package/dist/index.d.ts +22 -13
  142. package/dist/index.d.ts.map +1 -1
  143. package/dist/index.js +24 -15
  144. package/dist/services/__test__/decodeRequestBody.spec.d.ts +2 -0
  145. package/dist/services/__test__/decodeRequestBody.spec.d.ts.map +1 -0
  146. package/dist/services/__test__/decodeRequestBody.spec.js +39 -0
  147. package/dist/services/__test__/errorHandler.spec.d.ts +2 -0
  148. package/dist/services/__test__/errorHandler.spec.d.ts.map +1 -0
  149. package/dist/services/__test__/errorHandler.spec.js +115 -0
  150. package/dist/services/__test__/formParse.spec.d.ts +2 -0
  151. package/dist/services/__test__/formParse.spec.d.ts.map +1 -0
  152. package/dist/services/__test__/formParse.spec.js +78 -0
  153. package/dist/services/__test__/getCaller.spec.d.ts +2 -0
  154. package/dist/services/__test__/getCaller.spec.d.ts.map +1 -0
  155. package/dist/services/__test__/getCaller.spec.js +60 -0
  156. package/dist/services/__test__/getScopedParams.spec.d.ts +2 -0
  157. package/dist/services/__test__/getScopedParams.spec.d.ts.map +1 -0
  158. package/dist/services/__test__/getScopedParams.spec.js +51 -0
  159. package/dist/services/__test__/schemaValidator.spec.d.ts +2 -0
  160. package/dist/services/__test__/schemaValidator.spec.d.ts.map +1 -0
  161. package/dist/services/__test__/schemaValidator.spec.js +89 -0
  162. package/dist/services/decodeErrorMessageFromRequest.d.ts +17 -0
  163. package/dist/services/decodeErrorMessageFromRequest.d.ts.map +1 -0
  164. package/dist/services/decodeErrorMessageFromRequest.js +30 -0
  165. package/dist/services/decodeRequestBody.d.ts +17 -0
  166. package/dist/services/decodeRequestBody.d.ts.map +1 -0
  167. package/dist/services/decodeRequestBody.js +38 -0
  168. package/dist/services/errorHandler.d.ts +44 -0
  169. package/dist/services/errorHandler.d.ts.map +1 -0
  170. package/dist/services/errorHandler.js +93 -0
  171. package/dist/services/formParse.d.ts +40 -1
  172. package/dist/services/formParse.d.ts.map +1 -1
  173. package/dist/services/formParse.js +44 -3
  174. package/dist/services/getCaller.d.ts +17 -0
  175. package/dist/services/getCaller.d.ts.map +1 -0
  176. package/dist/services/getCaller.js +45 -0
  177. package/dist/services/getScopedParams.d.ts +26 -1
  178. package/dist/services/getScopedParams.d.ts.map +1 -1
  179. package/dist/services/getScopedParams.js +26 -2
  180. package/dist/services/httpDebug.d.ts +35 -0
  181. package/dist/services/httpDebug.d.ts.map +1 -0
  182. package/dist/services/httpDebug.js +56 -0
  183. package/dist/services/schemaValidator.d.ts +13 -0
  184. package/dist/services/schemaValidator.d.ts.map +1 -0
  185. package/dist/services/schemaValidator.js +51 -0
  186. package/dist/services/sendFileToS3.d.ts +50 -18
  187. package/dist/services/sendFileToS3.d.ts.map +1 -1
  188. package/dist/services/sendFileToS3.js +73 -31
  189. package/package.json +14 -6
  190. package/src/api/__test__/deleteRequest.spec.ts +107 -0
  191. package/src/api/__test__/getRequest.spec.ts +76 -0
  192. package/src/api/__test__/inboxFlowRequest.spec.ts +125 -0
  193. package/src/api/__test__/makeRequest.spec.ts +121 -0
  194. package/src/api/__test__/patchRequest.spec.ts +125 -0
  195. package/src/api/__test__/postRequest.spec.ts +125 -0
  196. package/src/api/__test__/putRequest.spec.ts +125 -0
  197. package/src/api/deleteRequest.ts +22 -0
  198. package/src/api/getRequest.ts +20 -0
  199. package/src/api/inboxFlowRequest.ts +76 -0
  200. package/src/api/makeRequest.ts +117 -0
  201. package/src/api/patchRequest.ts +22 -0
  202. package/src/api/postRequest.ts +22 -0
  203. package/src/api/putRequest.ts +22 -0
  204. package/src/config/__test__/apiInstance.spec.ts +129 -0
  205. package/src/config/__test__/inboxFlowInstance.spec.ts +61 -0
  206. package/src/config/apiInstance.ts +148 -0
  207. package/src/config/inboxFlowInstance.ts +65 -0
  208. package/src/http/badResponses/__test__/badGateway.spec.ts +50 -0
  209. package/src/http/badResponses/__test__/badRequest.spec.ts +50 -0
  210. package/src/http/badResponses/__test__/conflict.spec.ts +50 -0
  211. package/src/http/badResponses/__test__/forbidden.spec.ts +50 -0
  212. package/src/http/badResponses/__test__/notFound.spec.ts +50 -0
  213. package/src/http/badResponses/__test__/notImplemented.spec.ts +50 -0
  214. package/src/http/badResponses/__test__/serverError.spec.ts +50 -0
  215. package/src/http/badResponses/__test__/unauthorized.spec.ts +50 -0
  216. package/src/http/badResponses/__test__/unprocessableEntity.spec.ts +76 -0
  217. package/src/http/badResponses/badGateway.ts +63 -0
  218. package/src/http/badResponses/badRequest.ts +63 -0
  219. package/src/http/badResponses/conflict.ts +63 -0
  220. package/src/http/badResponses/forbidden.ts +63 -0
  221. package/src/http/badResponses/notFound.ts +63 -0
  222. package/src/http/badResponses/notImplemented.ts +63 -0
  223. package/src/http/badResponses/serverError.ts +63 -0
  224. package/src/http/badResponses/unauthorized.ts +63 -0
  225. package/src/http/badResponses/unprocessableEntity.ts +79 -0
  226. package/src/http/successResponses/__test__/created.spec.ts +65 -0
  227. package/src/http/successResponses/__test__/found.spec.ts +65 -0
  228. package/src/http/successResponses/__test__/noContent.spec.ts +49 -0
  229. package/src/http/successResponses/__test__/success.spec.ts +68 -0
  230. package/src/http/successResponses/__test__/updated.spec.ts +68 -0
  231. package/src/http/successResponses/created.ts +64 -0
  232. package/src/http/successResponses/found.ts +67 -0
  233. package/src/http/successResponses/noContent.ts +42 -0
  234. package/src/http/successResponses/success.ts +64 -0
  235. package/src/http/successResponses/updated.ts +64 -0
  236. package/src/index.ts +25 -16
  237. package/src/services/__test__/decodeRequestBody.spec.ts +51 -0
  238. package/src/services/__test__/errorHandler.spec.ts +133 -0
  239. package/src/services/__test__/formParse.spec.ts +96 -0
  240. package/src/services/__test__/getCaller.spec.ts +78 -0
  241. package/src/services/__test__/getScopedParams.spec.ts +68 -0
  242. package/src/services/__test__/schemaValidator.spec.ts +106 -0
  243. package/src/services/decodeErrorMessageFromRequest.ts +36 -0
  244. package/src/services/decodeRequestBody.ts +42 -0
  245. package/src/services/errorHandler.ts +99 -0
  246. package/src/services/formParse.ts +46 -6
  247. package/src/services/getCaller.ts +58 -0
  248. package/src/services/getScopedParams.ts +29 -2
  249. package/src/services/httpDebug.ts +64 -0
  250. package/src/services/schemaValidator.ts +66 -0
  251. package/src/services/sendFileToS3.ts +81 -56
  252. package/vitest.config.ts +5 -0
  253. package/dist/helpers/globalErrorHandler.d.ts +0 -3
  254. package/dist/helpers/globalErrorHandler.d.ts.map +0 -1
  255. package/dist/helpers/globalErrorHandler.js +0 -53
  256. package/dist/httpBadResponses/badRequest.d.ts +0 -9
  257. package/dist/httpBadResponses/badRequest.d.ts.map +0 -1
  258. package/dist/httpBadResponses/badRequest.js +0 -23
  259. package/dist/httpBadResponses/conflict.d.ts +0 -9
  260. package/dist/httpBadResponses/conflict.d.ts.map +0 -1
  261. package/dist/httpBadResponses/conflict.js +0 -23
  262. package/dist/httpBadResponses/forbidden.d.ts +0 -9
  263. package/dist/httpBadResponses/forbidden.d.ts.map +0 -1
  264. package/dist/httpBadResponses/forbidden.js +0 -23
  265. package/dist/httpBadResponses/notFound.d.ts +0 -9
  266. package/dist/httpBadResponses/notFound.d.ts.map +0 -1
  267. package/dist/httpBadResponses/notFound.js +0 -23
  268. package/dist/httpBadResponses/serverError.d.ts +0 -9
  269. package/dist/httpBadResponses/serverError.d.ts.map +0 -1
  270. package/dist/httpBadResponses/serverError.js +0 -23
  271. package/dist/httpBadResponses/unauthorized.d.ts +0 -9
  272. package/dist/httpBadResponses/unauthorized.d.ts.map +0 -1
  273. package/dist/httpBadResponses/unauthorized.js +0 -23
  274. package/dist/httpBadResponses/unprocessableEntity.d.ts +0 -17
  275. package/dist/httpBadResponses/unprocessableEntity.d.ts.map +0 -1
  276. package/dist/httpBadResponses/unprocessableEntity.js +0 -30
  277. package/dist/httpResponses/created.d.ts +0 -10
  278. package/dist/httpResponses/created.d.ts.map +0 -1
  279. package/dist/httpResponses/created.js +0 -30
  280. package/dist/httpResponses/noContent.d.ts +0 -9
  281. package/dist/httpResponses/noContent.d.ts.map +0 -1
  282. package/dist/httpResponses/noContent.js +0 -28
  283. package/dist/httpResponses/success.d.ts +0 -10
  284. package/dist/httpResponses/success.d.ts.map +0 -1
  285. package/dist/httpResponses/success.js +0 -30
  286. package/dist/httpResponses/updated.d.ts +0 -10
  287. package/dist/httpResponses/updated.d.ts.map +0 -1
  288. package/dist/httpResponses/updated.js +0 -30
  289. package/dist/services/extractJsonFromRequest.d.ts +0 -3
  290. package/dist/services/extractJsonFromRequest.d.ts.map +0 -1
  291. package/dist/services/extractJsonFromRequest.js +0 -29
  292. package/src/helpers/globalErrorHandler.ts +0 -64
  293. package/src/httpBadResponses/badRequest.ts +0 -29
  294. package/src/httpBadResponses/conflict.ts +0 -29
  295. package/src/httpBadResponses/forbidden.ts +0 -29
  296. package/src/httpBadResponses/notFound.ts +0 -29
  297. package/src/httpBadResponses/serverError.ts +0 -29
  298. package/src/httpBadResponses/unauthorized.ts +0 -29
  299. package/src/httpBadResponses/unprocessableEntity.ts +0 -43
  300. package/src/httpResponses/created.ts +0 -35
  301. package/src/httpResponses/noContent.ts +0 -33
  302. package/src/httpResponses/success.ts +0 -35
  303. package/src/httpResponses/updated.ts +0 -35
  304. package/src/services/extractJsonFromRequest.ts +0 -30
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Handles errors and converts them into appropriate HTTP responses.
3
+ *
4
+ * This function takes an error object and determines its type to return
5
+ * the corresponding HTTP response. It supports both success and error
6
+ * response types, converting them into a standardized format using the
7
+ * `toResponse` method when applicable.
8
+ *
9
+ * @param error - The error object to handle. It can be an instance of various
10
+ * HTTP response classes or a generic error.
11
+ *
12
+ * @returns The corresponding HTTP response object if the error matches a known
13
+ * type, or `undefined` if no match is found.
14
+ *
15
+ * ### Supported Success Responses:
16
+ * - `Found`
17
+ * - `Created`
18
+ * - `Updated`
19
+ * - `Success`
20
+ * - `NoContent`
21
+ *
22
+ * ### Supported Error Responses:
23
+ * - `BadGateway`
24
+ * - `BadRequest`
25
+ * - `Conflict`
26
+ * - `Forbidden`
27
+ * - `NotFound`
28
+ * - `NotImplemented`
29
+ * - `ServerError`
30
+ * - `Unauthorized`
31
+ * - `UnprocessableEntity`
32
+ *
33
+ * ### Example Usage:
34
+ * ```typescript
35
+ * try {
36
+ * // Some operation that might throw an error
37
+ * } catch (error) {
38
+ * return errorHandler(error);
39
+ * }
40
+ * ```
41
+ */
42
+ declare function errorHandler(error: any): Response;
43
+ export { errorHandler };
44
+ //# sourceMappingURL=errorHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errorHandler.d.ts","sourceRoot":"","sources":["../../src/services/errorHandler.ts"],"names":[],"mappings":"AAgBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAEH,iBAAS,YAAY,CAAC,KAAK,EAAE,GAAG,GAAG,QAAQ,CAsC1C;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
@@ -0,0 +1,93 @@
1
+ import { BadGateway } from "../http/badResponses/badGateway";
2
+ import { BadRequest } from "../http/badResponses/badRequest";
3
+ import { Conflict } from "../http/badResponses/conflict";
4
+ import { Forbidden } from "../http/badResponses/forbidden";
5
+ import { NotFound } from "../http/badResponses/notFound";
6
+ import { NotImplemented } from "../http/badResponses/notImplemented";
7
+ import { ServerError } from "../http/badResponses/serverError";
8
+ import { Unauthorized } from "../http/badResponses/unauthorized";
9
+ import { UnprocessableEntity } from "../http/badResponses/unprocessableEntity";
10
+ import { Created } from "../http/successResponses/created";
11
+ import { Found } from "../http/successResponses/found";
12
+ import { NoContent } from "../http/successResponses/noContent";
13
+ import { Success } from "../http/successResponses/success";
14
+ import { Updated } from "../http/successResponses/updated";
15
+ /**
16
+ * Handles errors and converts them into appropriate HTTP responses.
17
+ *
18
+ * This function takes an error object and determines its type to return
19
+ * the corresponding HTTP response. It supports both success and error
20
+ * response types, converting them into a standardized format using the
21
+ * `toResponse` method when applicable.
22
+ *
23
+ * @param error - The error object to handle. It can be an instance of various
24
+ * HTTP response classes or a generic error.
25
+ *
26
+ * @returns The corresponding HTTP response object if the error matches a known
27
+ * type, or `undefined` if no match is found.
28
+ *
29
+ * ### Supported Success Responses:
30
+ * - `Found`
31
+ * - `Created`
32
+ * - `Updated`
33
+ * - `Success`
34
+ * - `NoContent`
35
+ *
36
+ * ### Supported Error Responses:
37
+ * - `BadGateway`
38
+ * - `BadRequest`
39
+ * - `Conflict`
40
+ * - `Forbidden`
41
+ * - `NotFound`
42
+ * - `NotImplemented`
43
+ * - `ServerError`
44
+ * - `Unauthorized`
45
+ * - `UnprocessableEntity`
46
+ *
47
+ * ### Example Usage:
48
+ * ```typescript
49
+ * try {
50
+ * // Some operation that might throw an error
51
+ * } catch (error) {
52
+ * return errorHandler(error);
53
+ * }
54
+ * ```
55
+ */
56
+ function errorHandler(error) {
57
+ switch (true) {
58
+ case error instanceof Response:
59
+ return error;
60
+ case error instanceof Found:
61
+ return error.toResponse();
62
+ case error instanceof Created:
63
+ return error.toResponse();
64
+ case error instanceof Updated:
65
+ return error.toResponse();
66
+ case error instanceof Success:
67
+ return error.toResponse();
68
+ case error instanceof NoContent:
69
+ return error.toResponse();
70
+ }
71
+ switch (true) {
72
+ case error instanceof BadGateway:
73
+ return error.toResponse();
74
+ case error instanceof BadRequest:
75
+ return error.toResponse();
76
+ case error instanceof Conflict:
77
+ return error.toResponse();
78
+ case error instanceof Forbidden:
79
+ return error.toResponse();
80
+ case error instanceof NotFound:
81
+ return error.toResponse();
82
+ case error instanceof NotImplemented:
83
+ return error.toResponse();
84
+ case error instanceof ServerError:
85
+ return error.toResponse();
86
+ case error instanceof Unauthorized:
87
+ return error.toResponse();
88
+ case error instanceof UnprocessableEntity:
89
+ return error.toResponse();
90
+ }
91
+ return new ServerError("Server error").toResponse();
92
+ }
93
+ export { errorHandler };
@@ -1,3 +1,42 @@
1
1
  import type { FormParseProps, FormParseReturnType } from "@arkyn/types";
2
- export declare function formParse<T extends FormParseProps>([formData, schema,]: T): FormParseReturnType<T>;
2
+ /**
3
+ * Parses form data using a Zod schema and returns the result.
4
+ *
5
+ * @template T - A type that extends `FormParseProps`.
6
+ *
7
+ * @param {T} param0 - An array containing the form data and the Zod schema.
8
+ * @param {T[0]} param0[0] - The form data to be validated.
9
+ * @param {T[1]} param0[1] - The Zod schema used for validation.
10
+ *
11
+ * @returns {FormParseReturnType<T>} An object containing the validation result.
12
+ * - If validation fails, it includes:
13
+ * - `success`: A boolean indicating the validation status (false).
14
+ * - `fieldErrors`: An object mapping field names to their respective error messages.
15
+ * - `fields`: The original form data.
16
+ * - If validation succeeds, it includes:
17
+ * - `success`: A boolean indicating the validation status (true).
18
+ * - `data`: The parsed and validated data.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * import { z } from "zod";
23
+ *
24
+ * const schema = z.object({
25
+ * name: z.string().min(1, "Name is required"),
26
+ * age: z.number().min(18, "Must be at least 18"),
27
+ * });
28
+ *
29
+ * const formData = { name: "", age: 17 };
30
+ *
31
+ * const result = formParse([formData, schema]);
32
+ *
33
+ * if (!result.success) {
34
+ * console.log(result.fieldErrors); // { name: "Name is required", age: "Must be at least 18" }
35
+ * } else {
36
+ * console.log(result.data); // Parsed data
37
+ * }
38
+ * ```
39
+ */
40
+ declare function formParse<T extends FormParseProps>([formData, schema,]: T): FormParseReturnType<T>;
41
+ export { formParse };
3
42
  //# sourceMappingURL=formParse.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"formParse.d.ts","sourceRoot":"","sources":["../../src/services/formParse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAExE,wBAAgB,SAAS,CAAC,CAAC,SAAS,cAAc,EAAE,CAClD,QAAQ,EACR,MAAM,EACP,EAAE,CAAC,GAAG,mBAAmB,CAAC,CAAC,CAAC,CAoB5B"}
1
+ {"version":3,"file":"formParse.d.ts","sourceRoot":"","sources":["../../src/services/formParse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAExE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,iBAAS,SAAS,CAAC,CAAC,SAAS,cAAc,EAAE,CAC3C,QAAQ,EACR,MAAM,EACP,EAAE,CAAC,GAAG,mBAAmB,CAAC,CAAC,CAAC,CAmB5B;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
@@ -1,8 +1,48 @@
1
- export function formParse([formData, schema,]) {
1
+ /**
2
+ * Parses form data using a Zod schema and returns the result.
3
+ *
4
+ * @template T - A type that extends `FormParseProps`.
5
+ *
6
+ * @param {T} param0 - An array containing the form data and the Zod schema.
7
+ * @param {T[0]} param0[0] - The form data to be validated.
8
+ * @param {T[1]} param0[1] - The Zod schema used for validation.
9
+ *
10
+ * @returns {FormParseReturnType<T>} An object containing the validation result.
11
+ * - If validation fails, it includes:
12
+ * - `success`: A boolean indicating the validation status (false).
13
+ * - `fieldErrors`: An object mapping field names to their respective error messages.
14
+ * - `fields`: The original form data.
15
+ * - If validation succeeds, it includes:
16
+ * - `success`: A boolean indicating the validation status (true).
17
+ * - `data`: The parsed and validated data.
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * import { z } from "zod";
22
+ *
23
+ * const schema = z.object({
24
+ * name: z.string().min(1, "Name is required"),
25
+ * age: z.number().min(18, "Must be at least 18"),
26
+ * });
27
+ *
28
+ * const formData = { name: "", age: 17 };
29
+ *
30
+ * const result = formParse([formData, schema]);
31
+ *
32
+ * if (!result.success) {
33
+ * console.log(result.fieldErrors); // { name: "Name is required", age: "Must be at least 18" }
34
+ * } else {
35
+ * console.log(result.data); // Parsed data
36
+ * }
37
+ * ```
38
+ */
39
+ function formParse([formData, schema,]) {
2
40
  const zodResponse = schema.safeParse(formData);
3
41
  if (zodResponse.success === false) {
4
- const errorsArray = Object.entries(zodResponse.error.formErrors.fieldErrors);
5
- const errorsObject = Object.fromEntries(errorsArray.map((item) => [item[0], item[1]?.[0] || "Error"]));
42
+ const errorsObject = Object.fromEntries(zodResponse.error.errors.map((item) => [
43
+ item.path.join("."),
44
+ item.message,
45
+ ]));
6
46
  return {
7
47
  success: zodResponse.success,
8
48
  fieldErrors: errorsObject,
@@ -13,3 +53,4 @@ export function formParse([formData, schema,]) {
13
53
  return { success: zodResponse.success, data: zodResponse.data };
14
54
  }
15
55
  }
56
+ export { formParse };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Retrieves information about the caller of the current function.
3
+ *
4
+ * This function analyzes the stack trace to determine the file path and function name
5
+ * of the caller. It excludes stack trace entries related to the `@arkyn/server` package
6
+ * and attempts to resolve the file path relative to the project root directory.
7
+ *
8
+ * @returns An object containing:
9
+ * - `functionName`: The name of the function that called the current function, or "Unknown function" if it cannot be determined.
10
+ * - `callerInfo`: The file path of the caller relative to the project root, or "Unknown caller" if it cannot be determined.
11
+ */
12
+ declare function getCaller(): {
13
+ functionName: string;
14
+ callerInfo: string;
15
+ };
16
+ export { getCaller };
17
+ //# sourceMappingURL=getCaller.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getCaller.d.ts","sourceRoot":"","sources":["../../src/services/getCaller.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AAEH,iBAAS,SAAS;;;EAyCjB;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
@@ -0,0 +1,45 @@
1
+ import path from "path";
2
+ /**
3
+ * Retrieves information about the caller of the current function.
4
+ *
5
+ * This function analyzes the stack trace to determine the file path and function name
6
+ * of the caller. It excludes stack trace entries related to the `@arkyn/server` package
7
+ * and attempts to resolve the file path relative to the project root directory.
8
+ *
9
+ * @returns An object containing:
10
+ * - `functionName`: The name of the function that called the current function, or "Unknown function" if it cannot be determined.
11
+ * - `callerInfo`: The file path of the caller relative to the project root, or "Unknown caller" if it cannot be determined.
12
+ */
13
+ function getCaller() {
14
+ const projectRoot = process.cwd();
15
+ const error = new Error();
16
+ const stackLines = error.stack?.split("\n").map((line) => line.trim()) || [];
17
+ let callerInfo = "Unknown caller";
18
+ let functionName = "Unknown function";
19
+ const relevantLines = stackLines.filter((line) => !line.includes("@arkyn/server"));
20
+ let foundGetCaller = false;
21
+ for (const line of relevantLines) {
22
+ if (!foundGetCaller) {
23
+ if (line.includes("getCaller")) {
24
+ foundGetCaller = true;
25
+ }
26
+ continue;
27
+ }
28
+ const match = line.match(/at (.+?) \((.+?)\)/) || line.match(/at (.+)/);
29
+ if (match) {
30
+ const rawFuncName = match[1]?.split(" ")[0] || "";
31
+ functionName =
32
+ rawFuncName && !rawFuncName.includes("/")
33
+ ? rawFuncName
34
+ : "Unknown function";
35
+ let fullPath = match[2] || match[1];
36
+ if (fullPath.startsWith(projectRoot)) {
37
+ fullPath = path.relative(projectRoot, fullPath);
38
+ }
39
+ callerInfo = fullPath;
40
+ break;
41
+ }
42
+ }
43
+ return { functionName, callerInfo };
44
+ }
45
+ export { getCaller };
@@ -1,3 +1,28 @@
1
- declare function getScopedParams(request: Request, scope?: string): URLSearchParams;
1
+ import type { GetScopedParamsFunction } from "@arkyn/types";
2
+ /**
3
+ * Extracts and returns scoped query parameters from a request URL.
4
+ *
5
+ * @param request - The incoming request object containing the URL.
6
+ * @param scope - An optional string representing the scope prefix for filtering query parameters.
7
+ * If no scope is provided, all query parameters are returned.
8
+ *
9
+ * @returns A `URLSearchParams` object containing the scoped query parameters.
10
+ * If a scope is provided, only parameters with keys starting with the scope
11
+ * (e.g., `scope:key`) are included, and the scope prefix is removed from the keys.
12
+ * If no scope is provided, all query parameters are returned as-is.
13
+ *
14
+ * @example
15
+ * // Example 1: No scope provided
16
+ * const request = { url: "https://example.com?key1=value1&key2=value2" };
17
+ * const params = getScopedParams(request);
18
+ * console.log(params.toString()); // Output: "key1=value1&key2=value2"
19
+ *
20
+ * @example
21
+ * // Example 2: Scope provided
22
+ * const request = { url: "https://example.com?scope:key1=value1&scope:key2=value2&key3=value3" };
23
+ * const params = getScopedParams(request, "scope");
24
+ * console.log(params.toString()); // Output: "key1=value1&key2=value2"
25
+ */
26
+ declare const getScopedParams: GetScopedParamsFunction;
2
27
  export { getScopedParams };
3
28
  //# sourceMappingURL=getScopedParams.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getScopedParams.d.ts","sourceRoot":"","sources":["../../src/services/getScopedParams.ts"],"names":[],"mappings":"AAAA,iBAAS,eAAe,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,GAAE,MAAW,mBAW5D;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
1
+ {"version":3,"file":"getScopedParams.d.ts","sourceRoot":"","sources":["../../src/services/getScopedParams.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAE5D;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,QAAA,MAAM,eAAe,EAAE,uBAWtB,CAAC;AAEF,OAAO,EAAE,eAAe,EAAE,CAAC"}
@@ -1,4 +1,28 @@
1
- function getScopedParams(request, scope = "") {
1
+ /**
2
+ * Extracts and returns scoped query parameters from a request URL.
3
+ *
4
+ * @param request - The incoming request object containing the URL.
5
+ * @param scope - An optional string representing the scope prefix for filtering query parameters.
6
+ * If no scope is provided, all query parameters are returned.
7
+ *
8
+ * @returns A `URLSearchParams` object containing the scoped query parameters.
9
+ * If a scope is provided, only parameters with keys starting with the scope
10
+ * (e.g., `scope:key`) are included, and the scope prefix is removed from the keys.
11
+ * If no scope is provided, all query parameters are returned as-is.
12
+ *
13
+ * @example
14
+ * // Example 1: No scope provided
15
+ * const request = { url: "https://example.com?key1=value1&key2=value2" };
16
+ * const params = getScopedParams(request);
17
+ * console.log(params.toString()); // Output: "key1=value1&key2=value2"
18
+ *
19
+ * @example
20
+ * // Example 2: Scope provided
21
+ * const request = { url: "https://example.com?scope:key1=value1&scope:key2=value2&key3=value3" };
22
+ * const params = getScopedParams(request, "scope");
23
+ * console.log(params.toString()); // Output: "key1=value1&key2=value2"
24
+ */
25
+ const getScopedParams = (request, scope = "") => {
2
26
  const url = new URL(request.url);
3
27
  if (scope === "")
4
28
  return url.searchParams;
@@ -6,5 +30,5 @@ function getScopedParams(request, scope = "") {
6
30
  .filter(([key]) => key.startsWith(`${scope}:`))
7
31
  .map(([key, value]) => [key.replace(`${scope}:`, ""), value]);
8
32
  return new URLSearchParams(scopedSearchParams);
9
- }
33
+ };
10
34
  export { getScopedParams };
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Logs debug information to the console when in development mode or when the
3
+ * `SHOW_ERRORS_IN_CONSOLE` environment variable is set to "true".
4
+ *
5
+ * This function provides detailed information about the caller function,
6
+ * its location, and the provided body and cause, if any.
7
+ *
8
+ * @param name - A string representing the name or context of the debug log.
9
+ * @param body - The main content or data to be logged.
10
+ * @param cause - (Optional) Additional information or error cause to be logged.
11
+ *
12
+ * @remarks
13
+ * The debug logs are only displayed when the application is running in
14
+ * development mode (`NODE_ENV === "development"`) or when the
15
+ * `SHOW_ERRORS_IN_CONSOLE` environment variable is explicitly set to "true".
16
+ *
17
+ * The logs include:
18
+ * - The name of the debug context.
19
+ * - The caller function name and its location.
20
+ * - The provided body content.
21
+ * - The optional cause, if provided.
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * httpDebug("FetchUserData", { userId: 123 });
26
+ * ```
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * httpDebug("FetchUserDataError", { userId: 123 }, new Error("User not found"));
31
+ * ```
32
+ */
33
+ declare function httpDebug(name: string, body: any, cause?: any): void;
34
+ export { httpDebug };
35
+ //# sourceMappingURL=httpDebug.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"httpDebug.d.ts","sourceRoot":"","sources":["../../src/services/httpDebug.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,iBAAS,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,EAAE,GAAG,QAyBtD;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
@@ -0,0 +1,56 @@
1
+ import { InboxFlowInstance } from "../config/inboxFlowInstance";
2
+ import { getCaller } from "../services/getCaller";
3
+ /**
4
+ * Logs debug information to the console when in development mode or when the
5
+ * `SHOW_ERRORS_IN_CONSOLE` environment variable is set to "true".
6
+ *
7
+ * This function provides detailed information about the caller function,
8
+ * its location, and the provided body and cause, if any.
9
+ *
10
+ * @param name - A string representing the name or context of the debug log.
11
+ * @param body - The main content or data to be logged.
12
+ * @param cause - (Optional) Additional information or error cause to be logged.
13
+ *
14
+ * @remarks
15
+ * The debug logs are only displayed when the application is running in
16
+ * development mode (`NODE_ENV === "development"`) or when the
17
+ * `SHOW_ERRORS_IN_CONSOLE` environment variable is explicitly set to "true".
18
+ *
19
+ * The logs include:
20
+ * - The name of the debug context.
21
+ * - The caller function name and its location.
22
+ * - The provided body content.
23
+ * - The optional cause, if provided.
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * httpDebug("FetchUserData", { userId: 123 });
28
+ * ```
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * httpDebug("FetchUserDataError", { userId: 123 }, new Error("User not found"));
33
+ * ```
34
+ */
35
+ function httpDebug(name, body, cause) {
36
+ const isDebugMode = process.env.NODE_ENV === "development" ||
37
+ process.env?.SHOW_ERRORS_IN_CONSOLE === "true";
38
+ if (isDebugMode) {
39
+ const reset = "\x1b[0m";
40
+ const cyan = "\x1b[36m";
41
+ const debugName = `${cyan}[ARKYN-DEBUG]${reset}`;
42
+ const { callerInfo, functionName } = getCaller();
43
+ let consoleData = `${debugName} ${name} initialized\n`;
44
+ consoleData += `${debugName} Caller Function: ${functionName}\n`;
45
+ consoleData += `${debugName} Caller Location: ${callerInfo}\n`;
46
+ consoleData += `${debugName} Body: ${JSON.stringify(body, null, 2)}\n`;
47
+ if (cause) {
48
+ consoleData += `${debugName} Cause: ${JSON.stringify(cause, null, 2)}\n`;
49
+ }
50
+ console.log(consoleData);
51
+ const arkynKeys = InboxFlowInstance.getInboxConfig();
52
+ if (arkynKeys)
53
+ console.log(arkynKeys);
54
+ }
55
+ }
56
+ export { httpDebug };
@@ -0,0 +1,13 @@
1
+ import { Schema, z } from "zod";
2
+ declare class SchemaValidator<T extends Schema> {
3
+ readonly schema: T;
4
+ functionName: string;
5
+ callerInfo: string;
6
+ constructor(schema: T);
7
+ isValid(data: any): boolean;
8
+ safeValidate(data: any): z.SafeParseReturnType<z.infer<T>, z.infer<T>>;
9
+ validate(data: any): z.infer<T>;
10
+ formValidate(data: any, message?: string): z.infer<T>;
11
+ }
12
+ export { SchemaValidator };
13
+ //# sourceMappingURL=schemaValidator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schemaValidator.d.ts","sourceRoot":"","sources":["../../src/services/schemaValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAiBhC,cAAM,eAAe,CAAC,CAAC,SAAS,MAAM;IAIxB,QAAQ,CAAC,MAAM,EAAE,CAAC;IAH9B,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;gBAEE,MAAM,EAAE,CAAC;IAM9B,OAAO,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO;IAI3B,YAAY,CAAC,IAAI,EAAE,GAAG,GAAG,CAAC,CAAC,mBAAmB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAItE,QAAQ,CAAC,IAAI,EAAE,GAAG,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IAQ/B,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;CAoBtD;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
@@ -0,0 +1,51 @@
1
+ import { Schema, z } from "zod";
2
+ import { ServerError } from "../http/badResponses/serverError";
3
+ import { UnprocessableEntity } from "../http/badResponses/unprocessableEntity";
4
+ import { formParse } from "./formParse";
5
+ import { getCaller } from "./getCaller";
6
+ import { httpDebug } from "./httpDebug";
7
+ function formatErrorMessage(error) {
8
+ const title = "Error validating:";
9
+ const lines = error.issues.map(({ path, message }) => `-> ${path.join(".")}: ${message}`);
10
+ return [title, ...lines].join("\n");
11
+ }
12
+ class SchemaValidator {
13
+ schema;
14
+ functionName;
15
+ callerInfo;
16
+ constructor(schema) {
17
+ this.schema = schema;
18
+ const { callerInfo, functionName } = getCaller();
19
+ this.callerInfo = callerInfo;
20
+ this.functionName = functionName;
21
+ }
22
+ isValid(data) {
23
+ return this.schema.safeParse(data).success;
24
+ }
25
+ safeValidate(data) {
26
+ return this.schema.safeParse(data);
27
+ }
28
+ validate(data) {
29
+ try {
30
+ return this.schema.parse(data);
31
+ }
32
+ catch (error) {
33
+ throw new ServerError(formatErrorMessage(error));
34
+ }
35
+ }
36
+ formValidate(data, message) {
37
+ const formParsed = formParse([data, this.schema]);
38
+ if (!formParsed.success) {
39
+ httpDebug("UnprocessableEntity", formParsed);
40
+ const firstErrorKey = Object.keys(formParsed.fieldErrors)[0];
41
+ throw new UnprocessableEntity({
42
+ fields: formParsed.fields,
43
+ fieldErrors: formParsed.fieldErrors,
44
+ data: { scrollTo: firstErrorKey },
45
+ message,
46
+ }, false);
47
+ }
48
+ return formParsed.data;
49
+ }
50
+ }
51
+ export { SchemaValidator };
@@ -1,20 +1,52 @@
1
- import { type ActionFunctionArgs } from "@remix-run/node";
2
- type AWSConfig = {
3
- AWS_REGION: string;
4
- AWS_ACCESS_KEY_ID: string;
5
- AWS_SECRET_ACCESS_KEY: string;
6
- AWS_S3_BUCKET: string;
7
- };
8
- type Config = {
9
- maxPartSize?: number;
10
- fileName?: string;
11
- };
12
- declare function sendFileToS3(props: ActionFunctionArgs, awsS3Config: AWSConfig, config?: Config): Promise<{
13
- error: string;
14
- url?: undefined;
15
- } | {
16
- url: string;
17
- error?: undefined;
18
- }>;
1
+ import type { SendFileToS3Function } from "@arkyn/types";
2
+ /**
3
+ * Handles file uploads to an AWS S3 bucket. This function processes a file
4
+ * from a multipart form request, validates and optionally compresses the file,
5
+ * and uploads it to S3. It supports image-specific operations such as resizing
6
+ * validation and quality reduction.
7
+ *
8
+ * @param request - The HTTP request containing the multipart form data.
9
+ * @param awsS3Config - Configuration object for AWS S3, including bucket name,
10
+ * region, and credentials.
11
+ * @param config - Optional configuration object for file handling.
12
+ *
13
+ * @param config.fileName - The name of the form field containing the file. Defaults to `"file"`.
14
+ * @param config.maxPartSize - The maximum size (in bytes) for each part of the file. Defaults to `5_000_000`.
15
+ * @param config.reduceImageQuality - The quality percentage for image compression. Defaults to `100`.
16
+ * @param config.validateImageSize - Whether to validate the image dimensions. Defaults to `false`.
17
+ * @param config.validateImageMessage - The error message template for invalid image dimensions.
18
+ * Defaults to `"Invalid dimensions {{width}}px x {{height}}px"`.
19
+ *
20
+ * @returns A promise that resolves to an object containing the uploaded file's URL
21
+ * or an error message if validation fails.
22
+ *
23
+ * @throws {BadRequest} If no file is uploaded.
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * const awsS3Config = {
28
+ * AWS_S3_BUCKET: "my-bucket",
29
+ * AWS_REGION: "us-east-1",
30
+ * AWS_ACCESS_KEY_ID: "my-access-key",
31
+ * AWS_SECRET_ACCESS_KEY: "my-secret-key",
32
+ * };
33
+ *
34
+ * const config = {
35
+ * fileName: "upload",
36
+ * maxPartSize: 10_000_000,
37
+ * reduceImageQuality: 80,
38
+ * validateImageSize: true,
39
+ * validateImageMessage: "Invalid dimensions {{width}}px x {{height}}px",
40
+ * };
41
+ *
42
+ * const response = await sendFileToS3(request, awsS3Config, config);
43
+ * if (response.error) {
44
+ * console.error(response.error);
45
+ * } else {
46
+ * console.log("File uploaded to:", response.url);
47
+ * }
48
+ * ```
49
+ */
50
+ declare const sendFileToS3: SendFileToS3Function;
19
51
  export { sendFileToS3 };
20
52
  //# sourceMappingURL=sendFileToS3.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sendFileToS3.d.ts","sourceRoot":"","sources":["../../src/services/sendFileToS3.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,KAAK,kBAAkB,EAExB,MAAM,iBAAiB,CAAC;AAOzB,KAAK,SAAS,GAAG;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qBAAqB,EAAE,MAAM,CAAC;IAC9B,aAAa,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF,KAAK,MAAM,GAAG;IACZ,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAmDF,iBAAe,YAAY,CACzB,KAAK,EAAE,kBAAkB,EACzB,WAAW,EAAE,SAAS,EACtB,MAAM,GAAE,MAGP;;;;;;GAwEF;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
1
+ {"version":3,"file":"sendFileToS3.d.ts","sourceRoot":"","sources":["../../src/services/sendFileToS3.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAa,oBAAoB,EAAE,MAAM,cAAc,CAAC;AA8CpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AAEH,QAAA,MAAM,YAAY,EAAE,oBAkFnB,CAAC;AAEF,OAAO,EAAE,YAAY,EAAE,CAAC"}