@alevnyacow/nzmt 0.0.6 → 0.0.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/dist/index.js CHANGED
@@ -1,44 +1,19 @@
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
+ import { v4 } from "uuid";
5
+ var store_namespaceObject = {};
6
+ __webpack_require__.r(store_namespaceObject);
7
+ __webpack_require__.d(store_namespaceObject, {
8
+ InRAM: ()=>InRAM,
9
+ methods: ()=>methods
10
+ });
5
11
  var zod_controller_utils_namespaceObject = {};
6
12
  __webpack_require__.r(zod_controller_utils_namespaceObject);
7
13
  __webpack_require__.d(zod_controller_utils_namespaceObject, {
8
14
  DefaultErrorCodes: ()=>zod_controller_utils_DefaultErrorCodes,
9
15
  endpoints: ()=>endpoints
10
16
  });
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
17
  class ErrorFactory {
43
18
  static isControllerError = (error)=>{
44
19
  if (!error || 'object' != typeof error) return false;
@@ -161,136 +136,6 @@ const zodModuleMethodFactory = (metadata, sharedConfig = {})=>(methodName, handl
161
136
  }
162
137
  };
163
138
  };
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
139
  var zod_controller_utils_DefaultErrorCodes = /*#__PURE__*/ function(DefaultErrorCodes) {
295
140
  DefaultErrorCodes["REQUEST_PARSING"] = "ZOD-CONTROLLER___REQUEST-PARSING";
296
141
  DefaultErrorCodes["RESPONSE_PARSING"] = "ZOD-CONTROLLER___RESPONSE-PARSING";
@@ -456,4 +301,165 @@ const endpoints = (metadata, sharedConfig = {})=>{
456
301
  };
457
302
  return endpointLogic;
458
303
  };
