@alevnyacow/nzmt 0.0.20 → 0.0.22

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/bin/cli.js CHANGED
@@ -7,6 +7,10 @@ var args = process.argv.slice(2);
7
7
  var [command, entityName] = args;
8
8
 
9
9
  function camelizeVariants(str) {
10
+ if (!str.includes('-')) {
11
+ return [str, str.substring(0, 1).toUpperCase() + str.substring(1)]
12
+ }
13
+
10
14
  const words = str.split("-");
11
15
 
12
16
  const lowerCamel = words
@@ -253,9 +257,49 @@ function generateStores(lowerCase, upperCase) {
253
257
 
254
258
  }
255
259
 
260
+
256
261
  if (command === 'store') {
257
262
  var [lowerCase, upperCase] = camelizeVariants(entityName)
258
263
  generateStores(lowerCase, upperCase)
259
264
  process.exit(0);
260
265
  }
261
266
 
267
+ function generateEntity(upperCase) {
268
+ const folder = config?.paths?.entities ? path.resolve(process.cwd(), config?.paths?.entities, entityName) : path.resolve(process.cwd(), entityName);
269
+
270
+ fs.mkdirSync(folder, { recursive: true })
271
+
272
+ const body = [
273
+ "import z from 'zod'",
274
+ "import { Entities } from '@alevnyacow/nzmt'",
275
+ "",
276
+ `export type ${upperCase}Model = z.infer<typeof ${upperCase}.schema>`,
277
+ "",
278
+ `export class ${upperCase} {`,
279
+ "\tstatic schema = z.object({",
280
+ "\t\tid: Entities.Identifier.schema,",
281
+ "\t\t",
282
+ "\t})",
283
+ "\t",
284
+ `\tprivate constructor(private readonly data: ${upperCase}Model) {}`,
285
+ "\t",
286
+ `\tstatic create = (data: ${upperCase}Model) => {`,
287
+ `\t\tconst parsedModel = ${upperCase}.schema.parse(data)`,
288
+ `\t\treturn new ${upperCase}(parsedModel)`,
289
+ "\t}",
290
+ "\t",
291
+ `\tget model(): ${upperCase}Model {`,
292
+ "\t\treturn this.data",
293
+ "\t}",
294
+ "}"
295
+ ].join('\n')
296
+
297
+ fs.writeFileSync(path.resolve(folder, `${entityName}.entity.ts`), body)
298
+ fs.writeFileSync(path.resolve(folder, 'index.ts'), `export * from './${entityName}.entity'`)
299
+ }
300
+
301
+ if (command === 'entity') {
302
+ var [lowerCase, upperCase] = camelizeVariants(entityName)
303
+ generateEntity(upperCase)
304
+ process.exit(0)
305
+ }
@@ -0,0 +1,11 @@
1
+ import z from 'zod';
2
+ export type IdentifierModel = z.infer<typeof Identifier.schema>;
3
+ export declare class Identifier {
4
+ private readonly data;
5
+ static schema: z.ZodString;
6
+ private constructor();
7
+ static create: (data: IdentifierModel) => Identifier;
8
+ static get randomUUID(): Identifier;
9
+ get model(): IdentifierModel;
10
+ get isUUID(): boolean;
11
+ }
@@ -0,0 +1,2 @@
1
+ export * from './identifier.entity';
2
+ export * from './pagination.entity';
package/dist/index.cjs CHANGED
@@ -35,7 +35,13 @@ __webpack_require__.r(__webpack_exports__);
35
35
  __webpack_require__.d(__webpack_exports__, {
36
36
  Controller: ()=>zod_controller_utils_namespaceObject,
37
37
  Store: ()=>store_namespaceObject,
38
- zodModuleMethodFactory: ()=>zodModuleMethodFactory
38
+ Module: ()=>zod_module_utils_namespaceObject,
39
+ Entities: ()=>entities_namespaceObject
40
+ });
41
+ var zod_module_utils_namespaceObject = {};
42
+ __webpack_require__.r(zod_module_utils_namespaceObject);
43
+ __webpack_require__.d(zod_module_utils_namespaceObject, {
44
+ methods: ()=>methods
39
45
  });
40
46
  var zod_controller_utils_namespaceObject = {};
41
47
  __webpack_require__.r(zod_controller_utils_namespaceObject);
@@ -47,7 +53,13 @@ var store_namespaceObject = {};
47
53
  __webpack_require__.r(store_namespaceObject);
48
54
  __webpack_require__.d(store_namespaceObject, {
49
55
  InRAM: ()=>InRAM,
50
- methods: ()=>methods
56
+ methods: ()=>store_zod_utils_methods
57
+ });
58
+ var entities_namespaceObject = {};
59
+ __webpack_require__.r(entities_namespaceObject);
60
+ __webpack_require__.d(entities_namespaceObject, {
61
+ Identifier: ()=>Identifier,
62
+ Pagination: ()=>Pagination
51
63
  });
52
64
  class ErrorFactory {
53
65
  static isControllerError = (error)=>{
@@ -142,7 +154,7 @@ class ErrorFactory {
142
154
  });
143
155
  };
144
156
  }
