@voxgig/apidef 8.23.0 → 8.25.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 +251 -86
- package/dist/guide/heuristic01.js.map +1 -1
- package/dist/model.d.ts +1 -0
- 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 +21 -2
- package/dist/transform/body.js +228 -15
- package/dist/transform/body.js.map +1 -1
- package/dist/transform/field.d.ts +3 -1
- package/dist/transform/field.js +154 -91
- 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 +10 -1
- package/dist/utility.js +145 -13
- package/dist/utility.js.map +1 -1
- package/model/apidef.aontu +1 -0
- 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 +316 -94
- package/src/model.ts +2 -0
- package/src/transform/args.ts +50 -14
- package/src/transform/body.ts +280 -16
- package/src/transform/field.ts +184 -91
- 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 +177 -13
package/src/transform/args.ts
CHANGED
|
@@ -71,11 +71,7 @@ const argsTransform: Transform = async function(
|
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
else {
|
|
74
|
-
|
|
75
|
-
argdefs.push(...((pathdef as any)?.parameters ?? []))
|
|
76
|
-
|
|
77
|
-
const opdef: MethodDef = (pathdef as any)?.[mpoint.m.toLowerCase()]
|
|
78
|
-
argdefs.push(...(opdef?.parameters ?? []))
|
|
74
|
+
argdefs.push(...routeArgdefs(def, mpoint))
|
|
79
75
|
}
|
|
80
76
|
|
|
81
77
|
resolveArgs(ctx, ment, mop, mpoint, argdefs)
|
|
@@ -90,6 +86,22 @@ const argsTransform: Transform = async function(
|
|
|
90
86
|
}
|
|
91
87
|
|
|
92
88
|
|
|
89
|
+
function routeArgdefs(def: any, mpoint: ModelPoint): ParameterDef[] {
|
|
90
|
+
const pathdef: PathDef = def.paths?.[mpoint.o]
|
|
91
|
+
const opdef: MethodDef = (pathdef as any)?.[mpoint.m.toLowerCase()]
|
|
92
|
+
return [...((pathdef as any)?.parameters ?? []), ...(opdef?.parameters ?? [])]
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
// The names a caller gives a REST route's arguments, as this step names them.
|
|
97
|
+
function routeArgNames(def: any, mpoint: ModelPoint): string[] {
|
|
98
|
+
const route = { ...mpoint, g: {} } as ModelPoint
|
|
99
|
+
resolveArgs(undefined, { name: '' } as ModelEntity, { name: '', points: [] } as unknown as ModelOp, route,
|
|
100
|
+
routeArgdefs(def, mpoint))
|
|
101
|
+
return Object.values(route.g).flat().map((arg: any) => arg.n)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
|
|
93
105
|
// Locate the normalised root-field descriptor a GraphQL point came from.
|
|
94
106
|
function graphqlFieldDef(def: any, mpoint: ModelPoint): any {
|
|
95
107
|
const field = mpoint.gq?.field ?? mpoint.o
|
|
@@ -123,6 +135,11 @@ function resolveArgs(
|
|
|
123
135
|
const placeholders = [...String(mpoint.o ?? '').matchAll(/\{([^}]+)\}/g)].map((m) => m[1])
|
|
124
136
|
|
|
125
137
|
each(argdefs, (argdef: ParameterDef) => {
|
|
138
|
+
// A Swagger body parameter is the request body, which the body step reads.
|
|
139
|
+
if ('body' === (argdef as any).in) {
|
|
140
|
+
return
|
|
141
|
+
}
|
|
142
|
+
|
|
126
143
|
const specName = normalizeFieldName(argdef.name)
|
|
127
144
|
const orig = depluralize(snakify(specName))
|
|
128
145
|
|
|
@@ -158,23 +175,27 @@ function resolveArgs(
|
|
|
158
175
|
param: argdef.name,
|
|
159
176
|
})
|
|
160
177
|
}
|
|
161
|
-
//
|
|
162
|
-
//
|
|
163
|
-
//
|
|
178
|
+
// A path argument is named by the lookup that names its segment, so the
|
|
179
|
+
// two agree under a raw rename key, and one that fills a placeholder is
|
|
180
|
+
// required, as OpenAPI requires; a GraphQL argument keeps its own flag.
|
|
181
|
+
// Any other rename map is keyed by the spec original or the snakified form.
|
|
182
|
+
const path = 'param' === kind
|
|
183
|
+
const fills = path && placeholders.some((p) => p === argdef.name || canonizeParam(p) === orig)
|
|
164
184
|
const renameMap = mpoint.r[kind]
|
|
165
|
-
const name =
|
|
185
|
+
const name = path ? paramName(argdef.name, mpoint.r.param) :
|
|
166
186
|
(renameMap?.[specName] ?? renameMap?.[orig] ?? orig)
|
|
187
|
+
const schema = paramSchema(argdef)
|
|
167
188
|
// The name the definition gives, which the SDK sends on the wire. The
|
|
168
189
|
// model name beside it is only what a caller writes.
|
|
169
190
|
const marg: ModelArg = {
|
|
170
191
|
n: name,
|
|
171
192
|
or: String(argdef.name),
|
|
172
|
-
t: inferFieldType(name, validator(
|
|
193
|
+
t: inferFieldType(name, validator(schema?.type)),
|
|
173
194
|
k: kind,
|
|
174
|
-
r:
|
|
195
|
+
r: fills || !!argdef.required
|
|
175
196
|
}
|
|
176
197
|
|
|
177
|
-
const example = resolveArgExample(argdef)
|
|
198
|
+
const example = resolveArgExample(argdef, schema)
|
|
178
199
|
if (undefined !== example) {
|
|
179
200
|
marg.ex = example
|
|
180
201
|
}
|
|
@@ -223,7 +244,21 @@ function resolveArgs(
|
|
|
223
244
|
}
|
|
224
245
|
|
|
225
246
|
|
|
226
|
-
|
|
247
|
+
// Type facts sit on a Swagger 2 parameter itself, and under `schema` in
|
|
248
|
+
// OpenAPI 3. A formData parameter converts to a body field, not a parameter.
|
|
249
|
+
function paramSchema(argdef: any): any {
|
|
250
|
+
if (null != argdef?.schema) {
|
|
251
|
+
return argdef.schema
|
|
252
|
+
}
|
|
253
|
+
if ('formData' === argdef?.in) {
|
|
254
|
+
return undefined
|
|
255
|
+
}
|
|
256
|
+
// Swagger 2's file type is a binary string in OpenAPI 3.
|
|
257
|
+
return 'file' === argdef?.type ? { ...argdef, type: 'string', format: 'binary' } : argdef
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
function resolveArgExample(argdef: any, schema: any): any {
|
|
227
262
|
if (undefined !== argdef?.example) return argdef.example
|
|
228
263
|
|
|
229
264
|
const examples = argdef?.examples
|
|
@@ -235,7 +270,6 @@ function resolveArgExample(argdef: any): any {
|
|
|
235
270
|
}
|
|
236
271
|
}
|
|
237
272
|
|
|
238
|
-
const schema = argdef?.schema
|
|
239
273
|
if (schema) {
|
|
240
274
|
if (undefined !== schema.example) return schema.example
|
|
241
275
|
if (undefined !== schema.default) return schema.default
|
|
@@ -247,4 +281,6 @@ function resolveArgExample(argdef: any): any {
|
|
|
247
281
|
|
|
248
282
|
export {
|
|
249
283
|
argsTransform,
|
|
284
|
+
resolveArgExample,
|
|
285
|
+
routeArgNames,
|
|
250
286
|
}
|
package/src/transform/body.ts
CHANGED
|
@@ -4,7 +4,9 @@ import type { TransformResult, Transform } from '../transform'
|
|
|
4
4
|
|
|
5
5
|
import { KIT } from '../types'
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
fieldName, guideActive, mergedProperties, requestWrapperOf, sortedKeys,
|
|
9
|
+
} from '../utility'
|
|
8
10
|
|
|
9
11
|
import type {
|
|
10
12
|
BodyKind,
|
|
@@ -37,6 +39,12 @@ type Offer = {
|
|
|
37
39
|
swagger?: boolean
|
|
38
40
|
}
|
|
39
41
|
|
|
42
|
+
type Ranked = {
|
|
43
|
+
offer: Offer
|
|
44
|
+
declared: string
|
|
45
|
+
body: ModelBody
|
|
46
|
+
}
|
|
47
|
+
|
|
40
48
|
|
|
41
49
|
const bodyTransform: Transform = async function(
|
|
42
50
|
ctx: any,
|
|
@@ -58,6 +66,10 @@ const bodyTransform: Transform = async function(
|
|
|
58
66
|
if (null != rb) {
|
|
59
67
|
mpoint.rb = rb
|
|
60
68
|
}
|
|
69
|
+
const bf = bodyFields(def, mpoint, media.body)
|
|
70
|
+
if (null != bf) {
|
|
71
|
+
mpoint.bf = bf
|
|
72
|
+
}
|
|
61
73
|
const rs = responseBody(def, mpoint.m, mpoint.o, media.response)
|
|
62
74
|
if (null != rs) {
|
|
63
75
|
mpoint.rs = rs
|
|
@@ -98,14 +110,12 @@ function requestBody(
|
|
|
98
110
|
path: string,
|
|
99
111
|
media?: string,
|
|
100
112
|
): ModelBody | undefined {
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
if (!isMap(opdef)) {
|
|
113
|
+
const offers = requestOffers(def, method, path)
|
|
114
|
+
if (null == offers) {
|
|
104
115
|
return undefined
|
|
105
116
|
}
|
|
106
117
|
|
|
107
|
-
const body = chooseBody(
|
|
108
|
-
swaggerOffers(def, pathdef, opdef) : openapiOffers(opdef), media)
|
|
118
|
+
const body = chooseBody(offers, media)
|
|
109
119
|
|
|
110
120
|
if (null == body || ('json' === body.kind && JSON_MEDIA === essence(body.media) &&
|
|
111
121
|
(body.alternatives ?? []).every((other) => 'json' === other.kind))) {
|
|
@@ -133,20 +143,23 @@ function responseBody(
|
|
|
133
143
|
}
|
|
134
144
|
|
|
135
145
|
|
|
146
|
+
function requestOffers(def: any, method: string, path: string): Offer[] | undefined {
|
|
147
|
+
const pathdef = def?.paths?.[path]
|
|
148
|
+
const opdef = pathdef?.[String(method).toLowerCase()]
|
|
149
|
+
if (!isMap(opdef)) {
|
|
150
|
+
return undefined
|
|
151
|
+
}
|
|
152
|
+
return null != def.swagger ? swaggerOffers(def, pathdef, opdef) : openapiOffers(opdef)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
|
|
136
156
|
function chooseBody(offers: Offer[], media?: string): ModelBody | undefined {
|
|
137
|
-
const ranked = offers
|
|
138
|
-
.sort((a, b) => compare(a.media, b.media))
|
|
139
|
-
.map((offer) => ({ declared: offer.media.trim().toLowerCase(), body: describeBody(offer) }))
|
|
140
|
-
.sort((a, b) => byPreference(a.body, b.body))
|
|
157
|
+
const ranked = rankOffers(offers)
|
|
141
158
|
const bodies = ranked.map((entry) => entry.body).filter((body, i, all) =>
|
|
142
159
|
i === all.findIndex((other) => other.media === body.media))
|
|
143
160
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const chosen = null == named ? bodies[0] :
|
|
147
|
-
ranked.find((entry) => entry.declared === named)?.body ??
|
|
148
|
-
bodies.find((body) => body.media.toLowerCase() === named) ??
|
|
149
|
-
describeBody({ media: textOf(media) as string })
|
|
161
|
+
const chosen = chooseOffer(ranked, media)?.body ??
|
|
162
|
+
(null == textOf(media) ? undefined : describeBody({ media: textOf(media) as string }))
|
|
150
163
|
|
|
151
164
|
if (null == chosen) {
|
|
152
165
|
return undefined
|
|
@@ -157,6 +170,23 @@ function chooseBody(offers: Offer[], media?: string): ModelBody | undefined {
|
|
|
157
170
|
}
|
|
158
171
|
|
|
159
172
|
|
|
173
|
+
function rankOffers(offers: Offer[]): Ranked[] {
|
|
174
|
+
return offers
|
|
175
|
+
.sort((a, b) => compare(a.media, b.media))
|
|
176
|
+
.map((offer) => ({ offer, declared: offer.media.trim().toLowerCase(), body: describeBody(offer) }))
|
|
177
|
+
.sort((a, b) => byPreference(a.body, b.body))
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
// A named media type is matched as declared first, so a range keeps its schema.
|
|
182
|
+
function chooseOffer(ranked: Ranked[], media?: string): Ranked | undefined {
|
|
183
|
+
const named = textOf(media)?.toLowerCase()
|
|
184
|
+
return null == named ? ranked[0] :
|
|
185
|
+
ranked.find((entry) => entry.declared === named) ??
|
|
186
|
+
ranked.find((entry) => entry.body.media.toLowerCase() === named)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
|
|
160
190
|
function openapiOffers(opdef: any): Offer[] {
|
|
161
191
|
const content = opdef.requestBody?.content
|
|
162
192
|
if (!isMap(content)) {
|
|
@@ -195,9 +225,11 @@ function swaggerOffers(def: any, pathdef: any, opdef: any): Offer[] {
|
|
|
195
225
|
}
|
|
196
226
|
|
|
197
227
|
const bodySchema = null == body ? undefined : (body.schema ?? {})
|
|
228
|
+
const required = form.filter((param) => true === param.required).map((param) => param.name)
|
|
198
229
|
const formSchema = 0 === form.length ? undefined : {
|
|
199
230
|
type: 'object',
|
|
200
231
|
properties: Object.fromEntries(form.map((param) => [param.name, formProperty(param)])),
|
|
232
|
+
...(0 < required.length ? { required } : {}),
|
|
201
233
|
}
|
|
202
234
|
|
|
203
235
|
const declared = listOf(Array.isArray(opdef.consumes) ? opdef.consumes : def.consumes)
|
|
@@ -415,8 +447,240 @@ function compare(a: string, b: string): number {
|
|
|
415
447
|
}
|
|
416
448
|
|
|
417
449
|
|
|
450
|
+
// The schema the body step sends a request body with, when that body is JSON.
|
|
451
|
+
function jsonSchema(offers: Offer[], media?: string): any {
|
|
452
|
+
const chosen = chooseOffer(rankOffers(offers), media)
|
|
453
|
+
return 'json' === chosen?.body.kind ? chosen.offer.schema : undefined
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
function jsonRequestSchema(opdef: any): any {
|
|
458
|
+
return isMap(opdef) ? jsonSchema(openapiOffers(opdef)) : undefined
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
// The JSON schema a point's request body is sent with, under the media type
|
|
463
|
+
// the guide names, else the one the body step prefers.
|
|
464
|
+
function requestSchema(def: any, method: string, path: string, media?: string): any {
|
|
465
|
+
return jsonSchema(requestOffers(def, method, path) ?? [], media)
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
// The schema of the request body offered under the media type the guide
|
|
470
|
+
// names, of any kind, else the preferred JSON one.
|
|
471
|
+
function selectedRequestSchema(def: any, method: string, path: string, media?: string): any {
|
|
472
|
+
const offers = requestOffers(def, method, path) ?? []
|
|
473
|
+
const named = null == textOf(media) ? undefined : chooseOffer(rankOffers(offers), media)
|
|
474
|
+
return null == named ? jsonSchema(offers) : named.offer.schema
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
// The request schema a point's fields come from, read through the key a
|
|
479
|
+
// wrapped body sends the record under.
|
|
480
|
+
function requestFieldSchema(def: any, mpoint: ModelPoint, media?: string): any {
|
|
481
|
+
const declared = def?.paths?.[mpoint.o]?.[String(mpoint.m).toLowerCase()]?.requestBody
|
|
482
|
+
const schema = selectedRequestSchema(def, mpoint.m, mpoint.o, media) ??
|
|
483
|
+
(isMap(declared) ? declared.schema : undefined)
|
|
484
|
+
const wrapper = requestWrapperOf(mpoint.t?.req)
|
|
485
|
+
return null == wrapper ? schema : mergedProperties(schema)?.[wrapper] ?? schema
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
// The names of the properties a point's JSON body declares, and of the field
|
|
490
|
+
// an array body is sent from, as the entity's fields name them: false for
|
|
491
|
+
// none, undefined for no body or a body that is not JSON.
|
|
492
|
+
function bodyFields(def: any, mpoint: ModelPoint, media?: string): string[] | false | undefined {
|
|
493
|
+
const sent = chooseBody(requestOffers(def, mpoint.m, mpoint.o) ?? [], media)
|
|
494
|
+
if (null == sent ? null == requestDecl(def, mpoint.m, mpoint.o) : 'json' !== sent.kind) {
|
|
495
|
+
return undefined
|
|
496
|
+
}
|
|
497
|
+
const carrier = arrayCarrier(def, mpoint, media)?.name
|
|
498
|
+
const names = declaredProperties(requestFieldSchema(def, mpoint, media))
|
|
499
|
+
.concat(null == carrier ? [] : [carrier])
|
|
500
|
+
.map(fieldName)
|
|
501
|
+
const unique = [...new Set(names)].sort(compare)
|
|
502
|
+
return 0 < unique.length ? unique : false
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
// A body that may be one of several objects declares the properties of each.
|
|
507
|
+
function declaredProperties(schema: any, seen: Set<any> = new Set()): string[] {
|
|
508
|
+
if (!isMap(schema) || seen.has(schema)) {
|
|
509
|
+
return []
|
|
510
|
+
}
|
|
511
|
+
seen.add(schema)
|
|
512
|
+
return (isMap(schema.properties) ? Object.keys(schema.properties) : [])
|
|
513
|
+
.concat(['allOf', 'oneOf', 'anyOf'].flatMap((key) =>
|
|
514
|
+
listOf(schema[key]).flatMap((member: any) => declaredProperties(member, seen))))
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
function arrayRequestSchema(def: any, method: string, path: string, media?: string): any {
|
|
519
|
+
return arrayShape(requestSchema(def, method, path, media))
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
// An array, or an allOf whose parts make one, or a oneOf or anyOf of one, or
|
|
524
|
+
// of arrays alone, beside any null: each fact from the first part that states
|
|
525
|
+
// it, outermost first, the items from the array's own part first, and null
|
|
526
|
+
// only where the composition admits it.
|
|
527
|
+
function arrayShape(schema: any): any {
|
|
528
|
+
const parts: any[] = []
|
|
529
|
+
const visit = (node: any) => {
|
|
530
|
+
if (isMap(node) && !parts.includes(node)) {
|
|
531
|
+
parts.push(node)
|
|
532
|
+
; (Array.isArray(node.allOf) ? node.allOf : []).forEach(visit)
|
|
533
|
+
for (const one of [node.oneOf, node.anyOf]) {
|
|
534
|
+
const members = Array.isArray(one) ? one.filter((member: any) => !nullOnly(member)) : []
|
|
535
|
+
if (1 === members.length) visit(members[0])
|
|
536
|
+
else if (1 < members.length && members.every(arrayOnly)) parts.push(unionArray(members))
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
visit(schema)
|
|
541
|
+
const found = parts.find(isArray)
|
|
542
|
+
const list = null == found || null != found.type ? found : { ...found, type: 'array' }
|
|
543
|
+
if (null == list) {
|
|
544
|
+
return list
|
|
545
|
+
}
|
|
546
|
+
const items = list.items ?? parts.find((part) => null != part.items)?.items
|
|
547
|
+
const description = parts.find((part) => null != part.description)?.description
|
|
548
|
+
const types = [list.type].flat().filter((type: any) => 'null' !== type)
|
|
549
|
+
return {
|
|
550
|
+
...list,
|
|
551
|
+
type: 1 === types.length ? types[0] : types,
|
|
552
|
+
...(null == items ? {} : { items }),
|
|
553
|
+
...(null == description ? {} : { description }),
|
|
554
|
+
nullable: admitsNull(schema),
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
// Null must pass the schema's own type, const and enum, every allOf part,
|
|
560
|
+
// exactly one oneOf member, and some anyOf member.
|
|
561
|
+
function admitsNull(schema: any): boolean {
|
|
562
|
+
if (!isMap(schema)) return false
|
|
563
|
+
const own = (null == schema.type || hasType(schema, 'null') || true === schema.nullable) &&
|
|
564
|
+
(!('const' in schema) || null === schema.const) &&
|
|
565
|
+
(!Array.isArray(schema.enum) || schema.enum.includes(null))
|
|
566
|
+
return own && listOf(schema.allOf).every(admitsNull) &&
|
|
567
|
+
(!Array.isArray(schema.oneOf) || 1 === schema.oneOf.filter(admitsNull).length) &&
|
|
568
|
+
(!Array.isArray(schema.anyOf) || schema.anyOf.some(admitsNull))
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
// Only null passes: by its type, a const or enum of null alone, an allOf part
|
|
573
|
+
// that passes only null, or a oneOf or anyOf every member of which does.
|
|
574
|
+
function nullOnly(schema: any): boolean {
|
|
575
|
+
if (!isMap(schema)) return false
|
|
576
|
+
if ('const' in schema) return null === schema.const
|
|
577
|
+
if (Array.isArray(schema.enum)) return 0 < schema.enum.length && schema.enum.every((v: any) => null === v)
|
|
578
|
+
if (listOf(schema.allOf).some(nullOnly)) return true
|
|
579
|
+
if ([schema.oneOf, schema.anyOf].some((one) => Array.isArray(one) && 0 < one.length && one.every(nullOnly))) {
|
|
580
|
+
return true
|
|
581
|
+
}
|
|
582
|
+
return null != schema.type && [schema.type].flat().every((type) => 'null' === type)
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
// Only arrays pass: by a const, or an enum, of arrays alone.
|
|
587
|
+
function arrayValued(schema: any): boolean {
|
|
588
|
+
return Array.isArray(schema.const) ||
|
|
589
|
+
(Array.isArray(schema.enum) && 0 < schema.enum.length && schema.enum.every(Array.isArray))
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
function isArray(schema: any): boolean {
|
|
594
|
+
return isMap(schema) && (hasType(schema, 'array') || (null == schema.type && arrayValued(schema)))
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
// Only arrays pass, beside any null: a union member that admits a string too is
|
|
599
|
+
// no array.
|
|
600
|
+
function arrayOnly(schema: any): boolean {
|
|
601
|
+
if (!isMap(schema)) return false
|
|
602
|
+
if (arrayValued(schema)) return true
|
|
603
|
+
const types = null == schema.type ? [] : [schema.type].flat()
|
|
604
|
+
return types.includes('array') && types.every((type: any) => 'array' === type || 'null' === type)
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
// The items are kept only where every member's items name one component.
|
|
609
|
+
function unionArray(members: any[]): any {
|
|
610
|
+
const ref = members[0].items?.['x-ref']
|
|
611
|
+
const one = null != ref && members.every((member) => ref === member.items?.['x-ref'])
|
|
612
|
+
return { type: 'array', ...(one ? { items: members[0].items } : {}) }
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
// A nullable array says so with `nullable` in OpenAPI 3.0, and a type list in 3.1.
|
|
617
|
+
function nullableType(schema: any): any {
|
|
618
|
+
return true === schema.nullable && !hasType(schema, 'null') ? [schema.type, 'null'].flat() : schema.type
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
const REQDATA_FIELD_RE = /^`reqdata\.([A-Za-z_][A-Za-z0-9_]*)`$/
|
|
623
|
+
|
|
624
|
+
// The field of the request data a point's JSON array body is sent from, when
|
|
625
|
+
// its request transform unwraps one.
|
|
626
|
+
function arrayCarrier(
|
|
627
|
+
def: any,
|
|
628
|
+
mpoint: ModelPoint,
|
|
629
|
+
media?: string,
|
|
630
|
+
): { name: string, required: boolean, type: string | string[], description?: string } | undefined {
|
|
631
|
+
const req: any = mpoint.t?.req
|
|
632
|
+
const name = 'string' === typeof req ? req.match(REQDATA_FIELD_RE)?.[1] : undefined
|
|
633
|
+
const schema = null == name ? undefined : arrayRequestSchema(def, mpoint.m, mpoint.o, media)
|
|
634
|
+
if (null == name || null == schema) {
|
|
635
|
+
return undefined
|
|
636
|
+
}
|
|
637
|
+
const decl = requestDecl(def, mpoint.m, mpoint.o)
|
|
638
|
+
return {
|
|
639
|
+
name,
|
|
640
|
+
required: true === decl?.required,
|
|
641
|
+
type: nullableType(schema),
|
|
642
|
+
description: textOf(decl?.description) ?? textOf(schema.description),
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
// OpenAPI's request body, or the Swagger parameter that is one.
|
|
648
|
+
function requestDecl(def: any, method: string, path: string): any {
|
|
649
|
+
const pathdef = def?.paths?.[path]
|
|
650
|
+
const opdef = pathdef?.[String(method).toLowerCase()]
|
|
651
|
+
if (!isMap(opdef)) {
|
|
652
|
+
return undefined
|
|
653
|
+
}
|
|
654
|
+
return null != def.swagger ?
|
|
655
|
+
swaggerParams(pathdef, opdef).find((param) => 'body' === param.in) : opdef.requestBody
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
|
|
659
|
+
// Types compare by value, and a type list as the set it is, in one order.
|
|
660
|
+
function sameType(a: any, b: any): boolean {
|
|
661
|
+
return JSON.stringify(typeSet(a)) === JSON.stringify(typeSet(b))
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
|
|
665
|
+
function typeSet(type: any): any {
|
|
666
|
+
return !Array.isArray(type) ? type :
|
|
667
|
+
type.every((member: any) => 'string' === typeof member) ? [...type].sort() : type.map(typeSet)
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
|
|
418
671
|
export {
|
|
419
672
|
bodyTransform,
|
|
673
|
+
bodyFields,
|
|
674
|
+
guideMedia,
|
|
420
675
|
requestBody,
|
|
421
676
|
responseBody,
|
|
677
|
+
jsonRequestSchema,
|
|
678
|
+
requestSchema,
|
|
679
|
+
arrayRequestSchema,
|
|
680
|
+
arrayCarrier,
|
|
681
|
+
requestDecl,
|
|
682
|
+
requestFieldSchema,
|
|
683
|
+
nullableType,
|
|
684
|
+
sameType,
|
|
685
|
+
selectedRequestSchema,
|
|
422
686
|
}
|