@alevnyacow/nzmt 0.1.3 → 0.1.4

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/README.md CHANGED
@@ -25,6 +25,97 @@
25
25
 
26
26
  ## <a id='module'></a>Module
27
27
 
28
+ ### methods
29
+
30
+ Returns zod module method generator.
31
+
32
+ 1. How to make a method generator:
33
+
34
+ ```ts
35
+ const mathServiceMethods = methods({
36
+ // Service name, will be used in errors
37
+ name: 'MathService',
38
+ // Method schemas description
39
+ schemas: {
40
+ // Method `basicOperations`
41
+ basicOperation: {
42
+ // `basicOperations` payload (Zod schema)
43
+ payload: z.object({
44
+ lhs: z.number(),
45
+ rhs: z.number(),
46
+ operation: z.enum(['+', '-', '/', '*'])
47
+ }),
48
+ // `basicOperations` response (Zod schema)
49
+ response: z.object({
50
+ result: z.number()
51
+ })
52
+ },
53
+ // ...another methods
54
+ }
55
+ })
56
+ ```
57
+
58
+ 2. How to use this generator (example with class, you can use it also without classes):
59
+
60
+ ```ts
61
+ export class MathService {
62
+ // created generator
63
+ private methods = mathServiceMethods
64
+
65
+ public DIVIDED_BY_ZERO = 'DIVIDED_BY_ZERO'
66
+
67
+ public basicMathOperation = this.methods(
68
+ // Method name, working TS intellisense
69
+ 'basicOperation',
70
+ // handler logic
71
+ async (
72
+ // payload, also with TS intellisense
73
+ { lhs, operation, rhs },
74
+ // errors generator
75
+ { methodError }
76
+ ) => {
77
+ switch(operation) {
78
+ case '*': {
79
+ // all types are infered from schemas, so
80
+ // TS intellisense also works with return types
81
+ return { result: lhs * rhs }
82
+ }
83
+ case '+': {
84
+ return { result: lhs + rhs }
85
+ }
86
+ case '-': {
87
+ return { result: lhs - rhs }
88
+ }
89
+ case '/': {
90
+ if (rhs === 0) {
91
+ /**
92
+ * You can create error with just code. All
93
+ * metadata like method name, zod module name
94
+ * or payload will present in this error object.
95
+ */
96
+ throw methodError(DIVIDED_BY_ZERO)
97
+ }
98
+ return { result: lhs / rhs }
99
+ }
100
+ }
101
+ }
102
+ )
103
+ }
104
+ ```
105
+
106
+ 3. How to use zod module:
107
+
108
+ ```ts
109
+ const mathService = new MathService();
110
+
111
+ // types infered from zod schemas, working intellisense
112
+ const { result } = mathService.basicMathOperation({
113
+ lhs: 10,
114
+ rhs: 15,
115
+ operation: '+'
116
+ })
117
+ ```
118
+
28
119
  ## <a id='controller'></a>Controller
29
120
 
30
121
  ## <a id='store'></a>Store
package/bin/cli.js CHANGED
@@ -128,7 +128,7 @@ function generateStores(lowerCase, upperCase) {
128
128
  "",
129
129
  "\tactionsPayload: {",
130
130
  config?.paths?.entities ? `\t\tcreate: ${upperCase}.schema.omit({ id: true }),` : "\t\tcreate: z.object({ }),",
131
- config?.paths?.entities ? `\t\tupdate: ${upperCase}.schema.omit({ id: true }),` : "\t\tupdate: z.object({ }),",
131
+ config?.paths?.entities ? `\t\tupdate: ${upperCase}.schema.omit({ id: true }).partial(),` : "\t\tupdate: z.object({ }),",
132
132
  "\t},",
133
133
  "",
134
134
  `\tname: '${upperCase}Store'`,
package/dist/index.cjs CHANGED
@@ -163,7 +163,7 @@ const methods = (metadata, sharedConfig = {})=>(methodName, handler, config = {}
163
163
  methodError: (payload, cause)=>ErrorFactory.forModule(name).inMethod(methodName).newError('string' == typeof payload ? {
164
164
  error: payload,
165
165
  code: payload,
166
- details: payload
166
+ details: parsedPayload
167
167
  } : payload, cause)
168
168
  });
169
169
  const parsedResponse = schemas[methodName].response.parse(response);
package/dist/index.js CHANGED
@@ -127,7 +127,7 @@ const methods = (metadata, sharedConfig = {})=>(methodName, handler, config = {}
127
127
  methodError: (payload, cause)=>ErrorFactory.forModule(name).inMethod(methodName).newError('string' == typeof payload ? {
128
128
  error: payload,
129
129
  code: payload,
130
- details: payload
130
+ details: parsedPayload
131
131
  } : payload, cause)
132
132
  });
133
133
  const parsedResponse = schemas[methodName].response.parse(response);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alevnyacow/nzmt",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Next Zod Modules Toolkit",
5
5
  "repository": {
6
6
  "type": "git",