@voxgig/apidef 8.23.0 → 8.24.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.
- package/dist/builder/entity/entity.d.ts +2 -1
- package/dist/builder/entity/entity.js +14 -11
- package/dist/builder/entity/entity.js.map +1 -1
- package/dist/guide/guide.js +5 -2
- package/dist/guide/guide.js.map +1 -1
- package/dist/guide/heuristic01.d.ts +2 -1
- package/dist/guide/heuristic01.js +232 -85
- package/dist/guide/heuristic01.js.map +1 -1
- package/dist/transform/args.d.ts +4 -1
- package/dist/transform/args.js +42 -13
- package/dist/transform/args.js.map +1 -1
- package/dist/transform/body.d.ts +19 -2
- package/dist/transform/body.js +189 -15
- package/dist/transform/body.js.map +1 -1
- package/dist/transform/field.d.ts +3 -1
- package/dist/transform/field.js +159 -87
- package/dist/transform/field.js.map +1 -1
- package/dist/transform/operation.js +77 -24
- package/dist/transform/operation.js.map +1 -1
- package/dist/transform/select.js +10 -4
- package/dist/transform/select.js.map +1 -1
- package/dist/transform/top.d.ts +2 -1
- package/dist/transform/top.js +50 -3
- package/dist/transform/top.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utility.d.ts +9 -1
- package/dist/utility.js +139 -13
- package/dist/utility.js.map +1 -1
- package/package.json +1 -1
- package/src/builder/entity/entity.ts +17 -15
- package/src/guide/guide.ts +5 -3
- package/src/guide/heuristic01.ts +292 -93
- package/src/transform/args.ts +50 -14
- package/src/transform/body.ts +231 -15
- package/src/transform/field.ts +190 -88
- package/src/transform/operation.ts +97 -26
- package/src/transform/select.ts +13 -4
- package/src/transform/top.ts +59 -3
- package/src/utility.ts +169 -13
package/src/transform/field.ts
CHANGED
|
@@ -6,10 +6,13 @@ import type { TransformResult, Transform } from '../transform'
|
|
|
6
6
|
|
|
7
7
|
import {
|
|
8
8
|
validator, canonizeField, inferFieldType, normalizeFieldName, envelopeProp,
|
|
9
|
-
composedEnvelopeProp, mergedProperties, canonizeCmpName,
|
|
10
|
-
scanUntaggedUnion, firstSentence, humanTitle,
|
|
9
|
+
composedEnvelopeProp, mergedProperties, canonizeCmpName, requestWrapperOf,
|
|
10
|
+
scanUntaggedUnion, firstSentence, humanTitle, collapseScalarAllOf,
|
|
11
|
+
itemEnvelopeOf,
|
|
11
12
|
} from '../utility'
|
|
12
13
|
|
|
14
|
+
import { arrayCarrier, guideMedia, sameType, selectedRequestSchema } from './body'
|
|
15
|
+
|
|
13
16
|
import { KIT } from '../types'
|
|
14
17
|
|
|
15
18
|
import type {
|
|
@@ -40,7 +43,7 @@ const fieldTransform: Transform = async function(
|
|
|
40
43
|
|
|
41
44
|
const opFieldPrecedence: OpName[] = ['load', 'create', 'update', 'patch', 'list']
|
|
42
45
|
|
|
43
|
-
each(kit.entity, (ment: ModelEntity,
|
|
46
|
+
each(kit.entity, (ment: ModelEntity, entname: string) => {
|
|
44
47
|
const fields = ment.fields
|
|
45
48
|
|
|
46
49
|
for (let opname of opFieldPrecedence) {
|
|
@@ -49,20 +52,25 @@ const fieldTransform: Transform = async function(
|
|
|
49
52
|
const mpoints = mop.points
|
|
50
53
|
|
|
51
54
|
for (let mpoint of mpoints) {
|
|
52
|
-
const
|
|
55
|
+
const media = guideMedia(guide, entname, opname, mpoint).body
|
|
56
|
+
const opfields = resolveOpFields(ment, mop, mpoint, def, media)
|
|
57
|
+
const carrier = carrierOf(mpoint, def, media)?.name
|
|
53
58
|
|
|
54
59
|
for (let opfield of opfields) {
|
|
55
|
-
|
|
56
|
-
fields[opfield.n] = opfield
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
mergeField(mop, fields[opfield.n], opfield)
|
|
60
|
-
}
|
|
60
|
+
addField(fields, mop, opfield, carrier === opfield.n)
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
// A remove's response is no record, so its carrier is all it adds.
|
|
67
|
+
for (const mpoint of ment.op.remove?.points ?? []) {
|
|
68
|
+
const carrier = carrierOf(mpoint, def, guideMedia(guide, entname, 'remove', mpoint).body)
|
|
69
|
+
if (null != carrier) {
|
|
70
|
+
addField(fields, ment.op.remove as ModelOp, modelField(carrierDef(carrier)), true)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
66
74
|
|
|
67
75
|
const gent = guide?.entity?.[ment.name]
|
|
68
76
|
const composite = compositeId(ment, gent, def)
|
|
@@ -563,58 +571,80 @@ function resolveOpFields(
|
|
|
563
571
|
ment: ModelEntity,
|
|
564
572
|
mop: ModelOp,
|
|
565
573
|
mpoint: ModelPoint,
|
|
566
|
-
def: any
|
|
574
|
+
def: any,
|
|
575
|
+
media?: string,
|
|
567
576
|
): ModelField[] {
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
for (let fielddef of fielddefs) {
|
|
572
|
-
const fieldname = (fielddef as any).key$ as string
|
|
573
|
-
// Field names are WIRE identifiers — see canonizeField. Using the
|
|
574
|
-
// entity-name canonizer here renamed modelType -> model_type and
|
|
575
|
-
// items -> item, so the SDK read keys the server never sends.
|
|
576
|
-
const name = canonizeField(normalizeFieldName(fieldname))
|
|
577
|
-
const mfield: ModelField = {
|
|
578
|
-
n: name,
|
|
579
|
-
h: humanTitle(name),
|
|
580
|
-
t: inferFieldType(name, validator(fielddef.type)),
|
|
581
|
-
r: !!fielddef.required,
|
|
582
|
-
op: {},
|
|
583
|
-
}
|
|
584
|
-
const fdesc = (fielddef as any).description
|
|
585
|
-
if ('string' === typeof fdesc && '' !== fdesc.trim()) {
|
|
586
|
-
const short = firstSentence(fdesc)
|
|
587
|
-
if ('' !== short) {
|
|
588
|
-
mfield.sh = short
|
|
589
|
-
}
|
|
590
|
-
}
|
|
577
|
+
return findFieldDefs(ment, mop, mpoint, def, media).map(modelField)
|
|
578
|
+
}
|
|
591
579
|
|
|
592
|
-
for (const [flag, attr] of [['readOnly', 'ro'], ['writeOnly', 'wo'], ['deprecated', 'de']] as const) {
|
|
593
|
-
if (true === (fielddef as any)[flag]) {
|
|
594
|
-
mfield[attr] = true
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
580
|
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
581
|
+
// The names of a route's fields, read with no action and no carrier of its own.
|
|
582
|
+
function routeFieldNames(
|
|
583
|
+
ment: ModelEntity,
|
|
584
|
+
opname: string,
|
|
585
|
+
mpoint: ModelPoint,
|
|
586
|
+
def: any,
|
|
587
|
+
media?: string,
|
|
588
|
+
): string[] {
|
|
589
|
+
const route = { k: mpoint.k, m: mpoint.m, o: mpoint.o, q: {} } as ModelPoint
|
|
590
|
+
return resolveOpFields(ment, { name: opname } as ModelOp, route, def, media).map((mfield) => mfield.n)
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
function modelField(fielddef: SchemaDef): ModelField {
|
|
595
|
+
const fieldname = (fielddef as any).key$ as string
|
|
596
|
+
// Field names are WIRE identifiers — see canonizeField. Using the
|
|
597
|
+
// entity-name canonizer here renamed modelType -> model_type and
|
|
598
|
+
// items -> item, so the SDK read keys the server never sends.
|
|
599
|
+
const name = canonizeField(normalizeFieldName(fieldname))
|
|
600
|
+
const mfield: ModelField = {
|
|
601
|
+
n: name,
|
|
602
|
+
h: humanTitle(name),
|
|
603
|
+
t: inferFieldType(name, validator(fielddef.type)),
|
|
604
|
+
r: !!fielddef.required,
|
|
605
|
+
op: {},
|
|
606
|
+
}
|
|
607
|
+
const fdesc = (fielddef as any).description
|
|
608
|
+
if ('string' === typeof fdesc && '' !== fdesc.trim()) {
|
|
609
|
+
const short = firstSentence(fdesc)
|
|
610
|
+
if ('' !== short) {
|
|
611
|
+
mfield.sh = short
|
|
604
612
|
}
|
|
613
|
+
}
|
|
605
614
|
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
// of leaving it looking like a modelling failure.
|
|
610
|
-
const union = scanUntaggedUnion(fielddef)
|
|
611
|
-
if (null != union) {
|
|
612
|
-
mfield.union = union
|
|
615
|
+
for (const [flag, attr] of [['readOnly', 'ro'], ['writeOnly', 'wo'], ['deprecated', 'de']] as const) {
|
|
616
|
+
if (true === (fielddef as any)[flag]) {
|
|
617
|
+
mfield[attr] = true
|
|
613
618
|
}
|
|
614
|
-
mfields.push(mfield)
|
|
615
619
|
}
|
|
616
620
|
|
|
617
|
-
|
|
621
|
+
// `format` is an open vocabulary — OpenAPI defines a handful and lets a
|
|
622
|
+
// spec coin its own — so it is carried as the string it is rather than
|
|
623
|
+
// interpreted here. `password` is the one a generator acts on today.
|
|
624
|
+
const ffmt = (fielddef as any).format
|
|
625
|
+
if ('string' === typeof ffmt && '' !== ffmt.trim()) {
|
|
626
|
+
mfield.fo = ffmt.trim()
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// Record an untagged union under this field. The field is already typed
|
|
630
|
+
// openly ($ANY/$ARRAY/$OBJECT) because there is nothing to narrow it to;
|
|
631
|
+
// this says WHY, so the generated docs can explain the open type instead
|
|
632
|
+
// of leaving it looking like a modelling failure.
|
|
633
|
+
const union = scanUntaggedUnion(fielddef)
|
|
634
|
+
if (null != union) {
|
|
635
|
+
mfield.union = union
|
|
636
|
+
}
|
|
637
|
+
return mfield
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
function addField(fields: Record<string, ModelField>, mop: ModelOp, field: ModelField, carrier: boolean) {
|
|
642
|
+
if (!Object.prototype.hasOwnProperty.call(fields, field.n)) {
|
|
643
|
+
fields[field.n] = field
|
|
644
|
+
}
|
|
645
|
+
else {
|
|
646
|
+
mergeField(mop, fields[field.n], field, carrier)
|
|
647
|
+
}
|
|
618
648
|
}
|
|
619
649
|
|
|
620
650
|
|
|
@@ -693,7 +723,8 @@ function findFieldDefs(
|
|
|
693
723
|
ment: ModelEntity,
|
|
694
724
|
mop: ModelOp,
|
|
695
725
|
mpoint: ModelPoint,
|
|
696
|
-
def: any
|
|
726
|
+
def: any,
|
|
727
|
+
media?: string,
|
|
697
728
|
): SchemaDef[] {
|
|
698
729
|
if ('graphql' === mpoint.k) {
|
|
699
730
|
return findGraphqlFieldDefs(ment, mpoint, def)
|
|
@@ -728,8 +759,10 @@ function findFieldDefs(
|
|
|
728
759
|
if ('list' == mop.name) {
|
|
729
760
|
const unwrapped = unwrapArrayWrapper(fieldSets)
|
|
730
761
|
envelope = null == unwrapped ? composedEnvelopeProp(fieldSets, 'list') : null
|
|
762
|
+
const itemkey = itemEnvelopeOf(mpoint.t?.res)
|
|
731
763
|
if (unwrapped) {
|
|
732
|
-
fieldSets = unwrapped
|
|
764
|
+
fieldSets = null == itemkey ? unwrapped :
|
|
765
|
+
mergedProperties(unwrapped)?.[itemkey] ?? unwrapped
|
|
733
766
|
}
|
|
734
767
|
else if (null != envelope) {
|
|
735
768
|
fieldSets = mergedProperties(fieldSets)?.[envelope]?.items
|
|
@@ -764,45 +797,26 @@ function findFieldDefs(
|
|
|
764
797
|
return fielddefs
|
|
765
798
|
}
|
|
766
799
|
|
|
767
|
-
// A QUERY (RFC 10008)
|
|
768
|
-
//
|
|
769
|
-
//
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
800
|
+
// A QUERY (RFC 10008) body is a filter, not the entity shape, so a QUERY
|
|
801
|
+
// op's fields come from its response alone; an action's body is the
|
|
802
|
+
// verb's arguments, never the record.
|
|
803
|
+
const reqschema = selectedRequestSchema(def, mpoint.m, mpoint.o, media) ?? getx(requestBody, 'schema')
|
|
804
|
+
if ((requestBody || null != reqschema) && 'query' !== method && !isAction) {
|
|
805
|
+
// A body that sends the record under one key holds its fields there.
|
|
806
|
+
const reqkey = requestWrapperOf(mpoint.t?.req)
|
|
773
807
|
fieldSets = [
|
|
774
808
|
fieldSets,
|
|
775
|
-
|
|
776
|
-
getx(requestBody, 'schema')
|
|
809
|
+
null == reqkey ? reqschema : mergedProperties(reqschema)?.[reqkey] ?? reqschema,
|
|
777
810
|
]
|
|
778
811
|
}
|
|
779
812
|
|
|
780
813
|
|
|
781
|
-
if (fieldSets) {
|
|
782
|
-
|
|
783
|
-
fieldSets = fieldSets.allOf
|
|
784
|
-
}
|
|
785
|
-
else if (fieldSets.properties) {
|
|
786
|
-
fieldSets = [fieldSets]
|
|
787
|
-
}
|
|
814
|
+
if (fieldSets && (Array.isArray(fieldSets.allOf) || fieldSets.properties)) {
|
|
815
|
+
fieldSets = [fieldSets]
|
|
788
816
|
}
|
|
789
817
|
|
|
790
818
|
each(fieldSets, (fieldSet: any) => {
|
|
791
|
-
|
|
792
|
-
? fieldSet.required : []
|
|
793
|
-
each(fieldSet?.properties, (property: any) => {
|
|
794
|
-
// Don't mutate the parsed schema: a $ref-resolved schema is shared
|
|
795
|
-
// across every operation that references it, so flipping
|
|
796
|
-
// `property.required = true` here would leak this operation's
|
|
797
|
-
// required[] onto all the others. Derive `required` onto a shallow
|
|
798
|
-
// copy instead (matches the Go port, which builds fresh field defs).
|
|
799
|
-
if (!property.required && requiredNames.includes(property.key$)) {
|
|
800
|
-
fielddefs.push({ ...property, required: true })
|
|
801
|
-
}
|
|
802
|
-
else {
|
|
803
|
-
fielddefs.push(property)
|
|
804
|
-
}
|
|
805
|
-
})
|
|
819
|
+
fielddefs.push(...composedFields(fieldSet))
|
|
806
820
|
})
|
|
807
821
|
}
|
|
808
822
|
|
|
@@ -814,10 +828,94 @@ function findFieldDefs(
|
|
|
814
828
|
}
|
|
815
829
|
}
|
|
816
830
|
|
|
831
|
+
// An array body is sent from one field of the request data, and has no
|
|
832
|
+
// properties of its own to contribute.
|
|
833
|
+
const carrier = carrierOf(mpoint, def, media)
|
|
834
|
+
if (null != carrier) {
|
|
835
|
+
fielddefs.push(carrierDef(carrier))
|
|
836
|
+
}
|
|
837
|
+
|
|
817
838
|
return fielddefs
|
|
818
839
|
}
|
|
819
840
|
|
|
820
841
|
|
|
842
|
+
// An action's request fields are its own, so it declares no carrier.
|
|
843
|
+
function carrierOf(mpoint: ModelPoint, def: any, media?: string) {
|
|
844
|
+
if ('graphql' === mpoint.k || null != (mpoint as any).q?.['$action']) {
|
|
845
|
+
return undefined
|
|
846
|
+
}
|
|
847
|
+
return arrayCarrier(def, mpoint, media)
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
// Optional, as the record never holds it.
|
|
852
|
+
function carrierDef(carrier: { name: string, type: string | string[], description?: string }): SchemaDef {
|
|
853
|
+
return { key$: carrier.name, type: carrier.type, description: carrier.description } as SchemaDef
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
// A schema and its allOf members describe one object: a name any of them
|
|
858
|
+
// requires is required, and a property declared more than once is one field
|
|
859
|
+
// taking each fact from the first declaration that states it.
|
|
860
|
+
function composedFields(schema: any): any[] {
|
|
861
|
+
const parts: any[] = []
|
|
862
|
+
const seen = new Set<any>()
|
|
863
|
+
const visit = (node: any) => {
|
|
864
|
+
if (null == node || 'object' !== typeof node || seen.has(node)) {
|
|
865
|
+
return
|
|
866
|
+
}
|
|
867
|
+
seen.add(node)
|
|
868
|
+
parts.push(node)
|
|
869
|
+
if (Array.isArray(node.allOf)) {
|
|
870
|
+
node.allOf.forEach(visit)
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
visit(schema)
|
|
874
|
+
|
|
875
|
+
const required = new Set(parts.flatMap((part) => Array.isArray(part.required) ? part.required : []))
|
|
876
|
+
const decls: Record<string, any[]> = Object.create(null)
|
|
877
|
+
for (const part of parts) {
|
|
878
|
+
each(part.properties, (property: any) => {
|
|
879
|
+
(decls[property.key$] = decls[property.key$] ?? []).push(collapseScalarAllOf(property))
|
|
880
|
+
})
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
return Object.keys(decls).map((name) => {
|
|
884
|
+
const property = 1 === decls[name].length ? decls[name][0] : mergeDeclarations(decls[name])
|
|
885
|
+
// A copy: parsed schemas are shared by every operation referencing them.
|
|
886
|
+
return !property.required && required.has(name) ? { ...property, required: true } : property
|
|
887
|
+
})
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
function mergeDeclarations(decls: any[]): any {
|
|
892
|
+
const merged: any = {}
|
|
893
|
+
for (const decl of decls) {
|
|
894
|
+
for (const [key, value] of Object.entries(decl)) {
|
|
895
|
+
if (!statesFact(key, merged[key]) && statesFact(key, value)) {
|
|
896
|
+
merged[key] = value
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
return merged
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
|
|
904
|
+
const ANNOTATION_FLAGS = ['readOnly', 'writeOnly', 'deprecated']
|
|
905
|
+
|
|
906
|
+
// A blank string states nothing, and nor does a false annotation flag, so a
|
|
907
|
+
// later declaration may.
|
|
908
|
+
function statesFact(key: string, value: any): boolean {
|
|
909
|
+
if (null == value) {
|
|
910
|
+
return false
|
|
911
|
+
}
|
|
912
|
+
if ('string' === typeof value) {
|
|
913
|
+
return '' !== value.trim()
|
|
914
|
+
}
|
|
915
|
+
return !(false === value && ANNOTATION_FLAGS.includes(key))
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
|
|
821
919
|
function answersOnlyAccepted(responses: any): boolean {
|
|
822
920
|
return null == responses['200'] && null == responses['201'] && null != responses['202']
|
|
823
921
|
}
|
|
@@ -935,12 +1033,15 @@ function inferTypeFromValue(value: any): string {
|
|
|
935
1033
|
}
|
|
936
1034
|
|
|
937
1035
|
|
|
1036
|
+
// A carrier keeps its array type for its operation where it meets a field of
|
|
1037
|
+
// another type.
|
|
938
1038
|
function mergeField(
|
|
939
1039
|
mop: ModelOp,
|
|
940
1040
|
existingField: ModelField,
|
|
941
|
-
newField: ModelField
|
|
1041
|
+
newField: ModelField,
|
|
1042
|
+
carrier: boolean = false,
|
|
942
1043
|
) {
|
|
943
|
-
if (newField.r !== existingField.r) {
|
|
1044
|
+
if (newField.r !== existingField.r || (carrier && !sameType(newField.t, existingField.t))) {
|
|
944
1045
|
existingField.op[mop.name] = {
|
|
945
1046
|
req: newField.r,
|
|
946
1047
|
type: newField.t,
|
|
@@ -965,4 +1066,5 @@ export {
|
|
|
965
1066
|
fieldTransform,
|
|
966
1067
|
inferFieldsFromExamples,
|
|
967
1068
|
inferTypeFromValue,
|
|
1069
|
+
routeFieldNames,
|
|
968
1070
|
}
|
|
@@ -1,4 +1,14 @@
|
|
|
1
|
-
import { guideActive } from '../utility'
|
|
1
|
+
import { bodyRequestTransform, guideActive } from '../utility'
|
|
2
|
+
|
|
3
|
+
import { arrayBodyField } from '../guide/heuristic01'
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
arrayCarrier, arrayRequestSchema, guideMedia, nullableType, requestDecl, requestSchema, sameType,
|
|
7
|
+
} from './body'
|
|
8
|
+
|
|
9
|
+
import { routeFieldNames } from './field'
|
|
10
|
+
|
|
11
|
+
import { routeArgNames } from './args'
|
|
2
12
|
|
|
3
13
|
|
|
4
14
|
import { each } from 'jostraca'
|
|
@@ -21,6 +31,7 @@ import type {
|
|
|
21
31
|
ModelOpMap,
|
|
22
32
|
ModelOp,
|
|
23
33
|
ModelPoint,
|
|
34
|
+
ModelEntity,
|
|
24
35
|
} from '../model'
|
|
25
36
|
|
|
26
37
|
|
|
@@ -40,7 +51,7 @@ const IGNORED_OPS = ['head', 'options', 'OPTIONS']
|
|
|
40
51
|
const operationTransform: Transform = async function(
|
|
41
52
|
ctx: any,
|
|
42
53
|
): Promise<TransformResult> {
|
|
43
|
-
const { apimodel, guide } = ctx
|
|
54
|
+
const { apimodel, def, guide } = ctx
|
|
44
55
|
const kit = apimodel.main[KIT]
|
|
45
56
|
|
|
46
57
|
let msg = 'operation '
|
|
@@ -59,12 +70,24 @@ const operationTransform: Transform = async function(
|
|
|
59
70
|
patch: undefined,
|
|
60
71
|
}
|
|
61
72
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
73
|
+
const on = { gent, def, entname, guide }
|
|
74
|
+
resolveLoad(opm, on)
|
|
75
|
+
resolveList(opm, on)
|
|
76
|
+
resolveCreate(opm, on)
|
|
77
|
+
resolveUpdate(opm, on)
|
|
78
|
+
resolveRemove(opm, on)
|
|
79
|
+
resolvePatch(opm, on)
|
|
80
|
+
|
|
81
|
+
// After patch has joined update, so each operation's routes are final.
|
|
82
|
+
for (const mop of Object.values(opm)) {
|
|
83
|
+
for (const mpoint of mop?.points ?? []) {
|
|
84
|
+
if (heuristicRequest(on, mop!, mpoint)) {
|
|
85
|
+
mpoint.t.req = undefined
|
|
86
|
+
}
|
|
87
|
+
mpoint.t.req = mpoint.t.req ?? requestDefault(on, kit.entity[entname], opm, mop!, mpoint)
|
|
88
|
+
mpoint.t.res = mpoint.t.res ?? '`body`'
|
|
89
|
+
}
|
|
90
|
+
}
|
|
68
91
|
|
|
69
92
|
kit.entity[entname].op = opm
|
|
70
93
|
|
|
@@ -118,40 +141,40 @@ function collectOps(ctx: any, gent: GuideEntity) {
|
|
|
118
141
|
|
|
119
142
|
|
|
120
143
|
|
|
121
|
-
function resolveLoad(opm: ModelOpMap,
|
|
122
|
-
const opdesc = opm.load = resolveOp('load',
|
|
144
|
+
function resolveLoad(opm: ModelOpMap, on: OpEntity): undefined | ModelOp {
|
|
145
|
+
const opdesc = opm.load = resolveOp('load', on)
|
|
123
146
|
return opdesc
|
|
124
147
|
}
|
|
125
148
|
|
|
126
149
|
|
|
127
|
-
function resolveList(opm: ModelOpMap,
|
|
128
|
-
const opdesc = opm.list = resolveOp('list',
|
|
150
|
+
function resolveList(opm: ModelOpMap, on: OpEntity): undefined | ModelOp {
|
|
151
|
+
const opdesc = opm.list = resolveOp('list', on)
|
|
129
152
|
return opdesc
|
|
130
153
|
}
|
|
131
154
|
|
|
132
155
|
|
|
133
|
-
function resolveCreate(opm: ModelOpMap,
|
|
134
|
-
const opdesc = opm.create = resolveOp('create',
|
|
156
|
+
function resolveCreate(opm: ModelOpMap, on: OpEntity): undefined | ModelOp {
|
|
157
|
+
const opdesc = opm.create = resolveOp('create', on)
|
|
135
158
|
return opdesc
|
|
136
159
|
}
|
|
137
160
|
|
|
138
161
|
|
|
139
|
-
function resolveUpdate(opm: ModelOpMap,
|
|
140
|
-
const opdesc = opm.update = resolveOp('update',
|
|
162
|
+
function resolveUpdate(opm: ModelOpMap, on: OpEntity): undefined | ModelOp {
|
|
163
|
+
const opdesc = opm.update = resolveOp('update', on)
|
|
141
164
|
return opdesc
|
|
142
165
|
}
|
|
143
166
|
|
|
144
167
|
|
|
145
|
-
function resolveRemove(opm: ModelOpMap,
|
|
146
|
-
const opdesc = opm.remove = resolveOp('remove',
|
|
168
|
+
function resolveRemove(opm: ModelOpMap, on: OpEntity): undefined | ModelOp {
|
|
169
|
+
const opdesc = opm.remove = resolveOp('remove', on)
|
|
147
170
|
return opdesc
|
|
148
171
|
}
|
|
149
172
|
|
|
150
173
|
|
|
151
|
-
function resolvePatch(opm: ModelOpMap,
|
|
152
|
-
const opdesc = resolveOp('patch',
|
|
174
|
+
function resolvePatch(opm: ModelOpMap, on: OpEntity): undefined | ModelOp {
|
|
175
|
+
const opdesc = resolveOp('patch', on)
|
|
153
176
|
|
|
154
|
-
if (null != opdesc && (null == opm.update || onlyActionPaths(gent, 'update'))) {
|
|
177
|
+
if (null != opdesc && (null == opm.update || onlyActionPaths(on.gent, 'update'))) {
|
|
155
178
|
if (null != opm.update) {
|
|
156
179
|
opdesc.points.push(...opm.update.points)
|
|
157
180
|
}
|
|
@@ -174,9 +197,12 @@ function onlyActionPaths(gent: GuideEntity, opname: OpName): boolean {
|
|
|
174
197
|
}
|
|
175
198
|
|
|
176
199
|
|
|
177
|
-
|
|
200
|
+
type OpEntity = { gent: GuideEntity, def: any, entname: string, guide: any }
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
function resolveOp(opname: OpName, on: OpEntity): undefined | ModelOp {
|
|
178
204
|
let mop: undefined | ModelOp = undefined
|
|
179
|
-
let opdesc = (gent as any).opm$[opname]
|
|
205
|
+
let opdesc = (on.gent as any).opm$[opname]
|
|
180
206
|
if (opdesc) {
|
|
181
207
|
mop = {
|
|
182
208
|
name: opname,
|
|
@@ -195,9 +221,6 @@ function resolveOp(opname: OpName, gent: GuideEntity): undefined | ModelOp {
|
|
|
195
221
|
}
|
|
196
222
|
}
|
|
197
223
|
|
|
198
|
-
mpoint.t.req = mpoint.t.req ?? '`reqdata`'
|
|
199
|
-
mpoint.t.res = mpoint.t.res ?? '`body`'
|
|
200
|
-
|
|
201
224
|
return mpoint
|
|
202
225
|
})
|
|
203
226
|
}
|
|
@@ -208,6 +231,54 @@ function resolveOp(opname: OpName, gent: GuideEntity): undefined | ModelOp {
|
|
|
208
231
|
|
|
209
232
|
|
|
210
233
|
|
|
234
|
+
// The request transform the heuristic took from the default body, still in
|
|
235
|
+
// place where the guide selects another media type for the point.
|
|
236
|
+
function heuristicRequest(on: OpEntity, mop: ModelOp, mpoint: ModelPoint): boolean {
|
|
237
|
+
const chosen = guideMedia(on.guide, on.entname, mop.name, mpoint).body
|
|
238
|
+
if (null == chosen || null == mpoint.t.req) return false
|
|
239
|
+
const generated = bodyRequestTransform(requestSchema(on.def, mpoint.m, mpoint.o), [on.gent.name])
|
|
240
|
+
return null != generated && sameTransform(mpoint.t.req, generated)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
function sameTransform(a: any, b: any): boolean {
|
|
245
|
+
const canon = (t: any) => 'string' === typeof t ? t :
|
|
246
|
+
JSON.stringify(Object.keys(t ?? {}).sort().map((k: string) => [k, t[k]]))
|
|
247
|
+
return canon(a) === canon(b)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
// An array body is sent from one field of the request data, named for its
|
|
252
|
+
// records, and never for an argument of its operation, a field another route of
|
|
253
|
+
// its entity has, as fields span operations, or a carrier already named for an
|
|
254
|
+
// array of another type or requiredness. Decided here, not by the guide
|
|
255
|
+
// heuristic, as the guide's media type decides the body.
|
|
256
|
+
function requestDefault(
|
|
257
|
+
on: OpEntity, ment: ModelEntity, opm: ModelOpMap, mop: ModelOp, mpoint: ModelPoint,
|
|
258
|
+
): any {
|
|
259
|
+
const media = (opname: string, q: ModelPoint) => guideMedia(on.guide, on.entname, opname, q).body
|
|
260
|
+
const chosen = media(mop.name, mpoint)
|
|
261
|
+
const list = arrayRequestSchema(on.def, mpoint.m, mpoint.o, chosen)
|
|
262
|
+
if (null == list) {
|
|
263
|
+
return (null == chosen ? undefined :
|
|
264
|
+
bodyRequestTransform(requestSchema(on.def, mpoint.m, mpoint.o, chosen), [on.gent.name])) ?? '`reqdata`'
|
|
265
|
+
}
|
|
266
|
+
const required = true === requestDecl(on.def, mpoint.m, mpoint.o)?.required
|
|
267
|
+
const others = mop.points.filter((q) => q !== mpoint)
|
|
268
|
+
const routes = Object.values(opm).flatMap((op) =>
|
|
269
|
+
(op?.points ?? []).filter((q) => q !== mpoint).map((q) => ({ opname: op!.name, q })))
|
|
270
|
+
const taken = [
|
|
271
|
+
...mop.points.flatMap((q) => routeArgNames(on.def, q)),
|
|
272
|
+
...routes.flatMap(({ opname, q }) => routeFieldNames(ment, opname, q, on.def, media(opname, q))),
|
|
273
|
+
...others.map((q) => arrayCarrier(on.def, q, media(mop.name, q)))
|
|
274
|
+
.filter((carrier) => null != carrier &&
|
|
275
|
+
!(sameType(carrier.type, nullableType(list)) && carrier.required === required))
|
|
276
|
+
.map((carrier) => carrier!.name),
|
|
277
|
+
]
|
|
278
|
+
return '`reqdata.' + arrayBodyField(list, on.entname, taken) + '`'
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
|
|
211
282
|
export {
|
|
212
283
|
operationTransform,
|
|
213
284
|
}
|
package/src/transform/select.ts
CHANGED
|
@@ -7,6 +7,8 @@ import type { TransformResult, Transform } from '../transform'
|
|
|
7
7
|
|
|
8
8
|
import { KIT } from '../types'
|
|
9
9
|
|
|
10
|
+
import { arrayCarrier, guideMedia } from './body'
|
|
11
|
+
|
|
10
12
|
import type {
|
|
11
13
|
KitModel,
|
|
12
14
|
Guide,
|
|
@@ -34,13 +36,14 @@ const selectTransform: Transform = async function(
|
|
|
34
36
|
|
|
35
37
|
let msg = 'select '
|
|
36
38
|
|
|
37
|
-
each(kit.entity, (ment: ModelEntity,
|
|
38
|
-
each(ment.op, (mop: ModelOp,
|
|
39
|
+
each(kit.entity, (ment: ModelEntity, entname: string) => {
|
|
40
|
+
each(ment.op, (mop: ModelOp, opname: OpName) => {
|
|
39
41
|
each(mop.points, (mpoint: ModelPoint) => {
|
|
40
42
|
// GraphQL defs have no `paths`; the lookup is only passed through to
|
|
41
43
|
// an unused parameter, so skip it rather than dereference undefined.
|
|
42
44
|
const pdef: PathDef = def.paths?.[mpoint.o]
|
|
43
|
-
|
|
45
|
+
const carrier = arrayCarrier(def, mpoint, guideMedia(guide, entname, opname, mpoint).body)
|
|
46
|
+
resolveSelect(guide, ment, mop, mpoint, pdef, carrier)
|
|
44
47
|
})
|
|
45
48
|
if (null != mop.points && 0 < mop.points.length) {
|
|
46
49
|
sortPoints(guide, ment, mop)
|
|
@@ -60,7 +63,8 @@ function resolveSelect(
|
|
|
60
63
|
ment: ModelEntity,
|
|
61
64
|
_mop: ModelOp,
|
|
62
65
|
mpoint: ModelPoint,
|
|
63
|
-
_pdef: PathDef
|
|
66
|
+
_pdef: PathDef,
|
|
67
|
+
carrier?: { name: string, required: boolean },
|
|
64
68
|
) {
|
|
65
69
|
const select: any = mpoint.q
|
|
66
70
|
const margs: any = mpoint.g
|
|
@@ -85,6 +89,11 @@ function resolveSelect(
|
|
|
85
89
|
})
|
|
86
90
|
})
|
|
87
91
|
|
|
92
|
+
// The field an array body is sent from, when the body is required.
|
|
93
|
+
if (carrier?.required && !select.exist.includes(carrier.name)) {
|
|
94
|
+
select.exist.push(carrier.name)
|
|
95
|
+
}
|
|
96
|
+
|
|
88
97
|
select.exist.sort()
|
|
89
98
|
|
|
90
99
|
const gent = guide.entity[ment.name]
|