@alevnyacow/nzmt 0.1.3 → 0.1.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/README.md +93 -0
- package/bin/cli.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,6 +25,99 @@
|
|
|
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
|
+
import { Module } from '@alevnyacow/nzmt'
|
|
36
|
+
|
|
37
|
+
const mathServiceMethods = Module.methods({
|
|
38
|
+
// Service name, will be used in errors
|
|
39
|
+
name: 'MathService',
|
|
40
|
+
// Method schemas description
|
|
41
|
+
schemas: {
|
|
42
|
+
// Method `basicOperations`
|
|
43
|
+
basicOperation: {
|
|
44
|
+
// `basicOperations` payload (Zod schema)
|
|
45
|
+
payload: z.object({
|
|
46
|
+
lhs: z.number(),
|
|
47
|
+
rhs: z.number(),
|
|
48
|
+
operation: z.enum(['+', '-', '/', '*'])
|
|
49
|
+
}),
|
|
50
|
+
// `basicOperations` response (Zod schema)
|
|
51
|
+
response: z.object({
|
|
52
|
+
result: z.number()
|
|
53
|
+
})
|
|
54
|
+
},
|
|
55
|
+
// ...another methods
|
|
56
|
+
}
|
|
57
|
+
})
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
2. How to use this generator (example with class, you can use it also without classes):
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
export class MathService {
|
|
64
|
+
// created generator
|
|
65
|
+
private methods = mathServiceMethods
|
|
66
|
+
|
|
67
|
+
public DIVIDED_BY_ZERO = 'DIVIDED_BY_ZERO'
|
|
68
|
+
|
|
69
|
+
public basicMathOperation = this.methods(
|
|
70
|
+
// Method name, working TS intellisense
|
|
71
|
+
'basicOperation',
|
|
72
|
+
// handler logic
|
|
73
|
+
async (
|
|
74
|
+
// payload, also with TS intellisense
|
|
75
|
+
{ lhs, operation, rhs },
|
|
76
|
+
// errors generator
|
|
77
|
+
{ methodError }
|
|
78
|
+
) => {
|
|
79
|
+
switch(operation) {
|
|
80
|
+
case '*': {
|
|
81
|
+
// all types are infered from schemas, so
|
|
82
|
+
// TS intellisense also works with return types
|
|
83
|
+
return { result: lhs * rhs }
|
|
84
|
+
}
|
|
85
|
+
case '+': {
|
|
86
|
+
return { result: lhs + rhs }
|
|
87
|
+
}
|
|
88
|
+
case '-': {
|
|
89
|
+
return { result: lhs - rhs }
|
|
90
|
+
}
|
|
91
|
+
case '/': {
|
|
92
|
+
if (rhs === 0) {
|
|
93
|
+
/**
|
|
94
|
+
* You can create error with just code. All
|
|
95
|
+
* metadata like method name, zod module name
|
|
96
|
+
* or payload will present in this error object.
|
|
97
|
+
*/
|
|
98
|
+
throw methodError(DIVIDED_BY_ZERO)
|
|
99
|
+
}
|
|
100
|
+
return { result: lhs / rhs }
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
3. How to use zod module:
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
const mathService = new MathService();
|
|
112
|
+
|
|
113
|
+
// types infered from zod schemas, working intellisense
|
|
114
|
+
const { result } = mathService.basicMathOperation({
|
|
115
|
+
lhs: 10,
|
|
116
|
+
rhs: 15,
|
|
117
|
+
operation: '+'
|
|
118
|
+
})
|
|
119
|
+
```
|
|
120
|
+
|
|
28
121
|
## <a id='controller'></a>Controller
|
|
29
122
|
|
|
30
123
|
## <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:
|
|
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:
|
|
130
|
+
details: parsedPayload
|
|
131
131
|
} : payload, cause)
|
|
132
132
|
});
|
|
133
133
|
const parsedResponse = schemas[methodName].response.parse(response);
|