145
- const zodModuleMethodFactory = (metadata, sharedConfig = {})=>(methodName, handler, config = {})=>{
157
+ const methods = (metadata, sharedConfig = {})=>(methodName, handler, config = {})=>{
146
158
  const { name, schemas } = metadata;
147
159
  return async (payload)=>{
148
160
  try {
@@ -414,14 +426,14 @@ const mapStoreSchemasToModuleMetadata = (schemas, name)=>({
414
426
  }
415
427
  }
416
428
  });
417
- const methods = (schemas)=>{
429
+ const store_zod_utils_methods = (schemas)=>{
418
430
  const data = mapStoreSchemasToModuleMetadata(schemas, schemas.name);
419
- return zodModuleMethodFactory(data);
431
+ return methods(data);
420
432
  };
421
433
  const InRAM = (schemas, options)=>{
422
434
  class RAMStore {
423
435
  ___data = [];
424
- method = methods(schemas);
436
+ method = store_zod_utils_methods(schemas);
425
437
  ___listSearchLogic = (entity, pattern)=>{
426
438
  const patternKeys = Object.entries(pattern).filter(([_, value])=>!!value).map((x)=>x[0]);
427
439
  const entityAsObject = entity;
@@ -500,13 +512,33 @@ const InRAM = (schemas, options)=>{
500
512
  }
501
513
  return RAMStore;
502
514
  };
515
+ const external_node_crypto_namespaceObject = require("node:crypto");
516
+ class Identifier {
517
+ data;
518
+ static schema = external_zod_default().string().nonempty();
519
+ constructor(data){
520
+ this.data = data;
521
+ }
522
+ static create = (data)=>new Identifier(Identifier.schema.parse(data));
523
+ static get randomUUID() {
524
+ return Identifier.create((0, external_node_crypto_namespaceObject.randomUUID)());
525
+ }
526
+ get model() {
527
+ return this.data;
528
+ }
529
+ get isUUID() {
530
+ return external_zod_default().uuidv4().safeParse(this.data).success;
531
+ }
532
+ }
503
533
  exports.Controller = __webpack_exports__.Controller;
534
+ exports.Entities = __webpack_exports__.Entities;
535
+ exports.Module = __webpack_exports__.Module;
504
536
  exports.Store = __webpack_exports__.Store;
505
- exports.zodModuleMethodFactory = __webpack_exports__.zodModuleMethodFactory;
506
537
  for(var __rspack_i in __webpack_exports__)if (-1 === [
507
538
  "Controller",
508
- "Store",
509
- "zodModuleMethodFactory"
539
+ "Entities",
540
+ "Module",
541
+ "Store"
510
542
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
511
543
  Object.defineProperty(exports, '__esModule', {
512
544
  value: true
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
- export { zodModuleMethodFactory } from './zod-module.utils';
1
+ export * as Module from './zod-module.utils';
2
2
  export * as Controller from './zod-controller.utils';
3
3
  export * as Store from './store';
4
+ export * as Entities from './entities';
package/dist/index.js CHANGED
@@ -1,11 +1,18 @@
1
1
  import { __webpack_require__ } from "./rslib-runtime.js";
2
2
  import { NextResponse } from "next/server";
3
3
  import zod from "zod";
4
+ import { randomUUID } from "node:crypto";
5
+ var entities_namespaceObject = {};
6
+ __webpack_require__.r(entities_namespaceObject);
7
+ __webpack_require__.d(entities_namespaceObject, {
8
+ Identifier: ()=>Identifier,
9
+ Pagination: ()=>Pagination
10
+ });
4
11
  var store_namespaceObject = {};
5
12
  __webpack_require__.r(store_namespaceObject);
6
13
  __webpack_require__.d(store_namespaceObject, {
7
14
  InRAM: ()=>InRAM,
8
- methods: ()=>methods
15
+ methods: ()=>store_zod_utils_methods
9
16
  });
10
17
  var zod_controller_utils_namespaceObject = {};
11
18
  __webpack_require__.r(zod_controller_utils_namespaceObject);
@@ -13,6 +20,11 @@ __webpack_require__.d(zod_controller_utils_namespaceObject, {
13
20
  DefaultErrorCodes: ()=>zod_controller_utils_DefaultErrorCodes,
14
21
  endpoints: ()=>endpoints
15
22
  });
23
+ var zod_module_utils_namespaceObject = {};
24
+ __webpack_require__.r(zod_module_utils_namespaceObject);
25
+ __webpack_require__.d(zod_module_utils_namespaceObject, {
26
+ methods: ()=>methods
27
+ });
16
28
  class ErrorFactory {
17
29
  static isControllerError = (error)=>{
18
30
  if (!error || 'object' != typeof error) return false;
@@ -106,7 +118,7 @@ class ErrorFactory {
106
118
  });
107
119
  };
108
120
  }
109
- const zodModuleMethodFactory = (metadata, sharedConfig = {})=>(methodName, handler, config = {})=>{
121
+ const methods = (metadata, sharedConfig = {})=>(methodName, handler, config = {})=>{
110
122
  const { name, schemas } = metadata;
111
123
  return async (payload)=>{
112
124
  try {
@@ -375,14 +387,14 @@ const mapStoreSchemasToModuleMetadata = (schemas, name)=>({
375
387
  }
376
388
  }
377
389
  });
378
- const methods = (schemas)=>{
390
+ const store_zod_utils_methods = (schemas)=>{
379
391
  const data = mapStoreSchemasToModuleMetadata(schemas, schemas.name);
380
- return zodModuleMethodFactory(data);
392
+ return methods(data);
381
393
  };
382
394
  const InRAM = (schemas, options)=>{
383
395
  class RAMStore {
384
396
  ___data = [];
385
- method = methods(schemas);
397
+ method = store_zod_utils_methods(schemas);
386
398
  ___listSearchLogic = (entity, pattern)=>{
387
399
  const patternKeys = Object.entries(pattern).filter(([_, value])=>!!value).map((x)=>x[0]);
388
400
  const entityAsObject = entity;
@@ -461,4 +473,21 @@ const InRAM = (schemas, options)=>{
461
473
  }
462
474
  return RAMStore;
463
475
  };
464
- export { store_namespaceObject as Store, zodModuleMethodFactory, zod_controller_utils_namespaceObject as Controller };
476
+ class Identifier {
477
+ data;
478
+ static schema = zod.string().nonempty();
479
+ constructor(data){
480
+ this.data = data;
481
+ }
482
+ static create = (data)=>new Identifier(Identifier.schema.parse(data));
483
+ static get randomUUID() {
484
+ return Identifier.create(randomUUID());
485
+ }
486
+ get model() {
487
+ return this.data;
488
+ }
489
+ get isUUID() {
490
+ return zod.uuidv4().safeParse(this.data).success;
491
+ }
492
+ }
493
+ export { entities_namespaceObject as Entities, store_namespaceObject as Store, zod_controller_utils_namespaceObject as Controller, zod_module_utils_namespaceObject as Module };
@@ -1,5 +1,5 @@
1
1
  import z from 'zod';
2
- import { Pagination } from './store.pagination.entity';
2
+ import { Pagination } from '../entities/pagination.entity';
3
3
  import type { CRUD, Types } from './store.shared-models.utils';
4
4
  import { type Contract, type Metadata } from './store.zod.utils';
5
5
  export declare const InRAM: <T extends Metadata>(schemas: T, options?: {
@@ -364,7 +364,7 @@ export declare const InRAM: <T extends Metadata>(schemas: T, options?: {
364
364
  success: z.ZodBoolean;
365
365
  }, z.core.$strip>;
366
366
  };
367
- })[Method]["response"]>>, config?: import("../zod-module.utils").ZodModuleConfig) => (payload: z.core.output<(Record<Exclude<keyof (T["customOperations"] extends Record<string, {
367
+ })[Method]["response"]>>, config?: import("../zod-module.utils").Config) => (payload: z.core.output<(Record<Exclude<keyof (T["customOperations"] extends Record<string, {
368
368
  payload: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
369
369
  response: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
370
370
  }> ? { [operation in keyof T["customOperations"]]: {
@@ -741,7 +741,7 @@ export declare const InRAM: <T extends Metadata>(schemas: T, options?: {
741
741
  update: AUpdate;
742
742
  };
743
743
  } : never : never : never)["SearchPayload"]["list"];
744
- pagination?: import("./store.pagination.entity").PaginationModel;
744
+ pagination?: import("../entities").PaginationModel;
745
745
  }) => Promise<(Contract<T> extends infer T_2 ? T_2 extends Contract<T> ? T_2 extends {
746
746
  list: (data: {
747
747
  filter: infer SList;
@@ -1,4 +1,4 @@
1
- import type { PaginationModel } from './store.pagination.entity';
1
+ import type { PaginationModel } from '../entities/pagination.entity';
2
2
  export type CRUD<Models extends {
3
3
  list: unknown;
4
4
  detail: unknown;
@@ -1,6 +1,6 @@
1
1
  import type { ZodArray, ZodBoolean, ZodObject, ZodString, ZodType, ZodUnion } from 'zod';
2
2
  import z from 'zod';
3
- import { Pagination } from './store.pagination.entity';
3
+ import { Pagination } from '../entities/pagination.entity';
4
4
  import type { CRUD } from './store.shared-models.utils';
5
5
  type ZodSchema = ZodType;
6
6
  type Models<List, Detail> = {
@@ -261,7 +261,7 @@ export declare const methods: <T extends Metadata>(schemas: T) => <Method extend
261
261
  success: ZodBoolean;
262
262
  }, z.core.$strip>;
263
263
  };
264
- })[Method]["response"]>>, config?: import("../zod-module.utils").ZodModuleConfig) => (payload: z.core.output<(Record<Exclude<keyof (T["customOperations"] extends Record<string, {
264
+ })[Method]["response"]>>, config?: import("../zod-module.utils").Config) => (payload: z.core.output<(Record<Exclude<keyof (T["customOperations"] extends Record<string, {
265
265
  payload: ZodSchema;
266
266
  response: ZodSchema;
267
267
  }> ? { [operation in keyof T["customOperations"]]: {
@@ -12,8 +12,8 @@ type ZodAPISchemas = {
12
12
  response?: ZodObject;
13
13
  };
14
14
  type ErrorResponse = {
15
- message: string;
16
- code?: string;
15
+ code: string;
16
+ message?: string;
17
17
  details?: any;
18
18
  };
19
19
  type EndpointErrorGenerator = (payload: string | ErrorBaseCreatingPayload, errorStatus?: number, cause?: unknown) => ControllerErrorModel;
@@ -17,13 +17,13 @@ export type DTOs<T extends Metadata> = {
17
17
  export type Methods<T extends Metadata> = {
18
18
  [K in keyof T['schemas']]: (payload: z.infer<T['schemas'][K]['payload']>) => Promise<z.infer<T['schemas'][K]['response']>>;
19
19
  };
20
- export type ZodModuleConfig = {
20
+ export type Config = {
21
21
  onError?: (e: ModuleErrorModel) => Promise<void>;
22
22
  };
23
- export declare const zodModuleMethodFactory: <T extends Schemas>(metadata: {
23
+ export declare const methods: <T extends Schemas>(metadata: {
24
24
  schemas: T;
25
25
  name: string;
26
- }, sharedConfig?: ZodModuleConfig) => <Method extends keyof T>(methodName: Method, handler: (payload: z.infer<T[Method]["payload"]>, config: {
26
+ }, sharedConfig?: Config) => <Method extends keyof T>(methodName: Method, handler: (payload: z.infer<T[Method]["payload"]>, config: {
27
27
  methodError: (payload: string | ErrorBaseCreatingPayload, cause?: unknown) => ModuleErrorModel;
28
- }) => Promise<z.infer<T[Method]["response"]>>, config?: ZodModuleConfig) => (payload: z.infer<T[Method]["payload"]>) => Promise<z.infer<T[Method]["response"]>>;
28
+ }) => Promise<z.infer<T[Method]["response"]>>, config?: Config) => (payload: z.infer<T[Method]["payload"]>) => Promise<z.infer<T[Method]["response"]>>;
29
29
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alevnyacow/nzmt",
3
- "version": "0.0.20",
3
+ "version": "0.0.22",
4
4
  "description": "Next Zod Modules Toolkit",
5
5
  "type": "module",
6
6
  "exports": {