@alevnyacow/nzmt 0.26.4 → 0.27.0
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 +38 -27
- package/bin/cli.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -126,6 +126,12 @@ export default async function Page() {
|
|
|
126
126
|
|
|
127
127
|
# Scaffolder commands glossary
|
|
128
128
|
|
|
129
|
+
## Initialization
|
|
130
|
+
|
|
131
|
+
| Command | Scaffolding result |Options|
|
|
132
|
+
|---------|-------------|-------|
|
|
133
|
+
| `npx nzmt init` | **init**ialization |pass `prismaClientPath:` to work with Prisma. E.g. `npx nzmt init prismaClientPath:@/generated/prisma/client`|
|
|
134
|
+
|
|
129
135
|
## Complex scaffolding
|
|
130
136
|
|
|
131
137
|
| Command | Scaffolding result |
|
|
@@ -135,18 +141,23 @@ export default async function Page() {
|
|
|
135
141
|
| `npx nzmt se <name>` | **s**tored **e**ntity: entity + store (contracts linked). |
|
|
136
142
|
| `npx nzmt rq` | API **r**outes and React **q**ueries for all of your controllers. This command will also remove endpoints which don't exist anymore with according React query hooks |
|
|
137
143
|
|
|
138
|
-
##
|
|
144
|
+
## Primary server modules scaffolding
|
|
139
145
|
|
|
140
146
|
| Command | Scaffolding result |Options|
|
|
141
147
|
|---------|-------------|-------|
|
|
142
148
|
| `npx nzmt e <name>` | **e**ntity ||
|
|
143
149
|
| `npx nzmt vo <name>` | **v**alue **o**bject ||
|
|
144
150
|
| `npx nzmt cs <name>` | **c**ustom **s**tore (all schemas are `z.object({})`) ||
|
|
145
|
-
| `npx nzmt p <name>` | **p**rovider | |
|
|
146
151
|
| `npx nzmt s <name>` | **s**ervice |`i:UserStore,Logger` will automatically inject `UserStore` and `Logger`. E.g. `npx nzmt s shop i:UserStore,ProductStore` will create `ShopService` with already injected `UserStore` and `ProductStore`|
|
|
147
152
|
| `npx nzmt c <name>` | **c**ontroller |`i:UserService` will automatically inject `UserService`. `Logger` and `Guards` are injected by default regardless of `i:` option|
|
|
148
153
|
|
|
149
|
-
|
|
154
|
+
## Auxiliary server modules scaffolding
|
|
155
|
+
|
|
156
|
+
| Command | Scaffolding result |
|
|
157
|
+
|---------|-------------|
|
|
158
|
+
| `npx nzmt p <name>` | **p**rovider |
|
|
159
|
+
| `npx nzmt i <name>` | **i**nfrastructure module |
|
|
160
|
+
|
|
150
161
|
|
|
151
162
|
# How to implement your own methods
|
|
152
163
|
|
|
@@ -163,13 +174,22 @@ They:
|
|
|
163
174
|
Service method description example:
|
|
164
175
|
|
|
165
176
|
```ts
|
|
177
|
+
// ...some service metadata
|
|
166
178
|
orderDetails: {
|
|
167
|
-
payload: Order.schema.pick({
|
|
179
|
+
payload: Order.schema.pick({
|
|
180
|
+
name: true,
|
|
181
|
+
createdDate: true
|
|
182
|
+
}),
|
|
168
183
|
response: z.object({
|
|
169
184
|
user: User.schema,
|
|
170
|
-
products: z.array(
|
|
185
|
+
products: z.array(
|
|
186
|
+
Product.schema.omit({
|
|
187
|
+
price: true
|
|
188
|
+
})
|
|
189
|
+
)
|
|
171
190
|
})
|
|
172
191
|
}
|
|
192
|
+
// ...some service metadata
|
|
173
193
|
```
|
|
174
194
|
|
|
175
195
|
## Services
|
|
@@ -177,20 +197,24 @@ orderDetails: {
|
|
|
177
197
|
1. **Define method in metadata** (`*.service.metadata.ts`):
|
|
178
198
|
|
|
179
199
|
```ts
|
|
200
|
+
// ...service metadata
|
|
180
201
|
foo: {
|
|
181
202
|
request: z.object({ requestString: z.string() }),
|
|
182
203
|
response: z.object({ responseNumber: z.number() })
|
|
183
204
|
}
|
|
205
|
+
// ...service metadata
|
|
184
206
|
```
|
|
185
207
|
|
|
186
208
|
2. **Implement it in service** (`*.service.ts`):
|
|
187
209
|
|
|
188
210
|
```ts
|
|
189
|
-
//
|
|
211
|
+
// ...service class implementation
|
|
190
212
|
foo = this.methods('foo', async ({ requestString }) => {
|
|
213
|
+
// 'foo' string is strongly-typed, don't worry
|
|
191
214
|
// all input and output types are also infered
|
|
192
215
|
return Number(requestString)
|
|
193
216
|
})
|
|
217
|
+
// ..service class implementation
|
|
194
218
|
```
|
|
195
219
|
|
|
196
220
|
## Controllers
|
|
@@ -200,43 +224,30 @@ Same idea, but metadata uses optional `query`, optional `body`, and `response`.
|
|
|
200
224
|
1. **Metadata** (`*.controller.metadata.ts`):
|
|
201
225
|
|
|
202
226
|
```ts
|
|
227
|
+
// ...controller metadata
|
|
203
228
|
POST: {
|
|
204
229
|
query: z.object({ id: z.string() }),
|
|
205
230
|
body: z.object({ delta: z.number() }),
|
|
206
231
|
response: z.object({ success: z.boolean() })
|
|
207
232
|
}
|
|
233
|
+
// ...controller metadata
|
|
208
234
|
```
|
|
209
235
|
|
|
210
236
|
2. **Implementation** (`*.controller.ts`):
|
|
211
237
|
|
|
238
|
+
`query` and `body` are merged into one object in implementation
|
|
239
|
+
|
|
212
240
|
```ts
|
|
241
|
+
// ...controller class implementation
|
|
213
242
|
POST = this.endpoints('POST', async ({ id, delta }) => {
|
|
214
243
|
return { success: true }
|
|
215
244
|
})
|
|
245
|
+
// ..controller class implementation
|
|
216
246
|
```
|
|
217
247
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
## Usage in Next.js
|
|
248
|
+
### React-queries and API routes
|
|
221
249
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
```ts
|
|
225
|
-
// api/user-controller/route.ts
|
|
226
|
-
const controller = fromDI<UserController>('UserController')
|
|
227
|
-
|
|
228
|
-
export const GET = controller.GET
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
And servers can be used directlt as Server Actions:
|
|
232
|
-
|
|
233
|
-
```tsx
|
|
234
|
-
export default async function() {
|
|
235
|
-
const service = fromDI<UserService>('UserService')
|
|
236
|
-
const users = await service.getList({ filter: {} })
|
|
237
|
-
// ...
|
|
238
|
-
}
|
|
239
|
-
```
|
|
250
|
+
Once you done implementing controller methods, just run `nmx nzmt rq`. This command will generate up-to-date API routes and React Query hooks for all your controllers. You can call it also, for example, in a pre-commit hook so that your backend and frontend integration is always kept in sync.
|
|
240
251
|
|
|
241
252
|
# FAQ
|
|
242
253
|
|
package/bin/cli.js
CHANGED
|
@@ -863,7 +863,7 @@ function generateProvider(lowerCase, upperCase) {
|
|
|
863
863
|
`import { PublicFields } from '@/${config.paths.infrastructure}/ts-helpers'`,
|
|
864
864
|
`import { ${upperCase}Provider } from './${entityName}.provider'`,
|
|
865
865
|
'',
|
|
866
|
-
`export class ${upperCase}MockProvider implements
|
|
866
|
+
`export class ${upperCase}MockProvider implements PublicFields<${upperCase}Provider> {`,
|
|
867
867
|
`\t`,
|
|
868
868
|
`}`
|
|
869
869
|
].join('\n'))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alevnyacow/nzmt",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.27.0",
|
|
4
4
|
"description": "Next Zod Modules Toolkit",
|
|
5
5
|
"keywords": ["next", "full-stack", "server", "backend", "cli", "scaffolding", "zod", "rest", "contract programming", "contract-first", "react-query", "ddd", "domain-driven"],
|
|
6
6
|
"repository": {
|