@alevnyacow/nzmt 0.10.2 → 0.12.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.
Files changed (2) hide show
  1. package/bin/cli.js +79 -6
  2. package/package.json +1 -1
package/bin/cli.js CHANGED
@@ -388,7 +388,7 @@ function generateStores(lowerCase, upperCase, withEntityPreset) {
388
388
 
389
389
  fs.mkdirSync(folder, { recursive: true })
390
390
 
391
- const withEntity = withEntityPreset || !(options ?? []).includes('dont-import-entity')
391
+ const withEntity = withEntityPreset || (options ?? []).includes('import-entity')
392
392
 
393
393
  // Contract
394
394
 
@@ -743,10 +743,10 @@ function toKebabFromPascal(str) {
743
743
  .toLowerCase()
744
744
  }
745
745
 
746
- function generateService(lowerCase, upperCase) {
746
+ function generateService(lowerCase, upperCase, store) {
747
747
  const folder = config?.paths?.services ? path.resolve(process.cwd(), `${config.coreFolder}${config?.paths?.services}`, entityName) : path.resolve(process.cwd(), entityName);
748
748
 
749
- const proxiedStore = options.find(x => x.startsWith('p:'))?.split(':')?.at(-1)
749
+ const proxiedStore = store || options.find(x => x.startsWith('p:'))?.split(':')?.at(-1)
750
750
  if (proxiedStore && !proxiedStore.endsWith('Store')) {
751
751
  throw 'Only stores can be proxied in services!'
752
752
  }
@@ -849,10 +849,19 @@ if (command.toLowerCase() === 'service' || command === 'S') {
849
849
  process.exit(0)
850
850
  }
851
851
 
852
- function generateController(upperCase, lowerCase) {
852
+ function generateController(upperCase, lowerCase, crudService) {
853
+ if (crudService && !crudService.endsWith('Service')) {
854
+ throw 'Incorrect crudService'
855
+ }
856
+
853
857
  const folder = config?.paths?.controllers ? path.resolve(process.cwd(), `${config.coreFolder}${config?.paths?.controllers}`, entityName) : path.resolve(process.cwd(), entityName);
854
858
 
855
- const injections = options.filter(x => x.startsWith('i:')).flatMap(x => x.split(':')[1]).join(',').split(',').filter(x => !!x.length)
859
+ let injections = options.filter(x => x.startsWith('i:')).flatMap(x => x.split(':')[1]).join(',').split(',').filter(x => !!x.length)
860
+ if (crudService && !injections.includes(crudService)) {
861
+ injections = injections.concat(crudService)
862
+ }
863
+
864
+ const crudServiceLowercase = crudService ? crudService.substring(0, 1).toUpperCase() + crudService.substring(1) : undefined
856
865
 
857
866
  const importInjections = injections.map((i) => {
858
867
  if (i.endsWith('Controller')) {
@@ -876,10 +885,37 @@ function generateController(upperCase, lowerCase) {
876
885
 
877
886
  fs.writeFileSync(path.resolve(folder, `${entityName}.controller.metadata.ts`), [
878
887
  `import { Controller } from '@alevnyacow/nzmt'`,
888
+ crudService ? `import { ${crudServiceLowercase}Metadata } from '@${config.paths.services}/${toKebabFromPascal(crudService).slice(0, -'-service'.length)}'` : undefined
879
889
  ``,
880
890
  `export const ${lowerCase}ControllerMetadata = {`,
881
891
  `\tname: '${upperCase}Controller',`,
882
- `\tschemas: {}`,
892
+ crudService ? [
893
+ `\tschemas: {`,
894
+ `\t\tGET: {`,
895
+ `\t\t\tquery: z.union([`,
896
+ `\t\t\t\tValueObjects.Pagination.schema.extend(${crudServiceLowercase}Metadata.schemas.getList.payload.shape.filter.shape),`
897
+ `\t\t\t\t${crudServiceLowercase}Metadata.schemas.getList.payload.shape.filter`,
898
+ `\t\t\t])`,
899
+ `\t\t\tresponse: ${crudServiceLowercase}Metadata.schemas.getList.response`,
900
+ `\t\t},`,
901
+ `\t\tdetails_GET: {`,
902
+ `\t\t\tquery: ${crudServiceLowercase}Metadata.schemas.getDetails.payload,`,
903
+ `\t\t\tresponse: ${crudServiceLowercase}Metadata.schemas.getDetails.response`,
904
+ `\t\t},`,
905
+ `\t\tPOST: {`,
906
+ `\t\t\tbody: ${crudServiceLowercase}Metadata.schemas.create.payload,`,
907
+ `\t\t\tresponse: ${crudServiceLowercase}Metadata.schemas.create.response`,
908
+ `\t\t},`,
909
+ `\t\tPATCH: {`,
910
+ `\t\t\tbody: ${crudServiceLowercase}Metadata.schemas.update.payload,`,
911
+ `\t\t\tresponse: ${crudServiceLowercase}Metadata.schemas.update.response`,
912
+ `\t\t},`,
913
+ `\t\tDELETE: {`,
914
+ `\t\t\tquery: ${crudServiceLowercase}Metadata.schemas.delete.payload,`,
915
+ `\t\t\tresponse: ${crudServiceLowercase}Metadata.schemas.delete.response`,
916
+ `\t\t},`,
917
+ `\t}`,
918
+ ].join('\n') : `\tschemas: {}`,
883
919
  `} satisfies Controller.Metadata`,
884
920
  ``,
885
921
  `export type ${upperCase}API = Controller.Contract<typeof ${lowerCase}ControllerMetadata>`
@@ -902,6 +938,19 @@ function generateController(upperCase, lowerCase) {
902
938
  ``,
903
939
  `\tprivate readonly endpoints = Controller.endpoints(${lowerCase}ControllerMetadata)`,
904
940
  ``,
941
+ crudService ? [
942
+ `\tGET = this.endpoints('GET', async (x) => {`,
943
+ `\t\tif ('pageSize' in x && 'zeroBasedIndex' in x) {`,
944
+ `\t\t\tconst { pageSize, zeroBasedIndex, ...filter } = x`,
945
+ `\t\t\treturn await this.${crudServiceLowercase}.getList({ filter, pagination: { pageSize, zeroBasedIndex } })`,
946
+ `\t\t}`,
947
+ `\t\treturn await this.${crudServiceLowercase}.getList({ filter: x })`,
948
+ ``,
949
+ `\tdetails_GET = this.endpoints('details_GET', this.${crudServiceLowercase}.getDetails)`,
950
+ `\tPOST = this.endpoints('POST', this.${crudServiceLowercase}.create)`,
951
+ `\tPATCH = this.endpoints('PATCH', this.${crudServiceLowercase}.update)`,
952
+ `\tDELETE = this.endpoints('DELETE', this.${crudServiceLowercase}.delete)`,
953
+ ].join('\n') : undefined
905
954
  `}`
906
955
  ].filter(x => typeof x === 'string').join('\n'))
907
956
 
@@ -933,4 +982,28 @@ if (command.toLowerCase() === 'controller' || command === 'c') {
933
982
  var [lowerCase, upperCase] = camelizeVariants(entityName)
934
983
  generateController(upperCase, lowerCase)
935
984
  process.exit(0)
985
+ }
986
+
987
+ if (command.toLowerCase() === 'stored-entity' || command === 'se') {
988
+ var [lowerCase, upperCase] = camelizeVariants(entityName)
989
+ generateEntity(upperCase)
990
+ generateStores(lowerCase, upperCase, true)
991
+ process.exit(0)
992
+ }
993
+
994
+ if (command.toLowerCase() === 'crud-service') {
995
+ var [lowerCase, upperCase] = camelizeVariants(entityName)
996
+ generateEntity(upperCase)
997
+ generateStores(lowerCase, upperCase, true)
998
+ generateService(lowerCase, upperCase, upperCase + 'Store')
999
+ process.exit(0)
1000
+ }
1001
+
1002
+ if (command.toLowerCase() === 'crud-api') {
1003
+ var [lowerCase, upperCase] = camelizeVariants(entityName)
1004
+ generateEntity(upperCase)
1005
+ generateStores(lowerCase, upperCase, true)
1006
+ generateService(lowerCase, upperCase, upperCase + 'Store')
1007
+ generateController(upperCase, lowerCase, upperCase + 'Service')
1008
+ process.exit(0)
936
1009
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alevnyacow/nzmt",
3
- "version": "0.10.2",
3
+ "version": "0.12.0",
4
4
  "description": "Next Zod Modules Toolkit",
5
5
  "repository": {
6
6
  "type": "git",