@voxgig/apidef 3.0.2 → 3.1.1

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 (74) hide show
  1. package/dist/apidef.d.ts +5 -4
  2. package/dist/apidef.js +12 -21
  3. package/dist/apidef.js.map +1 -1
  4. package/dist/builder/entity/info.js +1 -1
  5. package/dist/builder/entity/info.js.map +1 -1
  6. package/dist/builder/flow.js +29 -27
  7. package/dist/builder/flow.js.map +1 -1
  8. package/dist/desc.d.ts +11 -13
  9. package/dist/guide/guide.js +1 -1
  10. package/dist/guide/guide.js.map +1 -1
  11. package/dist/guide/heuristic01.js +4 -2
  12. package/dist/guide/heuristic01.js.map +1 -1
  13. package/dist/model.d.ts +42 -7
  14. package/dist/parse.d.ts +3 -1
  15. package/dist/parse.js +35 -2
  16. package/dist/parse.js.map +1 -1
  17. package/dist/resolver.js +2 -1
  18. package/dist/resolver.js.map +1 -1
  19. package/dist/transform/args.d.ts +2 -2
  20. package/dist/transform/args.js +9 -13
  21. package/dist/transform/args.js.map +1 -1
  22. package/dist/transform/clean.d.ts +2 -2
  23. package/dist/transform/clean.js +2 -1
  24. package/dist/transform/clean.js.map +1 -1
  25. package/dist/transform/entity.d.ts +3 -3
  26. package/dist/transform/entity.js.map +1 -1
  27. package/dist/transform/field.d.ts +2 -2
  28. package/dist/transform/field.js +2 -3
  29. package/dist/transform/field.js.map +1 -1
  30. package/dist/transform/flow.d.ts +3 -0
  31. package/dist/transform/flow.js +26 -0
  32. package/dist/transform/flow.js.map +1 -0
  33. package/dist/transform/flowstep.d.ts +3 -0
  34. package/dist/transform/flowstep.js +145 -0
  35. package/dist/transform/flowstep.js.map +1 -0
  36. package/dist/transform/operation.d.ts +2 -2
  37. package/dist/transform/operation.js +15 -8
  38. package/dist/transform/operation.js.map +1 -1
  39. package/dist/transform/select.d.ts +2 -2
  40. package/dist/transform/select.js +28 -7
  41. package/dist/transform/select.js.map +1 -1
  42. package/dist/transform/top.d.ts +0 -9
  43. package/dist/transform/top.js +13 -0
  44. package/dist/transform/top.js.map +1 -1
  45. package/dist/transform.d.ts +1 -1
  46. package/dist/tsconfig.tsbuildinfo +1 -1
  47. package/dist/types.d.ts +9 -1
  48. package/dist/types.js.map +1 -1
  49. package/dist/utility.d.ts +5 -1
  50. package/dist/utility.js +64 -1
  51. package/dist/utility.js.map +1 -1
  52. package/model/apidef.jsonic +8 -7
  53. package/package.json +7 -7
  54. package/src/apidef.ts +15 -22
  55. package/src/builder/entity/info.ts +1 -1
  56. package/src/builder/flow.ts +16 -9
  57. package/src/desc.ts +28 -17
  58. package/src/guide/guide.ts +2 -1
  59. package/src/guide/heuristic01.ts +5 -3
  60. package/src/model.ts +51 -6
  61. package/src/parse.ts +50 -3
  62. package/src/resolver.ts +3 -1
  63. package/src/transform/args.ts +11 -15
  64. package/src/transform/clean.ts +4 -5
  65. package/src/transform/entity.ts +4 -7
  66. package/src/transform/field.ts +7 -25
  67. package/src/transform/flow.ts +59 -0
  68. package/src/transform/flowstep.ts +242 -0
  69. package/src/transform/operation.ts +27 -31
  70. package/src/transform/select.ts +44 -15
  71. package/src/transform/top.ts +11 -42
  72. package/src/transform.ts +4 -4
  73. package/src/types.ts +2 -6
  74. package/src/utility.ts +87 -1