459
- export { store_ram_utils_RAMStore as RAMStore, zodModuleMethodFactory, zodStoreMethodFactory, zod_controller_utils_namespaceObject as Controller };
304
+ class Pagination {
305
+ data;
306
+ static schema = zod.object({
307
+ zeroBasedIndex: zod.coerce.number().int().nonnegative(),
308
+ pageSize: zod.coerce.number().int().positive()
309
+ });
310
+ constructor(data){
311
+ this.data = data;
312
+ }
313
+ static create = (data)=>{
314
+ const parsedModel = Pagination.schema.parse(data);
315
+ return new Pagination(parsedModel);
316
+ };
317
+ get model() {
318
+ return this.data;
319
+ }
320
+ same = (pagination)=>pagination.model.pageSize === this.model.pageSize && pagination.model.zeroBasedIndex === this.model.zeroBasedIndex;
321
+ get nextPage() {
322
+ return Pagination.create({
323
+ pageSize: this.data.pageSize,
324
+ zeroBasedIndex: this.data.zeroBasedIndex + 1
325
+ });
326
+ }
327
+ get previousPage() {
328
+ if (0 === this.model.zeroBasedIndex) return Pagination.create(this.model);
329
+ return Pagination.create({
330
+ pageSize: this.model.pageSize,
331
+ zeroBasedIndex: this.model.zeroBasedIndex - 1
332
+ });
333
+ }
334
+ }
335
+ const mapStoreSchemasToModuleMetadata = (schemas, name)=>({
336
+ name,
337
+ schemas: {
338
+ ...schemas.customOperations ?? {},
339
+ list: {
340
+ payload: zod.object({
341
+ filter: schemas.searchPayload.list,
342
+ pagination: Pagination.schema.optional()
343
+ }),
344
+ response: zod.array(schemas.models.list)
345
+ },
346
+ details: {
347
+ payload: zod.object({
348
+ filter: schemas.searchPayload.specific
349
+ }),
350
+ response: schemas.models.details.nullable()
351
+ },
352
+ create: {
353
+ payload: zod.object({
354
+ payload: schemas.actionsPayload.create
355
+ }),
356
+ response: zod.object({
357
+ id: zod.string()
358
+ })
359
+ },
360
+ updateOne: {
361
+ payload: zod.object({
362
+ filter: schemas.searchPayload.specific,
363
+ payload: schemas.actionsPayload.update
364
+ }),
365
+ response: zod.object({
366
+ success: zod.boolean()
367
+ })
368
+ },
369
+ deleteOne: {
370
+ payload: zod.object({
371
+ filter: schemas.searchPayload.specific
372
+ }),
373
+ response: zod.object({
374
+ success: zod.boolean()
375
+ })
376
+ }
377
+ }
378
+ });
379
+ const methods = (schemas)=>{
380
+ const data = mapStoreSchemasToModuleMetadata(schemas, schemas.name);
381
+ return zodModuleMethodFactory(data);
382
+ };
383
+ const InRAM = (schemas, options)=>{
384
+ class RAMStore {
385
+ ___data = [];
386
+ method = methods(schemas);
387
+ ___listSearchLogic = (entity, pattern)=>{
388
+ const patternKeys = Object.entries(pattern).filter(([_, value])=>!!value).map((x)=>x[0]);
389
+ const entityAsObject = entity;
390
+ return Object.entries(entityAsObject).filter(([key])=>patternKeys.includes(key)).every(([key, value])=>value === pattern[key] || value?.toString()?.includes(pattern[key]?.toString()));
391
+ };
392
+ ___specificSearchLogic = (entity, pattern)=>{
393
+ const patternKeys = Object.entries(pattern).filter(([_, value])=>!!value).map((x)=>x[0]);
394
+ const entityAsObject = entity;
395
+ return Object.entries(entityAsObject).filter(([key])=>patternKeys.includes(key)).every(([key, value])=>value === pattern[key] || value?.toString()?.includes(pattern[key]?.toString()));
396
+ };
397
+ ___mapDetailToList = (entity)=>entity;
398
+ ___mapCreatePayloadToDetail = (payload)=>({
399
+ ...payload,
400
+ id: v4()
401
+ });
402
+ ___mapUpdatePayloadToDetail = (prevValue, update)=>({
403
+ ...prevValue,
404
+ ...update
405
+ });
406
+ constructor(){
407
+ if (!options) return;
408
+ if (options.searchLogic) {
409
+ if (options.searchLogic.list) this.___listSearchLogic = options.searchLogic.list;
410
+ if (options.searchLogic.specific) this.___specificSearchLogic = options.searchLogic.specific;
411
+ }
412
+ if (options.mappers) {
413
+ if (options.mappers.detailToList) this.___mapDetailToList = options.mappers.detailToList;
414
+ if (options.mappers.createPayloadToDetail) this.___mapCreatePayloadToDetail = options.mappers.createPayloadToDetail;
415
+ if (options.mappers.updatePayloadToDetail) this.___mapUpdatePayloadToDetail = options.mappers.updatePayloadToDetail;
416
+ }
417
+ if (options.initialData) this.___data = [
418
+ ...options.initialData
419
+ ];
420
+ }
421
+ list = this.method('list', async ({ filter, pagination = {
422
+ pageSize: 1000,
423
+ zeroBasedIndex: 0
424
+ } })=>{
425
+ schemas.searchPayload.list.parse(filter);
426
+ Pagination.schema.parse(pagination);
427
+ const afterFiltration = this.___data.filter((x)=>this.___listSearchLogic(x, filter));
428
+ const afterPagination = afterFiltration.filter((_, i)=>i >= pagination.pageSize * pagination.zeroBasedIndex && i < (pagination.zeroBasedIndex + 1) * pagination.pageSize);
429
+ const result = afterPagination.map((x)=>this.___mapDetailToList(x));
430
+ zod.array(schemas.models.list).parse(result);
431
+ return result;
432
+ });
433
+ details = this.method('details', async ({ filter })=>{
434
+ const details = this.___data.find((x)=>this.___specificSearchLogic(x, filter));
435
+ if (!details) return null;
436
+ return details;
437
+ });
438
+ create = this.method('create', async ({ payload })=>{
439
+ const newItem = this.___mapCreatePayloadToDetail(payload);
440
+ this.___data = this.___data.concat(newItem);
441
+ return {
442
+ id: newItem.id
443
+ };
444
+ });
445
+ updateOne = this.method('updateOne', async ({ filter, payload })=>{
446
+ const index = this.___data.findIndex((x)=>this.___specificSearchLogic(x, filter));
447
+ this.___data = this.___data.map((x, i)=>{
448
+ if (i !== index) return x;
449
+ return this.___mapUpdatePayloadToDetail(x, payload);
450
+ });
451
+ return {
452
+ success: index > -1
453
+ };
454
+ });
455
+ deleteOne = this.method('deleteOne', async ({ filter })=>{
456
+ const index = this.___data.findIndex((x)=>this.___specificSearchLogic(x, filter));
457
+ this.___data = this.___data.filter((_x, i)=>i !== index);
458
+ return {
459
+ success: index > -1
460
+ };
461
+ });
462
+ }
463
+ return RAMStore;
464
+ };
465
+ export { store_namespaceObject as Store, zodModuleMethodFactory, zod_controller_utils_namespaceObject as API };
@@ -1,3 +1,3 @@
1
- export { type StoreTypes } from './store.shared-models.utils';
2
- export { zodStoreMethodFactory, type ZodStore, type ZodStoreMetadata } from './store.zod.utils';
3
- export { RAMStore } from './store.ram.utils';
1
+ export { type Types } from './store.shared-models.utils';
2
+ export * from './store.zod.utils';
3
+ export * from './store.ram.utils';