@arkyn/server 2.0.1-beta.5 → 2.0.1-beta.7

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.5",
3
+ "version": "2.0.1-beta.7",
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 { SchemaValidator } from "./services/schemaValidator";
@@ -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 SchemaValidator<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 { SchemaValidator };
File without changes