@@ -1,11 +1,9 @@
1
1
 
2
2
 
3
- import { each, snakify } from 'jostraca'
3
+ import { each } from 'jostraca'
4
4
 
5
5
  import type { TransformResult, Transform } from '../transform'
6
6
 
7
- import { formatJSONIC, depluralize, validator } from '../utility'
8
-
9
7
 
10
8
  import { KIT } from '../types'
11
9
 
@@ -16,8 +14,6 @@ import type {
16
14
 
17
15
  import type {
18
16
  PathDef,
19
- ParameterDef,
20
- MethodDef,
21
17
  } from '../def'
22
18
 
23
19
  import type {
@@ -30,7 +26,7 @@ import type {
30
26
 
31
27
 
32
28
 
33
- const selectTransform = async function(
29
+ const selectTransform: Transform = async function(
34
30
  ctx: any,
35
31
  ): Promise<TransformResult> {
36
32
  const { apimodel, def, guide } = ctx
@@ -38,12 +34,15 @@ const selectTransform = async function(
38
34
 
39
35
  let msg = 'select '
40
36
 
41
- each(kit.entity, (ment: ModelEntity, entname: string) => {
42
- each(ment.op, (mop: ModelOp, opname: OpName) => {
37
+ each(kit.entity, (ment: ModelEntity, _entname: string) => {
38
+ each(ment.op, (mop: ModelOp, _opname: OpName) => {
43
39
  each(mop.alts, (malt: ModelAlt) => {
44
40
  const pdef: PathDef = def.paths[malt.orig]
45
41
  resolveSelect(guide, ment, mop, malt, pdef)
46
42
  })
43
+ if (null != mop.alts && 0 < mop.alts.length) {
44
+ sortAlts(guide, ment, mop)
45
+ }
47
46
  })
48
47
 
49
48
  msg += ment.name + ' '
@@ -53,7 +52,13 @@ const selectTransform = async function(
53
52
  }
54
53
 
55
54
 
56
- function resolveSelect(guide: Guide, ment: ModelEntity, mop: ModelOp, malt: ModelAlt, pdef: PathDef) {
55
+ function resolveSelect(
56
+ guide: Guide,
57
+ ment: ModelEntity,
58
+ _mop: ModelOp,
59
+ malt: ModelAlt,
60
+ _pdef: PathDef
61
+ ) {
57
62
  const select: any = malt.select
58
63
  const margs: any = malt.args
59
64
 
@@ -61,19 +66,17 @@ function resolveSelect(guide: Guide, ment: ModelEntity, mop: ModelOp, malt: Mode
61
66
 
62
67
  argkinds.map((kind: string) => {
63
68
  each(margs[kind], (marg: ModelArg) => {
64
- select[kind] = (select[kind] ?? {})
65
-
66
- if (marg.req) {
67
- select[kind][marg.name] = true
69
+ if (!select.exist.includes(marg.name)) {
70
+ select.exist.push(marg.name)
68
71
  }
69
72
  })
70
73
  })
71
74
 
75
+ select.exist.sort()
76
+
72
77
  const gent = guide.entity[ment.name]
73
78
  const gpath = gent.path[malt.orig]
74
79
 
75
- // console.log('GPATH', gpath)
76
-
77
80
  if (gpath.action) {
78
81
  const actname = Object.keys(gpath.action)[0]
79
82
 
@@ -85,6 +88,32 @@ function resolveSelect(guide: Guide, ment: ModelEntity, mop: ModelOp, malt: Mode
85
88
  }
86
89
 
87
90
 
91
+ function sortAlts(
92
+ _guide: Guide,
93
+ _ment: ModelEntity,
94
+ mop: ModelOp,
95
+ ) {
96
+ mop.alts.sort((a: ModelAlt, b: ModelAlt) => {
97
+ // longest exist len first
98
+ let order = b.select.exist.length - a.select.exist.length
99
+ if (0 === order) {
100
+ if (null != a.select.$action && null != b.select.$action) {
101
+ order = a.select.$action < b.select.$action ? -1 :
102
+ a.select.$action > b.select.$action ? 1 : 0
103
+ }
104
+
105
+ if (0 === order) {
106
+ const a_exist_str = a.select.exist.join('\t')
107
+ const b_exist_str = b.select.exist.join('\t')
108
+ order = a_exist_str < b_exist_str ? -1 :
109
+ a_exist_str > b_exist_str ? 1 : 0
110
+ }
111
+ }
112
+
113
+ return order
114
+ })
115
+ }
116
+
88
117
  export {
89
118
  selectTransform,
90
119
  }
@@ -1,6 +1,4 @@
1
1
 
2
- import { each, getx } from 'jostraca'
3
-
4
2
  import { joinurl } from '@voxgig/struct'
5
3
 
6
4
  import { KIT } from '../types'
@@ -9,37 +7,16 @@ import type { TransformResult } from '../transform'
9
7
 
10
8
  import type {
11
9
  KitModel,
12
- TypeName,
13
- MethodName,
14
10
  } from '../types'
15
11
 
16
12
  import type {
17
- PathDef,
18
- MethodDef,
19
- ServerDef,
20
- ServerVariableDef,
21
- ParameterDef,
22
- SchemaDef,
23
- } from '../def'
24
-
25
- import type {
26
- GuidePathRename,
27
- GuideOp,
28
- GuidePath,
13
+ // GuidePath,
29
14
  PathDesc,
30
15
  OpDesc,
31
16
  } from '../desc'
32
17
 
33
18
  import type {
34
19
  OpName,
35
- ModelEntityRelations,
36
- ModelOpMap,
37
- ModelFieldOp,
38
- ModelField,
39
- ModelArg,
40
- ModelAlt,
41
- ModelOp,
42
- ModelEntity,
43
20
  } from '../model'
44
21
 
45
22
 
@@ -49,22 +26,13 @@ import type {
49
26
  // Model* => Generated SDK Model
50
27
 
51
28
 
52
- type GuideEntity = {
53
- name: string,
54
- path: Record<string, GuidePath>
55
-
56
- paths$: PathDesc[]
57
- opm$: Record<OpName, OpDesc>
58
- }
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
29
+ // type GuideEntity = {
30
+ // name: string,
31
+ // path: Record<string, GuidePath>
67
32
 
33
+ // paths$: PathDesc[]
34
+ // opm$: Record<OpName, OpDesc>
35
+ // }
68
36
 
69
37
 
70
38
  const topTransform = async function(
@@ -91,6 +59,7 @@ export {
91
59
  topTransform
92
60
  }
93
61
 
94
- export type {
95
- GuideEntity,
96
- }
62
+
63
+ // export type {
64
+ // GuideEntity,
65
+ // }
package/src/transform.ts CHANGED
@@ -39,10 +39,10 @@ type TransformResult = {
39
39
 
40
40
  type Transform = (
41
41
  ctx: TransformCtx,
42
- guide: Guide,
43
- tspec: TransformSpec,
44
- apimodel: any,
45
- def: any,
42
+ // guide: Guide,
43
+ // tspec: TransformSpec,
44
+ // apimodel: any,
45
+ // def: any,
46
46
  ) => Promise<TransformResult>
47
47
 
48
48
  type ProcessResult = {
package/src/types.ts CHANGED
@@ -96,17 +96,13 @@ type Build = ReturnType<typeof BuildShape>
96
96
 
97
97
 
98
98
  type ApiModel = {
99
- /*
100
99
  main: {
101
- api: Record<string, any>
102
- sdk: {
100
+ kit: {
103
101
  info: Record<string, any>
104
102
  entity: Record<string, any>
105
103
  flow: Record<string, any>
106
104
  }
107
- def: Record<string, any>
108
- }
109
- */
105
+ }
110
106
  }
111
107
 
112
108
 
package/src/utility.ts CHANGED
@@ -39,7 +39,7 @@ function writeFileSyncWarn(warn: Warner, fs: any, path: string, text: string) {
39
39
  catch (err: any) {
40
40
  warn({
41
41
  err,
42
- note: 'Unable to save file: ' + path
42
+ note: 'Unable to save file: ' + relativizePath(path)
43
43
  })
44
44
  }
45
45
  }
@@ -780,6 +780,8 @@ function allcapify(s?: string) {
780
780
  }
781
781
 
782
782
 
783
+ // Get value from object as string, and format as indicated.
784
+ // Example: nom({foo:'bar'},'Foo') -> 'Bar'
783
785
  function nom(v: any, format: string): string {
784
786
  let formatstr = 'string' == typeof format ? format : null
785
787
  if (null == formatstr) {
@@ -795,6 +797,88 @@ function nom(v: any, format: string): string {
795
797
  return out
796
798
  }
797
799
 
800
+
801
+ function relativizePath(path: string): string {
802
+ const cwd = process.cwd()
803
+ if (path.startsWith(cwd)) {
804
+ return '.' + path.slice(cwd.length)
805
+ }
806
+ return path
807
+ }
808
+
809
+
810
+ function getModelPath(
811
+ model: any,
812
+ path: string,
813
+ flags?: { required?: boolean }
814
+ ): any {
815
+ const required = flags?.required ?? true
816
+
817
+ if (path === '') {
818
+ if (required) {
819
+ throw new Error('getModelPath: empty path provided')
820
+ }
821
+ return undefined
822
+ }
823
+
824
+ const parts = path.split('.')
825
+ const fullPath = path // Store the full path for error messages
826
+ let current = model
827
+ let validPath: string[] = []
828
+
829
+ for (let i = 0; i < parts.length; i++) {
830
+ const part = parts[i]
831
+
832
+ if (current == null) {
833
+ if (required) {
834
+ const validPathStr = validPath.length > 0 ? validPath.join('.') : '(root)'
835
+ throw new Error(
836
+ `getModelPath: path not found at '${fullPath}'.\n` +
837
+ `Valid path up to: '${validPathStr}'.\n` +
838
+ `Cannot access property '${part}' of ${current === null ? 'null' : 'undefined'}.`
839
+ )
840
+ }
841
+ return undefined
842
+ }
843
+
844
+ // Check if current is an object before using 'in' operator
845
+ if (typeof current !== 'object' || current === null) {
846
+ if (required) {
847
+ const validPathStr = validPath.length > 0 ? validPath.join('.') : '(root)'
848
+ throw new Error(
849
+ `getModelPath: path not found at '${fullPath}'.\n` +
850
+ `Valid path up to: '${validPathStr}'.\n` +
851
+ `Cannot access property '${part}' of ${typeof current}.`
852
+ )
853
+ }
854
+ return undefined
855
+ }
856
+
857
+ // Check if the key exists
858
+ if (!(part in current)) {
859
+ if (required) {
860
+ const validPathStr = validPath.length > 0 ? validPath.join('.') : '(root)'
861
+ const availableKeys = Array.isArray(current)
862
+ ? `array indices 0-${current.length - 1}`
863
+ : `[${Object.keys(current).join(', ')}]`
864
+
865
+ throw new Error(
866
+ `getModelPath: path not found at '${fullPath}'.\n` +
867
+ `Valid path up to: '${validPathStr}'.\n` +
868
+ `Property '${part}' does not exist.\n` +
869
+ `Available keys: ${availableKeys}`
870
+ )
871
+ }
872
+ return undefined
873
+ }
874
+
875
+ validPath.push(part)
876
+ current = current[part]
877
+ }
878
+
879
+ return current
880
+ }
881
+
798
882
  export type {
799
883
  PathMatch
800
884
  }
@@ -816,5 +900,7 @@ export {
816
900
  findPathsWithPrefix,
817
901
  writeFileSyncWarn,
818
902
  warnOnError,
903
+ relativizePath,
904
+ getModelPath,
819
905
 
820
906
  }