@alevnyacow/nzmt 0.0.6 → 0.0.8
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/dist/index.cjs +176 -174
- package/dist/index.d.ts +2 -3
- package/dist/index.js +169 -164
- package/dist/store/index.d.ts +3 -3
- package/dist/store/store.ram.utils.d.ts +356 -68
- package/dist/store/store.shared-models.utils.d.ts +2 -14
- package/dist/store/store.zod.utils.d.ts +62 -50
- package/dist/zod-controller.utils.d.ts +1 -1
- package/dist/zod-module.utils.d.ts +6 -6
- package/package.json +1 -2
package/dist/index.js
CHANGED
|
@@ -1,44 +1,18 @@
|
|
|
1
1
|
import { __webpack_require__ } from "./rslib-runtime.js";
|
|
2
|
-
import { v4 } from "uuid";
|
|
3
|
-
import zod from "zod";
|
|
4
2
|
import { NextResponse } from "next/server";
|
|
3
|
+
import zod from "zod";
|
|
4
|
+
var store_namespaceObject = {};
|
|
5
|
+
__webpack_require__.r(store_namespaceObject);
|
|
6
|
+
__webpack_require__.d(store_namespaceObject, {
|
|
7
|
+
InRAM: ()=>InRAM,
|
|
8
|
+
methods: ()=>methods
|
|
9
|
+
});
|
|
5
10
|
var zod_controller_utils_namespaceObject = {};
|
|
6
11
|
__webpack_require__.r(zod_controller_utils_namespaceObject);
|
|
7
12
|
__webpack_require__.d(zod_controller_utils_namespaceObject, {
|
|
8
13
|
DefaultErrorCodes: ()=>zod_controller_utils_DefaultErrorCodes,
|
|
9
14
|
endpoints: ()=>endpoints
|
|
10
15
|
});
|
|
11
|
-
class Pagination {
|
|
12
|
-
data;
|
|
13
|
-
static schema = zod.object({
|
|
14
|
-
zeroBasedIndex: zod.coerce.number().int().nonnegative(),
|
|
15
|
-
pageSize: zod.coerce.number().int().positive()
|
|
16
|
-
});
|
|
17
|
-
constructor(data){
|
|
18
|
-
this.data = data;
|
|
19
|
-
}
|
|
20
|
-
static create = (data)=>{
|
|
21
|
-
const parsedModel = Pagination.schema.parse(data);
|
|
22
|
-
return new Pagination(parsedModel);
|
|
23
|
-
};
|
|
24
|
-
get model() {
|
|
25
|
-
return this.data;
|
|
26
|
-
}
|
|
27
|
-
same = (pagination)=>pagination.model.pageSize === this.model.pageSize && pagination.model.zeroBasedIndex === this.model.zeroBasedIndex;
|
|
28
|
-
get nextPage() {
|
|
29
|
-
return Pagination.create({
|
|
30
|
-
pageSize: this.data.pageSize,
|
|
31
|
-
zeroBasedIndex: this.data.zeroBasedIndex + 1
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
get previousPage() {
|
|
35
|
-
if (0 === this.model.zeroBasedIndex) return Pagination.create(this.model);
|
|
36
|
-
return Pagination.create({
|
|
37
|
-
pageSize: this.model.pageSize,
|
|
38
|
-
zeroBasedIndex: this.model.zeroBasedIndex - 1
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
16
|
class ErrorFactory {
|
|
43
17
|
static isControllerError = (error)=>{
|
|
44
18
|
if (!error || 'object' != typeof error) return false;
|
|
@@ -161,136 +135,6 @@ const zodModuleMethodFactory = (metadata, sharedConfig = {})=>(methodName, handl
|
|
|
161
135
|
}
|
|
162
136
|
};
|
|
163
137
|
};
|
|
164
|
-
const mapStoreSchemasToModuleMetadata = (schemas, name)=>({
|
|
165
|
-
name,
|
|
166
|
-
schemas: {
|
|
167
|
-
...schemas.customOperations ?? {},
|
|
168
|
-
list: {
|
|
169
|
-
payload: zod.object({
|
|
170
|
-
filter: schemas.searchPayload.list,
|
|
171
|
-
pagination: Pagination.schema.optional()
|
|
172
|
-
}),
|
|
173
|
-
response: zod.array(schemas.models.list)
|
|
174
|
-
},
|
|
175
|
-
details: {
|
|
176
|
-
payload: zod.object({
|
|
177
|
-
filter: schemas.searchPayload.specific
|
|
178
|
-
}),
|
|
179
|
-
response: schemas.models.details.nullable()
|
|
180
|
-
},
|
|
181
|
-
create: {
|
|
182
|
-
payload: zod.object({
|
|
183
|
-
payload: schemas.actionsPayload.create
|
|
184
|
-
}),
|
|
185
|
-
response: zod.object({
|
|
186
|
-
id: zod.string()
|
|
187
|
-
})
|
|
188
|
-
},
|
|
189
|
-
updateOne: {
|
|
190
|
-
payload: zod.object({
|
|
191
|
-
filter: schemas.searchPayload.specific,
|
|
192
|
-
payload: schemas.actionsPayload.update
|
|
193
|
-
}),
|
|
194
|
-
response: zod.object({
|
|
195
|
-
success: zod.boolean()
|
|
196
|
-
})
|
|
197
|
-
},
|
|
198
|
-
deleteOne: {
|
|
199
|
-
payload: zod.object({
|
|
200
|
-
filter: schemas.searchPayload.specific
|
|
201
|
-
}),
|
|
202
|
-
response: zod.object({
|
|
203
|
-
success: zod.boolean()
|
|
204
|
-
})
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
const zodStoreMethodFactory = (schemas)=>{
|
|
209
|
-
const data = mapStoreSchemasToModuleMetadata(schemas, schemas.name);
|
|
210
|
-
return zodModuleMethodFactory(data);
|
|
211
|
-
};
|
|
212
|
-
const store_ram_utils_RAMStore = (schemas, options)=>{
|
|
213
|
-
class RAMStore {
|
|
214
|
-
___data = [];
|
|
215
|
-
method = zodStoreMethodFactory(schemas);
|
|
216
|
-
___listSearchLogic = (entity, pattern)=>{
|
|
217
|
-
const patternKeys = Object.entries(pattern).filter(([_, value])=>!!value).map((x)=>x[0]);
|
|
218
|
-
const entityAsObject = entity;
|
|
219
|
-
return Object.entries(entityAsObject).filter(([key])=>patternKeys.includes(key)).every(([key, value])=>value === pattern[key] || value?.toString()?.includes(pattern[key]?.toString()));
|
|
220
|
-
};
|
|
221
|
-
___specificSearchLogic = (entity, pattern)=>{
|
|
222
|
-
const patternKeys = Object.entries(pattern).filter(([_, value])=>!!value).map((x)=>x[0]);
|
|
223
|
-
const entityAsObject = entity;
|
|
224
|
-
return Object.entries(entityAsObject).filter(([key])=>patternKeys.includes(key)).every(([key, value])=>value === pattern[key] || value?.toString()?.includes(pattern[key]?.toString()));
|
|
225
|
-
};
|
|
226
|
-
___mapDetailToList = (entity)=>entity;
|
|
227
|
-
___mapCreatePayloadToDetail = (payload)=>({
|
|
228
|
-
...payload,
|
|
229
|
-
id: v4()
|
|
230
|
-
});
|
|
231
|
-
___mapUpdatePayloadToDetail = (prevValue, update)=>({
|
|
232
|
-
...prevValue,
|
|
233
|
-
...update
|
|
234
|
-
});
|
|
235
|
-
constructor(){
|
|
236
|
-
if (!options) return;
|
|
237
|
-
if (options.searchLogic) {
|
|
238
|
-
if (options.searchLogic.list) this.___listSearchLogic = options.searchLogic.list;
|
|
239
|
-
if (options.searchLogic.specific) this.___specificSearchLogic = options.searchLogic.specific;
|
|
240
|
-
}
|
|
241
|
-
if (options.mappers) {
|
|
242
|
-
if (options.mappers.detailToList) this.___mapDetailToList = options.mappers.detailToList;
|
|
243
|
-
if (options.mappers.createPayloadToDetail) this.___mapCreatePayloadToDetail = options.mappers.createPayloadToDetail;
|
|
244
|
-
if (options.mappers.updatePayloadToDetail) this.___mapUpdatePayloadToDetail = options.mappers.updatePayloadToDetail;
|
|
245
|
-
}
|
|
246
|
-
if (options.initialData) this.___data = [
|
|
247
|
-
...options.initialData
|
|
248
|
-
];
|
|
249
|
-
}
|
|
250
|
-
list = this.method('list', async ({ filter, pagination = {
|
|
251
|
-
pageSize: 1000,
|
|
252
|
-
zeroBasedIndex: 0
|
|
253
|
-
} })=>{
|
|
254
|
-
schemas.searchPayload.list.parse(filter);
|
|
255
|
-
Pagination.schema.parse(pagination);
|
|
256
|
-
const afterFiltration = this.___data.filter((x)=>this.___listSearchLogic(x, filter));
|
|
257
|
-
const afterPagination = afterFiltration.filter((_, i)=>i >= pagination.pageSize * pagination.zeroBasedIndex && i < (pagination.zeroBasedIndex + 1) * pagination.pageSize);
|
|
258
|
-
const result = afterPagination.map((x)=>this.___mapDetailToList(x));
|
|
259
|
-
zod.array(schemas.models.list).parse(result);
|
|
260
|
-
return result;
|
|
261
|
-
});
|
|
262
|
-
details = this.method('details', async ({ filter })=>{
|
|
263
|
-
const details = this.___data.find((x)=>this.___specificSearchLogic(x, filter));
|
|
264
|
-
if (!details) return null;
|
|
265
|
-
return details;
|
|
266
|
-
});
|
|
267
|
-
create = this.method('create', async ({ payload })=>{
|
|
268
|
-
const newItem = this.___mapCreatePayloadToDetail(payload);
|
|
269
|
-
this.___data = this.___data.concat(newItem);
|
|
270
|
-
return {
|
|
271
|
-
id: newItem.id
|
|
272
|
-
};
|
|
273
|
-
});
|
|
274
|
-
updateOne = this.method('updateOne', async ({ filter, payload })=>{
|
|
275
|
-
const index = this.___data.findIndex((x)=>this.___specificSearchLogic(x, filter));
|
|
276
|
-
this.___data = this.___data.map((x, i)=>{
|
|
277
|
-
if (i !== index) return x;
|
|
278
|
-
return this.___mapUpdatePayloadToDetail(x, payload);
|
|
279
|
-
});
|
|
280
|
-
return {
|
|
281
|
-
success: index > -1
|
|
282
|
-
};
|
|
283
|
-
});
|
|
284
|
-
deleteOne = this.method('deleteOne', async ({ filter })=>{
|
|
285
|
-
const index = this.___data.findIndex((x)=>this.___specificSearchLogic(x, filter));
|
|
286
|
-
this.___data = this.___data.filter((_x, i)=>i !== index);
|
|
287
|
-
return {
|
|
288
|
-
success: index > -1
|
|
289
|
-
};
|
|
290
|
-
});
|
|
291
|
-
}
|
|
292
|
-
return RAMStore;
|
|
293
|
-
};
|
|
294
138
|
var zod_controller_utils_DefaultErrorCodes = /*#__PURE__*/ function(DefaultErrorCodes) {
|
|
295
139
|
DefaultErrorCodes["REQUEST_PARSING"] = "ZOD-CONTROLLER___REQUEST-PARSING";
|
|
296
140
|
DefaultErrorCodes["RESPONSE_PARSING"] = "ZOD-CONTROLLER___RESPONSE-PARSING";
|
|
@@ -456,4 +300,165 @@ const endpoints = (metadata, sharedConfig = {})=>{
|
|
|
456
300
|
};
|
|
457
301
|
return endpointLogic;
|
|
458
302
|
};
|
|
459
|
-
|
|
303
|
+
class Pagination {
|
|
304
|
+
data;
|
|
305
|
+
static schema = zod.object({
|
|
306
|
+
zeroBasedIndex: zod.coerce.number().int().nonnegative(),
|
|
307
|
+
pageSize: zod.coerce.number().int().positive()
|
|
308
|
+
});
|
|
309
|
+
constructor(data){
|
|
310
|
+
this.data = data;
|
|
311
|
+
}
|
|
312
|
+
static create = (data)=>{
|
|
313
|
+
const parsedModel = Pagination.schema.parse(data);
|
|
314
|
+
return new Pagination(parsedModel);
|
|
315
|
+
};
|
|
316
|
+
get model() {
|
|
317
|
+
return this.data;
|
|
318
|
+
}
|
|
319
|
+
same = (pagination)=>pagination.model.pageSize === this.model.pageSize && pagination.model.zeroBasedIndex === this.model.zeroBasedIndex;
|
|
320
|
+
get nextPage() {
|
|
321
|
+
return Pagination.create({
|
|
322
|
+
pageSize: this.data.pageSize,
|
|
323
|
+
zeroBasedIndex: this.data.zeroBasedIndex + 1
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
get previousPage() {
|
|
327
|
+
if (0 === this.model.zeroBasedIndex) return Pagination.create(this.model);
|
|
328
|
+
return Pagination.create({
|
|
329
|
+
pageSize: this.model.pageSize,
|
|
330
|
+
zeroBasedIndex: this.model.zeroBasedIndex - 1
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
const mapStoreSchemasToModuleMetadata = (schemas, name)=>({
|
|
335
|
+
name,
|
|
336
|
+
schemas: {
|
|
337
|
+
...schemas.customOperations ?? {},
|
|
338
|
+
list: {
|
|
339
|
+
payload: zod.object({
|
|
340
|
+
filter: schemas.searchPayload.list,
|
|
341
|
+
pagination: Pagination.schema.optional()
|
|
342
|
+
}),
|
|
343
|
+
response: zod.array(schemas.models.list)
|
|
344
|
+
},
|
|
345
|
+
details: {
|
|
346
|
+
payload: zod.object({
|
|
347
|
+
filter: schemas.searchPayload.specific
|
|
348
|
+
}),
|
|
349
|
+
response: schemas.models.details.nullable()
|
|
350
|
+
},
|
|
351
|
+
create: {
|
|
352
|
+
payload: zod.object({
|
|
353
|
+
payload: schemas.actionsPayload.create
|
|
354
|
+
}),
|
|
355
|
+
response: zod.object({
|
|
356
|
+
id: zod.string()
|
|
357
|
+
})
|
|
358
|
+
},
|
|
359
|
+
updateOne: {
|
|
360
|
+
payload: zod.object({
|
|
361
|
+
filter: schemas.searchPayload.specific,
|
|
362
|
+
payload: schemas.actionsPayload.update
|
|
363
|
+
}),
|
|
364
|
+
response: zod.object({
|
|
365
|
+
success: zod.boolean()
|
|
366
|
+
})
|
|
367
|
+
},
|
|
368
|
+
deleteOne: {
|
|
369
|
+
payload: zod.object({
|
|
370
|
+
filter: schemas.searchPayload.specific
|
|
371
|
+
}),
|
|
372
|
+
response: zod.object({
|
|
373
|
+
success: zod.boolean()
|
|
374
|
+
})
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
const methods = (schemas)=>{
|
|
379
|
+
const data = mapStoreSchemasToModuleMetadata(schemas, schemas.name);
|
|
380
|
+
return zodModuleMethodFactory(data);
|
|
381
|
+
};
|
|
382
|
+
const InRAM = (schemas, options)=>{
|
|
383
|
+
class RAMStore {
|
|
384
|
+
___data = [];
|
|
385
|
+
method = methods(schemas);
|
|
386
|
+
___listSearchLogic = (entity, pattern)=>{
|
|
387
|
+
const patternKeys = Object.entries(pattern).filter(([_, value])=>!!value).map((x)=>x[0]);
|
|
388
|
+
const entityAsObject = entity;
|
|
389
|
+
return Object.entries(entityAsObject).filter(([key])=>patternKeys.includes(key)).every(([key, value])=>value === pattern[key] || value?.toString()?.includes(pattern[key]?.toString()));
|
|
390
|
+
};
|
|
391
|
+
___specificSearchLogic = (entity, pattern)=>{
|
|
392
|
+
const patternKeys = Object.entries(pattern).filter(([_, value])=>!!value).map((x)=>x[0]);
|
|
393
|
+
const entityAsObject = entity;
|
|
394
|
+
return Object.entries(entityAsObject).filter(([key])=>patternKeys.includes(key)).every(([key, value])=>value === pattern[key] || value?.toString()?.includes(pattern[key]?.toString()));
|
|
395
|
+
};
|
|
396
|
+
___mapDetailToList = (entity)=>entity;
|
|
397
|
+
___mapCreatePayloadToDetail = (payload)=>({
|
|
398
|
+
...payload,
|
|
399
|
+
id: Math.random().toString()
|
|
400
|
+
});
|
|
401
|
+
___mapUpdatePayloadToDetail = (prevValue, update)=>({
|
|
402
|
+
...prevValue,
|
|
403
|
+
...update
|
|
404
|
+
});
|
|
405
|
+
constructor(){
|
|
406
|
+
if (!options) return;
|
|
407
|
+
if (options.searchLogic) {
|
|
408
|
+
if (options.searchLogic.list) this.___listSearchLogic = options.searchLogic.list;
|
|
409
|
+
if (options.searchLogic.specific) this.___specificSearchLogic = options.searchLogic.specific;
|
|
410
|
+
}
|
|
411
|
+
if (options.mappers) {
|
|
412
|
+
if (options.mappers.detailToList) this.___mapDetailToList = options.mappers.detailToList;
|
|
413
|
+
if (options.mappers.createPayloadToDetail) this.___mapCreatePayloadToDetail = options.mappers.createPayloadToDetail;
|
|
414
|
+
if (options.mappers.updatePayloadToDetail) this.___mapUpdatePayloadToDetail = options.mappers.updatePayloadToDetail;
|
|
415
|
+
}
|
|
416
|
+
if (options.initialData) this.___data = [
|
|
417
|
+
...options.initialData
|
|
418
|
+
];
|
|
419
|
+
}
|
|
420
|
+
list = this.method('list', async ({ filter, pagination = {
|
|
421
|
+
pageSize: 1000,
|
|
422
|
+
zeroBasedIndex: 0
|
|
423
|
+
} })=>{
|
|
424
|
+
schemas.searchPayload.list.parse(filter);
|
|
425
|
+
Pagination.schema.parse(pagination);
|
|
426
|
+
const afterFiltration = this.___data.filter((x)=>this.___listSearchLogic(x, filter));
|
|
427
|
+
const afterPagination = afterFiltration.filter((_, i)=>i >= pagination.pageSize * pagination.zeroBasedIndex && i < (pagination.zeroBasedIndex + 1) * pagination.pageSize);
|
|
428
|
+
const result = afterPagination.map((x)=>this.___mapDetailToList(x));
|
|
429
|
+
zod.array(schemas.models.list).parse(result);
|
|
430
|
+
return result;
|
|
431
|
+
});
|
|
432
|
+
details = this.method('details', async ({ filter })=>{
|
|
433
|
+
const details = this.___data.find((x)=>this.___specificSearchLogic(x, filter));
|
|
434
|
+
if (!details) return null;
|
|
435
|
+
return details;
|
|
436
|
+
});
|
|
437
|
+
create = this.method('create', async ({ payload })=>{
|
|
438
|
+
const newItem = this.___mapCreatePayloadToDetail(payload);
|
|
439
|
+
this.___data = this.___data.concat(newItem);
|
|
440
|
+
return {
|
|
441
|
+
id: newItem.id
|
|
442
|
+
};
|
|
443
|
+
});
|
|
444
|
+
updateOne = this.method('updateOne', async ({ filter, payload })=>{
|
|
445
|
+
const index = this.___data.findIndex((x)=>this.___specificSearchLogic(x, filter));
|
|
446
|
+
this.___data = this.___data.map((x, i)=>{
|
|
447
|
+
if (i !== index) return x;
|
|
448
|
+
return this.___mapUpdatePayloadToDetail(x, payload);
|
|
449
|
+
});
|
|
450
|
+
return {
|
|
451
|
+
success: index > -1
|
|
452
|
+
};
|
|
453
|
+
});
|
|
454
|
+
deleteOne = this.method('deleteOne', async ({ filter })=>{
|
|
455
|
+
const index = this.___data.findIndex((x)=>this.___specificSearchLogic(x, filter));
|
|
456
|
+
this.___data = this.___data.filter((_x, i)=>i !== index);
|
|
457
|
+
return {
|
|
458
|
+
success: index > -1
|
|
459
|
+
};
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
return RAMStore;
|
|
463
|
+
};
|
|
464
|
+
export { store_namespaceObject as Store, zodModuleMethodFactory, zod_controller_utils_namespaceObject as API };
|
package/dist/store/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { type
|
|
2
|
-
export
|
|
3
|
-
export
|
|
1
|
+
export { type Types } from './store.shared-models.utils';
|
|
2
|
+
export * from './store.zod.utils';
|
|
3
|
+
export * from './store.ram.utils';
|