@arkyn/server 2.0.1-beta.4 → 2.0.1-beta.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkyn/server",
3
- "version": "2.0.1-beta.4",
3
+ "version": "2.0.1-beta.6",
4
4
  "main": "./dist/bundle.js",
5
5
  "module": "./src/index.ts",
6
6
  "type": "module",
@@ -1,4 +1,4 @@
1
- import { httpDebug } from "../httpDebug";
1
+ import { httpDebug } from "../../services/httpDebug";
2
2
 
3
3
  type UnprocessableEntityProps = {
4
4
  data?: any;
@@ -12,7 +12,7 @@ class UnprocessableEntity {
12
12
  status: number = 422;
13
13
  statusText: string;
14
14
 
15
- constructor(props: UnprocessableEntityProps) {
15
+ constructor(props: UnprocessableEntityProps, enableDebug = false) {
16
16
  this.statusText = props.message || "Unprocessable Entity";
17
17
  this.body = {
18
18
  name: "UnprocessableEntity",
@@ -22,7 +22,7 @@ class UnprocessableEntity {
22
22
  fields: props.fields,
23
23
  };
24
24
 
25
- httpDebug("UnprocessableEntity", this.body);
25
+ enableDebug && httpDebug("UnprocessableEntity", this.body);
26
26
  }
27
27
 
28
28
  toResponse() {
package/src/index.ts CHANGED
@@ -17,5 +17,8 @@ export { Updated } from "./http/successResponses/updated";
17
17
  export { decodeRequestBody } from "./services/decodeRequestBody";
18
18
  export { errorHandler } from "./services/errorHandler";
19
19
  export { formParse } from "./services/formParse";
20
+ export { getCaller } from "./services/getCaller";
20
21
  export { getScopedParams } from "./services/getScopedParams";
22
+ export { httpDebug } from "./services/httpDebug";
21
23
  export { sendFileToS3 } from "./services/sendFileToS3";
24
+ export { Validator } from "./services/validator";
@@ -0,0 +1,40 @@
1
+ import path from "path";
2
+
3
+ function getCaller() {
4
+ // Diretório raiz do projeto
5
+ const projectRoot = process.cwd();
6
+
7
+ // Captura a stack trace
8
+ const error = new Error();
9
+ const stackLines = error.stack?.split("\n").map((line) => line.trim()) || [];
10
+
11
+ let callerInfo = "Unknown caller";
12
+ let functionName = "Unknown function";
13
+
14
+ for (const line of stackLines) {
15
+ if (
16
+ !line.includes("httpDebug") &&
17
+ !line.includes("BadGateway") &&
18
+ !line.includes("BadRequest")
19
+ ) {
20
+ // Captura formatos diferentes de stack trace
21
+ const match = line.match(/at (.+?) \((.+?)\)/) || line.match(/at (.+)/);
22
+ if (match) {
23
+ functionName = match[1].split(" ")[0]; // Nome da função
24
+ let fullPath = match[2] || match[1]; // Caminho absoluto do arquivo
25
+
26
+ // Transforma caminho absoluto em relativo ao projeto
27
+ if (fullPath.startsWith(projectRoot)) {
28
+ fullPath = path.relative(projectRoot, fullPath);
29
+ }
30
+
31
+ callerInfo = fullPath;
32
+ break;
33
+ }
34
+ }
35
+ }
36
+
37
+ return { functionName, callerInfo };
38
+ }
39
+
40
+ export { getCaller };
@@ -0,0 +1,23 @@
1
+ import { getCaller } from "../services/getCaller";
2
+
3
+ function httpDebug(name: string, body: any, cause?: any) {
4
+ const isDebugMode =
5
+ process.env.NODE_ENV === "development" ||
6
+ process.env?.SHOW_ERRORS_IN_CONSOLE === "true";
7
+
8
+ if (isDebugMode) {
9
+ const reset = "\x1b[0m";
10
+ const cyan = "\x1b[36m";
11
+
12
+ const debugName = `${cyan}[ARKYN-DEBUG]${reset}`;
13
+ const { callerInfo, functionName } = getCaller();
14
+
15
+ console.log(`${debugName} ${name} initialized`);
16
+ console.log(`${debugName} Caller Function: ${functionName}`);
17
+ console.log(`${debugName} Caller Location: ${callerInfo}`);
18
+ console.log(`${debugName} Body:`, body);
19
+ if (cause) console.log(`[ARKYN-DEBUG] Cause:`, cause);
20
+ }
21
+ }
22
+
23
+ export { httpDebug };
@@ -0,0 +1,66 @@
1
+ import { Schema, z } from "zod";
2
+
3
+ import { ServerError } from "../http/badResponses/serverError";
4
+ import { UnprocessableEntity } from "../http/badResponses/unprocessableEntity";
5
+ import { formParse } from "./formParse";
6
+ import { getCaller } from "./getCaller";
7
+ import { httpDebug } from "./httpDebug";
8
+
9
+ function formatErrorMessage(error: z.ZodError) {
10
+ const title = "Error validating:";
11
+ const lines = error.issues.map(
12
+ ({ path, message }) => `-> ${path.join(".")}: ${message}`
13
+ );
14
+
15
+ return [title, ...lines].join("\n");
16
+ }
17
+
18
+ class Validator<T extends Schema> {
19
+ functionName: string;
20
+ callerInfo: string;
21
+
22
+ constructor(readonly schema: T) {
23
+ const { callerInfo, functionName } = getCaller();
24
+ this.callerInfo = callerInfo;
25
+ this.functionName = functionName;
26
+ }
27
+
28
+ isValid(data: any): boolean {
29
+ return this.schema.safeParse(data).success;
30
+ }
31
+
32
+ safeValidate(data: any): z.SafeParseReturnType<z.infer<T>, z.infer<T>> {
33
+ return this.schema.safeParse(data);
34
+ }
35
+
36
+ validate(data: any): z.infer<T> {
37
+ try {
38
+ return this.schema.parse(data);
39
+ } catch (error: any) {
40
+ throw new ServerError(formatErrorMessage(error));
41
+ }
42
+ }
43
+
44
+ formValidate(data: any, message?: string): z.infer<T> {
45
+ const formParsed = formParse([data, this.schema]);
46
+
47
+ if (!formParsed.success) {
48
+ httpDebug("UnprocessableEntity", formParsed);
49
+ const firstErrorKey = Object.keys(formParsed.fieldErrors)[0];
50
+
51
+ throw new UnprocessableEntity(
52
+ {
53
+ fields: formParsed.fields,
54
+ fieldErrors: formParsed.fieldErrors,
55
+ data: { scrollTo: firstErrorKey },
56
+ message,
57
+ },
58
+ false
59
+ );
60
+ }
61
+
62
+ return formParsed.data;
63
+ }
64
+ }
65
+
66
+ export { Validator };
@@ -1,43 +0,0 @@
1
- function httpDebug(name: string, body: any, cause?: any) {
2
- const isDebugMode =
3
- process.env.NODE_ENV === "development" ||
4
- process.env?.SHOW_ERRORS_IN_CONSOLE === "true";
5
-
6
- if (isDebugMode) {
7
- const reset = "\x1b[0m";
8
- const cyan = "\x1b[36m";
9
-
10
- const debugName = `${cyan}[ARKYN-DEBUG]${reset}`;
11
-
12
- const error = new Error();
13
- const stackLines =
14
- error.stack?.split("\n").map((line) => line.trim()) || [];
15
-
16
- let callerInfo = "Unknown caller";
17
- let functionName = "Unknown function";
18
-
19
- for (const line of stackLines) {
20
- if (
21
- !line.includes("httpDebug") &&
22
- !line.includes("BadGateway") &&
23
- !line.includes("BadRequest")
24
- ) {
25
- // Captura formatos diferentes de stack trace
26
- const match = line.match(/at (.+?) \((.+?)\)/) || line.match(/at (.+)/);
27
- if (match) {
28
- functionName = match[1].split(" ")[0]; // Pega apenas o nome da função
29
- callerInfo = match[2] || match[1]; // Caminho do arquivo e linha
30
- break;
31
- }
32
- }
33
- }
34
-
35
- console.log(`${debugName} ${name} initialized`);
36
- console.log(`${debugName} Caller Function: ${functionName}`);
37
- console.log(`${debugName} Caller Location: ${callerInfo}`);
38
- console.log(`${debugName} Body:`, body);
39
- if (cause) console.log(`[ARKYN-DEBUG] Cause:`, cause);
40
- }
41
- }
42
-
43
- export { httpDebug };