@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.
- package/LICENSE.txt +24 -0
- package/README.md +116 -0
- package/dist/api/__test__/deleteRequest.spec.d.ts +2 -0
- package/dist/api/__test__/deleteRequest.spec.d.ts.map +1 -0
- package/dist/api/__test__/deleteRequest.spec.js +84 -0
- package/dist/api/__test__/getRequest.spec.d.ts +2 -0
- package/dist/api/__test__/getRequest.spec.d.ts.map +1 -0
- package/dist/api/__test__/getRequest.spec.js +62 -0
- package/dist/api/__test__/inboxFlowRequest.spec.d.ts +2 -0
- package/dist/api/__test__/inboxFlowRequest.spec.d.ts.map +1 -0
- package/dist/api/__test__/inboxFlowRequest.spec.js +96 -0
- package/dist/api/__test__/makeRequest.spec.d.ts +2 -0
- package/dist/api/__test__/makeRequest.spec.d.ts.map +1 -0
- package/dist/api/__test__/makeRequest.spec.js +80 -0
- package/dist/api/__test__/patchRequest.spec.d.ts +2 -0
- package/dist/api/__test__/patchRequest.spec.d.ts.map +1 -0
- package/dist/api/__test__/patchRequest.spec.js +84 -0
- package/dist/api/__test__/postRequest.spec.d.ts +2 -0
- package/dist/api/__test__/postRequest.spec.d.ts.map +1 -0
- package/dist/api/__test__/postRequest.spec.js +84 -0
- package/dist/api/__test__/putRequest.spec.d.ts +2 -0
- package/dist/api/__test__/putRequest.spec.d.ts.map +1 -0
- package/dist/api/__test__/putRequest.spec.js +84 -0
- package/dist/api/deleteRequest.d.ts +13 -0
- package/dist/api/deleteRequest.d.ts.map +1 -0
- package/dist/api/deleteRequest.js +14 -0
- package/dist/api/getRequest.d.ts +12 -0
- package/dist/api/getRequest.d.ts.map +1 -0
- package/dist/api/getRequest.js +13 -0
- package/dist/api/inboxFlowRequest.d.ts +40 -0
- package/dist/api/inboxFlowRequest.d.ts.map +1 -0
- package/dist/api/inboxFlowRequest.js +63 -0
- package/dist/api/makeRequest.d.ts +38 -0
- package/dist/api/makeRequest.d.ts.map +1 -0
- package/dist/api/makeRequest.js +103 -0
- package/dist/api/patchRequest.d.ts +13 -0
- package/dist/api/patchRequest.d.ts.map +1 -0
- package/dist/api/patchRequest.js +14 -0
- package/dist/api/postRequest.d.ts +13 -0
- package/dist/api/postRequest.d.ts.map +1 -0
- package/dist/api/postRequest.js +14 -0
- package/dist/api/putRequest.d.ts +13 -0
- package/dist/api/putRequest.d.ts.map +1 -0
- package/dist/api/putRequest.js +14 -0
- package/dist/config/__test__/apiInstance.spec.d.ts +2 -0
- package/dist/config/__test__/apiInstance.spec.d.ts.map +1 -0
- package/dist/config/__test__/apiInstance.spec.js +86 -0
- package/dist/config/__test__/inboxFlowInstance.spec.d.ts +2 -0
- package/dist/config/__test__/inboxFlowInstance.spec.d.ts.map +1 -0
- package/dist/config/__test__/inboxFlowInstance.spec.js +48 -0
- package/dist/config/apiInstance.d.ts +80 -0
- package/dist/config/apiInstance.d.ts.map +1 -0
- package/dist/config/apiInstance.js +111 -0
- package/dist/config/inboxFlowInstance.d.ts +44 -0
- package/dist/config/inboxFlowInstance.d.ts.map +1 -0
- package/dist/config/inboxFlowInstance.js +46 -0
- package/dist/http/badResponses/__test__/badGateway.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/badGateway.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/badGateway.spec.js +42 -0
- package/dist/http/badResponses/__test__/badRequest.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/badRequest.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/badRequest.spec.js +40 -0
- package/dist/http/badResponses/__test__/conflict.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/conflict.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/conflict.spec.js +40 -0
- package/dist/http/badResponses/__test__/forbidden.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/forbidden.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/forbidden.spec.js +40 -0
- package/dist/http/badResponses/__test__/notFound.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/notFound.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/notFound.spec.js +40 -0
- package/dist/http/badResponses/__test__/notImplemented.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/notImplemented.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/notImplemented.spec.js +40 -0
- package/dist/http/badResponses/__test__/serverError.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/serverError.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/serverError.spec.js +40 -0
- package/dist/http/badResponses/__test__/unauthorized.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/unauthorized.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/unauthorized.spec.js +40 -0
- package/dist/http/badResponses/__test__/unprocessableEntity.spec.d.ts +2 -0
- package/dist/http/badResponses/__test__/unprocessableEntity.spec.d.ts.map +1 -0
- package/dist/http/badResponses/__test__/unprocessableEntity.spec.js +66 -0
- package/dist/http/badResponses/badGateway.d.ts +34 -0
- package/dist/http/badResponses/badGateway.d.ts.map +1 -0
- package/dist/http/badResponses/badGateway.js +52 -0
- package/dist/http/badResponses/badRequest.d.ts +34 -0
- package/dist/http/badResponses/badRequest.d.ts.map +1 -0
- package/dist/http/badResponses/badRequest.js +52 -0
- package/dist/http/badResponses/conflict.d.ts +34 -0
- package/dist/http/badResponses/conflict.d.ts.map +1 -0
- package/dist/http/badResponses/conflict.js +52 -0
- package/dist/http/badResponses/forbidden.d.ts +34 -0
- package/dist/http/badResponses/forbidden.d.ts.map +1 -0
- package/dist/http/badResponses/forbidden.js +52 -0
- package/dist/http/badResponses/notFound.d.ts +34 -0
- package/dist/http/badResponses/notFound.d.ts.map +1 -0
- package/dist/http/badResponses/notFound.js +52 -0
- package/dist/http/badResponses/notImplemented.d.ts +34 -0
- package/dist/http/badResponses/notImplemented.d.ts.map +1 -0
- package/dist/http/badResponses/notImplemented.js +52 -0
- package/dist/http/badResponses/serverError.d.ts +34 -0
- package/dist/http/badResponses/serverError.d.ts.map +1 -0
- package/dist/http/badResponses/serverError.js +52 -0
- package/dist/http/badResponses/unauthorized.d.ts +34 -0
- package/dist/http/badResponses/unauthorized.d.ts.map +1 -0
- package/dist/http/badResponses/unauthorized.js +52 -0
- package/dist/http/badResponses/unprocessableEntity.d.ts +43 -0
- package/dist/http/badResponses/unprocessableEntity.d.ts.map +1 -0
- package/dist/http/badResponses/unprocessableEntity.js +60 -0
- package/dist/http/successResponses/__test__/created.spec.d.ts +2 -0
- package/dist/http/successResponses/__test__/created.spec.d.ts.map +1 -0
- package/dist/http/successResponses/__test__/created.spec.js +57 -0
- package/dist/http/successResponses/__test__/found.spec.d.ts +2 -0
- package/dist/http/successResponses/__test__/found.spec.d.ts.map +1 -0
- package/dist/http/successResponses/__test__/found.spec.js +57 -0
- package/dist/http/successResponses/__test__/noContent.spec.d.ts +2 -0
- package/dist/http/successResponses/__test__/noContent.spec.d.ts.map +1 -0
- package/dist/http/successResponses/__test__/noContent.spec.js +40 -0
- package/dist/http/successResponses/__test__/success.spec.d.ts +2 -0
- package/dist/http/successResponses/__test__/success.spec.d.ts.map +1 -0
- package/dist/http/successResponses/__test__/success.spec.js +60 -0
- package/dist/http/successResponses/__test__/updated.spec.d.ts +2 -0
- package/dist/http/successResponses/__test__/updated.spec.d.ts.map +1 -0
- package/dist/http/successResponses/__test__/updated.spec.js +60 -0
- package/dist/http/successResponses/created.d.ts +36 -0
- package/dist/http/successResponses/created.d.ts.map +1 -0
- package/dist/http/successResponses/created.js +54 -0
- package/dist/http/successResponses/found.d.ts +39 -0
- package/dist/http/successResponses/found.d.ts.map +1 -0
- package/dist/http/successResponses/found.js +57 -0
- package/dist/http/successResponses/noContent.d.ts +25 -0
- package/dist/http/successResponses/noContent.d.ts.map +1 -0
- package/dist/http/successResponses/noContent.js +35 -0
- package/dist/http/successResponses/success.d.ts +36 -0
- package/dist/http/successResponses/success.d.ts.map +1 -0
- package/dist/http/successResponses/success.js +54 -0
- package/dist/http/successResponses/updated.d.ts +36 -0
- package/dist/http/successResponses/updated.d.ts.map +1 -0
- package/dist/http/successResponses/updated.js +54 -0
- package/dist/index.d.ts +22 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +24 -15
- package/dist/services/__test__/decodeRequestBody.spec.d.ts +2 -0
- package/dist/services/__test__/decodeRequestBody.spec.d.ts.map +1 -0
- package/dist/services/__test__/decodeRequestBody.spec.js +39 -0
- package/dist/services/__test__/errorHandler.spec.d.ts +2 -0
- package/dist/services/__test__/errorHandler.spec.d.ts.map +1 -0
- package/dist/services/__test__/errorHandler.spec.js +115 -0
- package/dist/services/__test__/formParse.spec.d.ts +2 -0
- package/dist/services/__test__/formParse.spec.d.ts.map +1 -0
- package/dist/services/__test__/formParse.spec.js +78 -0
- package/dist/services/__test__/getCaller.spec.d.ts +2 -0
- package/dist/services/__test__/getCaller.spec.d.ts.map +1 -0
- package/dist/services/__test__/getCaller.spec.js +60 -0
- package/dist/services/__test__/getScopedParams.spec.d.ts +2 -0
- package/dist/services/__test__/getScopedParams.spec.d.ts.map +1 -0
- package/dist/services/__test__/getScopedParams.spec.js +51 -0
- package/dist/services/__test__/schemaValidator.spec.d.ts +2 -0
- package/dist/services/__test__/schemaValidator.spec.d.ts.map +1 -0
- package/dist/services/__test__/schemaValidator.spec.js +89 -0
- package/dist/services/decodeErrorMessageFromRequest.d.ts +17 -0
- package/dist/services/decodeErrorMessageFromRequest.d.ts.map +1 -0
- package/dist/services/decodeErrorMessageFromRequest.js +30 -0
- package/dist/services/decodeRequestBody.d.ts +17 -0
- package/dist/services/decodeRequestBody.d.ts.map +1 -0
- package/dist/services/decodeRequestBody.js +38 -0
- package/dist/services/errorHandler.d.ts +44 -0
- package/dist/services/errorHandler.d.ts.map +1 -0
- package/dist/services/errorHandler.js +93 -0
- package/dist/services/formParse.d.ts +40 -1
- package/dist/services/formParse.d.ts.map +1 -1
- package/dist/services/formParse.js +44 -3
- package/dist/services/getCaller.d.ts +17 -0
- package/dist/services/getCaller.d.ts.map +1 -0
- package/dist/services/getCaller.js +45 -0
- package/dist/services/getScopedParams.d.ts +26 -1
- package/dist/services/getScopedParams.d.ts.map +1 -1
- package/dist/services/getScopedParams.js +26 -2
- package/dist/services/httpDebug.d.ts +35 -0
- package/dist/services/httpDebug.d.ts.map +1 -0
- package/dist/services/httpDebug.js +56 -0
- package/dist/services/schemaValidator.d.ts +13 -0
- package/dist/services/schemaValidator.d.ts.map +1 -0
- package/dist/services/schemaValidator.js +51 -0
- package/dist/services/sendFileToS3.d.ts +50 -18
- package/dist/services/sendFileToS3.d.ts.map +1 -1
- package/dist/services/sendFileToS3.js +73 -31
- package/package.json +14 -6
- package/src/api/__test__/deleteRequest.spec.ts +107 -0
- package/src/api/__test__/getRequest.spec.ts +76 -0
- package/src/api/__test__/inboxFlowRequest.spec.ts +125 -0
- package/src/api/__test__/makeRequest.spec.ts +121 -0
- package/src/api/__test__/patchRequest.spec.ts +125 -0
- package/src/api/__test__/postRequest.spec.ts +125 -0
- package/src/api/__test__/putRequest.spec.ts +125 -0
- package/src/api/deleteRequest.ts +22 -0
- package/src/api/getRequest.ts +20 -0
- package/src/api/inboxFlowRequest.ts +76 -0
- package/src/api/makeRequest.ts +117 -0
- package/src/api/patchRequest.ts +22 -0
- package/src/api/postRequest.ts +22 -0
- package/src/api/putRequest.ts +22 -0
- package/src/config/__test__/apiInstance.spec.ts +129 -0
- package/src/config/__test__/inboxFlowInstance.spec.ts +61 -0
- package/src/config/apiInstance.ts +148 -0
- package/src/config/inboxFlowInstance.ts +65 -0
- package/src/http/badResponses/__test__/badGateway.spec.ts +50 -0
- package/src/http/badResponses/__test__/badRequest.spec.ts +50 -0
- package/src/http/badResponses/__test__/conflict.spec.ts +50 -0
- package/src/http/badResponses/__test__/forbidden.spec.ts +50 -0
- package/src/http/badResponses/__test__/notFound.spec.ts +50 -0
- package/src/http/badResponses/__test__/notImplemented.spec.ts +50 -0
- package/src/http/badResponses/__test__/serverError.spec.ts +50 -0
- package/src/http/badResponses/__test__/unauthorized.spec.ts +50 -0
- package/src/http/badResponses/__test__/unprocessableEntity.spec.ts +76 -0
- package/src/http/badResponses/badGateway.ts +63 -0
- package/src/http/badResponses/badRequest.ts +63 -0
- package/src/http/badResponses/conflict.ts +63 -0
- package/src/http/badResponses/forbidden.ts +63 -0
- package/src/http/badResponses/notFound.ts +63 -0
- package/src/http/badResponses/notImplemented.ts +63 -0
- package/src/http/badResponses/serverError.ts +63 -0
- package/src/http/badResponses/unauthorized.ts +63 -0
- package/src/http/badResponses/unprocessableEntity.ts +79 -0
- package/src/http/successResponses/__test__/created.spec.ts +65 -0
- package/src/http/successResponses/__test__/found.spec.ts +65 -0
- package/src/http/successResponses/__test__/noContent.spec.ts +49 -0
- package/src/http/successResponses/__test__/success.spec.ts +68 -0
- package/src/http/successResponses/__test__/updated.spec.ts +68 -0
- package/src/http/successResponses/created.ts +64 -0
- package/src/http/successResponses/found.ts +67 -0
- package/src/http/successResponses/noContent.ts +42 -0
- package/src/http/successResponses/success.ts +64 -0
- package/src/http/successResponses/updated.ts +64 -0
- package/src/index.ts +25 -16
- package/src/services/__test__/decodeRequestBody.spec.ts +51 -0
- package/src/services/__test__/errorHandler.spec.ts +133 -0
- package/src/services/__test__/formParse.spec.ts +96 -0
- package/src/services/__test__/getCaller.spec.ts +78 -0
- package/src/services/__test__/getScopedParams.spec.ts +68 -0
- package/src/services/__test__/schemaValidator.spec.ts +106 -0
- package/src/services/decodeErrorMessageFromRequest.ts +36 -0
- package/src/services/decodeRequestBody.ts +42 -0
- package/src/services/errorHandler.ts +99 -0
- package/src/services/formParse.ts +46 -6
- package/src/services/getCaller.ts +58 -0
- package/src/services/getScopedParams.ts +29 -2
- package/src/services/httpDebug.ts +64 -0
- package/src/services/schemaValidator.ts +66 -0
- package/src/services/sendFileToS3.ts +81 -56
- package/vitest.config.ts +5 -0
- package/dist/helpers/globalErrorHandler.d.ts +0 -3
- package/dist/helpers/globalErrorHandler.d.ts.map +0 -1
- package/dist/helpers/globalErrorHandler.js +0 -53
- package/dist/httpBadResponses/badRequest.d.ts +0 -9
- package/dist/httpBadResponses/badRequest.d.ts.map +0 -1
- package/dist/httpBadResponses/badRequest.js +0 -23
- package/dist/httpBadResponses/conflict.d.ts +0 -9
- package/dist/httpBadResponses/conflict.d.ts.map +0 -1
- package/dist/httpBadResponses/conflict.js +0 -23
- package/dist/httpBadResponses/forbidden.d.ts +0 -9
- package/dist/httpBadResponses/forbidden.d.ts.map +0 -1
- package/dist/httpBadResponses/forbidden.js +0 -23
- package/dist/httpBadResponses/notFound.d.ts +0 -9
- package/dist/httpBadResponses/notFound.d.ts.map +0 -1
- package/dist/httpBadResponses/notFound.js +0 -23
- package/dist/httpBadResponses/serverError.d.ts +0 -9
- package/dist/httpBadResponses/serverError.d.ts.map +0 -1
- package/dist/httpBadResponses/serverError.js +0 -23
- package/dist/httpBadResponses/unauthorized.d.ts +0 -9
- package/dist/httpBadResponses/unauthorized.d.ts.map +0 -1
- package/dist/httpBadResponses/unauthorized.js +0 -23
- package/dist/httpBadResponses/unprocessableEntity.d.ts +0 -17
- package/dist/httpBadResponses/unprocessableEntity.d.ts.map +0 -1
- package/dist/httpBadResponses/unprocessableEntity.js +0 -30
- package/dist/httpResponses/created.d.ts +0 -10
- package/dist/httpResponses/created.d.ts.map +0 -1
- package/dist/httpResponses/created.js +0 -30
- package/dist/httpResponses/noContent.d.ts +0 -9
- package/dist/httpResponses/noContent.d.ts.map +0 -1
- package/dist/httpResponses/noContent.js +0 -28
- package/dist/httpResponses/success.d.ts +0 -10
- package/dist/httpResponses/success.d.ts.map +0 -1
- package/dist/httpResponses/success.js +0 -30
- package/dist/httpResponses/updated.d.ts +0 -10
- package/dist/httpResponses/updated.d.ts.map +0 -1
- package/dist/httpResponses/updated.js +0 -30
- package/dist/services/extractJsonFromRequest.d.ts +0 -3
- package/dist/services/extractJsonFromRequest.d.ts.map +0 -1
- package/dist/services/extractJsonFromRequest.js +0 -29
- package/src/helpers/globalErrorHandler.ts +0 -64
- package/src/httpBadResponses/badRequest.ts +0 -29
- package/src/httpBadResponses/conflict.ts +0 -29
- package/src/httpBadResponses/forbidden.ts +0 -29
- package/src/httpBadResponses/notFound.ts +0 -29
- package/src/httpBadResponses/serverError.ts +0 -29
- package/src/httpBadResponses/unauthorized.ts +0 -29
- package/src/httpBadResponses/unprocessableEntity.ts +0 -43
- package/src/httpResponses/created.ts +0 -35
- package/src/httpResponses/noContent.ts +0 -33
- package/src/httpResponses/success.ts +0 -35
- package/src/httpResponses/updated.ts +0 -35
- 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
|
-
|
|
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,
|
|
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
|
-
|
|
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
|
|
5
|
-
|
|
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
|
-
|
|
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,
|
|
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
|
-
|
|
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 {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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,
|
|
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"}
|